return 0;
}
+int asynchronous_fsync(int fd, pid_t *ret_pid) {
+ int r;
+
+ assert(fd >= 0);
+ /* Same as asynchronous_sync() above, but calls fsync() on a specific fd */
+
+ r = safe_fork_full("(sd-fsync)",
+ /* stdio_fds= */ NULL,
+ /* except_fds= */ &fd,
+ /* n_except_fds= */ 1,
+ FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|(ret_pid ? 0 : FORK_DETACH), ret_pid);
+ if (r < 0)
+ return r;
+ if (r == 0) {
+ /* Child process */
+ fsync(fd);
+ _exit(EXIT_SUCCESS);
+ }
+
+ return 0;
+}
+
/* We encode the fd to close in the userdata pointer as an unsigned value. The highest bit indicates whether
* we need to fork again */
#define NEED_DOUBLE_FORK (1U << (sizeof(unsigned) * 8 - 1))
#include "process-util.h"
#include "reboot-util.h"
#include "rlimit-util.h"
+#include "shutdown.h"
#include "signal-util.h"
#include "string-util.h"
#include "switch-root.h"
return r;
}
-static int sync_with_progress(void) {
+int sync_with_progress(int fd) {
unsigned long long dirty = ULLONG_MAX;
+ _cleanup_free_ char *path = NULL;
+ const char *what;
pid_t pid;
int r;
/* Due to the possibility of the sync operation hanging, we fork a child process and monitor
* the progress. If the timeout lapses, the assumption is that the particular sync stalled. */
- r = asynchronous_sync(&pid);
- if (r < 0)
- return log_error_errno(r, "Failed to fork sync(): %m");
+ if (fd >= 0) {
+ r = asynchronous_fsync(fd, &pid);
+ if (r < 0)
+ return log_error_errno(r, "Failed to fork fsync(): %m");
+
+ (void) fd_get_path(fd, &path);
+ } else {
+ r = asynchronous_sync(&pid);
+ if (r < 0)
+ return log_error_errno(r, "Failed to fork sync(): %m");
+ }
- log_info("Syncing filesystems and block devices.");
+ what = path ?: "filesystems and block devices";
+ log_info("Syncing %s.", what);
/* Start monitoring the sync operation. If more than
* SYNC_PROGRESS_ATTEMPTS lapse without progress being made,
/* Sync finished without error (sync() call itself does not return an error code) */
return 0;
if (r != -ETIMEDOUT)
- return log_error_errno(r, "Failed to sync filesystems and block devices: %m");
+ return log_error_errno(r, "Failed to sync %s: %m", what);
/* Reset the check counter if we made some progress */
if (sync_making_progress(&dirty) > 0)
/* Only reached in the event of a timeout. We should issue a kill to the stray process. */
(void) kill(pid, SIGKILL);
return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT),
- "Syncing filesystems and block devices - timed out, issuing SIGKILL to PID "PID_FMT".",
+ "Syncing %s - timed out, issuing SIGKILL to PID "PID_FMT".",
+ what,
pid);
}
* desperately trying to sync IO to disk within their timeout. Do not remove this sync, data corruption will
* result. */
if (!in_container)
- (void) sync_with_progress();
+ (void) sync_with_progress(-EBADF);
disable_coredumps();
disable_binfmt();
* which might have caused IO, hence let's do it once more. Do not remove this sync, data corruption
* will result. */
if (!in_container)
- (void) sync_with_progress();
+ (void) sync_with_progress(-EBADF);
notify_supervisor();