This depmod script will create a modules.dep file, just like the depmod
[oweals/busybox.git] / libbb / find_pid_by_name.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) tons of folks.  Tracking down who wrote what
6  * isn't something I'm going to worry about...  If you wrote something
7  * here, please feel free to acknowledge your work.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 
24  * Permission has been granted to redistribute this code under the GPL.
25  *
26  */
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include "libbb.h"
34
35 #define READ_BUF_SIZE   50
36
37
38 /* For Erik's nifty devps device driver */
39 #ifdef BB_FEATURE_USE_DEVPS_PATCH
40 #include <linux/devps.h> 
41
42 /* find_pid_by_name()
43  *  
44  *  This finds the pid of the specified process,
45  *  by using the /dev/ps device driver.
46  *
47  *  Returns a list of all matching PIDs
48  */
49 extern pid_t* find_pid_by_name( char* pidName)
50 {
51         int fd, i, j;
52         char device[] = "/dev/ps";
53         pid_t num_pids;
54         pid_t* pid_array = NULL;
55         pid_t* pidList=NULL;
56
57         /* open device */ 
58         fd = open(device, O_RDONLY);
59         if (fd < 0)
60                 perror_msg_and_die("open failed for `%s'", device);
61
62         /* Find out how many processes there are */
63         if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) 
64                 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
65         
66         /* Allocate some memory -- grab a few extras just in case 
67          * some new processes start up while we wait. The kernel will
68          * just ignore any extras if we give it too many, and will trunc.
69          * the list if we give it too few.  */
70         pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
71         pid_array[0] = num_pids+10;
72
73         /* Now grab the pid list */
74         if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) 
75                 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
76
77         /* Now search for a match */
78         for (i=1, j=0; i<pid_array[0] ; i++) {
79                 char* p;
80                 struct pid_info info;
81
82             info.pid = pid_array[i];
83             if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
84                         perror_msg_and_die("\nDEVPS_GET_PID_INFO");
85
86                 /* Make sure we only match on the process name */
87                 p=info.command_line+1;
88                 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) { 
89                         (p)++;
90                 }
91                 if (isspace(*(p)))
92                                 *p='\0';
93
94                 if ((strstr(info.command_line, pidName) != NULL)
95                                 && (strlen(pidName) == strlen(info.command_line))) {
96                         pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
97                         pidList[j++]=info.pid;
98                 }
99         }
100         if (pidList) {
101                 pidList[j]=0;
102         } else if ( strcmp(pidName, "init")==0) {
103                 /* If we found nothing and they were trying to kill "init", 
104                  * guess PID 1 and call it good...  Perhaps we should simply
105                  * exit if /proc isn't mounted, but this will do for now. */
106                 pidList=xrealloc( pidList, sizeof(pid_t));
107                 pidList[0]=1;
108         } else {
109                 pidList=xrealloc( pidList, sizeof(pid_t));
110                 pidList[0]=-1;
111         }
112
113         /* Free memory */
114         free( pid_array);
115
116         /* close device */
117         if (close (fd) != 0) 
118                 perror_msg_and_die("close failed for `%s'", device);
119
120         return pidList;
121 }
122
123 #else           /* BB_FEATURE_USE_DEVPS_PATCH */
124
125 /* find_pid_by_name()
126  *  
127  *  This finds the pid of the specified process.
128  *  Currently, it's implemented by rummaging through 
129  *  the proc filesystem.
130  *
131  *  Returns a list of all matching PIDs
132  */
133 extern pid_t* find_pid_by_name( char* pidName)
134 {
135         DIR *dir;
136         struct dirent *next;
137         pid_t* pidList=NULL;
138         int i=0;
139
140         dir = opendir("/proc");
141         if (!dir)
142                 perror_msg_and_die("Cannot open /proc");
143         
144         while ((next = readdir(dir)) != NULL) {
145                 FILE *status;
146                 char filename[READ_BUF_SIZE];
147                 char buffer[READ_BUF_SIZE];
148                 char name[READ_BUF_SIZE];
149
150                 /* Must skip ".." since that is outside /proc */
151                 if (strcmp(next->d_name, "..") == 0)
152                         continue;
153
154                 /* If it isn't a number, we don't want it */
155                 if (!isdigit(*next->d_name))
156                         continue;
157
158                 sprintf(filename, "/proc/%s/status", next->d_name);
159                 if (! (status = fopen(filename, "r")) ) {
160                         continue;
161                 }
162                 if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
163                         fclose(status);
164                         continue;
165                 }
166                 fclose(status);
167
168                 /* Buffer should contain a string like "Name:   binary_name" */
169                 sscanf(buffer, "%*s %s", name);
170                 if (strcmp(name, pidName) == 0) {
171                         pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
172                         pidList[i++]=strtol(next->d_name, NULL, 0);
173                 }
174         }
175
176         if (pidList)
177                 pidList[i]=0;
178         else if ( strcmp(pidName, "init")==0) {
179                 /* If we found nothing and they were trying to kill "init", 
180                  * guess PID 1 and call it good...  Perhaps we should simply
181                  * exit if /proc isn't mounted, but this will do for now. */
182                 pidList=xrealloc( pidList, sizeof(pid_t));
183                 pidList[0]=1;
184         } else {
185                 pidList=xrealloc( pidList, sizeof(pid_t));
186                 pidList[0]=-1;
187         }
188         return pidList;
189 }
190 #endif                                                  /* BB_FEATURE_USE_DEVPS_PATCH */
191
192 /* END CODE */
193 /*
194 Local Variables:
195 c-file-style: "linux"
196 c-basic-offset: 4
197 tab-width: 4
198 End:
199 */