aboutsummaryrefslogtreecommitdiff
path: root/procps
diff options
context:
space:
mode:
Diffstat (limited to 'procps')
-rw-r--r--procps/fuser.c217
-rw-r--r--procps/sysctl.c3
2 files changed, 120 insertions, 100 deletions
diff --git a/procps/fuser.c b/procps/fuser.c
index c91ae217b..40789dd5e 100644
--- a/procps/fuser.c
+++ b/procps/fuser.c
@@ -34,19 +34,19 @@ static int fuser_option(char *option)
34{ 34{
35 int opt = 0; 35 int opt = 0;
36 36
37 if(!(strlen(option))) return 0; 37 if (!option[0])
38 if(option[0] != '-') return 0; 38 return 0;
39 if (option[0] != '-')
40 return 0;
39 ++option; 41 ++option;
40 while(*option != '\0') { 42 while (*option != '\0') {
41 if(*option == 'm') opt |= FUSER_OPT_MOUNT; 43 if (*option == 'm') opt |= FUSER_OPT_MOUNT;
42 else if(*option == 'k') opt |= FUSER_OPT_KILL; 44 else if (*option == 'k') opt |= FUSER_OPT_KILL;
43 else if(*option == 's') opt |= FUSER_OPT_SILENT; 45 else if (*option == 's') opt |= FUSER_OPT_SILENT;
44 else if(*option == '6') opt |= FUSER_OPT_IP6; 46 else if (*option == '6') opt |= FUSER_OPT_IP6;
45 else if(*option == '4') opt |= FUSER_OPT_IP4; 47 else if (*option == '4') opt |= FUSER_OPT_IP4;
46 else { 48 else
47 bb_error_msg_and_die( 49 bb_error_msg_and_die("unsupported option '%c'", *option);
48 "Unsupported option '%c'", *option);
49 }
50 ++option; 50 ++option;
51 } 51 }
52 return opt; 52 return opt;
@@ -56,7 +56,8 @@ static int fuser_file_to_dev_inode(const char *filename,
56 dev_t *dev, ino_t *inode) 56 dev_t *dev, ino_t *inode)
57{ 57{
58 struct stat f_stat; 58 struct stat f_stat;
59 if((stat(filename, &f_stat)) < 0) return 0; 59 if ((stat(filename, &f_stat)) < 0)
60 return 0;
60 *inode = f_stat.st_ino; 61 *inode = f_stat.st_ino;
61 *dev = f_stat.st_dev; 62 *dev = f_stat.st_dev;
62 return 1; 63 return 1;
@@ -68,7 +69,7 @@ static int fuser_find_socket_dev(dev_t *dev)
68 struct stat buf; 69 struct stat buf;
69 70
70 if (fd >= 0 && (fstat(fd, &buf)) == 0) { 71 if (fd >= 0 && (fstat(fd, &buf)) == 0) {
71 *dev = buf.st_dev; 72 *dev = buf.st_dev;
72 close(fd); 73 close(fd);
73 return 1; 74 return 1;
74 } 75 }
@@ -80,9 +81,11 @@ static int fuser_parse_net_arg(const char *filename,
80{ 81{
81 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5]; 82 char path[sizeof(FUSER_PROC_DIR)+12], tproto[5];
82 83
83 if((sscanf(filename, "%d/%4s", port, tproto)) != 2) return 0; 84 if ((sscanf(filename, "%d/%4s", port, tproto)) != 2)
84 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, tproto); 85 return 0;
85 if((access(path, R_OK)) != 0) return 0; 86 sprintf(path, FUSER_PROC_DIR "/net/%s", tproto);
87 if ((access(path, R_OK)) != 0)
88 return 0;
86 *proto = xstrdup(tproto); 89 *proto = xstrdup(tproto);
87 return 1; 90 return 1;
88} 91}
@@ -91,17 +94,19 @@ static int fuser_add_pid(pid_list *plist, pid_t pid)
91{ 94{
92 pid_list *curr = NULL, *last = NULL; 95 pid_list *curr = NULL, *last = NULL;
93 96
94 if(plist->pid == 0) plist->pid = pid; 97 if (plist->pid == 0)
98 plist->pid = pid;
95 curr = plist; 99 curr = plist;
96 while(curr != NULL) { 100 while (curr != NULL) {
97 if(curr->pid == pid) return 1; 101 if (curr->pid == pid)
102 return 1;
98 last = curr; 103 last = curr;
99 curr = curr->next; 104 curr = curr->next;
100 } 105 }
101 curr = xmalloc(sizeof(pid_list)); 106 curr = xzalloc(sizeof(pid_list));
102 last->next = curr; 107 last->next = curr;
103 curr->pid = pid; 108 curr->pid = pid;
104 curr->next = NULL; 109 /*curr->next = NULL;*/
105 return 1; 110 return 1;
106} 111}
107 112
@@ -109,21 +114,22 @@ static int fuser_add_inode(inode_list *ilist, dev_t dev, ino_t inode)
109{ 114{
110 inode_list *curr = NULL, *last = NULL; 115 inode_list *curr = NULL, *last = NULL;
111 116
112 if(!ilist->inode && !ilist->dev) { 117 if (!ilist->inode && !ilist->dev) {
113 ilist->dev = dev; 118 ilist->dev = dev;
114 ilist->inode = inode; 119 ilist->inode = inode;
115 } 120 }
116 curr = ilist; 121 curr = ilist;
117 while(curr != NULL) { 122 while (curr != NULL) {
118 if(curr->inode == inode && curr->dev == dev) return 1; 123 if (curr->inode == inode && curr->dev == dev)
124 return 1;
119 last = curr; 125 last = curr;
120 curr = curr->next; 126 curr = curr->next;
121 } 127 }
122 curr = xmalloc(sizeof(inode_list)); 128 curr = xzalloc(sizeof(inode_list));
123 last->next = curr; 129 last->next = curr;
124 curr->dev = dev; 130 curr->dev = dev;
125 curr->inode = inode; 131 curr->inode = inode;
126 curr->next = NULL; 132 /*curr->next = NULL;*/
127 return 1; 133 return 1;
128} 134}
129 135
@@ -134,29 +140,31 @@ static int fuser_scan_proc_net(int opts, const char *proto,
134 char addr[128]; 140 char addr[128];
135 ino_t tmp_inode; 141 ino_t tmp_inode;
136 dev_t tmp_dev; 142 dev_t tmp_dev;
137 long long uint64_inode; 143 long long uint64_inode;
138 int tmp_port; 144 int tmp_port;
139 FILE *f; 145 FILE *f;
140 146
141 if(!fuser_find_socket_dev(&tmp_dev)) tmp_dev = 0; 147 if (!fuser_find_socket_dev(&tmp_dev))
142 sprintf(path, "%s/net/%s", FUSER_PROC_DIR, proto); 148 tmp_dev = 0;
143 149 sprintf(path, FUSER_PROC_DIR "/net/%s", proto);
144 if (!(f = fopen(path, "r"))) return 0; 150
145 while(fgets(line, FUSER_MAX_LINE, f)) { 151 f = fopen(path, "r");
146 if(sscanf(line, 152 if (!f)
147 "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x " 153 return 0;
148 "%*x:%*x %*x %*d %*d %llu", 154 while (fgets(line, FUSER_MAX_LINE, f)) {
149 addr, &tmp_port, &uint64_inode) == 3) { 155 if (sscanf(line, "%*d: %64[0-9A-Fa-f]:%x %*x:%*x %*x %*x:%*x "
150 if((strlen(addr) == 8) && 156 "%*x:%*x %*x %*d %*d %llu",
151 (opts & FUSER_OPT_IP6)) continue; 157 addr, &tmp_port, &uint64_inode) == 3
152 else if((strlen(addr) > 8) && 158 ) {
153 (opts & FUSER_OPT_IP4)) continue; 159 if (strlen(addr) == 8 && (opts & FUSER_OPT_IP6))
154 if(tmp_port == port) { 160 continue;
161 if (strlen(addr) > 8 && (opts & FUSER_OPT_IP4))
162 continue;
163 if (tmp_port == port) {
155 tmp_inode = uint64_inode; 164 tmp_inode = uint64_inode;
156 fuser_add_inode(ilist, tmp_dev, tmp_inode); 165 fuser_add_inode(ilist, tmp_dev, tmp_inode);
157 } 166 }
158 } 167 }
159
160 } 168 }
161 fclose(f); 169 fclose(f);
162 return 1; 170 return 1;
@@ -168,10 +176,10 @@ static int fuser_search_dev_inode(int opts, inode_list *ilist,
168 inode_list *curr; 176 inode_list *curr;
169 curr = ilist; 177 curr = ilist;
170 178
171 while(curr) { 179 while (curr) {
172 if((opts & FUSER_OPT_MOUNT) && curr->dev == dev) 180 if ((opts & FUSER_OPT_MOUNT) && curr->dev == dev)
173 return 1; 181 return 1;
174 if(curr->inode == inode && curr->dev == dev) 182 if (curr->inode == inode && curr->dev == dev)
175 return 1; 183 return 1;
176 curr = curr->next; 184 curr = curr->next;
177 } 185 }
@@ -188,17 +196,19 @@ static int fuser_scan_pid_maps(int opts, const char *fname, pid_t pid,
188 long long uint64_inode; 196 long long uint64_inode;
189 dev_t dev; 197 dev_t dev;
190 198
191 if (!(file = fopen(fname, "r"))) return 0; 199 file = fopen(fname, "r");
200 if (!file)
201 return 0;
192 while (fgets(line, FUSER_MAX_LINE, file)) { 202 while (fgets(line, FUSER_MAX_LINE, file)) {
193 if(sscanf(line, "%*s %*s %*s %x:%x %llu", 203 if (sscanf(line, "%*s %*s %*s %x:%x %llu", &major, &minor, &uint64_inode) != 3)
194 &major, &minor, &uint64_inode) != 3) continue; 204 continue;
195 inode = uint64_inode; 205 inode = uint64_inode;
196 if(major == 0 && minor == 0 && inode == 0) continue; 206 if (major == 0 && minor == 0 && inode == 0)
207 continue;
197 dev = makedev(major, minor); 208 dev = makedev(major, minor);
198 if(fuser_search_dev_inode(opts, ilist, dev, inode)) { 209 if (fuser_search_dev_inode(opts, ilist, dev, inode)) {
199 fuser_add_pid(plist, pid); 210 fuser_add_pid(plist, pid);
200 } 211 }
201
202 } 212 }
203 fclose(file); 213 fclose(file);
204 return 1; 214 return 1;
@@ -210,8 +220,9 @@ static int fuser_scan_link(int opts, const char *lname, pid_t pid,
210 ino_t inode; 220 ino_t inode;
211 dev_t dev; 221 dev_t dev;
212 222
213 if(!fuser_file_to_dev_inode(lname, &dev, &inode)) return 0; 223 if (!fuser_file_to_dev_inode(lname, &dev, &inode))
214 if(fuser_search_dev_inode(opts, ilist, dev, inode)) 224 return 0;
225 if (fuser_search_dev_inode(opts, ilist, dev, inode))
215 fuser_add_pid(plist, pid); 226 fuser_add_pid(plist, pid);
216 return 1; 227 return 1;
217} 228}
@@ -223,19 +234,18 @@ static int fuser_scan_dir_links(int opts, const char *dname, pid_t pid,
223 struct dirent *de; 234 struct dirent *de;
224 char *lname; 235 char *lname;
225 236
226 if((d = opendir(dname))) { 237 d = opendir(dname);
227 while((de = readdir(d)) != NULL) { 238 if (!d)
228 lname = concat_subpath_file(dname, de->d_name); 239 return 0;
229 if(lname == NULL) 240 while ((de = readdir(d)) != NULL) {
230 continue; 241 lname = concat_subpath_file(dname, de->d_name);
231 fuser_scan_link(opts, lname, pid, ilist, plist); 242 if (lname == NULL)
232 free(lname); 243 continue;
233 } 244 fuser_scan_link(opts, lname, pid, ilist, plist);
234 closedir(d); 245 free(lname);
235 } 246 }
236 else return 0; 247 closedir(d);
237 return 1; 248 return 1;
238
239} 249}
240 250
241static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist) 251static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
@@ -245,12 +255,15 @@ static int fuser_scan_proc_pids(int opts, inode_list *ilist, pid_list *plist)
245 pid_t pid; 255 pid_t pid;
246 char *dname; 256 char *dname;
247 257
248 if(!(d = opendir(FUSER_PROC_DIR))) return 0; 258 d = opendir(FUSER_PROC_DIR);
249 while((de = readdir(d)) != NULL) { 259 if (!d)
260 return 0;
261 while ((de = readdir(d)) != NULL) {
250 pid = (pid_t)atoi(de->d_name); 262 pid = (pid_t)atoi(de->d_name);
251 if(!pid) continue; 263 if (!pid)
264 continue;
252 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name); 265 dname = concat_subpath_file(FUSER_PROC_DIR, de->d_name);
253 if(chdir(dname) < 0) { 266 if (chdir(dname) < 0) {
254 free(dname); 267 free(dname);
255 continue; 268 continue;
256 } 269 }
@@ -272,9 +285,11 @@ static int fuser_print_pid_list(pid_list *plist)
272{ 285{
273 pid_list *curr = plist; 286 pid_list *curr = plist;
274 287
275 if(plist == NULL) return 0; 288 if (plist == NULL)
276 while(curr != NULL) { 289 return 0;
277 if(curr->pid > 0) printf("%d ", curr->pid); 290 while (curr != NULL) {
291 if (curr->pid > 0)
292 printf("%d ", curr->pid);
278 curr = curr->next; 293 curr = curr->next;
279 } 294 }
280 puts(""); 295 puts("");
@@ -287,12 +302,12 @@ static int fuser_kill_pid_list(pid_list *plist, int sig)
287 pid_t mypid = getpid(); 302 pid_t mypid = getpid();
288 int success = 1; 303 int success = 1;
289 304
290 if(plist == NULL) return 0; 305 if (plist == NULL)
291 while(curr != NULL) { 306 return 0;
292 if(curr->pid > 0 && curr->pid != mypid) { 307 while (curr != NULL) {
308 if (curr->pid > 0 && curr->pid != mypid) {
293 if (kill(curr->pid, sig) != 0) { 309 if (kill(curr->pid, sig) != 0) {
294 bb_perror_msg( 310 bb_perror_msg("kill pid '%d'", curr->pid);
295 "cannot kill pid '%d'", curr->pid);
296 success = 0; 311 success = 0;
297 } 312 }
298 } 313 }
@@ -304,11 +319,12 @@ static int fuser_kill_pid_list(pid_list *plist, int sig)
304int fuser_main(int argc, char **argv); 319int fuser_main(int argc, char **argv);
305int fuser_main(int argc, char **argv) 320int fuser_main(int argc, char **argv)
306{ 321{
322 /*static -- huh???*/ int opt = 0; /* FUSER_OPT_ */
323
307 int port, i, optn; 324 int port, i, optn;
308 int* fni; /* file name indexes of argv */ 325 int* fni; /* file name indexes of argv */
309 int fnic = 0; /* file name index count */ 326 int fnic = 0; /* file name index count */
310 const char *proto; 327 const char *proto;
311 static int opt = 0; /* FUSER_OPT_ */
312 dev_t dev; 328 dev_t dev;
313 ino_t inode; 329 ino_t inode;
314 pid_list *pids; 330 pid_list *pids;
@@ -320,30 +336,31 @@ int fuser_main(int argc, char **argv)
320 bb_show_usage(); 336 bb_show_usage();
321 337
322 fni = xmalloc(sizeof(int)); 338 fni = xmalloc(sizeof(int));
323 for (i=1;i<argc;i++) { 339 for (i = 1; i < argc; i++) {
324 optn = fuser_option(argv[i]); 340 optn = fuser_option(argv[i]);
325 if(optn) opt |= optn; 341 if (optn)
326 else if(argv[i][0] == '-') { 342 opt |= optn;
343 else if (argv[i][0] == '-') {
327 killsig = get_signum(argv[i]+1); 344 killsig = get_signum(argv[i]+1);
328 if(0 > killsig) 345 if (killsig < 0)
329 killsig = SIGTERM; 346 killsig = SIGTERM;
330 } 347 } else {
331 else {
332 fni = xrealloc(fni, sizeof(int) * (fnic+2)); 348 fni = xrealloc(fni, sizeof(int) * (fnic+2));
333 fni[fnic++] = i; 349 fni[fnic++] = i;
334 } 350 }
335 } 351 }
336 if(!fnic) return 1; 352
353 if (!fnic)
354 return 1;
337 355
338 inodes = xmalloc(sizeof(inode_list)); 356 inodes = xmalloc(sizeof(inode_list));
339 for (i=0;i<fnic;i++) { 357 for (i = 0; i < fnic; i++) {
340 if(fuser_parse_net_arg(argv[fni[i]], &proto, &port)) { 358 if (fuser_parse_net_arg(argv[fni[i]], &proto, &port)) {
341 fuser_scan_proc_net(opt, proto, port, inodes); 359 fuser_scan_proc_net(opt, proto, port, inodes);
342 } 360 } else {
343 else { 361 if (!fuser_file_to_dev_inode(argv[fni[i]], &dev, &inode)) {
344 if(!fuser_file_to_dev_inode( 362 if (ENABLE_FEATURE_CLEAN_UP)
345 argv[fni[i]], &dev, &inode)) { 363 free(inodes);
346 if (ENABLE_FEATURE_CLEAN_UP) free(inodes);
347 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]); 364 bb_perror_msg_and_die("cannot open '%s'", argv[fni[i]]);
348 } 365 }
349 fuser_add_inode(inodes, dev, inode); 366 fuser_add_inode(inodes, dev, inode);
@@ -352,17 +369,19 @@ int fuser_main(int argc, char **argv)
352 pids = xmalloc(sizeof(pid_list)); 369 pids = xmalloc(sizeof(pid_list));
353 success = fuser_scan_proc_pids(opt, inodes, pids); 370 success = fuser_scan_proc_pids(opt, inodes, pids);
354 /* if the first pid in the list is 0, none have been found */ 371 /* if the first pid in the list is 0, none have been found */
355 if(pids->pid == 0) success = 0; 372 if (pids->pid == 0)
356 if(success) { 373 success = 0;
357 if(opt & FUSER_OPT_KILL) { 374 if (success) {
375 if (opt & FUSER_OPT_KILL) {
358 success = fuser_kill_pid_list(pids, killsig); 376 success = fuser_kill_pid_list(pids, killsig);
359 } 377 } else if (!(opt & FUSER_OPT_SILENT)) {
360 else if(!(opt & FUSER_OPT_SILENT)) {
361 success = fuser_print_pid_list(pids); 378 success = fuser_print_pid_list(pids);
362 } 379 }
363 } 380 }
364 free(pids); 381 if (ENABLE_FEATURE_CLEAN_UP) {
365 free(inodes); 382 free(pids);
383 free(inodes);
384 }
366 /* return 0 on (success == 1) 1 otherwise */ 385 /* return 0 on (success == 1) 1 otherwise */
367 return (success != 1); 386 return (success != 1);
368} 387}
diff --git a/procps/sysctl.c b/procps/sysctl.c
index b5a01894f..7c72ac933 100644
--- a/procps/sysctl.c
+++ b/procps/sysctl.c
@@ -202,7 +202,8 @@ int sysctl_write_setting(const char *setting, int output)
202 while ((cptr = strchr(outname, '/')) != NULL) 202 while ((cptr = strchr(outname, '/')) != NULL)
203 *cptr = '.'; 203 *cptr = '.';
204 204
205 if ((fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { 205 fd = open(tmpname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
206 if (fd < 0) {
206 switch (errno) { 207 switch (errno) {
207 case ENOENT: 208 case ENOENT:
208 bb_error_msg(ERR_INVALID_KEY, outname); 209 bb_error_msg(ERR_INVALID_KEY, outname);