[systemd-devel] [PATCH 3/4] Adding halt binary to shutdown the system

fidencio at profusion.mobi fidencio at profusion.mobi
Thu Sep 30 14:10:47 PDT 2010


From: Fabiano Fidencio <fidencio at profusion.mobi>

This functions are working as follows:
    - Send a SIGTERM to all process
    - Send a SIGKILL to all process
    - Try to umount all mount points
    - Try to remount read-only all mount points that can't
    be umounted
    - Call shutdown

    If one step fail, shutdown will be aborted
---
 Makefile.am |   14 ++++++++++-
 src/halt.c  |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletions(-)
 create mode 100644 src/halt.c

diff --git a/Makefile.am b/Makefile.am
index 06b2cca..4d8e181 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -89,6 +89,7 @@ rootlibexec_PROGRAMS = \
 	systemd-update-utmp \
 	systemd-random-seed \
 	systemd-shutdownd \
+	systemd-halt \
 	systemd-modules-load \
 	systemd-remount-api-vfs \
 	systemd-kmsg-syslogd \
@@ -349,7 +350,7 @@ libsystemd_core_la_SOURCES = \
 	src/service.c \
 	src/automount.c \
 	src/mount.c \
-	src/umount.c \
+	src/halt.c \
 	src/swap.c \
 	src/device.c \
 	src/target.c \
@@ -629,6 +630,17 @@ systemd_shutdownd_CFLAGS = \
 systemd_shutdownd_LDADD = \
 	libsystemd-basic.la
 
+systemd_halt_SOURCES = \
+	src/mount-setup.c \
+	src/umount.c \
+	src/halt.c
+
+systemd_halt_CFLAGS = \
+	$(AM_CFLAGS)
+
+systemd_halt_LDADD = \
+	libsystemd-basic.la
+
 systemd_modules_load_SOURCES = \
 	src/modules-load.c
 
diff --git a/src/halt.c b/src/halt.c
new file mode 100644
index 0000000..cce5dee
--- /dev/null
+++ b/src/halt.c
@@ -0,0 +1,76 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 ProFUSION embedded systems
+
+  systemd 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.
+
+  systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/reboot.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "umount.h"
+
+static volatile unsigned int run = 1;
+
+static void sig_alarm(int sig) {
+        run = 0;
+}
+
+int main(int argc, char *argv[]) {
+        int c, cmd;
+
+        if (getppid() != 1)
+                return -1;
+
+        if (argc != 2)
+                return -1;
+
+        switch ((c = getopt(argc, argv, "hr"))) {
+                case 'h':
+                        cmd = RB_POWER_OFF;
+                        break;
+                case 'r':
+                        cmd = RB_AUTOBOOT;
+                        break;
+                default:
+                        return -1;
+        }
+
+        /* lock us into memory */
+        mlockall(MCL_CURRENT|MCL_FUTURE);
+
+        signal(SIGALRM, sig_alarm);
+        kill(-1, SIGTERM);
+        alarm(10);
+        while (run) {
+                pid_t r = wait(NULL);
+                if (r == (pid_t)-1 && errno == ECHILD)
+                        run = 0;
+        }
+        alarm(0);
+        kill(-1, SIGKILL);
+
+        if (umount_init() < 0)
+                return -1;
+
+        return reboot(cmd);
+}
-- 
1.7.3



More information about the systemd-devel mailing list