forked from abcz316/SKRoot-linuxKernelRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_cmdline_utils.h
More file actions
203 lines (188 loc) · 4.93 KB
/
process_cmdline_utils.h
File metadata and controls
203 lines (188 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef PROCESS_CMDLINE_UTILS_H_
#define PROCESS_CMDLINE_UTILS_H_
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <vector>
#include <time.h>
#include "kernel_root_helper.h"
#include "kernel_root_key.h"
static ssize_t find_all_cmdline_process(const char* str_root_key, const char* target_cmdline, std::vector<pid_t> & vOut)
{
int pid;
DIR* dir;
FILE *fp;
char filename[32];
char cmdline[256];
struct dirent * entry;
vOut.clear();
if (kernel_root::get_root(str_root_key) != 0) {
return -1000001;
}
dir = opendir("/proc");
if (dir == NULL) {
return -1000002;
}
while ((entry = readdir(dir)) != NULL) {
// 如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
continue;
}
else if (entry->d_type != DT_DIR) {
continue;
}
else if (strspn(entry->d_name, "1234567890") != strlen(entry->d_name)) {
continue;
}
pid = atoi(entry->d_name);
if (pid != 0) {
sprintf(filename, "/proc/%d/cmdline", pid);
fp = fopen(filename, "r");
if (fp) {
fgets(cmdline, sizeof(cmdline), fp);
fclose(fp);
//TRACE("[+] find %d process cmdline: %s\n", id, cmdline);
if (strstr(cmdline, target_cmdline)) {
/* process found */
vOut.push_back(pid);
}
}
}
}
closedir(dir);
return 0;
}
static ssize_t safe_find_all_cmdline_process(const char* str_root_key, const char* target_cmdline, std::vector<pid_t> & vOut)
{
fork_pipe_info finfo;
if(fork_pipe_child_process(finfo)) {
ssize_t ret = find_all_cmdline_process(str_root_key, target_cmdline, vOut);
std::vector<int> vec_pid;
for (pid_t t : vOut) {
vec_pid.push_back(t);
}
write_errcode_to_father(finfo, ret);
write_vec_int_to_father(finfo, vec_pid);
_exit(0);
return 0;
}
ssize_t err = 0;
if(!wait_fork_child_process(finfo)) {
err = -1000011;
} else {
std::vector<int> vec_pid;
if(!read_errcode_from_child(finfo, err)) {
err = -1000012;
} else if(!read_vec_int_from_child(finfo, vec_pid)) {
err = -1000013;
}
for (int pid : vec_pid) {
vOut.push_back(pid);
}
}
return err;
}
static ssize_t wait_and_find_cmdline_process(const char* str_root_key, const char* target_cmdline, int timeout, pid_t & pid)
{
int old_pid;
int fd;
size_t length;
DIR* dir;
char filename[32];
char arg_list[1024];
char* next_arg;
std::string all_cmdline;
struct dirent * entry;
pid = 0;
if (kernel_root::get_root(str_root_key) != 0) {
return -1000021;
}
clock_t start = clock();
while (1) {
sleep(0);
dir = opendir("/proc");
if (dir == NULL) {
return -1000022;
}
while ((entry = readdir(dir)) != NULL) {
// 如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
continue;
}
else if (entry->d_type != DT_DIR) {
continue;
}
else if (strspn(entry->d_name, "1234567890") != strlen(entry->d_name)) {
continue;
}
old_pid = atoi(entry->d_name);
if (old_pid != 0) {
sprintf(filename, "/proc/%d/cmdline", old_pid);
fd = open(filename, O_RDONLY);
if (fd >= 0) {
length = read (fd, arg_list, sizeof (arg_list));
close(fd);
/* read does not NUL-terminate the buffer, so do it here. */
arg_list[length] = '\0';
next_arg = arg_list;
while (next_arg < arg_list + length) {
/* Print the argument. Each is NUL-terminated,
* so just treat it like an ordinary string.
*/
//
if(!all_cmdline.empty()) {
all_cmdline += " ";
}
all_cmdline+=next_arg;
/* Advance to the next argument. Since each argument is NUL-terminated,
* strlen counts the length of the next argument, not the entire argument list.
*/
next_arg += strlen (next_arg) + 1;
}
//printf("[+] find %d process arg: %s\n", old_pid, all_cmdline.c_str());
if(all_cmdline.find(target_cmdline) != -1) {
/* process found */
pid = old_pid;
break;
}
}
}
}
closedir(dir);
if (pid > 0) {
break;
}
clock_t finish = clock();
double total_time = (double)(finish - start) / CLOCKS_PER_SEC;
if(total_time >= timeout) {
break;
}
}
return pid > 0 ? 0 : -1000023;
}
static ssize_t safe_wait_and_find_cmdline_process(const char* str_root_key, const char* target_cmdline, int timeout, pid_t &pid)
{
fork_pipe_info finfo;
if(fork_pipe_child_process(finfo)) {
pid_t pid;
ssize_t ret = wait_and_find_cmdline_process(str_root_key, target_cmdline, timeout, pid);
write_errcode_to_father(finfo, ret);
write_int_to_father(finfo, pid);
_exit(0);
return 0;
}
ssize_t err = 0;
if(!wait_fork_child_process(finfo)) {
err = -1000031;
} else {
if(!read_errcode_from_child(finfo, err)) {
err = -1000032;
} else if(!read_int_from_child(finfo, pid)) {
err = -1000033;
}
}
return err;
}
#endif /* PROCESS_CMDLINE_UTILS_H_ */