]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Report failure of 'create --command' command
authorEric Gillespie <epg@pretzelnet.org>
Thu, 11 Dec 2025 17:58:02 +0000 (11:58 -0600)
committerEric Gillespie <epg@pretzelnet.org>
Sat, 20 Dec 2025 23:23:20 +0000 (17:23 -0600)
When we can't even run a child process at all (system(3) returns -1),
throw runtime_error_with_errno without taking the post snapshot.

When the child is killed or exits with non-zero status, report that to
stderr and exit(EXIT_FAILURE) .

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

index 75f674365a7cbf9012a0e6fa789b5fef927d03ca..65cbaeec2ed9e8b73b7c4df7ab40e384919198eb 100644 (file)
@@ -21,6 +21,8 @@
  */
 
 
+#include <sys/wait.h>
+
 #include <iostream>
 
 #include <snapper/AppUtil.h>
@@ -193,10 +195,22 @@ namespace snapper
 
            case CreateType::PRE_POST: {
                snapshot1 = snapper->createPreSnapshot(scd, report);
-               system(command.c_str());
+               int const status = system(command.c_str());
+               if (status == -1) {
+                   throw runtime_error_with_errno("open failed", errno);
+               }
                snapshot2 = snapper->createPostSnapshot(snapshot1, scd, report);
                if (print_number)
                    cout << snapshot1->getNum() << ".." << snapshot2->getNum() << endl;
+               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)));
+                   }
+               } else {
+                   SN_THROW(Exception(sformat("%s killed with %d", command.c_str(), WTERMSIG(status))));
+               }
            } break;
        }
     }
index 613e07d0261ac74e7532d40396723d501cba15e2..2b4a793c5c929d04c890861edd4af07f606b8453 100644 (file)
            <varlistentry>
              <term><option>--command</option> <replaceable>command</replaceable></term>
              <listitem>
-               <para>Create a pre and post snapshot and run command in between.</para>
+               <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>
              </listitem>
            </varlistentry>
            <varlistentry>