]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
generators: define custom main func definer and use it where applicable
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 4 Dec 2018 10:49:42 +0000 (11:49 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 12 Dec 2018 20:58:00 +0000 (21:58 +0100)
There should be no functional difference, except that the error message
is changd from "three or no arguments" to "zero or three arguments". Somehow
the inverted form always seemed strange.

umask() call is also dropped from run-generator. I think it wasn't dropped in
053254e3cb215df3b8c905bc39b920f8817e1c7d because the run generator was merged
around the same time.

src/cryptsetup/cryptsetup-generator.c
src/debug-generator/debug-generator.c
src/fstab-generator/fstab-generator.c
src/rc-local-generator/rc-local-generator.c
src/run-generator/run-generator.c
src/shared/generator.h
src/shared/main-func.h
src/sysv-generator/sysv-generator.c
src/veritysetup/veritysetup-generator.c

index 746505a1811e2710bacdb2298352cbb2facca03a..8759a26148093c45aa0c1f56df94a5b26feb870c 100644 (file)
@@ -13,7 +13,6 @@
 #include "hashmap.h"
 #include "id128-util.h"
 #include "log.h"
-#include "main-func.h"
 #include "mkdir.h"
 #include "parse-util.h"
 #include "path-util.h"
@@ -33,7 +32,7 @@ typedef struct crypto_device {
         bool create;
 } crypto_device;
 
-static const char *arg_dest = "/tmp";
+static const char *arg_dest = NULL;
 static bool arg_enabled = true;
 static bool arg_read_crypttab = true;
 static bool arg_whitelist = false;
@@ -580,16 +579,10 @@ static int add_proc_cmdline_devices(void) {
 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func,
                                               crypto_device, crypt_device_free);
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
-
-        if (argc > 1)
-                arg_dest = argv[1];
+        assert_se(arg_dest = dest);
 
         arg_disks = hashmap_new(&crypt_device_hash_ops);
         if (!arg_disks)
@@ -613,4 +606,4 @@ static int run(int argc, char *argv[]) {
         return 0;
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);
index 2615aa189df90351ae179551d8f351625df318c4..fa4ca57bbf3aa46a8788b8ebdc8992392fb70cd3 100644 (file)
@@ -2,7 +2,6 @@
 
 #include "alloc-util.h"
 #include "generator.h"
-#include "main-func.h"
 #include "mkdir.h"
 #include "parse-util.h"
 #include "proc-cmdline.h"
@@ -12,8 +11,8 @@
 #include "unit-name.h"
 #include "util.h"
 
+static const char *arg_dest = NULL;
 static char *arg_default_unit = NULL;
-static const char *arg_dest = "/tmp";
 static char **arg_mask = NULL;
 static char **arg_wants = NULL;
 static bool arg_debug_shell = false;
@@ -142,17 +141,10 @@ static int generate_wants_symlinks(void) {
         return r;
 }
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r, q;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                       "This program takes three or no arguments.");
-
-        if (argc > 1)
-                arg_dest = argv[2];
+        assert_se(arg_dest = dest_early);
 
         r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_RD_STRICT | PROC_CMDLINE_STRIP_RD_PREFIX);
         if (r < 0)
@@ -170,4 +162,4 @@ static int run(int argc, char *argv[]) {
         return r < 0 ? r : q;
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);
index 8acee53d6cf6e673104f7a135c11722177a76f8f..d5e5412059d869ee1dc9478ae517feb34b7cf2f1 100644 (file)
@@ -40,8 +40,8 @@ typedef enum MountpointFlags {
         GROWFS    = 1 << 4,
 } MountpointFlags;
 
-static const char *arg_dest = "/tmp";
-static const char *arg_dest_late = "/tmp";
+static const char *arg_dest = NULL;
+static const char *arg_dest_late = NULL;
 static bool arg_fstab_enabled = true;
 static char *arg_root_what = NULL;
 static char *arg_root_fstype = NULL;
@@ -868,19 +868,11 @@ static int determine_root(void) {
         return 1;
 }
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
-                                       "This program takes three or no arguments.");
-
-        if (argc > 1)
-                arg_dest = argv[1];
-        if (argc > 3)
-                arg_dest_late = argv[3];
+        assert_se(arg_dest = dest);
+        assert_se(arg_dest_late = dest_late);
 
         r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
         if (r < 0)
@@ -928,4 +920,4 @@ static int run(int argc, char *argv[]) {
         return r;
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);
index cd5510bf12a7d64abc85097623d159b5d473345e..7a3948e92d986f0a9b460d16ac5943eebb4b76a7 100644 (file)
@@ -6,12 +6,11 @@
 
 #include "generator.h"
 #include "log.h"
-#include "main-func.h"
 #include "mkdir.h"
 #include "string-util.h"
 #include "util.h"
 
-static const char *arg_dest = "/tmp";
+static const char *arg_dest = NULL;
 
 /* So you are reading this, and might wonder: why is this implemented as a generator rather than as a plain, statically
  * enabled service that carries appropriate ConditionFileIsExecutable= lines? The answer is this: conditions bypass
@@ -55,16 +54,10 @@ static int check_executable(const char *path) {
         return 0;
 }
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r = 0, k = 0;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
-
-        if (argc > 1)
-                arg_dest = argv[1];
+        assert_se(arg_dest = dest);
 
         if (check_executable(RC_LOCAL_SCRIPT_PATH_START) >= 0) {
                 log_debug("Automatically adding rc-local.service.");
@@ -81,4 +74,4 @@ static int run(int argc, char *argv[]) {
         return r < 0 ? r : k;
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);
index 243a426cda8d9b322941ab46ee1f789d49301d0b..a5dfac01d4ac76c088ef987eedd656ede9d89f04 100644 (file)
@@ -5,13 +5,12 @@
 #include "fd-util.h"
 #include "fileio.h"
 #include "generator.h"
-#include "main-func.h"
 #include "mkdir.h"
 #include "proc-cmdline.h"
 #include "specifier.h"
 #include "strv.h"
 
-static const char *arg_dest = "/tmp";
+static const char *arg_dest = NULL;
 static char **arg_commands = NULL;
 static char *arg_success_action = NULL;
 static char *arg_failure_action = NULL;
@@ -122,20 +121,10 @@ static int generate(void) {
         return 0;
 }
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4) {
-                log_error("This program takes three or no arguments.");
-                return -EINVAL;
-        }
-
-        if (argc > 1)
-                arg_dest = argv[1];
-
-        umask(0022);
+        assert_se(arg_dest = dest);
 
         r = proc_cmdline_parse(parse, NULL, PROC_CMDLINE_RD_STRICT|PROC_CMDLINE_STRIP_RD_PREFIX);
         if (r < 0)
@@ -144,4 +133,4 @@ static int run(int argc, char *argv[]) {
         return generate();
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);
index f1a055401a60dbd9a977584ab8af16c1847a2822..5a1c1e32f7f7d765c23bf562031e0f2cda8e1f0e 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <stdio.h>
 
+#include "main-func.h"
+
 int generator_open_unit_file(
         const char *dest,
         const char *source,
@@ -49,3 +51,17 @@ int generator_hook_up_growfs(
         const char *target);
 
 void log_setup_generator(void);
+
+/* Similar to DEFINE_MAIN_FUNCTION, but initializes logging and assigns positional arguments. */
+#define DEFINE_MAIN_GENERATOR_FUNCTION(impl)                            \
+        _DEFINE_MAIN_FUNCTION(                                          \
+                ({                                                      \
+                        log_setup_generator();                          \
+                        if (argc > 1 && argc != 4)                      \
+                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), \
+                                                       "This program takes zero or three arguments."); \
+                }),                                                     \
+                impl(argc > 1 ? argv[1] : "/tmp",                       \
+                     argc > 1 ? argv[2] : "/tmp",                       \
+                     argc > 1 ? argv[3] : "/tmp"),                      \
+                r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)
index ab6b272117d7dfd9463bfea9cae01c32a86bc576..3c182e802b985c5ce25150690055cb242e4b0b5b 100644 (file)
@@ -9,10 +9,11 @@
 #include "spawn-polkit-agent.h"
 #include "static-destruct.h"
 
-#define _DEFINE_MAIN_FUNCTION(impl, ret)                                \
+#define _DEFINE_MAIN_FUNCTION(intro, impl, ret)                         \
         int main(int argc, char *argv[]) {                              \
                 int r;                                                  \
-                r = impl(argc, argv);                                   \
+                intro;                                                  \
+                r = impl;                                               \
                 static_destruct();                                      \
                 ask_password_agent_close();                             \
                 polkit_agent_close();                                   \
 /* Negative return values from impl are mapped to EXIT_FAILURE, and
  * everything else means success! */
 #define DEFINE_MAIN_FUNCTION(impl)                                      \
-        _DEFINE_MAIN_FUNCTION(impl, r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)
+        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)
 
 /* Zero is mapped to EXIT_SUCCESS, negative values are mapped to EXIT_FAILURE,
  * and postive values are propagated.
  * Note: "true" means failure! */
 #define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl)                \
-        _DEFINE_MAIN_FUNCTION(impl, r < 0 ? EXIT_FAILURE : r)
+        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), r < 0 ? EXIT_FAILURE : r)
index c4acdc4a50b54601b6bd635c30ba6a6c74f72362..514b6e01695206ff0b0a1edc504fe005929feecd 100644 (file)
@@ -42,7 +42,7 @@ static const struct {
          * means they are shut down anyway at system power off if running. */
 };
 
-static const char *arg_dest = "/tmp";
+static const char *arg_dest = NULL;
 
 typedef struct SysvStub {
         char *name;
@@ -917,22 +917,14 @@ finish:
         return r;
 }
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         _cleanup_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
         _cleanup_(lookup_paths_free) LookupPaths lp = {};
         SysvStub *service;
         Iterator j;
         int r;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4) {
-                log_error("This program takes three or no arguments.");
-                return -EINVAL;
-        }
-
-        if (argc > 1)
-                arg_dest = argv[3];
+        assert_se(arg_dest = dest_late);
 
         r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
         if (r < 0)
@@ -961,4 +953,4 @@ static int run(int argc, char *argv[]) {
         return 0;
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);
index 51996ec37e210ee1208550e0e7ed02d3cacdea55..65a4e7b0fd558d5d200fb5998d89631ee23c79ec 100644 (file)
@@ -204,16 +204,10 @@ static int determine_devices(void) {
         return 1;
 }
 
-static int run(int argc, char *argv[]) {
+static int run(const char *dest, const char *dest_early, const char *dest_late) {
         int r;
 
-        log_setup_generator();
-
-        if (argc > 1 && argc != 4)
-                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
-
-        if (argc > 1)
-                arg_dest = argv[1];
+        assert_se(arg_dest = dest);
 
         r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
         if (r < 0)
@@ -229,11 +223,7 @@ static int run(int argc, char *argv[]) {
         if (r < 0)
                 return r;
 
-        r = create_device();
-        if (r < 0)
-                return r;
-
-        return 0;
+        return create_device();
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_GENERATOR_FUNCTION(run);