diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-09-09 17:49:33 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-09-09 19:12:31 +0200 |
commit | 13b5225d6278af15e84ebd1889f04cfe81b47787 (patch) | |
tree | a3744f9123f530eb239fabb07ad266863be1d1ac /src/shutdown | |
parent | machinectl: suppress redirection notice if --quiet is specified (diff) | |
download | systemd-13b5225d6278af15e84ebd1889f04cfe81b47787.tar.xz systemd-13b5225d6278af15e84ebd1889f04cfe81b47787.zip |
shutdown: teach sync_with_progress() to optionally sync a specific fd only
This is preparation for reusing the logic for syncing DM and other
devices with a timeout applied.
Diffstat (limited to 'src/shutdown')
-rw-r--r-- | src/shutdown/shutdown.c | 31 | ||||
-rw-r--r-- | src/shutdown/shutdown.h | 4 |
2 files changed, 26 insertions, 9 deletions
diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index 03e6e70fd8..e6c9e0f806 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -40,6 +40,7 @@ #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" @@ -223,8 +224,10 @@ static int sync_making_progress(unsigned long long *prev_dirty) { 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; @@ -233,11 +236,20 @@ static int sync_with_progress(void) { /* 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, @@ -248,7 +260,7 @@ static int sync_with_progress(void) { /* 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) @@ -258,7 +270,8 @@ static int sync_with_progress(void) { /* 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); } @@ -432,7 +445,7 @@ int main(int argc, char *argv[]) { * 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(); @@ -600,7 +613,7 @@ int main(int argc, char *argv[]) { * 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(); diff --git a/src/shutdown/shutdown.h b/src/shutdown/shutdown.h new file mode 100644 index 0000000000..99aaec697b --- /dev/null +++ b/src/shutdown/shutdown.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +int sync_with_progress(int fd); |