aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/utility.c b/utility.c
index b91da4ce4..c3a102c5e 100644
--- a/utility.c
+++ b/utility.c
@@ -1259,17 +1259,15 @@ extern int device_open(char *device, int mode)
1259 * This finds the pid of the specified process, 1259 * This finds the pid of the specified process,
1260 * by using the /dev/ps device driver. 1260 * by using the /dev/ps device driver.
1261 * 1261 *
1262 * [return] 1262 * Returns a list of all matching PIDs
1263 * 0 failure
1264 * pid when the pid is found.
1265 */ 1263 */
1266extern pid_t findPidByName( char* pidName) 1264extern pid_t* findPidByName( char* pidName)
1267{ 1265{
1268 int fd, i; 1266 int fd, i, j;
1269 char device[] = "/dev/ps"; 1267 char device[] = "/dev/ps";
1270 pid_t thePid = 0;
1271 pid_t num_pids; 1268 pid_t num_pids;
1272 pid_t* pid_array = NULL; 1269 pid_t* pid_array = NULL;
1270 pid_t* pidList=NULL;
1273 1271
1274 /* open device */ 1272 /* open device */
1275 fd = open(device, O_RDONLY); 1273 fd = open(device, O_RDONLY);
@@ -1300,10 +1298,13 @@ extern pid_t findPidByName( char* pidName)
1300 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno)); 1298 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1301 1299
1302 if ((strstr(info.command_line, pidName) != NULL)) { 1300 if ((strstr(info.command_line, pidName) != NULL)) {
1303 thePid = info.pid; 1301 pidList=realloc( pidList, sizeof(pid_t) * (j+2));
1304 break; 1302 if (pidList==NULL)
1303 fatalError("out of memory\n");
1304 pidList[j++]=info.pid;
1305 } 1305 }
1306 } 1306 }
1307 pidList[j]=0;
1307 1308
1308 /* Free memory */ 1309 /* Free memory */
1309 free( pid_array); 1310 free( pid_array);
@@ -1312,7 +1313,7 @@ extern pid_t findPidByName( char* pidName)
1312 if (close (fd) != 0) 1313 if (close (fd) != 0)
1313 fatalError( "close failed for `%s': %s\n",device, strerror (errno)); 1314 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1314 1315
1315 return thePid; 1316 return pidList;
1316} 1317}
1317#else /* BB_FEATURE_USE_DEVPS_PATCH */ 1318#else /* BB_FEATURE_USE_DEVPS_PATCH */
1318#if ! defined BB_FEATURE_USE_PROCFS 1319#if ! defined BB_FEATURE_USE_PROCFS
@@ -1325,14 +1326,14 @@ extern pid_t findPidByName( char* pidName)
1325 * Currently, it's implemented by rummaging through 1326 * Currently, it's implemented by rummaging through
1326 * the proc filesystem. 1327 * the proc filesystem.
1327 * 1328 *
1328 * [return] 1329 * Returns a list of all matching PIDs
1329 * 0 failure
1330 * pid when the pid is found.
1331 */ 1330 */
1332extern pid_t findPidByName( char* pidName) 1331extern pid_t* findPidByName( char* pidName)
1333{ 1332{
1334 DIR *dir; 1333 DIR *dir;
1335 struct dirent *next; 1334 struct dirent *next;
1335 pid_t* pidList=NULL;
1336 int i=0;
1336 1337
1337 dir = opendir("/proc"); 1338 dir = opendir("/proc");
1338 if (!dir) 1339 if (!dir)
@@ -1347,7 +1348,7 @@ extern pid_t findPidByName( char* pidName)
1347 if (!isdigit(*next->d_name)) 1348 if (!isdigit(*next->d_name))
1348 continue; 1349 continue;
1349 1350
1350 /* Now open the command line file */ 1351 /* Now open the status file */
1351 sprintf(filename, "/proc/%s/status", next->d_name); 1352 sprintf(filename, "/proc/%s/status", next->d_name);
1352 status = fopen(filename, "r"); 1353 status = fopen(filename, "r");
1353 if (!status) { 1354 if (!status) {
@@ -1357,10 +1358,14 @@ extern pid_t findPidByName( char* pidName)
1357 fclose(status); 1358 fclose(status);
1358 1359
1359 if ((strstr(buffer, pidName) != NULL)) { 1360 if ((strstr(buffer, pidName) != NULL)) {
1360 return strtol(next->d_name, NULL, 0); 1361 pidList=realloc( pidList, sizeof(pid_t) * (i+2));
1362 if (pidList==NULL)
1363 fatalError("out of memory\n");
1364 pidList[i++]=strtol(next->d_name, NULL, 0);
1361 } 1365 }
1362 } 1366 }
1363 return 0; 1367 pidList[i]=0;
1368 return pidList;
1364} 1369}
1365#endif /* BB_FEATURE_USE_DEVPS_PATCH */ 1370#endif /* BB_FEATURE_USE_DEVPS_PATCH */
1366#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */ 1371#endif /* BB_KILLALL || ( BB_FEATURE_LINUXRC && ( BB_HALT || BB_REBOOT || BB_POWEROFF )) */
@@ -1371,7 +1376,7 @@ extern void *xmalloc(size_t size)
1371 void *cp = malloc(size); 1376 void *cp = malloc(size);
1372 1377
1373 if (cp == NULL) 1378 if (cp == NULL)
1374 fatalError("out of memory"); 1379 fatalError("out of memory\n");
1375 return cp; 1380 return cp;
1376} 1381}
1377 1382