]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-id128: allow sd_id128_get_machine() and friend to be called with NULL
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 Dec 2022 04:34:15 +0000 (13:34 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 Dec 2022 15:07:17 +0000 (00:07 +0900)
It may be useful to check if the machine ID or friends is set or not.

src/libsystemd/sd-id128/sd-id128.c

index 146c379398d4c1487e744c6c11c106c1d3a129ee..9fac1bb3fc35ad949834147395690e2eabaec12d 100644 (file)
@@ -124,8 +124,6 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
         static thread_local sd_id128_t saved_machine_id = {};
         int r;
 
-        assert_return(ret, -EINVAL);
-
         if (sd_id128_is_null(saved_machine_id)) {
                 r = id128_read("/etc/machine-id", ID128_FORMAT_PLAIN, &saved_machine_id);
                 if (r < 0)
@@ -135,7 +133,8 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
                         return -ENOMEDIUM;
         }
 
-        *ret = saved_machine_id;
+        if (ret)
+                *ret = saved_machine_id;
         return 0;
 }
 
@@ -143,8 +142,6 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) {
         static thread_local sd_id128_t saved_boot_id = {};
         int r;
 
-        assert_return(ret, -EINVAL);
-
         if (sd_id128_is_null(saved_boot_id)) {
                 r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID, &saved_boot_id);
                 if (r == -ENOENT && proc_mounted() == 0)
@@ -156,7 +153,8 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) {
                         return -ENOMEDIUM;
         }
 
-        *ret = saved_boot_id;
+        if (ret)
+                *ret = saved_boot_id;
         return 0;
 }
 
@@ -276,26 +274,20 @@ _public_ int sd_id128_get_invocation(sd_id128_t *ret) {
         static thread_local sd_id128_t saved_invocation_id = {};
         int r;
 
-        assert_return(ret, -EINVAL);
-
         if (sd_id128_is_null(saved_invocation_id)) {
                 /* We first check the environment. The environment variable is primarily relevant for user
                  * services, and sufficiently safe as long as no privilege boundary is involved. */
                 r = get_invocation_from_environment(&saved_invocation_id);
-                if (r >= 0) {
-                        *ret = saved_invocation_id;
-                        return 0;
-                } else if (r != -ENXIO)
-                        return r;
-
-                /* The kernel keyring is relevant for system services (as for user services we don't store
-                 * the invocation ID in the keyring, as there'd be no trust benefit in that). */
-                r = get_invocation_from_keyring(&saved_invocation_id);
+                if (r == -ENXIO)
+                        /* The kernel keyring is relevant for system services (as for user services we don't
+                         * store the invocation ID in the keyring, as there'd be no trust benefit in that). */
+                        r = get_invocation_from_keyring(&saved_invocation_id);
                 if (r < 0)
                         return r;
         }
 
-        *ret = saved_invocation_id;
+        if (ret)
+                *ret = saved_invocation_id;
         return 0;
 }