[systemd-commits] 6 commits - Makefile.am src/journal src/libsystemd-network src/shared src/sleep tmpfiles.d/systemd.conf

Lennart Poettering lennart at kemper.freedesktop.org
Wed Jun 18 15:00:31 PDT 2014


 Makefile.am                             |    8 
 src/journal/coredump.c                  |  468 +++++++++++++++++++++++++-------
 src/journal/coredump.conf               |   14 
 src/journal/coredumpctl.c               |  408 ++++++++++++++++++++++-----
 src/journal/journald-server.c           |    2 
 src/libsystemd-network/sd-dhcp-server.c |   29 +
 src/shared/acl-util.h                   |    8 
 src/shared/sleep-config.c               |    4 
 src/sleep/sleep.c                       |    1 
 tmpfiles.d/systemd.conf                 |    3 
 10 files changed, 753 insertions(+), 192 deletions(-)

New commits:
commit a035f8191addc9422cbe84bf984adcd43134a0db
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Jun 18 23:55:36 2014 +0200

    coredump: add 3 more metadata fields to coredump entries

diff --git a/src/journal/coredump.c b/src/journal/coredump.c
index a396491..3365f9f 100644
--- a/src/journal/coredump.c
+++ b/src/journal/coredump.c
@@ -346,16 +346,16 @@ int main(int argc, char* argv[]) {
         _cleanup_free_ char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
                 *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
                 *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *coredump_data = NULL,
-                *coredump_filename = NULL;
+                *coredump_filename = NULL, *core_slice = NULL, *core_cgroup = NULL, *core_owner_uid = NULL;
 
         _cleanup_close_ int coredump_fd = -1;
 
-        struct iovec iovec[14];
+        struct iovec iovec[17];
         off_t coredump_size;
         int r, j = 0;
-        pid_t pid;
-        uid_t uid;
+        uid_t uid, owner_uid;
         gid_t gid;
+        pid_t pid;
         char *t;
 
         /* Make sure we never enter a loop */
@@ -459,6 +459,21 @@ int main(int argc, char* argv[]) {
                         IOVEC_SET_STRING(iovec[j++], core_session);
         }
 
+        if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
+                asprintf(&core_owner_uid, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
+
+                if (core_owner_uid)
+                        IOVEC_SET_STRING(iovec[j++], core_owner_uid);
+        }
+
+        if (sd_pid_get_slice(pid, &t) >= 0) {
+                core_slice = strappend("COREDUMP_SLICE=", t);
+                free(t);
+
+                if (core_slice)
+                        IOVEC_SET_STRING(iovec[j++], core_slice);
+        }
+
         if (get_process_exe(pid, &t) >= 0) {
                 core_exe = strappend("COREDUMP_EXE=", t);
                 free(t);
@@ -475,6 +490,14 @@ int main(int argc, char* argv[]) {
                         IOVEC_SET_STRING(iovec[j++], core_cmdline);
         }
 
+        if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) {
+                core_cgroup = strappend("COREDUMP_CGROUP=", t);
+                free(t);
+
+                if (core_cgroup)
+                        IOVEC_SET_STRING(iovec[j++], core_cgroup);
+        }
+
         core_timestamp = strjoin("COREDUMP_TIMESTAMP=", argv[ARG_TIMESTAMP], "000000", NULL);
         if (core_timestamp)
                 IOVEC_SET_STRING(iovec[j++], core_timestamp);
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 8699716..ea45946 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -402,7 +402,8 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                 *pid = NULL, *uid = NULL, *gid = NULL,
                 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
                 *unit = NULL, *user_unit = NULL, *session = NULL,
-                *boot_id = NULL, *machine_id = NULL, *hostname = NULL, *coredump = NULL;
+                *boot_id = NULL, *machine_id = NULL, *hostname = NULL,
+                *coredump = NULL, *slice = NULL, *cgroup = NULL, *owner_uid = NULL;
         const void *d;
         size_t l;
 
@@ -420,6 +421,9 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                 retrieve(d, l, "COREDUMP_UNIT", &unit);
                 retrieve(d, l, "COREDUMP_USER_UNIT", &user_unit);
                 retrieve(d, l, "COREDUMP_SESSION", &session);
+                retrieve(d, l, "COREDUMP_OWNER_UID", &owner_uid);
+                retrieve(d, l, "COREDUMP_SLICE", &slice);
+                retrieve(d, l, "COREDUMP_CGROUP", &cgroup);
                 retrieve(d, l, "_BOOT_ID", &boot_id);
                 retrieve(d, l, "_MACHINE_ID", &machine_id);
                 retrieve(d, l, "_HOSTNAME", &hostname);
@@ -429,12 +433,42 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
                 fputs("\n", file);
 
         fprintf(file,
-                "           PID: %s\n"
-                "           UID: %s\n"
-                "           GID: %s\n",
-                strna(pid),
-                strna(uid),
-                strna(gid));
+                "           PID: %s%s%s\n",
+                ansi_highlight(), strna(pid), ansi_highlight_off());
+
+        if (uid) {
+                uid_t n;
+
+                if (parse_uid(uid, &n) >= 0) {
+                        _cleanup_free_ char *u = NULL;
+
+                        u = uid_to_name(n);
+                        fprintf(file,
+                                "           UID: %s (%s)\n",
+                                uid, u);
+                } else {
+                        fprintf(file,
+                                "           UID: %s\n",
+                                uid);
+                }
+        }
+
+        if (gid) {
+                gid_t n;
+
+                if (parse_gid(gid, &n) >= 0) {
+                        _cleanup_free_ char *g = NULL;
+
+                        g = gid_to_name(n);
+                        fprintf(file,
+                                "           GID: %s (%s)\n",
+                                gid, g);
+                } else {
+                        fprintf(file,
+                                "           GID: %s\n",
+                                gid);
+                }
+        }
 
         if (sgnl) {
                 int sig;
@@ -446,17 +480,37 @@ static int print_info(FILE *file, sd_journal *j, bool need_space) {
         }
 
         if (exe)
-                fprintf(file, "    Executable: %s\n", exe);
+                fprintf(file, "    Executable: %s%s%s\n", ansi_highlight(), exe, ansi_highlight_off());
         if (comm)
                 fprintf(file, "          Comm: %s\n", comm);
         if (cmdline)
                 fprintf(file, "  Command Line: %s\n", cmdline);
+        if (cgroup)
+                fprintf(file, " Control Group: %s\n", cgroup);
         if (unit)
                 fprintf(file, "          Unit: %s\n", unit);
         if (user_unit)
                 fprintf(file, "     User Unit: %s\n", unit);
+        if (slice)
+                fprintf(file, "         Slice: %s\n", slice);
         if (session)
                 fprintf(file, "       Session: %s\n", session);
+        if (owner_uid) {
+                uid_t n;
+
+                if (parse_uid(owner_uid, &n) >= 0) {
+                        _cleanup_free_ char *u = NULL;
+
+                        u = uid_to_name(n);
+                        fprintf(file,
+                                "     Owner UID: %s (%s)\n",
+                                owner_uid, u);
+                } else {
+                        fprintf(file,
+                                "     Owner UID: %s\n",
+                                owner_uid);
+                }
+        }
         if (boot_id)
                 fprintf(file, "       Boot ID: %s\n", boot_id);
         if (machine_id)

commit e15758cce3c606a56c5aab080b54b02e8b263c9b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Jun 18 23:34:59 2014 +0200

    coredump: add new "info" verb to coredumpctl showing detailed information about a coredump

diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index db0f3d6..8699716 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -40,13 +40,14 @@
 
 static enum {
         ACTION_NONE,
+        ACTION_INFO,
         ACTION_LIST,
         ACTION_DUMP,
         ACTION_GDB,
 } arg_action = ACTION_LIST;
 
 static FILE* output = NULL;
-static char* arg_field = NULL;
+static const char* arg_field = NULL;
 
 static int arg_no_pager = false;
 static int arg_no_legend = false;
@@ -93,6 +94,7 @@ static int help(void) {
                "  --version          Print version string\n"
                "  -F --field=FIELD   List all values a certain field takes\n"
                "  list [MATCHES...]  List available coredumps\n"
+               "  info [MATCHES...]  Show detailed information about one or more coredumps\n"
                "  dump [MATCHES...]  Print first matching coredump to stdout\n"
                "  gdb [MATCHES...]   Start gdb for the first matching coredump\n"
                , program_invocation_short_name);
@@ -203,7 +205,6 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                                 log_error("cannot use --field/-F more than once");
                                 return -EINVAL;
                         }
-
                         arg_field = optarg;
                         break;
 
@@ -222,6 +223,8 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                         arg_action = ACTION_DUMP;
                 else if (streq(cmd, "gdb"))
                         arg_action = ACTION_GDB;
+                else if (streq(cmd, "info"))
+                        arg_action = ACTION_INFO;
                 else {
                         log_error("Unknown action '%s'", cmd);
                         return -EINVAL;
@@ -272,11 +275,63 @@ static int retrieve(const void *data,
         return 0;
 }
 
+#define filename_escape(s) xescape((s), "./")
+
+static int make_coredump_path(sd_journal *j, char **ret) {
+        _cleanup_free_ char
+                *pid = NULL, *boot_id = NULL, *tstamp = NULL, *comm = NULL,
+                *p = NULL, *b = NULL, *t = NULL, *c = NULL;
+        const void *d;
+        size_t l;
+        char *fn;
+
+        assert(j);
+        assert(ret);
+
+        SD_JOURNAL_FOREACH_DATA(j, d, l) {
+                retrieve(d, l, "COREDUMP_COMM", &comm);
+                retrieve(d, l, "COREDUMP_PID", &pid);
+                retrieve(d, l, "COREDUMP_TIMESTAMP", &tstamp);
+                retrieve(d, l, "_BOOT_ID", &boot_id);
+        }
+
+        if (!pid || !comm || !tstamp || !boot_id) {
+                log_error("Failed to retrieve necessary fields to find coredump on disk.");
+                return -ENOENT;
+        }
+
+        p = filename_escape(pid);
+        if (!p)
+                return log_oom();
+
+        t = filename_escape(tstamp);
+        if (!t)
+                return log_oom();
+
+        c = filename_escape(comm);
+        if (!t)
+                return log_oom();
+
+        b = filename_escape(boot_id);
+        if (!b)
+                return log_oom();
+
+        fn = strjoin("/var/lib/systemd/coredump/core.", c, ".", b, ".", p, ".", t, NULL);
+        if (!fn)
+                return log_oom();
+
+        *ret = fn;
+        return 0;
+}
+
 static void print_field(FILE* file, sd_journal *j) {
         _cleanup_free_ char *value = NULL;
         const void *d;
         size_t l;
 
+        assert(file);
+        assert(j);
+
         assert(arg_field);
 
         SD_JOURNAL_FOREACH_DATA(j, d, l)
@@ -286,7 +341,7 @@ static void print_field(FILE* file, sd_journal *j) {
                 fprintf(file, "%s\n", value);
 }
 
-static int print_entry(FILE* file, sd_journal *j, int had_legend) {
+static int print_list(FILE* file, sd_journal *j, int had_legend) {
         _cleanup_free_ char
                 *pid = NULL, *uid = NULL, *gid = NULL,
                 *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL;
@@ -296,6 +351,9 @@ static int print_entry(FILE* file, sd_journal *j, int had_legend) {
         char buf[FORMAT_TIMESTAMP_MAX];
         int r;
 
+        assert(file);
+        assert(j);
+
         SD_JOURNAL_FOREACH_DATA(j, d, l) {
                 retrieve(d, l, "COREDUMP_PID", &pid);
                 retrieve(d, l, "COREDUMP_UID", &uid);
@@ -339,6 +397,80 @@ static int print_entry(FILE* file, sd_journal *j, int had_legend) {
         return 0;
 }
 
+static int print_info(FILE *file, sd_journal *j, bool need_space) {
+        _cleanup_free_ char
+                *pid = NULL, *uid = NULL, *gid = NULL,
+                *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL,
+                *unit = NULL, *user_unit = NULL, *session = NULL,
+                *boot_id = NULL, *machine_id = NULL, *hostname = NULL, *coredump = NULL;
+        const void *d;
+        size_t l;
+
+        assert(file);
+        assert(j);
+
+        SD_JOURNAL_FOREACH_DATA(j, d, l) {
+                retrieve(d, l, "COREDUMP_PID", &pid);
+                retrieve(d, l, "COREDUMP_UID", &uid);
+                retrieve(d, l, "COREDUMP_GID", &gid);
+                retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
+                retrieve(d, l, "COREDUMP_EXE", &exe);
+                retrieve(d, l, "COREDUMP_COMM", &comm);
+                retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
+                retrieve(d, l, "COREDUMP_UNIT", &unit);
+                retrieve(d, l, "COREDUMP_USER_UNIT", &user_unit);
+                retrieve(d, l, "COREDUMP_SESSION", &session);
+                retrieve(d, l, "_BOOT_ID", &boot_id);
+                retrieve(d, l, "_MACHINE_ID", &machine_id);
+                retrieve(d, l, "_HOSTNAME", &hostname);
+        }
+
+        if (need_space)
+                fputs("\n", file);
+
+        fprintf(file,
+                "           PID: %s\n"
+                "           UID: %s\n"
+                "           GID: %s\n",
+                strna(pid),
+                strna(uid),
+                strna(gid));
+
+        if (sgnl) {
+                int sig;
+
+                if (safe_atoi(sgnl, &sig) >= 0)
+                        fprintf(file, "        Signal: %s (%s)\n", sgnl, signal_to_string(sig));
+                else
+                        fprintf(file, "        Signal: %s\n", sgnl);
+        }
+
+        if (exe)
+                fprintf(file, "    Executable: %s\n", exe);
+        if (comm)
+                fprintf(file, "          Comm: %s\n", comm);
+        if (cmdline)
+                fprintf(file, "  Command Line: %s\n", cmdline);
+        if (unit)
+                fprintf(file, "          Unit: %s\n", unit);
+        if (user_unit)
+                fprintf(file, "     User Unit: %s\n", unit);
+        if (session)
+                fprintf(file, "       Session: %s\n", session);
+        if (boot_id)
+                fprintf(file, "       Boot ID: %s\n", boot_id);
+        if (machine_id)
+                fprintf(file, "    Machine ID: %s\n", machine_id);
+        if (hostname)
+                fprintf(file, "      Hostname: %s\n", hostname);
+
+        if (make_coredump_path(j, &coredump) >= 0)
+                if (access(coredump, F_OK) >= 0)
+                        fprintf(file, "      Coredump: %s\n", coredump);
+
+        return 0;
+}
+
 static int dump_list(sd_journal *j) {
         int found = 0;
 
@@ -350,10 +482,12 @@ static int dump_list(sd_journal *j) {
         sd_journal_set_data_threshold(j, 4096);
 
         SD_JOURNAL_FOREACH(j) {
-                if (arg_field)
+                if (arg_action == ACTION_INFO)
+                        print_info(stdout, j, found++);
+                else if (arg_field)
                         print_field(stdout, j);
                 else
-                        print_entry(stdout, j, found++);
+                        print_list(stdout, j, found++);
         }
 
         if (!arg_field && !found) {
@@ -381,55 +515,6 @@ static int focus(sd_journal *j) {
         return r;
 }
 
-#define filename_escape(s) xescape((s), "./")
-
-static int make_coredump_path(sd_journal *j, char **ret) {
-        _cleanup_free_ char
-                *pid = NULL, *boot_id = NULL, *tstamp = NULL, *comm = NULL,
-                *p = NULL, *b = NULL, *t = NULL, *c = NULL;
-        const void *d;
-        size_t l;
-        char *fn;
-
-        assert(j);
-        assert(ret);
-
-        SD_JOURNAL_FOREACH_DATA(j, d, l) {
-                retrieve(d, l, "COREDUMP_COMM", &comm);
-                retrieve(d, l, "COREDUMP_PID", &pid);
-                retrieve(d, l, "COREDUMP_TIMESTAMP", &tstamp);
-                retrieve(d, l, "_BOOT_ID", &boot_id);
-        }
-
-        if (!pid || !comm || !tstamp || !boot_id) {
-                log_error("Failed to retrieve necessary fields to find coredump on disk.");
-                return -ENOENT;
-        }
-
-        p = filename_escape(pid);
-        if (!p)
-                return log_oom();
-
-        t = filename_escape(tstamp);
-        if (!t)
-                return log_oom();
-
-        c = filename_escape(comm);
-        if (!t)
-                return log_oom();
-
-        b = filename_escape(boot_id);
-        if (!b)
-                return log_oom();
-
-        fn = strjoin("/var/lib/systemd/coredump/core.", c, ".", b, ".", p, ".", t, NULL);
-        if (!fn)
-                return log_oom();
-
-        *ret = fn;
-        return 0;
-}
-
 static int dump_core(sd_journal* j) {
         const void *data;
         size_t len, ret;
@@ -444,10 +529,10 @@ static int dump_core(sd_journal* j) {
         if (r < 0)
                 return r;
 
-        print_entry(output ? stdout : stderr, j, false);
+        print_info(output ? stdout : stderr, j, false);
 
         if (on_tty() && !output) {
-                log_error("Refusing to dump core to tty");
+                log_error("Refusing to dump core to tty.");
                 return -ENOTTY;
         }
 
@@ -519,7 +604,8 @@ static int run_gdb(sd_journal *j) {
         if (r < 0)
                 return r;
 
-        print_entry(stdout, j, false);
+        print_info(stdout, j, false);
+        fputs("\n", stdout);
 
         r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
         if (r < 0) {
@@ -675,6 +761,7 @@ int main(int argc, char *argv[]) {
         switch(arg_action) {
 
         case ACTION_LIST:
+        case ACTION_INFO:
                 if (!arg_no_pager)
                         pager_open(false);
 

commit a276ae74299fbdcf3321740c74fbf97501c63aad
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Jun 18 23:05:15 2014 +0200

    coredump: make sure coredumpctl can handle externally stored coredumps

diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index bf943bc..db0f3d6 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -36,6 +36,7 @@
 #include "pager.h"
 #include "macro.h"
 #include "journal-internal.h"
+#include "copy.h"
 
 static enum {
         ACTION_NONE,
@@ -45,7 +46,7 @@ static enum {
 } arg_action = ACTION_LIST;
 
 static FILE* output = NULL;
-static char* field = NULL;
+static char* arg_field = NULL;
 
 static int arg_no_pager = false;
 static int arg_no_legend = false;
@@ -79,21 +80,21 @@ static Set *new_matches(void) {
 }
 
 static int help(void) {
-        printf("%s [OPTIONS...] [MATCHES...]\n\n"
+
+        printf("%s [OPTIONS...]\n\n"
                "List or retrieve coredumps from the journal.\n\n"
                "Flags:\n"
-               "  -o --output=FILE  Write output to FILE\n"
-               "     --no-pager     Do not pipe output into a pager\n"
-               "     --no-legend    Do not print the column headers.\n\n"
+               "  -o --output=FILE   Write output to FILE\n"
+               "     --no-pager      Do not pipe output into a pager\n"
+               "     --no-legend     Do not print the column headers.\n\n"
 
                "Commands:\n"
-               "  -h --help         Show this help\n"
-               "  --version         Print version string\n"
-               "  -F --field=FIELD  List all values a certain field takes\n"
-               "  gdb               Start gdb for the first matching coredump\n"
-               "  list              List available coredumps\n"
-               "  dump PID          Print coredump to stdout\n"
-               "  dump PATH         Print coredump to stdout\n"
+               "  -h --help          Show this help\n"
+               "  --version          Print version string\n"
+               "  -F --field=FIELD   List all values a certain field takes\n"
+               "  list [MATCHES...]  List available coredumps\n"
+               "  dump [MATCHES...]  Print first matching coredump to stdout\n"
+               "  gdb [MATCHES...]   Start gdb for the first matching coredump\n"
                , program_invocation_short_name);
 
         return 0;
@@ -198,12 +199,12 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                         break;
 
                 case 'F':
-                        if (field) {
+                        if (arg_field) {
                                 log_error("cannot use --field/-F more than once");
                                 return -EINVAL;
                         }
 
-                        field = optarg;
+                        arg_field = optarg;
                         break;
 
                 case '?':
@@ -227,7 +228,7 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
                 }
         }
 
-        if (field && arg_action != ACTION_LIST) {
+        if (arg_field && arg_action != ACTION_LIST) {
                 log_error("Option --field/-F only makes sense with list");
                 return -EINVAL;
         }
@@ -245,9 +246,10 @@ static int parse_argv(int argc, char *argv[], Set *matches) {
 static int retrieve(const void *data,
                     size_t len,
                     const char *name,
-                    const char **var) {
+                    char **var) {
 
         size_t ident;
+        char *v;
 
         ident = strlen(name) + 1; /* name + "=" */
 
@@ -260,30 +262,34 @@ static int retrieve(const void *data,
         if (((const char*) data)[ident - 1] != '=')
                 return 0;
 
-        *var = strndup((const char*)data + ident, len - ident);
-        if (!*var)
+        v = strndup((const char*)data + ident, len - ident);
+        if (!v)
                 return log_oom();
 
+        free(*var);
+        *var = v;
+
         return 0;
 }
 
 static void print_field(FILE* file, sd_journal *j) {
-        _cleanup_free_ const char *value = NULL;
+        _cleanup_free_ char *value = NULL;
         const void *d;
         size_t l;
 
-        assert(field);
+        assert(arg_field);
 
         SD_JOURNAL_FOREACH_DATA(j, d, l)
-                retrieve(d, l, field, &value);
+                retrieve(d, l, arg_field, &value);
+
         if (value)
                 fprintf(file, "%s\n", value);
 }
 
 static int print_entry(FILE* file, sd_journal *j, int had_legend) {
-        _cleanup_free_ const char
+        _cleanup_free_ char
                 *pid = NULL, *uid = NULL, *gid = NULL,
-                *sgnl = NULL, *exe = NULL;
+                *sgnl = NULL, *exe = NULL, *comm = NULL, *cmdline = NULL;
         const void *d;
         size_t l;
         usec_t t;
@@ -292,18 +298,15 @@ static int print_entry(FILE* file, sd_journal *j, int had_legend) {
 
         SD_JOURNAL_FOREACH_DATA(j, d, l) {
                 retrieve(d, l, "COREDUMP_PID", &pid);
-                retrieve(d, l, "COREDUMP_PID", &pid);
                 retrieve(d, l, "COREDUMP_UID", &uid);
                 retrieve(d, l, "COREDUMP_GID", &gid);
                 retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
                 retrieve(d, l, "COREDUMP_EXE", &exe);
-                if (!exe)
-                        retrieve(d, l, "COREDUMP_COMM", &exe);
-                if (!exe)
-                        retrieve(d, l, "COREDUMP_CMDLINE", &exe);
+                retrieve(d, l, "COREDUMP_COMM", &comm);
+                retrieve(d, l, "COREDUMP_CMDLINE", &cmdline);
         }
 
-        if (!pid && !uid && !gid && !sgnl && !exe) {
+        if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline) {
                 log_warning("Empty coredump log entry");
                 return -EINVAL;
         }
@@ -327,11 +330,11 @@ static int print_entry(FILE* file, sd_journal *j, int had_legend) {
 
         fprintf(file, "%*s %*s %*s %*s %*s %s\n",
                 FORMAT_TIMESTAMP_MAX-1, buf,
-                6, pid,
-                5, uid,
-                5, gid,
-                3, sgnl,
-                exe);
+                6, strna(pid),
+                5, strna(uid),
+                5, strna(gid),
+                3, strna(sgnl),
+                strna(exe ?: (comm ?: cmdline)));
 
         return 0;
 }
@@ -347,13 +350,13 @@ static int dump_list(sd_journal *j) {
         sd_journal_set_data_threshold(j, 4096);
 
         SD_JOURNAL_FOREACH(j) {
-                if (field)
+                if (arg_field)
                         print_field(stdout, j);
                 else
                         print_entry(stdout, j, found++);
         }
 
-        if (!field && !found) {
+        if (!arg_field && !found) {
                 log_notice("No coredumps found");
                 return -ESRCH;
         }
@@ -378,6 +381,55 @@ static int focus(sd_journal *j) {
         return r;
 }
 
+#define filename_escape(s) xescape((s), "./")
+
+static int make_coredump_path(sd_journal *j, char **ret) {
+        _cleanup_free_ char
+                *pid = NULL, *boot_id = NULL, *tstamp = NULL, *comm = NULL,
+                *p = NULL, *b = NULL, *t = NULL, *c = NULL;
+        const void *d;
+        size_t l;
+        char *fn;
+
+        assert(j);
+        assert(ret);
+
+        SD_JOURNAL_FOREACH_DATA(j, d, l) {
+                retrieve(d, l, "COREDUMP_COMM", &comm);
+                retrieve(d, l, "COREDUMP_PID", &pid);
+                retrieve(d, l, "COREDUMP_TIMESTAMP", &tstamp);
+                retrieve(d, l, "_BOOT_ID", &boot_id);
+        }
+
+        if (!pid || !comm || !tstamp || !boot_id) {
+                log_error("Failed to retrieve necessary fields to find coredump on disk.");
+                return -ENOENT;
+        }
+
+        p = filename_escape(pid);
+        if (!p)
+                return log_oom();
+
+        t = filename_escape(tstamp);
+        if (!t)
+                return log_oom();
+
+        c = filename_escape(comm);
+        if (!t)
+                return log_oom();
+
+        b = filename_escape(boot_id);
+        if (!b)
+                return log_oom();
+
+        fn = strjoin("/var/lib/systemd/coredump/core.", c, ".", b, ".", p, ".", t, NULL);
+        if (!fn)
+                return log_oom();
+
+        *ret = fn;
+        return 0;
+}
+
 static int dump_core(sd_journal* j) {
         const void *data;
         size_t len, ret;
@@ -400,19 +452,44 @@ static int dump_core(sd_journal* j) {
         }
 
         r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
-        if (r < 0) {
+        if (r == ENOENT) {
+                _cleanup_free_ char *fn = NULL;
+                _cleanup_close_ int fd = -1;
+
+                r = make_coredump_path(j, &fn);
+                if (r < 0)
+                        return r;
+
+                fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+                if (fd < 0) {
+                        if (errno == ENOENT)
+                                log_error("Coredump neither in journal file nor stored externally on disk.");
+                        else
+                                log_error("Failed to open coredump file: %s", strerror(-r));
+
+                        return -errno;
+                }
+
+                r = copy_bytes(fd, output ? fileno(output) : STDOUT_FILENO);
+                if (r < 0) {
+                        log_error("Failed to stream coredump: %s", strerror(-r));
+                        return r;
+                }
+
+        } else if (r < 0) {
                 log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
                 return r;
-        }
 
-        assert(len >= 9);
-        data = (const uint8_t*) data + 9;
-        len -= 9;
+        } else {
+                assert(len >= 9);
+                data = (const uint8_t*) data + 9;
+                len -= 9;
 
-        ret = fwrite(data, len, 1, output ? output : stdout);
-        if (ret != 1) {
-                log_error("dumping coredump: %m (%zu)", ret);
-                return -errno;
+                ret = fwrite(data, len, 1, output ?: stdout);
+                if (ret != 1) {
+                        log_error("Dumping coredump failed: %m (%zu)", ret);
+                        return -errno;
+                }
         }
 
         r = sd_journal_previous(j);
@@ -423,15 +500,16 @@ static int dump_core(sd_journal* j) {
 }
 
 static int run_gdb(sd_journal *j) {
-        char path[] = "/var/tmp/coredump-XXXXXX";
+
+        _cleanup_free_ char *exe = NULL, *coredump = NULL;
+        char temp[] = "/var/tmp/coredump-XXXXXX";
+        bool unlink_temp = false;
+        const char *path;
         const void *data;
+        siginfo_t st;
         size_t len;
-        ssize_t sz;
         pid_t pid;
-        _cleanup_free_ char *exe = NULL;
         int r;
-        _cleanup_close_ int fd = -1;
-        siginfo_t st;
 
         assert(j);
 
@@ -462,35 +540,63 @@ static int run_gdb(sd_journal *j) {
                 return -ENOENT;
         }
 
+        if (!path_is_absolute(exe)) {
+                log_error("Binary is not an absolute path.");
+                return -ENOENT;
+        }
+
         r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
-        if (r < 0) {
+        if (r == -ENOENT) {
+
+                r = make_coredump_path(j, &coredump);
+                if (r < 0)
+                        return r;
+
+                if (access(coredump, R_OK) < 0) {
+                        if (errno == ENOENT)
+                                log_error("Coredump neither in journal file nor stored externally on disk.");
+                        else
+                                log_error("Failed to access coredump fiile: %s", strerror(-r));
+
+                        return -errno;
+                }
+
+                path = coredump;
+
+        } else if (r < 0) {
                 log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
                 return r;
-        }
 
-        assert(len >= 9);
-        data = (const uint8_t*) data + 9;
-        len -= 9;
+        } else {
+                _cleanup_close_ int fd = -1;
+                ssize_t sz;
 
-        fd = mkostemp_safe(path, O_WRONLY|O_CLOEXEC);
-        if (fd < 0) {
-                log_error("Failed to create temporary file: %m");
-                return -errno;
-        }
+                assert(len >= 9);
+                data = (const uint8_t*) data + 9;
+                len -= 9;
 
-        sz = write(fd, data, len);
-        if (sz < 0) {
-                log_error("Failed to write temporary file: %m");
-                r = -errno;
-                goto finish;
-        }
-        if (sz != (ssize_t) len) {
-                log_error("Short write to temporary file.");
-                r = -EIO;
-                goto finish;
-        }
+                fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
+                if (fd < 0) {
+                        log_error("Failed to create temporary file: %m");
+                        return -errno;
+                }
+
+                unlink_temp = true;
+
+                sz = write(fd, data, len);
+                if (sz < 0) {
+                        log_error("Failed to write temporary file: %m");
+                        r = -errno;
+                        goto finish;
+                }
+                if (sz != (ssize_t) len) {
+                        log_error("Short write to temporary file.");
+                        r = -EIO;
+                        goto finish;
+                }
 
-        fd = safe_close(fd);
+                path = temp;
+        }
 
         pid = fork();
         if (pid < 0) {
@@ -500,6 +606,7 @@ static int run_gdb(sd_journal *j) {
         }
         if (pid == 0) {
                 execlp("gdb", "gdb", exe, path, NULL);
+
                 log_error("Failed to invoke gdb: %m");
                 _exit(1);
         }
@@ -513,7 +620,9 @@ static int run_gdb(sd_journal *j) {
         r = st.si_code == CLD_EXITED ? st.si_status : 255;
 
 finish:
-        unlink(path);
+        if (unlink_temp)
+                unlink(temp);
+
         return r;
 }
 

commit a45e6ef38fef30586b1c02cfe1696f3932a5566f
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Jun 18 22:06:25 2014 +0200

    tmpfiles: automatically clean up /var/lib/systemd/coredump after 3d

diff --git a/tmpfiles.d/systemd.conf b/tmpfiles.d/systemd.conf
index fbc4782..8cba5ca 100644
--- a/tmpfiles.d/systemd.conf
+++ b/tmpfiles.d/systemd.conf
@@ -25,3 +25,6 @@ Z /run/log/journal/%m ~2750 root systemd-journal - -
 
 z /var/log/journal 2755 root systemd-journal - -
 z /var/log/journal/%m 2755 root systemd-journal - -
+
+d /var/lib/systemd 0755 root root -
+d /var/lib/systemd/coredump 0755 root root 3d

commit 34c10968cbe3b5591b3c0ce225b8694edd9709d0
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Jun 18 22:02:18 2014 +0200

    coredump: optionally store coredumps on disk, not in the journal
    
    Introduce a new configuration file /etc/systemd/coredump.conf to
    configure when to place coredumps in the journal and when on disk.
    
    Since the coredumps are quite large, default to storing them only on
    disk.

diff --git a/Makefile.am b/Makefile.am
index 82145c6..7c20e33 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3689,6 +3689,14 @@ systemd_coredump_LDADD = \
 rootlibexec_PROGRAMS += \
 	systemd-coredump
 
+dist_pkgsysconf_DATA += \
+	src/journal/coredump.conf
+
+if HAVE_ACL
+systemd_coredump_LDADD += \
+	libsystemd-acl.la
+endif
+
 systemd_coredumpctl_SOURCES = \
 	src/journal/coredumpctl.c
 
diff --git a/src/journal/coredump.c b/src/journal/coredump.c
index 74b1dbd..a396491 100644
--- a/src/journal/coredump.c
+++ b/src/journal/coredump.c
@@ -23,12 +23,11 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <sys/prctl.h>
+#include <sys/types.h>
+#include <attr/xattr.h>
 
 #include <systemd/sd-journal.h>
-
-#ifdef HAVE_LOGIND
 #include <systemd/sd-login.h>
-#endif
 
 #include "log.h"
 #include "util.h"
@@ -37,13 +36,28 @@
 #include "special.h"
 #include "cgroup-util.h"
 #include "journald-native.h"
+#include "conf-parser.h"
+#include "copy.h"
+
+#ifdef HAVE_ACL
+#include <sys/acl.h>
+#include "acl-util.h"
+#endif
+
+/* The maximum size up to which we process coredumps */
+#define PROCESS_SIZE_MAX ((off_t) (2LLU*1024LLU*1024LLU*1024LLU))
+
+/* The maximum size up to which we leave the coredump around on
+ * disk */
+#define EXTERNAL_SIZE_MAX PROCESS_SIZE_MAX
+
+/* The maximum size up to which we store the coredump in the
+ * journal */
+#define JOURNAL_SIZE_MAX ((size_t) (767LU*1024LU*1024LU))
 
-/* Few programs have less than 3MiB resident */
-#define COREDUMP_MIN_START (3*1024*1024u)
 /* Make sure to not make this larger than the maximum journal entry
  * size. See ENTRY_SIZE_MAX in journald-native.c. */
-#define COREDUMP_MAX (767*1024*1024u)
-assert_cc(COREDUMP_MAX <= ENTRY_SIZE_MAX);
+assert_cc(JOURNAL_SIZE_MAX <= ENTRY_SIZE_MAX);
 
 enum {
         ARG_PID = 1,
@@ -55,44 +69,272 @@ enum {
         _ARG_MAX
 };
 
-static int divert_coredump(void) {
-        _cleanup_fclose_ FILE *f = NULL;
+typedef enum CoredumpStorage {
+        COREDUMP_STORAGE_NONE,
+        COREDUMP_STORAGE_EXTERNAL,
+        COREDUMP_STORAGE_JOURNAL,
+        COREDUMP_STORAGE_BOTH,
+        _COREDUMP_STORAGE_MAX,
+        _COREDUMP_STORAGE_INVALID = -1
+} CoredumpStorage;
+
+static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
+static off_t arg_process_size_max = PROCESS_SIZE_MAX;
+static off_t arg_external_size_max = EXTERNAL_SIZE_MAX;
+static size_t arg_journal_size_max = JOURNAL_SIZE_MAX;
+
+static const char* const coredump_storage_table[_COREDUMP_STORAGE_MAX] = {
+        [COREDUMP_STORAGE_NONE] = "none",
+        [COREDUMP_STORAGE_EXTERNAL] = "external",
+        [COREDUMP_STORAGE_JOURNAL] = "journal",
+        [COREDUMP_STORAGE_BOTH] = "both",
+};
+
+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage);
+static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
+
+static int parse_config(void) {
+
+        static const ConfigTableItem items[] = {
+                { "Coredump", "ProcessSizeMax",  config_parse_iec_off,           0, &arg_process_size_max  },
+                { "Coredump", "ExternalSizeMax", config_parse_iec_off,           0, &arg_external_size_max },
+                { "Coredump", "JournalSizeMax",  config_parse_iec_size,          0, &arg_journal_size_max  },
+                { "Coredump", "Storage",         config_parse_coredump_storage,  0, &arg_storage           },
+                {}
+        };
+
+        return config_parse(
+                        NULL,
+                        "/etc/systemd/coredump.conf",
+                        NULL,
+                        "Coredump\0",
+                        config_item_table_lookup,
+                        (void*) items,
+                        false,
+                        false,
+                        NULL);
+}
+
+static int fix_acl(int fd, uid_t uid) {
+
+#ifdef HAVE_ACL
+        _cleanup_(acl_freep) acl_t acl = NULL;
+        acl_entry_t entry;
+        acl_permset_t permset;
+
+        if (uid <= SYSTEM_UID_MAX)
+                return 0;
+
+        /* Make sure normal users can read (but not write or delete)
+         * their own coredumps */
+
+        acl = acl_get_fd(fd);
+        if (!acl) {
+                log_error("Failed to get ACL: %m");
+                return -errno;
+        }
+
+        if (acl_create_entry(&acl, &entry) < 0 ||
+            acl_set_tag_type(entry, ACL_USER) < 0 ||
+            acl_set_qualifier(entry, &uid) < 0) {
+                log_error("Failed to patch ACL: %m");
+                return -errno;
+        }
+
+        if (acl_get_permset(entry, &permset) < 0 ||
+            acl_add_perm(permset, ACL_READ) < 0 ||
+            calc_acl_mask_if_needed(&acl) < 0) {
+                log_warning("Failed to patch ACL: %m");
+                return -errno;
+        }
+
+        if (acl_set_fd(fd, acl) < 0) {
+                log_error("Failed to apply ACL: %m");
+                return -errno;
+        }
+#endif
+
+        return 0;
+}
+
+static int fix_xattr(int fd, char *argv[]) {
+        int r = 0;
+
+        /* Attach some metadate to coredumps via extended
+         * attributes. Just because we can. */
+
+        if (!isempty(argv[ARG_PID]))
+                if (fsetxattr(fd, "user.coredump.pid", argv[ARG_PID], strlen(argv[ARG_PID]), XATTR_CREATE) < 0)
+                        r = -errno;
+
+        if (!isempty(argv[ARG_UID]))
+                if (fsetxattr(fd, "user.coredump.uid", argv[ARG_UID], strlen(argv[ARG_UID]), XATTR_CREATE) < 0)
+                        r = -errno;
+
+        if (!isempty(argv[ARG_GID]))
+                if (fsetxattr(fd, "user.coredump.gid", argv[ARG_GID], strlen(argv[ARG_GID]), XATTR_CREATE) < 0)
+                        r = -errno;
+
+        if (!isempty(argv[ARG_SIGNAL]))
+                if (fsetxattr(fd, "user.coredump.signal", argv[ARG_SIGNAL], strlen(argv[ARG_SIGNAL]), XATTR_CREATE) < 0)
+                        r = -errno;
+
+        if (!isempty(argv[ARG_TIMESTAMP]))
+                if (fsetxattr(fd, "user.coredump.timestamp", argv[ARG_TIMESTAMP], strlen(argv[ARG_TIMESTAMP]), XATTR_CREATE) < 0)
+                        r = -errno;
 
-        log_info("Detected coredump of the journal daemon itself, diverting coredump to /var/lib/systemd/coredump/.");
+        if (!isempty(argv[ARG_COMM]))
+                if (fsetxattr(fd, "user.coredump.comm", argv[ARG_COMM], strlen(argv[ARG_COMM]), XATTR_CREATE) < 0)
+                        r = -errno;
+
+        return r;
+}
+
+#define filename_escape(s) xescape((s), "./")
+
+static int save_external_coredump(char **argv, uid_t uid, char **ret_filename, int *ret_fd, off_t *ret_size) {
+        _cleanup_free_ char *p = NULL, *t = NULL, *c = NULL, *fn = NULL, *tmp = NULL;
+        _cleanup_close_ int fd = -1;
+        sd_id128_t boot;
+        struct stat st;
+        int r;
+
+        assert(argv);
+        assert(ret_filename);
+        assert(ret_fd);
+        assert(ret_size);
+
+        c = filename_escape(argv[ARG_COMM]);
+        if (!c)
+                return log_oom();
+
+        p = filename_escape(argv[ARG_PID]);
+        if (!p)
+                return log_oom();
+
+        t = filename_escape(argv[ARG_TIMESTAMP]);
+        if (!t)
+                return log_oom();
+
+        r = sd_id128_get_boot(&boot);
+        if (r < 0) {
+                log_error("Failed to determine boot ID: %s", strerror(-r));
+                return r;
+        }
+
+        r = asprintf(&fn,
+                     "/var/lib/systemd/coredump/core.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
+                     c,
+                     SD_ID128_FORMAT_VAL(boot),
+                     p,
+                     t);
+        if (r < 0)
+                return log_oom();
+
+        tmp = tempfn_random(fn);
+        if (!tmp)
+                return log_oom();
 
         mkdir_p_label("/var/lib/systemd/coredump", 0755);
 
-        f = fopen("/var/lib/systemd/coredump/core.systemd-journald", "we");
-        if (!f) {
+        fd = open(tmp, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
+        if (fd < 0) {
                 log_error("Failed to create coredump file: %m");
                 return -errno;
         }
 
-        for (;;) {
-                uint8_t buffer[4096];
-                size_t l, q;
+        r = copy_bytes(STDIN_FILENO, fd);
+        if (r < 0) {
+                log_error("Failed to dump coredump to file: %s", strerror(-r));
+                goto fail;
+        }
 
-                l = fread(buffer, 1, sizeof(buffer), stdin);
-                if (l <= 0) {
-                        if (ferror(f)) {
-                                log_error("Failed to read coredump: %m");
-                                return -errno;
-                        }
+        /* Ignore errors on these */
+        fchmod(fd, 0640);
+        fix_acl(fd, uid);
+        fix_xattr(fd, argv);
 
-                        break;
-                }
+        if (fsync(fd) < 0) {
+                log_error("Failed to sync coredump: %m");
+                r = -errno;
+                goto fail;
+        }
 
-                q = fwrite(buffer, 1, l, f);
-                if (q != l) {
-                        log_error("Failed to write coredump: %m");
-                        return -errno;
-                }
+        if (fstat(fd, &st) < 0) {
+                log_error("Failed to fstat coredump: %m");
+                r = -errno;
+                goto fail;
+        }
+
+        if (rename(tmp, fn) < 0) {
+                log_error("Failed to rename coredump: %m");
+                r = -errno;
+                goto fail;
+        }
+
+        *ret_filename = fn;
+        *ret_fd = fd;
+        *ret_size = st.st_size;
+
+        fn = NULL;
+        fd = -1;
+
+        return 0;
+
+fail:
+        unlink_noerrno(tmp);
+        return r;
+}
+
+static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
+        _cleanup_free_ char *field = NULL;
+        ssize_t n;
+
+        assert(ret);
+        assert(ret_size);
+
+        if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
+                log_warning("Failed to seek: %m");
+                return -errno;
         }
 
-        fflush(f);
+        field = malloc(9 + size);
+        if (!field) {
+                log_warning("Failed to allocate memory fore coredump, coredump will not be stored.");
+                return -ENOMEM;
+        }
+
+        memcpy(field, "COREDUMP=", 9);
+
+        n = read(fd, field + 9, size);
+        if (n < 0) {
+                log_error("Failed to read core data: %s", strerror(-n));
+                return (int) n;
+        }
+        if ((size_t) n < size) {
+                log_error("Core data too short.");
+                return -EIO;
+        }
+
+        *ret = field;
+        *ret_size = size + 9;
+
+        field = NULL;
+
+        return 0;
+}
 
-        if (ferror(f)) {
-                log_error("Failed to write coredump: %m");
+static int maybe_remove_external_coredump(const char *filename, off_t size) {
+
+        if (!filename)
+                return 0;
+
+        if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) &&
+            size <= arg_external_size_max)
+                return 0;
+
+        if (unlink(filename) < 0) {
+                log_error("Failed to unlink %s: %m", filename);
                 return -errno;
         }
 
@@ -100,47 +342,80 @@ static int divert_coredump(void) {
 }
 
 int main(int argc, char* argv[]) {
+
+        _cleanup_free_ char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
+                *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
+                *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *coredump_data = NULL,
+                *coredump_filename = NULL;
+
+        _cleanup_close_ int coredump_fd = -1;
+
+        struct iovec iovec[14];
+        off_t coredump_size;
         int r, j = 0;
-        char *t;
-        ssize_t n;
         pid_t pid;
         uid_t uid;
         gid_t gid;
-        struct iovec iovec[14];
-        size_t coredump_bufsize = 0, coredump_size = 0;
-        _cleanup_free_ char *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
-                *core_timestamp = NULL, *core_comm = NULL, *core_exe = NULL, *core_unit = NULL,
-                *core_session = NULL, *core_message = NULL, *core_cmdline = NULL, *coredump_data = NULL;
+        char *t;
 
+        /* Make sure we never enter a loop */
         prctl(PR_SET_DUMPABLE, 0);
 
-        if (argc != _ARG_MAX) {
-                log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
-                log_open();
+        /* First, log to a safe place, since we don't know what
+         * crashed and it might be journald which we'd rather not log
+         * to then. */
+        log_set_target(LOG_TARGET_KMSG);
+        log_set_max_level(LOG_DEBUG);
+        log_open();
 
+        if (argc != _ARG_MAX) {
                 log_error("Invalid number of arguments passed from kernel.");
                 r = -EINVAL;
                 goto finish;
         }
 
-        r = parse_pid(argv[ARG_PID], &pid);
+        /* Ignore all parse errors */
+        parse_config();
+        log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
+
+        r = parse_uid(argv[ARG_UID], &uid);
         if (r < 0) {
-                log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
-                log_open();
+                log_error("Failed to parse UID.");
+                goto finish;
+        }
 
+        r = parse_pid(argv[ARG_PID], &pid);
+        if (r < 0) {
                 log_error("Failed to parse PID.");
                 goto finish;
         }
 
+        r = parse_gid(argv[ARG_GID], &gid);
+        if (r < 0) {
+                log_error("Failed to parse GID.");
+                goto finish;
+        }
+
         if (cg_pid_get_unit(pid, &t) >= 0) {
 
                 if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
-                        /* Make sure we don't make use of the journal,
-                         * if it's the journal which is crashing */
-                        log_set_target(LOG_TARGET_KMSG);
-                        log_open();
 
-                        r = divert_coredump();
+                        /* If we are journald, we cut things short,
+                         * don't write to the journal, but still
+                         * create a coredump. */
+
+                        if (arg_storage != COREDUMP_STORAGE_NONE)
+                                arg_storage = COREDUMP_STORAGE_EXTERNAL;
+
+                        r = save_external_coredump(argv, uid, &coredump_filename, &coredump_fd, &coredump_size);
+                        if (r < 0)
+                                goto finish;
+
+                        r = maybe_remove_external_coredump(coredump_filename, coredump_size);
+                        if (r < 0)
+                                goto finish;
+
+                        log_info("Detected coredump of the journal daemon itself, diverted to %s.", coredump_filename);
                         goto finish;
                 }
 
@@ -151,23 +426,11 @@ int main(int argc, char* argv[]) {
         if (core_unit)
                 IOVEC_SET_STRING(iovec[j++], core_unit);
 
-        /* OK, now we know it's not the journal, hence make use of
-         * it */
+        /* 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();
 
-        r = parse_uid(argv[ARG_UID], &uid);
-        if (r < 0) {
-                log_error("Failed to parse UID.");
-                goto finish;
-        }
-
-        r = parse_gid(argv[ARG_GID], &gid);
-        if (r < 0) {
-                log_error("Failed to parse GID.");
-                goto finish;
-        }
-
         core_pid = strappend("COREDUMP_PID=", argv[ARG_PID]);
         if (core_pid)
                 IOVEC_SET_STRING(iovec[j++], core_pid);
@@ -188,7 +451,6 @@ int main(int argc, char* argv[]) {
         if (core_comm)
                 IOVEC_SET_STRING(iovec[j++], core_comm);
 
-#ifdef HAVE_LOGIND
         if (sd_pid_get_session(pid, &t) >= 0) {
                 core_session = strappend("COREDUMP_SESSION=", t);
                 free(t);
@@ -197,8 +459,6 @@ int main(int argc, char* argv[]) {
                         IOVEC_SET_STRING(iovec[j++], core_session);
         }
 
-#endif
-
         if (get_process_exe(pid, &t) >= 0) {
                 core_exe = strappend("COREDUMP_EXE=", t);
                 free(t);
@@ -226,12 +486,22 @@ int main(int argc, char* argv[]) {
         if (core_message)
                 IOVEC_SET_STRING(iovec[j++], core_message);
 
+        /* Always stream the coredump to disk, if that's possible */
+        r = save_external_coredump(argv, uid, &coredump_filename, &coredump_fd, &coredump_size);
+        if (r < 0)
+                goto finish;
+
+        /* If we don't want to keep the coredump on disk, remove it
+         * now, as later on we will lack the privileges for it. */
+        r = maybe_remove_external_coredump(coredump_filename, coredump_size);
+        if (r < 0)
+                goto finish;
+
         /* Now, let's drop privileges to become the user who owns the
          * segfaulted process and allocate the coredump memory under
          * his uid. This also ensures that the credentials journald
          * will see are the ones of the coredumping user, thus making
          * sure the user himself gets access to the core dump. */
-
         if (setresgid(gid, gid, gid) < 0 ||
             setresuid(uid, uid, uid) < 0) {
                 log_error("Failed to drop privileges: %m");
@@ -239,40 +509,21 @@ int main(int argc, char* argv[]) {
                 goto finish;
         }
 
-        for (;;) {
-                if (!GREEDY_REALLOC(coredump_data, coredump_bufsize,
-                                    MAX(coredump_size + 1, COREDUMP_MIN_START/2))) {
-                        log_warning("Failed to allocate memory for core, core will not be stored.");
-                        goto finalize;
-                }
+        /* Optionally store the entire coredump in the journal */
+        if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) &&
+            coredump_size <= (off_t) arg_journal_size_max) {
+                size_t sz;
 
-                if (coredump_size == 0) {
-                        memcpy(coredump_data, "COREDUMP=", 9);
-                        coredump_size = 9;
-                }
+                /* Store the coredump itself in the journal */
 
-                n = loop_read(STDIN_FILENO, coredump_data + coredump_size,
-                              coredump_bufsize - coredump_size, false);
-                if (n < 0) {
-                        log_error("Failed to read core data: %s", strerror(-n));
-                        r = (int) n;
-                        goto finish;
-                } else if (n == 0)
-                        break;
-
-                coredump_size += n;
-
-                if (coredump_size > COREDUMP_MAX) {
-                        log_error("Core too large, core will not be stored.");
-                        goto finalize;
+                r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
+                if (r >= 0) {
+                        iovec[j].iov_base = coredump_data;
+                        iovec[j].iov_len = sz;
+                        j++;
                 }
         }
 
-        iovec[j].iov_base = coredump_data;
-        iovec[j].iov_len = coredump_size;
-        j++;
-
-finalize:
         r = sd_journal_sendv(iovec, j);
         if (r < 0)
                 log_error("Failed to log coredump: %s", strerror(-r));
diff --git a/src/journal/coredump.conf b/src/journal/coredump.conf
new file mode 100644
index 0000000..53e471e
--- /dev/null
+++ b/src/journal/coredump.conf
@@ -0,0 +1,14 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+#
+# See coredump.conf(5) for details
+
+[Coredump]
+#Storage=external
+#ProcessSizeMax=2G
+#ExternalSizeMax=2G
+#JournalSizeMax=767M
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index eda5dcf..39a1a07 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -205,7 +205,7 @@ void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
                 log_warning("Failed to fix access mode on %s, ignoring: %s", f->path, strerror(-r));
 
 #ifdef HAVE_ACL
-        if (uid <= 0)
+        if (uid <= SYSTEM_UID_MAX)
                 return;
 
         acl = acl_get_fd(f->fd);
diff --git a/src/shared/acl-util.h b/src/shared/acl-util.h
index 36ef490..a753ad1 100644
--- a/src/shared/acl-util.h
+++ b/src/shared/acl-util.h
@@ -26,3 +26,11 @@
 int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry);
 int calc_acl_mask_if_needed(acl_t *acl_p);
 int search_acl_groups(char*** dst, const char* path, bool* belong);
+
+static inline void acl_freep(acl_t *acl) {
+
+        if (!*acl)
+                return;
+
+        acl_free(*acl);
+}
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c
index cf1cd40..1972cdb 100644
--- a/src/shared/sleep-config.c
+++ b/src/shared/sleep-config.c
@@ -31,6 +31,7 @@
 #define USE(x, y) do{ (x) = (y); (y) = NULL; } while(0)
 
 int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
+
         _cleanup_strv_free_ char
                 **suspend_mode = NULL, **suspend_state = NULL,
                 **hibernate_mode = NULL, **hibernate_state = NULL,
@@ -44,7 +45,8 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
                 { "Sleep",   "HibernateState",   config_parse_strv,  0, &hibernate_state },
                 { "Sleep",   "HybridSleepMode",  config_parse_strv,  0, &hybrid_mode  },
                 { "Sleep",   "HybridSleepState", config_parse_strv,  0, &hybrid_state },
-                {}};
+                {}
+        };
 
         int r;
         FILE _cleanup_fclose_ *f;
diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c
index 94bcb29..118d10c 100644
--- a/src/sleep/sleep.c
+++ b/src/sleep/sleep.c
@@ -135,6 +135,7 @@ static int execute(char **modes, char **states) {
 }
 
 static int help(void) {
+
         printf("%s COMMAND\n\n"
                "Suspend the system, hibernate the system, or both.\n\n"
                "Commands:\n"

commit 3bdace9bf779ce051f00c14914b35c3a26164aa9
Author: Lennart Poettering <lennart at poettering.net>
Date:   Wed Jun 18 18:54:52 2014 +0200

    dhcp-server: simplify dhcp server unref call
    
    No need to use HASHMAP_ITERATE when we destruct all entries anyway.

diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c
index d949502..5bdcf86 100644
--- a/src/libsystemd-network/sd-dhcp-server.c
+++ b/src/libsystemd-network/sd-dhcp-server.c
@@ -109,25 +109,26 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(DHCPLease*, dhcp_lease_free);
 #define _cleanup_dhcp_lease_free_ _cleanup_(dhcp_lease_freep)
 
 sd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server) {
-        if (server && REFCNT_DEC(server->n_ref) <= 0) {
-                DHCPLease *lease;
-                Iterator i;
+        DHCPLease *lease;
 
-                log_dhcp_server(server, "UNREF");
+        if (!server)
+                return NULL;
 
-                sd_dhcp_server_stop(server);
+        if (REFCNT_DEC(server->n_ref) > 0)
+                return NULL;
 
-                sd_event_unref(server->event);
+        log_dhcp_server(server, "UNREF");
 
-                HASHMAP_FOREACH(lease, server->leases_by_client_id, i) {
-                        hashmap_remove(server->leases_by_client_id, lease);
-                        dhcp_lease_free(lease);
-                }
+        sd_dhcp_server_stop(server);
 
-                hashmap_free(server->leases_by_client_id);
-                free(server->bound_leases);
-                free(server);
-        }
+        sd_event_unref(server->event);
+
+        while ((lease = hashmap_steal_first(server->leases_by_client_id)))
+                dhcp_lease_free(lease);
+        hashmap_free(server->leases_by_client_id);
+
+        free(server->bound_leases);
+        free(server);
 
         return NULL;
 }



More information about the systemd-commits mailing list