diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-23 14:56:43 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-06-23 14:56:43 +0000 |
commit | 198badafd82905c9a2e76eeacb7ce463d8518bda (patch) | |
tree | fcb367654b5c6cafdca09ef189342fc7019095fc | |
parent | 118b81df76be0e372309d76196c8eedf19ac56cd (diff) | |
download | busybox-w32-198badafd82905c9a2e76eeacb7ce463d8518bda.tar.gz busybox-w32-198badafd82905c9a2e76eeacb7ce463d8518bda.tar.bz2 busybox-w32-198badafd82905c9a2e76eeacb7ce463d8518bda.zip |
pidof: size optimizations (-50 bytes)
-rw-r--r-- | include/usage.h | 2 | ||||
-rw-r--r-- | libbb/find_pid_by_name.c | 29 | ||||
-rw-r--r-- | procps/pidof.c | 33 |
3 files changed, 44 insertions, 20 deletions
diff --git a/include/usage.h b/include/usage.h index 2baa495ac..6d02c8bbf 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -2544,7 +2544,7 @@ | |||
2544 | USE_FEATURE_PIDOF_SINGLE( \ | 2544 | USE_FEATURE_PIDOF_SINGLE( \ |
2545 | "\n -s Display only a single PID") \ | 2545 | "\n -s Display only a single PID") \ |
2546 | USE_FEATURE_PIDOF_OMIT( \ | 2546 | USE_FEATURE_PIDOF_OMIT( \ |
2547 | "\n -o Omit given pid") \ | 2547 | "\n -o PID Omit given pid") \ |
2548 | USE_FEATURE_PIDOF_OMIT( \ | 2548 | USE_FEATURE_PIDOF_OMIT( \ |
2549 | "\n Use %PPID to omit the parent pid of pidof itself") | 2549 | "\n Use %PPID to omit the parent pid of pidof itself") |
2550 | #define pidof_example_usage \ | 2550 | #define pidof_example_usage \ |
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index e98616940..13ccb545d 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c | |||
@@ -9,6 +9,35 @@ | |||
9 | 9 | ||
10 | #include "libbb.h" | 10 | #include "libbb.h" |
11 | 11 | ||
12 | /* | ||
13 | In Linux we have three ways to determine "process name": | ||
14 | 1. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field. | ||
15 | 2. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall. | ||
16 | 3. /proc/PID/exe symlink. Points to the running executable file. | ||
17 | |||
18 | kernel threads: | ||
19 | comm: thread name | ||
20 | cmdline: empty | ||
21 | exe: <readlink fails> | ||
22 | |||
23 | executable | ||
24 | comm: first 15 chars of base name | ||
25 | (if executable is a symlink, then first 15 chars of symlink name are used) | ||
26 | cmdline: argv[0] from exec syscall | ||
27 | exe: points to executable (resolves symlink, unlike comm) | ||
28 | |||
29 | script (an executable with #!/path/to/interpreter): | ||
30 | comm: first 15 chars of script's base name (symlinks are not resolved) | ||
31 | cmdline: /path/to/interpreter (symlinks are not resolved) | ||
32 | (script name is in argv[1], args are pushed into argv[2] etc) | ||
33 | exe: points to interpreter's executable (symlinks are resolved) | ||
34 | |||
35 | If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y), | ||
36 | some commands started from busybox shell, xargs or find are started by | ||
37 | execXXX("/proc/self/exe", applet_name, params....) | ||
38 | and therefore comm field contains "exe". | ||
39 | */ | ||
40 | |||
12 | /* find_pid_by_name() | 41 | /* find_pid_by_name() |
13 | * | 42 | * |
14 | * Modified by Vladimir Oleynik for use with libbb/procps.c | 43 | * Modified by Vladimir Oleynik for use with libbb/procps.c |
diff --git a/procps/pidof.c b/procps/pidof.c index 3541aeee0..01e587cbf 100644 --- a/procps/pidof.c +++ b/procps/pidof.c | |||
@@ -20,9 +20,9 @@ int pidof_main(int argc, char **argv); | |||
20 | int pidof_main(int argc, char **argv) | 20 | int pidof_main(int argc, char **argv) |
21 | { | 21 | { |
22 | unsigned first = 1; | 22 | unsigned first = 1; |
23 | unsigned fail = 1; | ||
24 | unsigned opt; | 23 | unsigned opt; |
25 | #if ENABLE_FEATURE_PIDOF_OMIT | 24 | #if ENABLE_FEATURE_PIDOF_OMIT |
25 | char ppid_str[sizeof(int)*3 + 1]; | ||
26 | llist_t *omits = NULL; /* list of pids to omit */ | 26 | llist_t *omits = NULL; /* list of pids to omit */ |
27 | opt_complementary = "o::"; | 27 | opt_complementary = "o::"; |
28 | #endif | 28 | #endif |
@@ -35,14 +35,12 @@ int pidof_main(int argc, char **argv) | |||
35 | #if ENABLE_FEATURE_PIDOF_OMIT | 35 | #if ENABLE_FEATURE_PIDOF_OMIT |
36 | /* fill omit list. */ | 36 | /* fill omit list. */ |
37 | { | 37 | { |
38 | char getppid_str[sizeof(int)*3 + 1]; | 38 | llist_t *omits_p = omits; |
39 | llist_t * omits_p = omits; | ||
40 | while (omits_p) { | 39 | while (omits_p) { |
41 | /* are we asked to exclude the parent's process ID? */ | 40 | /* are we asked to exclude the parent's process ID? */ |
42 | if (!strncmp(omits_p->data, "%PPID", 5)) { | 41 | if (strcmp(omits_p->data, "%PPID") == 0) { |
43 | llist_pop(&omits_p); | 42 | sprintf(ppid_str, "%u", (unsigned)getppid()); |
44 | snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid()); | 43 | omits_p->data = ppid_str; |
45 | llist_add_to(&omits_p, getppid_str); | ||
46 | } | 44 | } |
47 | omits_p = omits_p->link; | 45 | omits_p = omits_p->link; |
48 | } | 46 | } |
@@ -56,27 +54,24 @@ int pidof_main(int argc, char **argv) | |||
56 | /* reverse the pidlist like GNU pidof does. */ | 54 | /* reverse the pidlist like GNU pidof does. */ |
57 | pidList = pidlist_reverse(find_pid_by_name(argv[optind])); | 55 | pidList = pidlist_reverse(find_pid_by_name(argv[optind])); |
58 | for (pl = pidList; *pl; pl++) { | 56 | for (pl = pidList; *pl; pl++) { |
59 | SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0; | ||
60 | #if ENABLE_FEATURE_PIDOF_OMIT | 57 | #if ENABLE_FEATURE_PIDOF_OMIT |
61 | if (opt & OPT_OMIT) { | 58 | if (opt & OPT_OMIT) { |
62 | llist_t *omits_p = omits; | 59 | llist_t *omits_p = omits; |
63 | while (omits_p) { | 60 | while (omits_p) { |
64 | if (xatoul(omits_p->data) == *pl) { | 61 | if (xatoul(omits_p->data) == *pl) { |
65 | omitted = 1; | 62 | goto omitting; |
66 | break; | 63 | } |
67 | } else | 64 | omits_p = omits_p->link; |
68 | omits_p = omits_p->link; | ||
69 | } | 65 | } |
70 | } | 66 | } |
71 | #endif | 67 | #endif |
72 | if (!omitted) { | 68 | printf(" %u" + first, (unsigned)*pl); |
73 | printf(" %u" + first, (unsigned)*pl); | 69 | first = 0; |
74 | first = 0; | ||
75 | } | ||
76 | fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); | ||
77 | |||
78 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) | 70 | if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) |
79 | break; | 71 | break; |
72 | #if ENABLE_FEATURE_PIDOF_OMIT | ||
73 | omitting: ; | ||
74 | #endif | ||
80 | } | 75 | } |
81 | free(pidList); | 76 | free(pidList); |
82 | optind++; | 77 | optind++; |
@@ -87,5 +82,5 @@ int pidof_main(int argc, char **argv) | |||
87 | if (ENABLE_FEATURE_CLEAN_UP) | 82 | if (ENABLE_FEATURE_CLEAN_UP) |
88 | llist_free(omits, NULL); | 83 | llist_free(omits, NULL); |
89 | #endif | 84 | #endif |
90 | return fail ? EXIT_FAILURE : EXIT_SUCCESS; | 85 | return first; /* 1 (failure) - no processes found */ |
91 | } | 86 | } |