aboutsummaryrefslogtreecommitdiff
path: root/libbb/find_pid_by_name.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/find_pid_by_name.c')
-rw-r--r--libbb/find_pid_by_name.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
new file mode 100644
index 000000000..e98616940
--- /dev/null
+++ b/libbb/find_pid_by_name.c
@@ -0,0 +1,55 @@
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/* find_pid_by_name()
13 *
14 * Modified by Vladimir Oleynik for use with libbb/procps.c
15 * This finds the pid of the specified process.
16 * Currently, it's implemented by rummaging through
17 * the proc filesystem.
18 *
19 * Returns a list of all matching PIDs
20 * It is the caller's duty to free the returned pidlist.
21 */
22pid_t* find_pid_by_name(const char* procName)
23{
24 pid_t* pidList;
25 int i = 0;
26 procps_status_t* p = NULL;
27
28 pidList = xmalloc(sizeof(*pidList));
29 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) {
30 if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) {
31 pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
32 pidList[i++] = p->pid;
33 }
34 }
35
36 pidList[i] = 0;
37 return pidList;
38}
39
40pid_t *pidlist_reverse(pid_t *pidList)
41{
42 int i = 0;
43 while (pidList[i])
44 i++;
45 if (--i >= 0) {
46 pid_t k;
47 int j;
48 for (j = 0; i > j; i--, j++) {
49 k = pidList[i];
50 pidList[i] = pidList[j];
51 pidList[j] = k;
52 }
53 }
54 return pidList;
55}