chiark / gitweb /
Introduce loop_read_exact helper
[elogind.git] / src / core / machine-id-setup.c
index 7e0aee6a75ba883b92f737033cd0f99730d1fdf6..623dffdea1d20c378481892b81488270fdc09925 100644 (file)
@@ -23,7 +23,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
-#include <stdlib.h>
 #include <fcntl.h>
 #include <sys/mount.h>
 
@@ -65,7 +64,6 @@ static int generate(char id[34], const char *root) {
         unsigned char *p;
         sd_id128_t buf;
         char  *q;
-        ssize_t k;
         const char *vm_id, *dbus_machine_id;
 
         assert(id);
@@ -73,16 +71,15 @@ static int generate(char id[34], const char *root) {
         if (isempty(root))
                 dbus_machine_id = "/var/lib/dbus/machine-id";
         else
-                dbus_machine_id = strappenda(root, "/var/lib/dbus/machine-id");
+                dbus_machine_id = strjoina(root, "/var/lib/dbus/machine-id");
 
         /* First, try reading the D-Bus machine id, unless it is a symlink */
         fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
         if (fd >= 0) {
-                k = loop_read(fd, id, 33, false);
+                r = loop_read_exact(fd, id, 33, false);
                 safe_close(fd);
 
-                if (k == 33 && id[32] == '\n') {
-
+                if (r >= 0 && id[32] == '\n') {
                         id[32] = 0;
                         if (id128_is_valid(id)) {
                                 id[32] = '\n';
@@ -120,14 +117,14 @@ static int generate(char id[34], const char *root) {
 
                         r = detect_vm(&vm_id);
                         if (r > 0 && streq(vm_id, "kvm")) {
-                                char uuid[37];
+                                char uuid[36];
 
                                 fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
                                 if (fd >= 0) {
-                                        k = loop_read(fd, uuid, 36, false);
+                                        r = loop_read_exact(fd, uuid, 36, false);
                                         safe_close(fd);
 
-                                        if (k >= 36) {
+                                        if (r >= 0) {
                                                 r = shorten_uuid(id, uuid);
                                                 if (r >= 0) {
                                                         log_info("Initializing machine ID from KVM UUID.");
@@ -163,7 +160,8 @@ static int get_valid_machine_id(int fd, char id[34]) {
         assert(fd >= 0);
         assert(id);
 
-        if (loop_read(fd, id_to_validate, 33, false) == 33 && id_to_validate[32] == '\n') {
+        if (loop_read_exact(fd, id_to_validate, 33, false) >= 0 &&
+            id_to_validate[32] == '\n') {
                 id_to_validate[32] = 0;
 
                 if (id128_is_valid(id_to_validate)) {
@@ -199,7 +197,7 @@ int machine_id_commit(const char *root) {
         else {
                 char *x;
 
-                x = strappenda(root, "/etc/machine-id");
+                x = strjoina(root, "/etc/machine-id");
                 etc_machine_id = path_kill_slashes(x);
         }
 
@@ -281,10 +279,10 @@ int machine_id_setup(const char *root) {
         } else {
                 char *x;
 
-                x = strappenda(root, "/etc/machine-id");
+                x = strjoina(root, "/etc/machine-id");
                 etc_machine_id = path_kill_slashes(x);
 
-                x = strappenda(root, "/run/machine-id");
+                x = strjoina(root, "/run/machine-id");
                 run_machine_id = path_kill_slashes(x);
         }