[PATCH] Rework shutDown() to better accomidate "nokill" better.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rework shutDown() so that we don't do unmounts until we're going to
reboot or power off the machine.  Also some minor cleanups like taking
out all the negative-phrasing of "noKill" in the code itself.
---
 loader/init.c     |   27 ++++++++--------
 loader/init.h     |   28 +++++++++++++++++
 loader/shutdown.c |   87 ++++++++++++++++++++++++++++++++++-------------------
 3 files changed, 97 insertions(+), 45 deletions(-)
 create mode 100644 loader/init.h

diff --git a/loader/init.c b/loader/init.c
index 1c275d8..ff0f62e 100644
--- a/loader/init.c
+++ b/loader/init.c
@@ -53,6 +53,7 @@
 #include <termios.h>
 #include <libgen.h>
 
+#include "init.h"
 #include "copy.h"
 #include "devt.h"
 #include "devices.h"
@@ -110,10 +111,8 @@ char * env[] = {
  */
 
 int testing=0;
-void unmountFilesystems(void);
-void disableSwap(void);
-void shutDown(int noKill, int doReboot, int doPowerOff);
-static int getNoKill(void);
+void shutDown(int doKill, reboot_action rebootAction);
+static int getKillPolicy(void);
 struct termios ts;
 
 static void printstr(char * string) {
@@ -396,22 +395,22 @@ static void termReset(void) {
 /* reboot handler */
 static void sigintHandler(int signum) {
     termReset();
-    shutDown(getNoKill(), 1, 0);
+    shutDown(getKillPolicy(), REBOOT);
 }
 
 /* halt handler */
 static void sigUsr1Handler(int signum) {
     termReset();
-    shutDown(getNoKill(), 0, 0);
+    shutDown(getKillPolicy(), HALT);
 }
 
 /* poweroff handler */
 static void sigUsr2Handler(int signum) {
     termReset();
-    shutDown(getNoKill(), 0, 1);
+    shutDown(getKillPolicy(), POWEROFF);
 }
 
-static int getNoKill(void) {
+static int getKillPolicy(void) {
     int fd;
     int len;
     char buf[1024];
@@ -421,9 +420,9 @@ static int getNoKill(void) {
         len = read(fd, buf, sizeof(buf) - 1);
         close(fd);
         if ((len > 0) && strstr(buf, "nokill"))
-            return 1;
+            return 0;
     }
-    return 0;
+    return 1;
 }
 
 static int getInitPid(void) {
@@ -453,7 +452,7 @@ int main(int argc, char **argv) {
     int doShutdown =0;
     int isSerial = 0;
     char * console = NULL;
-    int noKill = 0;
+    int doKill = 1;
     char * argvc[15];
     char ** argvp = argvc;
     char twelve = 12;
@@ -541,7 +540,7 @@ int main(int argc, char **argv) {
         }
     }
 
-    noKill = getNoKill();
+    doKill = getKillPolicy();
 
 #if !defined(__s390__) && !defined(__s390x__)
     static struct termios orig_cmode;
@@ -749,7 +748,7 @@ int main(int argc, char **argv) {
         printf("running %s\n", argvc[0]);
         execve(argvc[0], argvc, env);
 
-        shutDown(1, 0, 0);
+        shutDown(1, HALT);
     }
 
     /* signal handlers for halt/poweroff */
@@ -797,7 +796,7 @@ int main(int argc, char **argv) {
     if (testing)
         exit(0);
 
-    shutDown(noKill, doReboot, 0);
+    shutDown(doKill, doReboot?REBOOT:HALT);
 
     return 0;
 }
diff --git a/loader/init.h b/loader/init.h
new file mode 100644
index 0000000..733bc8e
--- /dev/null
+++ b/loader/init.h
@@ -0,0 +1,28 @@
+/*
+ * init.h
+ *
+ * Copyright (C) 2009  Red Hat, Inc.  All rights reserved.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef INIT_H
+#define INIT_H
+
+typedef enum {
+	REBOOT,
+	POWEROFF,
+	HALT
+} reboot_action;
+
+#endif /* INIT_H */
diff --git a/loader/shutdown.c b/loader/shutdown.c
index 044d021..54fd9a5 100644
--- a/loader/shutdown.c
+++ b/loader/shutdown.c
@@ -30,6 +30,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "init.h"
+
 #ifdef AS_SHUTDOWN
 int testing = 0;
 #else
@@ -39,25 +41,11 @@ extern int testing;
 void disableSwap(void);
 void unmountFilesystems(void);
 
-static void rebootHandler(int signum) {
-    printf("rebooting system\n");
-#if USE_MINILIBC
-    reboot(0xfee1dead, 672274793, 0x1234567);
-#else
-    reboot(RB_AUTOBOOT);
-#endif
-}
-
-void shutDown(int noKill, int doReboot, int doPowerOff) {
-    sync(); sync();
+static void performTerminations(int doKill) {
+	if (testing || !doKill)
+		return;
 
-    printf("disabling swap...\n");
-    disableSwap();
-
-    printf("unmounting filesystems...\n"); 
-    unmountFilesystems();
-
-    if (!testing && !noKill) {
+	sync();
 	printf("sending termination signals...");
 	kill(-1, 15);
 	sleep(2);
@@ -67,25 +55,60 @@ void shutDown(int noKill, int doReboot, int doPowerOff) {
 	kill(-1, 9);
 	sleep(2);
 	printf("done\n");
-    }
+}
 
-    if (doReboot) {
-	printf("rebooting system\n");
-	sleep(2);
+static void performUnmounts(int doKill) {
+	if (testing || !doKill)
+		return;
+
+	printf("disabling swap...\n");
+	disableSwap();
+
+	printf("unmounting filesystems...\n"); 
+	unmountFilesystems();
+}
+
+static void performReboot(reboot_action rebootAction) {
+	if (rebootAction == POWEROFF) {
+        printf("powering off system\n");
+		sleep(2);
+        reboot(RB_POWER_OFF);
+	} else if (rebootAction == REBOOT) {
+		printf("rebooting system\n");
+		sleep(2);
 
 #if USE_MINILIBC
-	reboot(0xfee1dead, 672274793, 0x1234567);
+		reboot(0xfee1dead, 672274793, 0x1234567);
 #else
-	reboot(RB_AUTOBOOT);
+		reboot(RB_AUTOBOOT);
 #endif
-    } else if (doPowerOff)  {
-        printf("powering off system\n");
-        reboot(RB_POWER_OFF);
-    } else {
+	}
+}
+
+static int shouldReboot = 0;
+
+static void rebootHandler(int signum) {
+    shouldReboot = 1;
+}
+
+void shutDown(int doKill, reboot_action rebootAction) {
+	if (rebootAction == POWEROFF || rebootAction == REBOOT) {
+		performUnmounts(doKill);
+		performTerminations(doKill);
+		if (!doKill)
+			performReboot(rebootAction);
+	}
+	
 	printf("you may safely reboot your system\n");
-        signal(SIGINT, rebootHandler);
-        while (1) sleep(60);
-    }
+    signal(SIGINT, rebootHandler);
+	while (1) {
+		sleep(1);
+		if (shouldReboot) {
+			performUnmounts(1);
+			performTerminations(1);
+			performReboot(REBOOT);
+		}
+	}
 
     exit(0);
 
@@ -122,3 +145,5 @@ int main(int argc, char ** argv) {
     return 0;
 }
 #endif
+
+/* vim:set shiftwidth=4 softtabstop=4 ts=4: */
-- 
1.6.4

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux