]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-send: close fd on exit when running with valgrind 22605/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 22 Feb 2022 17:03:54 +0000 (02:03 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 22 Feb 2022 22:38:43 +0000 (07:38 +0900)
Fixes an issue reported in #22576.

src/libsystemd/meson.build
src/libsystemd/sd-journal/journal-send.c
src/libsystemd/sd-journal/journal-send.h [new file with mode: 0644]
src/libsystemd/sd-journal/test-journal-send.c

index 2e5255d4794798e9d7776a4f269a0649e5318abc..efd22207ace8f7084e80b3e2a2eb60918e1cc743 100644 (file)
@@ -12,6 +12,7 @@ sd_journal_sources = files(
         'sd-journal/journal-file.h',
         'sd-journal/journal-internal.h',
         'sd-journal/journal-send.c',
+        'sd-journal/journal-send.h',
         'sd-journal/journal-vacuum.c',
         'sd-journal/journal-vacuum.h',
         'sd-journal/journal-verify.c',
index 421782515631793cb6d1d0c61783b287bb24395d..1e10ed55244cc4e5aef28f7d2534c673e8e0ab54 100644 (file)
@@ -6,6 +6,9 @@
 #include <stddef.h>
 #include <sys/un.h>
 #include <unistd.h>
+#if HAVE_VALGRIND_VALGRIND_H
+#include <valgrind/valgrind.h>
+#endif
 
 #define SD_JOURNAL_SUPPRESS_LOCATION
 
@@ -14,8 +17,9 @@
 #include "alloc-util.h"
 #include "errno-util.h"
 #include "fd-util.h"
-#include "io-util.h"
 #include "fileio.h"
+#include "io-util.h"
+#include "journal-send.h"
 #include "memfd-util.h"
 #include "socket-util.h"
 #include "stdio-util.h"
  * all its threads, and all its subprocesses. This means we need to
  * initialize it atomically, and need to operate on it atomically
  * never assuming we are the only user */
+static int fd_plus_one = 0;
 
 static int journal_fd(void) {
         int fd;
-        static int fd_plus_one = 0;
 
 retry:
         if (fd_plus_one > 0)
@@ -62,6 +66,24 @@ retry:
         return fd;
 }
 
+#if VALGRIND
+void close_journal_fd(void) {
+        /* Be nice to valgrind. This is not atomic. This must be used only in tests. */
+
+        if (!RUNNING_ON_VALGRIND)
+                return;
+
+        if (getpid() != gettid())
+                return;
+
+        if (fd_plus_one <= 0)
+                return;
+
+        safe_close(fd_plus_one - 1);
+        fd_plus_one = 0;
+}
+#endif
+
 _public_ int sd_journal_print(int priority, const char *format, ...) {
         int r;
         va_list ap;
diff --git a/src/libsystemd/sd-journal/journal-send.h b/src/libsystemd/sd-journal/journal-send.h
new file mode 100644 (file)
index 0000000..cf8b199
--- /dev/null
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#if VALGRIND
+void close_journal_fd(void);
+#else
+static inline void close_journal_fd(void) {}
+#endif
index b6644e65c1abc40ea78d5bb8f5c10706ebedff90..533b8d91e6a9d0049913aec06f29003ad6eb3070 100644 (file)
@@ -5,7 +5,9 @@
 #include <unistd.h>
 
 #include "sd-journal.h"
+
 #include "fileio.h"
+#include "journal-send.h"
 #include "macro.h"
 #include "memory-util.h"
 
@@ -103,5 +105,6 @@ int main(int argc, char *argv[]) {
         /* Sleep a bit to make it easy for journald to collect metadata. */
         sleep(1);
 
+        close_journal_fd();
         return 0;
 }