From: Yu Watanabe Date: Fri, 31 Mar 2023 06:55:01 +0000 (+0900) Subject: sd-id128: introduce id128_get_machine() and id128_get_machine_at() X-Git-Tag: v254-rc1~806^2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=64c8c1bf1cae592d5de448eea522d8dd1fb7d755;p=thirdparty%2Fsystemd.git sd-id128: introduce id128_get_machine() and id128_get_machine_at() --- diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 6f7660d9b42..7bcbd8e5584 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -32,6 +32,9 @@ static inline int id128_write(const char *path, Id128Flag f, sd_id128_t id) { return id128_write_at(AT_FDCWD, path, f, id); } +int id128_get_machine(const char *root, sd_id128_t *ret); +int id128_get_machine_at(int rfd, sd_id128_t *ret); + void id128_hash_func(const sd_id128_t *p, struct siphash *state); int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) _pure_; extern const struct hash_ops id128_hash_ops; diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index 075e64d5537..6a82a7f7b89 100644 --- a/src/libsystemd/sd-id128/sd-id128.c +++ b/src/libsystemd/sd-id128/sd-id128.c @@ -7,6 +7,7 @@ #include "sd-id128.h" #include "alloc-util.h" +#include "chase.h" #include "fd-util.h" #include "hexdecoct.h" #include "hmac.h" @@ -15,6 +16,7 @@ #include "macro.h" #include "missing_syscall.h" #include "missing_threads.h" +#include "path-util.h" #include "random-util.h" #include "stat-util.h" #include "user-util.h" @@ -136,6 +138,38 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) { return 0; } +int id128_get_machine_at(int rfd, sd_id128_t *ret) { + _cleanup_close_ int fd = -EBADF; + int r; + + assert(rfd >= 0 || rfd == AT_FDCWD); + + r = dir_fd_is_root_or_cwd(rfd); + if (r < 0) + return r; + if (r > 0) + return sd_id128_get_machine(ret); + + fd = chase_and_openat(rfd, "/etc/machine-id", CHASE_AT_RESOLVE_IN_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); + if (fd < 0) + return fd; + + return id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret); +} + +int id128_get_machine(const char *root, sd_id128_t *ret) { + _cleanup_close_ int fd = -EBADF; + + if (empty_or_root(root)) + return sd_id128_get_machine(ret); + + fd = chase_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); + if (fd < 0) + return fd; + + return id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret); +} + _public_ int sd_id128_get_boot(sd_id128_t *ret) { static thread_local sd_id128_t saved_boot_id = {}; int r;