X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Flibelogind%2Fsd-device%2Fsd-device.c;h=7cea5a074658ef5a63dda89881eba859a19dd0a2;hb=d76bb3c179b7a32b109e39aa87ff09c8f5a8c178;hp=2e30b85ad2549210607b508484bac3112efa6967;hpb=092ac9b6200b0c82a877713af54b665d3ea558e8;p=elogind.git diff --git a/src/libelogind/sd-device/sd-device.c b/src/libelogind/sd-device/sd-device.c index 2e30b85ad..7cea5a074 100644 --- a/src/libelogind/sd-device/sd-device.c +++ b/src/libelogind/sd-device/sd-device.c @@ -184,15 +184,17 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) { 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); @@ -498,6 +500,8 @@ int device_read_uevent_file(sd_device *device) { if (device->uevent_loaded || device->sealed) return 0; + device->uevent_loaded = true; + r = sd_device_get_syspath(device, &syspath); if (r < 0) return r; @@ -508,6 +512,9 @@ int device_read_uevent_file(sd_device *device) { 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("sd-device: failed to read uevent file '%s': %s", path, strerror(-r)); return r; @@ -565,8 +572,6 @@ int device_read_uevent_file(sd_device *device) { log_debug("sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %s", major, minor, path, strerror(-r)); } - device->uevent_loaded = true; - return 0; } @@ -780,12 +785,15 @@ _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) { path_startswith(device->devpath, "/class/") || path_startswith(device->devpath, "/bus/")) r = device_set_subsystem(device, "subsystem"); - if (r < 0) + 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; @@ -896,10 +904,16 @@ _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 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; @@ -994,6 +1008,8 @@ _public_ int sd_device_get_sysname(sd_device *device, const char **ret) { return r; } + assert_return(device->sysname, -ENOENT); + *ret = device->sysname; return 0; @@ -1183,17 +1199,19 @@ int device_get_id_filename(sd_device *device, const char **ret) { 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 @@ -1204,9 +1222,12 @@ int device_get_id_filename(sd_device *device, const char **ret) { 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; @@ -1218,7 +1239,7 @@ int device_get_id_filename(sd_device *device, const char **ret) { 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; @@ -1235,9 +1256,11 @@ static int device_read_db(sd_device *device) { 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; @@ -1255,7 +1278,7 @@ static int device_read_db(sd_device *device) { } /* devices with a database entry are initialized */ - device->is_initialized = true;; + device->is_initialized = true; for (i = 0; i < db_len; i++) { switch (state) { @@ -1306,11 +1329,13 @@ static int device_read_db(sd_device *device) { } } - 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; @@ -1354,6 +1379,8 @@ _public_ int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *u } _public_ const char *sd_device_get_tag_first(sd_device *device) { + void *v; + assert_return(device, NULL); (void) device_read_db(device); @@ -1361,10 +1388,13 @@ _public_ const char *sd_device_get_tag_first(sd_device *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); @@ -1372,10 +1402,13 @@ _public_ const char *sd_device_get_tag_next(sd_device *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); @@ -1383,10 +1416,13 @@ _public_ const char *sd_device_get_devlink_first(sd_device *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); @@ -1394,7 +1430,8 @@ _public_ const char *sd_device_get_devlink_next(sd_device *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) { @@ -1465,7 +1502,7 @@ _public_ const char *sd_device_get_property_first(sd_device *device, const char 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; @@ -1487,7 +1524,7 @@ _public_ const char *sd_device_get_property_next(sd_device *device, const char * 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; @@ -1545,6 +1582,7 @@ static int device_sysattrs_read_all(sd_device *device) { } _public_ const char *sd_device_get_sysattr_first(sd_device *device) { + void *v; int r; assert_return(device, NULL); @@ -1559,16 +1597,20 @@ _public_ const char *sd_device_get_sysattr_first(sd_device *device) { 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) {