diff options
author | We-unite <3205135446@qq.com> | 2024-07-19 17:02:11 +0800 |
---|---|---|
committer | We-unite <3205135446@qq.com> | 2024-07-19 17:05:29 +0800 |
commit | 7cf8e470471d30fc821a8be350dcb97dc64e5add (patch) | |
tree | e747afbc698bba079047f17674eef260a4c6733d /src/basefunc.go | |
parent | 2e37dcf708be08dff963ee08cb7a9557f32e690d (diff) | |
download | godo-7cf8e470471d30fc821a8be350dcb97dc64e5add.tar.gz godo-7cf8e470471d30fc821a8be350dcb97dc64e5add.zip |
Depart the whole program into several files.
Put all the src code in only one file is to ugly, so devide it!
and mv them into src dir to keep the whole repo clear.
Diffstat (limited to 'src/basefunc.go')
-rw-r--r-- | src/basefunc.go | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/basefunc.go b/src/basefunc.go new file mode 100644 index 0000000..5fff3e8 --- /dev/null +++ b/src/basefunc.go | |||
@@ -0,0 +1,115 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "bufio" | ||
5 | "fmt" | ||
6 | "os" | ||
7 | "path/filepath" | ||
8 | "regexp" | ||
9 | "strconv" | ||
10 | "strings" | ||
11 | "time" | ||
12 | ) | ||
13 | |||
14 | func figureOutSyscalls() error { | ||
15 | NRRegex := regexp.MustCompile(`#define __NR_(.*?) (\d+)$`) | ||
16 | file, err := os.Open("/usr/include/asm/unistd_64.h") | ||
17 | if err != nil { | ||
18 | return err | ||
19 | } | ||
20 | defer file.Close() | ||
21 | |||
22 | scanner := bufio.NewScanner(file) | ||
23 | for scanner.Scan() { | ||
24 | line := scanner.Text() | ||
25 | if NRRegex.MatchString(line) { | ||
26 | match := NRRegex.FindStringSubmatch(line) | ||
27 | num, err := strconv.Atoi(match[2]) | ||
28 | if err != nil { | ||
29 | return err | ||
30 | } | ||
31 | syscallTable[num] = match[1] | ||
32 | } | ||
33 | } | ||
34 | return nil | ||
35 | } | ||
36 | |||
37 | func getPid() (int, error) { | ||
38 | // 指定要搜索的关键词 | ||
39 | keyword := "/usr/bin/containerd" | ||
40 | |||
41 | // 获取/proc目录下的所有子目录 | ||
42 | procDir, err := filepath.Glob("/proc/*") | ||
43 | if err != nil { | ||
44 | return 0, err | ||
45 | } | ||
46 | |||
47 | // 遍历子目录,查找包含关键词的进程 | ||
48 | for _, dir := range procDir { | ||
49 | pid, err := strconv.Atoi(filepath.Base(dir)) | ||
50 | if err != nil { | ||
51 | continue // 跳过非PID的目录 | ||
52 | } | ||
53 | |||
54 | // 检查进程是否包含关键词 | ||
55 | if containsKeyword(pid, keyword) { | ||
56 | return pid, nil | ||
57 | } | ||
58 | } | ||
59 | err = fmt.Errorf("Error: no containerd process found.") | ||
60 | return 0, err | ||
61 | } | ||
62 | |||
63 | func containsKeyword(pid int, keyword string) bool { | ||
64 | // 构造完整的进程命令路径 | ||
65 | cmdPath := fmt.Sprintf("/proc/%d/cmdline", pid) | ||
66 | |||
67 | // 打开文件 | ||
68 | file, err := os.Open(cmdPath) | ||
69 | if err != nil { | ||
70 | return false | ||
71 | } | ||
72 | defer file.Close() | ||
73 | |||
74 | // 读取文件内容 | ||
75 | scanner := bufio.NewScanner(file) | ||
76 | scanner.Split(bufio.ScanLines) | ||
77 | for scanner.Scan() { | ||
78 | line := scanner.Text() | ||
79 | if strings.Contains(line, keyword) { | ||
80 | return true | ||
81 | } | ||
82 | } | ||
83 | return false | ||
84 | } | ||
85 | |||
86 | func getTimeFromStr(timeStr string) (time.Time, error) { | ||
87 | timestampFloat, err := strconv.ParseFloat(timeStr, 64) | ||
88 | if err != nil { | ||
89 | return time.Unix(0, 0), err | ||
90 | } | ||
91 | secs := int64(timestampFloat) | ||
92 | nsecs := int64((timestampFloat - float64(secs)) * 1e9) | ||
93 | |||
94 | // 只精确到毫秒就够了 | ||
95 | t := time.Unix(secs, nsecs).Truncate(time.Millisecond) | ||
96 | return t, nil | ||
97 | } | ||
98 | |||
99 | func hexToAscii(hexString string) string { | ||
100 | bytes := []byte{} | ||
101 | for i := 0; i < len(hexString); i += 2 { | ||
102 | hexPair := hexString[i : i+2] | ||
103 | // 将十六进制数转换为十进制数 | ||
104 | decimal, err := strconv.ParseInt(hexPair, 16, 8) | ||
105 | if err != nil { | ||
106 | return "Invalid hex string" | ||
107 | } | ||
108 | char := byte(decimal) | ||
109 | bytes = append(bytes, char) | ||
110 | } | ||
111 | |||
112 | asciiString := strings.ReplaceAll(string(bytes), "\000", " ") | ||
113 | |||
114 | return asciiString | ||
115 | } | ||