Lines Matching refs:device
25 #include "sd-device.h"
28 #include "device-internal.h"
29 #include "device-private.h"
30 #include "device-util.h"
46 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
50 device = new0(sd_device, 1);
51 if (!device)
54 device->n_ref = 1;
55 device->watch_handle = -1;
57 *ret = device;
58 device = NULL;
63 _public_ sd_device *sd_device_ref(sd_device *device) {
64 if (device)
65 assert_se(++ device->n_ref >= 2);
67 return device;
70 _public_ sd_device *sd_device_unref(sd_device *device) {
71 if (device && -- device->n_ref == 0) {
72 sd_device_unref(device->parent);
73 free(device->syspath);
74 free(device->sysname);
75 free(device->devtype);
76 free(device->devname);
77 free(device->subsystem);
78 free(device->driver);
79 free(device->id_filename);
80 free(device->properties_strv);
81 free(device->properties_nulstr);
83 ordered_hashmap_free_free_free(device->properties);
84 ordered_hashmap_free_free_free(device->properties_db);
85 hashmap_free_free_free(device->sysattr_values);
86 set_free_free(device->sysattrs);
87 set_free_free(device->tags);
88 set_free_free(device->devlinks);
90 free(device);
96 int device_add_property_aux(sd_device *device, const char *_key, const char *_value, bool db) {
99 assert(device);
103 properties = &device->properties_db;
105 properties = &device->properties;
139 device->properties_generation ++;
140 device->properties_buf_outdated = true;
146 int device_add_property_internal(sd_device *device, const char *key, const char *value) {
147 return device_add_property_aux(device, key, value, false);
150 int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
155 assert(device);
160 log_debug("sd-device: syspath '%s' is not a subdirectory of /sys", _syspath);
167 /* the device does not exist (any more?) */
174 /* the device does not exist (any more?) */
177 return log_debug_errno(errno, "sd-device: could not canonicalize '%s': %m", _syspath);
180 log_debug_errno(r, "sd-device: could not get target of '%s': %m", _syspath);
192 /* this is not a valid device */
195 log_debug("sd-device: %s does not have an uevent file: %m", syspath);
211 r = device_add_property_internal(device, "DEVPATH", devpath);
215 free(device->syspath);
216 device->syspath = syspath;
219 device->devpath = devpath;
225 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
231 r = device_new_aux(&device);
235 r = device_set_syspath(device, syspath, true);
239 *ret = device;
240 device = NULL;
331 int device_set_devtype(sd_device *device, const char *_devtype) {
335 assert(device);
342 r = device_add_property_internal(device, "DEVTYPE", devtype);
346 free(device->devtype);
347 device->devtype = devtype;
353 int device_set_ifindex(sd_device *device, const char *_ifindex) {
356 assert(device);
363 r = device_add_property_internal(device, "IFINDEX", _ifindex);
367 device->ifindex = ifindex;
372 int device_set_devname(sd_device *device, const char *_devname) {
376 assert(device);
389 r = device_add_property_internal(device, "DEVNAME", devname);
393 free(device->devname);
394 device->devname = devname;
400 int device_set_devmode(sd_device *device, const char *_devmode) {
404 assert(device);
414 r = device_add_property_internal(device, "DEVMODE", _devmode);
418 device->devmode = devmode;
423 int device_set_devnum(sd_device *device, const char *major, const char *minor) {
427 assert(device);
442 r = device_add_property_internal(device, "MAJOR", major);
447 r = device_add_property_internal(device, "MINOR", minor);
452 device->devnum = makedev(maj, min);
457 static int handle_uevent_line(sd_device *device, const char *key, const char *value, const char **major, const char **minor) {
460 assert(device);
467 r = device_set_devtype(device, value);
471 r = device_set_ifindex(device, value);
475 r = device_set_devname(device, value);
479 r = device_set_devmode(device, value);
487 r = device_add_property_internal(device, key, value);
495 int device_read_uevent_file(sd_device *device) {
511 assert(device);
513 if (device->uevent_loaded || device->sealed)
516 device->uevent_loaded = true;
518 r = sd_device_get_syspath(device, &syspath);
532 log_debug_errno(r, "sd-device: failed to read uevent file '%s': %m", path);
553 log_debug("sd-device: ignoring invalid uevent line '%s'", key);
569 r = handle_uevent_line(device, key, value, &major, &minor);
571 log_debug_errno(r, "sd-device: failed to handle uevent entry '%s=%s': %m", key, value);
583 r = device_set_devnum(device, major, minor);
585 log_debug_errno(r, "sd-device: could not set 'MAJOR=%s' or 'MINOR=%s' from '%s': %m", major, minor, path);
591 _public_ int sd_device_get_ifindex(sd_device *device, int *ifindex) {
594 assert_return(device, -EINVAL);
597 r = device_read_uevent_file(device);
601 *ifindex = device->ifindex;
627 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
644 r = sd_device_new_from_subsystem_sysname(&device, "net", ifr.ifr_name);
648 r = sd_device_get_ifindex(device, &ifindex);
652 /* this is racey, so we might end up with the wrong device */
656 *ret = device;
657 device = NULL;
681 _public_ int sd_device_get_syspath(sd_device *device, const char **ret) {
682 assert_return(device, -EINVAL);
685 assert(path_startswith(device->syspath, "/sys/"));
687 *ret = device->syspath;
747 int device_set_subsystem(sd_device *device, const char *_subsystem) {
751 assert(device);
758 r = device_add_property_internal(device, "SUBSYSTEM", subsystem);
762 free(device->subsystem);
763 device->subsystem = subsystem;
766 device->subsystem_set = true;
771 _public_ int sd_device_get_subsystem(sd_device *device, const char **ret) {
773 assert_return(device, -EINVAL);
775 if (!device->subsystem_set) {
782 r = sd_device_get_syspath(device, &syspath);
789 r = device_set_subsystem(device, subsystem);
791 else if (path_startswith(device->devpath, "/module/"))
792 r = device_set_subsystem(device, "module");
793 else if (strstr(device->devpath, "/drivers/"))
794 r = device_set_subsystem(device, "drivers");
795 else if (path_startswith(device->devpath, "/subsystem/") ||
796 path_startswith(device->devpath, "/class/") ||
797 path_startswith(device->devpath, "/bus/"))
798 r = device_set_subsystem(device, "subsystem");
800 return log_debug_errno(r, "sd-device: could not set subsystem for %s: %m", device->devpath);
802 device->subsystem_set = true;
805 if (!device->subsystem)
808 *ret = device->subsystem;
813 _public_ int sd_device_get_devtype(sd_device *device, const char **devtype) {
817 assert(device);
819 r = device_read_uevent_file(device);
823 *devtype = device->devtype;
860 _public_ int sd_device_get_devnum(sd_device *device, dev_t *devnum) {
863 assert_return(device, -EINVAL);
866 r = device_read_uevent_file(device);
870 *devnum = device->devnum;
875 int device_set_driver(sd_device *device, const char *_driver) {
879 assert(device);
886 r = device_add_property_internal(device, "DRIVER", driver);
890 free(device->driver);
891 device->driver = driver;
894 device->driver_set = true;
899 _public_ int sd_device_get_driver(sd_device *device, const char **ret) {
900 assert_return(device, -EINVAL);
903 if (!device->driver_set) {
909 r = sd_device_get_syspath(device, &syspath);
916 r = device_set_driver(device, driver);
918 return log_debug_errno(r, "sd-device: could not set driver for %s: %m", device->devpath);
920 device->driver_set = true;
922 return log_debug_errno(r, "sd-device: could not set driver for %s: %m", device->devpath);
925 if (!device->driver)
928 *ret = device->driver;
933 _public_ int sd_device_get_devpath(sd_device *device, const char **devpath) {
934 assert_return(device, -EINVAL);
937 assert(device->devpath);
938 assert(device->devpath[0] == '/');
940 *devpath = device->devpath;
945 _public_ int sd_device_get_devname(sd_device *device, const char **devname) {
948 assert_return(device, -EINVAL);
951 r = device_read_uevent_file(device);
955 if (!device->devname)
958 assert(path_startswith(device->devname, "/dev/"));
960 *devname = device->devname;
965 static int device_set_sysname(sd_device *device) {
971 pos = strrchr(device->devpath, '/');
977 if (*pos == '\0' || pos <= device->devpath)
999 free(device->sysname);
1000 device->sysname = sysname;
1003 device->sysnum = sysnum;
1005 device->sysname_set = true;
1010 _public_ int sd_device_get_sysname(sd_device *device, const char **ret) {
1013 assert_return(device, -EINVAL);
1016 if (!device->sysname_set) {
1017 r = device_set_sysname(device);
1022 assert_return(device->sysname, -ENOENT);
1024 *ret = device->sysname;
1029 _public_ int sd_device_get_sysnum(sd_device *device, const char **ret) {
1032 assert_return(device, -EINVAL);
1035 if (!device->sysname_set) {
1036 r = device_set_sysname(device);
1041 *ret = device->sysnum;
1052 int device_add_tag(sd_device *device, const char *tag) {
1055 assert(device);
1061 r = set_ensure_allocated(&device->tags, &string_hash_ops);
1065 r = set_put_strdup(device->tags, tag);
1069 device->tags_generation ++;
1070 device->property_tags_outdated = true;
1075 int device_add_devlink(sd_device *device, const char *devlink) {
1078 assert(device);
1081 r = set_ensure_allocated(&device->devlinks, &string_hash_ops);
1085 r = set_put_strdup(device->devlinks, devlink);
1089 device->devlinks_generation ++;
1090 device->property_devlinks_outdated = true;
1095 static int device_add_property_internal_from_string(sd_device *device, const char *str) {
1099 assert(device);
1115 return device_add_property_internal(device, key, value);
1118 int device_set_usec_initialized(sd_device *device, const char *initialized) {
1122 assert(device);
1129 r = device_add_property_internal(device, "USEC_INITIALIZED", initialized);
1133 device->usec_initialized = usec_initialized;
1138 static int handle_db_line(sd_device *device, char key, const char *value) {
1142 assert(device);
1147 r = device_add_tag(device, value);
1154 r = device_add_devlink(device, path);
1160 r = device_add_property_internal_from_string(device, value);
1166 r = device_set_usec_initialized(device, value);
1172 r = safe_atoi(value, &device->devlink_priority);
1178 r = safe_atoi(value, &device->watch_handle);
1184 log_debug("device db: unknown key '%c'", key);
1190 int device_get_id_filename(sd_device *device, const char **ret) {
1191 assert(device);
1194 if (!device->id_filename) {
1200 r = sd_device_get_subsystem(device, &subsystem);
1204 r = sd_device_get_devnum(device, &devnum);
1208 r = sd_device_get_ifindex(device, &ifindex);
1232 sysname = basename(device->devpath);
1244 device->id_filename = id;
1248 *ret = device->id_filename;
1253 int device_read_db_aux(sd_device *device, bool force) {
1270 if (device->db_loaded || (!force && device->sealed))
1273 device->db_loaded = true;
1275 r = device_get_id_filename(device, &id);
1286 return log_debug_errno(r, "sd-device: failed to read db '%s': %m", path);
1290 device->is_initialized = true;
1304 log_debug("sd-device: ignoring invalid db entry with key '%c'", key);
1328 r = handle_db_line(device, key, value);
1330 log_debug_errno(r, "sd-device: failed to handle db entry '%c:%s': %m", key, value);
1344 static int device_read_db(sd_device *device) {
1345 return device_read_db_aux(device, false);
1348 _public_ int sd_device_get_is_initialized(sd_device *device, int *initialized) {
1351 assert_return(device, -EINVAL);
1354 r = device_read_db(device);
1358 *initialized = device->is_initialized;
1363 _public_ int sd_device_get_usec_since_initialized(sd_device *device, uint64_t *usec) {
1367 assert_return(device, -EINVAL);
1370 r = device_read_db(device);
1374 if (!device->is_initialized)
1377 if (!device->usec_initialized)
1382 if (now_ts < device->usec_initialized)
1385 *usec = now_ts - device->usec_initialized;
1390 _public_ const char *sd_device_get_tag_first(sd_device *device) {
1393 assert_return(device, NULL);
1395 (void) device_read_db(device);
1397 device->tags_iterator_generation = device->tags_generation;
1398 device->tags_iterator = ITERATOR_FIRST;
1400 set_iterate(device->tags, &device->tags_iterator, &v);
1404 _public_ const char *sd_device_get_tag_next(sd_device *device) {
1407 assert_return(device, NULL);
1409 (void) device_read_db(device);
1411 if (device->tags_iterator_generation != device->tags_generation)
1414 set_iterate(device->tags, &device->tags_iterator, &v);
1418 _public_ const char *sd_device_get_devlink_first(sd_device *device) {
1421 assert_return(device, NULL);
1423 (void) device_read_db(device);
1425 device->devlinks_iterator_generation = device->devlinks_generation;
1426 device->devlinks_iterator = ITERATOR_FIRST;
1428 set_iterate(device->devlinks, &device->devlinks_iterator, &v);
1432 _public_ const char *sd_device_get_devlink_next(sd_device *device) {
1435 assert_return(device, NULL);
1437 (void) device_read_db(device);
1439 if (device->devlinks_iterator_generation != device->devlinks_generation)
1442 set_iterate(device->devlinks, &device->devlinks_iterator, &v);
1446 static int device_properties_prepare(sd_device *device) {
1449 assert(device);
1451 r = device_read_uevent_file(device);
1455 r = device_read_db(device);
1459 if (device->property_devlinks_outdated) {
1463 devlink = sd_device_get_devlink_first(device);
1467 while ((devlink = sd_device_get_devlink_next(device)))
1470 r = device_add_property_internal(device, "DEVLINKS", devlinks);
1474 device->property_devlinks_outdated = false;
1477 if (device->property_tags_outdated) {
1481 tag = sd_device_get_tag_first(device);
1485 while ((tag = sd_device_get_tag_next(device)))
1490 r = device_add_property_internal(device, "TAGS", tags);
1494 device->property_tags_outdated = false;
1500 _public_ const char *sd_device_get_property_first(sd_device *device, const char **_value) {
1505 assert_return(device, NULL);
1507 r = device_properties_prepare(device);
1511 device->properties_iterator_generation = device->properties_generation;
1512 device->properties_iterator = ITERATOR_FIRST;
1514 ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
1522 _public_ const char *sd_device_get_property_next(sd_device *device, const char **_value) {
1527 assert_return(device, NULL);
1529 r = device_properties_prepare(device);
1533 if (device->properties_iterator_generation != device->properties_generation)
1536 ordered_hashmap_iterate(device->properties, &device->properties_iterator, (void**)&value, (const void**)&key);
1544 static int device_sysattrs_read_all(sd_device *device) {
1550 assert(device);
1552 if (device->sysattrs_read)
1555 r = sd_device_get_syspath(device, &syspath);
1563 r = set_ensure_allocated(&device->sysattrs, &string_hash_ops);
1583 r = set_put_strdup(device->sysattrs, dent->d_name);
1588 device->sysattrs_read = true;
1593 _public_ const char *sd_device_get_sysattr_first(sd_device *device) {
1597 assert_return(device, NULL);
1599 if (!device->sysattrs_read) {
1600 r = device_sysattrs_read_all(device);
1607 device->sysattrs_iterator = ITERATOR_FIRST;
1609 set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
1613 _public_ const char *sd_device_get_sysattr_next(sd_device *device) {
1616 assert_return(device, NULL);
1618 if (!device->sysattrs_read)
1621 set_iterate(device->sysattrs, &device->sysattrs_iterator, &v);
1625 _public_ int sd_device_has_tag(sd_device *device, const char *tag) {
1626 assert_return(device, -EINVAL);
1629 (void) device_read_db(device);
1631 return !!set_contains(device->tags, tag);
1634 _public_ int sd_device_get_property_value(sd_device *device, const char *key, const char **_value) {
1638 assert_return(device, -EINVAL);
1642 r = device_properties_prepare(device);
1646 value = ordered_hashmap_get(device->properties, key);
1656 static int device_add_sysattr_value(sd_device *device, const char *_key, char *value) {
1661 assert(device);
1664 r = hashmap_ensure_allocated(&device->sysattr_values, &string_hash_ops);
1668 value_old = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
1675 r = hashmap_put(device->sysattr_values, key, value);
1684 static int device_get_sysattr_value(sd_device *device, const char *_key, const char **_value) {
1687 assert(device);
1690 value = hashmap_get2(device->sysattr_values, _key, (void **) &key);
1702 _public_ int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value) {
1709 assert_return(device, -EINVAL);
1713 r = device_get_sysattr_value(device, sysattr, &cached_value);
1728 r = sd_device_get_syspath(device, &syspath);
1736 r = device_add_sysattr_value(device, sysattr, NULL);
1769 r = device_add_sysattr_value(device, sysattr, value);
1779 static void device_remove_sysattr_value(sd_device *device, const char *_key) {
1783 assert(device);
1786 value = hashmap_remove2(device->sysattr_values, _key, (void **) &key);
1793 _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *_value) {
1803 assert_return(device, -EINVAL);
1807 device_remove_sysattr_value(device, sysattr);
1812 r = sd_device_get_syspath(device, &syspath);
1823 r = device_add_sysattr_value(device, sysattr, value);
1866 r = device_add_sysattr_value(device, sysattr, value);