]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-id128/sd-id128.c
util-lib: split out IO related calls to io-util.[ch]
[thirdparty/systemd.git] / src / libsystemd / sd-id128 / sd-id128.c
index 9ee40ab91eaaee64a3f13dc61dc354bc8de26400..335ad3b1b4406b2049f9e05462a9176f568869a5 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 
-#include "util.h"
-#include "macro.h"
 #include "sd-id128.h"
 
-_public_ char *sd_id128_to_string(sd_id128_t id, char s[33]) {
+#include "fd-util.h"
+#include "io-util.h"
+#include "macro.h"
+#include "random-util.h"
+#include "util.h"
+
+_public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) {
         unsigned n;
 
         assert_return(s, NULL);
@@ -108,9 +112,9 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
         static thread_local bool saved_machine_id_valid = false;
         _cleanup_close_ int fd = -1;
         char buf[33];
-        ssize_t k;
         unsigned j;
         sd_id128_t t;
+        int r;
 
         assert_return(ret, -EINVAL);
 
@@ -123,13 +127,9 @@ _public_ int sd_id128_get_machine(sd_id128_t *ret) {
         if (fd < 0)
                 return -errno;
 
-        k = loop_read(fd, buf, 33, false);
-        if (k < 0)
-                return (int) k;
-
-        if (k != 33)
-                return -EIO;
-
+        r = loop_read_exact(fd, buf, 33, false);
+        if (r < 0)
+                return r;
         if (buf[32] !='\n')
                 return -EIO;
 
@@ -157,10 +157,10 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) {
         static thread_local bool saved_boot_id_valid = false;
         _cleanup_close_ int fd = -1;
         char buf[36];
-        ssize_t k;
         unsigned j;
         sd_id128_t t;
         char *p;
+        int r;
 
         assert_return(ret, -EINVAL);
 
@@ -173,21 +173,21 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) {
         if (fd < 0)
                 return -errno;
 
-        k = loop_read(fd, buf, 36, false);
-        if (k < 0)
-                return (int) k;
-
-        if (k != 36)
-                return -EIO;
+        r = loop_read_exact(fd, buf, 36, false);
+        if (r < 0)
+                return r;
 
         for (j = 0, p = buf; j < 16; j++) {
                 int a, b;
 
-                if (p >= buf + k)
+                if (p >= buf + 35)
                         return -EIO;
 
-                if (*p == '-')
+                if (*p == '-') {
                         p++;
+                        if (p >= buf + 35)
+                                return -EIO;
+                }
 
                 a = unhexchar(p[0]);
                 b = unhexchar(p[1]);
@@ -208,26 +208,18 @@ _public_ int sd_id128_get_boot(sd_id128_t *ret) {
 }
 
 _public_ int sd_id128_randomize(sd_id128_t *ret) {
-        _cleanup_close_ int fd = -1;
         sd_id128_t t;
-        ssize_t k;
+        int r;
 
         assert_return(ret, -EINVAL);
 
-        fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return -errno;
-
-        k = loop_read(fd, &t, 16, false);
-        if (k < 0)
-                return (int) k;
-
-        if (k != 16)
-                return -EIO;
+        r = dev_urandom(&t, sizeof(t));
+        if (r < 0)
+                return r;
 
         /* Turn this into a valid v4 UUID, to be nice. Note that we
          * only guarantee this for newly generated UUIDs, not for
-         * pre-existing ones.*/
+         * pre-existing ones. */
 
         *ret = make_v4_uuid(t);
         return 0;