Skip to content

Commit c6f7962

Browse files
keszybzbluca
authored andcommitted
coredump: restore compatibility with older patterns
This was broken in f45b801. Unfortunately the review does not talk about backward compatibility at all. There are two places where it matters: - During upgrades, the replacement of kernel.core_pattern is asynchronous. For example, during rpm upgrades, it would be updated a post-transaction file trigger. In other scenarios, the update might only happen after reboot. We have a potentially long window where the old pattern is in place. We need to capture coredumps during upgrades too. - With --backtrace. The interface of --backtrace, in hindsight, is not great. But there are users of --backtrace which were written to use a specific set of arguments, and we can't just break compatiblity. One example is systemd-coredump-python, but there are also reports of users using --backtrace to generate coredump logs. Thus, we require the original set of args, and will use the additional args if found. A test is added to verify that --backtrace works with and without the optional args. (cherry picked from commit ded0aac) (cherry picked from commit f9b8b75c11bba9b63096904be98cc529c304eb97) (cherry picked from commit 385a33b043406ad79a7207f3906c3b15192a3333)
1 parent 92df356 commit c6f7962

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/coredump/coredump.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ enum {
9595
META_ARGV_SIGNAL, /* %s: number of signal causing dump */
9696
META_ARGV_TIMESTAMP, /* %t: time of dump, expressed as seconds since the Epoch (we expand this to μs granularity) */
9797
META_ARGV_RLIMIT, /* %c: core file size soft resource limit */
98-
META_ARGV_HOSTNAME, /* %h: hostname */
98+
_META_ARGV_REQUIRED,
99+
/* The fields below were added to kernel/core_pattern at later points, so they might be missing. */
100+
META_ARGV_HOSTNAME = _META_ARGV_REQUIRED, /* %h: hostname */
99101
_META_ARGV_MAX,
102+
/* If new fields are added, they should be added here, to maintain compatibility
103+
* with callers which don't know about the new fields. */
100104

101105
/* The following indexes are cached for a couple of special fields we use (and
102106
* thereby need to be retrieved quickly) for naming coredump files, and attaching
@@ -107,7 +111,7 @@ enum {
107111
_META_MANDATORY_MAX,
108112

109113
/* The rest are similar to the previous ones except that we won't fail if one of
110-
* them is missing. */
114+
* them is missing in a message sent over the socket. */
111115

112116
META_EXE = _META_MANDATORY_MAX,
113117
META_UNIT,
@@ -1169,14 +1173,17 @@ static int gather_pid_metadata_from_argv(
11691173
assert(context);
11701174

11711175
/* We gather all metadata that were passed via argv[] into an array of iovecs that
1172-
* we'll forward to the socket unit */
1176+
* we'll forward to the socket unit.
1177+
*
1178+
* We require at least _META_ARGV_REQUIRED args, but will accept more.
1179+
* We know how to parse _META_ARGV_MAX args. The rest will be ignored. */
11731180

1174-
if (argc < _META_ARGV_MAX)
1181+
if (argc < _META_ARGV_REQUIRED)
11751182
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1176-
"Not enough arguments passed by the kernel (%i, expected %i).",
1177-
argc, _META_ARGV_MAX);
1183+
"Not enough arguments passed by the kernel (%i, expected between %i and %i).",
1184+
argc, _META_ARGV_REQUIRED, _META_ARGV_MAX);
11781185

1179-
for (int i = 0; i < _META_ARGV_MAX; i++) {
1186+
for (int i = 0; i < MIN(argc, _META_ARGV_MAX); i++) {
11801187

11811188
t = argv[i];
11821189

test/units/testsuite-74.coredump.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,18 @@ rm -f /tmp/core.{output,redirected}
186186
(! "${UNPRIV_CMD[@]}" coredumpctl dump "$CORE_TEST_BIN" >/dev/null)
187187

188188
# --backtrace mode
189-
# Pass one of the existing journal coredump records to systemd-coredump and
190-
# use our PID as the source to make matching the coredump later easier
191-
# systemd-coredump args: PID UID GID SIGNUM TIMESTAMP CORE_SOFT_RLIMIT HOSTNAME
189+
# Pass one of the existing journal coredump records to systemd-coredump.
190+
# Use our PID as the source to be able to create a PIDFD and to make matching easier.
191+
# systemd-coredump args: PID UID GID SIGNUM TIMESTAMP CORE_SOFT_RLIMIT [HOSTNAME]
192192
journalctl -b -n 1 --output=export --output-fields=MESSAGE,COREDUMP COREDUMP_EXE="/usr/bin/test-dump" |
193-
/usr/lib/systemd/systemd-coredump --backtrace $$ 0 0 6 1679509994 12345 mymachine
194-
# Wait a bit for the coredump to get processed
195-
timeout 30 bash -c "while [[ \$(coredumpctl list -q --no-legend $$ | wc -l) -eq 0 ]]; do sleep 1; done"
196-
coredumpctl info "$$"
193+
/usr/lib/systemd/systemd-coredump --backtrace $$ 0 0 6 1679509900 12345
194+
journalctl -b -n 1 --output=export --output-fields=MESSAGE,COREDUMP COREDUMP_EXE="/usr/bin/test-dump" |
195+
/usr/lib/systemd/systemd-coredump --backtrace $$ 0 0 6 1679509901 12345 mymachine
196+
# Wait a bit for the coredumps to get processed
197+
timeout 30 bash -c "while [[ \$(coredumpctl list -q --no-legend $$ | wc -l) -lt 2 ]]; do sleep 1; done"
198+
coredumpctl info $$
199+
coredumpctl info COREDUMP_TIMESTAMP=1679509900000000
200+
coredumpctl info COREDUMP_TIMESTAMP=1679509901000000
197201
coredumpctl info COREDUMP_HOSTNAME="mymachine"
198202

199203
# This used to cause a stack overflow

0 commit comments

Comments
 (0)