From c65eb8365344eeb72ee2c0b333ab54d925263b3f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 25 Jan 2013 22:33:33 +0100 Subject: [PATCH] shutdown: issue a sync() as soon as shutdown.target is queued --- Makefile.am | 7 ++++-- src/core/job.c | 26 ++++++++++++++++++++ src/core/job.h | 2 ++ src/core/sync.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/sync.h | 24 +++++++++++++++++++ src/core/transaction.c | 1 + 6 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 src/core/sync.c create mode 100644 src/core/sync.h Index: systemd-195/Makefile.am =================================================================== --- systemd-195.orig/Makefile.am +++ systemd-195/Makefile.am @@ -1038,7 +1038,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 nodist_libsystemd_core_la_SOURCES = \ src/core/load-fragment-gperf.c \ @@ -1052,7 +1054,8 @@ libsystemd_core_la_CFLAGS = \ $(LIBWRAP_CFLAGS) \ $(PAM_CFLAGS) \ $(AUDIT_CFLAGS) \ - $(KMOD_CFLAGS) + $(KMOD_CFLAGS) \ + -pthread libsystemd_core_la_LIBADD = \ libsystemd-capability.la \ Index: systemd-195/src/core/job.c =================================================================== --- systemd-195.orig/src/core/job.c +++ systemd-195/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; @@ -1045,6 +1048,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: systemd-195/src/core/job.h =================================================================== --- systemd-195.orig/src/core/job.h +++ systemd-195/src/core/job.h @@ -217,6 +217,8 @@ int job_finish_and_invalidate(Job *j, Jo 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); Index: systemd-195/src/core/sync.c =================================================================== --- /dev/null +++ systemd-195/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 . +***/ + +#include +#include + +#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; +} Index: systemd-195/src/core/sync.h =================================================================== --- /dev/null +++ systemd-195/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 . +***/ + +int asynchronous_sync(void); Index: systemd-195/src/core/transaction.c =================================================================== --- systemd-195.orig/src/core/transaction.c +++ systemd-195/src/core/transaction.c @@ -592,6 +592,7 @@ static int transaction_apply(Transaction job_add_to_run_queue(j); job_add_to_dbus_queue(j); job_start_timer(j); + job_shutdown_magic(j); } return 0;