diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-03-16 22:47:14 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-03-16 22:47:14 +0000 |
commit | aad1a88c76f208d188fd061e3723bd637437e8d5 (patch) | |
tree | 75ac2b994f7f04af776e93eeedba82b873ff557e /libbb/find_pid_by_name.c | |
parent | dfba13d199bb58d802df99c1ef20fdc0d628e86d (diff) | |
download | busybox-w32-aad1a88c76f208d188fd061e3723bd637437e8d5.tar.gz busybox-w32-aad1a88c76f208d188fd061e3723bd637437e8d5.tar.bz2 busybox-w32-aad1a88c76f208d188fd061e3723bd637437e8d5.zip |
Convert utility.c into libbb.a. It is now a whole pile of .c
files. Clean up the resulting damage and fix up the makefile.
-Erik
Diffstat (limited to 'libbb/find_pid_by_name.c')
-rw-r--r-- | libbb/find_pid_by_name.c | 168 |
1 files changed, 168 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..a22ee1ff8 --- /dev/null +++ b/libbb/find_pid_by_name.c | |||
@@ -0,0 +1,168 @@ | |||
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 | |||
36 | /* For Erik's nifty devps device driver */ | ||
37 | #ifdef BB_FEATURE_USE_DEVPS_PATCH | ||
38 | #include <linux/devps.h> | ||
39 | |||
40 | /* find_pid_by_name() | ||
41 | * | ||
42 | * This finds the pid of the specified process, | ||
43 | * by using the /dev/ps device driver. | ||
44 | * | ||
45 | * Returns a list of all matching PIDs | ||
46 | */ | ||
47 | extern pid_t* find_pid_by_name( char* pidName) | ||
48 | { | ||
49 | int fd, i, j; | ||
50 | char device[] = "/dev/ps"; | ||
51 | pid_t num_pids; | ||
52 | pid_t* pid_array = NULL; | ||
53 | pid_t* pidList=NULL; | ||
54 | |||
55 | /* open device */ | ||
56 | fd = open(device, O_RDONLY); | ||
57 | if (fd < 0) | ||
58 | perror_msg_and_die("open failed for `%s'", device); | ||
59 | |||
60 | /* Find out how many processes there are */ | ||
61 | if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0) | ||
62 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); | ||
63 | |||
64 | /* Allocate some memory -- grab a few extras just in case | ||
65 | * some new processes start up while we wait. The kernel will | ||
66 | * just ignore any extras if we give it too many, and will trunc. | ||
67 | * the list if we give it too few. */ | ||
68 | pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t)); | ||
69 | pid_array[0] = num_pids+10; | ||
70 | |||
71 | /* Now grab the pid list */ | ||
72 | if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0) | ||
73 | perror_msg_and_die("\nDEVPS_GET_PID_LIST"); | ||
74 | |||
75 | /* Now search for a match */ | ||
76 | for (i=1, j=0; i<pid_array[0] ; i++) { | ||
77 | char* p; | ||
78 | struct pid_info info; | ||
79 | |||
80 | info.pid = pid_array[i]; | ||
81 | if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0) | ||
82 | perror_msg_and_die("\nDEVPS_GET_PID_INFO"); | ||
83 | |||
84 | /* Make sure we only match on the process name */ | ||
85 | p=info.command_line+1; | ||
86 | while ((*p != 0) && !isspace(*(p)) && (*(p-1) != '\\')) { | ||
87 | (p)++; | ||
88 | } | ||
89 | if (isspace(*(p))) | ||
90 | *p='\0'; | ||
91 | |||
92 | if ((strstr(info.command_line, pidName) != NULL) | ||
93 | && (strlen(pidName) == strlen(info.command_line))) { | ||
94 | pidList=xrealloc( pidList, sizeof(pid_t) * (j+2)); | ||
95 | pidList[j++]=info.pid; | ||
96 | } | ||
97 | } | ||
98 | if (pidList) | ||
99 | pidList[j]=0; | ||
100 | |||
101 | /* Free memory */ | ||
102 | free( pid_array); | ||
103 | |||
104 | /* close device */ | ||
105 | if (close (fd) != 0) | ||
106 | perror_msg_and_die("close failed for `%s'", device); | ||
107 | |||
108 | return pidList; | ||
109 | } | ||
110 | |||
111 | #else /* BB_FEATURE_USE_DEVPS_PATCH */ | ||
112 | |||
113 | /* find_pid_by_name() | ||
114 | * | ||
115 | * This finds the pid of the specified process. | ||
116 | * Currently, it's implemented by rummaging through | ||
117 | * the proc filesystem. | ||
118 | * | ||
119 | * Returns a list of all matching PIDs | ||
120 | */ | ||
121 | extern pid_t* find_pid_by_name( char* pidName) | ||
122 | { | ||
123 | DIR *dir; | ||
124 | struct dirent *next; | ||
125 | pid_t* pidList=NULL; | ||
126 | int i=0; | ||
127 | |||
128 | dir = opendir("/proc"); | ||
129 | if (!dir) | ||
130 | perror_msg_and_die("Cannot open /proc"); | ||
131 | |||
132 | while ((next = readdir(dir)) != NULL) { | ||
133 | FILE *status; | ||
134 | char filename[256]; | ||
135 | char buffer[256]; | ||
136 | |||
137 | /* If it isn't a number, we don't want it */ | ||
138 | if (!isdigit(*next->d_name)) | ||
139 | continue; | ||
140 | |||
141 | sprintf(filename, "/proc/%s/cmdline", next->d_name); | ||
142 | status = fopen(filename, "r"); | ||
143 | if (!status) { | ||
144 | continue; | ||
145 | } | ||
146 | fgets(buffer, 256, status); | ||
147 | fclose(status); | ||
148 | |||
149 | if (strstr(get_last_path_component(buffer), pidName) != NULL) { | ||
150 | pidList=xrealloc( pidList, sizeof(pid_t) * (i+2)); | ||
151 | pidList[i++]=strtol(next->d_name, NULL, 0); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | if (pidList) | ||
156 | pidList[i]=0; | ||
157 | return pidList; | ||
158 | } | ||
159 | #endif /* BB_FEATURE_USE_DEVPS_PATCH */ | ||
160 | |||
161 | /* END CODE */ | ||
162 | /* | ||
163 | Local Variables: | ||
164 | c-file-style: "linux" | ||
165 | c-basic-offset: 4 | ||
166 | tab-width: 4 | ||
167 | End: | ||
168 | */ | ||