summaryrefslogtreecommitdiff
path: root/src/libsystemd
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-03-26 14:03:10 +0900
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2023-03-27 11:05:25 +0200
commit830e52caa2bf1a29f56cb93e7ed85acb1bda11c3 (patch)
tree9816d11fe5a8b0ce1212178ffab8e8db023797e2 /src/libsystemd
parent4e24df3cc32fdfd48db9184d138f0051e0dafbbe (diff)
downloadsystemd-830e52caa2bf1a29f56cb93e7ed85acb1bda11c3.tar.gz
sd-id128: make id128_read() optionally take root directory
Diffstat (limited to 'src/libsystemd')
-rw-r--r--src/libsystemd/sd-id128/id128-util.c13
-rw-r--r--src/libsystemd/sd-id128/id128-util.h2
-rw-r--r--src/libsystemd/sd-id128/sd-id128.c4
3 files changed, 11 insertions, 8 deletions
diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c
index a009a110a9..974aafa5b7 100644
--- a/src/libsystemd/sd-id128/id128-util.c
+++ b/src/libsystemd/sd-id128/id128-util.c
@@ -4,6 +4,7 @@
#include <fcntl.h>
#include <unistd.h>
+#include "chase.h"
#include "fd-util.h"
#include "hexdecoct.h"
#include "id128-util.h"
@@ -101,12 +102,14 @@ int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret) {
return r == -EINVAL ? -EUCLEAN : r;
}
-int id128_read(const char *p, Id128FormatFlag f, sd_id128_t *ret) {
+int id128_read(const char *root, const char *p, Id128FormatFlag f, sd_id128_t *ret) {
_cleanup_close_ int fd = -EBADF;
- fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ assert(p);
+
+ fd = chase_and_open(p, root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, /* ret_path = */ NULL);
if (fd < 0)
- return -errno;
+ return fd;
return id128_read_fd(fd, f, ret);
}
@@ -184,9 +187,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("/sys/class/dmi/id/product_uuid", ID128_FORMAT_UUID, &uuid);
+ r = id128_read(NULL, "/sys/class/dmi/id/product_uuid", ID128_FORMAT_UUID, &uuid);
if (r == -ENOENT)
- r = id128_read("/proc/device-tree/vm,uuid", ID128_FORMAT_UUID, &uuid);
+ r = id128_read(NULL, "/proc/device-tree/vm,uuid", ID128_FORMAT_UUID, &uuid);
if (r < 0)
return r;
diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h
index e094de6441..2ebca8fd95 100644
--- a/src/libsystemd/sd-id128/id128-util.h
+++ b/src/libsystemd/sd-id128/id128-util.h
@@ -19,7 +19,7 @@ typedef enum Id128FormatFlag {
} Id128FormatFlag;
int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret);
-int id128_read(const char *p, Id128FormatFlag f, sd_id128_t *ret);
+int id128_read(const char *root, const char *p, Id128FormatFlag f, sd_id128_t *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);
diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c
index 5ce36cf2fc..03f0ddd182 100644
--- a/src/libsystemd/sd-id128/sd-id128.c
+++ b/src/libsystemd/sd-id128/sd-id128.c
@@ -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("/etc/machine-id", ID128_FORMAT_PLAIN, &saved_machine_id);
+ r = id128_read(NULL, "/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("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &saved_boot_id);
+ r = id128_read(NULL, "/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &saved_boot_id);
if (r == -ENOENT && proc_mounted() == 0)
return -ENOSYS;
if (r < 0)