]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-device/sd-device.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / libsystemd / sd-device / sd-device.c
index 3179d16b213a45a5eb0308f644b90dceed4cbc03..0e7a26523d4a57ae1e0cd916d685db05ae830c00 100644 (file)
 ***/
 
 #include <ctype.h>
-#include <sys/types.h>
 #include <net/if.h>
+#include <sys/types.h>
 
-#include "util.h"
-#include "macro.h"
-#include "path-util.h"
-#include "strxcpyx.h"
+#include "sd-device.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 "parse-util.h"
+#include "path-util.h"
 #include "set.h"
+#include "string-util.h"
 #include "strv.h"
-
-#include "sd-device.h"
-
-#include "device-util.h"
-#include "device-private.h"
-#include "device-internal.h"
+#include "strxcpyx.h"
+#include "util.h"
 
 int device_new_aux(sd_device **ret) {
         _cleanup_device_unref_ sd_device *device = NULL;
@@ -158,16 +160,21 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
 
         if (verify) {
                 r = readlink_and_canonicalize(_syspath, &syspath);
-                if (r == -EINVAL) {
+                if (r == -ENOENT)
+                        /* the device does not exist (any more?) */
+                        return -ENODEV;
+                else if (r == -EINVAL) {
                         /* not a symlink */
                         syspath = canonicalize_file_name(_syspath);
                         if (!syspath) {
-                                log_debug("sd-device: could not canonicalize '%s': %m", _syspath);
-                                return -errno;
+                                if (errno == ENOENT)
+                                        /* the device does not exist (any more?) */
+                                        return -ENODEV;
+
+                                return log_debug_errno(errno, "sd-device: could not canonicalize '%s': %m", _syspath);
                         }
-                /* ignore errors due to the link not being a symlink */
-                } else if (r < 0 && r != -EINVAL) {
-                        log_debug("sd-device: could not get target of '%s': %s", _syspath, strerror(-r));
+                } else if (r < 0) {
+                        log_debug_errno(r, "sd-device: could not get target of '%s': %m", _syspath);
                         return r;
                 }
 
@@ -178,15 +185,17 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
                         path = strjoina(syspath, "/uevent");
                         r = access(path, F_OK);
                         if (r < 0) {
+                                if (errno == ENOENT)
+                                        /* this is not a valid device */
+                                        return -ENODEV;
+
                                 log_debug("sd-device: %s does not have an uevent file: %m", syspath);
                                 return -errno;
                         }
                 } else {
                         /* everything else just just needs to be a directory */
-                        if (!is_dir(syspath, false)) {
-                                log_debug("sd-device: %s is not a directory", syspath);
-                                return -EINVAL;
-                        }
+                        if (!is_dir(syspath, false))
+                                return -ENODEV;
                 }
         } else {
                 syspath = strdup(_syspath);
@@ -288,20 +297,32 @@ _public_ int sd_device_new_from_subsystem_sysname(sd_device **ret, const char *s
                 } else
                         return -EINVAL;
         } else {
-                syspath = strjoina("/sys/subsystem/", subsystem, "/devices/", sysname);
+                char *name;
+                size_t len = 0;
+
+                /* translate sysname back to sysfs filename */
+                name = strdupa(sysname);
+                while (name[len] != '\0') {
+                        if (name[len] == '/')
+                                name[len] = '!';
+
+                        len ++;
+                }
+
+                syspath = strjoina("/sys/subsystem/", subsystem, "/devices/", name);
                 if (access(syspath, F_OK) >= 0)
                         return sd_device_new_from_syspath(ret, syspath);
 
-                syspath = strjoina("/sys/bus/", subsystem, "/devices/", sysname);
+                syspath = strjoina("/sys/bus/", subsystem, "/devices/", name);
                 if (access(syspath, F_OK) >= 0)
                         return sd_device_new_from_syspath(ret, syspath);
 
-                syspath = strjoina("/sys/class/", subsystem, "/", sysname);
+                syspath = strjoina("/sys/class/", subsystem, "/", name);
                 if (access(syspath, F_OK) >= 0)
                         return sd_device_new_from_syspath(ret, syspath);
         }
 
-        return -ENOENT;
+        return -ENODEV;
 }
 
 int device_set_devtype(sd_device *device, const char *_devtype) {
@@ -492,6 +513,8 @@ int device_read_uevent_file(sd_device *device) {
         if (device->uevent_loaded || device->sealed)
                 return 0;
 
+        device->uevent_loaded = true;
+
         r = sd_device_get_syspath(device, &syspath);
         if (r < 0)
                 return r;
@@ -499,8 +522,14 @@ int device_read_uevent_file(sd_device *device) {
         path = strjoina(syspath, "/uevent");
 
         r = read_full_file(path, &uevent, &uevent_len);
-        if (r < 0) {
-                log_debug("sd-device: failed to read uevent file '%s': %s", path, strerror(-r));
+        if (r == -EACCES)
+                /* empty uevent files may be write-only */
+                return 0;
+        else if (r == -ENOENT)
+                /* some devices may not have uevent files, see set_syspath() */
+                return 0;
+        else if (r < 0) {
+                log_debug_errno(r, "sd-device: failed to read uevent file '%s': %m", path);
                 return r;
         }
 
@@ -539,7 +568,7 @@ int device_read_uevent_file(sd_device *device) {
 
                                 r = handle_uevent_line(device, key, value, &major, &minor);
                                 if (r < 0)
-                                        log_debug("sd-device: failed to handle uevent entry '%s=%s': %s", key, value, strerror(-r));
+                                        log_debug_errno(r, "sd-device: failed to handle uevent entry '%s=%s': %m", key, value);
 
                                 state = PRE_KEY;
                         }
@@ -553,11 +582,9 @@ int device_read_uevent_file(sd_device *device) {
         if (major) {
                 r = device_set_devnum(device, major, minor);
                 if (r < 0)
-                        log_debug("sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %s", major, minor, path, strerror(-r));
+                        log_debug_errno(r, "sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %m", major, minor, path);
         }
 
-        device->uevent_loaded = true;
-
         return 0;
 }
 
@@ -624,7 +651,7 @@ _public_ int sd_device_new_from_device_id(sd_device **ret, const char *id) {
                 if (r < 0)
                         return r;
 
-                /* this si racey, so we might end up with the wrong device */
+                /* this is racey, so we might end up with the wrong device */
                 if (ifr.ifr_ifindex != ifindex)
                         return -ENODEV;
 
@@ -697,7 +724,7 @@ static int device_new_from_child(sd_device **ret, sd_device *child) {
                 return 0;
         }
 
-        return -ENOENT;
+        return -ENODEV;
 }
 
 _public_ int sd_device_get_parent(sd_device *child, sd_device **ret) {
@@ -769,14 +796,17 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
                         r = device_set_subsystem(device, "drivers");
                 else if (path_startswith(device->devpath, "/subsystem/") ||
                          path_startswith(device->devpath, "/class/") ||
-                         path_startswith(device->devpath, "/buss/"))
+                         path_startswith(device->devpath, "/bus/"))
                         r = device_set_subsystem(device, "subsystem");
-                if (r < 0)
-                        return r;
+                if (r < 0 && r != -ENOENT)
+                        return log_debug_errno(r, "sd-device: could not set subsystem for %s: %m", device->devpath);
 
                 device->subsystem_set = true;
         }
 
+        if (!device->subsystem)
+                return -ENOENT;
+
         *ret = device->subsystem;
 
         return 0;
@@ -887,10 +917,16 @@ _public_ int sd_device_get_driver(sd_device *device, const char **ret) {
                 if (r >= 0) {
                         r = device_set_driver(device, driver);
                         if (r < 0)
-                                return r;
-                }
+                                return log_debug_errno(r, "sd-device: could not set driver for %s: %m", device->devpath);
+                } else if (r == -ENOENT)
+                        device->driver_set = true;
+                else
+                        return log_debug_errno(r, "sd-device: could not set driver for %s: %m", device->devpath);
         }
 
+        if (!device->driver)
+                return -ENOENT;
+
         *ret = device->driver;
 
         return 0;
@@ -985,6 +1021,8 @@ _public_ int sd_device_get_sysname(sd_device *device, const char **ret) {
                         return r;
         }
 
+        assert_return(device->sysname, -ENOENT);
+
         *ret = device->sysname;
 
         return 0;
@@ -1174,17 +1212,19 @@ int device_get_id_filename(sd_device *device, const char **ret) {
                         return r;
 
                 if (major(devnum) > 0) {
+                        assert(subsystem);
+
                         /* use dev_t -- b259:131072, c254:0 */
                         r = asprintf(&id, "%c%u:%u",
                                      streq(subsystem, "block") ? 'b' : 'c',
                                      major(devnum), minor(devnum));
                         if (r < 0)
-                                return -errno;
+                                return -ENOMEM;
                 } else if (ifindex > 0) {
                         /* use netdev ifindex -- n3 */
                         r = asprintf(&id, "n%u", ifindex);
                         if (r < 0)
-                                return -errno;
+                                return -ENOMEM;
                 } else {
                         /* use $subsys:$sysname -- pci:0000:00:1f.2
                          * sysname() has '!' translated, get it from devpath
@@ -1195,9 +1235,12 @@ int device_get_id_filename(sd_device *device, const char **ret) {
                         if (!sysname)
                                 return -EINVAL;
 
+                        if (!subsystem)
+                                return -EINVAL;
+
                         r = asprintf(&id, "+%s:%s", subsystem, sysname);
                         if (r < 0)
-                                return -errno;
+                                return -ENOMEM;
                 }
 
                 device->id_filename = id;
@@ -1209,7 +1252,7 @@ int device_get_id_filename(sd_device *device, const char **ret) {
         return 0;
 }
 
-static int device_read_db(sd_device *device) {
+int device_read_db_aux(sd_device *device, bool force) {
         _cleanup_free_ char *db = NULL;
         char *path;
         const char *id, *value;
@@ -1226,9 +1269,11 @@ static int device_read_db(sd_device *device) {
                 INVALID_LINE,
         } state = PRE_KEY;
 
-        if (device->db_loaded || device->sealed)
+        if (device->db_loaded || (!force && device->sealed))
                 return 0;
 
+        device->db_loaded = true;
+
         r = device_get_id_filename(device, &id);
         if (r < 0)
                 return r;
@@ -1239,14 +1284,12 @@ 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 */
-        device->is_initialized = true;;
+        device->is_initialized = true;
 
         for (i = 0; i < db_len; i++) {
                 switch (state) {
@@ -1286,7 +1329,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;
                         }
@@ -1297,11 +1340,13 @@ static int device_read_db(sd_device *device) {
                 }
         }
 
-        device->db_loaded = true;
-
         return 0;
 }
 
+static int device_read_db(sd_device *device) {
+        return device_read_db_aux(device, false);
+}
+
 _public_ int sd_device_get_is_initialized(sd_device *device, int *initialized) {
         int r;
 
@@ -1345,6 +1390,8 @@ _public_ int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *u
 }
 
 _public_ const char *sd_device_get_tag_first(sd_device *device) {
+        void *v;
+
         assert_return(device, NULL);
 
         (void) device_read_db(device);
@@ -1352,10 +1399,13 @@ _public_ const char *sd_device_get_tag_first(sd_device *device) {
         device->tags_iterator_generation = device->tags_generation;
         device->tags_iterator = ITERATOR_FIRST;
 
-        return set_iterate(device->tags, &device->tags_iterator);
+        set_iterate(device->tags, &device->tags_iterator, &v);
+        return v;
 }
 
 _public_ const char *sd_device_get_tag_next(sd_device *device) {
+        void *v;
+
         assert_return(device, NULL);
 
         (void) device_read_db(device);
@@ -1363,10 +1413,13 @@ _public_ const char *sd_device_get_tag_next(sd_device *device) {
         if (device->tags_iterator_generation != device->tags_generation)
                 return NULL;
 
-        return set_iterate(device->tags, &device->tags_iterator);
+        set_iterate(device->tags, &device->tags_iterator, &v);
+        return v;
 }
 
 _public_ const char *sd_device_get_devlink_first(sd_device *device) {
+        void *v;
+
         assert_return(device, NULL);
 
         (void) device_read_db(device);
@@ -1374,10 +1427,13 @@ _public_ const char *sd_device_get_devlink_first(sd_device *device) {
         device->devlinks_iterator_generation = device->devlinks_generation;
         device->devlinks_iterator = ITERATOR_FIRST;
 
-        return set_iterate(device->devlinks, &device->devlinks_iterator);
+        set_iterate(device->devlinks, &device->devlinks_iterator, &v);
+        return v;
 }
 
 _public_ const char *sd_device_get_devlink_next(sd_device *device) {
+        void *v;
+
         assert_return(device, NULL);
 
         (void) device_read_db(device);
@@ -1385,7 +1441,8 @@ _public_ const char *sd_device_get_devlink_next(sd_device *device) {
         if (device->devlinks_iterator_generation != device->devlinks_generation)
                 return NULL;
 
-        return set_iterate(device->devlinks, &device->devlinks_iterator);
+        set_iterate(device->devlinks, &device->devlinks_iterator, &v);
+        return v;
 }
 
 static int device_properties_prepare(sd_device *device) {
@@ -1456,7 +1513,7 @@ _public_ const char *sd_device_get_property_first(sd_device *device, const char
         device->properties_iterator_generation = device->properties_generation;
         device->properties_iterator = ITERATOR_FIRST;
 
-        value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key);
+        ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
 
         if (_value)
                 *_value = value;
@@ -1478,7 +1535,7 @@ _public_ const char *sd_device_get_property_next(sd_device *device, const char *
         if (device->properties_iterator_generation != device->properties_generation)
                 return NULL;
 
-        value = ordered_hashmap_iterate(device->properties, &device->properties_iterator, (const void**)&key);
+        ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
 
         if (_value)
                 *_value = value;
@@ -1536,6 +1593,7 @@ static int device_sysattrs_read_all(sd_device *device) {
 }
 
 _public_ const char *sd_device_get_sysattr_first(sd_device *device) {
+        void *v;
         int r;
 
         assert_return(device, NULL);
@@ -1550,16 +1608,20 @@ _public_ const char *sd_device_get_sysattr_first(sd_device *device) {
 
         device->sysattrs_iterator = ITERATOR_FIRST;
 
-        return set_iterate(device->sysattrs, &device->sysattrs_iterator);
+        set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
+        return v;
 }
 
 _public_ const char *sd_device_get_sysattr_next(sd_device *device) {
+        void *v;
+
         assert_return(device, NULL);
 
         if (!device->sysattrs_read)
                 return NULL;
 
-        return set_iterate(device->sysattrs, &device->sysattrs_iterator);
+        set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
+        return v;
 }
 
 _public_ int sd_device_has_tag(sd_device *device, const char *tag) {
@@ -1593,9 +1655,9 @@ _public_ int sd_device_get_property_value(sd_device *device, const char *key, co
 }
 
 /* replaces the value if it already exists */
-static int device_add_sysattr_value(sd_device *device, const char *_key, const char *_value) {
+static int device_add_sysattr_value(sd_device *device, const char *_key, char *value) {
         _cleanup_free_ char *key = NULL;
-        _cleanup_free_ char *value = NULL;
+        _cleanup_free_ char *value_old = NULL;
         int r;
 
         assert(device);
@@ -1605,28 +1667,18 @@ static int device_add_sysattr_value(sd_device *device, const char *_key, const c
         if (r < 0)
                 return r;
 
-        value = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
+        value_old = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
         if (!key) {
                 key = strdup(_key);
                 if (!key)
                         return -ENOMEM;
         }
 
-        free(value);
-        value = NULL;
-
-        if (_value) {
-                value = strdup(_value);
-                if (!value)
-                        return -ENOMEM;
-        }
-
         r = hashmap_put(device->sysattr_values, key, value);
         if (r < 0)
                 return r;
 
         key = NULL;
-        value = NULL;
 
         return 0;
 }
@@ -1740,8 +1792,9 @@ static void device_remove_sysattr_value(sd_device *device, const char *_key) {
 
 /* set the attribute and save it in the cache. If a NULL value is passed the
  * attribute is cleared from the cache */
-_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *value) {
+_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *_value) {
         _cleanup_close_ int fd = -1;
+        _cleanup_free_ char *value = NULL;
         const char *syspath;
         char *path;
         struct stat statbuf;
@@ -1752,7 +1805,7 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
         assert_return(device, -EINVAL);
         assert_return(sysattr, -EINVAL);
 
-        if (!value) {
+        if (!_value) {
                 device_remove_sysattr_value(device, sysattr);
 
                 return 0;
@@ -1765,7 +1818,11 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
         path = strjoina(syspath, "/", sysattr);
         r = lstat(path, &statbuf);
         if (r < 0) {
-                r = device_add_sysattr_value(device, sysattr, "");
+                value = strdup("");
+                if (!value)
+                        return -ENOMEM;
+
+                r = device_add_sysattr_value(device, sysattr, value);
                 if (r < 0)
                         return r;
 
@@ -1783,11 +1840,11 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
         if ((statbuf.st_mode & S_IRUSR) == 0)
                 return -EACCES;
 
-        value_len = strlen(value);
+        value_len = strlen(_value);
 
         /* drop trailing newlines */
-        while (value_len > 0 && value[--value_len] == '\n')
-                value[value_len] = '\0';
+        while (value_len > 0 && _value[value_len - 1] == '\n')
+                _value[--value_len] = '\0';
 
         /* value length is limited to 4k */
         if (value_len > 4096)
@@ -1797,6 +1854,10 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
         if (fd < 0)
                 return -errno;
 
+        value = strdup(_value);
+        if (!value)
+                return -ENOMEM;
+
         size = write(fd, value, value_len);
         if (size < 0)
                 return -errno;
@@ -1808,5 +1869,7 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
         if (r < 0)
                 return r;
 
+        value = NULL;
+
         return 0;
 }