]> git-server-git.apps.pok.os.sepia.ceph.com Git - xfsprogs-dev.git/commitdiff
libfrog: hoist a couple of service helper functions
authorDarrick J. Wong <djwong@kernel.org>
Mon, 2 Mar 2026 18:19:40 +0000 (10:19 -0800)
committerDarrick J. Wong <djwong@kernel.org>
Thu, 9 Apr 2026 22:30:16 +0000 (15:30 -0700)
Hoist a couple of service/daemon-related helper functions to libfrog so
that we can share the code between xfs_scrub and xfs_healer.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
libfrog/systemd.h
scrub/xfs_scrub.c

index 4f414bc3c1e9c3a217e4566d3dfdde50a6f8c482..c96df4afa39aa69c33c664c70e655059f3630662 100644 (file)
@@ -17,4 +17,32 @@ enum systemd_unit_manage {
 
 int systemd_manage_unit(enum systemd_unit_manage how, const char *unitname);
 
+static inline bool systemd_is_service(void)
+{
+       return getenv("SERVICE_MODE") != NULL;
+}
+
+/* Special processing for a service/daemon program that is exiting. */
+static inline int
+systemd_service_exit(int ret)
+{
+       /*
+        * We have to sleep 2 seconds here because journald uses the pid to
+        * connect our log messages to the systemd service.  This is critical
+        * for capturing all the log messages if the service fails, because
+        * failure analysis tools use the service name to gather log messages
+        * for reporting.
+        */
+       sleep(2);
+
+       /*
+        * If we're being run as a service, the return code must fit the LSB
+        * init script action error guidelines, which is to say that we
+        * compress all errors to 1 ("generic or unspecified error", LSB 5.0
+        * section 22.2) and hope the admin will scan the log for what actually
+        * happened.
+        */
+       return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
 #endif /* __LIBFROG_SYSTEMD_H__ */
index 3dba972a7e8d2ab45355c3bdc424c3f9100ffe97..79937aa8cce4c46e33718c36ee01bf1e9b7e166a 100644 (file)
@@ -19,6 +19,7 @@
 #include "unicrash.h"
 #include "progress.h"
 #include "libfrog/histogram.h"
+#include "libfrog/systemd.h"
 
 /*
  * XFS Online Metadata Scrub (and Repair)
@@ -866,8 +867,7 @@ main(
        if (stdout_isatty && !progress_fp)
                progress_fp = fdopen(1, "w+");
 
-       if (getenv("SERVICE_MODE"))
-               is_service = true;
+       is_service = systemd_is_service();
 
        /* Initialize overall phase stats. */
        error = phase_start(&all_pi, 0, NULL);
@@ -960,29 +960,15 @@ out_unicrash:
        hist_free(&ctx.datadev_hist);
        hist_free(&ctx.rtdev_hist);
 
-       /*
-        * If we're being run as a service, the return code must fit the LSB
-        * init script action error guidelines, which is to say that we
-        * compress all errors to 1 ("generic or unspecified error", LSB 5.0
-        * section 22.2) and hope the admin will scan the log for what
-        * actually happened.
-        *
-        * We have to sleep 2 seconds here because journald uses the pid to
-        * connect our log messages to the systemd service.  This is critical
-        * for capturing all the log messages if the scrub fails, because the
-        * fail service uses the service name to gather log messages for the
-        * error report.
-        *
-        * Note: We don't count a lack of kernel support as a service failure
-        * because we haven't determined that there's anything wrong with the
-        * filesystem.
-        */
        if (is_service) {
-               sleep(2);
+               /*
+                * Note: We don't count a lack of kernel support as a service
+                * failure because we haven't determined that there's anything
+                * wrong with the filesystem.
+                */
                if (!ctx.scrub_setup_succeeded)
-                       return 0;
-               if (ret != SCRUB_RET_SUCCESS)
-                       return 1;
+                       ret = 0;
+               return systemd_service_exit(ret);
        }
 
        return ret;