From: Eric Gillespie Date: Thu, 11 Dec 2025 23:46:16 +0000 (-0600) Subject: Reuse exit code of 'create --command' command X-Git-Tag: v0.13.1~55^2~2 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=ea5f9f15329d8a2ffdf98db6d2b8c2210a1d0260;p=thirdparty%2Fsnapper.git Reuse exit code of 'create --command' command When the child exits with non-zero status, use it as snapper's own exit code, as it may be meaningful. We pass the exit code up the stack with a new CommandException type. --- diff --git a/client/snapper/cmd-create.cc b/client/snapper/cmd-create.cc index 65cbaeec..477b711b 100644 --- a/client/snapper/cmd-create.cc +++ b/client/snapper/cmd-create.cc @@ -25,6 +25,7 @@ #include +#include #include #include "../utils/text.h" @@ -205,8 +206,7 @@ namespace snapper if (WIFEXITED(status)) { int const exit_status = WEXITSTATUS(status); if (exit_status != 0) { - // TODO(epg): Can we have snapper itself exit with this status? - SN_THROW(Exception(sformat("%s exited %d", command.c_str(), exit_status))); + SN_THROW(CommandException(exit_status)); } } else { SN_THROW(Exception(sformat("%s killed with %d", command.c_str(), WTERMSIG(status)))); diff --git a/client/snapper/cmd.h b/client/snapper/cmd.h index 7bb5c423..e55c08f4 100644 --- a/client/snapper/cmd.h +++ b/client/snapper/cmd.h @@ -195,4 +195,14 @@ namespace snapper command_debug(GlobalOptions& global_options, GetOpts& get_opts, ProxySnappers* snappers, ProxySnapper* snapper, Plugins::Report& report); + struct CommandException : public Exception + { + explicit CommandException(int exit_status) + : Exception(), + exit_status(exit_status) + {} + + const int exit_status; + }; + } diff --git a/client/snapper/snapper.cc b/client/snapper/snapper.cc index e2ce27ab..a161f494 100644 --- a/client/snapper/snapper.cc +++ b/client/snapper/snapper.cc @@ -368,6 +368,11 @@ main(int argc, char** argv) cerr << e.what() << endl; exit(EXIT_FAILURE); } + catch (const CommandException& e) + { + SN_CAUGHT(e); + exit(e.exit_status); + } catch (const Exception& e) { SN_CAUGHT(e); diff --git a/doc/snapper.xml.in b/doc/snapper.xml.in index 2b4a793c..296ddfb9 100644 --- a/doc/snapper.xml.in +++ b/doc/snapper.xml.in @@ -537,8 +537,8 @@ Create a pre and post snapshot and run command in between. If snapper is unable to execute the command at all, it does not take the post snapshot. If the - command exits with a non-zero exit code, snapper takes the post snapshot and exits 1 - to indicate the failure. + command exits with a non-zero exit code, snapper takes the post snapshot and exits + with that same exit code.