GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — linuxKernelRoot/testRoot/testRoot.h at master · apachecn/linuxKernelRoot Expand file tree/ Copy pathtestRoot.hMore file actions 255 lines (219 loc) · 5.62 KB / Copy pathtestRoot.hFile metadata and controls255 lines (219 loc) · 5.62 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 #ifndef TEST_ROOT_H_ #define TEST_ROOT_H_ #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <vector> #include "kernel_root_helper.h" //安静输出模式 #define QUIET_PRINTF #ifdef QUIET_PRINTF #undef TRACE #define TRACE(fmt, ...) #else #ifdef __ANDROID__ #include <android/log.h> #define LOG_TAG "JNIGlue" //#define TRACE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) #define TRACE(fmt, ...) printf(fmt, ##__VA_ARGS__) #else #define TRACE(fmt, ...) printf(fmt, ##__VA_ARGS__) #endif #endif static size_t get_executable_path(char* processdir, char* processname, size_t len) { char* path_end; if (readlink("/proc/self/exe", processdir, len) <= 0) { return -1; } path_end = strrchr(processdir, '/'); if (path_end == NULL) { return -1; } ++path_end; strcpy(processname, path_end); *path_end = '\0'; return (size_t)(path_end - processdir); } static int find_all_cmdline_process(unsigned int root_key, const char* target_cmdline, std::vector<pid_t> & vOut) { int id; DIR* dir; FILE *fp; char filename[32]; char cmdline[256]; struct dirent * entry; if (get_root(root_key) != 0) { return -1; } if (!is_disable_selinux_status()) { //要关掉SELinux才能找到进程PID if (disable_selinux(root_key) != 0) { return -2; } } dir = opendir("/proc"); if (dir == NULL) return -3; 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; } id = atoi(entry->d_name); if (id != 0) { sprintf(filename, "/proc/%d/cmdline", id); 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(id); } } } } closedir(dir); return 0; } static int safe_find_all_cmdline_process(unsigned int root_key, const char* target_cmdline, std::vector<pid_t> & vOut) { int fd[2]; if (pipe(fd)) { return -1000; } pid_t pid; if ((pid = fork()) < 0) { //fork error return -1001; } else if (pid == 0) { /* 子进程 */ close(fd[0]); //close read pipe int ret = find_all_cmdline_process(root_key, target_cmdline, vOut); write(fd[1], &ret, sizeof(ret)); size_t cnt = vOut.size(); write(fd[1], &cnt, sizeof(cnt)); for (pid_t t : vOut) { write(fd[1], &t, sizeof(t)); } close(fd[1]); //close write pipe force_kill_myself(); } else { /*父进程*/ close(fd[1]); //close write pipe int status; /* 等待目标进程停止或终止. WUNTRACED - 解释见参考手册 */ if (waitpid(pid, &status, WUNTRACED) < 0 && errno != EACCES) { return -6; } int ret = -1002; read(fd[0], (void*)&ret, sizeof(ret)); size_t cnt = 0; read(fd[0], (void*)&cnt, sizeof(cnt)); for (size_t i = 0; i < cnt; i++) { pid_t t; read(fd[0], (void*)&t, sizeof(t)); vOut.push_back(t); } close(fd[0]); //close read pipe return ret; } return -1003; } static int wait_and_find_cmdline_process(unsigned int root_key, const char* target_cmdline) { int id; pid_t pid = -1; DIR* dir; FILE *fp; char filename[32]; char cmdline[256]; struct dirent * entry; if (get_root(root_key) != 0) { return -1; } if (!is_disable_selinux_status()) { //要关掉SELinux才能找到进程PID if (disable_selinux(root_key) != 0) { return -2; } } while (1) { sleep(0); dir = opendir("/proc"); if (dir == NULL) return -3; 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; } id = atoi(entry->d_name); if (id != 0) { sprintf(filename, "/proc/%d/cmdline", id); 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 */ pid = id; break; } } } } closedir(dir); if (pid <= 0) { continue; } break; } return pid; } static int safe_wait_and_find_cmdline_process(unsigned int root_key, const char* target_cmdline) { int fd[2]; if (pipe(fd)) { return -1000; } pid_t pid; if ((pid = fork()) < 0) { //fork error return -1001; } else if (pid == 0) { /* 子进程 */ close(fd[0]); //close read pipe int ret = wait_and_find_cmdline_process(root_key, target_cmdline); write(fd[1], &ret, sizeof(ret)); close(fd[1]); //close write pipe force_kill_myself(); } else { /*父进程*/ close(fd[1]); //close write pipe int status; /* 等待目标进程停止或终止. WUNTRACED - 解释见参考手册 */ if (waitpid(pid, &status, WUNTRACED) < 0 && errno != EACCES) { return -6; } int ret = -1002; read(fd[0], (void*)&ret, sizeof(ret)); close(fd[0]); //close read pipe return ret; } return -1003; } #endif /* TEST_ROOT_H_ */ |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |