diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-17 18:39:36 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-07-17 18:39:36 +0000 |
| commit | 3b3ca113ed00a6781a28172bb3a6860a5f79ea02 (patch) | |
| tree | 7834909b21af2b0fdbb7c3c52dc3fef12ba0c051 | |
| parent | 18875bf772d72c9c543be8f9ab46dec451f547ea (diff) | |
| download | busybox-w32-3b3ca113ed00a6781a28172bb3a6860a5f79ea02.tar.gz busybox-w32-3b3ca113ed00a6781a28172bb3a6860a5f79ea02.tar.bz2 busybox-w32-3b3ca113ed00a6781a28172bb3a6860a5f79ea02.zip | |
pidof/killall: allow find_pid_by_name to find running
processes started as scripts_with_name_longer_than_15_bytes.sh
closes bug 4054 (and is generally neat)
| -rw-r--r-- | include/libbb.h | 3 | ||||
| -rw-r--r-- | libbb/find_pid_by_name.c | 43 | ||||
| -rw-r--r-- | libbb/procps.c | 17 | ||||
| -rw-r--r-- | procps/pgrep.c | 9 | ||||
| -rw-r--r-- | procps/pidof.c | 4 |
5 files changed, 53 insertions, 23 deletions
diff --git a/include/libbb.h b/include/libbb.h index aafdfa32c..14af1368c 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -1166,6 +1166,7 @@ typedef struct procps_status_t { | |||
| 1166 | uint8_t shift_pages_to_bytes; | 1166 | uint8_t shift_pages_to_bytes; |
| 1167 | uint8_t shift_pages_to_kb; | 1167 | uint8_t shift_pages_to_kb; |
| 1168 | /* Fields are set to 0/NULL if failed to determine (or not requested) */ | 1168 | /* Fields are set to 0/NULL if failed to determine (or not requested) */ |
| 1169 | uint16_t argv_len; | ||
| 1169 | char *argv0; | 1170 | char *argv0; |
| 1170 | USE_SELINUX(char *context;) | 1171 | USE_SELINUX(char *context;) |
| 1171 | /* Everything below must contain no ptrs to malloc'ed data: | 1172 | /* Everything below must contain no ptrs to malloc'ed data: |
| @@ -1213,7 +1214,7 @@ enum { | |||
| 1213 | PSSCAN_UTIME = 1 << 13, | 1214 | PSSCAN_UTIME = 1 << 13, |
| 1214 | PSSCAN_TTY = 1 << 14, | 1215 | PSSCAN_TTY = 1 << 14, |
| 1215 | PSSCAN_SMAPS = (1 << 15) * ENABLE_FEATURE_TOPMEM, | 1216 | PSSCAN_SMAPS = (1 << 15) * ENABLE_FEATURE_TOPMEM, |
| 1216 | PSSCAN_ARGVN = (1 << 16) * (ENABLE_PGREP | ENABLE_PKILL), | 1217 | PSSCAN_ARGVN = (1 << 16) * (ENABLE_PGREP || ENABLE_PKILL || ENABLE_PIDOF), |
| 1217 | USE_SELINUX(PSSCAN_CONTEXT = 1 << 17,) | 1218 | USE_SELINUX(PSSCAN_CONTEXT = 1 << 17,) |
| 1218 | PSSCAN_START_TIME = 1 << 18, | 1219 | PSSCAN_START_TIME = 1 << 18, |
| 1219 | /* These are all retrieved from proc/NN/stat in one go: */ | 1220 | /* These are all retrieved from proc/NN/stat in one go: */ |
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index ae2f11643..2ee423cb7 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c | |||
| @@ -38,6 +38,35 @@ execXXX("/proc/self/exe", applet_name, params....) | |||
| 38 | and therefore comm field contains "exe". | 38 | and therefore comm field contains "exe". |
| 39 | */ | 39 | */ |
| 40 | 40 | ||
| 41 | static int comm_match(procps_status_t *p, const char *procName) | ||
| 42 | { | ||
| 43 | int argv1idx; | ||
| 44 | |||
| 45 | /* comm does not match */ | ||
| 46 | if (strncmp(p->comm, procName, 15) != 0) | ||
| 47 | return 0; | ||
| 48 | |||
| 49 | /* in Linux, if comm is 15 chars, it may be a truncated */ | ||
| 50 | if (p->comm[14] == '\0') /* comm is not truncated - match */ | ||
| 51 | return 1; | ||
| 52 | |||
| 53 | /* comm is truncated, but first 15 chars match. | ||
| 54 | * This can be crazily_long_script_name.sh! | ||
| 55 | * The telltale sign is basename(argv[1]) == procName. */ | ||
| 56 | |||
| 57 | if (!p->argv0) | ||
| 58 | return 0; | ||
| 59 | |||
| 60 | argv1idx = strlen(p->argv0) + 1; | ||
| 61 | if (argv1idx >= p->argv_len) | ||
| 62 | return 0; | ||
| 63 | |||
| 64 | if (strcmp(bb_basename(p->argv0 + argv1idx), procName) != 0) | ||
| 65 | return 0; | ||
| 66 | |||
| 67 | return 1; | ||
| 68 | } | ||
| 69 | |||
| 41 | /* find_pid_by_name() | 70 | /* find_pid_by_name() |
| 42 | * | 71 | * |
| 43 | * Modified by Vladimir Oleynik for use with libbb/procps.c | 72 | * Modified by Vladimir Oleynik for use with libbb/procps.c |
| @@ -48,24 +77,20 @@ and therefore comm field contains "exe". | |||
| 48 | * Returns a list of all matching PIDs | 77 | * Returns a list of all matching PIDs |
| 49 | * It is the caller's duty to free the returned pidlist. | 78 | * It is the caller's duty to free the returned pidlist. |
| 50 | */ | 79 | */ |
| 51 | pid_t* FAST_FUNC find_pid_by_name(const char* procName) | 80 | pid_t* FAST_FUNC find_pid_by_name(const char *procName) |
| 52 | { | 81 | { |
| 53 | pid_t* pidList; | 82 | pid_t* pidList; |
| 54 | int i = 0; | 83 | int i = 0; |
| 55 | procps_status_t* p = NULL; | 84 | procps_status_t* p = NULL; |
| 56 | 85 | ||
| 57 | pidList = xmalloc(sizeof(*pidList)); | 86 | pidList = xzalloc(sizeof(*pidList)); |
| 58 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) { | 87 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN))) { |
| 59 | if ( | 88 | if (comm_match(p, procName) |
| 60 | /* we require comm to match and to not be truncated */ | ||
| 61 | /* in Linux, if comm is 15 chars, it may be a truncated | ||
| 62 | * name, so we don't allow that to match */ | ||
| 63 | (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0) | ||
| 64 | /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ | 89 | /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ |
| 65 | || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) | 90 | || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) |
| 66 | /* TOOD: we can also try /proc/NUM/exe link, do we want that? */ | 91 | /* TOOD: we can also try /proc/NUM/exe link, do we want that? */ |
| 67 | ) { | 92 | ) { |
| 68 | pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); | 93 | pidList = xrealloc_vector(pidList, 2, i); |
| 69 | pidList[i++] = p->pid; | 94 | pidList[i++] = p->pid; |
| 70 | } | 95 | } |
| 71 | } | 96 | } |
diff --git a/libbb/procps.c b/libbb/procps.c index a5168a077..f799099fd 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
| @@ -78,7 +78,7 @@ const char* FAST_FUNC get_cached_groupname(gid_t gid) | |||
| 78 | 78 | ||
| 79 | #define PROCPS_BUFSIZE 1024 | 79 | #define PROCPS_BUFSIZE 1024 |
| 80 | 80 | ||
| 81 | static int FAST_FUNC read_to_buf(const char *filename, void *buf) | 81 | static int read_to_buf(const char *filename, void *buf) |
| 82 | { | 82 | { |
| 83 | int fd; | 83 | int fd; |
| 84 | /* open_read_close() would do two reads, checking for EOF. | 84 | /* open_read_close() would do two reads, checking for EOF. |
| @@ -385,16 +385,15 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) | |||
| 385 | n = read_to_buf(filename, buf); | 385 | n = read_to_buf(filename, buf); |
| 386 | if (n <= 0) | 386 | if (n <= 0) |
| 387 | break; | 387 | break; |
| 388 | #if ENABLE_PGREP || ENABLE_PKILL | ||
| 389 | if (flags & PSSCAN_ARGVN) { | 388 | if (flags & PSSCAN_ARGVN) { |
| 390 | do { | 389 | sp->argv_len = n; |
| 391 | n--; | 390 | sp->argv0 = xmalloc(n + 1); |
| 392 | if (buf[n] == '\0') | 391 | memcpy(sp->argv0, buf, n + 1); |
| 393 | buf[n] = ' '; | 392 | /* sp->argv0[n] = '\0'; - buf has it */ |
| 394 | } while (n); | 393 | } else { |
| 394 | sp->argv_len = 0; | ||
| 395 | sp->argv0 = xstrdup(buf); | ||
| 395 | } | 396 | } |
| 396 | #endif | ||
| 397 | sp->argv0 = xstrdup(buf); | ||
| 398 | } | 397 | } |
| 399 | #endif | 398 | #endif |
| 400 | break; | 399 | break; |
diff --git a/procps/pgrep.c b/procps/pgrep.c index 7b3cd8e50..0e8e5294e 100644 --- a/procps/pgrep.c +++ b/procps/pgrep.c | |||
| @@ -111,8 +111,15 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv) | |||
| 111 | if (proc->pid == pid) | 111 | if (proc->pid == pid) |
| 112 | continue; | 112 | continue; |
| 113 | cmd = proc->argv0; | 113 | cmd = proc->argv0; |
| 114 | if (!cmd) | 114 | if (!cmd) { |
| 115 | cmd = proc->comm; | 115 | cmd = proc->comm; |
| 116 | } else { | ||
| 117 | int i = proc->argv_len; | ||
| 118 | while (i) { | ||
| 119 | if (!cmd[i]) cmd[i] = ' '; | ||
| 120 | i--; | ||
| 121 | } | ||
| 122 | } | ||
| 116 | /* NB: OPT_INVERT is always 0 or 1 */ | 123 | /* NB: OPT_INVERT is always 0 or 1 */ |
| 117 | if ((regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */ | 124 | if ((regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */ |
| 118 | && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT | 125 | && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT |
diff --git a/procps/pidof.c b/procps/pidof.c index 2519473e8..780504433 100644 --- a/procps/pidof.c +++ b/procps/pidof.c | |||
| @@ -22,7 +22,6 @@ int pidof_main(int argc UNUSED_PARAM, char **argv) | |||
| 22 | unsigned first = 1; | 22 | unsigned first = 1; |
| 23 | unsigned opt; | 23 | unsigned opt; |
| 24 | #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 */ | 25 | llist_t *omits = NULL; /* list of pids to omit */ |
| 27 | opt_complementary = "o::"; | 26 | opt_complementary = "o::"; |
| 28 | #endif | 27 | #endif |
| @@ -39,8 +38,7 @@ int pidof_main(int argc UNUSED_PARAM, char **argv) | |||
| 39 | while (omits_p) { | 38 | while (omits_p) { |
| 40 | /* are we asked to exclude the parent's process ID? */ | 39 | /* are we asked to exclude the parent's process ID? */ |
| 41 | if (strcmp(omits_p->data, "%PPID") == 0) { | 40 | if (strcmp(omits_p->data, "%PPID") == 0) { |
| 42 | sprintf(ppid_str, "%u", (unsigned)getppid()); | 41 | omits_p->data = utoa((unsigned)getppid()); |
| 43 | omits_p->data = ppid_str; | ||
| 44 | } | 42 | } |
| 45 | omits_p = omits_p->link; | 43 | omits_p = omits_p->link; |
| 46 | } | 44 | } |
