Skip to content

Commit f11fa83

Browse files
YHNdnzjbluca
authored andcommitted
shutdown: clean up sync_with_progress a bit
Also, ignore the error on caller's side. (cherry picked from commit 758760a) (cherry picked from commit 489cf96)
1 parent 8923d93 commit f11fa83

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

src/shutdown/shutdown.c

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -189,47 +189,43 @@ static int switch_root_initramfs(void) {
189189
static int sync_making_progress(unsigned long long *prev_dirty) {
190190
_cleanup_fclose_ FILE *f = NULL;
191191
unsigned long long val = 0;
192-
int ret;
192+
int r;
193+
194+
assert(prev_dirty);
193195

194196
f = fopen("/proc/meminfo", "re");
195197
if (!f)
196198
return log_warning_errno(errno, "Failed to open /proc/meminfo: %m");
197199

198200
for (;;) {
199201
_cleanup_free_ char *line = NULL;
200-
unsigned long long ull = 0;
201-
int q;
202+
unsigned long long ull;
202203

203-
q = read_line(f, LONG_LINE_MAX, &line);
204-
if (q < 0)
205-
return log_warning_errno(q, "Failed to parse /proc/meminfo: %m");
206-
if (q == 0)
204+
r = read_line(f, LONG_LINE_MAX, &line);
205+
if (r < 0)
206+
return log_warning_errno(r, "Failed to parse /proc/meminfo: %m");
207+
if (r == 0)
207208
break;
208209

209210
if (!first_word(line, "NFS_Unstable:") && !first_word(line, "Writeback:") && !first_word(line, "Dirty:"))
210211
continue;
211212

212213
errno = 0;
213214
if (sscanf(line, "%*s %llu %*s", &ull) != 1) {
214-
if (errno != 0)
215-
log_warning_errno(errno, "Failed to parse /proc/meminfo: %m");
216-
else
217-
log_warning("Failed to parse /proc/meminfo");
218-
215+
log_warning_errno(errno_or_else(EIO), "Failed to parse /proc/meminfo field, ignoring: %m");
219216
return false;
220217
}
221218

222219
val += ull;
223220
}
224221

225-
ret = *prev_dirty > val;
222+
r = *prev_dirty > val;
226223
*prev_dirty = val;
227-
return ret;
224+
return r;
228225
}
229226

230-
static void sync_with_progress(void) {
227+
static int sync_with_progress(void) {
231228
unsigned long long dirty = ULLONG_MAX;
232-
unsigned checks;
233229
pid_t pid;
234230
int r;
235231

@@ -239,37 +235,32 @@ static void sync_with_progress(void) {
239235
* the progress. If the timeout lapses, the assumption is that the particular sync stalled. */
240236

241237
r = asynchronous_sync(&pid);
242-
if (r < 0) {
243-
log_error_errno(r, "Failed to fork sync(): %m");
244-
return;
245-
}
238+
if (r < 0)
239+
return log_error_errno(r, "Failed to fork sync(): %m");
246240

247241
log_info("Syncing filesystems and block devices.");
248242

249243
/* Start monitoring the sync operation. If more than
250244
* SYNC_PROGRESS_ATTEMPTS lapse without progress being made,
251245
* we assume that the sync is stalled */
252-
for (checks = 0; checks < SYNC_PROGRESS_ATTEMPTS; checks++) {
246+
for (unsigned checks = 0; checks < SYNC_PROGRESS_ATTEMPTS; checks++) {
253247
r = wait_for_terminate_with_timeout(pid, SYNC_TIMEOUT_USEC);
254248
if (r == 0)
255-
/* Sync finished without error.
256-
* (The sync itself does not return an error code) */
257-
return;
258-
else if (r == -ETIMEDOUT) {
259-
/* Reset the check counter if the "Dirty" value is
260-
* decreasing */
261-
if (sync_making_progress(&dirty) > 0)
262-
checks = 0;
263-
} else {
264-
log_error_errno(r, "Failed to sync filesystems and block devices: %m");
265-
return;
266-
}
249+
/* Sync finished without error (sync() call itself does not return an error code) */
250+
return 0;
251+
if (r != -ETIMEDOUT)
252+
return log_error_errno(r, "Failed to sync filesystems and block devices: %m");
253+
254+
/* Reset the check counter if we made some progress */
255+
if (sync_making_progress(&dirty) > 0)
256+
checks = 0;
267257
}
268258

269-
/* Only reached in the event of a timeout. We should issue a kill
270-
* to the stray process. */
271-
log_error("Syncing filesystems and block devices - timed out, issuing SIGKILL to PID "PID_FMT".", pid);
259+
/* Only reached in the event of a timeout. We should issue a kill to the stray process. */
272260
(void) kill(pid, SIGKILL);
261+
return log_error_errno(SYNTHETIC_ERRNO(ETIMEDOUT),
262+
"Syncing filesystems and block devices - timed out, issuing SIGKILL to PID "PID_FMT".",
263+
pid);
273264
}
274265

275266
static int read_current_sysctl_printk_log_level(void) {
@@ -424,7 +415,7 @@ int main(int argc, char *argv[]) {
424415
* desperately trying to sync IO to disk within their timeout. Do not remove this sync, data corruption will
425416
* result. */
426417
if (!in_container)
427-
sync_with_progress();
418+
(void) sync_with_progress();
428419

429420
disable_coredumps();
430421
disable_binfmt();
@@ -587,7 +578,7 @@ int main(int argc, char *argv[]) {
587578
* which might have caused IO, hence let's do it once more. Do not remove this sync, data corruption
588579
* will result. */
589580
if (!in_container)
590-
sync_with_progress();
581+
(void) sync_with_progress();
591582

592583
if (streq(arg_verb, "exit")) {
593584
if (in_container) {

0 commit comments

Comments
 (0)