@@ -260,7 +260,7 @@ static int fsck_progress_socket(void) {
260
260
return TAKE_FD (fd );
261
261
}
262
262
263
- int main (int argc , char * argv []) {
263
+ static int run (int argc , char * argv []) {
264
264
_cleanup_close_pair_ int progress_pipe [2 ] = { -1 , -1 };
265
265
_cleanup_ (sd_device_unrefp ) sd_device * dev = NULL;
266
266
const char * device , * type ;
@@ -269,15 +269,15 @@ int main(int argc, char *argv[]) {
269
269
int r , exit_status ;
270
270
pid_t pid ;
271
271
272
- if (argc > 2 ) {
273
- log_error ("This program expects one or no arguments." );
274
- return EXIT_FAILURE ;
275
- }
276
-
277
272
log_set_target (LOG_TARGET_AUTO );
278
273
log_parse_environment ();
279
274
log_open ();
280
275
276
+ if (argc > 2 ) {
277
+ log_error ("This program expects one or no arguments." );
278
+ return - EINVAL ;
279
+ }
280
+
281
281
umask (0022 );
282
282
283
283
r = proc_cmdline_parse (parse_proc_cmdline_item , NULL , PROC_CMDLINE_STRIP_RD_PREFIX );
@@ -286,47 +286,37 @@ int main(int argc, char *argv[]) {
286
286
287
287
test_files ();
288
288
289
- if (!arg_force && arg_skip ) {
290
- r = 0 ;
291
- goto finish ;
292
- }
289
+ if (!arg_force && arg_skip )
290
+ return 0 ;
293
291
294
292
if (argc > 1 ) {
295
293
device = argv [1 ];
296
294
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 );
301
297
302
298
if (!S_ISBLK (st .st_mode )) {
303
299
log_error ("%s is not a block device." , device );
304
- r = - EINVAL ;
305
- goto finish ;
300
+ return - EINVAL ;
306
301
}
307
302
308
303
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 );
313
306
314
307
root_directory = false;
315
308
} else {
316
309
struct timespec times [2 ];
317
310
318
311
/* Find root device */
319
312
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" );
324
315
325
316
/* Virtual root devices don't need an fsck */
326
317
if (major (st .st_dev ) == 0 ) {
327
318
log_debug ("Root directory is virtual or btrfs, skipping check." );
328
- r = 0 ;
329
- goto finish ;
319
+ return 0 ;
330
320
}
331
321
332
322
/* check if we are already writable */
@@ -335,21 +325,16 @@ int main(int argc, char *argv[]) {
335
325
336
326
if (utimensat (AT_FDCWD , "/" , times , 0 ) == 0 ) {
337
327
log_info ("Root directory is writable, skipping check." );
338
- r = 0 ;
339
- goto finish ;
328
+ return 0 ;
340
329
}
341
330
342
331
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" );
347
334
348
335
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" );
353
338
354
339
root_directory = true;
355
340
}
@@ -360,20 +345,17 @@ int main(int argc, char *argv[]) {
360
345
log_device_warning_errno (dev , r , "Couldn't detect if fsck.%s may be used, proceeding: %m" , type );
361
346
else if (r == 0 ) {
362
347
log_device_info (dev , "fsck.%s doesn't exist, not checking file system." , type );
363
- goto finish ;
348
+ return 0 ;
364
349
}
365
350
}
366
351
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" );
373
355
374
356
r = safe_fork ("(fsck)" , FORK_RESET_SIGNALS |FORK_DEATHSIG |FORK_LOG , & pid );
375
357
if (r < 0 )
376
- goto finish ;
358
+ return r ;
377
359
if (r == 0 ) {
378
360
char dash_c [STRLEN ("-C" ) + DECIMAL_STR_MAX (int ) + 1 ];
379
361
int progress_socket = -1 ;
@@ -425,35 +407,29 @@ int main(int argc, char *argv[]) {
425
407
}
426
408
427
409
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 ]));
430
411
431
412
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 ;
436
415
if (exit_status & ~1 ) {
437
416
log_error ("fsck failed with exit status %i." , exit_status );
438
417
439
418
if ((exit_status & FSCK_SYSTEM_SHOULD_REBOOT ) && root_directory ) {
440
419
/* System should be rebooted. */
441
420
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 ))
444
423
/* Some other problem */
445
424
start_target (SPECIAL_EMERGENCY_TARGET , "replace" );
446
- r = - EINVAL ;
447
- } else {
425
+ else
448
426
log_warning ("Ignoring error." );
449
- r = 0 ;
450
- }
451
- } else
452
- r = 0 ;
427
+ }
453
428
454
429
if (exit_status & FSCK_ERROR_CORRECTED )
455
430
(void ) touch ("/run/systemd/quotacheck" );
456
431
457
- finish :
458
- return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS ;
432
+ return exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED );
459
433
}
434
+
435
+ DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE (run );
0 commit comments