From: Darrick J. Wong Date: Mon, 2 Mar 2026 18:19:40 +0000 (-0800) Subject: libfrog: hoist a couple of service helper functions X-Git-Tag: v7.0.0~53 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=27ddec39aa0d9139b45025d0124a7dbfd0b3a63e;p=xfsprogs-dev.git libfrog: hoist a couple of service helper functions 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" Reviewed-by: Christoph Hellwig --- diff --git a/libfrog/systemd.h b/libfrog/systemd.h index 4f414bc3..c96df4af 100644 --- a/libfrog/systemd.h +++ b/libfrog/systemd.h @@ -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__ */ diff --git a/scrub/xfs_scrub.c b/scrub/xfs_scrub.c index 3dba972a..79937aa8 100644 --- a/scrub/xfs_scrub.c +++ b/scrub/xfs_scrub.c @@ -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;