1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-25 10:04:04 +03:00

Merge pull request #27122 from yuwata/id128-at

undefined
This commit is contained in:
Daan De Meyer 2023-04-04 09:25:37 +02:00 committed by GitHub
commit b3a3ed2d50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 24 deletions

View File

@ -4,8 +4,8 @@
#include <fcntl.h>
#include <unistd.h>
#include "chase.h"
#include "fd-util.h"
#include "fs-util.h"
#include "hexdecoct.h"
#include "id128-util.h"
#include "io-util.h"
@ -41,7 +41,7 @@ bool id128_is_valid(const char *s) {
return false;
}
int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret) {
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 */
ssize_t l;
int r;
@ -102,19 +102,20 @@ int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret) {
return r == -EINVAL ? -EUCLEAN : r;
}
int id128_read(const char *root, const char *p, Id128FormatFlag f, sd_id128_t *ret) {
int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret) {
_cleanup_close_ int fd = -EBADF;
assert(p);
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(path);
fd = chase_and_open(p, root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, /* ret_path = */ NULL);
fd = xopenat(dir_fd, path, O_RDONLY|O_CLOEXEC|O_NOCTTY, 0);
if (fd < 0)
return fd;
return id128_read_fd(fd, f, ret);
}
int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id) {
int id128_write_fd(int fd, Id128Flag f, sd_id128_t id) {
char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
size_t sz;
int r;
@ -144,12 +145,15 @@ int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id) {
return 0;
}
int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id) {
int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id) {
_cleanup_close_ int fd = -EBADF;
fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
assert(dir_fd >= 0 || dir_fd == AT_FDCWD);
assert(path);
fd = xopenat(dir_fd, path, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
if (fd < 0)
return -errno;
return fd;
return id128_write_fd(fd, f, id);
}
@ -187,9 +191,9 @@ int id128_get_product(sd_id128_t *ret) {
/* Reads the systems product UUID from DMI or devicetree (where it is located on POWER). This is
* particularly relevant in VM environments, where VM managers typically place a VM uuid there. */
r = id128_read(NULL, "/sys/class/dmi/id/product_uuid", ID128_FORMAT_UUID, &uuid);
r = id128_read("/sys/class/dmi/id/product_uuid", ID128_FORMAT_UUID, &uuid);
if (r == -ENOENT)
r = id128_read(NULL, "/proc/device-tree/vm,uuid", ID128_FORMAT_UUID, &uuid);
r = id128_read("/proc/device-tree/vm,uuid", ID128_FORMAT_UUID, &uuid);
if (r < 0)
return r;

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <fcntl.h>
#include <stdbool.h>
#include "sd-id128.h"
@ -10,19 +11,25 @@
bool id128_is_valid(const char *s) _pure_;
typedef enum Id128FormatFlag {
ID128_FORMAT_PLAIN = 1 << 0, /* formatted as 32 hex chars as-is */
ID128_FORMAT_UUID = 1 << 1, /* formatted as 36 character uuid string */
ID128_FORMAT_ANY = ID128_FORMAT_PLAIN | ID128_FORMAT_UUID,
typedef enum Id128Flag {
ID128_FORMAT_PLAIN = 1 << 0, /* formatted as 32 hex chars as-is */
ID128_FORMAT_UUID = 1 << 1, /* formatted as 36 character uuid string */
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. */
} Id128FormatFlag;
} Id128Flag;
int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret);
int id128_read(const char *root, const char *p, Id128FormatFlag f, sd_id128_t *ret);
int id128_read_fd(int fd, Id128Flag f, sd_id128_t *ret);
int id128_read_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t *ret);
static inline int id128_read(const char *path, Id128Flag f, sd_id128_t *ret) {
return id128_read_at(AT_FDCWD, path, f, ret);
}
int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id);
int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id);
int id128_write_fd(int fd, Id128Flag f, sd_id128_t id);
int id128_write_at(int dir_fd, const char *path, Id128Flag f, sd_id128_t id);
static inline int id128_write(const char *path, Id128Flag f, sd_id128_t id) {
return id128_write_at(AT_FDCWD, path, f, id);
}
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_;

View File

@ -126,7 +126,7 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
int r;
if (sd_id128_is_null(saved_machine_id)) {
r = id128_read(NULL, "/etc/machine-id", ID128_FORMAT_PLAIN, &saved_machine_id);
r = id128_read("/etc/machine-id", ID128_FORMAT_PLAIN, &saved_machine_id);
if (r < 0)
return r;
@ -144,7 +144,7 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) {
int r;
if (sd_id128_is_null(saved_boot_id)) {
r = id128_read(NULL, "/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &saved_boot_id);
r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &saved_boot_id);
if (r == -ENOENT && proc_mounted() == 0)
return -ENOSYS;
if (r < 0)

View File

@ -158,11 +158,14 @@ static int run(int argc, char *argv[]) {
}
if (arg_commit) {
const char *etc_machine_id;
r = machine_id_commit(arg_root);
if (r < 0)
return r;
r = id128_read(arg_root, "/etc/machine-id", ID128_FORMAT_PLAIN, &id);
etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
r = id128_read(etc_machine_id, ID128_FORMAT_PLAIN, &id);
if (r < 0)
return log_error_errno(r, "Failed to read machine ID back: %m");
} else {

View File

@ -2828,6 +2828,7 @@ static int mount_tunnel_open(void) {
}
static int setup_machine_id(const char *directory) {
const char *etc_machine_id;
sd_id128_t id;
int r;
@ -2838,7 +2839,9 @@ static int setup_machine_id(const char *directory) {
* in the container and our idea of the container UUID will always be in sync (at least if PID 1 in the
* container behaves nicely). */
r = id128_read(directory, "/etc/machine-id", ID128_FORMAT_PLAIN, &id);
etc_machine_id = prefix_roota(directory, "/etc/machine-id");
r = id128_read(etc_machine_id, ID128_FORMAT_PLAIN, &id);
if (r < 0) {
if (!ERRNO_IS_MACHINE_ID_UNSET(r)) /* If the file is missing, empty, or uninitialized, we don't mind */
return log_error_errno(r, "Failed to read machine ID from container image: %m");

View File

@ -11,6 +11,8 @@
#include "fd-util.h"
#include "id128-util.h"
#include "macro.h"
#include "path-util.h"
#include "rm-rf.h"
#include "string-util.h"
#include "tests.h"
#include "tmpfile-util.h"
@ -215,4 +217,76 @@ TEST(benchmark_sd_id128_get_machine_app_specific) {
log_info("%lf µs each\n", (double) q / iterations);
}
TEST(id128_at) {
_cleanup_(rm_rf_physical_and_freep) char *t = NULL;
_cleanup_close_ int tfd = -EBADF;
_cleanup_free_ char *p = NULL;
sd_id128_t id, i;
tfd = mkdtemp_open(NULL, O_PATH, &t);
assert_se(tfd >= 0);
assert_se(mkdirat(tfd, "etc", 0755) >= 0);
assert_se(symlinkat("etc", tfd, "etc2") >= 0);
assert_se(symlinkat("machine-id", tfd, "etc/hoge-id") >= 0);
assert_se(sd_id128_randomize(&id) == 0);
assert_se(id128_write_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, id) >= 0);
if (geteuid() == 0)
assert_se(id128_write_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, id) >= 0);
else
assert_se(id128_write_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, id) == -EACCES);
assert_se(unlinkat(tfd, "etc/machine-id", 0) >= 0);
assert_se(id128_write_at(tfd, "etc2/machine-id", ID128_FORMAT_PLAIN, id) >= 0);
assert_se(unlinkat(tfd, "etc/machine-id", 0) >= 0);
assert_se(id128_write_at(tfd, "etc/hoge-id", ID128_FORMAT_PLAIN, id) >= 0);
assert_se(unlinkat(tfd, "etc/machine-id", 0) >= 0);
assert_se(id128_write_at(tfd, "etc2/hoge-id", ID128_FORMAT_PLAIN, id) >= 0);
/* id128_read_at() */
i = SD_ID128_NULL; /* Not necessary in real code, but for testing that the id is really assigned. */
assert_se(id128_read_at(tfd, "etc/machine-id", ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
i = SD_ID128_NULL;
assert_se(id128_read_at(tfd, "etc2/machine-id", ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
i = SD_ID128_NULL;
assert_se(id128_read_at(tfd, "etc/hoge-id", ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
i = SD_ID128_NULL;
assert_se(id128_read_at(tfd, "etc2/hoge-id", ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
/* id128_read() */
assert_se(p = path_join(t, "/etc/machine-id"));
i = SD_ID128_NULL;
assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
free(p);
assert_se(p = path_join(t, "/etc2/machine-id"));
i = SD_ID128_NULL;
assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
free(p);
assert_se(p = path_join(t, "/etc/hoge-id"));
i = SD_ID128_NULL;
assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
free(p);
assert_se(p = path_join(t, "/etc2/hoge-id"));
i = SD_ID128_NULL;
assert_se(id128_read(p, ID128_FORMAT_PLAIN, &i) >= 0);
assert_se(sd_id128_equal(id, i));
}
DEFINE_TEST_MAIN(LOG_INFO);