]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Reuse exit code of 'create --command' command
authorEric Gillespie <epg@pretzelnet.org>
Thu, 11 Dec 2025 23:46:16 +0000 (17:46 -0600)
committerEric Gillespie <epg@pretzelnet.org>
Sat, 20 Dec 2025 23:23:20 +0000 (17:23 -0600)
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.

client/snapper/cmd-create.cc
client/snapper/cmd.h
client/snapper/snapper.cc
doc/snapper.xml.in

index 65cbaeec2ed9e8b73b7c4df7ab40e384919198eb..477b711b7b6ee844fcc8ff56107e3b745dea9ace 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <iostream>
 
+#include <client/snapper/cmd.h>
 #include <snapper/AppUtil.h>
 
 #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))));
index 7bb5c4237710f37cfaac06ba9e1da8e5e99b2fb2..e55c08f4651e7170c3962334a87cfee920c0873e 100644 (file)
@@ -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;
+    };
+
 }
index e2ce27ab9c2992fa856075bff65ecfe36088bf24..a161f4940f5292505ae9b7732ffbe8aac182d336 100644 (file)
@@ -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);
index 2b4a793c5c929d04c890861edd4af07f606b8453..296ddfb9657653f17ba40ed07c83e93ba1b1cc3e 100644 (file)
              <listitem>
                <para>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.</para>
+               command exits with a non-zero exit code, snapper takes the post snapshot and exits
+               with that same exit code.</para>
              </listitem>
            </varlistentry>
            <varlistentry>