blob: db823d05b972323e2180e87f2a0892195787c01b [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 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
Denis Vlasenko198bada2007-06-23 14:56:43 +000012/*
13In Linux we have three ways to determine "process name":
141. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field.
152. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall.
163. /proc/PID/exe symlink. Points to the running executable file.
17
18kernel threads:
19 comm: thread name
20 cmdline: empty
21 exe: <readlink fails>
22
23executable
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
29script (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
35If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y),
36some commands started from busybox shell, xargs or find are started by
37execXXX("/proc/self/exe", applet_name, params....)
38and therefore comm field contains "exe".
39*/
40
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000041static int comm_match(procps_status_t *p, const char *procName)
42{
43 int argv1idx;
Alexander Shishkine766f622009-07-29 01:35:13 +020044 const char *argv1;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000045
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000046 if (strncmp(p->comm, procName, 15) != 0)
Alexander Shishkine766f622009-07-29 01:35:13 +020047 return 0; /* comm does not match */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000048
Alexander Shishkine766f622009-07-29 01:35:13 +020049 /* In Linux, if comm is 15 chars, it is truncated.
50 * (or maybe the name was exactly 15 chars, but there is
51 * no way to know that) */
52 if (p->comm[14] == '\0')
53 return 1; /* comm is not truncated - matches */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000054
55 /* comm is truncated, but first 15 chars match.
56 * This can be crazily_long_script_name.sh!
Alexander Shishkine766f622009-07-29 01:35:13 +020057 * The telltale sign is basename(argv[1]) == procName */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000058
59 if (!p->argv0)
60 return 0;
61
62 argv1idx = strlen(p->argv0) + 1;
63 if (argv1idx >= p->argv_len)
64 return 0;
Alexander Shishkine766f622009-07-29 01:35:13 +020065 argv1 = p->argv0 + argv1idx;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000066
Alexander Shishkine766f622009-07-29 01:35:13 +020067 if (strcmp(bb_basename(argv1), procName) != 0)
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000068 return 0;
69
70 return 1;
71}
72
Denis Vlasenko9f00e052009-02-14 01:36:11 +000073/* This finds the pid of the specified process.
74 * Currently, it's implemented by rummaging through
75 * the proc filesystem.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076 *
Denis Vlasenko9f00e052009-02-14 01:36:11 +000077 * Returns a list of all matching PIDs
78 * It is the caller's duty to free the returned pidlist.
Eric Andersenaad1a882001-03-16 22:47:14 +000079 *
Denis Vlasenko9f00e052009-02-14 01:36:11 +000080 * Modified by Vladimir Oleynik for use with libbb/procps.c
Eric Andersenaad1a882001-03-16 22:47:14 +000081 */
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000082pid_t* FAST_FUNC find_pid_by_name(const char *procName)
Eric Andersenaad1a882001-03-16 22:47:14 +000083{
Denis Vlasenko35fb5122006-11-01 09:16:49 +000084 pid_t* pidList;
Denis Vlasenkobe905d52006-09-27 14:17:31 +000085 int i = 0;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000086 procps_status_t* p = NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000087
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000088 pidList = xzalloc(sizeof(*pidList));
Alexander Shishkine766f622009-07-29 01:35:13 +020089 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) {
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000090 if (comm_match(p, procName)
Denis Vlasenkof7d07b12007-06-30 08:03:26 +000091 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
92 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
Alexander Shishkine766f622009-07-29 01:35:13 +020093 /* or we require /proc/PID/exe link to match */
94 || (p->exe && strcmp(bb_basename(p->exe), procName) == 0)
Denis Vlasenkof7d07b12007-06-30 08:03:26 +000095 ) {
Denis Vlasenko3b3ca112008-07-17 18:39:36 +000096 pidList = xrealloc_vector(pidList, 2, i);
Denis Vlasenkobe905d52006-09-27 14:17:31 +000097 pidList[i++] = p->pid;
Eric Andersenaad1a882001-03-16 22:47:14 +000098 }
99 }
100
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000101 pidList[i] = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +0000102 return pidList;
103}
Eric Andersenaad1a882001-03-16 22:47:14 +0000104
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000105pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +0000106{
Denis Vlasenkobe905d52006-09-27 14:17:31 +0000107 int i = 0;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000108 while (pidList[i])
109 i++;
110 if (--i >= 0) {
111 pid_t k;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +0000112 int j;
113 for (j = 0; i > j; i--, j++) {
114 k = pidList[i];
115 pidList[i] = pidList[j];
116 pidList[j] = k;
117 }
118 }
119 return pidList;
120}