blob: fe13f7211d1beced410f89c69cf1ae9caf488bfd [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Eric Andersenaad1a882001-03-16 22:47:14 +00009#include "libbb.h"
10
Denis Vlasenko198bada2007-06-23 14:56:43 +000011/*
12In Linux we have three ways to determine "process name":
131. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field.
142. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall.
153. /proc/PID/exe symlink. Points to the running executable file.
16
17kernel threads:
18 comm: thread name
19 cmdline: empty
20 exe: <readlink fails>
21
22executable
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
28script (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
34If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y),
35some commands started from busybox shell, xargs or find are started by
36execXXX("/proc/self/exe", applet_name, params....)
37and therefore comm field contains "exe".
38*/
39
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000040static int comm_match(procps_status_t *p, const char *procName)
41{
42 int argv1idx;
Alexander Shishkine766f622009-07-29 01:35:13 +020043 const char *argv1;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000044
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000045 if (strncmp(p->comm, procName, 15) != 0)
Alexander Shishkine766f622009-07-29 01:35:13 +020046 return 0; /* comm does not match */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000047
Alexander Shishkine766f622009-07-29 01:35:13 +020048 /* 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 Vlasenko3b3ca112008-07-17 18:39:36 +000053
54 /* comm is truncated, but first 15 chars match.
55 * This can be crazily_long_script_name.sh!
Alexander Shishkine766f622009-07-29 01:35:13 +020056 * The telltale sign is basename(argv[1]) == procName */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000057
58 if (!p->argv0)
59 return 0;
60
61 argv1idx = strlen(p->argv0) + 1;
62 if (argv1idx >= p->argv_len)
63 return 0;
Alexander Shishkine766f622009-07-29 01:35:13 +020064 argv1 = p->argv0 + argv1idx;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000065
Alexander Shishkine766f622009-07-29 01:35:13 +020066 if (strcmp(bb_basename(argv1), procName) != 0)
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000067 return 0;
68
69 return 1;
70}
71
Denis Vlasenko9f00e052009-02-14 01:36:11 +000072/* This finds the pid of the specified process.
73 * Currently, it's implemented by rummaging through
74 * the proc filesystem.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000075 *
Denis Vlasenko9f00e052009-02-14 01:36:11 +000076 * Returns a list of all matching PIDs
77 * It is the caller's duty to free the returned pidlist.
Eric Andersenaad1a882001-03-16 22:47:14 +000078 *
Denis Vlasenko9f00e052009-02-14 01:36:11 +000079 * Modified by Vladimir Oleynik for use with libbb/procps.c
Eric Andersenaad1a882001-03-16 22:47:14 +000080 */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000081pid_t* FAST_FUNC find_pid_by_name(const char *procName)
Eric Andersenaad1a882001-03-16 22:47:14 +000082{
Denis Vlasenko35fb5122006-11-01 09:16:49 +000083 pid_t* pidList;
Denis Vlasenkobe905d52006-09-27 14:17:31 +000084 int i = 0;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000085 procps_status_t* p = NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000086
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000087 pidList = xzalloc(sizeof(*pidList));
Alexander Shishkine766f622009-07-29 01:35:13 +020088 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) {
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000089 if (comm_match(p, procName)
Denis Vlasenkof7d07b12007-06-30 08:03:26 +000090 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
91 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
Alexander Shishkine766f622009-07-29 01:35:13 +020092 /* or we require /proc/PID/exe link to match */
Denys Vlasenko3a0eea02019-06-15 18:35:39 +020093 || (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 Vlasenkof7d07b12007-06-30 08:03:26 +000098 ) {
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000099 pidList = xrealloc_vector(pidList, 2, i);
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000100 pidList[i++] = p->pid;
Eric Andersenaad1a882001-03-16 22:47:14 +0000101 }
102 }
103
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000104 pidList[i] = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +0000105 return pidList;
106}
Eric Andersenaad1a882001-03-16 22:47:14 +0000107
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000108pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +0000109{
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000110 int i = 0;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000111 while (pidList[i])
112 i++;
113 if (--i >= 0) {
114 pid_t k;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +0000115 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}