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.
This commit is contained in:
Lennart Poettering 2017-12-22 13:19:56 +01:00
parent 4c253ed1ca
commit d00c263143
5 changed files with 13 additions and 18 deletions

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

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

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

View File

@ -31,10 +31,11 @@
#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.");

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