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);
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)
/* 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);
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 */
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;
}