From 3c1ea052a1b188ca12242700aa43efab5b0432fa Mon Sep 17 00:00:00 2001
From: Mike Yuan <me@yhndnzj.com>
Date: Sat, 9 Dec 2023 00:06:16 +0800
Subject: [PATCH 0032/1160] core/executor: do destruct static variables and
selinux before exiting
I was wondering why I couldn't trigger the assertion in safe_fclose()
when submitting #30251. It turned out that the static destructor was
not run at all :/
Replace main() with a minimized version of main-func.h. This also
prevents emitting negative exit codes.
(cherry picked from commit ba8245a77a074bf65db79a60d2b6e390d76ebde3)
src/core/executor.c | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
@@ -19,9 +19,10 @@
#include "label-util.h"
#include "parse-util.h"
#include "pretty-print.h"
+#include "selinux-util.h"
#include "static-destruct.h"
-static FILE* arg_serialization = NULL;
+static FILE *arg_serialization = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_serialization, fclosep);
@@ -171,9 +172,8 @@ static int parse_argv(int argc, char *argv[]) {
return 1 /* work to do */;
}
-int main(int argc, char *argv[]) {
+static int run(int argc, char *argv[]) {
_cleanup_fdset_free_ FDSet *fdset = NULL;
- int exit_status = EXIT_SUCCESS, r;
_cleanup_(cgroup_context_done) CGroupContext cgroup_context = {};
_cleanup_(exec_context_done) ExecContext context = {};
_cleanup_(exec_command_done) ExecCommand command = {};
@@ -188,15 +188,11 @@ int main(int argc, char *argv[]) {
.shared = &shared,
.dynamic_creds = &dynamic_creds,
};
+ int exit_status = EXIT_SUCCESS, r;
exec_context_init(&context);
cgroup_context_init(&cgroup_context);
- /* We use safe_fork() for spawning sd-pam helper process, which internally calls rename_process().
- * As the last step of renaming, all saved argvs are memzero()-ed. Hence, we need to save the argv
- * first to prevent showing "intense" cmdline. See #30352. */
- save_argc_argv(argc, argv);
-
/* We might be starting the journal itself, we'll be told by the caller what to do */
log_set_always_reopen_console(true);
log_set_prohibit_ipc(true);
@@ -258,3 +254,19 @@ int main(int argc, char *argv[]) {
return exit_status;
}
+
+int main(int argc, char *argv[]) {
+ int r;
+
+ /* We use safe_fork() for spawning sd-pam helper process, which internally calls rename_process().
+ * As the last step of renaming, all saved argvs are memzero()-ed. Hence, we need to save the argv
+ * first to prevent showing "intense" cmdline. See #30352. */
+ save_argc_argv(argc, argv);
+
+ r = run(argc, argv);
+
+ mac_selinux_finish();
+ static_destruct();
+
+ return r < 0 ? EXIT_FAILURE : r;
+}
--
2.33.0