]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-device/sd-device.c
coccinelle: make use of SYNTHETIC_ERRNO
[thirdparty/systemd.git] / src / libsystemd / sd-device / sd-device.c
index 1eb71e17dbfd8bf00d86ab13a484c91d2a131943..d8df1250feea0c58cfac74661ccc011fcbb2d461 100644 (file)
@@ -38,7 +38,6 @@ int device_new_aux(sd_device **ret) {
         *device = (sd_device) {
                 .n_ref = 1,
                 .watch_handle = -1,
-                .ifindex = -1,
                 .devmode = (mode_t) -1,
                 .devuid = (uid_t) -1,
                 .devgid = (gid_t) -1,
@@ -138,17 +137,17 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
         assert(_syspath);
 
         /* must be a subdirectory of /sys */
-        if (!path_startswith(_syspath, "/sys/")) {
-                log_debug("sd-device: syspath '%s' is not a subdirectory of /sys", _syspath);
-                return -EINVAL;
-        }
+        if (!path_startswith(_syspath, "/sys/"))
+                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+                                       "sd-device: Syspath '%s' is not a subdirectory of /sys",
+                                       _syspath);
 
         if (verify) {
                 r = chase_symlinks(_syspath, NULL, 0, &syspath);
                 if (r == -ENOENT)
                         return -ENODEV; /* the device does not exist (any more?) */
                 if (r < 0)
-                        return log_debug_errno(r, "sd-device: could not get target of '%s': %m", _syspath);
+                        return log_debug_errno(r, "sd-device: Failed to get target of '%s': %m", _syspath);
 
                 if (!path_startswith(syspath, "/sys")) {
                         _cleanup_free_ char *real_sys = NULL, *new_syspath = NULL;
@@ -157,17 +156,17 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
                         /* /sys is a symlink to somewhere sysfs is mounted on? In that case, we convert the path to real sysfs to "/sys". */
                         r = chase_symlinks("/sys", NULL, 0, &real_sys);
                         if (r < 0)
-                                return log_debug_errno(r, "sd-device: could not chase symlink /sys: %m");
+                                return log_debug_errno(r, "sd-device: Failed to chase symlink /sys: %m");
 
                         p = path_startswith(syspath, real_sys);
-                        if (!p) {
-                                log_debug("sd-device: canonicalized path '%s' does not starts with sysfs mount point '%s'", syspath, real_sys);
-                                return -ENODEV;
-                        }
+                        if (!p)
+                                return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
+                                                       "sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
+                                                       syspath, real_sys);
 
                         new_syspath = strjoin("/sys/", p);
                         if (!new_syspath)
-                                return log_oom();
+                                return -ENOMEM;
 
                         free_and_replace(syspath, new_syspath);
                         path_simplify(syspath, false);
@@ -514,7 +513,7 @@ int device_read_uevent_file(sd_device *device) {
                 /* some devices may not have uevent files, see set_syspath() */
                 return 0;
         else if (r < 0)
-                return log_debug_errno(r, "sd-device: failed to read uevent file '%s': %m", path);
+                return log_device_debug_errno(device, r, "sd-device: Failed to read uevent file '%s': %m", path);
 
         for (i = 0; i < uevent_len; i++)
                 switch (state) {
@@ -533,7 +532,7 @@ int device_read_uevent_file(sd_device *device) {
                                 state = PRE_VALUE;
                         } else if (strchr(NEWLINE, uevent[i])) {
                                 uevent[i] = '\0';
-                                log_debug("sd-device: ignoring invalid uevent line '%s'", key);
+                                log_device_debug(device, "sd-device: Invalid uevent line '%s', ignoring", key);
 
                                 state = PRE_KEY;
                         }
@@ -550,20 +549,20 @@ int device_read_uevent_file(sd_device *device) {
 
                                 r = handle_uevent_line(device, key, value, &major, &minor);
                                 if (r < 0)
-                                        log_debug_errno(r, "sd-device: failed to handle uevent entry '%s=%s': %m", key, value);
+                                        log_device_debug_errno(device, r, "sd-device: Failed to handle uevent entry '%s=%s', ignoring: %m", key, value);
 
                                 state = PRE_KEY;
                         }
 
                         break;
                 default:
-                        assert_not_reached("invalid state when parsing uevent file");
+                        assert_not_reached("Invalid state when parsing uevent file");
                 }
 
         if (major) {
                 r = device_set_devnum(device, major, minor);
                 if (r < 0)
-                        log_debug_errno(r, "sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %m", major, minor, path);
+                        log_device_debug_errno(device, r, "sd-device: Failed to set 'MAJOR=%s' or 'MINOR=%s' from '%s', ignoring: %m", major, minor, path);
         }
 
         return 0;
@@ -573,16 +572,16 @@ _public_ int sd_device_get_ifindex(sd_device *device, int *ifindex) {
         int r;
 
         assert_return(device, -EINVAL);
-        assert_return(ifindex, -EINVAL);
 
         r = device_read_uevent_file(device);
         if (r < 0)
                 return r;
 
-        if (device->ifindex < 0)
+        if (device->ifindex <= 0)
                 return -ENOENT;
 
-        *ifindex = device->ifindex;
+        if (ifindex)
+                *ifindex = device->ifindex;
 
         return 0;
 }
@@ -799,7 +798,7 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
                           path_startswith(device->devpath, "/bus/")))
                         r = device_set_subsystem(device, "subsystem");
                 if (r < 0 && r != -ENOENT)
-                        return log_debug_errno(r, "sd-device: could not set subsystem for %s: %m", device->devpath);
+                        return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem for %s: %m", device->devpath);
 
                 device->subsystem_set = true;
         } else if (!device->driver_subsystem_set)
@@ -822,7 +821,7 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
                                         r = device_set_drivers_subsystem(device, subsys + 1);
                         }
                         if (r < 0 && r != -ENOENT)
-                                return log_debug_errno(r, "sd-device: could not set subsystem for driver %s: %m", device->devpath);
+                                return log_device_debug_errno(device, r, "sd-device: Failed to set subsystem for driver %s: %m", device->devpath);
                 }
 
                 device->driver_subsystem_set = true;
@@ -890,7 +889,6 @@ _public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
         int r;
 
         assert_return(device, -EINVAL);
-        assert_return(devnum, -EINVAL);
 
         r = device_read_uevent_file(device);
         if (r < 0)
@@ -899,7 +897,8 @@ _public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
         if (major(device->devnum) <= 0)
                 return -ENOENT;
 
-        *devnum = device->devnum;
+        if (devnum)
+                *devnum = device->devnum;
 
         return 0;
 }
@@ -945,11 +944,11 @@ _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 log_debug_errno(r, "sd-device: could not set driver for %s: %m", device->devpath);
+                                return log_device_debug_errno(device, r, "sd-device: Failed to 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);
+                        return log_device_debug_errno(device, r, "sd-device: Failed to set driver for %s: %m", device->devpath);
         }
 
         if (!device->driver)
@@ -1212,7 +1211,7 @@ static int handle_db_line(sd_device *device, char key, const char *value) {
 
                 break;
         default:
-                log_debug("device db: unknown key '%c'", key);
+                log_device_debug(device, "sd-device: Unknown key '%c' in device db, ignoring", key);
         }
 
         return 0;
@@ -1241,9 +1240,9 @@ int device_get_id_filename(sd_device *device, const char **ret) {
                                      major(devnum), minor(devnum));
                         if (r < 0)
                                 return -ENOMEM;
-                } else if (sd_device_get_ifindex(device, &ifindex) >= 0 && ifindex > 0) {
+                } else if (sd_device_get_ifindex(device, &ifindex) >= 0) {
                         /* use netdev ifindex — n3 */
-                        r = asprintf(&id, "n%u", ifindex);
+                        r = asprintf(&id, "n%u", (unsigned) ifindex);
                         if (r < 0)
                                 return -ENOMEM;
                 } else {
@@ -1313,7 +1312,7 @@ int device_read_db_aux(sd_device *device, bool force) {
                 if (r == -ENOENT)
                         return 0;
                 else
-                        return log_debug_errno(r, "sd-device: failed to read db '%s': %m", path);
+                        return log_device_debug_errno(device, r, "sd-device: Failed to read db '%s': %m", path);
         }
 
         /* devices with a database entry are initialized */
@@ -1331,7 +1330,7 @@ int device_read_db_aux(sd_device *device, bool force) {
                         break;
                 case KEY:
                         if (db[i] != ':') {
-                                log_debug("sd-device: ignoring invalid db entry with key '%c'", key);
+                                log_device_debug(device, "sd-device: Invalid db entry with key '%c', ignoring", key);
 
                                 state = INVALID_LINE;
                         } else {
@@ -1357,14 +1356,14 @@ int device_read_db_aux(sd_device *device, bool force) {
                                 db[i] = '\0';
                                 r = handle_db_line(device, key, value);
                                 if (r < 0)
-                                        log_debug_errno(r, "sd-device: failed to handle db entry '%c:%s': %m", key, value);
+                                        log_device_debug_errno(device, r, "sd-device: Failed to handle db entry '%c:%s', ignoring: %m", key, value);
 
                                 state = PRE_KEY;
                         }
 
                         break;
                 default:
-                        assert_not_reached("invalid state when parsing db");
+                        assert_not_reached("Invalid state when parsing db");
                 }
         }
 
@@ -1375,19 +1374,16 @@ 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) {
+_public_ int sd_device_get_is_initialized(sd_device *device) {
         int r;
 
         assert_return(device, -EINVAL);
-        assert_return(initialized, -EINVAL);
 
         r = device_read_db(device);
         if (r < 0)
                 return r;
 
-        *initialized = device->is_initialized;
-
-        return 0;
+        return device->is_initialized;
 }
 
 _public_ int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *usec) {
@@ -1678,7 +1674,6 @@ _public_ int sd_device_get_property_value(sd_device *device, const char *key, co
 
         assert_return(device, -EINVAL);
         assert_return(key, -EINVAL);
-        assert_return(_value, -EINVAL);
 
         r = device_properties_prepare(device);
         if (r < 0)
@@ -1688,7 +1683,8 @@ _public_ int sd_device_get_property_value(sd_device *device, const char *key, co
         if (!value)
                 return -ENOENT;
 
-        *_value = value;
+        if (_value)
+                *_value = value;
 
         return 0;
 }
@@ -1831,12 +1827,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, const char *_value) {
-        _cleanup_close_ int fd = -1;
         _cleanup_free_ char *value = NULL;
-        const char *syspath;
-        char *path;
-        size_t len = 0;
-        ssize_t size;
+        const char *syspath, *path;
+        size_t len;
         int r;
 
         assert_return(device, -EINVAL);
@@ -1854,25 +1847,6 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
 
         path = strjoina(syspath, "/", sysattr);
 
-        fd = open(path, O_WRONLY | O_CLOEXEC | O_NOFOLLOW);
-        if (fd < 0) {
-                if (errno == ELOOP)
-                        return -EINVAL;
-                if (errno == EISDIR)
-                        return -EISDIR;
-
-                value = strdup("");
-                if (!value)
-                        return -ENOMEM;
-
-                r = device_add_sysattr_value(device, sysattr, value);
-                if (r < 0)
-                        return r;
-                value = NULL;
-
-                return -ENXIO;
-        }
-
         len = strlen(_value);
 
         /* drop trailing newlines */
@@ -1887,17 +1861,30 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
         if (!value)
                 return -ENOMEM;
 
-        size = write(fd, value, len);
-        if (size < 0)
-                return -errno;
+        r = write_string_file(path, value, WRITE_STRING_FILE_DISABLE_BUFFER | WRITE_STRING_FILE_NOFOLLOW);
+        if (r < 0) {
+                if (r == -ELOOP)
+                        return -EINVAL;
+                if (r == -EISDIR)
+                        return r;
 
-        if ((size_t)size != len)
-                return -EIO;
+                free(value);
+                value = strdup("");
+                if (!value)
+                        return -ENOMEM;
+
+                r = device_add_sysattr_value(device, sysattr, value);
+                if (r < 0)
+                        return r;
+
+                value = NULL;
+                return -ENXIO;
+        }
 
         r = device_add_sysattr_value(device, sysattr, value);
         if (r < 0)
                 return r;
-        value = NULL;
 
+        value = NULL;
         return 0;
 }