]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
id128: introduce ERRNO_IS_MACHINE_ID_UNSET() helper macro
authorLennart Poettering <lennart@poettering.net>
Wed, 1 Feb 2023 11:08:25 +0000 (12:08 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 1 Feb 2023 14:25:30 +0000 (15:25 +0100)
src/boot/bootctl-install.c
src/libsystemd/sd-id128/id128-util.h
src/libsystemd/sd-journal/journal-file.c
src/nspawn/nspawn.c
src/partition/repart.c
src/test/test-condition.c

index 1dee592d77f1b67832eb94e9c1c1980732eb8287..c382d2a84f994f6b3320fe220dcdcbf135da0e39 100644 (file)
@@ -6,30 +6,33 @@
 #include "bootctl-util.h"
 #include "chase-symlinks.h"
 #include "copy.h"
+#include "dirent-util.h"
+#include "efi-api.h"
 #include "env-file.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
 #include "glyph-util.h"
+#include "id128-util.h"
 #include "os-util.h"
 #include "path-util.h"
+#include "rm-rf.h"
 #include "stat-util.h"
 #include "sync-util.h"
 #include "tmpfile-util.h"
 #include "umask-util.h"
 #include "utf8.h"
-#include "dirent-util.h"
-#include "efi-api.h"
-#include "rm-rf.h"
 
 static int load_etc_machine_id(void) {
         int r;
 
         r = sd_id128_get_machine(&arg_machine_id);
-        if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* Not set or empty */
-                return 0;
-        if (r < 0)
+        if (r < 0) {
+                if (ERRNO_IS_MACHINE_ID_UNSET(r)) /* Not set or empty */
+                        return 0;
+
                 return log_error_errno(r, "Failed to get machine-id: %m");
+        }
 
         log_debug("Loaded machine ID %s from /etc/machine-id.", SD_ID128_TO_STRING(arg_machine_id));
         return 0;
index 887f443d6939cfe098fc57d5e1d9fae8f55552bf..e094de64419406c11a3b6226f3a8ee8ca0378741 100644 (file)
@@ -32,3 +32,10 @@ extern const struct hash_ops id128_hash_ops_free;
 sd_id128_t id128_make_v4_uuid(sd_id128_t id);
 
 int id128_get_product(sd_id128_t *ret);
+
+/* A helper to check for the three relevant cases of "machine ID not initialized" */
+#define ERRNO_IS_MACHINE_ID_UNSET(r)            \
+        IN_SET(abs(r),                          \
+               ENOENT,                          \
+               ENOMEDIUM,                       \
+               ENOPKG)
index a2111b69671ff852030409b87d633b623f9b0aa9..2ead29548b334f574f76e843ddf6cd2c47f0e2f8 100644 (file)
@@ -20,6 +20,7 @@
 #include "fd-util.h"
 #include "format-util.h"
 #include "fs-util.h"
+#include "id128-util.h"
 #include "journal-authenticate.h"
 #include "journal-def.h"
 #include "journal-file.h"
@@ -388,11 +389,13 @@ static int journal_file_refresh_header(JournalFile *f) {
         assert(f->header);
 
         r = sd_id128_get_machine(&f->header->machine_id);
-        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;
+        if (r < 0) {
+                if (!ERRNO_IS_MACHINE_ID_UNSET(r))
+                        return r;
+
+                /* don't have a machine-id, let's continue without */
+                f->header->machine_id = SD_ID128_NULL;
+        }
 
         r = sd_id128_get_boot(&f->header->boot_id);
         if (r < 0)
index f16d9506eb398cf976ebe5bff86d0c29a9b30591..3567d034e7b7eb515ab3ab2afdaf3229943fb20c 100644 (file)
@@ -2841,7 +2841,7 @@ static int setup_machine_id(const char *directory) {
 
         r = id128_read(etc_machine_id, ID128_FORMAT_PLAIN, &id);
         if (r < 0) {
-                if (!IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* If the file is missing, empty, or uninitialized, we don't mind */
+                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");
 
                 if (sd_id128_is_null(arg_uuid)) {
index 920b442316a014227c8813bd685f3815b13e4f4b..73712a36eb677dfc10da65c4d52ff9d523ab60c7 100644 (file)
@@ -4789,10 +4789,12 @@ static int context_read_seed(Context *context, const char *root) {
                         return log_error_errno(fd, "Failed to determine machine ID of image: %m");
                 else {
                         r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &context->seed);
-                        if (IN_SET(r, -ENOMEDIUM, -ENOPKG))
+                        if (r < 0) {
+                                if (!ERRNO_IS_MACHINE_ID_UNSET(r))
+                                        return log_error_errno(r, "Failed to parse machine ID of image: %m");
+
                                 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");
+                        }
 
                         return 0;
                 }
index 901d80eb3d54bc2868982be37e08fe66a1c3755a..8f52dab8ce8af74313fdeab96e2fb5a334e0970e 100644 (file)
@@ -250,7 +250,7 @@ TEST(condition_test_host) {
         int r;
 
         r = sd_id128_get_machine(&id);
-        if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG))
+        if (r < 0 && ERRNO_IS_MACHINE_ID_UNSET(r))
                 return (void) log_tests_skipped("/etc/machine-id missing");
         assert_se(r >= 0);