1
0
mirror of https://github.com/systemd/systemd synced 2026-04-12 18:14:51 +02:00

Compare commits

..

No commits in common. "5f74fcd41cb1a1b26c23e0f2ab405ae9cf6bcc93" and "7185af6234985a776368836d3d446f77d936a777" have entirely different histories.

8 changed files with 104 additions and 119 deletions

View File

@ -46,7 +46,8 @@ int gethostname_full(GetHostnameFlags flags, char **ret) {
assert_se(uname(&u) >= 0);
s = u.nodename;
if (isempty(s) || streq(s, "(none)") ||
if (isempty(s) ||
(!FLAGS_SET(flags, GET_HOSTNAME_ALLOW_NONE) && streq(s, "(none)")) ||
(!FLAGS_SET(flags, GET_HOSTNAME_ALLOW_LOCALHOST) && is_localhost(s)) ||
(FLAGS_SET(flags, GET_HOSTNAME_SHORT) && s[0] == '.')) {
if (!FLAGS_SET(flags, GET_HOSTNAME_FALLBACK_DEFAULT))

View File

@ -9,9 +9,10 @@
#include "strv.h"
typedef enum GetHostnameFlags {
GET_HOSTNAME_ALLOW_LOCALHOST = 1 << 0, /* accepts "localhost" or friends. */
GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 1, /* use default hostname if no hostname is set. */
GET_HOSTNAME_SHORT = 1 << 2, /* kills the FQDN part if present. */
GET_HOSTNAME_ALLOW_NONE = 1 << 0, /* accepts "(none)". */
GET_HOSTNAME_ALLOW_LOCALHOST = 1 << 1, /* accepts "localhost" or friends. */
GET_HOSTNAME_FALLBACK_DEFAULT = 1 << 2, /* use default hostname if no hostname is set. */
GET_HOSTNAME_SHORT = 1 << 3, /* kills the FQDN part if present. */
} GetHostnameFlags;
int gethostname_full(GetHostnameFlags flags, char **ret);

View File

@ -31,10 +31,10 @@ typedef enum LogTarget{
* used a regular log level. */
#define LOG_NULL (LOG_EMERG - 1)
/* Note to readers: << and >> have lower precedence (are evaluated earlier) than & and | */
/* Note to readers: << and >> have lower precedence than & and | */
#define SYNTHETIC_ERRNO(num) (1 << 30 | (num))
#define IS_SYNTHETIC_ERRNO(val) ((val) >> 30 & 1)
#define ERRNO_VALUE(val) (abs(val) & ~(1 << 30))
#define ERRNO_VALUE(val) (abs(val) & 255)
/* The callback function to be invoked when syntax warnings are seen
* in the unit files. */

View File

@ -43,13 +43,8 @@ static EFI_STATUS load_one_driver(
return log_error_status_stall(EFI_INVALID_PARAMETER, L"Image %s is not a driver, refusing: %r", fname);
err = BS->StartImage(image, NULL, NULL);
if (EFI_ERROR(err)) {
/* EFI_ABORTED signals an initializing driver. It uses this error code on success
* so that it is unloaded after. */
if (err != EFI_ABORTED)
log_error_stall(L"Failed to start image %s: %r", fname, err);
return err;
}
if (EFI_ERROR(err))
return log_error_status_stall(err, L"Failed to start image %s: %r", fname, err);
TAKE_PTR(image);
return EFI_SUCCESS;

View File

@ -793,9 +793,10 @@ static int submit_coredump(
r = maybe_remove_external_coredump(filename, coredump_node_fd >= 0 ? coredump_compressed_size : coredump_size);
if (r < 0)
return r;
if (r == 0)
if (r == 0) {
(void) iovw_put_string_field(iovw, "COREDUMP_FILENAME=", filename);
else if (arg_storage == COREDUMP_STORAGE_EXTERNAL)
} else if (arg_storage == COREDUMP_STORAGE_EXTERNAL)
log_info("The core will not be stored: size %"PRIu64" is greater than %"PRIu64" (the configured maximum)",
coredump_node_fd >= 0 ? coredump_compressed_size : coredump_size, arg_external_size_max);
@ -812,19 +813,16 @@ static int submit_coredump(
return log_error_errno(r, "Failed to drop privileges: %m");
/* Try to get a stack trace if we can */
if (coredump_size > arg_process_size_max)
if (coredump_size > arg_process_size_max) {
log_debug("Not generating stack trace: core size %"PRIu64" is greater "
"than %"PRIu64" (the configured maximum)",
coredump_size, arg_process_size_max);
else if (coredump_fd >= 0) {
bool skip = startswith(context->meta[META_COMM], "systemd-coredum"); /* COMM is 16 bytes usually */
} else if (coredump_fd >= 0)
(void) parse_elf_object(coredump_fd,
context->meta[META_EXE],
/* fork_disable_dump= */ skip, /* avoid loops */
/* fork_disable_dump= */endswith(context->meta[META_EXE], "systemd-coredump"), /* avoid loops */
&stacktrace,
&json_metadata);
}
log:
core_message = strjoina("Process ", context->meta[META_ARGV_PID],
@ -859,23 +857,20 @@ log:
(void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_JSON=", formatted_json);
}
/* In the unlikely scenario that context->meta[META_EXE] is not available,
* let's avoid guessing the module name and skip the loop. */
if (context->meta[META_EXE])
JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, json_metadata) {
JsonVariant *t;
JsonVariant *package_name, *package_version;
/* We only add structured fields for the 'main' ELF module, and only if we can identify it. */
/* We only add structured fields for the 'main' ELF module */
if (!path_equal_filename(module_name, context->meta[META_EXE]))
continue;
t = json_variant_by_key(module_json, "name");
if (t)
(void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_NAME=", json_variant_string(t));
package_name = json_variant_by_key(module_json, "name");
if (package_name)
(void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_NAME=", json_variant_string(package_name));
t = json_variant_by_key(module_json, "version");
if (t)
(void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_VERSION=", json_variant_string(t));
package_version = json_variant_by_key(module_json, "version");
if (package_version)
(void) iovw_put_string_field(iovw, "COREDUMP_PACKAGE_VERSION=", json_variant_string(package_version));
}
/* Optionally store the entire coredump in the journal */
@ -1186,7 +1181,7 @@ static int gather_pid_metadata(struct iovec_wrapper *iovw, Context *context) {
if (r < 0)
return r;
/* The following are optional, but we use them if present. */
/* The following are optional but we used them if present */
r = get_process_exe(pid, &t);
if (r >= 0)
r = iovw_put_string_field_free(iovw, "COREDUMP_EXE=", t);
@ -1196,6 +1191,7 @@ static int gather_pid_metadata(struct iovec_wrapper *iovw, Context *context) {
if (cg_pid_get_unit(pid, &t) >= 0)
(void) iovw_put_string_field_free(iovw, "COREDUMP_UNIT=", t);
/* The next are optional */
if (cg_pid_get_user_unit(pid, &t) >= 0)
(void) iovw_put_string_field_free(iovw, "COREDUMP_USER_UNIT=", t);

View File

@ -178,7 +178,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(Elf *, sym_elf_end, NULL);
static int frame_callback(Dwfl_Frame *frame, void *userdata) {
StackContext *c = userdata;
Dwarf_Addr pc, pc_adjusted;
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;
@ -197,22 +198,18 @@ static int frame_callback(Dwfl_Frame *frame, void *userdata) {
module = sym_dwfl_addrmodule(c->dwfl, pc_adjusted);
if (module) {
Dwarf_Addr start, bias = 0;
Dwarf_Die *cudie;
Dwarf_Die *s, *cudie;
int n;
Dwarf_Addr start;
cudie = sym_dwfl_module_addrdie(module, pc_adjusted, &bias);
if (cudie) {
_cleanup_free_ Dwarf_Die *scopes = NULL;
int n;
n = sym_dwarf_getscopes(cudie, pc_adjusted - bias, &scopes);
if (n > 0)
for (Dwarf_Die *s = scopes; s && s < scopes + n; s++) {
for (s = scopes; s && s < scopes + n; s++) {
if (IN_SET(sym_dwarf_tag(s), DW_TAG_subprogram, DW_TAG_inlined_subroutine, DW_TAG_entry_point)) {
Dwarf_Attribute *a, space;
if (!IN_SET(sym_dwarf_tag(s), DW_TAG_subprogram, DW_TAG_inlined_subroutine, DW_TAG_entry_point))
continue;
a = sym_dwarf_attr_integrate(s, DW_AT_MIPS_linkage_name, &space);
if (!a)
a = sym_dwarf_attr_integrate(s, DW_AT_linkage_name, &space);
@ -225,6 +222,7 @@ static int frame_callback(Dwfl_Frame *frame, void *userdata) {
break;
}
}
}
if (!symbol)
symbol = sym_dwfl_module_addrname(module, pc_adjusted);
@ -288,6 +286,7 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
/* Iterate over all program headers in that ELF object. These will have been copied by
* the kernel verbatim when the core file is generated. */
for (size_t i = 0; i < n_program_headers; ++i) {
size_t note_offset = 0, name_offset, desc_offset;
GElf_Phdr mem, *program_header;
GElf_Nhdr note_header;
Elf_Data *data;
@ -312,11 +311,8 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
if (!data)
continue;
for (size_t note_offset = 0, name_offset, desc_offset;
note_offset < data->d_size &&
(note_offset = sym_gelf_getnote(data, note_offset, &note_header, &name_offset, &desc_offset)) > 0;) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
while (note_offset < data->d_size &&
(note_offset = sym_gelf_getnote(data, note_offset, &note_header, &name_offset, &desc_offset)) > 0) {
const char *note_name = (const char *)data->d_buf + name_offset;
const char *payload = (const char *)data->d_buf + desc_offset;
@ -325,8 +321,8 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
/* Package metadata might have different owners, but the
* magic ID is always the same. */
if (note_header.n_type != ELF_PACKAGE_METADATA_ID)
continue;
if (note_header.n_type == ELF_PACKAGE_METADATA_ID) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
r = json_parse(payload, 0, &v, NULL, NULL);
if (r < 0)
@ -355,7 +351,6 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
r = json_build(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR(name, JSON_BUILD_VARIANT(v))));
if (r < 0)
return log_error_errno(r, "Failed to build JSON object: %m");
r = json_variant_merge(c->package_metadata, w);
if (r < 0)
return log_error_errno(r, "json_variant_merge of package meta with buildid failed: %m");
@ -371,6 +366,7 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
return 1;
}
}
}
if (ret_interpreter_found)
*ret_interpreter_found = interpreter_found;
@ -388,7 +384,6 @@ static int parse_buildid(Dwfl_Module *mod, Elf *elf, const char *name, StackCont
int r;
assert(mod || elf);
assert(name);
assert(c);
if (mod)
@ -577,7 +572,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
.package_metadata = &package_metadata,
.modules = &modules,
};
const char *elf_type;
const char *elf_architecture = NULL, *elf_type;
GElf_Ehdr elf_header;
size_t sz = 0;
int r;
@ -615,20 +610,19 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
elf_type = "coredump";
} else {
_cleanup_(json_variant_unrefp) JsonVariant *id_json = NULL;
const char *e = executable ?: "(unnamed)";
bool interpreter_found = false;
r = parse_buildid(NULL, c.elf, e, &c, &id_json);
r = parse_buildid(NULL, c.elf, executable, &c, &id_json);
if (r < 0)
return log_warning_errno(r, "Failed to parse build-id of ELF file: %m");
r = parse_package_metadata(e, id_json, c.elf, &interpreter_found, &c);
r = parse_package_metadata(executable, id_json, c.elf, &interpreter_found, &c);
if (r < 0)
return log_warning_errno(r, "Failed to parse package metadata of ELF file: %m");
/* If we found a build-id and nothing else, return at least that. */
if (!package_metadata && id_json) {
r = json_build(&package_metadata, JSON_BUILD_OBJECT(JSON_BUILD_PAIR(e, JSON_BUILD_VARIANT(id_json))));
r = json_build(&package_metadata, JSON_BUILD_OBJECT(JSON_BUILD_PAIR(executable, JSON_BUILD_VARIANT(id_json))));
if (r < 0)
return log_warning_errno(r, "Failed to build JSON object: %m");
}
@ -646,7 +640,8 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
return log_warning_errno(r, "Failed to build JSON object: %m");
#if HAVE_DWELF_ELF_E_MACHINE_STRING
const char *elf_architecture = sym_dwelf_elf_e_machine_string(elf_header.e_machine);
elf_architecture = sym_dwelf_elf_e_machine_string(elf_header.e_machine);
#endif
if (elf_architecture) {
_cleanup_(json_variant_unrefp) JsonVariant *json_architecture = NULL;
@ -662,7 +657,6 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
if (ret)
fprintf(c.f, "ELF object binary architecture: %s\n", elf_architecture);
}
#endif
/* We always at least have the ELF type, so merge that (and possibly the arch). */
r = json_variant_merge(&elf_metadata, package_metadata);
@ -689,8 +683,6 @@ int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, cha
_cleanup_free_ char *buf = NULL;
int r;
assert(fd >= 0);
r = dlopen_dw();
if (r < 0)
return r;

View File

@ -20,13 +20,16 @@
#include "util.h"
static int sethostname_idempotent_full(const char *s, bool really) {
struct utsname u;
_cleanup_free_ char *buf = NULL;
int r;
assert(s);
assert_se(uname(&u) >= 0);
r = gethostname_full(GET_HOSTNAME_ALLOW_NONE | GET_HOSTNAME_ALLOW_LOCALHOST, &buf);
if (r < 0)
return r;
if (streq_ptr(s, u.nodename))
if (streq(buf, s))
return 0;
if (really &&

View File

@ -1,7 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/utsname.h>
#include "sd-id128.h"
#include "errno-util.h"
@ -40,8 +38,7 @@ TEST(sysctl_normalize) {
}
TEST(sysctl_read) {
_cleanup_free_ char *s = NULL;
struct utsname u;
_cleanup_free_ char *s = NULL, *h = NULL;
sd_id128_t a, b;
int r;
@ -66,8 +63,8 @@ TEST(sysctl_read) {
s = mfree(s);
assert_se(sysctl_read("kernel/hostname", &s) >= 0);
assert_se(uname(&u) >= 0);
assert_se(streq_ptr(s, u.nodename));
assert_se(gethostname_full(GET_HOSTNAME_ALLOW_NONE|GET_HOSTNAME_ALLOW_LOCALHOST, &h) >= 0);
assert_se(streq(s, h));
r = sysctl_write("kernel/hostname", s);
assert_se(r >= 0 || ERRNO_IS_PRIVILEGE(r) || r == -EROFS);