coredumpctl: move resolve_filename() and retrieve() to coredumpctl-util.c

This commit is contained in:
Yu Watanabe
2026-08-26 02:05:55 +09:00
parent 7c8480fe37
commit a3b8ce6e33
4 changed files with 65 additions and 51 deletions
+51
View File
@@ -0,0 +1,51 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "chase.h"
#include "coredumpctl-util.h"
#include "log.h"
#include "string-util.h"
int resolve_filename(const char *root, char **p) {
char *resolved = NULL;
int r;
assert(p);
if (!*p)
return 0;
r = chase(*p, root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved, NULL);
if (r < 0)
return log_error_errno(r, "Failed to resolve \"%s%s\": %m", strempty(root), *p);
free_and_replace(*p, resolved);
/* chase() with flag CHASE_NONEXISTENT will return 0 if the file doesn't exist and 1 if it does.
* Return that to the caller. */
return r;
}
int retrieve(const void *data, size_t len, const char *name, char **var) {
size_t ident;
char *v;
assert(var);
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_and_replace(*var, v);
return 1;
}
+8
View File
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "forward.h"
int resolve_filename(const char *root, char **p);
int retrieve(const void *data, size_t len, const char *name, char **var);
+1 -50
View File
@@ -21,6 +21,7 @@
#include "chase.h"
#include "compress.h"
#include "coredumpctl.h"
#include "coredumpctl-util.h"
#include "dissect-image.h"
#include "dlopen-note.h"
#include "errno-util.h"
@@ -332,35 +333,6 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) {
return 1;
}
static int retrieve(const void *data,
size_t len,
const char *name,
char **var) {
size_t ident;
char *v;
assert(var);
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_and_replace(*var, v);
return 1;
}
static int print_field(FILE* file, sd_journal *j) {
const void *d;
size_t l;
@@ -452,27 +424,6 @@ error:
*ret_size = UINT64_MAX;
}
static int resolve_filename(const char *root, char **p) {
char *resolved = NULL;
int r;
assert(p);
if (!*p)
return 0;
r = chase(*p, root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved, NULL);
if (r < 0)
return log_error_errno(r, "Failed to resolve \"%s%s\": %m", strempty(root), *p);
free_and_replace(*p, resolved);
/* chase() with flag CHASE_NONEXISTENT will return 0 if the file doesn't exist and 1 if it does.
* Return that to the caller
*/
return r;
}
static int print_list(FILE* file, sd_journal *j, Table *t) {
_cleanup_free_ char
*mid = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
+5 -1
View File
@@ -17,6 +17,10 @@ systemd_coredump_sources = files(
systemd_coredump_export_sources = files(
'coredump-vacuum.c',
)
coredumpctl_sources = files(
'coredumpctl.c',
'coredumpctl-util.c',
)
coredump_gperf_c = custom_target(
input : 'coredump-gperf.gperf',
@@ -46,7 +50,7 @@ executables += [
executable_template + {
'name' : 'coredumpctl',
'public' : true,
'sources' : files('coredumpctl.c'),
'sources' : coredumpctl_sources,
'link_with' : [libshared],
'dependencies' : common_dependencies,
},