Skip to content

Commit eb9752d

Browse files
committed
journal-send: close fd on exit when running with valgrind
Fixes an issue reported in systemd#22576.
1 parent 9048a6c commit eb9752d

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/libsystemd/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sd_journal_sources = files(
1212
'sd-journal/journal-file.h',
1313
'sd-journal/journal-internal.h',
1414
'sd-journal/journal-send.c',
15+
'sd-journal/journal-send.h',
1516
'sd-journal/journal-vacuum.c',
1617
'sd-journal/journal-vacuum.h',
1718
'sd-journal/journal-verify.c',

src/libsystemd/sd-journal/journal-send.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <stddef.h>
77
#include <sys/un.h>
88
#include <unistd.h>
9+
#if HAVE_VALGRIND_VALGRIND_H
10+
#include <valgrind/valgrind.h>
11+
#endif
912

1013
#define SD_JOURNAL_SUPPRESS_LOCATION
1114

@@ -14,8 +17,9 @@
1417
#include "alloc-util.h"
1518
#include "errno-util.h"
1619
#include "fd-util.h"
17-
#include "io-util.h"
1820
#include "fileio.h"
21+
#include "io-util.h"
22+
#include "journal-send.h"
1923
#include "memfd-util.h"
2024
#include "socket-util.h"
2125
#include "stdio-util.h"
@@ -39,10 +43,10 @@
3943
* all its threads, and all its subprocesses. This means we need to
4044
* initialize it atomically, and need to operate on it atomically
4145
* never assuming we are the only user */
46+
static int fd_plus_one = 0;
4247

4348
static int journal_fd(void) {
4449
int fd;
45-
static int fd_plus_one = 0;
4650

4751
retry:
4852
if (fd_plus_one > 0)
@@ -62,6 +66,24 @@ static int journal_fd(void) {
6266
return fd;
6367
}
6468

69+
#if VALGRIND
70+
void close_journal_fd(void) {
71+
/* Be nice to valgrind. This is not atomic. This must be used only in tests. */
72+
73+
if (!RUNNING_ON_VALGRIND)
74+
return;
75+
76+
if (getpid() != gettid())
77+
return;
78+
79+
if (fd_plus_one <= 0)
80+
return;
81+
82+
safe_close(fd_plus_one - 1);
83+
fd_plus_one = 0;
84+
}
85+
#endif
86+
6587
_public_ int sd_journal_print(int priority, const char *format, ...) {
6688
int r;
6789
va_list ap;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
#pragma once
3+
4+
#if VALGRIND
5+
void close_journal_fd(void);
6+
#else
7+
static inline void close_journal_fd(void) {}
8+
#endif

src/libsystemd/sd-journal/test-journal-send.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include <unistd.h>
66

77
#include "sd-journal.h"
8+
89
#include "fileio.h"
10+
#include "journal-send.h"
911
#include "macro.h"
1012
#include "memory-util.h"
1113

@@ -103,5 +105,6 @@ int main(int argc, char *argv[]) {
103105
/* Sleep a bit to make it easy for journald to collect metadata. */
104106
sleep(1);
105107

108+
close_journal_fd();
106109
return 0;
107110
}

0 commit comments

Comments
 (0)