blob: ae2f11643cb61a22bb86a7aaf1609f443348a92d [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 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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
Eric Andersenaad1a882001-03-16 22:47:14 +000041/* find_pid_by_name()
Eric Andersenc7bda1c2004-03-15 08:29:22 +000042 *
Eric Andersen44608e92002-10-22 12:21:15 +000043 * Modified by Vladimir Oleynik for use with libbb/procps.c
Eric Andersenaad1a882001-03-16 22:47:14 +000044 * This finds the pid of the specified process.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000045 * Currently, it's implemented by rummaging through
Eric Andersenaad1a882001-03-16 22:47:14 +000046 * the proc filesystem.
47 *
48 * Returns a list of all matching PIDs
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000049 * It is the caller's duty to free the returned pidlist.
Eric Andersenaad1a882001-03-16 22:47:14 +000050 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000051pid_t* FAST_FUNC find_pid_by_name(const char* procName)
Eric Andersenaad1a882001-03-16 22:47:14 +000052{
Denis Vlasenko35fb5122006-11-01 09:16:49 +000053 pid_t* pidList;
Denis Vlasenkobe905d52006-09-27 14:17:31 +000054 int i = 0;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000055 procps_status_t* p = NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000056
Denis Vlasenko35fb5122006-11-01 09:16:49 +000057 pidList = xmalloc(sizeof(*pidList));
Denis Vlasenkof7d07b12007-06-30 08:03:26 +000058 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) {
59 if (
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)*/
65 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
Denis Vlasenko6446c2d2007-11-25 04:54:13 +000066 /* TOOD: we can also try /proc/NUM/exe link, do we want that? */
Denis Vlasenkof7d07b12007-06-30 08:03:26 +000067 ) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +000068 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
Denis Vlasenkobe905d52006-09-27 14:17:31 +000069 pidList[i++] = p->pid;
Eric Andersenaad1a882001-03-16 22:47:14 +000070 }
71 }
72
Denis Vlasenko35fb5122006-11-01 09:16:49 +000073 pidList[i] = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000074 return pidList;
75}
Eric Andersenaad1a882001-03-16 22:47:14 +000076
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000077pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000078{
Denis Vlasenkobe905d52006-09-27 14:17:31 +000079 int i = 0;
Denis Vlasenko35fb5122006-11-01 09:16:49 +000080 while (pidList[i])
81 i++;
82 if (--i >= 0) {
83 pid_t k;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000084 int j;
85 for (j = 0; i > j; i--, j++) {
86 k = pidList[i];
87 pidList[i] = pidList[j];
88 pidList[j] = k;
89 }
90 }
91 return pidList;
92}