1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define READ_BUF_SIZE 50
32 /* For Erik's nifty devps device driver */
33 #ifdef CONFIG_FEATURE_USE_DEVPS_PATCH
34 #include <linux/devps.h>
38 * This finds the pid of the specified process,
39 * by using the /dev/ps device driver.
41 * Returns a list of all matching PIDs
43 extern pid_t* find_pid_by_name( char* pidName)
46 char device[] = "/dev/ps";
48 pid_t* pid_array = NULL;
52 fd = open(device, O_RDONLY);
54 perror_msg_and_die("open failed for `%s'", device);
56 /* Find out how many processes there are */
57 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
58 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
60 /* Allocate some memory -- grab a few extras just in case
61 * some new processes start up while we wait. The kernel will
62 * just ignore any extras if we give it too many, and will trunc.
63 * the list if we give it too few. */
64 pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
65 pid_array[0] = num_pids+10;
67 /* Now grab the pid list */
68 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
69 perror_msg_and_die("\nDEVPS_GET_PID_LIST");
71 /* Now search for a match */
72 for (i=1, j=0; i<pid_array[0] ; i++) {
76 info.pid = pid_array[i];
77 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
78 perror_msg_and_die("\nDEVPS_GET_PID_INFO");
80 /* Make sure we only match on the process name */
81 p=info.command_line+1;
82 while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) {
88 if ((strstr(info.command_line, pidName) != NULL)
89 && (strlen(pidName) == strlen(info.command_line))) {
90 pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
91 pidList[j++]=info.pid;
96 } else if ( strcmp(pidName, "init")==0) {
97 /* If we found nothing and they were trying to kill "init",
98 * guess PID 1 and call it good... Perhaps we should simply
99 * exit if /proc isn't mounted, but this will do for now. */
100 pidList=xrealloc( pidList, sizeof(pid_t));
103 pidList=xrealloc( pidList, sizeof(pid_t));
112 perror_msg_and_die("close failed for `%s'", device);
117 #else /* CONFIG_FEATURE_USE_DEVPS_PATCH */
119 /* find_pid_by_name()
121 * This finds the pid of the specified process.
122 * Currently, it's implemented by rummaging through
123 * the proc filesystem.
125 * Returns a list of all matching PIDs
127 extern pid_t* find_pid_by_name( char* pidName)
134 dir = opendir("/proc");
136 perror_msg_and_die("Cannot open /proc");
138 while ((next = readdir(dir)) != NULL) {
140 char filename[READ_BUF_SIZE];
141 char buffer[READ_BUF_SIZE];
142 char name[READ_BUF_SIZE];
144 /* Must skip ".." since that is outside /proc */
145 if (strcmp(next->d_name, "..") == 0)
148 /* If it isn't a number, we don't want it */
149 if (!isdigit(*next->d_name))
152 sprintf(filename, "/proc/%s/status", next->d_name);
153 if (! (status = fopen(filename, "r")) ) {
156 if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
162 /* Buffer should contain a string like "Name: binary_name" */
163 sscanf(buffer, "%*s %s", name);
164 if (strcmp(name, pidName) == 0) {
165 pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
166 pidList[i++]=strtol(next->d_name, NULL, 0);
172 else if ( strcmp(pidName, "init")==0) {
173 /* If we found nothing and they were trying to kill "init",
174 * guess PID 1 and call it good... Perhaps we should simply
175 * exit if /proc isn't mounted, but this will do for now. */
176 pidList=xrealloc( pidList, sizeof(pid_t));
179 pidList=xrealloc( pidList, sizeof(pid_t));
184 #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */
189 c-file-style: "linux"