Simplify CRC table generation
[oweals/busybox.git] / libbb / find_pid_by_name.c
index ea1cba65b7d128cf13158159077c8ccfeaf30bf0..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>
@@ -36,7 +30,7 @@
 
 
 /* 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()
@@ -97,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);
@@ -110,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()
  *  
@@ -137,6 +141,10 @@ extern pid_t* find_pid_by_name( char* pidName)
                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;
@@ -161,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 */
 /*