shutdown: unify shutdown.c's and async.c's sync() helper process
authorLennart Poettering <lennart@poettering.net>
Fri, 22 Dec 2017 12:19:56 +0000 (13:19 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 25 Dec 2017 10:48:21 +0000 (11:48 +0100)
The helper processes are pretty much the same now, let's unify them
hence.

src/basic/async.c
src/basic/async.h
src/core/job.c
src/core/shutdown.c
src/test/test-async.c

index 21e05d9..2c51883 100644 (file)
@@ -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;
 }
 
index 7eac54d..01c975b 100644 (file)
@@ -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);
index 9ff5b28..b046698 100644 (file)
@@ -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) {
index 9ef4e77..877f882 100644 (file)
 #include <unistd.h>
 
 #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.");
 
index 2055ce2..87906b2 100644 (file)
@@ -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);