Skip to content

Commit 9013653

Browse files
committed
fsck: configure logging before use and define main through macro
There's a slight change in logic: before, when rebooting the machine, we could also request quotacheck (by touching /run/systemd/quotacheck) if the fsck helper set FSCK_ERROR_CORRECTED. This is just a race, and doesn't matter much in practice.
1 parent c88f45e commit 9013653

File tree

1 file changed

+36
-60
lines changed

1 file changed

+36
-60
lines changed

src/fsck/fsck.c

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static int fsck_progress_socket(void) {
260260
return TAKE_FD(fd);
261261
}
262262

263-
int main(int argc, char *argv[]) {
263+
static int run(int argc, char *argv[]) {
264264
_cleanup_close_pair_ int progress_pipe[2] = { -1, -1 };
265265
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
266266
const char *device, *type;
@@ -269,15 +269,15 @@ int main(int argc, char *argv[]) {
269269
int r, exit_status;
270270
pid_t pid;
271271

272-
if (argc > 2) {
273-
log_error("This program expects one or no arguments.");
274-
return EXIT_FAILURE;
275-
}
276-
277272
log_set_target(LOG_TARGET_AUTO);
278273
log_parse_environment();
279274
log_open();
280275

276+
if (argc > 2) {
277+
log_error("This program expects one or no arguments.");
278+
return -EINVAL;
279+
}
280+
281281
umask(0022);
282282

283283
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
@@ -286,47 +286,37 @@ int main(int argc, char *argv[]) {
286286

287287
test_files();
288288

289-
if (!arg_force && arg_skip) {
290-
r = 0;
291-
goto finish;
292-
}
289+
if (!arg_force && arg_skip)
290+
return 0;
293291

294292
if (argc > 1) {
295293
device = argv[1];
296294

297-
if (stat(device, &st) < 0) {
298-
r = log_error_errno(errno, "Failed to stat %s: %m", device);
299-
goto finish;
300-
}
295+
if (stat(device, &st) < 0)
296+
return log_error_errno(errno, "Failed to stat %s: %m", device);
301297

302298
if (!S_ISBLK(st.st_mode)) {
303299
log_error("%s is not a block device.", device);
304-
r = -EINVAL;
305-
goto finish;
300+
return -EINVAL;
306301
}
307302

308303
r = sd_device_new_from_devnum(&dev, 'b', st.st_rdev);
309-
if (r < 0) {
310-
log_error_errno(r, "Failed to detect device %s: %m", device);
311-
goto finish;
312-
}
304+
if (r < 0)
305+
return log_error_errno(r, "Failed to detect device %s: %m", device);
313306

314307
root_directory = false;
315308
} else {
316309
struct timespec times[2];
317310

318311
/* Find root device */
319312

320-
if (stat("/", &st) < 0) {
321-
r = log_error_errno(errno, "Failed to stat() the root directory: %m");
322-
goto finish;
323-
}
313+
if (stat("/", &st) < 0)
314+
return log_error_errno(errno, "Failed to stat() the root directory: %m");
324315

325316
/* Virtual root devices don't need an fsck */
326317
if (major(st.st_dev) == 0) {
327318
log_debug("Root directory is virtual or btrfs, skipping check.");
328-
r = 0;
329-
goto finish;
319+
return 0;
330320
}
331321

332322
/* check if we are already writable */
@@ -335,21 +325,16 @@ int main(int argc, char *argv[]) {
335325

336326
if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
337327
log_info("Root directory is writable, skipping check.");
338-
r = 0;
339-
goto finish;
328+
return 0;
340329
}
341330

342331
r = sd_device_new_from_devnum(&dev, 'b', st.st_dev);
343-
if (r < 0) {
344-
log_error_errno(r, "Failed to detect root device: %m");
345-
goto finish;
346-
}
332+
if (r < 0)
333+
return log_error_errno(r, "Failed to detect root device: %m");
347334

348335
r = sd_device_get_devname(dev, &device);
349-
if (r < 0) {
350-
log_device_error_errno(dev, r, "Failed to detect device node of root directory: %m");
351-
goto finish;
352-
}
336+
if (r < 0)
337+
return log_device_error_errno(dev, r, "Failed to detect device node of root directory: %m");
353338

354339
root_directory = true;
355340
}
@@ -360,20 +345,17 @@ int main(int argc, char *argv[]) {
360345
log_device_warning_errno(dev, r, "Couldn't detect if fsck.%s may be used, proceeding: %m", type);
361346
else if (r == 0) {
362347
log_device_info(dev, "fsck.%s doesn't exist, not checking file system.", type);
363-
goto finish;
348+
return 0;
364349
}
365350
}
366351

367-
if (arg_show_progress) {
368-
if (pipe(progress_pipe) < 0) {
369-
r = log_error_errno(errno, "pipe(): %m");
370-
goto finish;
371-
}
372-
}
352+
if (arg_show_progress &&
353+
pipe(progress_pipe) < 0)
354+
return log_error_errno(errno, "pipe(): %m");
373355

374356
r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
375357
if (r < 0)
376-
goto finish;
358+
return r;
377359
if (r == 0) {
378360
char dash_c[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1];
379361
int progress_socket = -1;
@@ -425,35 +407,29 @@ int main(int argc, char *argv[]) {
425407
}
426408

427409
progress_pipe[1] = safe_close(progress_pipe[1]);
428-
(void) process_progress(progress_pipe[0]);
429-
progress_pipe[0] = -1;
410+
(void) process_progress(TAKE_FD(progress_pipe[0]));
430411

431412
exit_status = wait_for_terminate_and_check("fsck", pid, WAIT_LOG_ABNORMAL);
432-
if (exit_status < 0) {
433-
r = exit_status;
434-
goto finish;
435-
}
413+
if (exit_status < 0)
414+
return exit_status;
436415
if (exit_status & ~1) {
437416
log_error("fsck failed with exit status %i.", exit_status);
438417

439418
if ((exit_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory) {
440419
/* System should be rebooted. */
441420
start_target(SPECIAL_REBOOT_TARGET, "replace-irreversibly");
442-
r = -EINVAL;
443-
} else if (exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)) {
421+
return -EINVAL;
422+
} else if (exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED))
444423
/* Some other problem */
445424
start_target(SPECIAL_EMERGENCY_TARGET, "replace");
446-
r = -EINVAL;
447-
} else {
425+
else
448426
log_warning("Ignoring error.");
449-
r = 0;
450-
}
451-
} else
452-
r = 0;
427+
}
453428

454429
if (exit_status & FSCK_ERROR_CORRECTED)
455430
(void) touch("/run/systemd/quotacheck");
456431

457-
finish:
458-
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
432+
return exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED);
459433
}
434+
435+
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);

0 commit comments

Comments
 (0)