-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: Move kernel checking to cli In order to simplify code of main.go, let us move code of kernel checking to cli. Furthermore, put kernel checking at pre-run phase of cli. * feat: Detect CAP_BPF when detect env If no capability to run bpf progs, we must check CAP_BPF asap. Without this check, there will be many noisy logs before log "error:operation not permitted", like #678 (comment). --------- Signed-off-by: Leon Hwang <[email protected]>
- Loading branch information
Showing
3 changed files
with
99 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2022 CFC4N <[email protected]>. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/cilium/ebpf" | ||
"github.com/cilium/ebpf/asm" | ||
"github.com/gojue/ecapture/pkg/util/kernel" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func detectKernel() error { | ||
// 系统内核版本检测 | ||
kv, err := kernel.HostVersion() | ||
if err != nil { | ||
return fmt.Errorf("failed to get the host kernel version: %v", err) | ||
} | ||
switch runtime.GOARCH { | ||
case "amd64": | ||
if kv < kernel.VersionCode(4, 18, 0) { | ||
return fmt.Errorf("the Linux/Android Kernel version %v (x86_64) is not supported. Requires a version greater than 4.18.", kv) | ||
} | ||
case "arm64": | ||
if kv < kernel.VersionCode(5, 5, 0) { | ||
return fmt.Errorf("the Linux/Android Kernel version %v (aarch64) is not supported. Requires a version greater than 5.5.", kv) | ||
} | ||
default: | ||
return fmt.Errorf("unsupported CPU arch:%v", runtime.GOARCH) | ||
} | ||
|
||
return nil | ||
} | ||
func detectBpfCap() error { | ||
// BPF 权限检测 | ||
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{ | ||
Name: "uprobe_dummy", | ||
Type: ebpf.Kprobe, | ||
Instructions: asm.Instructions{ | ||
asm.Mov.Imm(asm.R0, 0), | ||
asm.Return(), | ||
}, | ||
License: "GPL", | ||
}) | ||
if err != nil { | ||
if errors.Is(err, unix.EPERM) { | ||
return fmt.Errorf("the current user does not have CAP_BPF to load bpf programs. Please run as root or use sudo or add the --privileged=true flag for Docker.") | ||
} | ||
|
||
return fmt.Errorf("failed to create bpf program: %v", err) | ||
} | ||
defer prog.Close() | ||
|
||
return nil | ||
} | ||
|
||
func detectEnv() error { | ||
// 环境检测 | ||
|
||
if err := detectKernel(); err != nil { | ||
return err | ||
} | ||
|
||
if err := detectBpfCap(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters