summaryrefslogtreecommitdiff
path: root/libbb/find_pid_by_name.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-10-22 12:21:15 +0000
committerEric Andersen <andersen@codepoet.org>2002-10-22 12:21:15 +0000
commit44608e9693b03661fbab5e27650bb040c6871d11 (patch)
tree4555230653cdb82d998f076b29130d8fe18a6f7a /libbb/find_pid_by_name.c
parent1887b0478f2743ce7808e8b37462e18d584611e1 (diff)
downloadbusybox-w32-44608e9693b03661fbab5e27650bb040c6871d11.tar.gz
busybox-w32-44608e9693b03661fbab5e27650bb040c6871d11.tar.bz2
busybox-w32-44608e9693b03661fbab5e27650bb040c6871d11.zip
Patch last_pach62 from vodz. This patch moves all the /proc parsing
code into libbb so it can be shared by ps, top, etc, saving over 1.5k.
Diffstat (limited to 'libbb/find_pid_by_name.c')
-rw-r--r--libbb/find_pid_by_name.c54
1 files changed, 10 insertions, 44 deletions
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index 4eaee03e3..a648137e1 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -22,7 +22,6 @@
22#include <stdio.h> 22#include <stdio.h>
23#include <ctype.h> 23#include <ctype.h>
24#include <string.h> 24#include <string.h>
25#include <dirent.h>
26#include <stdlib.h> 25#include <stdlib.h>
27#include "libbb.h" 26#include "libbb.h"
28 27
@@ -40,7 +39,7 @@
40 * 39 *
41 * Returns a list of all matching PIDs 40 * Returns a list of all matching PIDs
42 */ 41 */
43extern long* find_pid_by_name( char* pidName) 42extern long* find_pid_by_name( const char* pidName)
44{ 43{
45 int fd, i, j; 44 int fd, i, j;
46 char device[] = "/dev/ps"; 45 char device[] = "/dev/ps";
@@ -112,61 +111,28 @@ extern long* find_pid_by_name( char* pidName)
112 111
113/* find_pid_by_name() 112/* find_pid_by_name()
114 * 113 *
114 * Modified by Vladimir Oleynik for use with libbb/procps.c
115 * This finds the pid of the specified process. 115 * This finds the pid of the specified process.
116 * Currently, it's implemented by rummaging through 116 * Currently, it's implemented by rummaging through
117 * the proc filesystem. 117 * the proc filesystem.
118 * 118 *
119 * Returns a list of all matching PIDs 119 * Returns a list of all matching PIDs
120 */ 120 */
121extern long* find_pid_by_name( char* pidName) 121extern long* find_pid_by_name( const char* pidName)
122{ 122{
123 DIR *dir; 123 long* pidList;
124 struct dirent *next;
125 long* pidList=NULL;
126 int i=0; 124 int i=0;
125 procps_status_t * p;
127 126
128 dir = opendir("/proc"); 127 pidList = xmalloc(sizeof(long));
129 if (!dir) 128 while ((p = procps_scan(0)) != 0) {
130 perror_msg_and_die("Cannot open /proc"); 129 if (strcmp(p->short_cmd, pidName) == 0) {
131
132 while ((next = readdir(dir)) != NULL) {
133 FILE *status;
134 char filename[READ_BUF_SIZE];
135 char buffer[READ_BUF_SIZE];
136 char name[READ_BUF_SIZE];
137
138 /* Must skip ".." since that is outside /proc */
139 if (strcmp(next->d_name, "..") == 0)
140 continue;
141
142 /* If it isn't a number, we don't want it */
143 if (!isdigit(*next->d_name))
144 continue;
145
146 sprintf(filename, "/proc/%s/status", next->d_name);
147 if (! (status = fopen(filename, "r")) ) {
148 continue;
149 }
150 if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
151 fclose(status);
152 continue;
153 }
154 fclose(status);
155
156 /* Buffer should contain a string like "Name: binary_name" */
157 sscanf(buffer, "%*s %s", name);
158 if (strcmp(name, pidName) == 0) {
159 pidList=xrealloc( pidList, sizeof(long) * (i+2)); 130 pidList=xrealloc( pidList, sizeof(long) * (i+2));
160 pidList[i++]=strtol(next->d_name, NULL, 0); 131 pidList[i++]=p->pid;
161 } 132 }
162 } 133 }
163 134
164 if (pidList) { 135 pidList[i] = i==0 ? -1 : 0;
165 pidList[i]=0;
166 } else {
167 pidList=xrealloc( pidList, sizeof(long));
168 pidList[0]=-1;
169 }
170 return pidList; 136 return pidList;
171} 137}
172#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */ 138#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */