aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c101
1 files changed, 90 insertions, 11 deletions
diff --git a/utility.c b/utility.c
index 64c1a4827..7de997474 100644
--- a/utility.c
+++ b/utility.c
@@ -56,6 +56,13 @@
56#include <linux/loop.h> 56#include <linux/loop.h>
57#endif 57#endif
58 58
59/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
60 * list of available filesystems used for the -t auto option */
61#if defined BB_FEATURE_USE_PROCFS && defined BB_FEATURE_USE_DEVPS_N_DEVMTAB
62//#error Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again.
63#error "Sorry, but busybox can't use both /proc and /dev/ps at the same time -- Pick one and try again."
64#endif
65
59 66
60#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF 67#if defined BB_MOUNT || defined BB_UMOUNT || defined BB_DF
61# if defined BB_FEATURE_USE_PROCFS 68# if defined BB_FEATURE_USE_PROCFS
@@ -64,9 +71,13 @@ const char mtab_file[] = "/proc/mounts";
64# if defined BB_MTAB 71# if defined BB_MTAB
65const char mtab_file[] = "/etc/mtab"; 72const char mtab_file[] = "/etc/mtab";
66# else 73# else
67# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or BB_FEATURE_USE_PROCFS 74# if defined BB_FEATURE_USE_DEVPS_N_DEVMTAB
75const char mtab_file[] = "/dev/mtab";
76# else
77# error With (BB_MOUNT||BB_UMOUNT||BB_DF) defined, you must define either BB_MTAB or ( BB_FEATURE_USE_PROCFS | BB_FEATURE_USE_DEVPS_N_DEVMTAB)
68# endif 78# endif
69# endif 79# endif
80# endif
70#endif 81#endif
71 82
72 83
@@ -1238,29 +1249,96 @@ extern int device_open(char *device, int mode)
1238 1249
1239#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT 1250#if defined BB_INIT || defined BB_HALT || defined BB_REBOOT
1240 1251
1252#ifdef BB_FEATURE_USE_DEVPS_N_DEVMTAB
1253#include <linux/devps.h>
1254#endif
1255
1256#if defined BB_FEATURE_USE_DEVPS_N_DEVMTAB
1257/* findPidByName()
1258 *
1259 * This finds the pid of the specified process,
1260 * by using the /dev/ps device driver.
1261 *
1262 * [return]
1263 * 0 failure
1264 * pid when the pid is found.
1265 */
1266extern pid_t findPidByName( char* pidName)
1267{
1268 int fd, i;
1269 char device[] = "/dev/ps";
1270 pid_t thePid = 0;
1271 pid_t num_pids;
1272 pid_t* pid_array = NULL;
1273
1274 /* open device */
1275 fd = open(device, O_RDONLY);
1276 if (fd < 0)
1277 fatalError( "open failed for `%s': %s\n", device, strerror (errno));
1278
1279 /* Find out how many processes there are */
1280 if (ioctl (fd, DEVPS_GET_NUM_PIDS, &num_pids)<0)
1281 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1282
1283 /* Allocate some memory -- grab a few extras just in case
1284 * some new processes start up while we wait. The kernel will
1285 * just ignore any extras if we give it too many, and will trunc.
1286 * the list if we give it too few. */
1287 pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
1288 pid_array[0] = num_pids+10;
1289
1290 /* Now grab the pid list */
1291 if (ioctl (fd, DEVPS_GET_PID_LIST, pid_array)<0)
1292 fatalError( "\nDEVPS_GET_PID_LIST: %s\n", strerror (errno));
1293
1294 /* Now search for a match */
1295 for (i=1; i<pid_array[0] ; i++) {
1296 struct pid_info info;
1297
1298 info.pid = pid_array[i];
1299 if (ioctl (fd, DEVPS_GET_PID_INFO, &info)<0)
1300 fatalError( "\nDEVPS_GET_PID_INFO: %s\n", strerror (errno));
1301
1302 if ((strstr(info.command_line, pidName) != NULL)) {
1303 thePid = info.pid;
1304 break;
1305 }
1306 }
1307
1308 /* Free memory */
1309 free( pid_array);
1310
1311 /* close device */
1312 if (close (fd) != 0)
1313 fatalError( "close failed for `%s': %s\n",device, strerror (errno));
1314
1315 return thePid;
1316}
1317#else /* BB_FEATURE_USE_DEVPS_N_DEVMTAB */
1241#if ! defined BB_FEATURE_USE_PROCFS 1318#if ! defined BB_FEATURE_USE_PROCFS
1242#error Sorry, I depend on the /proc filesystem right now. 1319#error Sorry, I depend on the /proc filesystem right now.
1243#endif 1320#endif
1244/* findInitPid() 1321/* findPidByName()
1245 * 1322 *
1246 * This finds the pid of init (which is not always 1). 1323 * This finds the pid of the specified process.
1247 * Currently, it's implemented by rummaging through the proc filesystem. 1324 * Currently, it's implemented by rummaging through
1325 * the proc filesystem.
1248 * 1326 *
1249 * [return] 1327 * [return]
1250 * 0 failure 1328 * 0 failure
1251 * pid when init's pid is found. 1329 * pid when the pid is found.
1252 */ 1330 */
1253extern pid_t findInitPid() 1331extern pid_t findPidByName( char* pidName)
1254{ 1332{
1255 pid_t init_pid; 1333 pid_t thePid;
1256 char filename[256]; 1334 char filename[256];
1257 char buffer[256]; 1335 char buffer[256];
1258 1336
1259 /* no need to opendir ;) */ 1337 /* no need to opendir ;) */
1260 for (init_pid = 1; init_pid < 65536; init_pid++) { 1338 for (thePid = 1; thePid < 65536; thePid++) {
1261 FILE *status; 1339 FILE *status;
1262 1340
1263 sprintf(filename, "/proc/%d/cmdline", init_pid); 1341 sprintf(filename, "/proc/%d/cmdline", thePid);
1264 status = fopen(filename, "r"); 1342 status = fopen(filename, "r");
1265 if (!status) { 1343 if (!status) {
1266 continue; 1344 continue;
@@ -1268,12 +1346,13 @@ extern pid_t findInitPid()
1268 fgets(buffer, 256, status); 1346 fgets(buffer, 256, status);
1269 fclose(status); 1347 fclose(status);
1270 1348
1271 if ((strstr(buffer, "init") != NULL)) { 1349 if ((strstr(buffer, pidName) != NULL)) {
1272 return init_pid; 1350 return thePid;
1273 } 1351 }
1274 } 1352 }
1275 return 0; 1353 return 0;
1276} 1354}
1355#endif /* BB_FEATURE_USE_DEVPS_N_DEVMTAB */
1277#endif /* BB_INIT || BB_HALT || BB_REBOOT */ 1356#endif /* BB_INIT || BB_HALT || BB_REBOOT */
1278 1357
1279#if defined BB_GUNZIP \ 1358#if defined BB_GUNZIP \