int r;
r = sd_id128_get_machine(&arg_machine_id);
- if (IN_SET(r, -ENOENT, -ENOMEDIUM)) /* Not set or empty */
+ if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* Not set or empty */
return 0;
if (r < 0)
return log_error_errno(r, "Failed to get machine-id: %m");
return false;
}
-int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) {
+int id128_read_fd(int fd, Id128FormatFlag f, sd_id128_t *ret) {
char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
ssize_t l;
assert(fd >= 0);
- assert(f < _ID128_FORMAT_MAX);
/* Reads an 128bit ID from a file, which may either be in plain format (32 hex digits), or in UUID format, both
* optionally followed by a newline and nothing else. ID files should really be newline terminated, but if they
* aren't that's OK too, following the rule of "Be conservative in what you send, be liberal in what you
- * accept". */
+ * accept".
+ *
+ * This returns the following:
+ * -ENOMEDIUM: an empty string,
+ * -ENOPKG: "uninitialized" or "uninitialized\n",
+ * -EINVAL: other invalid strings. */
l = loop_read(fd, buffer, sizeof(buffer), false); /* we expect a short read of either 32/33 or 36/37 chars */
if (l < 0)
switch (l) {
- case 13:
- case 14:
- /* Treat an "uninitialized" id file like an empty one */
- return f == ID128_PLAIN_OR_UNINIT && strneq(buffer, "uninitialized\n", l) ? -ENOMEDIUM : -EINVAL;
+ case STRLEN("uninitialized"):
+ case STRLEN("uninitialized\n"):
+ return strneq(buffer, "uninitialized\n", l) ? -ENOPKG : -EINVAL;
case SD_ID128_STRING_MAX: /* plain UUID with trailing newline */
if (buffer[SD_ID128_STRING_MAX-1] != '\n')
_fallthrough_;
case SD_ID128_STRING_MAX-1: /* plain UUID without trailing newline */
- if (f == ID128_UUID)
+ if (!FLAGS_SET(f, ID128_FORMAT_PLAIN))
return -EINVAL;
buffer[SD_ID128_STRING_MAX-1] = 0;
_fallthrough_;
case SD_ID128_UUID_STRING_MAX-1: /* RFC UUID without trailing newline */
- if (IN_SET(f, ID128_PLAIN, ID128_PLAIN_OR_UNINIT))
+ if (!FLAGS_SET(f, ID128_FORMAT_UUID))
return -EINVAL;
buffer[SD_ID128_UUID_STRING_MAX-1] = 0;
return sd_id128_from_string(buffer, ret);
}
-int id128_read(const char *p, Id128Format f, sd_id128_t *ret) {
+int id128_read(const char *p, Id128FormatFlag f, sd_id128_t *ret) {
_cleanup_close_ int fd = -1;
fd = open(p, O_RDONLY|O_CLOEXEC|O_NOCTTY);
return id128_read_fd(fd, f, ret);
}
-int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) {
+int id128_write_fd(int fd, Id128FormatFlag f, sd_id128_t id, bool do_sync) {
char buffer[SD_ID128_UUID_STRING_MAX + 1]; /* +1 is for trailing newline */
size_t sz;
int r;
assert(fd >= 0);
- assert(f < _ID128_FORMAT_MAX);
+ assert(IN_SET((f & ID128_FORMAT_ANY), ID128_FORMAT_PLAIN, ID128_FORMAT_UUID));
- if (f != ID128_UUID) {
+ if (FLAGS_SET(f, ID128_FORMAT_PLAIN)) {
assert_se(sd_id128_to_string(id, buffer));
sz = SD_ID128_STRING_MAX;
} else {
return 0;
}
-int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync) {
+int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id, bool do_sync) {
_cleanup_close_ int fd = -1;
fd = open(p, O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY|O_TRUNC, 0444);
/* 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_UUID, &uuid);
+ r = id128_read("/sys/class/dmi/id/product_uuid", ID128_FORMAT_UUID, &uuid);
if (r == -ENOENT)
- r = id128_read("/proc/device-tree/vm,uuid", ID128_UUID, &uuid);
+ r = id128_read("/proc/device-tree/vm,uuid", ID128_FORMAT_UUID, &uuid);
if (r < 0)
return r;
bool id128_is_valid(const char *s) _pure_;
-typedef enum Id128Format {
- ID128_ANY,
- ID128_PLAIN, /* formatted as 32 hex chars as-is */
- ID128_PLAIN_OR_UNINIT, /* formatted as 32 hex chars as-is; allow special "uninitialized"
- * value when reading from file (id128_read() and id128_read_fd()).
- *
- * This format should be used when reading a machine-id file. */
- ID128_UUID, /* formatted as 36 character uuid string */
- _ID128_FORMAT_MAX,
-} Id128Format;
-
-int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret);
-int id128_read(const char *p, Id128Format f, sd_id128_t *ret);
-
-int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync);
-int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync);
+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,
+} 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_write_fd(int fd, Id128FormatFlag f, sd_id128_t id, bool do_sync);
+int id128_write(const char *p, Id128FormatFlag f, sd_id128_t id, bool do_sync);
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_;
assert_return(ret, -EINVAL);
if (sd_id128_is_null(saved_machine_id)) {
- r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id);
+ r = id128_read("/etc/machine-id", ID128_FORMAT_PLAIN, &saved_machine_id);
if (r < 0)
return r;
assert_return(ret, -EINVAL);
if (sd_id128_is_null(saved_boot_id)) {
- r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id);
+ r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &saved_boot_id);
if (r < 0)
return r;
}
assert(f->header);
r = sd_id128_get_machine(&f->header->machine_id);
- if (IN_SET(r, -ENOENT, -ENOMEDIUM))
+ if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG))
/* We don't have a machine-id, let's continue without */
zero(f->header->machine_id);
else if (r < 0)
return r;
etc_machine_id = prefix_roota(arg_root, "/etc/machine-id");
- r = id128_read(etc_machine_id, ID128_PLAIN, &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 {
if (r < 0)
return log_error_errno(r, "Failed to generate random boot id: %m");
- r = id128_write(path, ID128_UUID, rnd, false);
+ r = id128_write(path, ID128_FORMAT_UUID, rnd, false);
if (r < 0)
return log_error_errno(r, "Failed to write boot id: %m");
etc_machine_id = prefix_roota(directory, "/etc/machine-id");
- r = id128_read(etc_machine_id, ID128_PLAIN_OR_UNINIT, &id);
+ r = id128_read(etc_machine_id, ID128_FORMAT_PLAIN, &id);
if (r < 0) {
- if (!IN_SET(r, -ENOENT, -ENOMEDIUM)) /* If the file is missing or empty, we don't mind */
+ if (!IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* 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");
if (sd_id128_is_null(arg_uuid)) {
else if (fd < 0)
return log_error_errno(fd, "Failed to determine machine ID of image: %m");
else {
- r = id128_read_fd(fd, ID128_PLAIN_OR_UNINIT, &context->seed);
- if (r == -ENOMEDIUM)
+ r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &context->seed);
+ if (IN_SET(r, -ENOMEDIUM, -ENOPKG))
log_info("No machine ID set, using randomized partition UUIDs.");
else if (r < 0)
return log_error_errno(r, "Failed to parse machine ID of image: %m");
if (fd < 0)
log_debug_errno(errno, "Failed to open %s: %m", path);
else {
- r = id128_read_fd(fd, ID128_PLAIN, &machine_id);
+ 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);
}
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_PLAIN, ret) >= 0) {
+ if (id128_read_fd(fd, ID128_FORMAT_PLAIN, ret) >= 0) {
log_info("Initializing machine ID from D-Bus machine ID.");
return 0;
}
if (sd_id128_is_null(machine_id)) {
/* Try to read any existing machine ID */
- if (id128_read_fd(fd, ID128_PLAIN, ret) >= 0)
+ if (id128_read_fd(fd, ID128_FORMAT_PLAIN, ret) >= 0)
return 0;
/* Hmm, so, the id currently stored is not useful, then let's generate one */
if (r < 0)
return log_error_errno(r, "Failed to sync %s: %m", etc_machine_id);
} else {
- r = id128_write_fd(fd, ID128_PLAIN, machine_id, true);
+ r = id128_write_fd(fd, ID128_FORMAT_PLAIN, machine_id, true);
if (r < 0)
return log_error_errno(r, "Failed to write %s: %m", etc_machine_id);
else
run_machine_id = prefix_roota(root, "/run/machine-id");
RUN_WITH_UMASK(0022)
- r = id128_write(run_machine_id, ID128_PLAIN, machine_id, false);
+ r = id128_write(run_machine_id, ID128_FORMAT_PLAIN, machine_id, false);
if (r < 0) {
(void) unlink(run_machine_id);
return log_error_errno(r, "Cannot write %s: %m", run_machine_id);
"%s is not on a temporary file system.",
etc_machine_id);
- r = id128_read_fd(fd, ID128_PLAIN, &id);
+ r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
if (r < 0)
return log_error_errno(r, "We didn't find a valid machine ID in %s: %m", etc_machine_id);
return r;
/* Update a persistent version of etc_machine_id */
- r = id128_write(etc_machine_id, ID128_PLAIN, id, true);
+ r = id128_write(etc_machine_id, ID128_FORMAT_PLAIN, id, true);
if (r < 0)
return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
/* Translate error for missing os-release file to EUNATCH. */
return fd == -ENOENT ? -EUNATCH : fd;
- r = id128_read_fd(fd, ID128_PLAIN, &id);
+ r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
} else
r = sd_id128_get_machine(&id);
if (r < 0)
int r;
r = sd_id128_get_machine(&id);
- if (IN_SET(r, -ENOENT, -ENOMEDIUM))
+ if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG))
return (void) log_tests_skipped("/etc/machine-id missing");
assert_se(r >= 0);
assert_se(fd >= 0);
safe_close(pfd);
- assert_se(id128_read_fd(fd, ID128_PLAIN, &a) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &a) >= 0);
assert_se(sd_id128_get_machine(&b) >= 0);
assert_se(sd_id128_equal(a, b));
}
/* First, write as UUID */
assert_se(sd_id128_randomize(&id) >= 0);
- assert_se(id128_write_fd(fd, ID128_UUID, id, false) >= 0);
+ assert_se(id128_write_fd(fd, ID128_FORMAT_UUID, id, false) >= 0);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &id2) == -EINVAL);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_UUID, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_ANY, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
/* Second, write as plain */
assert_se(ftruncate(fd, 0) >= 0);
assert_se(sd_id128_randomize(&id) >= 0);
- assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0);
+ assert_se(id128_write_fd(fd, ID128_FORMAT_PLAIN, id, false) >= 0);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_UUID, &id2) == -EINVAL);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_ANY, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
/* Third, write plain without trailing newline */
assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_UUID, &id2) == -EINVAL);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
- /* Third, write UUID without trailing newline */
+ /* Fourth, write UUID without trailing newline */
assert_se(lseek(fd, 0, SEEK_SET) == 0);
assert_se(ftruncate(fd, 0) >= 0);
assert_se(write(fd, sd_id128_to_uuid_string(id, q), 36) == 36);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &id2) == -EINVAL);
assert_se(lseek(fd, 0, SEEK_SET) == 0);
- assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
+ assert_se(id128_read_fd(fd, ID128_FORMAT_UUID, &id2) >= 0);
assert_se(sd_id128_equal(id, id2));
if (sd_booted() > 0 && access("/etc/machine-id", F_OK) >= 0) {