mirror of
https://github.com/Dasharo/systemd.git
synced 2026-03-06 15:02:31 -08:00
Merge pull request #12887 from fbuihuu/coredump-cleanup-part-1
Coredump cleanup part 1
This commit is contained in:
@@ -262,3 +262,85 @@ char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *f
|
||||
iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
|
||||
char *x;
|
||||
|
||||
x = set_iovec_string_field(iovec, n_iovec, field, value);
|
||||
free(value);
|
||||
return x;
|
||||
}
|
||||
|
||||
struct iovec_wrapper *iovw_new(void) {
|
||||
return malloc0(sizeof(struct iovec_wrapper));
|
||||
}
|
||||
|
||||
void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
|
||||
if (free_vectors)
|
||||
for (size_t i = 0; i < iovw->count; i++)
|
||||
free(iovw->iovec[i].iov_base);
|
||||
|
||||
iovw->iovec = mfree(iovw->iovec);
|
||||
iovw->count = 0;
|
||||
iovw->size_bytes = 0;
|
||||
}
|
||||
|
||||
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
|
||||
iovw_free_contents(iovw, true);
|
||||
|
||||
return mfree(iovw);
|
||||
}
|
||||
|
||||
struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
|
||||
iovw_free_contents(iovw, false);
|
||||
|
||||
return mfree(iovw);
|
||||
}
|
||||
|
||||
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
|
||||
if (iovw->count >= IOV_MAX)
|
||||
return -E2BIG;
|
||||
|
||||
if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
|
||||
return log_oom();
|
||||
|
||||
iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
|
||||
_cleanup_free_ char *x = NULL;
|
||||
int r;
|
||||
|
||||
x = strappend(field, value);
|
||||
if (!x)
|
||||
return log_oom();
|
||||
|
||||
r = iovw_put(iovw, x, strlen(x));
|
||||
if (r >= 0)
|
||||
TAKE_PTR(x);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value) {
|
||||
_cleanup_free_ _unused_ char *free_ptr = value;
|
||||
|
||||
return iovw_put_string_field(iovw, field, value);
|
||||
}
|
||||
|
||||
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < iovw->count; i++)
|
||||
iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
|
||||
}
|
||||
|
||||
size_t iovw_size(struct iovec_wrapper *iovw) {
|
||||
size_t n = 0, i;
|
||||
|
||||
for (i = 0; i < iovw->count; i++)
|
||||
n += iovw->iovec[i].iov_len;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -73,3 +73,20 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
|
||||
#define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)
|
||||
|
||||
char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);
|
||||
char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value);
|
||||
|
||||
struct iovec_wrapper {
|
||||
struct iovec *iovec;
|
||||
size_t count;
|
||||
size_t size_bytes;
|
||||
};
|
||||
|
||||
struct iovec_wrapper *iovw_new(void);
|
||||
struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw);
|
||||
struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw);
|
||||
void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors);
|
||||
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
|
||||
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value);
|
||||
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
|
||||
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new);
|
||||
size_t iovw_size(struct iovec_wrapper *iovw);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -108,7 +108,7 @@ static int thread_callback(Dwfl_Thread *thread, void *userdata) {
|
||||
return DWARF_CB_OK;
|
||||
}
|
||||
|
||||
int coredump_make_stack_trace(int fd, const char *executable, char **ret) {
|
||||
static int make_stack_trace(int fd, const char *executable, char **ret) {
|
||||
|
||||
static const Dwfl_Callbacks callbacks = {
|
||||
.find_elf = dwfl_build_id_find_elf,
|
||||
@@ -183,3 +183,13 @@ finish:
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void coredump_make_stack_trace(int fd, const char *executable, char **ret) {
|
||||
int r;
|
||||
|
||||
r = make_stack_trace(fd, executable, ret);
|
||||
if (r == -EINVAL)
|
||||
log_warning("Failed to generate stack trace: %s", dwfl_errmsg(dwfl_errno()));
|
||||
else if (r < 0)
|
||||
log_warning_errno(r, "Failed to generate stack trace: %m");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
int coredump_make_stack_trace(int fd, const char *executable, char **ret);
|
||||
void coredump_make_stack_trace(int fd, const char *executable, char **ret);
|
||||
|
||||
@@ -38,7 +38,7 @@ RemoteSource* source_new(int fd, bool passive_fd, char *name, Writer *writer) {
|
||||
if (!source)
|
||||
return NULL;
|
||||
|
||||
source->importer.fd = fd;
|
||||
source->importer = JOURNAL_IMPORTER_MAKE(fd);
|
||||
source->importer.passive_fd = passive_fd;
|
||||
source->importer.name = name;
|
||||
|
||||
|
||||
@@ -22,37 +22,6 @@ enum {
|
||||
IMPORTER_STATE_EOF, /* done */
|
||||
};
|
||||
|
||||
static int iovw_put(struct iovec_wrapper *iovw, void* data, size_t len) {
|
||||
if (iovw->count >= ENTRY_FIELD_COUNT_MAX)
|
||||
return -E2BIG;
|
||||
|
||||
if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
|
||||
return log_oom();
|
||||
|
||||
iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void iovw_free_contents(struct iovec_wrapper *iovw) {
|
||||
iovw->iovec = mfree(iovw->iovec);
|
||||
iovw->size_bytes = iovw->count = 0;
|
||||
}
|
||||
|
||||
static void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < iovw->count; i++)
|
||||
iovw->iovec[i].iov_base = (char*) iovw->iovec[i].iov_base - old + new;
|
||||
}
|
||||
|
||||
size_t iovw_size(struct iovec_wrapper *iovw) {
|
||||
size_t n = 0, i;
|
||||
|
||||
for (i = 0; i < iovw->count; i++)
|
||||
n += iovw->iovec[i].iov_len;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void journal_importer_cleanup(JournalImporter *imp) {
|
||||
if (imp->fd >= 0 && !imp->passive_fd) {
|
||||
@@ -62,7 +31,7 @@ void journal_importer_cleanup(JournalImporter *imp) {
|
||||
|
||||
free(imp->name);
|
||||
free(imp->buf);
|
||||
iovw_free_contents(&imp->iovw);
|
||||
iovw_free_contents(&imp->iovw, false);
|
||||
}
|
||||
|
||||
static char* realloc_buffer(JournalImporter *imp, size_t size) {
|
||||
@@ -466,7 +435,7 @@ void journal_importer_drop_iovw(JournalImporter *imp) {
|
||||
|
||||
/* This function drops processed data that along with the iovw that points at it */
|
||||
|
||||
iovw_free_contents(&imp->iovw);
|
||||
iovw_free_contents(&imp->iovw, false);
|
||||
|
||||
/* possibly reset buffer position */
|
||||
remain = imp->filled - imp->offset;
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include "io-util.h"
|
||||
#include "sd-id128.h"
|
||||
|
||||
#include "time-util.h"
|
||||
|
||||
/* Make sure not to make this smaller than the maximum coredump size.
|
||||
@@ -24,14 +24,6 @@
|
||||
/* The maximum number of fields in an entry */
|
||||
#define ENTRY_FIELD_COUNT_MAX 1024
|
||||
|
||||
struct iovec_wrapper {
|
||||
struct iovec *iovec;
|
||||
size_t size_bytes;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
size_t iovw_size(struct iovec_wrapper *iovw);
|
||||
|
||||
typedef struct JournalImporter {
|
||||
int fd;
|
||||
bool passive_fd;
|
||||
@@ -53,6 +45,9 @@ typedef struct JournalImporter {
|
||||
sd_id128_t boot_id;
|
||||
} JournalImporter;
|
||||
|
||||
#define JOURNAL_IMPORTER_INIT(_fd) { .fd = (_fd), .iovw = {} }
|
||||
#define JOURNAL_IMPORTER_MAKE(_fd) (JournalImporter) JOURNAL_IMPORTER_INIT(_fd)
|
||||
|
||||
void journal_importer_cleanup(JournalImporter *);
|
||||
int journal_importer_process_data(JournalImporter *);
|
||||
int journal_importer_push_data(JournalImporter *, const char *data, size_t size);
|
||||
|
||||
@@ -21,7 +21,7 @@ static void assert_iovec_entry(const struct iovec *iovec, const char* content) {
|
||||
"0::/user.slice/user-1002.slice/user@1002.service/gnome-terminal-server.service\n"
|
||||
|
||||
static void test_basic_parsing(void) {
|
||||
_cleanup_(journal_importer_cleanup) JournalImporter imp = {};
|
||||
_cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1);
|
||||
_cleanup_free_ char *journal_data_path = NULL;
|
||||
int r;
|
||||
|
||||
@@ -52,7 +52,7 @@ static void test_basic_parsing(void) {
|
||||
}
|
||||
|
||||
static void test_bad_input(void) {
|
||||
_cleanup_(journal_importer_cleanup) JournalImporter imp = {};
|
||||
_cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1);
|
||||
_cleanup_free_ char *journal_data_path = NULL;
|
||||
int r;
|
||||
|
||||
|
||||
@@ -9,4 +9,4 @@
|
||||
# and systemd-coredump(8) and core(5) for the explanation of the
|
||||
# setting below.
|
||||
|
||||
kernel.core_pattern=|@rootlibexecdir@/systemd-coredump %P %u %g %s %t %c %h %e
|
||||
kernel.core_pattern=|@rootlibexecdir@/systemd-coredump %P %u %g %s %t %c %h
|
||||
|
||||
Reference in New Issue
Block a user