Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
Denis Vlasenko | 198bada | 2007-06-23 14:56:43 +0000 | [diff] [blame] | 11 | /* |
| 12 | In Linux we have three ways to determine "process name": |
| 13 | 1. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field. |
| 14 | 2. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall. |
| 15 | 3. /proc/PID/exe symlink. Points to the running executable file. |
| 16 | |
| 17 | kernel threads: |
| 18 | comm: thread name |
| 19 | cmdline: empty |
| 20 | exe: <readlink fails> |
| 21 | |
| 22 | executable |
| 23 | comm: first 15 chars of base name |
| 24 | (if executable is a symlink, then first 15 chars of symlink name are used) |
| 25 | cmdline: argv[0] from exec syscall |
| 26 | exe: points to executable (resolves symlink, unlike comm) |
| 27 | |
| 28 | script (an executable with #!/path/to/interpreter): |
| 29 | comm: first 15 chars of script's base name (symlinks are not resolved) |
| 30 | cmdline: /path/to/interpreter (symlinks are not resolved) |
| 31 | (script name is in argv[1], args are pushed into argv[2] etc) |
| 32 | exe: points to interpreter's executable (symlinks are resolved) |
| 33 | |
| 34 | If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y), |
| 35 | some commands started from busybox shell, xargs or find are started by |
| 36 | execXXX("/proc/self/exe", applet_name, params....) |
| 37 | and therefore comm field contains "exe". |
| 38 | */ |
| 39 | |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 40 | static int comm_match(procps_status_t *p, const char *procName) |
| 41 | { |
| 42 | int argv1idx; |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 43 | const char *argv1; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 45 | if (strncmp(p->comm, procName, 15) != 0) |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 46 | return 0; /* comm does not match */ |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 47 | |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 48 | /* In Linux, if comm is 15 chars, it is truncated. |
| 49 | * (or maybe the name was exactly 15 chars, but there is |
| 50 | * no way to know that) */ |
| 51 | if (p->comm[14] == '\0') |
| 52 | return 1; /* comm is not truncated - matches */ |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 53 | |
| 54 | /* comm is truncated, but first 15 chars match. |
| 55 | * This can be crazily_long_script_name.sh! |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 56 | * The telltale sign is basename(argv[1]) == procName */ |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 57 | |
| 58 | if (!p->argv0) |
| 59 | return 0; |
| 60 | |
| 61 | argv1idx = strlen(p->argv0) + 1; |
| 62 | if (argv1idx >= p->argv_len) |
| 63 | return 0; |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 64 | argv1 = p->argv0 + argv1idx; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 65 | |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 66 | if (strcmp(bb_basename(argv1), procName) != 0) |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | |
| 69 | return 1; |
| 70 | } |
| 71 | |
Denis Vlasenko | 9f00e05 | 2009-02-14 01:36:11 +0000 | [diff] [blame] | 72 | /* This finds the pid of the specified process. |
| 73 | * Currently, it's implemented by rummaging through |
| 74 | * the proc filesystem. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 75 | * |
Denis Vlasenko | 9f00e05 | 2009-02-14 01:36:11 +0000 | [diff] [blame] | 76 | * Returns a list of all matching PIDs |
| 77 | * It is the caller's duty to free the returned pidlist. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 78 | * |
Denis Vlasenko | 9f00e05 | 2009-02-14 01:36:11 +0000 | [diff] [blame] | 79 | * Modified by Vladimir Oleynik for use with libbb/procps.c |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 80 | */ |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 81 | pid_t* FAST_FUNC find_pid_by_name(const char *procName) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 82 | { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 83 | pid_t* pidList; |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 84 | int i = 0; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 85 | procps_status_t* p = NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 87 | pidList = xzalloc(sizeof(*pidList)); |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 88 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) { |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 89 | if (comm_match(p, procName) |
Denis Vlasenko | f7d07b1 | 2007-06-30 08:03:26 +0000 | [diff] [blame] | 90 | /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/ |
| 91 | || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0) |
Alexander Shishkin | e766f62 | 2009-07-29 01:35:13 +0200 | [diff] [blame] | 92 | /* or we require /proc/PID/exe link to match */ |
Denys Vlasenko | 3a0eea0 | 2019-06-15 18:35:39 +0200 | [diff] [blame] | 93 | || (p->exe && strcmp( |
| 94 | procName[0] == '/' ? p->exe /* support "pidof /path/to/binary" case too */ |
| 95 | : bb_basename(p->exe), |
| 96 | procName |
| 97 | ) == 0) |
Denis Vlasenko | f7d07b1 | 2007-06-30 08:03:26 +0000 | [diff] [blame] | 98 | ) { |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 99 | pidList = xrealloc_vector(pidList, 2, i); |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 100 | pidList[i++] = p->pid; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 104 | pidList[i] = 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 105 | return pidList; |
| 106 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 108 | pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 109 | { |
Denis Vlasenko | be905d5 | 2006-09-27 14:17:31 +0000 | [diff] [blame] | 110 | int i = 0; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 111 | while (pidList[i]) |
| 112 | i++; |
| 113 | if (--i >= 0) { |
| 114 | pid_t k; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 115 | int j; |
| 116 | for (j = 0; i > j; i--, j++) { |
| 117 | k = pidList[i]; |
| 118 | pidList[i] = pidList[j]; |
| 119 | pidList[j] = k; |
| 120 | } |
| 121 | } |
| 122 | return pidList; |
| 123 | } |