Merge pull request #27137 from yuwata/id128-get-machine
sd-id128: introduce id128_get_machine() and _at()
This commit is contained in:
@@ -43,6 +43,7 @@ bool id128_is_valid(const char *s) {
|
||||
|
||||
int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret) {
|
||||
char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
|
||||
sd_id128_t id;
|
||||
ssize_t l;
|
||||
int r;
|
||||
|
||||
@@ -98,8 +99,18 @@ int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret) {
|
||||
return -EUCLEAN;
|
||||
}
|
||||
|
||||
r = sd_id128_from_string(buffer, ret);
|
||||
return r == -EINVAL ? -EUCLEAN : r;
|
||||
r = sd_id128_from_string(buffer, &id);
|
||||
if (r == -EINVAL)
|
||||
return -EUCLEAN;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (FLAGS_SET(f, ID128_REFUSE_NULL) && sd_id128_is_null(id))
|
||||
return -ENOMEDIUM;
|
||||
|
||||
if (ret)
|
||||
*ret = id;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret) {
|
||||
@@ -123,6 +134,9 @@ int id128_write_fd(int fd, Id128Flag f, sd_id128_t id) {
|
||||
assert(fd >= 0);
|
||||
assert(IN_SET((f & ID128_FORMAT_ANY), ID128_FORMAT_PLAIN, ID128_FORMAT_UUID));
|
||||
|
||||
if (FLAGS_SET(f, ID128_REFUSE_NULL) && sd_id128_is_null(id))
|
||||
return -ENOMEDIUM;
|
||||
|
||||
if (FLAGS_SET(f, ID128_FORMAT_PLAIN)) {
|
||||
assert_se(sd_id128_to_string(id, buffer));
|
||||
sz = SD_ID128_STRING_MAX;
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef enum Id128Flag {
|
||||
ID128_FORMAT_ANY = ID128_FORMAT_PLAIN | ID128_FORMAT_UUID,
|
||||
|
||||
ID128_SYNC_ON_WRITE = 1 << 2, /* Sync the file after write. Used only when writing an ID. */
|
||||
ID128_REFUSE_NULL = 1 << 3, /* Refuse all zero ID with -ENOMEDIUM. */
|
||||
} Id128Flag;
|
||||
|
||||
int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret);
|
||||
@@ -31,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;
|
||||
|
||||
@@ -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"
|
||||
@@ -126,12 +128,9 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
|
||||
int r;
|
||||
|
||||
if (sd_id128_is_null(saved_machine_id)) {
|
||||
r = id128_read("/etc/machine-id", ID128_FORMAT_PLAIN, &saved_machine_id);
|
||||
r = id128_read("/etc/machine-id", ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, &saved_machine_id);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (sd_id128_is_null(saved_machine_id))
|
||||
return -ENOMEDIUM;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
@@ -139,19 +138,48 @@ _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;
|
||||
|
||||
if (sd_id128_is_null(saved_boot_id)) {
|
||||
r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &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;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (sd_id128_is_null(saved_boot_id))
|
||||
return -ENOMEDIUM;
|
||||
}
|
||||
|
||||
if (ret)
|
||||
|
||||
Reference in New Issue
Block a user