summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-id128/sd-id128.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsystemd/sd-id128/sd-id128.c')
-rw-r--r--src/libsystemd/sd-id128/sd-id128.c49
1 files changed, 23 insertions, 26 deletions
diff --git a/src/libsystemd/sd-id128/sd-id128.c b/src/libsystemd/sd-id128/sd-id128.c
index 8f9801ae37..ec3a496dba 100644
--- a/src/libsystemd/sd-id128/sd-id128.c
+++ b/src/libsystemd/sd-id128/sd-id128.c
@@ -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;
}
@@ -206,22 +204,22 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
/* Chop off the final description string */
d = strrchr(description, ';');
if (!d)
- return -EIO;
+ return -EUCLEAN;
*d = 0;
/* Look for the permissions */
p = strrchr(description, ';');
if (!p)
- return -EIO;
+ return -EUCLEAN;
errno = 0;
perms = strtoul(p + 1, &e, 16);
if (errno > 0)
return -errno;
if (e == p + 1) /* Read at least one character */
- return -EIO;
+ return -EUCLEAN;
if (e != d) /* Must reached the end */
- return -EIO;
+ return -EUCLEAN;
if ((perms & ~MAX_PERMS) != 0)
return -EPERM;
@@ -231,7 +229,7 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
/* Look for the group ID */
g = strrchr(description, ';');
if (!g)
- return -EIO;
+ return -EUCLEAN;
r = parse_gid(g + 1, &gid);
if (r < 0)
return r;
@@ -242,7 +240,7 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
/* Look for the user ID */
u = strrchr(description, ';');
if (!u)
- return -EIO;
+ return -EUCLEAN;
r = parse_uid(u + 1, &uid);
if (r < 0)
return r;
@@ -253,13 +251,14 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
if (c < 0)
return -errno;
if (c != sizeof(sd_id128_t))
- return -EIO;
+ return -EUCLEAN;
return 0;
}
static int get_invocation_from_environment(sd_id128_t *ret) {
const char *e;
+ int r;
assert(ret);
@@ -267,33 +266,31 @@ static int get_invocation_from_environment(sd_id128_t *ret) {
if (!e)
return -ENXIO;
- return sd_id128_from_string(e, ret);
+ r = sd_id128_from_string(e, ret);
+ return r == -EINVAL ? -EUCLEAN : r;
}
_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;
+
+ if (sd_id128_is_null(saved_invocation_id))
+ return -ENOMEDIUM;
}
- *ret = saved_invocation_id;
+ if (ret)
+ *ret = saved_invocation_id;
return 0;
}