]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
main-func: make _DEFINE_MAIN_FUNC() take short function
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 21 Jan 2024 02:53:27 +0000 (11:53 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 11 Feb 2024 17:18:18 +0000 (02:18 +0900)
No functional change, just refactoring.

src/shared/generator.h
src/shared/main-func.h

index c17feafacc257673f4e479f1cd807a862729fd57..a5dc2847d70525d8699d73b207ea32aeffdc8ad7 100644 (file)
@@ -102,4 +102,4 @@ void log_setup_generator(void);
                 impl(argv[1],                                           \
                      argv[argc == 4 ? 2 : 1],                           \
                      argv[argc == 4 ? 3 : 1]),                          \
-                r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)
+                exit_failure_if_negative)
index 3f6b6a849d2d2f35d6b79fb9ae1d2db0384b6e40..b3aff4cee774d615fb772173482c8aa1863d4aff 100644 (file)
@@ -12,7 +12,7 @@
 #include "spawn-polkit-agent.h"
 #include "static-destruct.h"
 
-#define _DEFINE_MAIN_FUNCTION(intro, impl, ret)                         \
+#define _DEFINE_MAIN_FUNCTION(intro, impl, result_to_exit_status)       \
         int main(int argc, char *argv[]) {                              \
                 int r;                                                  \
                 assert_se(argc > 0 && !isempty(argv[0]));               \
                 r = impl;                                               \
                 if (r < 0)                                              \
                         (void) sd_notifyf(0, "ERRNO=%i", -r);           \
-                (void) sd_notifyf(0, "EXIT_STATUS=%i", ret);            \
+                (void) sd_notifyf(0, "EXIT_STATUS=%i",                  \
+                                  result_to_exit_status(r));            \
                 ask_password_agent_close();                             \
                 polkit_agent_close();                                   \
                 pager_close();                                          \
                 mac_selinux_finish();                                   \
                 static_destruct();                                      \
-                return ret;                                             \
+                return result_to_exit_status(r);                        \
         }
 
+static inline int exit_failure_if_negative(int result) {
+        return result < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
 /* Negative return values from impl are mapped to EXIT_FAILURE, and
  * everything else means success! */
 #define DEFINE_MAIN_FUNCTION(impl)                                      \
-        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)
+        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_negative)
+
+static inline int exit_failure_if_nonzero(int result) {
+        return result < 0 ? EXIT_FAILURE : result;
+}
 
 /* Zero is mapped to EXIT_SUCCESS, negative values are mapped to EXIT_FAILURE,
  * and positive values are propagated.
  * Note: "true" means failure! */
 #define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl)                \
-        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), r < 0 ? EXIT_FAILURE : r)
+        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_nonzero)