]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/executor: do destruct static variables and selinux before exiting
authorMike Yuan <me@yhndnzj.com>
Fri, 8 Dec 2023 16:06:16 +0000 (00:06 +0800)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 10 Dec 2023 05:13:35 +0000 (14:13 +0900)
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.

src/core/executor.c

index 51c727f3254e6c79cfcf27b6f378dba55c193f02..b2716efeeafe75ba17cc827fe24cb4fa01539a6c 100644 (file)
 #include "label-util.h"
 #include "parse-util.h"
 #include "pretty-print.h"
+#include "selinux-util.h"
 #include "static-destruct.h"
 
-static FILEarg_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;
+}