]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: call all cleanup functions before raise() 31028/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 20 Jan 2024 13:14:14 +0000 (22:14 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 11 Feb 2024 17:36:20 +0000 (02:36 +0900)
Note, even with this, memory allocated internally by glibc is not freed.
But, at least, memory explicitly allocated by us is freed cleanly even
Ctrl-C is pressed during 'journalctl --follow'.

Closes #30995.

src/journal/journalctl.c
src/shared/generator.h
src/shared/main-func.h

index d3ec22e224ef369c1295fbe605fc2550807f7ee5..073dc1426caf627f2d5c891a3601098f108d5a00 100644 (file)
@@ -2726,19 +2726,12 @@ static int run(int argc, char *argv[]) {
                         return r;
                 sig = r;
 
-                /* unref signal event sources. */
-                e = sd_event_unref(e);
-
                 r = update_cursor(j);
                 if (r < 0)
                         return r;
 
                 /* re-send the original signal. */
-                assert(SIGNAL_VALID(sig));
-                if (raise(sig) < 0)
-                        log_error("Failed to raise the original signal SIG%s, ignoring: %m", signal_to_string(sig));
-
-                return 0;
+                return sig;
         }
 
         r = show(&c);
@@ -2763,4 +2756,4 @@ static int run(int argc, char *argv[]) {
         return 0;
 }
 
-DEFINE_MAIN_FUNCTION(run);
+DEFINE_MAIN_FUNCTION_WITH_POSITIVE_SIGNAL(run);
index a5dc2847d70525d8699d73b207ea32aeffdc8ad7..e5db1c17ce0d74134d47d863f1cc87accdfaf255 100644 (file)
@@ -102,4 +102,5 @@ void log_setup_generator(void);
                 impl(argv[1],                                           \
                      argv[argc == 4 ? 2 : 1],                           \
                      argv[argc == 4 ? 3 : 1]),                          \
+                exit_failure_if_negative,                               \
                 exit_failure_if_negative)
index b3aff4cee774d615fb772173482c8aa1863d4aff..d0689b42d9cae934c955223c35ddf0e6cda916f1 100644 (file)
@@ -3,16 +3,22 @@
 
 #include <stdlib.h>
 
+#if HAVE_VALGRIND_VALGRIND_H
+#  include <valgrind/valgrind.h>
+#endif
+
 #include "sd-daemon.h"
 
 #include "argv-util.h"
+#include "hashmap.h"
 #include "pager.h"
 #include "selinux-util.h"
+#include "signal-util.h"
 #include "spawn-ask-password-agent.h"
 #include "spawn-polkit-agent.h"
 #include "static-destruct.h"
 
-#define _DEFINE_MAIN_FUNCTION(intro, impl, result_to_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]));               \
@@ -28,7 +34,7 @@
                 pager_close();                                          \
                 mac_selinux_finish();                                   \
                 static_destruct();                                      \
-                return result_to_exit_status(r);                        \
+                return result_to_return_value(r);                       \
         }
 
 static inline int exit_failure_if_negative(int result) {
@@ -38,7 +44,7 @@ static inline int exit_failure_if_negative(int result) {
 /* Negative return values from impl are mapped to EXIT_FAILURE, and
  * everything else means success! */
 #define DEFINE_MAIN_FUNCTION(impl)                                      \
-        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_negative)
+        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_negative, exit_failure_if_negative)
 
 static inline int exit_failure_if_nonzero(int result) {
         return result < 0 ? EXIT_FAILURE : result;
@@ -48,4 +54,28 @@ static inline int exit_failure_if_nonzero(int result) {
  * and positive values are propagated.
  * Note: "true" means failure! */
 #define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl)                \
-        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_nonzero)
+        _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;
+}
+
+/* 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. */
+#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_SIGNAL(impl)                 \
+        _DEFINE_MAIN_FUNCTION(,impl(argc, argv), exit_failure_if_negative, raise_or_exit_status)