1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

Merge pull request #11046 from keszybz/generator-mains

Macroify generators a bit more
This commit is contained in:
Lennart Poettering 2018-12-13 22:39:23 +01:00 committed by GitHub
commit fd0ec39d38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 103 additions and 166 deletions

View File

@ -13,7 +13,6 @@
#include "hashmap.h" #include "hashmap.h"
#include "id128-util.h" #include "id128-util.h"
#include "log.h" #include "log.h"
#include "main-func.h"
#include "mkdir.h" #include "mkdir.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
@ -33,7 +32,7 @@ typedef struct crypto_device {
bool create; bool create;
} crypto_device; } crypto_device;
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static bool arg_enabled = true; static bool arg_enabled = true;
static bool arg_read_crypttab = true; static bool arg_read_crypttab = true;
static bool arg_whitelist = false; 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, DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func,
crypto_device, crypt_device_free); 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; int r;
if (argc > 1 && argc != 4) assert_se(arg_dest = dest);
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
if (argc > 1)
arg_dest = argv[1];
log_setup_generator();
arg_disks = hashmap_new(&crypt_device_hash_ops); arg_disks = hashmap_new(&crypt_device_hash_ops);
if (!arg_disks) if (!arg_disks)
@ -613,4 +606,4 @@ static int run(int argc, char *argv[]) {
return 0; return 0;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -2,7 +2,6 @@
#include "alloc-util.h" #include "alloc-util.h"
#include "generator.h" #include "generator.h"
#include "main-func.h"
#include "mkdir.h" #include "mkdir.h"
#include "parse-util.h" #include "parse-util.h"
#include "proc-cmdline.h" #include "proc-cmdline.h"
@ -12,8 +11,8 @@
#include "unit-name.h" #include "unit-name.h"
#include "util.h" #include "util.h"
static const char *arg_dest = NULL;
static char *arg_default_unit = NULL; static char *arg_default_unit = NULL;
static const char *arg_dest = "/tmp";
static char **arg_mask = NULL; static char **arg_mask = NULL;
static char **arg_wants = NULL; static char **arg_wants = NULL;
static bool arg_debug_shell = false; static bool arg_debug_shell = false;
@ -142,17 +141,10 @@ static int generate_wants_symlinks(void) {
return r; 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; int r, q;
if (argc > 1 && argc != 4) assert_se(arg_dest = dest_early);
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program takes three or no arguments.");
if (argc > 1)
arg_dest = argv[2];
log_setup_generator();
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_RD_STRICT | PROC_CMDLINE_STRIP_RD_PREFIX); r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_RD_STRICT | PROC_CMDLINE_STRIP_RD_PREFIX);
if (r < 0) if (r < 0)
@ -170,4 +162,4 @@ static int run(int argc, char *argv[]) {
return r < 0 ? r : q; return r < 0 ? r : q;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -40,8 +40,8 @@ typedef enum MountpointFlags {
GROWFS = 1 << 4, GROWFS = 1 << 4,
} MountpointFlags; } MountpointFlags;
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static const char *arg_dest_late = "/tmp"; static const char *arg_dest_late = NULL;
static bool arg_fstab_enabled = true; static bool arg_fstab_enabled = true;
static char *arg_root_what = NULL; static char *arg_root_what = NULL;
static char *arg_root_fstype = NULL; static char *arg_root_fstype = NULL;
@ -539,7 +539,7 @@ static int parse_fstab(bool initrd) {
if (!what) if (!what)
return log_oom(); return log_oom();
if (is_device_path(what) && path_is_read_only_fs("sys") > 0) { if (is_device_path(what) && path_is_read_only_fs("/sys") > 0) {
log_info("Running in a container, ignoring fstab device entry for %s.", what); log_info("Running in a container, ignoring fstab device entry for %s.", what);
continue; continue;
} }
@ -868,19 +868,11 @@ static int determine_root(void) {
return 1; 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; int r;
if (argc > 1 && argc != 4) assert_se(arg_dest = dest);
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), assert_se(arg_dest_late = dest_late);
"This program takes three or no arguments.");
if (argc > 1)
arg_dest = argv[1];
if (argc > 3)
arg_dest_late = argv[3];
log_setup_generator();
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
if (r < 0) if (r < 0)
@ -928,4 +920,4 @@ static int run(int argc, char *argv[]) {
return r; return r;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -13,13 +13,13 @@
#include "mkdir.h" #include "mkdir.h"
#include "path-util.h" #include "path-util.h"
#include "process-util.h" #include "process-util.h"
#include "string-util.h" #include "strv.h"
#include "terminal-util.h" #include "terminal-util.h"
#include "unit-name.h" #include "unit-name.h"
#include "util.h" #include "util.h"
#include "virt.h" #include "virt.h"
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static int add_symlink(const char *fservice, const char *tservice) { static int add_symlink(const char *fservice, const char *tservice) {
char *from, *to; char *from, *to;
@ -99,37 +99,21 @@ static int verify_tty(const char *name) {
return 0; return 0;
} }
int main(int argc, char *argv[]) { static int run(const char *dest, const char *dest_early, const char *dest_late) {
static const char virtualization_consoles[] =
"hvc0\0"
"xvc0\0"
"hvsi0\0"
"sclp_line0\0"
"ttysclp0\0"
"3270!tty1\0";
_cleanup_free_ char *active = NULL; _cleanup_free_ char *active = NULL;
const char *j; const char *j;
int r; int r;
if (argc > 1 && argc != 4) { assert_se(arg_dest = dest);
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
arg_dest = argv[1];
log_setup_generator();
if (detect_container() > 0) { if (detect_container() > 0) {
_cleanup_free_ char *container_ttys = NULL; _cleanup_free_ char *container_ttys = NULL;
log_debug("Automatically adding console shell."); log_debug("Automatically adding console shell.");
if (add_symlink("console-getty.service", "console-getty.service") < 0) r = add_symlink("console-getty.service", "console-getty.service");
return EXIT_FAILURE; if (r < 0)
return r;
/* When $container_ttys is set for PID 1, spawn /* When $container_ttys is set for PID 1, spawn
* gettys on all ptys named therein. Note that despite * gettys on all ptys named therein. Note that despite
@ -157,13 +141,14 @@ int main(int argc, char *argv[]) {
if (!t) if (!t)
continue; continue;
if (add_container_getty(t) < 0) r = add_container_getty(t);
return EXIT_FAILURE; if (r < 0)
return r;
} }
} }
/* Don't add any further magic if we are in a container */ /* Don't add any further magic if we are in a container */
return EXIT_SUCCESS; return 0;
} }
if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
@ -176,10 +161,8 @@ int main(int argc, char *argv[]) {
_cleanup_free_ char *tty = NULL; _cleanup_free_ char *tty = NULL;
tty = strndup(word, l); tty = strndup(word, l);
if (!tty) { if (!tty)
log_oom(); return log_oom();
return EXIT_FAILURE;
}
/* We assume that gettys on virtual terminals are /* We assume that gettys on virtual terminals are
* started via manual configuration and do this magic * started via manual configuration and do this magic
@ -191,23 +174,33 @@ int main(int argc, char *argv[]) {
if (verify_tty(tty) < 0) if (verify_tty(tty) < 0)
continue; continue;
if (add_serial_getty(tty) < 0) r = add_serial_getty(tty);
return EXIT_FAILURE; if (r < 0)
return r;
} }
} }
/* Automatically add in a serial getty on the first /* Automatically add in a serial getty on the first
* virtualizer console */ * virtualizer console */
NULSTR_FOREACH(j, virtualization_consoles) { FOREACH_STRING(j,
char *p; "hvc0",
"xvc0",
"hvsi0",
"sclp_line0",
"ttysclp0",
"3270!tty1") {
const char *p;
p = strjoina("/sys/class/tty/", j); p = strjoina("/sys/class/tty/", j);
if (access(p, F_OK) < 0) if (access(p, F_OK) < 0)
continue; continue;
if (add_serial_getty(j) < 0) r = add_serial_getty(j);
return EXIT_FAILURE; if (r < 0)
return r;
} }
return EXIT_SUCCESS; return 0;
} }
DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -36,7 +36,7 @@
#include "util.h" #include "util.h"
#include "virt.h" #include "virt.h"
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static bool arg_enabled = true; static bool arg_enabled = true;
static bool arg_root_enabled = true; static bool arg_root_enabled = true;
static bool arg_root_rw = false; static bool arg_root_rw = false;
@ -668,22 +668,14 @@ static int add_mounts(void) {
return enumerate_partitions(devno); return enumerate_partitions(devno);
} }
int main(int argc, char *argv[]) { static int run(const char *dest, const char *dest_early, const char *dest_late) {
int r, k; int r, k;
if (argc > 1 && argc != 4) { assert_se(arg_dest = dest_late);
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
arg_dest = argv[3];
log_setup_generator();
if (detect_container() > 0) { if (detect_container() > 0) {
log_debug("In a container, exiting."); log_debug("In a container, exiting.");
return EXIT_SUCCESS; return 0;
} }
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
@ -692,19 +684,19 @@ int main(int argc, char *argv[]) {
if (!arg_enabled) { if (!arg_enabled) {
log_debug("Disabled, exiting."); log_debug("Disabled, exiting.");
return EXIT_SUCCESS; return 0;
} }
if (arg_root_enabled) if (arg_root_enabled)
r = add_root_mount(); r = add_root_mount();
else
r = 0;
if (!in_initrd()) { if (!in_initrd()) {
k = add_mounts(); k = add_mounts();
if (k < 0) if (r >= 0)
r = k; r = k;
} }
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; return r;
} }
DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -6,12 +6,11 @@
#include "generator.h" #include "generator.h"
#include "log.h" #include "log.h"
#include "main-func.h"
#include "mkdir.h" #include "mkdir.h"
#include "string-util.h" #include "string-util.h"
#include "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 /* 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 * 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; 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; int r = 0, k = 0;
log_setup_generator(); assert_se(arg_dest = dest);
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 (check_executable(RC_LOCAL_SCRIPT_PATH_START) >= 0) { if (check_executable(RC_LOCAL_SCRIPT_PATH_START) >= 0) {
log_debug("Automatically adding rc-local.service."); log_debug("Automatically adding rc-local.service.");
@ -81,4 +74,4 @@ static int run(int argc, char *argv[]) {
return r < 0 ? r : k; return r < 0 ? r : k;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -5,13 +5,12 @@
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "generator.h" #include "generator.h"
#include "main-func.h"
#include "mkdir.h" #include "mkdir.h"
#include "proc-cmdline.h" #include "proc-cmdline.h"
#include "specifier.h" #include "specifier.h"
#include "strv.h" #include "strv.h"
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static char **arg_commands = NULL; static char **arg_commands = NULL;
static char *arg_success_action = NULL; static char *arg_success_action = NULL;
static char *arg_failure_action = NULL; static char *arg_failure_action = NULL;
@ -122,20 +121,10 @@ static int generate(void) {
return 0; 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; int r;
log_setup_generator(); assert_se(arg_dest = dest);
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);
r = proc_cmdline_parse(parse, NULL, PROC_CMDLINE_RD_STRICT|PROC_CMDLINE_STRIP_RD_PREFIX); r = proc_cmdline_parse(parse, NULL, PROC_CMDLINE_RD_STRICT|PROC_CMDLINE_STRIP_RD_PREFIX);
if (r < 0) if (r < 0)
@ -144,4 +133,4 @@ static int run(int argc, char *argv[]) {
return generate(); return generate();
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -3,6 +3,8 @@
#include <stdio.h> #include <stdio.h>
#include "main-func.h"
int generator_open_unit_file( int generator_open_unit_file(
const char *dest, const char *dest,
const char *source, const char *source,
@ -49,3 +51,17 @@ int generator_hook_up_growfs(
const char *target); const char *target);
void log_setup_generator(void); 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)

View File

@ -9,10 +9,11 @@
#include "spawn-polkit-agent.h" #include "spawn-polkit-agent.h"
#include "static-destruct.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 main(int argc, char *argv[]) { \
int r; \ int r; \
r = impl(argc, argv); \ intro; \
r = impl; \
static_destruct(); \ static_destruct(); \
ask_password_agent_close(); \ ask_password_agent_close(); \
polkit_agent_close(); \ polkit_agent_close(); \
@ -24,10 +25,10 @@
/* Negative return values from impl are mapped to EXIT_FAILURE, and /* Negative return values from impl are mapped to EXIT_FAILURE, and
* everything else means success! */ * everything else means success! */
#define DEFINE_MAIN_FUNCTION(impl) \ #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, /* Zero is mapped to EXIT_SUCCESS, negative values are mapped to EXIT_FAILURE,
* and postive values are propagated. * and postive values are propagated.
* Note: "true" means failure! */ * Note: "true" means failure! */
#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl) \ #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)

View File

@ -15,7 +15,7 @@
* Implements the logic described in systemd.offline-updates(7). * Implements the logic described in systemd.offline-updates(7).
*/ */
static const char *arg_dest = "/tmp"; static const char *arg_dest = NULL;
static int generate_symlink(void) { static int generate_symlink(void) {
const char *p = NULL; const char *p = NULL;
@ -51,26 +51,20 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
return 0; return 0;
} }
int main(int argc, char *argv[]) { static int run(const char *dest, const char *dest_early, const char *dest_late) {
int r, k; int r;
if (argc > 1 && argc != 4) { assert_se(arg_dest = dest_early);
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
arg_dest = argv[2];
log_setup_generator();
r = generate_symlink(); r = generate_symlink();
if (r < 0)
return r;
if (r > 0) { r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
k = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); if (r < 0)
if (k < 0) log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
log_warning_errno(k, "Failed to parse kernel command line, ignoring: %m");
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; return 0;
} }
DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -42,7 +42,7 @@ static const struct {
* means they are shut down anyway at system power off if running. */ * 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 { typedef struct SysvStub {
char *name; char *name;
@ -917,22 +917,14 @@ finish:
return r; 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_(free_sysvstub_hashmapp) Hashmap *all_services = NULL;
_cleanup_(lookup_paths_free) LookupPaths lp = {}; _cleanup_(lookup_paths_free) LookupPaths lp = {};
SysvStub *service; SysvStub *service;
Iterator j; Iterator j;
int r; int r;
log_setup_generator(); assert_se(arg_dest = dest_late);
if (argc > 1 && argc != 4) {
log_error("This program takes three or no arguments.");
return -EINVAL;
}
if (argc > 1)
arg_dest = argv[3];
r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL); r = lookup_paths_init(&lp, UNIT_FILE_SYSTEM, LOOKUP_PATHS_EXCLUDE_GENERATED, NULL);
if (r < 0) if (r < 0)
@ -961,4 +953,4 @@ static int run(int argc, char *argv[]) {
return 0; return 0;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);

View File

@ -204,16 +204,10 @@ static int determine_devices(void) {
return 1; 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; int r;
if (argc > 1 && argc != 4) assert_se(arg_dest = dest);
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
if (argc > 1)
arg_dest = argv[1];
log_setup_generator();
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
if (r < 0) if (r < 0)
@ -229,11 +223,7 @@ static int run(int argc, char *argv[]) {
if (r < 0) if (r < 0)
return r; return r;
r = create_device(); return create_device();
if (r < 0)
return r;
return 0;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_GENERATOR_FUNCTION(run);