main-func: Reduce transitive includes

Let's move some logic from _DEFINE_MAIN_FUNCTION() and other places
in main-func.h into functions that we implement in main-func.c to
allow moving some included headers from the header to the .c file.
This commit is contained in:
Daan De Meyer
2025-05-06 10:07:27 +02:00
parent b75c6c3c3f
commit 76d62b63ef
48 changed files with 114 additions and 41 deletions
+1
View File
@@ -4,6 +4,7 @@
#include <unistd.h>
#include "alloc-util.h"
#include "argv-util.h"
#include "cgroup-util.h"
#include "dropin.h"
#include "escape.h"
+50
View File
@@ -0,0 +1,50 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#if HAVE_VALGRIND_VALGRIND_H
# include <valgrind/valgrind.h>
#endif
#include "sd-daemon.h"
#include "argv-util.h"
#include "ask-password-agent.h"
#include "hashmap.h"
#include "main-func.h"
#include "pager.h"
#include "polkit-agent.h"
#include "selinux-util.h"
#include "signal-util.h"
void main_prepare(int argc, char *argv[]) {
assert_se(argc > 0 && !isempty(argv[0]));
save_argc_argv(argc, argv);
}
void main_finalize(int r, int exit_status) {
if (r < 0)
(void) sd_notifyf(0, "ERRNO=%i", -r);
(void) sd_notifyf(0, "EXIT_STATUS=%i", exit_status);
ask_password_agent_close();
polkit_agent_close();
pager_close();
mac_selinux_finish();
}
int raise_or_exit_status(int ret) {
if (ret < 0)
return EXIT_FAILURE;
if (ret == 0)
return EXIT_SUCCESS;
if (!SIGNAL_VALID(ret))
return EXIT_FAILURE;
#if HAVE_VALGRIND_VALGRIND_H
/* If raise() below succeeds, the destructor cleanup_pools() in hashmap.c will never called. */
if (RUNNING_ON_VALGRIND)
hashmap_trim_pools();
#endif
(void) raise(ret);
/* exit with failure if raise() does not immediately abort the program. */
return EXIT_FAILURE;
}
+7 -41
View File
@@ -3,36 +3,19 @@
#include <stdlib.h>
#if HAVE_VALGRIND_VALGRIND_H
# include <valgrind/valgrind.h>
#endif
#include "sd-daemon.h"
#include "argv-util.h"
#include "ask-password-agent.h"
#include "hashmap.h"
#include "pager.h"
#include "polkit-agent.h"
#include "selinux-util.h"
#include "signal-util.h"
#include "static-destruct.h"
void main_prepare(int argc, char *argv[]);
void main_finalize(int r, int exit_status);
#define _DEFINE_MAIN_FUNCTION(intro, impl, result_to_exit_status, result_to_return_value) \
int main(int argc, char *argv[]) { \
int r; \
assert_se(argc > 0 && !isempty(argv[0])); \
save_argc_argv(argc, argv); \
main_prepare(argc, argv); \
intro; \
r = impl; \
if (r < 0) \
(void) sd_notifyf(0, "ERRNO=%i", -r); \
(void) sd_notifyf(0, "EXIT_STATUS=%i", \
result_to_exit_status(r)); \
ask_password_agent_close(); \
polkit_agent_close(); \
pager_close(); \
mac_selinux_finish(); \
main_finalize(r, result_to_exit_status(r)); \
static_destruct(); \
return result_to_return_value(r); \
}
@@ -56,24 +39,7 @@ static inline int exit_failure_if_nonzero(int result) {
#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl) \
_DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_nonzero, exit_failure_if_nonzero)
static inline int raise_or_exit_status(int ret) {
if (ret < 0)
return EXIT_FAILURE;
if (ret == 0)
return EXIT_SUCCESS;
if (!SIGNAL_VALID(ret))
return EXIT_FAILURE;
#if HAVE_VALGRIND_VALGRIND_H
/* If raise() below succeeds, the destructor cleanup_pools() in hashmap.c will never called. */
if (RUNNING_ON_VALGRIND)
hashmap_trim_pools();
#endif
(void) raise(ret);
/* exit with failure if raise() does not immediately abort the program. */
return EXIT_FAILURE;
}
int raise_or_exit_status(int ret);
/* Negative return values from impl are mapped to EXIT_FAILURE, zero is mapped to EXIT_SUCCESS,
* and raise if a positive signal is returned from impl. */
+1
View File
@@ -119,6 +119,7 @@ shared_sources = files(
'machine-id-setup.c',
'machine-pool.c',
'macvlan-util.c',
'main-func.c',
'mkdir-label.c',
'mkfs-util.c',
'module-util.c',