Merge pull request #27137 from yuwata/id128-get-machine

sd-id128: introduce id128_get_machine() and _at()
This commit is contained in:
Yu Watanabe
2023-04-05 15:24:45 +09:00
committed by GitHub
14 changed files with 113 additions and 91 deletions
+3 -17
View File
@@ -1173,23 +1173,9 @@ int image_read_metadata(Image *i) {
path = mfree(path);
r = chase("/etc/machine-id", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL);
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Failed to chase /etc/machine-id in image %s: %m", i->name);
else if (r >= 0) {
_cleanup_close_ int fd = -EBADF;
fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
log_debug_errno(errno, "Failed to open %s: %m", path);
else {
r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &machine_id);
if (r < 0)
log_debug_errno(r, "Image %s contains invalid machine ID.", i->name);
}
}
path = mfree(path);
r = id128_get_machine(i->path, &machine_id);
if (r < 0)
log_debug_errno(r, "Failed to read machine ID in image %s, ignoring: %m", i->name);
r = chase("/etc/machine-info", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL);
if (r < 0 && r != -ENOENT)
+1 -4
View File
@@ -444,10 +444,7 @@ int find_esp_and_warn_at(
assert(rfd >= 0 || rfd == AT_FDCWD);
if (rfd >= 0)
r = dir_fd_is_root(rfd);
else
r = true;
r = dir_fd_is_root_or_cwd(rfd);
if (r < 0)
return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
if (r == 0)
+5 -10
View File
@@ -8,6 +8,7 @@
#include "sd-id128.h"
#include "alloc-util.h"
#include "chase.h"
#include "fd-util.h"
#include "id128-util.h"
#include "io-util.h"
@@ -27,22 +28,16 @@
#include "virt.h"
static int generate_machine_id(const char *root, sd_id128_t *ret) {
const char *dbus_machine_id;
_cleanup_close_ int fd = -EBADF;
int r;
assert(ret);
/* First, try reading the D-Bus machine id, unless it is a symlink */
dbus_machine_id = prefix_roota(root, "/var/lib/dbus/machine-id");
fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
if (fd >= 0) {
if (id128_read_fd(fd, ID128_FORMAT_PLAIN, ret) >= 0) {
log_info("Initializing machine ID from D-Bus machine ID.");
return 0;
}
fd = safe_close(fd);
fd = chase_and_open("/var/lib/dbus/machine-id", root, CHASE_PREFIX_ROOT | CHASE_NOFOLLOW, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
if (fd >= 0 && id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret) >= 0) {
log_info("Initializing machine ID from D-Bus machine ID.");
return 0;
}
if (isempty(root) && running_in_chroot() <= 0) {
+3 -13
View File
@@ -187,19 +187,9 @@ int specifier_machine_id(char specifier, const void *data, const char *root, con
assert(ret);
if (root) {
_cleanup_close_ int fd = -EBADF;
fd = chase_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
if (fd < 0)
/* Translate error for missing os-release file to EUNATCH. */
return fd == -ENOENT ? -EUNATCH : fd;
r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
} else
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
r = id128_get_machine(root, &id);
if (r < 0) /* Translate error for missing /etc/machine-id file to EUNATCH. */
return r == -ENOENT ? -EUNATCH : r;
return specifier_id128(specifier, &id, root, userdata, ret);
}