Simplify CRC table generation
[oweals/busybox.git] / libbb / find_pid_by_name.c
index a22ee1ff8fd395b1a5f8cdf170a7c4bb868b055b..f183cc0bd9de3a43bb0559dc0a03b44a7cb06f2f 100644 (file)
@@ -2,9 +2,7 @@
 /*
  * Utility routines.
  *
- * Copyright (C) tons of folks.  Tracking down who wrote what
- * isn't something I'm going to worry about...  If you wrote something
- * here, please feel free to acknowledge your work.
+ * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Based in part on code from sash, Copyright (c) 1999 by David I. Bell 
- * Permission has been granted to redistribute this code under the GPL.
- *
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include "libbb.h"
 
+#define READ_BUF_SIZE  50
+
 
 /* For Erik's nifty devps device driver */
-#ifdef BB_FEATURE_USE_DEVPS_PATCH
+#ifdef CONFIG_FEATURE_USE_DEVPS_PATCH
 #include <linux/devps.h> 
 
 /* find_pid_by_name()
@@ -95,8 +91,18 @@ extern pid_t* find_pid_by_name( char* pidName)
                        pidList[j++]=info.pid;
                }
        }
-       if (pidList)
+       if (pidList) {
                pidList[j]=0;
+       } else if ( strcmp(pidName, "init")==0) {
+               /* If we found nothing and they were trying to kill "init", 
+                * guess PID 1 and call it good...  Perhaps we should simply
+                * exit if /proc isn't mounted, but this will do for now. */
+               pidList=xrealloc( pidList, sizeof(pid_t));
+               pidList[0]=1;
+       } else {
+               pidList=xrealloc( pidList, sizeof(pid_t));
+               pidList[0]=-1;
+       }
 
        /* Free memory */
        free( pid_array);
@@ -108,7 +114,7 @@ extern pid_t* find_pid_by_name( char* pidName)
        return pidList;
 }
 
-#else          /* BB_FEATURE_USE_DEVPS_PATCH */
+#else          /* CONFIG_FEATURE_USE_DEVPS_PATCH */
 
 /* find_pid_by_name()
  *  
@@ -131,22 +137,31 @@ extern pid_t* find_pid_by_name( char* pidName)
        
        while ((next = readdir(dir)) != NULL) {
                FILE *status;
-               char filename[256];
-               char buffer[256];
+               char filename[READ_BUF_SIZE];
+               char buffer[READ_BUF_SIZE];
+               char name[READ_BUF_SIZE];
+
+               /* Must skip ".." since that is outside /proc */
+               if (strcmp(next->d_name, "..") == 0)
+                       continue;
 
                /* If it isn't a number, we don't want it */
                if (!isdigit(*next->d_name))
                        continue;
 
-               sprintf(filename, "/proc/%s/cmdline", next->d_name);
-               status = fopen(filename, "r");
-               if (!status) {
+               sprintf(filename, "/proc/%s/status", next->d_name);
+               if (! (status = fopen(filename, "r")) ) {
+                       continue;
+               }
+               if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
+                       fclose(status);
                        continue;
                }
-               fgets(buffer, 256, status);
                fclose(status);
 
-               if (strstr(get_last_path_component(buffer), pidName) != NULL) {
+               /* Buffer should contain a string like "Name:   binary_name" */
+               sscanf(buffer, "%*s %s", name);
+               if (strcmp(name, pidName) == 0) {
                        pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
                        pidList[i++]=strtol(next->d_name, NULL, 0);
                }
@@ -154,9 +169,19 @@ extern pid_t* find_pid_by_name( char* pidName)
 
        if (pidList)
                pidList[i]=0;
+       else if ( strcmp(pidName, "init")==0) {
+               /* If we found nothing and they were trying to kill "init", 
+                * guess PID 1 and call it good...  Perhaps we should simply
+                * exit if /proc isn't mounted, but this will do for now. */
+               pidList=xrealloc( pidList, sizeof(pid_t));
+               pidList[0]=1;
+       } else {
+               pidList=xrealloc( pidList, sizeof(pid_t));
+               pidList[0]=-1;
+       }
        return pidList;
 }
-#endif                                                 /* BB_FEATURE_USE_DEVPS_PATCH */
+#endif                                                 /* CONFIG_FEATURE_USE_DEVPS_PATCH */
 
 /* END CODE */
 /*