Skip to content

Commit

Permalink
sd-id128: move get_boot_id_for_machine() to id128-util.c
Browse files Browse the repository at this point in the history
And rename it to id128_get_boot_for_machine().

This also splits out id128_get_boot() from sd_id128_get_boot(), and
make id128_get_boot_for_machine() use it.
  • Loading branch information
yuwata committed Mar 26, 2024
1 parent 17c512f commit 8e976dc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 82 deletions.
63 changes: 63 additions & 0 deletions src/libsystemd/sd-id128/id128-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/libsystemd/sd-id128/id128-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
16 changes: 13 additions & 3 deletions src/libsystemd/sd-id128/sd-id128.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
83 changes: 4 additions & 79 deletions src/shared/logs-show.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <syslog.h>
#include <unistd.h>

Expand All @@ -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"
Expand Down Expand Up @@ -1638,73 +1635,6 @@ 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 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);

r = loop_read_exact(fd, buf, 36, false);
safe_close(fd);
if (r < 0)
_exit(EXIT_FAILURE);

k = send(pair[1], buf, 36, MSG_NOSIGNAL);
if (k != 36)
_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], buf, 36, 0);
if (k != 36)
return -EIO;

buf[36] = 0;
r = sd_id128_from_string(buf, boot_id);
if (r < 0)
return r;

return 0;
}

int add_match_boot_id(sd_journal *j, sd_id128_t id) {
assert(j);
assert(!sd_id128_is_null(id));
Expand All @@ -1718,15 +1648,10 @@ int add_match_this_boot(sd_journal *j, const char *machine) {

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)
Expand Down

0 comments on commit 8e976dc

Please sign in to comment.