remove casts from xmalloc()
[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 /* 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  */
22 pid_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
40 pid_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 }