***/
#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;
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;
}
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);
} 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) {
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 < 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;
}
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;
}
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;
}
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;
return 0;
}
- return -ENOENT;
+ return -ENODEV;
}
_public_ int sd_device_get_parent(sd_device *child, sd_device **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;
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;
return r;
}
+ assert_return(device->sysname, -ENOENT);
+
*ret = device->sysname;
return 0;
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
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;
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;
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;
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) {
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;
}
}
}
- 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;
}
_public_ const char *sd_device_get_tag_first(sd_device *device) {
+ void *v;
+
assert_return(device, NULL);
(void) device_read_db(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);
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);
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);
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) {
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;
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;
}
_public_ const char *sd_device_get_sysattr_first(sd_device *device) {
+ void *v;
int r;
assert_return(device, NULL);
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) {
}
/* 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);
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;
}
/* 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;
assert_return(device, -EINVAL);
assert_return(sysattr, -EINVAL);
- if (!value) {
+ if (!_value) {
device_remove_sysattr_value(device, sysattr);
return 0;
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;
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)
if (fd < 0)
return -errno;
+ value = strdup(_value);
+ if (!value)
+ return -ENOMEM;
+
size = write(fd, value, value_len);
if (size < 0)
return -errno;
if (r < 0)
return r;
+ value = NULL;
+
return 0;
}