summaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-03-07 23:32:17 +0000
committerErik Andersen <andersen@codepoet.org>2000-03-07 23:32:17 +0000
commit2ac2fae728cca8a535b29bdd2fa6899e6f4992f2 (patch)
tree76eb5871ac3cde7d58048aadba75c7f40fab93b7 /utility.c
parentcbd0d625c7466af80f141e0ae24186e15987bf3e (diff)
downloadbusybox-w32-2ac2fae728cca8a535b29bdd2fa6899e6f4992f2.tar.gz
busybox-w32-2ac2fae728cca8a535b29bdd2fa6899e6f4992f2.tar.bz2
busybox-w32-2ac2fae728cca8a535b29bdd2fa6899e6f4992f2.zip
Fix bugs related to finding PIDs.
-Erik
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/utility.c b/utility.c
index 789c6f228..c3f9947c5 100644
--- a/utility.c
+++ b/utility.c
@@ -1247,7 +1247,7 @@ extern int device_open(char *device, int mode)
1247#endif /* BB_INIT BB_SYSLOGD */ 1247#endif /* BB_INIT BB_SYSLOGD */
1248 1248
1249 1249
1250#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT || defined BB_KILLALL 1250#if defined BB_KILLALL || defined BB_FEATURE_LINUXRC && ( defined BB_HALT || defined BB_REBOOT || defined BB_POWEROFF )
1251 1251
1252#ifdef BB_FEATURE_USE_DEVPS_N_DEVMTAB 1252#ifdef BB_FEATURE_USE_DEVPS_N_DEVMTAB
1253#include <linux/devps.h> 1253#include <linux/devps.h>
@@ -1318,6 +1318,7 @@ extern pid_t findPidByName( char* pidName)
1318#if ! defined BB_FEATURE_USE_PROCFS 1318#if ! defined BB_FEATURE_USE_PROCFS
1319#error Sorry, I depend on the /proc filesystem right now. 1319#error Sorry, I depend on the /proc filesystem right now.
1320#endif 1320#endif
1321
1321/* findPidByName() 1322/* findPidByName()
1322 * 1323 *
1323 * This finds the pid of the specified process. 1324 * This finds the pid of the specified process.
@@ -1330,15 +1331,24 @@ extern pid_t findPidByName( char* pidName)
1330 */ 1331 */
1331extern pid_t findPidByName( char* pidName) 1332extern pid_t findPidByName( char* pidName)
1332{ 1333{
1333 pid_t thePid; 1334 DIR *dir;
1334 char filename[256]; 1335 struct dirent *next;
1335 char buffer[256];
1336 1336
1337 /* no need to opendir ;) */ 1337 dir = opendir("/proc");
1338 for (thePid = 1; thePid < 65536; thePid++) { 1338 if (!dir)
1339 fatalError( "Cannot open /proc: %s\n", strerror (errno));
1340
1341 while ((next = readdir(dir)) != NULL) {
1339 FILE *status; 1342 FILE *status;
1343 char filename[256];
1344 char buffer[256];
1345
1346 /* If it isn't a number, we don't want it */
1347 if (!isdigit(*next->d_name))
1348 continue;
1340 1349
1341 sprintf(filename, "/proc/%d/cmdline", thePid); 1350 /* Now open the command line file */
1351 sprintf(filename, "/proc/%s/status", next->d_name);
1342 status = fopen(filename, "r"); 1352 status = fopen(filename, "r");
1343 if (!status) { 1353 if (!status) {
1344 continue; 1354 continue;
@@ -1347,13 +1357,13 @@ extern pid_t findPidByName( char* pidName)
1347 fclose(status); 1357 fclose(status);
1348 1358
1349 if ((strstr(buffer, pidName) != NULL)) { 1359 if ((strstr(buffer, pidName) != NULL)) {
1350 return thePid; 1360 return strtol(next->d_name, NULL, 0);
1351 } 1361 }
1352 } 1362 }
1353 return 0; 1363 return 0;
1354} 1364}
1355#endif /* BB_FEATURE_USE_DEVPS_N_DEVMTAB */ 1365#endif /* BB_FEATURE_USE_DEVPS_N_DEVMTAB */
1356#endif /* BB_INIT || BB_HALT || BB_REBOOT || KILLALL */ 1366#endif /* BB_INIT || BB_HALT || BB_REBOOT || BB_KILLALL || BB_POWEROFF */
1357 1367
1358#if defined BB_GUNZIP \ 1368#if defined BB_GUNZIP \
1359 || defined BB_GZIP \ 1369 || defined BB_GZIP \