From d00c2631435726b89f55de0510f3ea0133a335b6 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 22 Dec 2017 13:19:56 +0100 Subject: [PATCH] shutdown: unify shutdown.c's and async.c's sync() helper process The helper processes are pretty much the same now, let's unify them hence. --- src/basic/async.c | 5 ++--- src/basic/async.h | 2 +- src/core/job.c | 2 +- src/core/shutdown.c | 20 ++++++++------------ src/test/test-async.c | 2 +- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/basic/async.c b/src/basic/async.c index 21e05d9c2c..2c51883463 100644 --- a/src/basic/async.c +++ b/src/basic/async.c @@ -60,7 +60,7 @@ finish: return -r; } -int asynchronous_sync(void) { +int asynchronous_sync(pid_t *ret_pid) { int r; /* This forks off an invocation of fork() as a child process, in order to initiate synchronization to @@ -68,7 +68,7 @@ int asynchronous_sync(void) { * original process ever, and a thread would do that as the process can't exit with threads hanging in blocking * syscalls. */ - r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, NULL); + r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, ret_pid); if (r < 0) return r; if (r == 0) { @@ -77,7 +77,6 @@ int asynchronous_sync(void) { _exit(EXIT_SUCCESS); } - /* We don' really care about the PID from here on. It will exit when it's done. */ return 0; } diff --git a/src/basic/async.h b/src/basic/async.h index 7eac54d8b2..01c975bb30 100644 --- a/src/basic/async.h +++ b/src/basic/async.h @@ -22,5 +22,5 @@ int asynchronous_job(void* (*func)(void *p), void *arg); -int asynchronous_sync(void); +int asynchronous_sync(pid_t *ret_pid); int asynchronous_close(int fd); diff --git a/src/core/job.c b/src/core/job.c index 9ff5b288f9..b046698118 100644 --- a/src/core/job.c +++ b/src/core/job.c @@ -1244,7 +1244,7 @@ void job_shutdown_magic(Job *j) { if (detect_container() > 0) return; - (void) asynchronous_sync(); + (void) asynchronous_sync(NULL); } int job_get_timeout(Job *j, usec_t *timeout) { diff --git a/src/core/shutdown.c b/src/core/shutdown.c index 9ef4e7787a..877f8820dc 100644 --- a/src/core/shutdown.c +++ b/src/core/shutdown.c @@ -31,10 +31,11 @@ #include #include "alloc-util.h" +#include "async.h" #include "cgroup-util.h" -#include "fd-util.h" #include "def.h" #include "exec-util.h" +#include "fd-util.h" #include "fileio.h" #include "killall.h" #include "log.h" @@ -211,26 +212,21 @@ static bool sync_making_progress(unsigned long long *prev_dirty) { } static void sync_with_progress(void) { + unsigned long long dirty = ULONG_LONG_MAX; unsigned checks; pid_t pid; int r; - unsigned long long dirty = ULONG_LONG_MAX; BLOCK_SIGNALS(SIGCHLD); - /* Due to the possiblity of the sync operation hanging, we fork - * a child process and monitor the progress. If the timeout - * lapses, the assumption is that that particular sync stalled. */ - r = safe_fork("(sd-sync)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS, &pid); + /* Due to the possiblity of the sync operation hanging, we fork a child process and monitor the progress. If + * the timeout lapses, the assumption is that that particular sync stalled. */ + + r = asynchronous_sync(&pid); if (r < 0) { - log_error_errno(r, "Failed to fork: %m"); + log_error_errno(r, "Failed to fork sync(): %m"); return; } - if (r == 0) { - /* Start the sync operation here in the child */ - sync(); - _exit(EXIT_SUCCESS); - } log_info("Syncing filesystems and block devices."); diff --git a/src/test/test-async.c b/src/test/test-async.c index 2055ce25bf..87906b2e25 100644 --- a/src/test/test-async.c +++ b/src/test/test-async.c @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) { assert_se(asynchronous_job(async_func, NULL) >= 0); - assert_se(asynchronous_sync() >= 0); + assert_se(asynchronous_sync(NULL) >= 0); sleep(1);