shutdown: issue a sync() as soon as shutdown.target is queued

This commit is contained in:
Lennart Poettering 2013-01-25 22:33:33 +01:00
parent 97b5f60818
commit c65eb83653
6 changed files with 123 additions and 2 deletions

View 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 \

View 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"

View 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);

65
src/core/sync.c Normal file
View File

@ -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;
}

24
src/core/sync.h Normal file
View File

@ -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);

View 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;