Merge branch 'Teaman-ND' into Teaman-RT
[tomato.git] / release / src / router / busybox / libbb / find_pid_by_name.c
blobdb823d05b972323e2180e87f2a0892195787c01b
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
10 #include "libbb.h"
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.
18 kernel threads:
19 comm: thread name
20 cmdline: empty
21 exe: <readlink fails>
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)
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)
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".
41 static int comm_match(procps_status_t *p, const char *procName)
43 int argv1idx;
44 const char *argv1;
46 if (strncmp(p->comm, procName, 15) != 0)
47 return 0; /* comm does not match */
49 /* 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 */
55 /* comm is truncated, but first 15 chars match.
56 * This can be crazily_long_script_name.sh!
57 * The telltale sign is basename(argv[1]) == procName */
59 if (!p->argv0)
60 return 0;
62 argv1idx = strlen(p->argv0) + 1;
63 if (argv1idx >= p->argv_len)
64 return 0;
65 argv1 = p->argv0 + argv1idx;
67 if (strcmp(bb_basename(argv1), procName) != 0)
68 return 0;
70 return 1;
73 /* This finds the pid of the specified process.
74 * Currently, it's implemented by rummaging through
75 * the proc filesystem.
77 * Returns a list of all matching PIDs
78 * It is the caller's duty to free the returned pidlist.
80 * Modified by Vladimir Oleynik for use with libbb/procps.c
82 pid_t* FAST_FUNC find_pid_by_name(const char *procName)
84 pid_t* pidList;
85 int i = 0;
86 procps_status_t* p = NULL;
88 pidList = xzalloc(sizeof(*pidList));
89 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) {
90 if (comm_match(p, procName)
91 /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
92 || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
93 /* or we require /proc/PID/exe link to match */
94 || (p->exe && strcmp(bb_basename(p->exe), procName) == 0)
95 ) {
96 pidList = xrealloc_vector(pidList, 2, i);
97 pidList[i++] = p->pid;
101 pidList[i] = 0;
102 return pidList;
105 pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
107 int i = 0;
108 while (pidList[i])
109 i++;
110 if (--i >= 0) {
111 pid_t k;
112 int j;
113 for (j = 0; i > j; i--, j++) {
114 k = pidList[i];
115 pidList[i] = pidList[j];
116 pidList[j] = k;
119 return pidList;