]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-device/device-private.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-private.c
index 2add6bb0de03e483876434ff8d3bac3e884b2bf8..91690d83f68f93006fdf781bde47d82ec0ad553c 100644 (file)
 ***/
 
 #include <ctype.h>
-#include <sys/types.h>
 #include <net/if.h>
-
-#include "util.h"
-#include "macro.h"
-#include "refcnt.h"
-#include "path-util.h"
-#include "strxcpyx.h"
-#include "fileio.h"
-#include "hashmap.h"
-#include "set.h"
-#include "strv.h"
-#include "mkdir.h"
+#include <sys/types.h>
 
 #include "sd-device.h"
 
-#include "device-util.h"
 #include "device-internal.h"
 #include "device-private.h"
+#include "device-util.h"
+#include "fd-util.h"
+#include "fileio.h"
+#include "hashmap.h"
+#include "macro.h"
+#include "mkdir.h"
+#include "parse-util.h"
+#include "path-util.h"
+#include "refcnt.h"
+#include "set.h"
+#include "string-util.h"
+#include "strv.h"
+#include "strxcpyx.h"
+#include "util.h"
 
 int device_add_property(sd_device *device, const char *key, const char *value) {
         int r;
@@ -200,10 +202,8 @@ static int device_read_db(sd_device *device) {
         if (r < 0) {
                 if (r == -ENOENT)
                         return 0;
-                else {
-                        log_debug("sd-device: failed to read db '%s': %s", path, strerror(-r));
-                        return r;
-                }
+                else
+                        return log_debug_errno(r, "sd-device: failed to read db '%s': %m", path);
         }
 
         /* devices with a database entry are initialized */
@@ -247,7 +247,7 @@ static int device_read_db(sd_device *device) {
                                 db[i] = '\0';
                                 r = handle_db_line(device, key, value);
                                 if (r < 0)
-                                        log_debug("sd-device: failed to handle db entry '%c:%s': %s", key, value, strerror(-r));
+                                        log_debug_errno(r, "sd-device: failed to handle db entry '%c:%s': %m", key, value);
 
                                 state = PRE_KEY;
                         }
@@ -366,7 +366,7 @@ static int device_set_devgid(sd_device *device, const char *gid) {
         return 0;
 }
 
-static int device_ammend(sd_device *device, const char *key, const char *value) {
+static int device_amend(sd_device *device, const char *key, const char *value) {
         int r;
 
         assert(device);
@@ -419,32 +419,32 @@ static int device_ammend(sd_device *device, const char *key, const char *value)
                 if (r < 0)
                         return log_debug_errno(r, "sd-device: could not set devgid to '%s': %m", value);
         } else if (streq(key, "DEVLINKS")) {
-                char *devlinks, *next;
+                const char *word, *state;
+                size_t l;
 
-                devlinks = strdupa(value);
+                FOREACH_WORD(word, l, value, state) {
+                        char devlink[l + 1];
 
-                while ((next = strchr(devlinks, ' '))) {
-                        next[0] = '\0';
+                        strncpy(devlink, word, l);
+                        devlink[l] = '\0';
 
-                        r = device_add_devlink(device, devlinks);
+                        r = device_add_devlink(device, devlink);
                         if (r < 0)
-                                return log_debug_errno(r, "sd-device: could not add devlink '%s': %m", devlinks);
-
-                        devlinks = next + 1;
+                                return log_debug_errno(r, "sd-device: could not add devlink '%s': %m", devlink);
                 }
         } else if (streq(key, "TAGS")) {
-                char *tags, *next;
+                const char *word, *state;
+                size_t l;
 
-                tags = strdupa(value);
+                FOREACH_WORD_SEPARATOR(word, l, value, ":", state) {
+                        char tag[l + 1];
 
-                while ((next = strchr(tags, ':'))) {
-                        next[0] = '\0';
+                        (void)strncpy(tag, word, l);
+                        tag[l] = '\0';
 
-                        r = device_add_tag(device, tags);
+                        r = device_add_tag(device, tag);
                         if (r < 0)
-                                return log_debug_errno(r, "sd-device: could not add tag '%s': %m", tags);
-
-                        tags = next + 1;
+                                return log_debug_errno(r, "sd-device: could not add tag '%s': %m", tag);
                 }
         } else {
                 r = device_add_property_internal(device, key, value);
@@ -509,7 +509,7 @@ static int device_append(sd_device *device, char *key, const char **_major, cons
                                 return -EINVAL;
                 }
 
-                r = device_ammend(device, key, value);
+                r = device_amend(device, key, value);
                 if (r < 0)
                         return r;
         }
@@ -636,10 +636,10 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) {
 
 static int device_update_properties_bufs(sd_device *device) {
         const char *val, *prop;
-        char **buf_strv = NULL;
-        uint8_t *buf_nulstr = NULL;
-        size_t allocated_nulstr = 0, allocated_strv = 0;
-        size_t nulstr_len = 0, strv_size = 0;
+        _cleanup_free_ char **buf_strv = NULL;
+        _cleanup_free_ uint8_t *buf_nulstr = NULL;
+        size_t allocated_nulstr = 0;
+        size_t nulstr_len = 0, num = 0, i = 0;
 
         assert(device);
 
@@ -655,20 +655,29 @@ static int device_update_properties_bufs(sd_device *device) {
                 if (!buf_nulstr)
                         return -ENOMEM;
 
-                buf_strv = GREEDY_REALLOC0(buf_strv, allocated_strv, strv_size + 2);
-                if (!buf_strv)
-                        return -ENOMEM;
-
-                buf_strv[++ strv_size] = (char *)&buf_nulstr[nulstr_len];
                 strscpyl((char *)buf_nulstr + nulstr_len, len + 1, prop, "=", val, NULL);
                 nulstr_len += len + 1;
+                ++num;
+        }
+
+        /* build buf_strv from buf_nulstr */
+        buf_strv = new0(char *, num + 1);
+        if (!buf_strv)
+                return -ENOMEM;
+
+        NULSTR_FOREACH(val, (char*) buf_nulstr) {
+                buf_strv[i] = (char *) val;
+                assert(i < num);
+                i++;
         }
 
         free(device->properties_nulstr);
-        free(device->properties_strv);
         device->properties_nulstr = buf_nulstr;
+        buf_nulstr = NULL;
         device->properties_nulstr_len = nulstr_len;
+        free(device->properties_strv);
         device->properties_strv = buf_strv;
+        buf_strv = NULL;
 
         device->properties_buf_outdated = false;
 
@@ -1073,12 +1082,10 @@ int device_update_db(sd_device *device) {
         return 0;
 
 fail:
-        log_error_errno(r, "failed to create %s file '%s' for '%s'", has_info ? "db" : "empty",
-                        path, device->devpath);
-        unlink(path);
-        unlink(path_tmp);
+        (void) unlink(path);
+        (void) unlink(path_tmp);
 
-        return r;
+        return log_error_errno(r, "failed to create %s file '%s' for '%s'", has_info ? "db" : "empty", path, device->devpath);
 }
 
 int device_delete_db(sd_device *device) {
@@ -1100,3 +1107,9 @@ int device_delete_db(sd_device *device) {
 
         return 0;
 }
+
+int device_read_db_force(sd_device *device) {
+        assert(device);
+
+        return device_read_db_aux(device, true);
+}