From 32a1575fbd8807bad2570af0d631a0263f0eb765 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 1 Mar 2017 15:46:54 +0100 Subject: coredump: normalize generation/parsing of COREDUMP_TRUNCATED= Given that this is a field primarily processed by computers, and not so much by humans, assign "1" instead of "yes". Also, use parse_boolean() as we usually do for parsing it again. This makes things more alike udev options (as one example), such as SYSTEMD_READY where we also spit out "1" and "0", and parse with parse_boolean(). --- src/coredump/coredump.c | 2 +- src/coredump/coredumpctl.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 270af630cf..190ffc631e 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -795,7 +795,7 @@ log: IOVEC_SET_STRING(iovec[n_iovec++], core_message); if (truncated) - IOVEC_SET_STRING(iovec[n_iovec++], "COREDUMP_TRUNCATED=yes"); + IOVEC_SET_STRING(iovec[n_iovec++], "COREDUMP_TRUNCATED=1"); /* Optionally store the entire coredump in the journal */ if (arg_storage == COREDUMP_STORAGE_JOURNAL) { diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 3e9a00bbcf..bffb3d458f 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -414,7 +414,7 @@ static int print_list(FILE* file, sd_journal *j, int had_legend) { else present = "-"; - if (STR_IN_SET(present, "present", "journal") && streq_ptr(truncated, "yes")) + if (STR_IN_SET(present, "present", "journal") && truncated && parse_boolean(truncated) > 0) present = "truncated"; fprintf(file, "%-*s %*s %*s %*s %*s %-*s %s\n", @@ -583,8 +583,10 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) { fprintf(file, " Hostname: %s\n", hostname); if (filename) { - bool inacc = access(filename, R_OK) < 0; - bool trunc = streq_ptr(truncated, "yes"); + bool inacc, trunc; + + inacc = access(filename, R_OK) < 0; + trunc = truncated && parse_boolean(truncated) > 0; if (inacc || trunc) fprintf(file, " Storage: %s%s (%s%s%s)%s\n", -- cgit v1.2.3-54-g00ecf From b8cda92df500693e73655c221afd316522e0746d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 1 Mar 2017 15:58:40 +0100 Subject: coredump: introduce is_journald_crash() and is_pid1_crash() helpers We check these a number of times, hence let's unify these checks here. This also allows us to make the PID 1 check more elaborate as we can check both the PID and the cgroup. Checking the PID has the benefit that we'll also cover cases where PID 1 might still be in the root cgroup, and the cgroup check has the benefit that we also cover crashes in forked off crasher processes (the way we actually do it in systemd) --- src/coredump/coredump.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 190ffc631e..4c4f36aea0 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -695,6 +695,19 @@ static int change_uid_gid(const char *context[]) { return drop_privileges(uid, gid, 0); } +static bool is_journald_crash(const char *context[_CONTEXT_MAX]) { + assert(context); + + return streq_ptr(context[CONTEXT_UNIT], SPECIAL_JOURNALD_SERVICE); +} + +static bool is_pid1_crash(const char *context[_CONTEXT_MAX]) { + assert(context); + + return streq_ptr(context[CONTEXT_UNIT], SPECIAL_INIT_SCOPE) || + streq_ptr(context[CONTEXT_PID], "1"); +} + #define SUBMIT_COREDUMP_FIELDS 4 static int submit_coredump( @@ -715,7 +728,7 @@ static int submit_coredump( assert(n_iovec_allocated >= n_iovec + SUBMIT_COREDUMP_FIELDS); assert(input_fd >= 0); - journald_crash = streq_ptr(context[CONTEXT_UNIT], SPECIAL_JOURNALD_SERVICE); + journald_crash = is_journald_crash(context); /* Vacuum before we write anything again */ (void) coredump_vacuum(-1, arg_keep_free, arg_max_use); @@ -1103,14 +1116,14 @@ static int gather_pid_metadata( log_warning_errno(r, "Failed to get EXE, ignoring: %m"); if (cg_pid_get_unit(pid, &context[CONTEXT_UNIT]) >= 0) { - if (!streq(context[CONTEXT_UNIT], SPECIAL_JOURNALD_SERVICE)) { + if (!is_journald_crash((const char**) context)) { /* OK, now we know it's not the journal, hence we can make use of it now. */ log_set_target(LOG_TARGET_JOURNAL_OR_KMSG); log_open(); } /* If this is PID 1 disable coredump collection, we'll unlikely be able to process it later on. */ - if (streq(context[CONTEXT_UNIT], SPECIAL_INIT_SCOPE)) { + if (is_pid1_crash((const char**) context)) { log_notice("Due to PID 1 having crashed coredump collection will now be turned off."); (void) write_string_file("/proc/sys/kernel/core_pattern", "|/bin/false", 0); } @@ -1248,9 +1261,7 @@ static int process_kernel(int argc, char* argv[]) { assert(n_iovec <= ELEMENTSOF(iovec)); - if (STRPTR_IN_SET(context[CONTEXT_UNIT], - SPECIAL_JOURNALD_SERVICE, - SPECIAL_INIT_SCOPE)) + if (is_journald_crash((const char**) context) || is_pid1_crash((const char**) context)) r = submit_coredump((const char**) context, iovec, ELEMENTSOF(iovec), n_iovec, STDIN_FILENO); -- cgit v1.2.3-54-g00ecf From 501551e80315b1e50391dbc958bc2c8dcda7cf98 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 1 Mar 2017 16:00:47 +0100 Subject: coredump: define a macro for a "short bus call timeout" I think it would be a good idea to move such fixed, picked values out of the main sources into the head of a file, to make sure they are ultimately tunables. --- src/coredump/coredumpctl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index bffb3d458f..114a13fc78 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -51,6 +51,8 @@ #include "user-util.h" #include "util.h" +#define SHORT_BUS_CALL_TIMEOUT_USEC (3 * USEC_PER_SEC) + static usec_t arg_since = USEC_INFINITY, arg_until = USEC_INFINITY; static enum { @@ -992,7 +994,7 @@ static int check_units_active(void) { if (r < 0) return bus_log_create_error(r); - r = sd_bus_call(bus, m, 3 * USEC_PER_SEC, &error, &reply); + r = sd_bus_call(bus, m, SHORT_BUS_CALL_TIMEOUT_USEC, &error, &reply); if (r < 0) return log_error_errno(r, "Failed to check if any systemd-coredump@.service units are running: %s", bus_error_message(&error, r)); -- cgit v1.2.3-54-g00ecf