Mercurial > hg > toybox
comparison toys/pidof.c @ 475:1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 18 Feb 2012 15:12:41 -0600 |
parents | d76583999029 |
children | f0b07ce5f125 |
comparison
equal
deleted
inserted
replaced
474:d76583999029 | 475:1fb149e75ebf |
---|---|
5 * Copyright 2012 Andreas Heck <aheck@gmx.de> | 5 * Copyright 2012 Andreas Heck <aheck@gmx.de> |
6 * | 6 * |
7 * Not in SUSv4. | 7 * Not in SUSv4. |
8 * See http://opengroup.org/onlinepubs/9699919799/utilities/ | 8 * See http://opengroup.org/onlinepubs/9699919799/utilities/ |
9 | 9 |
10 USE_PIDOF(NEWTOY(pidof, "e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) | 10 USE_PIDOF(NEWTOY(pidof, "", TOYFLAG_USR|TOYFLAG_BIN)) |
11 | 11 |
12 config PIDOF | 12 config PIDOF |
13 bool "pidof" | 13 bool "pidof" |
14 default y | 14 default y |
15 help | 15 help |
18 Print the PIDs of all processes with the given names. | 18 Print the PIDs of all processes with the given names. |
19 */ | 19 */ |
20 | 20 |
21 #include "toys.h" | 21 #include "toys.h" |
22 | 22 |
23 DEFINE_GLOBALS( | |
24 int matched; | |
25 ) | |
23 #define TT this.pidof | 26 #define TT this.pidof |
24 | 27 |
25 #define PATH_LEN 64 | |
26 #define PROC_DIR "/proc/" | |
27 #define CMD_LINE "/cmdline" | |
28 | |
29 static int matched = 0; | |
30 | |
31 static int for_each_pid(void (*callback) (const char *pid)) { | |
32 DIR *dp; | |
33 struct dirent *entry; | |
34 FILE *fp; | |
35 int n, pathpos; | |
36 char cmd[PATH_MAX]; | |
37 char path[PATH_LEN]; | |
38 char **curname; | |
39 | |
40 dp = opendir(PROC_DIR); | |
41 if (!dp) { | |
42 perror("opendir"); | |
43 return 1; | |
44 } | |
45 | |
46 while ((entry = readdir(dp))) { | |
47 if (!isdigit(entry->d_name[0])) continue; | |
48 strcpy(path, PROC_DIR); | |
49 pathpos = strlen(PROC_DIR); | |
50 | |
51 if (pathpos + strlen(entry->d_name) + 1 > PATH_LEN) continue; | |
52 | |
53 strcpy(&path[pathpos], entry->d_name); | |
54 pathpos += strlen(entry->d_name); | |
55 | |
56 if (pathpos + strlen(CMD_LINE) + 1 > PATH_LEN) continue; | |
57 strcpy(&path[pathpos], CMD_LINE); | |
58 | |
59 fp = fopen(path, "r"); | |
60 if (!fp) { | |
61 perror("fopen"); | |
62 continue; | |
63 } | |
64 | |
65 n = fread(cmd, 1, PATH_MAX, fp); | |
66 fclose(fp); | |
67 if (n == 0) continue; | |
68 | |
69 for (curname = toys.optargs; *curname; curname++) { | |
70 if (strcmp(basename(cmd), *curname) == 0) { | |
71 callback(entry->d_name); | |
72 } | |
73 } | |
74 } | |
75 | |
76 closedir(dp); | |
77 | |
78 return 0; | |
79 } | |
80 | 28 |
81 static void print_pid (const char *pid) { | 29 static void print_pid (const char *pid) { |
82 if (matched) putchar(' '); | 30 if (TT.matched) putchar(' '); |
83 fputs(pid, stdout); | 31 fputs(pid, stdout); |
84 matched = 1; | 32 TT.matched = 1; |
85 } | 33 } |
86 | 34 |
87 void pidof_main(void) | 35 void pidof_main(void) |
88 { | 36 { |
89 int err; | 37 int err; |
90 | 38 |
39 TT.matched = 0; | |
40 | |
91 if (!toys.optargs) exit(1); | 41 if (!toys.optargs) exit(1); |
92 | 42 |
93 err = for_each_pid(print_pid); | 43 err = for_each_pid_with_name_in(toys.optargs, print_pid); |
94 if (err) exit(1); | 44 if (err) exit(1); |
95 | 45 |
96 if (!matched) | 46 if (!TT.matched) |
97 exit(1); | 47 exit(1); |
98 else | 48 else |
99 putchar('\n'); | 49 putchar('\n'); |
100 } | 50 } |