killall, pidof: use argv0 for process matching too
[oweals/busybox.git] / libbb / find_pid_by_name.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 #include "libbb.h"
11
12 /*
13 In Linux we have three ways to determine "process name":
14 1. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field.
15 2. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall.
16 3. /proc/PID/exe symlink. Points to the running executable file.
17
18 kernel threads:
19  comm: thread name
20  cmdline: empty
21  exe: <readlink fails>
22
23 executable
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
29 script (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
35 If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y),
36 some commands started from busybox shell, xargs or find are started by
37 execXXX("/proc/self/exe", applet_name, params....)
38 and therefore comm field contains "exe".
39 */
40
41 static const char *bb_basename(const char *name)
42 {
43         const char *cp = strrchr(name, '/');
44         if (cp)
45                 return cp + 1;
46         return name;
47 }
48
49 /* find_pid_by_name()
50  *
51  *  Modified by Vladimir Oleynik for use with libbb/procps.c
52  *  This finds the pid of the specified process.
53  *  Currently, it's implemented by rummaging through
54  *  the proc filesystem.
55  *
56  *  Returns a list of all matching PIDs
57  *  It is the caller's duty to free the returned pidlist.
58  */
59 pid_t* find_pid_by_name(const char* procName)
60 {
61         pid_t* pidList;
62         int i = 0;
63         procps_status_t* p = NULL;
64
65         pidList = xmalloc(sizeof(*pidList));
66         while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) {
67                 if (
68                 /* we require comm to match and to not be truncated */
69                 /* in Linux, if comm is 15 chars, it may be a truncated
70                  * name, so we don't allow that to match */
71                     (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0)
72                 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
73                  || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
74                 /* TOOD: we can also try exe, do we want that? */
75                 ) {
76                         pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
77                         pidList[i++] = p->pid;
78                 }
79         }
80
81         pidList[i] = 0;
82         return pidList;
83 }
84
85 pid_t *pidlist_reverse(pid_t *pidList)
86 {
87         int i = 0;
88         while (pidList[i])
89                 i++;
90         if (--i >= 0) {
91                 pid_t k;
92                 int j;
93                 for (j = 0; i > j; i--, j++) {
94                         k = pidList[i];
95                         pidList[i] = pidList[j];
96                         pidList[j] = k;
97                 }
98         }
99         return pidList;
100 }