diff options
author | Luca Boccassi <bluca@debian.org> | 2024-03-27 14:09:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-27 14:09:56 +0100 |
commit | 5e8adf9f5080bf29873b749b2133d6dbf2a02de6 (patch) | |
tree | 9c9d02862560cbb54b83bce52ad88dcaca0b7cf1 /src | |
parent | Merge pull request #31964 from yuwata/journalctl-filter-cleanups (diff) | |
parent | logs-show: read the current boot ID if nothing specified for add_match_boot_id() (diff) | |
download | systemd-5e8adf9f5080bf29873b749b2133d6dbf2a02de6.tar.xz systemd-5e8adf9f5080bf29873b749b2133d6dbf2a02de6.zip |
Merge pull request #31965 from yuwata/logs-show-cleanups
logs-show: several cleanups
Diffstat (limited to 'src')
-rw-r--r-- | src/journal-remote/journal-gatewayd.c | 10 | ||||
-rw-r--r-- | src/libsystemd/sd-id128/id128-util.c | 63 | ||||
-rw-r--r-- | src/libsystemd/sd-id128/id128-util.h | 3 | ||||
-rw-r--r-- | src/libsystemd/sd-id128/sd-id128.c | 16 | ||||
-rw-r--r-- | src/shared/logs-show.c | 88 |
5 files changed, 92 insertions, 88 deletions
diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index c747253858..17565f260a 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -512,15 +512,7 @@ static mhd_result request_parse_arguments_iterator( } if (r) { - sd_id128_t bid; - - r = sd_id128_get_boot(&bid); - if (r < 0) { - log_error_errno(r, "Failed to get boot ID: %m"); - return MHD_NO; - } - - r = add_match_boot_id(m->journal, bid); + r = add_match_boot_id(m->journal, SD_ID128_NULL); if (r < 0) { m->argument_parse_error = r; return MHD_NO; diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c index da20b503d4..298d21ed29 100644 --- a/src/libsystemd/sd-id128/id128-util.c +++ b/src/libsystemd/sd-id128/id128-util.c @@ -9,6 +9,8 @@ #include "hexdecoct.h" #include "id128-util.h" #include "io-util.h" +#include "namespace-util.h" +#include "process-util.h" #include "sha256.h" #include "stdio-util.h" #include "string-util.h" @@ -268,3 +270,64 @@ sd_id128_t id128_digest(const void *data, size_t size) { return id128_make_v4_uuid(id); } + +int id128_get_boot_for_machine(const char *machine, sd_id128_t *ret) { + _cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, rootfd = -EBADF; + _cleanup_close_pair_ int pair[2] = EBADF_PAIR; + pid_t pid, child; + sd_id128_t id; + ssize_t k; + int r; + + assert(ret); + + if (isempty(machine)) + return sd_id128_get_boot(ret); + + r = container_get_leader(machine, &pid); + if (r < 0) + return r; + + r = namespace_open(pid, &pidnsfd, &mntnsfd, /* ret_netns_fd = */ NULL, /* ret_userns_fd = */ NULL, &rootfd); + if (r < 0) + return r; + + if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) + return -errno; + + r = namespace_fork("(sd-bootidns)", "(sd-bootid)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, + pidnsfd, mntnsfd, -1, -1, rootfd, &child); + if (r < 0) + return r; + if (r == 0) { + pair[0] = safe_close(pair[0]); + + r = id128_get_boot(&id); + if (r < 0) + _exit(EXIT_FAILURE); + + k = send(pair[1], &id, sizeof(id), MSG_NOSIGNAL); + if (k != sizeof(id)) + _exit(EXIT_FAILURE); + + _exit(EXIT_SUCCESS); + } + + pair[1] = safe_close(pair[1]); + + r = wait_for_terminate_and_check("(sd-bootidns)", child, 0); + if (r < 0) + return r; + if (r != EXIT_SUCCESS) + return -EIO; + + k = recv(pair[0], &id, sizeof(id), 0); + if (k != sizeof(id)) + return -EIO; + + if (sd_id128_is_null(id)) + return -EIO; + + *ret = id; + return 0; +} diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 53ba50a8ac..458d430771 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -49,6 +49,9 @@ int id128_get_product(sd_id128_t *ret); sd_id128_t id128_digest(const void *data, size_t size); +int id128_get_boot(sd_id128_t *ret); +int id128_get_boot_for_machine(const char *machine, sd_id128_t *ret); + /* A helper to check for the three relevant cases of "machine ID not initialized" */ #define ERRNO_IS_NEG_MACHINE_ID_UNSET(r) \ IN_SET(r, \ diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index 9fda79ae26..4336d3f1b7 100644 --- a/src/libsystemd/sd-id128/sd-id128.c +++ b/src/libsystemd/sd-id128/sd-id128.c @@ -170,14 +170,24 @@ int id128_get_machine(const char *root, sd_id128_t *ret) { return id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret); } +int id128_get_boot(sd_id128_t *ret) { + int r; + + assert(ret); + + r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID | ID128_REFUSE_NULL, ret); + if (r == -ENOENT && proc_mounted() == 0) + return -ENOSYS; + + return r; +} + _public_ int sd_id128_get_boot(sd_id128_t *ret) { static thread_local sd_id128_t saved_boot_id = {}; int r; if (sd_id128_is_null(saved_boot_id)) { - r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID | ID128_REFUSE_NULL, &saved_boot_id); - if (r == -ENOENT && proc_mounted() == 0) - return -ENOSYS; + r = id128_get_boot(&saved_boot_id); if (r < 0) return r; } diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 85dd3db278..ac2fb1ea6b 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -5,7 +5,6 @@ #include <signal.h> #include <stdint.h> #include <stdlib.h> -#include <sys/socket.h> #include <syslog.h> #include <unistd.h> @@ -27,11 +26,9 @@ #include "log.h" #include "logs-show.h" #include "macro.h" -#include "namespace-util.h" #include "output-mode.h" #include "parse-util.h" #include "pretty-print.h" -#include "process-util.h" #include "sparse-endian.h" #include "stdio-util.h" #include "string-table.h" @@ -1638,99 +1635,38 @@ int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) { return r; } -static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) { - _cleanup_close_pair_ int pair[2] = EBADF_PAIR; - _cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, rootfd = -EBADF; - char buf[SD_ID128_UUID_STRING_MAX]; - pid_t pid, child; - ssize_t k; +int add_match_boot_id(sd_journal *j, sd_id128_t id) { int r; - assert(machine); - assert(boot_id); - - r = container_get_leader(machine, &pid); - if (r < 0) - return r; - - r = namespace_open(pid, &pidnsfd, &mntnsfd, /* ret_netns_fd = */ NULL, /* ret_userns_fd = */ NULL, &rootfd); - if (r < 0) - return r; - - if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) - return -errno; - - r = namespace_fork("(sd-bootidns)", "(sd-bootid)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL, - pidnsfd, mntnsfd, -1, -1, rootfd, &child); - if (r < 0) - return r; - if (r == 0) { - int fd; - - pair[0] = safe_close(pair[0]); - - fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY); - if (fd < 0) - _exit(EXIT_FAILURE); + assert(j); - r = loop_read_exact(fd, buf, 36, false); - safe_close(fd); + if (sd_id128_is_null(id)) { + r = sd_id128_get_boot(&id); if (r < 0) - _exit(EXIT_FAILURE); - - k = send(pair[1], buf, 36, MSG_NOSIGNAL); - if (k != 36) - _exit(EXIT_FAILURE); - - _exit(EXIT_SUCCESS); + return log_error_errno(r, "Failed to get boot ID: %m"); } - pair[1] = safe_close(pair[1]); - - r = wait_for_terminate_and_check("(sd-bootidns)", child, 0); - if (r < 0) - return r; - if (r != EXIT_SUCCESS) - return -EIO; - - k = recv(pair[0], buf, 36, 0); - if (k != 36) - return -EIO; - - buf[36] = 0; - r = sd_id128_from_string(buf, boot_id); + r = journal_add_match_pair(j, "_BOOT_ID", SD_ID128_TO_STRING(id)); if (r < 0) - return r; + return log_error_errno(r, "Failed to add match: %m"); return 0; } -int add_match_boot_id(sd_journal *j, sd_id128_t id) { - assert(j); - assert(!sd_id128_is_null(id)); - - return journal_add_match_pair(j, "_BOOT_ID", SD_ID128_TO_STRING(id)); -} - int add_match_this_boot(sd_journal *j, const char *machine) { sd_id128_t boot_id; int r; assert(j); - if (machine) { - r = get_boot_id_for_machine(machine, &boot_id); - if (r < 0) - return log_error_errno(r, "Failed to get boot id of container %s: %m", machine); - } else { - r = sd_id128_get_boot(&boot_id); - if (r < 0) - return log_error_errno(r, "Failed to get boot id: %m"); - } + r = id128_get_boot_for_machine(machine, &boot_id); + if (r < 0) + return log_error_errno(r, "Failed to get boot ID%s%s: %m", + isempty(machine) ? "" : " of container ", machine); r = add_match_boot_id(j, boot_id); if (r < 0) - return log_error_errno(r, "Failed to add match: %m"); + return r; r = sd_journal_add_conjunction(j); if (r < 0) |