#include "set.h"
#include "socket-util.h"
#include "stat-util.h"
+#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "strxcpyx.h"
#include "util.h"
int device_new_aux(sd_device **ret) {
- sd_device *device = NULL;
+ sd_device *device;
assert(ret);
.devmode = (mode_t) -1,
.devuid = (uid_t) -1,
.devgid = (gid_t) -1,
+ .action = _DEVICE_ACTION_INVALID,
};
*ret = device;
"sd-device: Canonicalized path '%s' does not starts with sysfs mount point '%s'",
syspath, real_sys);
- new_syspath = strjoin("/sys/", p);
+ new_syspath = path_join("/sys", p);
if (!new_syspath)
return -ENOMEM;
return r;
free_and_replace(device->syspath, syspath);
-
device->devpath = devpath;
-
return 0;
}
return r;
*ret = TAKE_PTR(device);
-
return 0;
}
assert_return(IN_SET(type, 'b', 'c'), -EINVAL);
/* use /sys/dev/{block,char}/<maj>:<min> link */
- snprintf(id, sizeof(id), "%u:%u", major(devnum), minor(devnum));
+ xsprintf(id, "%u:%u", major(devnum), minor(devnum));
syspath = strjoina("/sys/dev/", (type == 'b' ? "block" : "char"), "/", id);
if (device->uevent_loaded || device->sealed)
return 0;
- device->uevent_loaded = true;
-
r = sd_device_get_syspath(device, &syspath);
if (r < 0)
return r;
path = strjoina(syspath, "/uevent");
r = read_full_file(path, &uevent, &uevent_len);
- if (r == -EACCES)
+ if (r == -EACCES) {
/* empty uevent files may be write-only */
+ device->uevent_loaded = true;
return 0;
- else if (r == -ENOENT)
+ }
+ if (r == -ENOENT)
/* some devices may not have uevent files, see set_syspath() */
return 0;
- else if (r < 0)
+ if (r < 0)
return log_device_debug_errno(device, r, "sd-device: Failed to read uevent file '%s': %m", path);
+ device->uevent_loaded = true;
+
for (i = 0; i < uevent_len; i++)
switch (state) {
case PRE_KEY:
return sd_device_new_from_devnum(ret, id[0], devt);
}
- case 'n':
- {
+
+ case 'n': {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
_cleanup_close_ int sk = -1;
struct ifreq ifr = {};
return -ENODEV;
*ret = TAKE_PTR(device);
-
return 0;
}
- case '+':
- {
+
+ case '+': {
char subsys[PATH_MAX];
char *sysname;
return sd_device_new_from_subsystem_sysname(ret, subsys, sysname);
}
+
default:
return -EINVAL;
}
return -ENOENT;
*ret = child->parent;
-
return 0;
}
if (r < 0)
return r;
- free_and_replace(device->subsystem, subsystem);
-
device->subsystem_set = true;
-
- return 0;
+ return free_and_replace(device->subsystem, subsystem);
}
static int device_set_drivers_subsystem(sd_device *device, const char *_subsystem) {
if (r < 0)
return r;
- free_and_replace(device->driver_subsystem, subsystem);
-
- return 0;
+ return free_and_replace(device->driver_subsystem, subsystem);
}
_public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
return -ENOENT;
*ret = device->subsystem;
-
return 0;
}
return r;
*ret = parent;
-
return 0;
}
if (r < 0)
return r;
- free_and_replace(device->driver, driver);
-
device->driver_set = true;
-
- return 0;
+ return free_and_replace(device->driver, driver);
}
_public_ int sd_device_get_driver(sd_device *device, const char **ret) {
return -ENOENT;
*ret = device->driver;
-
return 0;
}
assert(device->devpath[0] == '/');
*devpath = device->devpath;
-
return 0;
}
assert(path_startswith(device->devname, "/dev/"));
*devname = device->devname;
-
return 0;
}
const char *pos;
size_t len = 0;
+ if (!device->devpath)
+ return -EINVAL;
+
pos = strrchr(device->devpath, '/');
if (!pos)
return -EINVAL;
if (len == 0)
sysnum = NULL;
- free_and_replace(device->sysname, sysname);
-
- device->sysnum = sysnum;
-
device->sysname_set = true;
-
- return 0;
+ device->sysnum = sysnum;
+ return free_and_replace(device->sysname, sysname);
}
_public_ int sd_device_get_sysname(sd_device *device, const char **ret) {
assert_return(device->sysname, -ENOENT);
*ret = device->sysname;
-
return 0;
}
return -ENOENT;
*ret = device->sysnum;
-
return 0;
}
static int device_add_property_internal_from_string(sd_device *device, const char *str) {
_cleanup_free_ char *key = NULL;
char *value;
+ int r;
assert(device);
assert(str);
if (isempty(++value))
value = NULL;
- return device_add_property_internal(device, key, value);
+ /* Add the property to both sd_device::properties and sd_device::properties_db,
+ * as this is called by only handle_db_line(). */
+ r = device_add_property_aux(device, key, value, false);
+ if (r < 0)
+ return r;
+
+ return device_add_property_aux(device, key, value, true);
}
-int device_set_usec_initialized(sd_device *device, const char *initialized) {
- uint64_t usec_initialized;
+int device_set_usec_initialized(sd_device *device, usec_t when) {
+ char s[DECIMAL_STR_MAX(usec_t)];
int r;
assert(device);
- assert(initialized);
- r = safe_atou64(initialized, &usec_initialized);
- if (r < 0)
- return r;
+ xsprintf(s, USEC_FMT, when);
- r = device_add_property_internal(device, "USEC_INITIALIZED", initialized);
+ r = device_add_property_internal(device, "USEC_INITIALIZED", s);
if (r < 0)
return r;
- device->usec_initialized = usec_initialized;
-
+ device->usec_initialized = when;
return 0;
}
return r;
break;
- case 'I':
- r = device_set_usec_initialized(device, value);
+ case 'I': {
+ usec_t t;
+
+ r = safe_atou64(value, &t);
+ if (r < 0)
+ return r;
+
+ r = device_set_usec_initialized(device, t);
if (r < 0)
return r;
break;
+ }
case 'L':
r = safe_atoi(value, &device->devlink_priority);
if (r < 0)
}
*ret = device->id_filename;
-
return 0;
}
-int device_read_db_aux(sd_device *device, bool force) {
+int device_read_db_internal_filename(sd_device *device, const char *filename) {
_cleanup_free_ char *db = NULL;
- char *path;
- const char *id, *value;
+ const char *value;
+ size_t db_len, i;
char key;
- size_t db_len;
- unsigned i;
int r;
enum {
INVALID_LINE,
} state = PRE_KEY;
- 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;
-
- path = strjoina("/run/udev/data/", id);
+ assert(device);
+ assert(filename);
- r = read_full_file(path, &db, &db_len);
+ r = read_full_file(filename, &db, &db_len);
if (r < 0) {
if (r == -ENOENT)
return 0;
- else
- return log_device_debug_errno(device, 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", filename);
}
/* devices with a database entry are initialized */
device->is_initialized = true;
+ device->db_loaded = true;
+
for (i = 0; i < db_len; i++) {
switch (state) {
case PRE_KEY:
break;
default:
- assert_not_reached("Invalid state when parsing db");
+ return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL), "sd-device: invalid db syntax.");
}
}
return 0;
}
-static int device_read_db(sd_device *device) {
- return device_read_db_aux(device, false);
+int device_read_db_internal(sd_device *device, bool force) {
+ const char *id, *path;
+ int r;
+
+ assert(device);
+
+ if (device->db_loaded || (!force && device->sealed))
+ return 0;
+
+ r = device_get_id_filename(device, &id);
+ if (r < 0)
+ return r;
+
+ path = strjoina("/run/udev/data/", id);
+
+ return device_read_db_internal_filename(device, path);
}
_public_ int sd_device_get_is_initialized(sd_device *device) {
return -EIO;
*usec = now_ts - device->usec_initialized;
-
return 0;
}
return v;
}
-static int device_properties_prepare(sd_device *device) {
+int device_properties_prepare(sd_device *device) {
int r;
assert(device);
if (_value)
*_value = value;
-
return key;
}
if (_value)
*_value = value;
-
return key;
}
if (_value)
*_value = value;
-
return 0;
}
r = hashmap_put(device->sysattr_values, key, value);
if (r < 0)
return r;
-
- key = NULL;
+ TAKE_PTR(key);
return 0;
}
if (_value)
*_value = value;
-
return 0;
}
static void device_remove_sysattr_value(sd_device *device, const char *_key) {
_cleanup_free_ char *key = NULL;
- _cleanup_free_ char *value = NULL;
assert(device);
assert(_key);
- value = hashmap_remove2(device->sysattr_values, _key, (void **) &key);
-
- return;
+ free(hashmap_remove2(device->sysattr_values, _key, (void **) &key));
}
/* set the attribute and save it in the cache. If a NULL value is passed the
if (!_value) {
device_remove_sysattr_value(device, sysattr);
-
return 0;
}
if (r == -EISDIR)
return r;
- free(value);
- value = strdup("");
- if (!value)
- return -ENOMEM;
+ r = free_and_strdup(&value, "");
+ if (r < 0)
+ return r;
r = device_add_sysattr_value(device, sysattr, value);
if (r < 0)
return r;
+ TAKE_PTR(value);
- value = NULL;
return -ENXIO;
}
r = device_add_sysattr_value(device, sysattr, value);
if (r < 0)
return r;
+ TAKE_PTR(value);
- value = NULL;
return 0;
}