*device = (sd_device) {
.n_ref = 1,
.watch_handle = -1,
+ .devmode = (mode_t) -1,
+ .devuid = (uid_t) -1,
+ .devgid = (gid_t) -1,
};
*ret = device;
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;
/* /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);
/* 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) {
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;
}
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;
int r;
assert_return(device, -EINVAL);
- assert_return(ifindex, -EINVAL);
r = device_read_uevent_file(device);
if (r < 0)
return r;
- *ifindex = device->ifindex;
+ if (device->ifindex <= 0)
+ return -ENOENT;
+
+ if (ifindex)
+ *ifindex = device->ifindex;
return 0;
}
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)
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;
if (r < 0)
return r;
+ if (!device->devtype)
+ return -ENOENT;
+
*devtype = device->devtype;
return 0;
int r;
assert_return(device, -EINVAL);
- assert_return(devnum, -EINVAL);
r = device_read_uevent_file(device);
if (r < 0)
return r;
- *devnum = device->devnum;
+ if (major(device->devnum) <= 0)
+ return -ENOENT;
+
+ if (devnum)
+ *devnum = device->devnum;
return 0;
}
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)
return r;
}
+ if (!device->sysnum)
+ return -ENOENT;
+
*ret = device->sysnum;
return 0;
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;
if (r < 0)
return r;
- r = sd_device_get_devnum(device, &devnum);
- if (r < 0)
- return r;
-
- r = sd_device_get_ifindex(device, &ifindex);
- if (r < 0)
- return r;
-
- if (major(devnum) > 0) {
+ if (sd_device_get_devnum(device, &devnum) >= 0) {
assert(subsystem);
/* use dev_t — b259:131072, c254:0 */
major(devnum), minor(devnum));
if (r < 0)
return -ENOMEM;
- } else if (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 {
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 */
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 {
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");
}
}
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) {
assert_return(device, -EINVAL);
assert_return(key, -EINVAL);
- assert_return(_value, -EINVAL);
r = device_properties_prepare(device);
if (r < 0)
if (!value)
return -ENOENT;
- *_value = value;
+ if (_value)
+ *_value = value;
return 0;
}
/* 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) {
- _cleanup_close_ int fd = -1;
+_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *_value) {
_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;
}