diff options
-rw-r--r-- | Makefile.am | 7 | ||||
-rw-r--r-- | src/core/job.c | 26 | ||||
-rw-r--r-- | src/core/job.h | 2 | ||||
-rw-r--r-- | src/core/sync.c | 65 | ||||
-rw-r--r-- | src/core/sync.h | 24 | ||||
-rw-r--r-- | src/core/transaction.c | 1 |
6 files changed, 123 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am index f912c5dd52..2b7d78ccf5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/core/job.c b/src/core/job.c index 6a03d17aa8..2bafbc1589 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -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" diff --git a/src/core/job.h b/src/core/job.h index 3aa49d4b46..d10e6b6b0e 100644 --- a/src/core/job.h +++ b/src/core/job.h @@ -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 index 0000000000..7e74b63071 --- /dev/null +++ b/src/core/sync.c @@ -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 index 0000000000..eb26c88deb --- /dev/null +++ b/src/core/sync.h @@ -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); diff --git a/src/core/transaction.c b/src/core/transaction.c index 1854047afd..dbc30af7d1 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -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; |