7 |
* Licensed under the GPL version 2, see the file LICENSE in this tarball. |
* Licensed under the GPL version 2, see the file LICENSE in this tarball. |
8 |
*/ |
*/ |
9 |
|
|
10 |
#include "busybox.h" |
#include "libbb.h" |
11 |
|
|
12 |
enum { |
enum { |
13 |
USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) |
USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) |
16 |
OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0, |
OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0, |
17 |
}; |
}; |
18 |
|
|
19 |
int pidof_main(int argc, char **argv) |
int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
20 |
|
int pidof_main(int argc UNUSED_PARAM, char **argv) |
21 |
{ |
{ |
22 |
unsigned first = 1; |
unsigned first = 1; |
|
unsigned fail = 1; |
|
23 |
unsigned opt; |
unsigned opt; |
24 |
#if ENABLE_FEATURE_PIDOF_OMIT |
#if ENABLE_FEATURE_PIDOF_OMIT |
25 |
llist_t *omits = NULL; /* list of pids to omit */ |
llist_t *omits = NULL; /* list of pids to omit */ |
27 |
#endif |
#endif |
28 |
|
|
29 |
/* do unconditional option parsing */ |
/* do unconditional option parsing */ |
30 |
opt = getopt32(argc, argv, "" |
opt = getopt32(argv, "" |
31 |
USE_FEATURE_PIDOF_SINGLE ("s") |
USE_FEATURE_PIDOF_SINGLE ("s") |
32 |
USE_FEATURE_PIDOF_OMIT("o:", &omits)); |
USE_FEATURE_PIDOF_OMIT("o:", &omits)); |
33 |
|
|
34 |
#if ENABLE_FEATURE_PIDOF_OMIT |
#if ENABLE_FEATURE_PIDOF_OMIT |
35 |
/* fill omit list. */ |
/* fill omit list. */ |
36 |
{ |
{ |
37 |
char getppid_str[sizeof(int)*3 + 1]; |
llist_t *omits_p = omits; |
|
llist_t * omits_p = omits; |
|
38 |
while (omits_p) { |
while (omits_p) { |
39 |
/* are we asked to exclude the parent's process ID? */ |
/* are we asked to exclude the parent's process ID? */ |
40 |
if (!strncmp(omits_p->data, "%PPID", 5)) { |
if (strcmp(omits_p->data, "%PPID") == 0) { |
41 |
llist_pop(&omits_p); |
omits_p->data = utoa((unsigned)getppid()); |
|
snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid()); |
|
|
llist_add_to(&omits_p, getppid_str); |
|
42 |
} |
} |
43 |
omits_p = omits_p->link; |
omits_p = omits_p->link; |
44 |
} |
} |
45 |
} |
} |
46 |
#endif |
#endif |
47 |
/* Looks like everything is set to go. */ |
/* Looks like everything is set to go. */ |
48 |
while (optind < argc) { |
argv += optind; |
49 |
|
while (*argv) { |
50 |
pid_t *pidList; |
pid_t *pidList; |
51 |
pid_t *pl; |
pid_t *pl; |
52 |
|
|
53 |
/* reverse the pidlist like GNU pidof does. */ |
/* reverse the pidlist like GNU pidof does. */ |
54 |
pidList = pidlist_reverse(find_pid_by_name(argv[optind])); |
pidList = pidlist_reverse(find_pid_by_name(*argv)); |
55 |
for (pl = pidList; *pl; pl++) { |
for (pl = pidList; *pl; pl++) { |
|
SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0; |
|
56 |
#if ENABLE_FEATURE_PIDOF_OMIT |
#if ENABLE_FEATURE_PIDOF_OMIT |
57 |
if (opt & OPT_OMIT) { |
if (opt & OPT_OMIT) { |
58 |
llist_t *omits_p = omits; |
llist_t *omits_p = omits; |
59 |
while (omits_p) { |
while (omits_p) { |
60 |
if (xatoul(omits_p->data) == *pl) { |
if (xatoul(omits_p->data) == (unsigned long)(*pl)) { |
61 |
omitted = 1; |
goto omitting; |
62 |
break; |
} |
63 |
} else |
omits_p = omits_p->link; |
|
omits_p = omits_p->link; |
|
64 |
} |
} |
65 |
} |
} |
66 |
#endif |
#endif |
67 |
if (!omitted) { |
printf(" %u" + first, (unsigned)*pl); |
68 |
printf(" %u" + first, (unsigned)*pl); |
first = 0; |
|
first = 0; |
|
|
} |
|
|
fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); |
|
|
|
|
69 |
if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
70 |
break; |
break; |
71 |
|
#if ENABLE_FEATURE_PIDOF_OMIT |
72 |
|
omitting: ; |
73 |
|
#endif |
74 |
} |
} |
75 |
free(pidList); |
free(pidList); |
76 |
optind++; |
argv++; |
77 |
} |
} |
78 |
putchar('\n'); |
if (!first) |
79 |
|
bb_putchar('\n'); |
80 |
|
|
81 |
#if ENABLE_FEATURE_PIDOF_OMIT |
#if ENABLE_FEATURE_PIDOF_OMIT |
82 |
if (ENABLE_FEATURE_CLEAN_UP) |
if (ENABLE_FEATURE_CLEAN_UP) |
83 |
llist_free(omits, NULL); |
llist_free(omits, NULL); |
84 |
#endif |
#endif |
85 |
return fail ? EXIT_FAILURE : EXIT_SUCCESS; |
return first; /* 1 (failure) - no processes found */ |
86 |
} |
} |