diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-12-08 14:30:31 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-12-12 21:57:31 +0900 |
commit | 057bf780e9d45480fbacdd3b060dbe37b37f9693 (patch) | |
tree | 4d610e28b18e2fd36ff68c024619398c3e68bc68 /src/libsystemd/sd-id128 | |
parent | 28bf2de201e890193b57accbf736c7d3d82d813a (diff) | |
download | systemd-057bf780e9d45480fbacdd3b060dbe37b37f9693.tar.gz |
sd-id128: make id128_read() or friends return -ENOPKG when the file contents is "uninitialized"
Then, this drops ID128_PLAIN_OR_UNINIT. Also, this renames
Id128Format -> Id128FormatFlag, and make it bitfield.
Fixes #25634.
Diffstat (limited to 'src/libsystemd/sd-id128')
-rw-r--r-- | src/libsystemd/sd-id128/id128-util.c | 35 | ||||
-rw-r--r-- | src/libsystemd/sd-id128/id128-util.h | 27 | ||||
-rw-r--r-- | src/libsystemd/sd-id128/sd-id128.c | 4 |
3 files changed, 32 insertions, 34 deletions
diff --git a/src/libsystemd/sd-id128/id128-util.c b/src/libsystemd/sd-id128/id128-util.c index 1085486ae2..2cf8848981 100644 --- a/src/libsystemd/sd-id128/id128-util.c +++ b/src/libsystemd/sd-id128/id128-util.c @@ -40,17 +40,21 @@ bool id128_is_valid(const char *s) { 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) @@ -60,10 +64,9 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) { 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') @@ -71,7 +74,7 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) { _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; @@ -83,7 +86,7 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) { _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; @@ -96,7 +99,7 @@ int id128_read_fd(int fd, Id128Format f, sd_id128_t *ret) { 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); @@ -106,15 +109,15 @@ int id128_read(const char *p, Id128Format f, sd_id128_t *ret) { 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 { @@ -136,7 +139,7 @@ int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync) { 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); @@ -178,9 +181,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_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; diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 17b180c10c..bb237cb6cc 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -10,22 +10,17 @@ 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_; diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c index 922b94c5ed..a5758e5841 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) { 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; @@ -145,7 +145,7 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) { 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; } |