From f95ca97d1bb94307ee0b8ed95d329dc9d6ccec85 Mon Sep 17 00:00:00 2001
From: John Beppu <beppu@lbox.org>
Date: Thu, 9 Dec 1999 22:10:18 +0000
Subject:     findInitPid() has been implemented and it seems to work.    
 reboot has been changed to take advantage of findInitPid();

---
 init/reboot.c |  3 ++-
 internal.h    |  1 +
 reboot.c      |  3 ++-
 utility.c     | 43 ++++++++++++++++++++++++++++++++++---------
 4 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/init/reboot.c b/init/reboot.c
index 53a3520e1..17ec9f82f 100644
--- a/init/reboot.c
+++ b/init/reboot.c
@@ -26,5 +26,6 @@
 extern int
 reboot_main(int argc, char ** argv)
 {
-	exit( kill(1, SIGUSR2));
+	/* don't assume init's pid == 1 */
+	exit( kill(findInitPid(), SIGUSR2));
 }
diff --git a/internal.h b/internal.h
index 830d30eb4..de13bb202 100644
--- a/internal.h
+++ b/internal.h
@@ -157,6 +157,7 @@ extern void write_mtab(char* blockDevice, char* directory,
 extern void erase_mtab(const char * name);
 extern int check_wildcard_match(const char* text, const char* pattern);
 extern long getNum (const char *cp);
+extern pid_t findInitPid();
 
 
 #if defined BB_MTAB
diff --git a/reboot.c b/reboot.c
index 53a3520e1..17ec9f82f 100644
--- a/reboot.c
+++ b/reboot.c
@@ -26,5 +26,6 @@
 extern int
 reboot_main(int argc, char ** argv)
 {
-	exit( kill(1, SIGUSR2));
+	/* don't assume init's pid == 1 */
+	exit( kill(findInitPid(), SIGUSR2));
 }
diff --git a/utility.c b/utility.c
index de7a0fbfc..42ee00c50 100644
--- a/utility.c
+++ b/utility.c
@@ -1068,14 +1068,39 @@ extern long getNum (const char *cp)
 }
 #endif
 
+#if 1
+/* findInitPid()
+ *  
+ *  This finds the pid of init (which is not always 1).
+ *  Currently, it's implemented by rummaging through the proc filesystem.
+ *
+ *  [return]
+ *  0	    failure
+ *  pid	    when init's pid is found.
+ */
+extern pid_t
+findInitPid()
+{
+    pid_t   init_pid;
+    char    filename[256];
+    char    buffer[256];
+
+    /* no need to opendir ;) */
+    for (init_pid = 1; init_pid < 65536; init_pid++) {
+	FILE	*status;
+
+	sprintf(filename, "/proc/%d/status", init_pid);
+	status = fopen(filename, "r");
+	if (!status) { continue; }
+	fgets(buffer, 256, status);
+	fclose(status);
+
+	if ( (strcmp(&buffer[6], "init\n") == 0)) {
+	    return init_pid;
+	}
+    }
+    return 0;
+}
+#endif
 
 /* END CODE */
-
-
-
-
-
-
-
-
-
-- 
cgit v1.2.3-55-g6feb