fuse2fs: return proper exit code from fuse_main
PATCH 1/1 fuse2fs: return proper exit code from fuse_main
Currently fuse2fs will always return 0 exit code indicating successful operation even though the mount failed either because we failed to properly read the file system in the first place, or the fuse_main() failed for whatever reason.
Fix it by using the return code from fuse_main(), or return 32 in case we fail because the file system is corrupted, or we encountered a problem preventing us mounting the file system. 32 because this is a libmount exit code indicating mount failed.
--- misc/fuse2fs.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
misc/fuse2fs.c +11 -6
@@ -3720,7 +3720,7 @@ int main(int argc, char *argv[]) {struct fuse_args args =FUSE_ARGS_INIT (argc, argv);struct fuse2fs fctx; -errcode_t err; +errcode_t err =0 ;char *logfile;char extra_args[BUFSIZ ];int ret =0 , flags =EXT2_FLAG_64BITS |EXT2_FLAG_EXCLUSIVE ;
@@ -3753,6 +3753,7 @@ int main(int argc, char *argv[]) fctx.err_fp =fopen (logfile,"a" );if (!fctx.err_fp) {perror (logfile); + err = errno; goto out; } }else
@@ -3766,7 +3767,6 @@ int main(int argc, char *argv[]) }/* Start up the fs (while we still can use stdout) */ - ret =2 ;if (!fctx.ro) flags |=EXT2_FLAG_RW ; err = ext2fs_open2(fctx.device, NULL, flags, 0, 0, unix_io_manager,
@@ -3779,8 +3779,6 @@ int main(int argc, char *argv[]) fctx.fs = global_fs; global_fs->priv_data = &fctx; - ret =3 ; -if (ext2fs_has_feature_journal_needs_recovery (global_fs->super)) {if (!fctx.ro) {printf (_ ("%s: recovering journal\n" ), fctx.device);
@@ -3797,6 +3795,7 @@ int main(int argc, char *argv[]) }else {printf ("%s" ,_ ("Journal needs recovery; running " "`e2fsck -E journal_only' is required.\n" )); + err =1 ; goto out; } }
@@ -3836,6 +3835,7 @@ int main(int argc, char *argv[])if (global_fs->super->s_state &EXT2_ERROR_FS ) {printf ("%s" ,_ ("Errors detected; running e2fsck is required.\n" )); + err =1 ; goto out; }
@@ -3859,11 +3859,16 @@ int main(int argc, char *argv[]) }pthread_mutex_init (&fctx.bfl,NULL ); -fuse_main (args.argc, args.argv, &fs_ops, &fctx); + ret =fuse_main (args.argc, args.argv, &fs_ops, &fctx);pthread_mutex_destroy (&fctx.bfl); - ret =0 ; out: +/* +* Encountered error reading the file system. Return standard "mount +* failure" mount exit code (32). +*/ +if (err) + ret =32 ;if (global_fs) { err =ext2fs_close (global_fs);if (err)
Signature · 1 line
2.17.1
Quoted 70 lines · Lukas Czerner
Currently fuse2fs will always return 0 exit code indicating successful operation even though the mount failed either because we failed to properly read the file system in the first place, or the
fuse_main()failed for whatever reason.Fix it by using the return code from
fuse_main(), or return 32 in case we fail because the file system is corrupted, or we encountered a problem preventing us mounting the file system. 32 because this is a libmount exit code indicating mount failed.Signed-off-by Lukas Czerner <[email protected]>--- misc/fuse2fs.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)misc/fuse2fs.c +4
-4@@ -3720,7 +3720,7 @@ int main(int argc, char *argv[]) {struct fuse_args args =FUSE_ARGS_INIT (argc, argv);struct fuse2fs fctx; -errcode_t err; +errcode_t err =0 ;char *logfile;char extra_args[BUFSIZ ];int ret =0 , flags =EXT2_FLAG_64BITS |EXT2_FLAG_EXCLUSIVE ;@@ -3753,6 +3753,7 @@ int main(int argc, char *argv[]) fctx.err_fp =fopen (logfile,"a" );if (!fctx.err_fp) {perror (logfile); + err = errno; goto out; } }else @@ -3766,7 +3767,6 @@ int main(int argc, char *argv[]) }/* Start up the fs (while we still can use stdout) */ - ret =2 ;if (!fctx.ro) flags |=EXT2_FLAG_RW ; err = ext2fs_open2(fctx.device, NULL, flags, 0, 0, unix_io_manager,@@ -3779,8 +3779,6 @@ int main(int argc, char *argv[]) fctx.fs = global_fs; global_fs->priv_data = &fctx; - ret =3 ; -if (ext2fs_has_feature_journal_needs_recovery (global_fs->super)) {if (!fctx.ro) {printf (_ ("%s: recovering journal\n" ), fctx.device);@@ -3797,6 +3795,7 @@ int main(int argc, char *argv[]) }else {printf ("%s" ,_ ("Journal needs recovery; running " "`e2fsck -E journal_only' is required.\n" )); + err =1 ; goto out; } }@@ -3836,6 +3835,7 @@ int main(int argc, char *argv[])if (global_fs->super->s_state &EXT2_ERROR_FS ) {printf ("%s" ,_ ("Errors detected; running e2fsck is required.\n" )); + err =1 ; goto out; }
misc/fuse2fs.c int main(int argc, char *argv[]) · line 3862
Quoted 5 lines
misc/fuse2fs.c +1
-1@@ -3859,11 +3859,16 @@ int main(int argc, char *argv[]) }pthread_mutex_init (&fctx.bfl,NULL ); -fuse_main (args.argc, args.argv, &fs_ops, &fctx); + ret =fuse_main (args.argc, args.argv, &fs_ops, &fctx);
Hmmm, what /does/ fuse_main return? According to the libfuse github site it returns 0 for success and "nonzero" for failure. The source code seems to return 1 on failure (I think), but we probably ought to set ret to 1 ("incorrect invocation or permissions") or 32 explicitly just in case they ever change their minds...
misc/fuse2fs.c int main(int argc, char *argv[]) · line 3871
Quoted 10 lines
misc/fuse2fs.c +6
-1pthread_mutex_destroy (&fctx.bfl); - ret =0 ; out: +/* +* Encountered error reading the file system. Return standard "mount +* failure" mount exit code (32). +*/ +if (err) + ret =32 ;
...I guess "mount failure" for libext2fs problems is good enough, though part of me thinks that we should return 1 if ext2fs_open can't open the block device due to EPERM/EACCESS.
<shrug> OTOH "mount failure" is sufficiently vague to hide just about anything behind. :)
--D
Quoted 6 lines
misc/fuse2fs.c +0
-0if (global_fs) { err =ext2fs_close (global_fs);if (err)Signature · 1 line
2.17.1
Quoted 73 lines · Darrick J. Wong
Quoted 72 lines · Lukas Czerner
Currently fuse2fs will always return 0 exit code indicating successful operation even though the mount failed either because we failed to properly read the file system in the first place, or the
fuse_main()failed for whatever reason.Fix it by using the return code from
fuse_main(), or return 32 in case we fail because the file system is corrupted, or we encountered a problem preventing us mounting the file system. 32 because this is a libmount exit code indicating mount failed.Signed-off-by Lukas Czerner <[email protected]>--- misc/fuse2fs.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)misc/fuse2fs.c +4
-4@@ -3720,7 +3720,7 @@ int main(int argc, char *argv[]) {struct fuse_args args =FUSE_ARGS_INIT (argc, argv);struct fuse2fs fctx; -errcode_t err; +errcode_t err =0 ;char *logfile;char extra_args[BUFSIZ ];int ret =0 , flags =EXT2_FLAG_64BITS |EXT2_FLAG_EXCLUSIVE ;@@ -3753,6 +3753,7 @@ int main(int argc, char *argv[]) fctx.err_fp =fopen (logfile,"a" );if (!fctx.err_fp) {perror (logfile); + err = errno; goto out; } }else @@ -3766,7 +3767,6 @@ int main(int argc, char *argv[]) }/* Start up the fs (while we still can use stdout) */ - ret =2 ;if (!fctx.ro) flags |=EXT2_FLAG_RW ; err = ext2fs_open2(fctx.device, NULL, flags, 0, 0, unix_io_manager,@@ -3779,8 +3779,6 @@ int main(int argc, char *argv[]) fctx.fs = global_fs; global_fs->priv_data = &fctx; - ret =3 ; -if (ext2fs_has_feature_journal_needs_recovery (global_fs->super)) {if (!fctx.ro) {printf (_ ("%s: recovering journal\n" ), fctx.device);@@ -3797,6 +3795,7 @@ int main(int argc, char *argv[]) }else {printf ("%s" ,_ ("Journal needs recovery; running " "`e2fsck -E journal_only' is required.\n" )); + err =1 ; goto out; } }@@ -3836,6 +3835,7 @@ int main(int argc, char *argv[])if (global_fs->super->s_state &EXT2_ERROR_FS ) {printf ("%s" ,_ ("Errors detected; running e2fsck is required.\n" )); + err =1 ; goto out; }@@ -3859,11 +3859,16 @@ int main(int argc, char *argv[]) }
Quoted 9 lines
misc/fuse2fs.c int main(int argc, char *argv[]) · line 3862
Quoted 3 lines
misc/fuse2fs.c +1
-1pthread_mutex_init (&fctx.bfl,NULL ); -fuse_main (args.argc, args.argv, &fs_ops, &fctx); + ret =fuse_main (args.argc, args.argv, &fs_ops, &fctx);Hmmm, what /does/
fuse_mainreturn? According to the libfuse github site it returns 0 for success and "nonzero" for failure. The source code seems to return 1 on failure (I think), but we probably ought to set ret to 1 ("incorrect invocation or permissions") or 32 explicitly just in case they ever change their minds...
Ah yeah, I was assuming (wrongly) that the fuse_main would return the libmount codes, which is not true. The source code for libfuse says:
* The following error codes may be returned from fuse_main():
* 1: Invalid option arguments * 2: No mount point specified * 3: FUSE setup failed * 4: Mounting failed * 5: Failed to daemonize (detach from session) * 6: Failed to set up signal handlers * 7: An error occured during the life of the file system
so the question is, do we want to map it somehow to the libmount error codes ?
* [u]mount(8) exit code 0: no errors
* [u]mount(8) exit code 1: incorrect invocation or permission
* [u]mount(8) exit code 2: out of memory, cannot fork, ...
* [u]mount(8) exit code 4: internal mount bug or wrong version
* [u]mount(8) exit code 8: user interrupt
* [u]mount(8) exit code 16: problems writing, locking, ... mtab/utab
* [u]mount(8) exit code 32: mount failure
* [u]mount(8) exit code 64: some mount succeeded; usually when executed with
* --all options. Never returned by libmount.
Or just return whatever we get from the main_fuse() and 1 for other errors ? I do not really mind either way.
-Lukas
Quoted 27 lines
misc/fuse2fs.c int main(int argc, char *argv[]) · line 3871
Quoted 11 lines
misc/fuse2fs.c +6
-1pthread_mutex_destroy (&fctx.bfl); - ret =0 ; out: +/* +* Encountered error reading the file system. Return standard "mount +* failure" mount exit code (32). +*/ +if (err) + ret =32 ;...I guess "mount failure" for libext2fs problems is good enough, though part of me thinks that we should return 1 if
ext2fs_opencan't open the block device due to EPERM/EACCESS.<shrug> OTOH "mount failure" is sufficiently vague to hide just about anything behind. :)
--D
Quoted 6 lines
misc/fuse2fs.c +0
-0if (global_fs) { err =ext2fs_close (global_fs);if (err)Signature · 1 line
2.17.1