generators: define custom main func definer and use it where applicable

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
053254e3cb because the run generator was merged
around the same time.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-12-12 21:58:00 +01:00
parent aba311f7ff
commit 7a44c7e31f
9 changed files with 51 additions and 93 deletions
+16
View 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)
+5 -4
View 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(); \
@@ -24,10 +25,10 @@
/* 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)