summaryrefslogtreecommitdiff
path: root/src/journal
diff options
context:
space:
mode:
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/audit-type.c2
-rw-r--r--src/journal/audit-type.h2
-rw-r--r--src/journal/cat.c12
-rw-r--r--src/journal/catalog.c148
-rw-r--r--src/journal/catalog.h4
-rw-r--r--src/journal/compress.c2
-rw-r--r--src/journal/compress.h2
-rw-r--r--src/journal/coredump-vacuum.c270
-rw-r--r--src/journal/coredump-vacuum.h27
-rw-r--r--src/journal/coredump.c882
-rw-r--r--src/journal/coredump.conf21
-rw-r--r--src/journal/coredumpctl.c882
-rw-r--r--src/journal/fsprg.c2
-rw-r--r--src/journal/fsprg.h2
-rw-r--r--src/journal/journal-authenticate.c2
-rw-r--r--src/journal/journal-authenticate.h2
-rw-r--r--src/journal/journal-def.h2
-rw-r--r--src/journal/journal-file.c38
-rw-r--r--src/journal/journal-file.h2
-rw-r--r--src/journal/journal-internal.h23
-rw-r--r--src/journal/journal-qrcode.c2
-rw-r--r--src/journal/journal-qrcode.h2
-rw-r--r--src/journal/journal-send.c2
-rw-r--r--src/journal/journal-vacuum.c2
-rw-r--r--src/journal/journal-vacuum.h2
-rw-r--r--src/journal/journal-verify.c2
-rw-r--r--src/journal/journal-verify.h2
-rw-r--r--src/journal/journalctl.c150
-rw-r--r--src/journal/journald-audit.c2
-rw-r--r--src/journal/journald-audit.h2
-rw-r--r--src/journal/journald-console.c2
-rw-r--r--src/journal/journald-console.h2
-rw-r--r--src/journal/journald-kmsg.c2
-rw-r--r--src/journal/journald-kmsg.h2
-rw-r--r--src/journal/journald-native.c2
-rw-r--r--src/journal/journald-native.h2
-rw-r--r--src/journal/journald-rate-limit.c2
-rw-r--r--src/journal/journald-rate-limit.h2
-rw-r--r--src/journal/journald-server.c15
-rw-r--r--src/journal/journald-server.h2
-rw-r--r--src/journal/journald-stream.c2
-rw-r--r--src/journal/journald-stream.h2
-rw-r--r--src/journal/journald-syslog.c2
-rw-r--r--src/journal/journald-syslog.h2
-rw-r--r--src/journal/journald-wall.c2
-rw-r--r--src/journal/journald-wall.h2
-rw-r--r--src/journal/journald.c2
-rw-r--r--src/journal/lookup3.h2
-rw-r--r--src/journal/mmap-cache.c79
-rw-r--r--src/journal/mmap-cache.h2
-rw-r--r--src/journal/sd-journal.c174
-rw-r--r--src/journal/stacktrace.c202
-rw-r--r--src/journal/stacktrace.h24
-rw-r--r--src/journal/test-audit-type.c2
-rw-r--r--src/journal/test-catalog.c132
-rw-r--r--src/journal/test-compress-benchmark.c2
-rw-r--r--src/journal/test-coredump-vacuum.c32
-rw-r--r--src/journal/test-journal-enum.c2
-rw-r--r--src/journal/test-journal-flush.c2
-rw-r--r--src/journal/test-journal-init.c2
-rw-r--r--src/journal/test-journal-interleaving.c2
-rw-r--r--src/journal/test-journal-match.c2
-rw-r--r--src/journal/test-journal-send.c2
-rw-r--r--src/journal/test-journal-stream.c2
-rw-r--r--src/journal/test-journal-syslog.c2
-rw-r--r--src/journal/test-journal-verify.c2
-rw-r--r--src/journal/test-journal.c2
-rw-r--r--src/journal/test-mmap-cache.c2
68 files changed, 607 insertions, 2608 deletions
diff --git a/src/journal/audit-type.c b/src/journal/audit-type.c
index 086bf7e7e3..71e8790ca8 100644
--- a/src/journal/audit-type.c
+++ b/src/journal/audit-type.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/audit-type.h b/src/journal/audit-type.h
index fa5284e027..1dd2163707 100644
--- a/src/journal/audit-type.h
+++ b/src/journal/audit-type.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/cat.c b/src/journal/cat.c
index 7fd4198df8..08c844d44f 100644
--- a/src/journal/cat.c
+++ b/src/journal/cat.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -34,7 +32,7 @@
#include "syslog-util.h"
#include "util.h"
-static char *arg_identifier = NULL;
+static const char *arg_identifier = NULL;
static int arg_priority = LOG_INFO;
static bool arg_level_prefix = true;
@@ -82,14 +80,10 @@ static int parse_argv(int argc, char *argv[]) {
return version();
case 't':
- free(arg_identifier);
if (isempty(optarg))
arg_identifier = NULL;
- else {
- arg_identifier = strdup(optarg);
- if (!arg_identifier)
- return log_oom();
- }
+ else
+ arg_identifier = optarg;
break;
case 'p':
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
index fcaa54aa0c..164a3a15f2 100644
--- a/src/journal/catalog.c
+++ b/src/journal/catalog.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -94,25 +92,87 @@ const struct hash_ops catalog_hash_ops = {
.compare = catalog_compare_func
};
+static bool next_header(const char **s) {
+ const char *e;
+
+ e = strchr(*s, '\n');
+
+ /* Unexpected end */
+ if (!e)
+ return false;
+
+ /* End of headers */
+ if (e == *s)
+ return false;
+
+ *s = e + 1;
+ return true;
+}
+
+static const char *skip_header(const char *s) {
+ while (next_header(&s))
+ ;
+ return s;
+}
+
+static char *combine_entries(const char *one, const char *two) {
+ const char *b1, *b2;
+ size_t l1, l2, n;
+ char *dest, *p;
+
+ /* Find split point of headers to body */
+ b1 = skip_header(one);
+ b2 = skip_header(two);
+
+ l1 = strlen(one);
+ l2 = strlen(two);
+ dest = new(char, l1 + l2 + 1);
+ if (!dest) {
+ log_oom();
+ return NULL;
+ }
+
+ p = dest;
+
+ /* Headers from @one */
+ n = b1 - one;
+ p = mempcpy(p, one, n);
+
+ /* Headers from @two, these will only be found if not present above */
+ n = b2 - two;
+ p = mempcpy(p, two, n);
+
+ /* Body from @one */
+ n = l1 - (b1 - one);
+ if (n > 0) {
+ memcpy(p, b1, n);
+ p += n;
+
+ /* Body from @two */
+ } else {
+ n = l2 - (b2 - two);
+ memcpy(p, b2, n);
+ p += n;
+ }
+
+ assert(p - dest <= (ptrdiff_t)(l1 + l2));
+ p[0] = '\0';
+ return dest;
+}
+
static int finish_item(
Hashmap *h,
- struct strbuf *sb,
sd_id128_t id,
const char *language,
- const char *payload) {
+ char *payload) {
- ssize_t offset;
_cleanup_free_ CatalogItem *i = NULL;
+ _cleanup_free_ char *combined = NULL, *prev = NULL;
int r;
assert(h);
- assert(sb);
assert(payload);
- offset = strbuf_add_string(sb, payload, strlen(payload));
- if (offset < 0)
- return log_oom();
-
i = new0(CatalogItem, 1);
if (!i)
return log_oom();
@@ -122,17 +182,27 @@ static int finish_item(
assert(strlen(language) > 1 && strlen(language) < 32);
strcpy(i->language, language);
}
- i->offset = htole64((uint64_t) offset);
- r = hashmap_put(h, i, i);
- if (r == -EEXIST) {
- log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.",
- SD_ID128_FORMAT_VAL(id), language ? language : "C");
- return 0;
- } else if (r < 0)
- return r;
+ prev = hashmap_get(h, i);
+
+ /* Already have such an item, combine them */
+ if (prev) {
+ combined = combine_entries(payload, prev);
+ if (!combined)
+ return log_oom();
+ r = hashmap_update(h, i, combined);
+ if (r < 0)
+ return r;
+ combined = NULL;
+
+ /* A new item */
+ } else {
+ r = hashmap_put(h, i, payload);
+ if (r < 0)
+ return r;
+ i = NULL;
+ }
- i = NULL;
return 0;
}
@@ -189,7 +259,7 @@ static int catalog_entry_lang(const char* filename, int line,
return 0;
}
-int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
+int catalog_import_file(Hashmap *h, const char *path) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *payload = NULL;
unsigned n = 0;
@@ -199,7 +269,6 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
int r;
assert(h);
- assert(sb);
assert(path);
f = fopen(path, "re");
@@ -254,10 +323,11 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
if (got_id) {
- r = finish_item(h, sb, id, lang ?: deflang, payload);
+ r = finish_item(h, id, lang ?: deflang, payload);
if (r < 0)
return r;
+ payload = NULL;
lang = mfree(lang);
}
@@ -310,9 +380,10 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
}
if (got_id) {
- r = finish_item(h, sb, id, lang ?: deflang, payload);
+ r = finish_item(h, id, lang ?: deflang, payload);
if (r < 0)
return r;
+ payload = NULL;
}
return 0;
@@ -389,8 +460,10 @@ int catalog_update(const char* database, const char* root, const char* const* di
_cleanup_strv_free_ char **files = NULL;
char **f;
struct strbuf *sb = NULL;
- _cleanup_hashmap_free_free_ Hashmap *h = NULL;
+ _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
_cleanup_free_ CatalogItem *items = NULL;
+ ssize_t offset;
+ char *payload;
CatalogItem *i;
Iterator j;
unsigned n;
@@ -413,7 +486,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
STRV_FOREACH(f, files) {
log_debug("Reading file '%s'", *f);
- r = catalog_import_file(h, sb, *f);
+ r = catalog_import_file(h, *f);
if (r < 0) {
log_error_errno(r, "Failed to import file '%s': %m", *f);
goto finish;
@@ -426,8 +499,6 @@ int catalog_update(const char* database, const char* root, const char* const* di
} else
log_debug("Found %u items in catalog.", hashmap_size(h));
- strbuf_complete(sb);
-
items = new(CatalogItem, hashmap_size(h));
if (!items) {
r = log_oom();
@@ -435,16 +506,25 @@ int catalog_update(const char* database, const char* root, const char* const* di
}
n = 0;
- HASHMAP_FOREACH(i, h, j) {
+ HASHMAP_FOREACH_KEY(payload, i, h, j) {
log_debug("Found " SD_ID128_FORMAT_STR ", language %s",
SD_ID128_FORMAT_VAL(i->id),
isempty(i->language) ? "C" : i->language);
+
+ offset = strbuf_add_string(sb, payload, strlen(payload));
+ if (offset < 0) {
+ r = log_oom();
+ goto finish;
+ }
+ i->offset = htole64((uint64_t) offset);
items[n++] = *i;
}
assert(n == hashmap_size(h));
qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
+ strbuf_complete(sb);
+
sz = write_catalog(database, sb, items, n);
if (sz < 0)
r = log_error_errno(sz, "Failed to write %s: %m", database);
@@ -587,7 +667,7 @@ finish:
static char *find_header(const char *s, const char *header) {
for (;;) {
- const char *v, *e;
+ const char *v;
v = startswith(s, header);
if (v) {
@@ -595,16 +675,8 @@ static char *find_header(const char *s, const char *header) {
return strndup(v, strcspn(v, NEWLINE));
}
- /* End of text */
- e = strchr(s, '\n');
- if (!e)
+ if (!next_header(&s))
return NULL;
-
- /* End of header */
- if (e == s)
- return NULL;
-
- s = e + 1;
}
}
diff --git a/src/journal/catalog.h b/src/journal/catalog.h
index bcc73c2631..1b1014b335 100644
--- a/src/journal/catalog.h
+++ b/src/journal/catalog.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
@@ -28,7 +26,7 @@
#include "hashmap.h"
#include "strbuf.h"
-int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path);
+int catalog_import_file(Hashmap *h, const char *path);
int catalog_update(const char* database, const char* root, const char* const* dirs);
int catalog_get(const char* database, sd_id128_t id, char **data);
int catalog_list(FILE *f, const char* database, bool oneline);
diff --git a/src/journal/compress.c b/src/journal/compress.c
index 78935fee74..1933b87b00 100644
--- a/src/journal/compress.c
+++ b/src/journal/compress.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/compress.h b/src/journal/compress.h
index 758598730a..c138099d9a 100644
--- a/src/journal/compress.h
+++ b/src/journal/compress.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/coredump-vacuum.c b/src/journal/coredump-vacuum.c
deleted file mode 100644
index 09ab60c6c4..0000000000
--- a/src/journal/coredump-vacuum.c
+++ /dev/null
@@ -1,270 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <sys/statvfs.h>
-
-#include "alloc-util.h"
-#include "coredump-vacuum.h"
-#include "dirent-util.h"
-#include "fd-util.h"
-#include "hashmap.h"
-#include "macro.h"
-#include "string-util.h"
-#include "time-util.h"
-#include "user-util.h"
-#include "util.h"
-
-#define DEFAULT_MAX_USE_LOWER (uint64_t) (1ULL*1024ULL*1024ULL) /* 1 MiB */
-#define DEFAULT_MAX_USE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
-#define DEFAULT_KEEP_FREE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
-#define DEFAULT_KEEP_FREE (uint64_t) (1024ULL*1024ULL) /* 1 MB */
-
-struct vacuum_candidate {
- unsigned n_files;
- char *oldest_file;
- usec_t oldest_mtime;
-};
-
-static void vacuum_candidate_free(struct vacuum_candidate *c) {
- if (!c)
- return;
-
- free(c->oldest_file);
- free(c);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate*, vacuum_candidate_free);
-
-static void vacuum_candidate_hasmap_free(Hashmap *h) {
- struct vacuum_candidate *c;
-
- while ((c = hashmap_steal_first(h)))
- vacuum_candidate_free(c);
-
- hashmap_free(h);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hasmap_free);
-
-static int uid_from_file_name(const char *filename, uid_t *uid) {
- const char *p, *e, *u;
-
- p = startswith(filename, "core.");
- if (!p)
- return -EINVAL;
-
- /* Skip the comm field */
- p = strchr(p, '.');
- if (!p)
- return -EINVAL;
- p++;
-
- /* Find end up UID */
- e = strchr(p, '.');
- if (!e)
- return -EINVAL;
-
- u = strndupa(p, e-p);
- return parse_uid(u, uid);
-}
-
-static bool vacuum_necessary(int fd, uint64_t sum, uint64_t keep_free, uint64_t max_use) {
- uint64_t fs_size = 0, fs_free = (uint64_t) -1;
- struct statvfs sv;
-
- assert(fd >= 0);
-
- if (fstatvfs(fd, &sv) >= 0) {
- fs_size = sv.f_frsize * sv.f_blocks;
- fs_free = sv.f_frsize * sv.f_bfree;
- }
-
- if (max_use == (uint64_t) -1) {
-
- if (fs_size > 0) {
- max_use = PAGE_ALIGN(fs_size / 10); /* 10% */
-
- if (max_use > DEFAULT_MAX_USE_UPPER)
- max_use = DEFAULT_MAX_USE_UPPER;
-
- if (max_use < DEFAULT_MAX_USE_LOWER)
- max_use = DEFAULT_MAX_USE_LOWER;
- } else
- max_use = DEFAULT_MAX_USE_LOWER;
- } else
- max_use = PAGE_ALIGN(max_use);
-
- if (max_use > 0 && sum > max_use)
- return true;
-
- if (keep_free == (uint64_t) -1) {
-
- if (fs_size > 0) {
- keep_free = PAGE_ALIGN((fs_size * 3) / 20); /* 15% */
-
- if (keep_free > DEFAULT_KEEP_FREE_UPPER)
- keep_free = DEFAULT_KEEP_FREE_UPPER;
- } else
- keep_free = DEFAULT_KEEP_FREE;
- } else
- keep_free = PAGE_ALIGN(keep_free);
-
- if (keep_free > 0 && fs_free < keep_free)
- return true;
-
- return false;
-}
-
-int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use) {
- _cleanup_closedir_ DIR *d = NULL;
- struct stat exclude_st;
- int r;
-
- if (keep_free == 0 && max_use == 0)
- return 0;
-
- if (exclude_fd >= 0) {
- if (fstat(exclude_fd, &exclude_st) < 0)
- return log_error_errno(errno, "Failed to fstat(): %m");
- }
-
- /* This algorithm will keep deleting the oldest file of the
- * user with the most coredumps until we are back in the size
- * limits. Note that vacuuming for journal files is different,
- * because we rely on rate-limiting of the messages there,
- * to avoid being flooded. */
-
- d = opendir("/var/lib/systemd/coredump");
- if (!d) {
- if (errno == ENOENT)
- return 0;
-
- return log_error_errno(errno, "Can't open coredump directory: %m");
- }
-
- for (;;) {
- _cleanup_(vacuum_candidate_hasmap_freep) Hashmap *h = NULL;
- struct vacuum_candidate *worst = NULL;
- struct dirent *de;
- uint64_t sum = 0;
-
- rewinddir(d);
-
- FOREACH_DIRENT(de, d, goto fail) {
- struct vacuum_candidate *c;
- struct stat st;
- uid_t uid;
- usec_t t;
-
- r = uid_from_file_name(de->d_name, &uid);
- if (r < 0)
- continue;
-
- if (fstatat(dirfd(d), de->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) < 0) {
- if (errno == ENOENT)
- continue;
-
- log_warning_errno(errno, "Failed to stat /var/lib/systemd/coredump/%s: %m", de->d_name);
- continue;
- }
-
- if (!S_ISREG(st.st_mode))
- continue;
-
- if (exclude_fd >= 0 &&
- exclude_st.st_dev == st.st_dev &&
- exclude_st.st_ino == st.st_ino)
- continue;
-
- r = hashmap_ensure_allocated(&h, NULL);
- if (r < 0)
- return log_oom();
-
- t = timespec_load(&st.st_mtim);
-
- c = hashmap_get(h, UID_TO_PTR(uid));
- if (c) {
-
- if (t < c->oldest_mtime) {
- char *n;
-
- n = strdup(de->d_name);
- if (!n)
- return log_oom();
-
- free(c->oldest_file);
- c->oldest_file = n;
- c->oldest_mtime = t;
- }
-
- } else {
- _cleanup_(vacuum_candidate_freep) struct vacuum_candidate *n = NULL;
-
- n = new0(struct vacuum_candidate, 1);
- if (!n)
- return log_oom();
-
- n->oldest_file = strdup(de->d_name);
- if (!n->oldest_file)
- return log_oom();
-
- n->oldest_mtime = t;
-
- r = hashmap_put(h, UID_TO_PTR(uid), n);
- if (r < 0)
- return log_oom();
-
- c = n;
- n = NULL;
- }
-
- c->n_files++;
-
- if (!worst ||
- worst->n_files < c->n_files ||
- (worst->n_files == c->n_files && c->oldest_mtime < worst->oldest_mtime))
- worst = c;
-
- sum += st.st_blocks * 512;
- }
-
- if (!worst)
- break;
-
- r = vacuum_necessary(dirfd(d), sum, keep_free, max_use);
- if (r <= 0)
- return r;
-
- if (unlinkat(dirfd(d), worst->oldest_file, 0) < 0) {
-
- if (errno == ENOENT)
- continue;
-
- return log_error_errno(errno, "Failed to remove file %s: %m", worst->oldest_file);
- } else
- log_info("Removed old coredump %s.", worst->oldest_file);
- }
-
- return 0;
-
-fail:
- return log_error_errno(errno, "Failed to read directory: %m");
-}
diff --git a/src/journal/coredump-vacuum.h b/src/journal/coredump-vacuum.h
deleted file mode 100644
index 7779c97574..0000000000
--- a/src/journal/coredump-vacuum.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <inttypes.h>
-#include <sys/types.h>
-
-int coredump_vacuum(int exclude_fd, uint64_t keep_free, uint64_t max_use);
diff --git a/src/journal/coredump.c b/src/journal/coredump.c
deleted file mode 100644
index 8298b02439..0000000000
--- a/src/journal/coredump.c
+++ /dev/null
@@ -1,882 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Lennart Poettering
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <errno.h>
-#include <stdio.h>
-#include <sys/prctl.h>
-#include <sys/xattr.h>
-#include <unistd.h>
-
-#ifdef HAVE_ELFUTILS
-# include <dwarf.h>
-# include <elfutils/libdwfl.h>
-#endif
-
-#include "sd-journal.h"
-#include "sd-login.h"
-
-#include "acl-util.h"
-#include "alloc-util.h"
-#include "capability-util.h"
-#include "cgroup-util.h"
-#include "compress.h"
-#include "conf-parser.h"
-#include "copy.h"
-#include "coredump-vacuum.h"
-#include "dirent-util.h"
-#include "escape.h"
-#include "fd-util.h"
-#include "fileio.h"
-#include "fs-util.h"
-#include "io-util.h"
-#include "journald-native.h"
-#include "log.h"
-#include "macro.h"
-#include "mkdir.h"
-#include "parse-util.h"
-#include "process-util.h"
-#include "special.h"
-#include "stacktrace.h"
-#include "string-table.h"
-#include "string-util.h"
-#include "strv.h"
-#include "user-util.h"
-#include "util.h"
-
-/* The maximum size up to which we process coredumps */
-#define PROCESS_SIZE_MAX ((uint64_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))
-
-/* Make sure to not make this larger than the maximum journal entry
- * size. See DATA_SIZE_MAX in journald-native.c. */
-assert_cc(JOURNAL_SIZE_MAX <= DATA_SIZE_MAX);
-
-enum {
- INFO_PID,
- INFO_UID,
- INFO_GID,
- INFO_SIGNAL,
- INFO_TIMESTAMP,
- INFO_COMM,
- INFO_EXE,
- _INFO_LEN
-};
-
-typedef enum CoredumpStorage {
- COREDUMP_STORAGE_NONE,
- COREDUMP_STORAGE_EXTERNAL,
- COREDUMP_STORAGE_JOURNAL,
- COREDUMP_STORAGE_BOTH,
- _COREDUMP_STORAGE_MAX,
- _COREDUMP_STORAGE_INVALID = -1
-} CoredumpStorage;
-
-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 CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
-static bool arg_compress = true;
-static uint64_t arg_process_size_max = PROCESS_SIZE_MAX;
-static uint64_t arg_external_size_max = EXTERNAL_SIZE_MAX;
-static size_t arg_journal_size_max = JOURNAL_SIZE_MAX;
-static uint64_t arg_keep_free = (uint64_t) -1;
-static uint64_t arg_max_use = (uint64_t) -1;
-
-static int parse_config(void) {
- static const ConfigTableItem items[] = {
- { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage },
- { "Coredump", "Compress", config_parse_bool, 0, &arg_compress },
- { "Coredump", "ProcessSizeMax", config_parse_iec_uint64, 0, &arg_process_size_max },
- { "Coredump", "ExternalSizeMax", config_parse_iec_uint64, 0, &arg_external_size_max },
- { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max },
- { "Coredump", "KeepFree", config_parse_iec_uint64, 0, &arg_keep_free },
- { "Coredump", "MaxUse", config_parse_iec_uint64, 0, &arg_max_use },
- {}
- };
-
- return config_parse_many(PKGSYSCONFDIR "/coredump.conf",
- CONF_PATHS_NULSTR("systemd/coredump.conf.d"),
- "Coredump\0",
- config_item_table_lookup, items,
- 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;
- int r;
-
- assert(fd >= 0);
-
- 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)
- return log_error_errno(errno, "Failed to get ACL: %m");
-
- if (acl_create_entry(&acl, &entry) < 0 ||
- acl_set_tag_type(entry, ACL_USER) < 0 ||
- acl_set_qualifier(entry, &uid) < 0) {
- log_error_errno(errno, "Failed to patch ACL: %m");
- return -errno;
- }
-
- if (acl_get_permset(entry, &permset) < 0 ||
- acl_add_perm(permset, ACL_READ) < 0)
- return log_warning_errno(errno, "Failed to patch ACL: %m");
-
- r = calc_acl_mask_if_needed(&acl);
- if (r < 0)
- return log_warning_errno(r, "Failed to patch ACL: %m");
-
- if (acl_set_fd(fd, acl) < 0)
- return log_error_errno(errno, "Failed to apply ACL: %m");
-#endif
-
- return 0;
-}
-
-static int fix_xattr(int fd, const char *info[_INFO_LEN]) {
-
- static const char * const xattrs[_INFO_LEN] = {
- [INFO_PID] = "user.coredump.pid",
- [INFO_UID] = "user.coredump.uid",
- [INFO_GID] = "user.coredump.gid",
- [INFO_SIGNAL] = "user.coredump.signal",
- [INFO_TIMESTAMP] = "user.coredump.timestamp",
- [INFO_COMM] = "user.coredump.comm",
- [INFO_EXE] = "user.coredump.exe",
- };
-
- int r = 0;
- unsigned i;
-
- assert(fd >= 0);
-
- /* Attach some metadata to coredumps via extended
- * attributes. Just because we can. */
-
- for (i = 0; i < _INFO_LEN; i++) {
- int k;
-
- if (isempty(info[i]) || !xattrs[i])
- continue;
-
- k = fsetxattr(fd, xattrs[i], info[i], strlen(info[i]), XATTR_CREATE);
- if (k < 0 && r == 0)
- r = -errno;
- }
-
- return r;
-}
-
-#define filename_escape(s) xescape((s), "./ ")
-
-static int fix_permissions(
- int fd,
- const char *filename,
- const char *target,
- const char *info[_INFO_LEN],
- uid_t uid) {
-
- assert(fd >= 0);
- assert(filename);
- assert(target);
- assert(info);
-
- /* Ignore errors on these */
- fchmod(fd, 0640);
- fix_acl(fd, uid);
- fix_xattr(fd, info);
-
- if (fsync(fd) < 0)
- return log_error_errno(errno, "Failed to sync coredump %s: %m", filename);
-
- if (rename(filename, target) < 0)
- return log_error_errno(errno, "Failed to rename coredump %s -> %s: %m", filename, target);
-
- return 0;
-}
-
-static int maybe_remove_external_coredump(const char *filename, uint64_t size) {
-
- /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
-
- if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) &&
- size <= arg_external_size_max)
- return 0;
-
- if (!filename)
- return 1;
-
- if (unlink(filename) < 0 && errno != ENOENT)
- return log_error_errno(errno, "Failed to unlink %s: %m", filename);
-
- return 1;
-}
-
-static int make_filename(const char *info[_INFO_LEN], char **ret) {
- _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
- sd_id128_t boot = {};
- int r;
-
- assert(info);
-
- c = filename_escape(info[INFO_COMM]);
- if (!c)
- return -ENOMEM;
-
- u = filename_escape(info[INFO_UID]);
- if (!u)
- return -ENOMEM;
-
- r = sd_id128_get_boot(&boot);
- if (r < 0)
- return r;
-
- p = filename_escape(info[INFO_PID]);
- if (!p)
- return -ENOMEM;
-
- t = filename_escape(info[INFO_TIMESTAMP]);
- if (!t)
- return -ENOMEM;
-
- if (asprintf(ret,
- "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
- c,
- u,
- SD_ID128_FORMAT_VAL(boot),
- p,
- t) < 0)
- return -ENOMEM;
-
- return 0;
-}
-
-static int save_external_coredump(
- const char *info[_INFO_LEN],
- uid_t uid,
- char **ret_filename,
- int *ret_node_fd,
- int *ret_data_fd,
- uint64_t *ret_size) {
-
- _cleanup_free_ char *fn = NULL, *tmp = NULL;
- _cleanup_close_ int fd = -1;
- struct stat st;
- int r;
-
- assert(info);
- assert(ret_filename);
- assert(ret_node_fd);
- assert(ret_data_fd);
- assert(ret_size);
-
- r = make_filename(info, &fn);
- if (r < 0)
- return log_error_errno(r, "Failed to determine coredump file name: %m");
-
- r = tempfn_random(fn, NULL, &tmp);
- if (r < 0)
- return log_error_errno(r, "Failed to determine temporary file name: %m");
-
- mkdir_p_label("/var/lib/systemd/coredump", 0755);
-
- fd = open(tmp, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
- if (fd < 0)
- return log_error_errno(errno, "Failed to create coredump file %s: %m", tmp);
-
- r = copy_bytes(STDIN_FILENO, fd, arg_process_size_max, false);
- if (r == -EFBIG) {
- log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", info[INFO_PID], info[INFO_COMM]);
- goto fail;
- } else if (IN_SET(r, -EDQUOT, -ENOSPC)) {
- log_error("Not enough disk space for coredump of %s (%s), refusing.", info[INFO_PID], info[INFO_COMM]);
- goto fail;
- } else if (r < 0) {
- log_error_errno(r, "Failed to dump coredump to file: %m");
- goto fail;
- }
-
- if (fstat(fd, &st) < 0) {
- log_error_errno(errno, "Failed to fstat coredump %s: %m", tmp);
- goto fail;
- }
-
- if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
- log_error_errno(errno, "Failed to seek on %s: %m", tmp);
- goto fail;
- }
-
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
- /* If we will remove the coredump anyway, do not compress. */
- if (maybe_remove_external_coredump(NULL, st.st_size) == 0
- && arg_compress) {
-
- _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL;
- _cleanup_close_ int fd_compressed = -1;
-
- fn_compressed = strappend(fn, COMPRESSED_EXT);
- if (!fn_compressed) {
- log_oom();
- goto uncompressed;
- }
-
- r = tempfn_random(fn_compressed, NULL, &tmp_compressed);
- if (r < 0) {
- log_error_errno(r, "Failed to determine temporary file name for %s: %m", fn_compressed);
- goto uncompressed;
- }
-
- fd_compressed = open(tmp_compressed, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
- if (fd_compressed < 0) {
- log_error_errno(errno, "Failed to create file %s: %m", tmp_compressed);
- goto uncompressed;
- }
-
- r = compress_stream(fd, fd_compressed, -1);
- if (r < 0) {
- log_error_errno(r, "Failed to compress %s: %m", tmp_compressed);
- goto fail_compressed;
- }
-
- r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, info, uid);
- if (r < 0)
- goto fail_compressed;
-
- /* OK, this worked, we can get rid of the uncompressed version now */
- unlink_noerrno(tmp);
-
- *ret_filename = fn_compressed; /* compressed */
- *ret_node_fd = fd_compressed; /* compressed */
- *ret_data_fd = fd; /* uncompressed */
- *ret_size = (uint64_t) st.st_size; /* uncompressed */
-
- fn_compressed = NULL;
- fd = fd_compressed = -1;
-
- return 0;
-
- fail_compressed:
- unlink_noerrno(tmp_compressed);
- }
-
-uncompressed:
-#endif
-
- r = fix_permissions(fd, tmp, fn, info, uid);
- if (r < 0)
- goto fail;
-
- *ret_filename = fn;
- *ret_data_fd = fd;
- *ret_node_fd = -1;
- *ret_size = (uint64_t) 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(fd >= 0);
- assert(ret);
- assert(ret_size);
-
- if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
- return log_warning_errno(errno, "Failed to seek: %m");
-
- field = malloc(9 + size);
- if (!field) {
- log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
- return -ENOMEM;
- }
-
- memcpy(field, "COREDUMP=", 9);
-
- n = read(fd, field + 9, size);
- if (n < 0)
- return log_error_errno((int) n, "Failed to read core data: %m");
- if ((size_t) n < size) {
- log_error("Core data too short.");
- return -EIO;
- }
-
- *ret = field;
- *ret_size = size + 9;
-
- field = NULL;
-
- return 0;
-}
-
-/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
- * 0:/dev/pts/23
- * pos: 0
- * flags: 0100002
- *
- * 1:/dev/pts/23
- * pos: 0
- * flags: 0100002
- *
- * 2:/dev/pts/23
- * pos: 0
- * flags: 0100002
- * EOF
- */
-static int compose_open_fds(pid_t pid, char **open_fds) {
- _cleanup_closedir_ DIR *proc_fd_dir = NULL;
- _cleanup_close_ int proc_fdinfo_fd = -1;
- _cleanup_free_ char *buffer = NULL;
- _cleanup_fclose_ FILE *stream = NULL;
- const char *fddelim = "", *path;
- struct dirent *dent = NULL;
- size_t size = 0;
- int r = 0;
-
- assert(pid >= 0);
- assert(open_fds != NULL);
-
- path = procfs_file_alloca(pid, "fd");
- proc_fd_dir = opendir(path);
- if (!proc_fd_dir)
- return -errno;
-
- proc_fdinfo_fd = openat(dirfd(proc_fd_dir), "../fdinfo", O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC|O_PATH);
- if (proc_fdinfo_fd < 0)
- return -errno;
-
- stream = open_memstream(&buffer, &size);
- if (!stream)
- return -ENOMEM;
-
- FOREACH_DIRENT(dent, proc_fd_dir, return -errno) {
- _cleanup_fclose_ FILE *fdinfo = NULL;
- _cleanup_free_ char *fdname = NULL;
- char line[LINE_MAX];
- int fd;
-
- r = readlinkat_malloc(dirfd(proc_fd_dir), dent->d_name, &fdname);
- if (r < 0)
- return r;
-
- fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname);
- fddelim = "\n";
-
- /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
- fd = openat(proc_fdinfo_fd, dent->d_name, O_NOFOLLOW|O_CLOEXEC|O_RDONLY);
- if (fd < 0)
- continue;
-
- fdinfo = fdopen(fd, "re");
- if (fdinfo == NULL) {
- close(fd);
- continue;
- }
-
- FOREACH_LINE(line, fdinfo, break) {
- fputs(line, stream);
- if (!endswith(line, "\n"))
- fputc('\n', stream);
- }
- }
-
- errno = 0;
- stream = safe_fclose(stream);
-
- if (errno > 0)
- return -errno;
-
- *open_fds = buffer;
- buffer = NULL;
-
- return 0;
-}
-
-int main(int argc, char* argv[]) {
-
- /* The small core field we allocate on the stack, to keep things simple */
- char
- *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
- *core_session = NULL, *core_exe = NULL, *core_comm = NULL, *core_cmdline = NULL,
- *core_cgroup = NULL, *core_cwd = NULL, *core_root = NULL, *core_unit = NULL,
- *core_slice = NULL;
-
- /* The larger ones we allocate on the heap */
- _cleanup_free_ char
- *core_timestamp = NULL, *core_message = NULL, *coredump_data = NULL, *core_owner_uid = NULL,
- *core_open_fds = NULL, *core_proc_status = NULL, *core_proc_maps = NULL, *core_proc_limits = NULL,
- *core_proc_cgroup = NULL, *core_environ = NULL;
-
- _cleanup_free_ char *exe = NULL, *comm = NULL, *filename = NULL;
- const char *info[_INFO_LEN];
-
- _cleanup_close_ int coredump_fd = -1, coredump_node_fd = -1;
-
- struct iovec iovec[26];
- uint64_t coredump_size;
- int r, j = 0;
- uid_t uid, owner_uid;
- gid_t gid;
- pid_t pid;
- char *t;
- const char *p;
-
- /* Make sure we never enter a loop */
- prctl(PR_SET_DUMPABLE, 0);
-
- /* 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_open();
-
- if (argc < INFO_COMM + 1) {
- log_error("Not enough arguments passed from kernel (%d, expected %d).",
- argc - 1, INFO_COMM + 1 - 1);
- r = -EINVAL;
- goto finish;
- }
-
- /* Ignore all parse errors */
- parse_config();
-
- log_debug("Selected storage '%s'.", coredump_storage_to_string(arg_storage));
- log_debug("Selected compression %s.", yes_no(arg_compress));
-
- r = parse_uid(argv[INFO_UID + 1], &uid);
- if (r < 0) {
- log_error("Failed to parse UID.");
- goto finish;
- }
-
- r = parse_pid(argv[INFO_PID + 1], &pid);
- if (r < 0) {
- log_error("Failed to parse PID.");
- goto finish;
- }
-
- r = parse_gid(argv[INFO_GID + 1], &gid);
- if (r < 0) {
- log_error("Failed to parse GID.");
- goto finish;
- }
-
- if (get_process_comm(pid, &comm) < 0) {
- log_warning("Failed to get COMM, falling back to the command line.");
- comm = strv_join(argv + INFO_COMM + 1, " ");
- }
-
- if (get_process_exe(pid, &exe) < 0)
- log_warning("Failed to get EXE.");
-
- info[INFO_PID] = argv[INFO_PID + 1];
- info[INFO_UID] = argv[INFO_UID + 1];
- info[INFO_GID] = argv[INFO_GID + 1];
- info[INFO_SIGNAL] = argv[INFO_SIGNAL + 1];
- info[INFO_TIMESTAMP] = argv[INFO_TIMESTAMP + 1];
- info[INFO_COMM] = comm;
- info[INFO_EXE] = exe;
-
- if (cg_pid_get_unit(pid, &t) >= 0) {
-
- if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
- free(t);
-
- /* 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(info, uid, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
- if (r < 0)
- goto finish;
-
- r = maybe_remove_external_coredump(filename, coredump_size);
- if (r < 0)
- goto finish;
-
- log_info("Detected coredump of the journal daemon itself, diverted to %s.", filename);
- goto finish;
- }
-
- core_unit = strjoina("COREDUMP_UNIT=", t);
- free(t);
-
- } else if (cg_pid_get_user_unit(pid, &t) >= 0) {
- core_unit = strjoina("COREDUMP_USER_UNIT=", t);
- free(t);
- }
-
- if (core_unit)
- IOVEC_SET_STRING(iovec[j++], core_unit);
-
- /* 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();
-
- core_pid = strjoina("COREDUMP_PID=", info[INFO_PID]);
- IOVEC_SET_STRING(iovec[j++], core_pid);
-
- core_uid = strjoina("COREDUMP_UID=", info[INFO_UID]);
- IOVEC_SET_STRING(iovec[j++], core_uid);
-
- core_gid = strjoina("COREDUMP_GID=", info[INFO_GID]);
- IOVEC_SET_STRING(iovec[j++], core_gid);
-
- core_signal = strjoina("COREDUMP_SIGNAL=", info[INFO_SIGNAL]);
- IOVEC_SET_STRING(iovec[j++], core_signal);
-
- if (sd_pid_get_session(pid, &t) >= 0) {
- core_session = strjoina("COREDUMP_SESSION=", t);
- free(t);
-
- IOVEC_SET_STRING(iovec[j++], core_session);
- }
-
- if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
- r = asprintf(&core_owner_uid,
- "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
- if (r > 0)
- IOVEC_SET_STRING(iovec[j++], core_owner_uid);
- }
-
- if (sd_pid_get_slice(pid, &t) >= 0) {
- core_slice = strjoina("COREDUMP_SLICE=", t);
- free(t);
-
- IOVEC_SET_STRING(iovec[j++], core_slice);
- }
-
- if (comm) {
- core_comm = strjoina("COREDUMP_COMM=", comm);
- IOVEC_SET_STRING(iovec[j++], core_comm);
- }
-
- if (exe) {
- core_exe = strjoina("COREDUMP_EXE=", exe);
- IOVEC_SET_STRING(iovec[j++], core_exe);
- }
-
- if (get_process_cmdline(pid, 0, false, &t) >= 0) {
- core_cmdline = strjoina("COREDUMP_CMDLINE=", t);
- free(t);
-
- IOVEC_SET_STRING(iovec[j++], core_cmdline);
- }
-
- if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) {
- core_cgroup = strjoina("COREDUMP_CGROUP=", t);
- free(t);
-
- IOVEC_SET_STRING(iovec[j++], core_cgroup);
- }
-
- if (compose_open_fds(pid, &t) >= 0) {
- core_open_fds = strappend("COREDUMP_OPEN_FDS=", t);
- free(t);
-
- if (core_open_fds)
- IOVEC_SET_STRING(iovec[j++], core_open_fds);
- }
-
- p = procfs_file_alloca(pid, "status");
- if (read_full_file(p, &t, NULL) >= 0) {
- core_proc_status = strappend("COREDUMP_PROC_STATUS=", t);
- free(t);
-
- if (core_proc_status)
- IOVEC_SET_STRING(iovec[j++], core_proc_status);
- }
-
- p = procfs_file_alloca(pid, "maps");
- if (read_full_file(p, &t, NULL) >= 0) {
- core_proc_maps = strappend("COREDUMP_PROC_MAPS=", t);
- free(t);
-
- if (core_proc_maps)
- IOVEC_SET_STRING(iovec[j++], core_proc_maps);
- }
-
- p = procfs_file_alloca(pid, "limits");
- if (read_full_file(p, &t, NULL) >= 0) {
- core_proc_limits = strappend("COREDUMP_PROC_LIMITS=", t);
- free(t);
-
- if (core_proc_limits)
- IOVEC_SET_STRING(iovec[j++], core_proc_limits);
- }
-
- p = procfs_file_alloca(pid, "cgroup");
- if (read_full_file(p, &t, NULL) >=0) {
- core_proc_cgroup = strappend("COREDUMP_PROC_CGROUP=", t);
- free(t);
-
- if (core_proc_cgroup)
- IOVEC_SET_STRING(iovec[j++], core_proc_cgroup);
- }
-
- if (get_process_cwd(pid, &t) >= 0) {
- core_cwd = strjoina("COREDUMP_CWD=", t);
- free(t);
-
- IOVEC_SET_STRING(iovec[j++], core_cwd);
- }
-
- if (get_process_root(pid, &t) >= 0) {
- core_root = strjoina("COREDUMP_ROOT=", t);
- free(t);
-
- IOVEC_SET_STRING(iovec[j++], core_root);
- }
-
- if (get_process_environ(pid, &t) >= 0) {
- core_environ = strappend("COREDUMP_ENVIRON=", t);
- free(t);
-
- if (core_environ)
- IOVEC_SET_STRING(iovec[j++], core_environ);
- }
-
- core_timestamp = strjoin("COREDUMP_TIMESTAMP=", info[INFO_TIMESTAMP], "000000", NULL);
- if (core_timestamp)
- IOVEC_SET_STRING(iovec[j++], core_timestamp);
-
- IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
-
- assert_cc(2 == LOG_CRIT);
- IOVEC_SET_STRING(iovec[j++], "PRIORITY=2");
-
- /* Vacuum before we write anything again */
- coredump_vacuum(-1, arg_keep_free, arg_max_use);
-
- /* Always stream the coredump to disk, if that's possible */
- r = save_external_coredump(info, uid, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
- if (r < 0)
- /* skip whole core dumping part */
- goto log;
-
- /* If we don't want to keep the coredump on disk, remove it
- * now, as later on we will lack the privileges for
- * it. However, we keep the fd to it, so that we can still
- * process it and log it. */
- r = maybe_remove_external_coredump(filename, coredump_size);
- if (r < 0)
- goto finish;
- if (r == 0) {
- const char *coredump_filename;
-
- coredump_filename = strjoina("COREDUMP_FILENAME=", filename);
- IOVEC_SET_STRING(iovec[j++], coredump_filename);
- }
-
- /* Vacuum again, but exclude the coredump we just created */
- coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use);
-
- /* Now, let's drop privileges to become the user who owns the
- * segfaulted process and allocate the coredump memory under
- * the user's uid. This also ensures that the credentials
- * journald will see are the ones of the coredumping user,
- * thus making sure the user gets access to the core
- * dump. Let's also get rid of all capabilities, if we run as
- * root, we won't need them anymore. */
- r = drop_privileges(uid, gid, 0);
- if (r < 0) {
- log_error_errno(r, "Failed to drop privileges: %m");
- goto finish;
- }
-
-#ifdef HAVE_ELFUTILS
- /* Try to get a strack trace if we can */
- if (coredump_size <= arg_process_size_max) {
- _cleanup_free_ char *stacktrace = NULL;
-
- r = coredump_make_stack_trace(coredump_fd, exe, &stacktrace);
- if (r >= 0)
- core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.\n\n", stacktrace, NULL);
- else if (r == -EINVAL)
- log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
- else
- log_warning_errno(r, "Failed to generate stack trace: %m");
- }
-
- if (!core_message)
-#endif
-log:
- core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.", NULL);
- if (core_message)
- IOVEC_SET_STRING(iovec[j++], core_message);
-
- /* Optionally store the entire coredump in the journal */
- if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) &&
- coredump_size <= arg_journal_size_max) {
- size_t sz = 0;
-
- /* Store the coredump itself in the journal */
-
- 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++;
- }
- }
-
- r = sd_journal_sendv(iovec, j);
- if (r < 0)
- log_error_errno(r, "Failed to log coredump: %m");
-
-finish:
- return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
-}
diff --git a/src/journal/coredump.conf b/src/journal/coredump.conf
deleted file mode 100644
index c2f0643e03..0000000000
--- a/src/journal/coredump.conf
+++ /dev/null
@@ -1,21 +0,0 @@
-# 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.
-#
-# Entries in this file show the compile time defaults.
-# You can change settings by editing this file.
-# Defaults can be restored by simply deleting this file.
-#
-# See coredump.conf(5) for details.
-
-[Coredump]
-#Storage=external
-#Compress=yes
-#ProcessSizeMax=2G
-#ExternalSizeMax=2G
-#JournalSizeMax=767M
-#MaxUse=
-#KeepFree=
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
deleted file mode 100644
index 40ffa6afbe..0000000000
--- a/src/journal/coredumpctl.c
+++ /dev/null
@@ -1,882 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Zbigniew Jędrzejewski-Szmek
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <fcntl.h>
-#include <getopt.h>
-#include <locale.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "sd-journal.h"
-
-#include "alloc-util.h"
-#include "compress.h"
-#include "fd-util.h"
-#include "fileio.h"
-#include "journal-internal.h"
-#include "log.h"
-#include "macro.h"
-#include "pager.h"
-#include "parse-util.h"
-#include "path-util.h"
-#include "process-util.h"
-#include "set.h"
-#include "sigbus.h"
-#include "signal-util.h"
-#include "string-util.h"
-#include "terminal-util.h"
-#include "user-util.h"
-#include "util.h"
-
-static enum {
- ACTION_NONE,
- ACTION_INFO,
- ACTION_LIST,
- ACTION_DUMP,
- ACTION_GDB,
-} arg_action = ACTION_LIST;
-static const char* arg_field = NULL;
-static const char *arg_directory = NULL;
-static int arg_no_pager = false;
-static int arg_no_legend = false;
-static int arg_one = false;
-static FILE* arg_output = NULL;
-
-static Set *new_matches(void) {
- Set *set;
- char *tmp;
- int r;
-
- set = set_new(NULL);
- if (!set) {
- log_oom();
- return NULL;
- }
-
- tmp = strdup("MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
- if (!tmp) {
- log_oom();
- set_free(set);
- return NULL;
- }
-
- r = set_consume(set, tmp);
- if (r < 0) {
- log_error_errno(r, "failed to add to set: %m");
- set_free(set);
- return NULL;
- }
-
- return set;
-}
-
-static int add_match(Set *set, const char *match) {
- _cleanup_free_ char *p = NULL;
- char *pattern = NULL;
- const char* prefix;
- pid_t pid;
- int r;
-
- if (strchr(match, '='))
- prefix = "";
- else if (strchr(match, '/')) {
- r = path_make_absolute_cwd(match, &p);
- if (r < 0)
- goto fail;
- match = p;
- prefix = "COREDUMP_EXE=";
- } else if (parse_pid(match, &pid) >= 0)
- prefix = "COREDUMP_PID=";
- else
- prefix = "COREDUMP_COMM=";
-
- pattern = strjoin(prefix, match, NULL);
- if (!pattern) {
- r = -ENOMEM;
- goto fail;
- }
-
- log_debug("Adding pattern: %s", pattern);
- r = set_consume(set, pattern);
- if (r < 0)
- goto fail;
-
- return 0;
-fail:
- return log_error_errno(r, "Failed to add match: %m");
-}
-
-static void help(void) {
- printf("%s [OPTIONS...]\n\n"
- "List or retrieve coredumps from the journal.\n\n"
- "Flags:\n"
- " -h --help Show this help\n"
- " --version Print version string\n"
- " --no-pager Do not pipe output into a pager\n"
- " --no-legend Do not print the column headers.\n"
- " -1 Show information about most recent entry only\n"
- " -F --field=FIELD List all values a certain field takes\n"
- " -o --output=FILE Write output to FILE\n\n"
- " -D --directory=DIR Use journal files from directory\n\n"
-
- "Commands:\n"
- " list [MATCHES...] List available coredumps (default)\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);
-}
-
-static int parse_argv(int argc, char *argv[], Set *matches) {
- enum {
- ARG_VERSION = 0x100,
- ARG_NO_PAGER,
- ARG_NO_LEGEND,
- };
-
- int r, c;
-
- static const struct option options[] = {
- { "help", no_argument, NULL, 'h' },
- { "version" , no_argument, NULL, ARG_VERSION },
- { "no-pager", no_argument, NULL, ARG_NO_PAGER },
- { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
- { "output", required_argument, NULL, 'o' },
- { "field", required_argument, NULL, 'F' },
- { "directory", required_argument, NULL, 'D' },
- {}
- };
-
- assert(argc >= 0);
- assert(argv);
-
- while ((c = getopt_long(argc, argv, "ho:F:1D:", options, NULL)) >= 0)
- switch(c) {
-
- case 'h':
- arg_action = ACTION_NONE;
- help();
- return 0;
-
- case ARG_VERSION:
- arg_action = ACTION_NONE;
- return version();
-
- case ARG_NO_PAGER:
- arg_no_pager = true;
- break;
-
- case ARG_NO_LEGEND:
- arg_no_legend = true;
- break;
-
- case 'o':
- if (arg_output) {
- log_error("cannot set output more than once");
- return -EINVAL;
- }
-
- arg_output = fopen(optarg, "we");
- if (!arg_output)
- return log_error_errno(errno, "writing to '%s': %m", optarg);
-
- break;
-
- case 'F':
- if (arg_field) {
- log_error("cannot use --field/-F more than once");
- return -EINVAL;
- }
- arg_field = optarg;
- break;
-
- case '1':
- arg_one = true;
- break;
-
- case 'D':
- arg_directory = optarg;
- break;
-
- case '?':
- return -EINVAL;
-
- default:
- assert_not_reached("Unhandled option");
- }
-
- if (optind < argc) {
- const char *cmd = argv[optind++];
- if (streq(cmd, "list"))
- arg_action = ACTION_LIST;
- else if (streq(cmd, "dump"))
- 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;
- }
- }
-
- if (arg_field && arg_action != ACTION_LIST) {
- log_error("Option --field/-F only makes sense with list");
- return -EINVAL;
- }
-
- while (optind < argc) {
- r = add_match(matches, argv[optind]);
- if (r != 0)
- return r;
- optind++;
- }
-
- return 0;
-}
-
-static int retrieve(const void *data,
- size_t len,
- const char *name,
- char **var) {
-
- size_t ident;
- char *v;
-
- ident = strlen(name) + 1; /* name + "=" */
-
- if (len < ident)
- return 0;
-
- if (memcmp(data, name, ident - 1) != 0)
- return 0;
-
- if (((const char*) data)[ident - 1] != '=')
- return 0;
-
- 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_ char *value = NULL;
- const void *d;
- size_t l;
-
- assert(file);
- assert(j);
-
- assert(arg_field);
-
- SD_JOURNAL_FOREACH_DATA(j, d, l)
- retrieve(d, l, arg_field, &value);
-
- if (value)
- fprintf(file, "%s\n", value);
-}
-
-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,
- *filename = NULL;
- const void *d;
- size_t l;
- usec_t t;
- char buf[FORMAT_TIMESTAMP_MAX];
- int r;
- bool present;
-
- 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_FILENAME", &filename);
- }
-
- if (!pid && !uid && !gid && !sgnl && !exe && !comm && !cmdline && !filename) {
- log_warning("Empty coredump log entry");
- return -EINVAL;
- }
-
- r = sd_journal_get_realtime_usec(j, &t);
- if (r < 0)
- return log_error_errno(r, "Failed to get realtime timestamp: %m");
-
- format_timestamp(buf, sizeof(buf), t);
- present = filename && access(filename, F_OK) == 0;
-
- if (!had_legend && !arg_no_legend)
- fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
- FORMAT_TIMESTAMP_WIDTH, "TIME",
- 6, "PID",
- 5, "UID",
- 5, "GID",
- 3, "SIG",
- 1, "PRESENT",
- "EXE");
-
- fprintf(file, "%-*s %*s %*s %*s %*s %*s %s\n",
- FORMAT_TIMESTAMP_WIDTH, buf,
- 6, strna(pid),
- 5, strna(uid),
- 5, strna(gid),
- 3, strna(sgnl),
- 1, present ? "*" : "",
- strna(exe ?: (comm ?: cmdline)));
-
- 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,
- *slice = NULL, *cgroup = NULL, *owner_uid = NULL,
- *message = NULL, *timestamp = NULL, *filename = NULL;
- const void *d;
- size_t l;
- 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);
- 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, "COREDUMP_OWNER_UID", &owner_uid);
- retrieve(d, l, "COREDUMP_SLICE", &slice);
- retrieve(d, l, "COREDUMP_CGROUP", &cgroup);
- retrieve(d, l, "COREDUMP_TIMESTAMP", &timestamp);
- retrieve(d, l, "COREDUMP_FILENAME", &filename);
- retrieve(d, l, "_BOOT_ID", &boot_id);
- retrieve(d, l, "_MACHINE_ID", &machine_id);
- retrieve(d, l, "_HOSTNAME", &hostname);
- retrieve(d, l, "MESSAGE", &message);
- }
-
- if (need_space)
- fputs("\n", file);
-
- if (comm)
- fprintf(file,
- " PID: %s%s%s (%s)\n",
- ansi_highlight(), strna(pid), ansi_normal(), comm);
- else
- fprintf(file,
- " PID: %s%s%s\n",
- ansi_highlight(), strna(pid), ansi_normal());
-
- 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;
-
- 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 (timestamp) {
- usec_t u;
-
- r = safe_atou64(timestamp, &u);
- if (r >= 0) {
- char absolute[FORMAT_TIMESTAMP_MAX], relative[FORMAT_TIMESPAN_MAX];
-
- fprintf(file,
- " Timestamp: %s (%s)\n",
- format_timestamp(absolute, sizeof(absolute), u),
- format_timestamp_relative(relative, sizeof(relative), u));
-
- } else
- fprintf(file, " Timestamp: %s\n", timestamp);
- }
-
- if (cmdline)
- fprintf(file, " Command Line: %s\n", cmdline);
- if (exe)
- fprintf(file, " Executable: %s%s%s\n", ansi_highlight(), exe, ansi_normal());
- 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)
- fprintf(file, " Machine ID: %s\n", machine_id);
- if (hostname)
- fprintf(file, " Hostname: %s\n", hostname);
-
- if (filename && access(filename, F_OK) == 0)
- fprintf(file, " Coredump: %s\n", filename);
-
- if (message) {
- _cleanup_free_ char *m = NULL;
-
- m = strreplace(message, "\n", "\n ");
-
- fprintf(file, " Message: %s\n", strstrip(m ?: message));
- }
-
- return 0;
-}
-
-static int focus(sd_journal *j) {
- int r;
-
- r = sd_journal_seek_tail(j);
- if (r == 0)
- r = sd_journal_previous(j);
- if (r < 0)
- return log_error_errno(r, "Failed to search journal: %m");
- if (r == 0) {
- log_error("No match found.");
- return -ESRCH;
- }
- return r;
-}
-
-static void print_entry(sd_journal *j, unsigned n_found) {
- assert(j);
-
- if (arg_action == ACTION_INFO)
- print_info(stdout, j, n_found);
- else if (arg_field)
- print_field(stdout, j);
- else
- print_list(stdout, j, n_found);
-}
-
-static int dump_list(sd_journal *j) {
- unsigned n_found = 0;
- int r;
-
- assert(j);
-
- /* The coredumps are likely to compressed, and for just
- * listing them we don't need to decompress them, so let's
- * pick a fairly low data threshold here */
- sd_journal_set_data_threshold(j, 4096);
-
- if (arg_one) {
- r = focus(j);
- if (r < 0)
- return r;
-
- print_entry(j, 0);
- } else {
- SD_JOURNAL_FOREACH(j)
- print_entry(j, n_found++);
-
- if (!arg_field && n_found <= 0) {
- log_notice("No coredumps found.");
- return -ESRCH;
- }
- }
-
- return 0;
-}
-
-static int save_core(sd_journal *j, int fd, char **path, bool *unlink_temp) {
- const char *data;
- _cleanup_free_ char *filename = NULL;
- size_t len;
- int r;
-
- assert((fd >= 0) != !!path);
- assert(!!path == !!unlink_temp);
-
- /* Prefer uncompressed file to journal (probably cached) to
- * compressed file (probably uncached). */
- r = sd_journal_get_data(j, "COREDUMP_FILENAME", (const void**) &data, &len);
- if (r < 0 && r != -ENOENT)
- log_warning_errno(r, "Failed to retrieve COREDUMP_FILENAME: %m");
- else if (r == 0)
- retrieve(data, len, "COREDUMP_FILENAME", &filename);
-
- if (filename && access(filename, R_OK) < 0) {
- log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
- "File %s is not readable: %m", filename);
- filename = mfree(filename);
- }
-
- if (filename && !endswith(filename, ".xz") && !endswith(filename, ".lz4")) {
- if (path) {
- *path = filename;
- filename = NULL;
- }
-
- return 0;
- } else {
- _cleanup_close_ int fdt = -1;
- char *temp = NULL;
-
- if (fd < 0) {
- temp = strdup("/var/tmp/coredump-XXXXXX");
- if (!temp)
- return log_oom();
-
- fdt = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
- if (fdt < 0)
- return log_error_errno(fdt, "Failed to create temporary file: %m");
- log_debug("Created temporary file %s", temp);
-
- fd = fdt;
- }
-
- r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
- if (r == 0) {
- ssize_t sz;
-
- assert(len >= 9);
- data += 9;
- len -= 9;
-
- sz = write(fdt, data, len);
- if (sz < 0) {
- r = log_error_errno(errno,
- "Failed to write temporary file: %m");
- goto error;
- }
- if (sz != (ssize_t) len) {
- log_error("Short write to temporary file.");
- r = -EIO;
- goto error;
- }
- } else if (filename) {
-#if defined(HAVE_XZ) || defined(HAVE_LZ4)
- _cleanup_close_ int fdf;
-
- fdf = open(filename, O_RDONLY | O_CLOEXEC);
- if (fdf < 0) {
- r = log_error_errno(errno,
- "Failed to open %s: %m",
- filename);
- goto error;
- }
-
- r = decompress_stream(filename, fdf, fd, -1);
- if (r < 0) {
- log_error_errno(r, "Failed to decompress %s: %m", filename);
- goto error;
- }
-#else
- log_error("Cannot decompress file. Compiled without compression support.");
- r = -EOPNOTSUPP;
- goto error;
-#endif
- } else {
- if (r == -ENOENT)
- log_error("Cannot retrieve coredump from journal nor disk.");
- else
- log_error_errno(r, "Failed to retrieve COREDUMP field: %m");
- goto error;
- }
-
- if (temp) {
- *path = temp;
- *unlink_temp = true;
- }
-
- return 0;
-
-error:
- if (temp) {
- unlink(temp);
- log_debug("Removed temporary file %s", temp);
- }
- return r;
- }
-}
-
-static int dump_core(sd_journal* j) {
- int r;
-
- assert(j);
-
- r = focus(j);
- if (r < 0)
- return r;
-
- print_info(arg_output ? stdout : stderr, j, false);
-
- if (on_tty() && !arg_output) {
- log_error("Refusing to dump core to tty.");
- return -ENOTTY;
- }
-
- r = save_core(j, arg_output ? fileno(arg_output) : STDOUT_FILENO, NULL, NULL);
- if (r < 0)
- return log_error_errno(r, "Coredump retrieval failed: %m");
-
- r = sd_journal_previous(j);
- if (r >= 0)
- log_warning("More than one entry matches, ignoring rest.");
-
- return 0;
-}
-
-static int run_gdb(sd_journal *j) {
- _cleanup_free_ char *exe = NULL, *path = NULL;
- bool unlink_path = false;
- const char *data;
- siginfo_t st;
- size_t len;
- pid_t pid;
- int r;
-
- assert(j);
-
- r = focus(j);
- if (r < 0)
- return r;
-
- print_info(stdout, j, false);
- fputs("\n", stdout);
-
- r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
- if (r < 0)
- return log_error_errno(r, "Failed to retrieve COREDUMP_EXE field: %m");
-
- assert(len > strlen("COREDUMP_EXE="));
- data += strlen("COREDUMP_EXE=");
- len -= strlen("COREDUMP_EXE=");
-
- exe = strndup(data, len);
- if (!exe)
- return log_oom();
-
- if (endswith(exe, " (deleted)")) {
- log_error("Binary already deleted.");
- return -ENOENT;
- }
-
- if (!path_is_absolute(exe)) {
- log_error("Binary is not an absolute path.");
- return -ENOENT;
- }
-
- r = save_core(j, -1, &path, &unlink_path);
- if (r < 0)
- return log_error_errno(r, "Failed to retrieve core: %m");
-
- pid = fork();
- if (pid < 0) {
- r = log_error_errno(errno, "Failed to fork(): %m");
- goto finish;
- }
- if (pid == 0) {
- (void) reset_all_signal_handlers();
- (void) reset_signal_mask();
-
- execlp("gdb", "gdb", exe, path, NULL);
-
- log_error_errno(errno, "Failed to invoke gdb: %m");
- _exit(1);
- }
-
- r = wait_for_terminate(pid, &st);
- if (r < 0) {
- log_error_errno(r, "Failed to wait for gdb: %m");
- goto finish;
- }
-
- r = st.si_code == CLD_EXITED ? st.si_status : 255;
-
-finish:
- if (unlink_path) {
- log_debug("Removed temporary file %s", path);
- unlink(path);
- }
-
- return r;
-}
-
-int main(int argc, char *argv[]) {
- _cleanup_(sd_journal_closep) sd_journal*j = NULL;
- const char* match;
- Iterator it;
- int r = 0;
- _cleanup_set_free_free_ Set *matches = NULL;
-
- setlocale(LC_ALL, "");
- log_parse_environment();
- log_open();
-
- matches = new_matches();
- if (!matches) {
- r = -ENOMEM;
- goto end;
- }
-
- r = parse_argv(argc, argv, matches);
- if (r < 0)
- goto end;
-
- if (arg_action == ACTION_NONE)
- goto end;
-
- sigbus_install();
-
- if (arg_directory) {
- r = sd_journal_open_directory(&j, arg_directory, 0);
- if (r < 0) {
- log_error_errno(r, "Failed to open journals in directory: %s: %m", arg_directory);
- goto end;
- }
- } else {
- r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
- if (r < 0) {
- log_error_errno(r, "Failed to open journal: %m");
- goto end;
- }
- }
-
- /* We want full data, nothing truncated. */
- sd_journal_set_data_threshold(j, 0);
-
- SET_FOREACH(match, matches, it) {
- r = sd_journal_add_match(j, match, strlen(match));
- if (r != 0) {
- log_error_errno(r, "Failed to add match '%s': %m",
- match);
- goto end;
- }
- }
-
- if (_unlikely_(log_get_max_level() >= LOG_DEBUG)) {
- _cleanup_free_ char *filter;
-
- filter = journal_make_match_string(j);
- log_debug("Journal filter: %s", filter);
- }
-
- switch(arg_action) {
-
- case ACTION_LIST:
- case ACTION_INFO:
- if (!arg_no_pager)
- pager_open(false);
-
- r = dump_list(j);
- break;
-
- case ACTION_DUMP:
- r = dump_core(j);
- break;
-
- case ACTION_GDB:
- r = run_gdb(j);
- break;
-
- default:
- assert_not_reached("Shouldn't be here");
- }
-
-end:
- pager_close();
-
- if (arg_output)
- fclose(arg_output);
-
- return r >= 0 ? r : EXIT_FAILURE;
-}
diff --git a/src/journal/fsprg.c b/src/journal/fsprg.c
index 12ae7449f9..8956eb1d58 100644
--- a/src/journal/fsprg.c
+++ b/src/journal/fsprg.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/*
* fsprg v0.1 - (seekable) forward-secure pseudorandom generator
* Copyright (C) 2012 B. Poettering
diff --git a/src/journal/fsprg.h b/src/journal/fsprg.h
index b79221fc2e..829b56e240 100644
--- a/src/journal/fsprg.h
+++ b/src/journal/fsprg.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#ifndef __fsprgh__
#define __fsprgh__
diff --git a/src/journal/journal-authenticate.c b/src/journal/journal-authenticate.c
index 45d7f4b340..d8af113d3f 100644
--- a/src/journal/journal-authenticate.c
+++ b/src/journal/journal-authenticate.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journal-authenticate.h b/src/journal/journal-authenticate.h
index 118bb1367b..6c87319ede 100644
--- a/src/journal/journal-authenticate.h
+++ b/src/journal/journal-authenticate.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journal-def.h b/src/journal/journal-def.h
index c003ac05dd..67edb43960 100644
--- a/src/journal/journal-def.h
+++ b/src/journal/journal-def.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index da8039712f..994d1ec5d8 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -100,7 +98,7 @@ static int journal_file_set_online(JournalFile *f) {
if (mmap_cache_got_sigbus(f->mmap, f->fd))
return -EIO;
- switch(f->header->state) {
+ switch (f->header->state) {
case STATE_ONLINE:
return 0;
@@ -247,6 +245,7 @@ static int journal_file_refresh_header(JournalFile *f) {
int r;
assert(f);
+ assert(f->header);
r = sd_id128_get_machine(&f->header->machine_id);
if (r < 0)
@@ -273,6 +272,7 @@ static int journal_file_verify_header(JournalFile *f) {
uint32_t flags;
assert(f);
+ assert(f->header);
if (memcmp(f->header->signature, HEADER_SIGNATURE, 8))
return -EBADMSG;
@@ -381,6 +381,7 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
int r;
assert(f);
+ assert(f->header);
/* We assume that this file is not sparse, and we know that
* for sure, since we always call posix_fallocate()
@@ -544,6 +545,7 @@ static uint64_t journal_file_entry_seqnum(JournalFile *f, uint64_t *seqnum) {
uint64_t r;
assert(f);
+ assert(f->header);
r = le64toh(f->header->tail_entry_seqnum) + 1;
@@ -573,6 +575,7 @@ int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, O
void *t;
assert(f);
+ assert(f->header);
assert(type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX);
assert(size >= sizeof(ObjectHeader));
assert(offset);
@@ -622,6 +625,7 @@ static int journal_file_setup_data_hash_table(JournalFile *f) {
int r;
assert(f);
+ assert(f->header);
/* We estimate that we need 1 hash table entry per 768 bytes
of journal file and we want to make sure we never get
@@ -655,6 +659,7 @@ static int journal_file_setup_field_hash_table(JournalFile *f) {
int r;
assert(f);
+ assert(f->header);
/* We use a fixed size hash table for the fields as this
* number should grow very slowly only */
@@ -681,6 +686,7 @@ int journal_file_map_data_hash_table(JournalFile *f) {
int r;
assert(f);
+ assert(f->header);
if (f->data_hash_table)
return 0;
@@ -706,6 +712,7 @@ int journal_file_map_field_hash_table(JournalFile *f) {
int r;
assert(f);
+ assert(f->header);
if (f->field_hash_table)
return 0;
@@ -735,6 +742,8 @@ static int journal_file_link_field(
int r;
assert(f);
+ assert(f->header);
+ assert(f->field_hash_table);
assert(o);
assert(offset > 0);
@@ -778,6 +787,8 @@ static int journal_file_link_data(
int r;
assert(f);
+ assert(f->header);
+ assert(f->data_hash_table);
assert(o);
assert(offset > 0);
@@ -826,6 +837,7 @@ int journal_file_find_field_object_with_hash(
int r;
assert(f);
+ assert(f->header);
assert(field && size > 0);
/* If the field hash table is empty, we can't find anything */
@@ -897,6 +909,7 @@ int journal_file_find_data_object_with_hash(
int r;
assert(f);
+ assert(f->header);
assert(data || size == 0);
/* If there's no data hash table, then there's no entry. */
@@ -1193,6 +1206,7 @@ static int link_entry_into_array(JournalFile *f,
Object *o;
assert(f);
+ assert(f->header);
assert(first);
assert(idx);
assert(p > 0);
@@ -1313,6 +1327,7 @@ static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
int r;
assert(f);
+ assert(f->header);
assert(o);
assert(offset > 0);
@@ -1363,6 +1378,7 @@ static int journal_file_append_entry_internal(
int r;
assert(f);
+ assert(f->header);
assert(items || n_items == 0);
assert(ts);
@@ -1507,6 +1523,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
struct dual_timestamp _ts;
assert(f);
+ assert(f->header);
assert(iovec || n_iovec == 0);
if (!ts) {
@@ -1514,10 +1531,6 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
ts = &_ts;
}
- if (f->tail_entry_monotonic_valid &&
- ts->monotonic < le64toh(f->header->tail_entry_monotonic))
- return -EINVAL;
-
#ifdef HAVE_GCRYPT
r = journal_file_maybe_append_tag(f, ts->realtime);
if (r < 0)
@@ -2022,6 +2035,8 @@ int journal_file_move_to_entry_by_seqnum(
direction_t direction,
Object **ret,
uint64_t *offset) {
+ assert(f);
+ assert(f->header);
return generic_array_bisect(f,
le64toh(f->header->entry_array_offset),
@@ -2057,6 +2072,8 @@ int journal_file_move_to_entry_by_realtime(
direction_t direction,
Object **ret,
uint64_t *offset) {
+ assert(f);
+ assert(f->header);
return generic_array_bisect(f,
le64toh(f->header->entry_array_offset),
@@ -2149,7 +2166,9 @@ void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset) {
int journal_file_compare_locations(JournalFile *af, JournalFile *bf) {
assert(af);
+ assert(af->header);
assert(bf);
+ assert(bf->header);
assert(af->location_type == LOCATION_SEEK);
assert(bf->location_type == LOCATION_SEEK);
@@ -2209,6 +2228,7 @@ int journal_file_next_entry(
int r;
assert(f);
+ assert(f->header);
n = le64toh(f->header->n_entries);
if (n <= 0)
@@ -2491,6 +2511,7 @@ void journal_file_dump(JournalFile *f) {
uint64_t p;
assert(f);
+ assert(f->header);
journal_file_print_header(f);
@@ -2575,6 +2596,7 @@ void journal_file_print_header(JournalFile *f) {
char bytes[FORMAT_BYTES_MAX];
assert(f);
+ assert(f->header);
printf("File Path: %s\n"
"File ID: %s\n"
@@ -3180,6 +3202,7 @@ void journal_default_metrics(JournalMetrics *m, int fd) {
int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *from, usec_t *to) {
assert(f);
+ assert(f->header);
assert(from || to);
if (from) {
@@ -3243,6 +3266,7 @@ int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, u
bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec) {
assert(f);
+ assert(f->header);
/* If we gained new header fields we gained new features,
* hence suggest a rotation */
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
index 7970ebe738..07b9561b8a 100644
--- a/src/journal/journal-file.h
+++ b/src/journal/journal-file.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
index fa5ca11636..7639325acf 100644
--- a/src/journal/journal-internal.h
+++ b/src/journal/journal-internal.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
@@ -103,18 +101,27 @@ struct sd_journal {
unsigned current_invalidate_counter, last_invalidate_counter;
usec_t last_process_usec;
+ /* Iterating through unique fields and their data values */
char *unique_field;
JournalFile *unique_file;
uint64_t unique_offset;
+ /* Iterating through known fields */
+ JournalFile *fields_file;
+ uint64_t fields_offset;
+ uint64_t fields_hash_table_index;
+ char *fields_buffer;
+ size_t fields_buffer_allocated;
+
int flags;
- bool on_network;
- bool no_new_files;
- bool unique_file_lost; /* File we were iterating over got
- removed, and there were no more
- files, so sd_j_enumerate_unique
- will return a value equal to 0. */
+ bool on_network:1;
+ bool no_new_files:1;
+ bool unique_file_lost:1; /* File we were iterating over got
+ removed, and there were no more
+ files, so sd_j_enumerate_unique
+ will return a value equal to 0. */
+ bool fields_file_lost:1;
bool has_runtime_files:1;
bool has_persistent_files:1;
diff --git a/src/journal/journal-qrcode.c b/src/journal/journal-qrcode.c
index 257ddb302b..e38730d65c 100644
--- a/src/journal/journal-qrcode.c
+++ b/src/journal/journal-qrcode.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journal-qrcode.h b/src/journal/journal-qrcode.h
index 7d14e8754b..ef39085561 100644
--- a/src/journal/journal-qrcode.h
+++ b/src/journal/journal-qrcode.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c
index def4caab92..c7d670f4ff 100644
--- a/src/journal/journal-send.c
+++ b/src/journal/journal-send.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
index 4b5fc76eb1..05e97620ae 100644
--- a/src/journal/journal-vacuum.c
+++ b/src/journal/journal-vacuum.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journal-vacuum.h b/src/journal/journal-vacuum.h
index 49ab90af91..1e750a2170 100644
--- a/src/journal/journal-vacuum.h
+++ b/src/journal/journal-vacuum.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c
index 715847e018..b968e89bb8 100644
--- a/src/journal/journal-verify.c
+++ b/src/journal/journal-verify.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journal-verify.h b/src/journal/journal-verify.h
index e392ab61d7..8f0eaf6daa 100644
--- a/src/journal/journal-verify.h
+++ b/src/journal/journal-verify.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 1686f38c4e..273242bea6 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -69,6 +67,8 @@
#include "strv.h"
#include "syslog-util.h"
#include "terminal-util.h"
+#include "udev.h"
+#include "udev-util.h"
#include "unit-name.h"
#include "user-util.h"
@@ -136,6 +136,8 @@ static enum {
ACTION_SYNC,
ACTION_ROTATE,
ACTION_VACUUM,
+ ACTION_LIST_FIELDS,
+ ACTION_LIST_FIELD_NAMES,
} arg_action = ACTION_SHOW;
typedef struct BootId {
@@ -145,6 +147,84 @@ typedef struct BootId {
LIST_FIELDS(struct BootId, boot_list);
} BootId;
+static int add_matches_for_device(sd_journal *j, const char *devpath) {
+ int r;
+ _cleanup_udev_unref_ struct udev *udev = NULL;
+ _cleanup_udev_device_unref_ struct udev_device *device = NULL;
+ struct udev_device *d = NULL;
+ struct stat st;
+
+ assert(j);
+ assert(devpath);
+
+ if (!path_startswith(devpath, "/dev/")) {
+ log_error("Devpath does not start with /dev/");
+ return -EINVAL;
+ }
+
+ udev = udev_new();
+ if (!udev)
+ return log_oom();
+
+ r = stat(devpath, &st);
+ if (r < 0)
+ log_error_errno(errno, "Couldn't stat file: %m");
+
+ d = device = udev_device_new_from_devnum(udev, S_ISBLK(st.st_mode) ? 'b' : 'c', st.st_rdev);
+ if (!device)
+ return log_error_errno(errno, "Failed to get udev device from devnum %u:%u: %m", major(st.st_rdev), minor(st.st_rdev));
+
+ while (d) {
+ _cleanup_free_ char *match = NULL;
+ const char *subsys, *sysname, *devnode;
+
+ subsys = udev_device_get_subsystem(d);
+ if (!subsys) {
+ d = udev_device_get_parent(d);
+ continue;
+ }
+
+ sysname = udev_device_get_sysname(d);
+ if (!sysname) {
+ d = udev_device_get_parent(d);
+ continue;
+ }
+
+ match = strjoin("_KERNEL_DEVICE=+", subsys, ":", sysname, NULL);
+ if (!match)
+ return log_oom();
+
+ r = sd_journal_add_match(j, match, 0);
+ if (r < 0)
+ return log_error_errno(r, "Failed to add match: %m");
+
+ devnode = udev_device_get_devnode(d);
+ if (devnode) {
+ _cleanup_free_ char *match1 = NULL;
+
+ r = stat(devnode, &st);
+ if (r < 0)
+ return log_error_errno(r, "Failed to stat() device node \"%s\": %m", devnode);
+
+ r = asprintf(&match1, "_KERNEL_DEVICE=%c%u:%u", S_ISBLK(st.st_mode) ? 'b' : 'c', major(st.st_rdev), minor(st.st_rdev));
+ if (r < 0)
+ return log_oom();
+
+ r = sd_journal_add_match(j, match1, 0);
+ if (r < 0)
+ return log_error_errno(r, "Failed to add match: %m");
+ }
+
+ d = udev_device_get_parent(d);
+ }
+
+ r = add_match_this_boot(j, arg_machine);
+ if (r < 0)
+ return log_error_errno(r, "Failed to add match for the current boot: %m");
+
+ return 0;
+}
+
static void pager_open_if_enabled(void) {
if (arg_no_pager)
@@ -244,6 +324,7 @@ static void help(void) {
"\nCommands:\n"
" -h --help Show this help text\n"
" --version Show package version\n"
+ " -N --fields List all field names currently used\n"
" -F --field=FIELD List all values that a specified field takes\n"
" --disk-usage Show total disk usage of all journal files\n"
" --vacuum-size=BYTES Reduce disk usage below specified size\n"
@@ -340,6 +421,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "unit", required_argument, NULL, 'u' },
{ "user-unit", required_argument, NULL, ARG_USER_UNIT },
{ "field", required_argument, NULL, 'F' },
+ { "fields", no_argument, NULL, 'N' },
{ "catalog", no_argument, NULL, 'x' },
{ "list-catalog", no_argument, NULL, ARG_LIST_CATALOG },
{ "dump-catalog", no_argument, NULL, ARG_DUMP_CATALOG },
@@ -361,7 +443,7 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "hefo:aln::qmb::kD:p:c:S:U:t:u:F:xrM:", options, NULL)) >= 0)
+ while ((c = getopt_long(argc, argv, "hefo:aln::qmb::kD:p:c:S:U:t:u:NF:xrM:", options, NULL)) >= 0)
switch (c) {
@@ -698,9 +780,14 @@ static int parse_argv(int argc, char *argv[]) {
break;
case 'F':
+ arg_action = ACTION_LIST_FIELDS;
arg_field = optarg;
break;
+ case 'N':
+ arg_action = ACTION_LIST_FIELD_NAMES;
+ break;
+
case 'x':
arg_catalog = true;
break;
@@ -825,13 +912,12 @@ static int add_matches(sd_journal *j, char **args) {
have_term = false;
} else if (path_is_absolute(*i)) {
- _cleanup_free_ char *p, *t = NULL, *t2 = NULL;
+ _cleanup_free_ char *p, *t = NULL, *t2 = NULL, *interpreter = NULL;
const char *path;
- _cleanup_free_ char *interpreter = NULL;
struct stat st;
p = canonicalize_file_name(*i);
- path = p ? p : *i;
+ path = p ?: *i;
if (lstat(path, &st) < 0)
return log_error_errno(errno, "Couldn't stat file: %m");
@@ -845,34 +931,37 @@ static int add_matches(sd_journal *j, char **args) {
return log_oom();
t = strappend("_COMM=", comm);
+ if (!t)
+ return log_oom();
/* Append _EXE only if the interpreter is not a link.
Otherwise, it might be outdated often. */
- if (lstat(interpreter, &st) == 0 &&
- !S_ISLNK(st.st_mode)) {
+ if (lstat(interpreter, &st) == 0 && !S_ISLNK(st.st_mode)) {
t2 = strappend("_EXE=", interpreter);
if (!t2)
return log_oom();
}
- } else
+ } else {
t = strappend("_EXE=", path);
- } else if (S_ISCHR(st.st_mode))
- (void) asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev));
- else if (S_ISBLK(st.st_mode))
- (void) asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev));
- else {
+ if (!t)
+ return log_oom();
+ }
+
+ r = sd_journal_add_match(j, t, 0);
+
+ if (r >=0 && t2)
+ r = sd_journal_add_match(j, t2, 0);
+
+ } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
+ r = add_matches_for_device(j, path);
+ if (r < 0)
+ return r;
+ } else {
log_error("File is neither a device node, nor regular file, nor executable: %s", *i);
return -EINVAL;
}
- if (!t)
- return log_oom();
-
- r = sd_journal_add_match(j, t, 0);
- if (t2)
- r = sd_journal_add_match(j, t2, 0);
have_term = true;
-
} else {
r = sd_journal_add_match(j, *i, 0);
have_term = true;
@@ -2003,6 +2092,8 @@ int main(int argc, char *argv[]) {
case ACTION_DISK_USAGE:
case ACTION_LIST_BOOTS:
case ACTION_VACUUM:
+ case ACTION_LIST_FIELDS:
+ case ACTION_LIST_FIELD_NAMES:
/* These ones require access to the journal files, continue below. */
break;
@@ -2085,7 +2176,20 @@ int main(int argc, char *argv[]) {
goto finish;
}
+ case ACTION_LIST_FIELD_NAMES: {
+ const char *field;
+
+ SD_JOURNAL_FOREACH_FIELD(j, field) {
+ printf("%s\n", field);
+ n_shown ++;
+ }
+
+ r = 0;
+ goto finish;
+ }
+
case ACTION_SHOW:
+ case ACTION_LIST_FIELDS:
break;
default:
@@ -2139,10 +2243,12 @@ int main(int argc, char *argv[]) {
log_debug("Journal filter: %s", filter);
}
- if (arg_field) {
+ if (arg_action == ACTION_LIST_FIELDS) {
const void *data;
size_t size;
+ assert(arg_field);
+
r = sd_journal_set_data_threshold(j, 0);
if (r < 0) {
log_error_errno(r, "Failed to unset data size threshold: %m");
diff --git a/src/journal/journald-audit.c b/src/journal/journald-audit.c
index 28970131e7..b2eb8a33ef 100644
--- a/src/journal/journald-audit.c
+++ b/src/journal/journald-audit.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-audit.h b/src/journal/journald-audit.h
index 5c88bb6383..8c7457778c 100644
--- a/src/journal/journald-audit.h
+++ b/src/journal/journald-audit.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-console.c b/src/journal/journald-console.c
index 04487c29b5..fcc9f25814 100644
--- a/src/journal/journald-console.c
+++ b/src/journal/journald-console.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-console.h b/src/journal/journald-console.h
index d8af2267e1..dda07e2c28 100644
--- a/src/journal/journald-console.h
+++ b/src/journal/journald-console.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c
index 1306ad6974..eb1ac90e98 100644
--- a/src/journal/journald-kmsg.c
+++ b/src/journal/journald-kmsg.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-kmsg.h b/src/journal/journald-kmsg.h
index 9a9d089967..dab49f1e8c 100644
--- a/src/journal/journald-kmsg.h
+++ b/src/journal/journald-kmsg.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c
index f80a6ebfe5..3d8f05996b 100644
--- a/src/journal/journald-native.c
+++ b/src/journal/journald-native.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-native.h b/src/journal/journald-native.h
index 2f9d458fb5..c13b80aa4f 100644
--- a/src/journal/journald-native.h
+++ b/src/journal/journald-native.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-rate-limit.c b/src/journal/journald-rate-limit.c
index 1c406aef8e..6f6a90fe4e 100644
--- a/src/journal/journald-rate-limit.c
+++ b/src/journal/journald-rate-limit.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-rate-limit.h b/src/journal/journald-rate-limit.h
index 466239d3c6..bb0abb7ee9 100644
--- a/src/journal/journald-rate-limit.h
+++ b/src/journal/journald-rate-limit.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 8ff7ef943b..ee2db8d29f 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -244,7 +242,6 @@ static int open_journal(
int flags,
bool seal,
JournalMetrics *metrics,
- JournalFile *template,
JournalFile **ret) {
int r;
JournalFile *f;
@@ -254,9 +251,9 @@ static int open_journal(
assert(ret);
if (reliably)
- r = journal_file_open_reliably(fname, flags, 0640, s->compress, seal, metrics, s->mmap, template, &f);
+ r = journal_file_open_reliably(fname, flags, 0640, s->compress, seal, metrics, s->mmap, NULL, &f);
else
- r = journal_file_open(fname, flags, 0640, s->compress, seal, metrics, s->mmap, template, &f);
+ r = journal_file_open(fname, flags, 0640, s->compress, seal, metrics, s->mmap, NULL, &f);
if (r < 0)
return r;
@@ -308,7 +305,7 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
journal_file_close(f);
}
- r = open_journal(s, true, p, O_RDWR|O_CREAT, s->seal, &s->system_metrics, NULL, &f);
+ r = open_journal(s, true, p, O_RDWR|O_CREAT, s->seal, &s->system_metrics, &f);
if (r < 0)
return s->system_journal;
@@ -1000,7 +997,7 @@ static int system_journal_open(Server *s, bool flush_requested) {
(void) mkdir(fn, 0755);
fn = strjoina(fn, "/system.journal");
- r = open_journal(s, true, fn, O_RDWR|O_CREAT, s->seal, &s->system_metrics, NULL, &s->system_journal);
+ r = open_journal(s, true, fn, O_RDWR|O_CREAT, s->seal, &s->system_metrics, &s->system_journal);
if (r >= 0) {
server_add_acls(s->system_journal, 0);
(void) determine_space_for(s, &s->system_metrics, "/var/log/journal/", "System journal", true, true, NULL, NULL);
@@ -1023,7 +1020,7 @@ static int system_journal_open(Server *s, bool flush_requested) {
* if it already exists, so that we can flush
* it into the system journal */
- r = open_journal(s, false, fn, O_RDWR, false, &s->runtime_metrics, NULL, &s->runtime_journal);
+ r = open_journal(s, false, fn, O_RDWR, false, &s->runtime_metrics, &s->runtime_journal);
if (r < 0) {
if (r != -ENOENT)
log_warning_errno(r, "Failed to open runtime journal: %m");
@@ -1040,7 +1037,7 @@ static int system_journal_open(Server *s, bool flush_requested) {
(void) mkdir("/run/log/journal", 0755);
(void) mkdir_parents(fn, 0750);
- r = open_journal(s, true, fn, O_RDWR|O_CREAT, false, &s->runtime_metrics, NULL, &s->runtime_journal);
+ r = open_journal(s, true, fn, O_RDWR|O_CREAT, false, &s->runtime_metrics, &s->runtime_journal);
if (r < 0)
return log_error_errno(r, "Failed to open runtime journal: %m");
}
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
index 49bbee0646..b9551dda1b 100644
--- a/src/journal/journald-server.h
+++ b/src/journal/journald-server.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c
index 90884b6929..6e8b405b53 100644
--- a/src/journal/journald-stream.c
+++ b/src/journal/journald-stream.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-stream.h b/src/journal/journald-stream.h
index e3497f0ded..db4c67fae3 100644
--- a/src/journal/journald-stream.h
+++ b/src/journal/journald-stream.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
index 9f2ccdcc77..5153fd0cce 100644
--- a/src/journal/journald-syslog.c
+++ b/src/journal/journald-syslog.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-syslog.h b/src/journal/journald-syslog.h
index 3774ebdf05..46ad715314 100644
--- a/src/journal/journald-syslog.h
+++ b/src/journal/journald-syslog.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald-wall.c b/src/journal/journald-wall.c
index 88bea3b86e..4d91fafffe 100644
--- a/src/journal/journald-wall.c
+++ b/src/journal/journald-wall.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/journald-wall.h b/src/journal/journald-wall.h
index 45c52854a0..ebc2b89fa8 100644
--- a/src/journal/journald-wall.h
+++ b/src/journal/journald-wall.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/journald.c b/src/journal/journald.c
index 293b788d03..272acb71c4 100644
--- a/src/journal/journald.c
+++ b/src/journal/journald.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/lookup3.h b/src/journal/lookup3.h
index 3224473a6a..787921ffbf 100644
--- a/src/journal/lookup3.h
+++ b/src/journal/lookup3.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
#include <inttypes.h>
diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c
index eb4b092e80..9c0ce8ccbf 100644
--- a/src/journal/mmap-cache.c
+++ b/src/journal/mmap-cache.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -40,9 +38,9 @@ typedef struct FileDescriptor FileDescriptor;
struct Window {
MMapCache *cache;
- bool invalidated;
- bool keep_always;
- bool in_unused;
+ bool invalidated:1;
+ bool keep_always:1;
+ bool in_unused:1;
int prot;
void *ptr;
@@ -78,7 +76,6 @@ struct MMapCache {
unsigned n_hit, n_missed;
-
Hashmap *fds;
Context *contexts[MMAP_CACHE_MAX_CONTEXTS];
@@ -174,10 +171,11 @@ _pure_ static bool window_matches(Window *w, int fd, int prot, uint64_t offset,
offset + size <= w->offset + w->size;
}
-static Window *window_add(MMapCache *m) {
+static Window *window_add(MMapCache *m, FileDescriptor *fd, int prot, bool keep_always, uint64_t offset, size_t size, void *ptr) {
Window *w;
assert(m);
+ assert(fd);
if (!m->last_unused || m->n_windows <= WINDOWS_MIN) {
@@ -195,6 +193,15 @@ static Window *window_add(MMapCache *m) {
}
w->cache = m;
+ w->fd = fd;
+ w->prot = prot;
+ w->keep_always = keep_always;
+ w->offset = offset;
+ w->size = size;
+ w->ptr = ptr;
+
+ LIST_PREPEND(by_fd, fd->windows, w);
+
return w;
}
@@ -408,7 +415,7 @@ static int try_context(
if (c->window->fd->sigbus)
return -EIO;
- c->window->keep_always |= keep_always;
+ c->window->keep_always = c->window->keep_always || keep_always;
*ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
return 1;
@@ -454,12 +461,39 @@ static int find_mmap(
return -ENOMEM;
context_attach_window(c, w);
- w->keep_always += keep_always;
+ w->keep_always = w->keep_always || keep_always;
*ret = (uint8_t*) w->ptr + (offset - w->offset);
return 1;
}
+static int mmap_try_harder(MMapCache *m, void *addr, int fd, int prot, int flags, uint64_t offset, size_t size, void **res) {
+ void *ptr;
+
+ assert(m);
+ assert(fd >= 0);
+ assert(res);
+
+ for (;;) {
+ int r;
+
+ ptr = mmap(addr, size, prot, flags, fd, offset);
+ if (ptr != MAP_FAILED)
+ break;
+ if (errno != ENOMEM)
+ return -errno;
+
+ r = make_room(m);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENOMEM;
+ }
+
+ *res = ptr;
+ return 0;
+}
+
static int add_mmap(
MMapCache *m,
int fd,
@@ -513,19 +547,9 @@ static int add_mmap(
wsize = PAGE_ALIGN(st->st_size - woffset);
}
- for (;;) {
- d = mmap(NULL, wsize, prot, MAP_SHARED, fd, woffset);
- if (d != MAP_FAILED)
- break;
- if (errno != ENOMEM)
- return -errno;
-
- r = make_room(m);
- if (r < 0)
- return r;
- if (r == 0)
- return -ENOMEM;
- }
+ r = mmap_try_harder(m, NULL, fd, prot, MAP_SHARED, woffset, wsize, &d);
+ if (r < 0)
+ return r;
c = context_add(m, context);
if (!c)
@@ -535,19 +559,10 @@ static int add_mmap(
if (!f)
goto outofmem;
- w = window_add(m);
+ w = window_add(m, f, prot, keep_always, woffset, wsize, d);
if (!w)
goto outofmem;
- w->keep_always = keep_always;
- w->ptr = d;
- w->offset = woffset;
- w->prot = prot;
- w->size = wsize;
- w->fd = f;
-
- LIST_PREPEND(by_fd, f->windows, w);
-
context_detach_window(c);
c->window = w;
LIST_PREPEND(by_window, w->contexts, c);
diff --git a/src/journal/mmap-cache.h b/src/journal/mmap-cache.h
index 37ea7b4a9c..199d944647 100644
--- a/src/journal/mmap-cache.h
+++ b/src/journal/mmap-cache.h
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
#pragma once
/***
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 74a5e262f8..5a2a28a8d4 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -1338,6 +1336,13 @@ static void remove_file_real(sd_journal *j, JournalFile *f) {
j->unique_file_lost = true;
}
+ if (j->fields_file == f) {
+ j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
+ j->fields_offset = 0;
+ if (!j->fields_file)
+ j->fields_file_lost = true;
+ }
+
journal_file_close(f);
j->current_invalidate_counter ++;
@@ -1806,6 +1811,7 @@ _public_ void sd_journal_close(sd_journal *j) {
free(j->path);
free(j->prefix);
free(j->unique_field);
+ free(j->fields_buffer);
free(j);
}
@@ -2512,24 +2518,20 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
* traversed files. */
found = false;
ORDERED_HASHMAP_FOREACH(of, j->files, i) {
- Object *oo;
- uint64_t op;
-
if (of == j->unique_file)
break;
- /* Skip this file it didn't have any fields
- * indexed */
- if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) &&
- le64toh(of->header->n_fields) <= 0)
+ /* Skip this file it didn't have any fields indexed */
+ if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
continue;
- r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), &oo, &op);
+ r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
if (r < 0)
return r;
-
- if (r > 0)
+ if (r > 0) {
found = true;
+ break;
+ }
}
if (found)
@@ -2552,6 +2554,154 @@ _public_ void sd_journal_restart_unique(sd_journal *j) {
j->unique_file_lost = false;
}
+_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **field) {
+ int r;
+
+ assert_return(j, -EINVAL);
+ assert_return(!journal_pid_changed(j), -ECHILD);
+ assert_return(field, -EINVAL);
+
+ if (!j->fields_file) {
+ if (j->fields_file_lost)
+ return 0;
+
+ j->fields_file = ordered_hashmap_first(j->files);
+ if (!j->fields_file)
+ return 0;
+
+ j->fields_hash_table_index = 0;
+ j->fields_offset = 0;
+ }
+
+ for (;;) {
+ JournalFile *f, *of;
+ Iterator i;
+ uint64_t m;
+ Object *o;
+ size_t sz;
+ bool found;
+
+ f = j->fields_file;
+
+ if (j->fields_offset == 0) {
+ bool eof = false;
+
+ /* We are not yet positioned at any field. Let's pick the first one */
+ r = journal_file_map_field_hash_table(f);
+ if (r < 0)
+ return r;
+
+ m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
+ for (;;) {
+ if (j->fields_hash_table_index >= m) {
+ /* Reached the end of the hash table, go to the next file. */
+ eof = true;
+ break;
+ }
+
+ j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
+
+ if (j->fields_offset != 0)
+ break;
+
+ /* Empty hash table bucket, go to next one */
+ j->fields_hash_table_index++;
+ }
+
+ if (eof) {
+ /* Proceed with next file */
+ j->fields_file = ordered_hashmap_next(j->files, f->path);
+ if (!j->fields_file) {
+ *field = NULL;
+ return 0;
+ }
+
+ j->fields_offset = 0;
+ j->fields_hash_table_index = 0;
+ continue;
+ }
+
+ } else {
+ /* We are already positioned at a field. If so, let's figure out the next field from it */
+
+ r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
+ if (r < 0)
+ return r;
+
+ j->fields_offset = le64toh(o->field.next_hash_offset);
+ if (j->fields_offset == 0) {
+ /* Reached the end of the hash table chain */
+ j->fields_hash_table_index++;
+ continue;
+ }
+ }
+
+ /* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
+ r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
+ if (r < 0)
+ return r;
+
+ /* Because we used OBJECT_UNUSED above, we need to do our type check manually */
+ if (o->object.type != OBJECT_FIELD) {
+ log_debug("%s:offset " OFSfmt ": object has type %i, expected %i", f->path, j->fields_offset, o->object.type, OBJECT_FIELD);
+ return -EBADMSG;
+ }
+
+ sz = le64toh(o->object.size) - offsetof(Object, field.payload);
+
+ /* Let's see if we already returned this field name before. */
+ found = false;
+ ORDERED_HASHMAP_FOREACH(of, j->files, i) {
+ if (of == f)
+ break;
+
+ /* Skip this file it didn't have any fields indexed */
+ if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
+ continue;
+
+ r = journal_file_find_field_object_with_hash(of, o->field.payload, sz, le64toh(o->field.hash), NULL, NULL);
+ if (r < 0)
+ return r;
+ if (r > 0) {
+ found = true;
+ break;
+ }
+ }
+
+ if (found)
+ continue;
+
+ /* Check if this is really a valid string containing no NUL byte */
+ if (memchr(o->field.payload, 0, sz))
+ return -EBADMSG;
+
+ if (sz > j->data_threshold)
+ sz = j->data_threshold;
+
+ if (!GREEDY_REALLOC(j->fields_buffer, j->fields_buffer_allocated, sz + 1))
+ return -ENOMEM;
+
+ memcpy(j->fields_buffer, o->field.payload, sz);
+ j->fields_buffer[sz] = 0;
+
+ if (!field_is_valid(j->fields_buffer))
+ return -EBADMSG;
+
+ *field = j->fields_buffer;
+ return 1;
+ }
+}
+
+_public_ void sd_journal_restart_fields(sd_journal *j) {
+ if (!j)
+ return;
+
+ j->fields_file = NULL;
+ j->fields_hash_table_index = 0;
+ j->fields_offset = 0;
+ j->fields_file_lost = false;
+}
+
_public_ int sd_journal_reliable_fd(sd_journal *j) {
assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD);
diff --git a/src/journal/stacktrace.c b/src/journal/stacktrace.c
deleted file mode 100644
index 4305462f80..0000000000
--- a/src/journal/stacktrace.c
+++ /dev/null
@@ -1,202 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <dwarf.h>
-#include <elfutils/libdwfl.h>
-
-#include "alloc-util.h"
-#include "fd-util.h"
-#include "formats-util.h"
-#include "macro.h"
-#include "stacktrace.h"
-#include "string-util.h"
-#include "util.h"
-
-#define FRAMES_MAX 64
-#define THREADS_MAX 64
-
-struct stack_context {
- FILE *f;
- Dwfl *dwfl;
- Elf *elf;
- unsigned n_thread;
- unsigned n_frame;
-};
-
-static int frame_callback(Dwfl_Frame *frame, void *userdata) {
- struct stack_context *c = userdata;
- Dwarf_Addr pc, pc_adjusted, bias = 0;
- _cleanup_free_ Dwarf_Die *scopes = NULL;
- const char *fname = NULL, *symbol = NULL;
- Dwfl_Module *module;
- bool is_activation;
-
- assert(frame);
- assert(c);
-
- if (c->n_frame >= FRAMES_MAX)
- return DWARF_CB_ABORT;
-
- if (!dwfl_frame_pc(frame, &pc, &is_activation))
- return DWARF_CB_ABORT;
-
- pc_adjusted = pc - (is_activation ? 0 : 1);
-
- module = dwfl_addrmodule(c->dwfl, pc_adjusted);
- if (module) {
- Dwarf_Die *s, *cudie;
- int n;
-
- cudie = dwfl_module_addrdie(module, pc_adjusted, &bias);
- if (cudie) {
- n = dwarf_getscopes(cudie, pc_adjusted - bias, &scopes);
- for (s = scopes; s < scopes + n; s++) {
- if (IN_SET(dwarf_tag(s), DW_TAG_subprogram, DW_TAG_inlined_subroutine, DW_TAG_entry_point)) {
- Dwarf_Attribute *a, space;
-
- a = dwarf_attr_integrate(s, DW_AT_MIPS_linkage_name, &space);
- if (!a)
- a = dwarf_attr_integrate(s, DW_AT_linkage_name, &space);
- if (a)
- symbol = dwarf_formstring(a);
- if (!symbol)
- symbol = dwarf_diename(s);
-
- if (symbol)
- break;
- }
- }
- }
-
- if (!symbol)
- symbol = dwfl_module_addrname(module, pc_adjusted);
-
- fname = dwfl_module_info(module, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
- }
-
- fprintf(c->f, "#%-2u 0x%016" PRIx64 " %s (%s)\n", c->n_frame, (uint64_t) pc, strna(symbol), strna(fname));
- c->n_frame ++;
-
- return DWARF_CB_OK;
-}
-
-static int thread_callback(Dwfl_Thread *thread, void *userdata) {
- struct stack_context *c = userdata;
- pid_t tid;
-
- assert(thread);
- assert(c);
-
- if (c->n_thread >= THREADS_MAX)
- return DWARF_CB_ABORT;
-
- if (c->n_thread != 0)
- fputc('\n', c->f);
-
- c->n_frame = 0;
-
- tid = dwfl_thread_tid(thread);
- fprintf(c->f, "Stack trace of thread " PID_FMT ":\n", tid);
-
- if (dwfl_thread_getframes(thread, frame_callback, c) < 0)
- return DWARF_CB_ABORT;
-
- c->n_thread ++;
-
- return DWARF_CB_OK;
-}
-
-int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
-
- static const Dwfl_Callbacks callbacks = {
- .find_elf = dwfl_build_id_find_elf,
- .find_debuginfo = dwfl_standard_find_debuginfo,
- };
-
- struct stack_context c = {};
- char *buf = NULL;
- size_t sz = 0;
- int r;
-
- assert(fd >= 0);
- assert(ret);
-
- if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
- return -errno;
-
- c.f = open_memstream(&buf, &sz);
- if (!c.f)
- return -ENOMEM;
-
- elf_version(EV_CURRENT);
-
- c.elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
- if (!c.elf) {
- r = -EINVAL;
- goto finish;
- }
-
- c.dwfl = dwfl_begin(&callbacks);
- if (!c.dwfl) {
- r = -EINVAL;
- goto finish;
- }
-
- if (dwfl_core_file_report(c.dwfl, c.elf, executable) < 0) {
- r = -EINVAL;
- goto finish;
- }
-
- if (dwfl_report_end(c.dwfl, NULL, NULL) != 0) {
- r = -EINVAL;
- goto finish;
- }
-
- if (dwfl_core_file_attach(c.dwfl, c.elf) < 0) {
- r = -EINVAL;
- goto finish;
- }
-
- if (dwfl_getthreads(c.dwfl, thread_callback, &c) < 0) {
- r = -EINVAL;
- goto finish;
- }
-
- c.f = safe_fclose(c.f);
-
- *ret = buf;
- buf = NULL;
-
- r = 0;
-
-finish:
- if (c.dwfl)
- dwfl_end(c.dwfl);
-
- if (c.elf)
- elf_end(c.elf);
-
- safe_fclose(c.f);
-
- free(buf);
-
- return r;
-}
diff --git a/src/journal/stacktrace.h b/src/journal/stacktrace.h
deleted file mode 100644
index 189e5c4597..0000000000
--- a/src/journal/stacktrace.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-#pragma once
-
-/***
- This file is part of systemd.
-
- Copyright 2014 Lennart Poettering
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-int coredump_make_stack_trace(int fd, const char *executable, char **ret);
diff --git a/src/journal/test-audit-type.c b/src/journal/test-audit-type.c
index 7946cf3c41..88a2e6d9d9 100644
--- a/src/journal/test-audit-type.c
+++ b/src/journal/test-audit-type.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-catalog.c b/src/journal/test-catalog.c
index 25980b7744..da6fcbca4d 100644
--- a/src/journal/test-catalog.c
+++ b/src/journal/test-catalog.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
@@ -46,61 +44,135 @@ static const char *no_catalog_dirs[] = {
NULL
};
-static void test_import(Hashmap *h, struct strbuf *sb,
- const char* contents, ssize_t size, int code) {
+static Hashmap * test_import(const char* contents, ssize_t size, int code) {
int r;
char name[] = "/tmp/test-catalog.XXXXXX";
_cleanup_close_ int fd;
+ Hashmap *h;
+
+ if (size < 0)
+ size = strlen(contents);
+
+ assert_se(h = hashmap_new(&catalog_hash_ops));
fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
assert_se(fd >= 0);
assert_se(write(fd, contents, size) == size);
- r = catalog_import_file(h, sb, name);
+ r = catalog_import_file(h, name);
assert_se(r == code);
unlink(name);
+
+ return h;
}
-static void test_catalog_importing(void) {
- Hashmap *h;
- struct strbuf *sb;
+static void test_catalog_import_invalid(void) {
+ _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
- assert_se(h = hashmap_new(&catalog_hash_ops));
- assert_se(sb = strbuf_new());
-
-#define BUF "xxx"
- test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
-#undef BUF
+ h = test_import("xxx", -1, -EINVAL);
assert_se(hashmap_isempty(h));
- log_debug("----------------------------------------");
+}
-#define BUF \
+static void test_catalog_import_badid(void) {
+ _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
+ const char *input =
"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \
"Subject: message\n" \
"\n" \
-"payload\n"
- test_import(h, sb, BUF, sizeof(BUF), -EINVAL);
-#undef BUF
+"payload\n";
+ h = test_import(input, -1, -EINVAL);
+}
- log_debug("----------------------------------------");
+static void test_catalog_import_one(void) {
+ _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
+ char *payload;
+ Iterator j;
-#define BUF \
+ const char *input =
"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
"Subject: message\n" \
"\n" \
-"payload\n"
- test_import(h, sb, BUF, sizeof(BUF), 0);
-#undef BUF
+"payload\n";
+ const char *expect =
+"Subject: message\n" \
+"\n" \
+"payload\n";
+ h = test_import(input, -1, 0);
assert_se(hashmap_size(h) == 1);
- log_debug("----------------------------------------");
+ HASHMAP_FOREACH(payload, h, j) {
+ assert_se(streq(expect, payload));
+ }
+}
+
+static void test_catalog_import_merge(void) {
+ _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
+ char *payload;
+ Iterator j;
+
+ const char *input =
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"payload\n" \
+"\n" \
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"\n" \
+"override payload\n";
+
+ const char *combined =
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"override payload\n";
+
+ h = test_import(input, -1, 0);
+ assert_se(hashmap_size(h) == 1);
- hashmap_free_free(h);
- strbuf_cleanup(sb);
+ HASHMAP_FOREACH(payload, h, j) {
+ assert_se(streq(combined, payload));
+ }
}
+static void test_catalog_import_merge_no_body(void) {
+ _cleanup_hashmap_free_free_free_ Hashmap *h = NULL;
+ char *payload;
+ Iterator j;
+
+ const char *input =
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"payload\n" \
+"\n" \
+"-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"\n";
+
+ const char *combined =
+"Subject: override subject\n" \
+"X-Header: hello\n" \
+"Subject: message\n" \
+"Defined-By: me\n" \
+"\n" \
+"payload\n";
+
+ h = test_import(input, -1, 0);
+ assert_se(hashmap_size(h) == 1);
+
+ HASHMAP_FOREACH(payload, h, j) {
+ assert_se(streq(combined, payload));
+ }
+}
static const char* database = NULL;
@@ -166,7 +238,11 @@ int main(int argc, char *argv[]) {
test_catalog_file_lang();
- test_catalog_importing();
+ test_catalog_import_invalid();
+ test_catalog_import_badid();
+ test_catalog_import_one();
+ test_catalog_import_merge();
+ test_catalog_import_merge_no_body();
test_catalog_update();
diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c
index baed0d82a4..5b2d130cd6 100644
--- a/src/journal/test-compress-benchmark.c
+++ b/src/journal/test-compress-benchmark.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd
diff --git a/src/journal/test-coredump-vacuum.c b/src/journal/test-coredump-vacuum.c
deleted file mode 100644
index 514dadc1dc..0000000000
--- a/src/journal/test-coredump-vacuum.c
+++ /dev/null
@@ -1,32 +0,0 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
-/***
- This file is part of systemd.
-
- Copyright 2012 Zbigniew Jędrzejewski-Szmek
-
- 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.
-
- systemd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
-
-#include <stdlib.h>
-
-#include "coredump-vacuum.h"
-
-int main(int argc, char *argv[]) {
-
- if (coredump_vacuum(-1, (uint64_t) -1, 70 * 1024) < 0)
- return EXIT_FAILURE;
-
- return EXIT_SUCCESS;
-}
diff --git a/src/journal/test-journal-enum.c b/src/journal/test-journal-enum.c
index d396fabdab..e5e9d9dcb3 100644
--- a/src/journal/test-journal-enum.c
+++ b/src/journal/test-journal-enum.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-flush.c b/src/journal/test-journal-flush.c
index 03d1522e23..7bd9c40366 100644
--- a/src/journal/test-journal-flush.c
+++ b/src/journal/test-journal-flush.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-init.c b/src/journal/test-journal-init.c
index 142da85041..ef21e2d05f 100644
--- a/src/journal/test-journal-init.c
+++ b/src/journal/test-journal-init.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c
index 5c055ef748..7f94990888 100644
--- a/src/journal/test-journal-interleaving.c
+++ b/src/journal/test-journal-interleaving.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-match.c b/src/journal/test-journal-match.c
index 4ebaa8b31a..3ab554b9b0 100644
--- a/src/journal/test-journal-match.c
+++ b/src/journal/test-journal-match.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-send.c b/src/journal/test-journal-send.c
index e537c1fe5f..d70f0b0bc8 100644
--- a/src/journal/test-journal-send.c
+++ b/src/journal/test-journal-send.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c
index 2c257e43b6..4e6f8c0f7b 100644
--- a/src/journal/test-journal-stream.c
+++ b/src/journal/test-journal-stream.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-syslog.c b/src/journal/test-journal-syslog.c
index 1784187fe9..4ff7f3ec2e 100644
--- a/src/journal/test-journal-syslog.c
+++ b/src/journal/test-journal-syslog.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c
index a7abb11fba..a26c624f41 100644
--- a/src/journal/test-journal-verify.c
+++ b/src/journal/test-journal-verify.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c
index 266e0d5473..0334b1cd1a 100644
--- a/src/journal/test-journal.c
+++ b/src/journal/test-journal.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
diff --git a/src/journal/test-mmap-cache.c b/src/journal/test-mmap-cache.c
index fdd48e531c..009aabf55e 100644
--- a/src/journal/test-mmap-cache.c
+++ b/src/journal/test-mmap-cache.c
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.