chiark / gitweb /
shutdown: issue a sync() as soon as shutdown.target is queued
authorLennart Poettering <lennart@poettering.net>
Fri, 25 Jan 2013 21:33:33 +0000 (22:33 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 9 Feb 2013 01:20:42 +0000 (02:20 +0100)
Makefile.am
src/core/job.c
src/core/job.h
src/core/sync.c [new file with mode: 0644]
src/core/sync.h [new file with mode: 0644]
src/core/transaction.c

index f912c5d..2b7d78c 100644 (file)
@@ -853,7 +853,9 @@ libsystemd_core_la_SOURCES = \
        src/core/syscall-list.c \
        src/core/syscall-list.h \
        src/core/audit-fd.c \
-       src/core/audit-fd.h
+       src/core/audit-fd.h \
+       src/core/sync.c \
+       src/core/sync.h
 
 if HAVE_KMOD
 libsystemd_core_la_SOURCES += \
@@ -873,7 +875,8 @@ libsystemd_core_la_CFLAGS = \
        $(LIBWRAP_CFLAGS) \
        $(PAM_CFLAGS) \
        $(AUDIT_CFLAGS) \
-       $(KMOD_CFLAGS)
+       $(KMOD_CFLAGS) \
+       -pthread
 
 libsystemd_core_la_LIBADD = \
        libsystemd-capability.la \
index 6a03d17..2bafbc1 100644 (file)
@@ -34,6 +34,9 @@
 #include "load-dropin.h"
 #include "log.h"
 #include "dbus-job.h"
+#include "special.h"
+#include "sync.h"
+#include "virt.h"
 
 JobBusClient* job_bus_client_new(DBusConnection *connection, const char *name) {
         JobBusClient *cl;
@@ -1061,6 +1064,29 @@ int job_coldplug(Job *j) {
         return 0;
 }
 
+void job_shutdown_magic(Job *j) {
+        assert(j);
+
+        /* The shutdown target gets some special treatment here: we
+         * tell the kernel to begin with flushing its disk caches, to
+         * optimize shutdown time a bit. Ideally we wouldn't hardcode
+         * this magic into PID 1. However all other processes aren't
+         * options either since they'd exit much sooner than PID 1 and
+         * asynchronous sync() would cause their exit to be
+         * delayed. */
+
+        if (!unit_has_name(j->unit, SPECIAL_SHUTDOWN_TARGET))
+                return;
+
+        if (j->type != JOB_START)
+                return;
+
+        if (detect_container(NULL) > 0)
+                return;
+
+        asynchronous_sync();
+}
+
 static const char* const job_state_table[_JOB_STATE_MAX] = {
         [JOB_WAITING] = "waiting",
         [JOB_RUNNING] = "running"
index 3aa49d4..d10e6b6 100644 (file)
@@ -217,6 +217,8 @@ int job_finish_and_invalidate(Job *j, JobResult result, bool recursive);
 
 char *job_dbus_path(Job *j);
 
+void job_shutdown_magic(Job *j);
+
 const char* job_type_to_string(JobType t);
 JobType job_type_from_string(const char *s);
 
diff --git a/src/core/sync.c b/src/core/sync.c
new file mode 100644 (file)
index 0000000..7e74b63
--- /dev/null
@@ -0,0 +1,65 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 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
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <pthread.h>
+#include <unistd.h>
+
+#include "sync.h"
+
+static void *sync_thread(void *p) {
+        sync();
+        return NULL;
+}
+
+int asynchronous_sync(void) {
+        pthread_attr_t a;
+        pthread_t t;
+        int r;
+
+        /* It kinda sucks that we have to resort to threads to
+         * implement an asynchronous sync(), but well, such is
+         * life.
+         *
+         * Note that issuing this command right before exiting a
+         * process will cause the process to wait for the sync() to
+         * complete. This function hence is nicely asynchronous really
+         * only in long running processes. */
+
+        r = pthread_attr_init(&a);
+        if (r != 0)
+                return -r;
+
+        r = pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
+        if (r != 0) {
+                r = -r;
+                goto finish;
+        }
+
+        r = pthread_create(&t, &a, sync_thread, NULL);
+        if (r != 0) {
+                r = -r;
+                goto finish;
+        }
+
+finish:
+        pthread_attr_destroy(&a);
+        return r;
+}
diff --git a/src/core/sync.h b/src/core/sync.h
new file mode 100644 (file)
index 0000000..eb26c88
--- /dev/null
@@ -0,0 +1,24 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 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
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+int asynchronous_sync(void);
index 1854047..dbc30af 100644 (file)
@@ -620,6 +620,7 @@ static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
                 job_add_to_run_queue(j);
                 job_add_to_dbus_queue(j);
                 job_start_timer(j);
+                job_shutdown_magic(j);
         }
 
         return 0;