Lines Matching refs:device

25 #include "sd-device.h"
28 #include "device-internal.h"
29 #include "device-private.h"
30 #include "device-util.h"
48 int device_add_property(sd_device *device, const char *key, const char *value) {
51 assert(device);
54 r = device_add_property_aux(device, key, value, false);
59 r = device_add_property_aux(device, key, value, true);
67 static int device_add_property_internal_from_string(sd_device *device, const char *str) {
71 assert(device);
87 return device_add_property_internal(device, key, value);
90 static int handle_db_line(sd_device *device, char key, const char *value) {
94 assert(device);
100 r = device_add_devlink(device, path);
106 r = safe_atoi(value, &device->devlink_priority);
112 r = device_add_property_internal_from_string(device, value);
118 r = device_add_tag(device, value);
124 r = safe_atoi(value, &device->watch_handle);
130 r = device_set_usec_initialized(device, value);
136 log_debug("device db: unknown key '%c'", key);
142 void device_set_devlink_priority(sd_device *device, int priority) {
143 assert(device);
145 device->devlink_priority = priority;
148 void device_set_is_initialized(sd_device *device) {
149 assert(device);
151 device->is_initialized = true;
154 int device_ensure_usec_initialized(sd_device *device, sd_device *device_old) {
159 assert(device);
170 r = device_set_usec_initialized(device, num);
177 static int device_read_db(sd_device *device) {
194 assert(device);
196 if (device->db_loaded || device->sealed)
199 r = device_get_id_filename(device, &id);
210 return log_debug_errno(r, "sd-device: failed to read db '%s': %m", path);
214 device_set_is_initialized(device);
228 log_debug("sd-device: ignoring invalid db entry with key '%c'", key);
252 r = handle_db_line(device, key, value);
254 log_debug_errno(r, "sd-device: failed to handle db entry '%c:%s': %m", key, value);
265 device->db_loaded = true;
270 uint64_t device_get_properties_generation(sd_device *device) {
271 assert(device);
273 return device->properties_generation;
276 uint64_t device_get_tags_generation(sd_device *device) {
277 assert(device);
279 return device->tags_generation;
282 uint64_t device_get_devlinks_generation(sd_device *device) {
283 assert(device);
285 return device->devlinks_generation;
288 int device_get_devnode_mode(sd_device *device, mode_t *mode) {
291 assert(device);
294 r = device_read_db(device);
298 *mode = device->devmode;
303 int device_get_devnode_uid(sd_device *device, uid_t *uid) {
306 assert(device);
309 r = device_read_db(device);
313 *uid = device->devuid;
318 static int device_set_devuid(sd_device *device, const char *uid) {
322 assert(device);
329 r = device_add_property_internal(device, "DEVUID", uid);
333 device->devuid = u;
338 int device_get_devnode_gid(sd_device *device, gid_t *gid) {
341 assert(device);
344 r = device_read_db(device);
348 *gid = device->devgid;
353 static int device_set_devgid(sd_device *device, const char *gid) {
357 assert(device);
364 r = device_add_property_internal(device, "DEVGID", gid);
368 device->devgid = g;
373 static int device_amend(sd_device *device, const char *key, const char *value) {
376 assert(device);
386 r = device_set_syspath(device, path, false);
388 return log_debug_errno(r, "sd-device: could not set syspath to '%s': %m", path);
390 r = device_set_subsystem(device, value);
392 return log_debug_errno(r, "sd-device: could not set subsystem to '%s': %m", value);
394 r = device_set_devtype(device, value);
396 return log_debug_errno(r, "sd-device: could not set devtype to '%s': %m", value);
398 r = device_set_devname(device, value);
400 return log_debug_errno(r, "sd-device: could not set devname to '%s': %m", value);
402 r = device_set_usec_initialized(device, value);
404 return log_debug_errno(r, "sd-device: could not set usec-initialized to '%s': %m", value);
406 r = device_set_driver(device, value);
408 return log_debug_errno(r, "sd-device: could not set driver to '%s': %m", value);
410 r = device_set_ifindex(device, value);
412 return log_debug_errno(r, "sd-device: could not set ifindex to '%s': %m", value);
414 r = device_set_devmode(device, value);
416 return log_debug_errno(r, "sd-device: could not set devmode to '%s': %m", value);
418 r = device_set_devuid(device, value);
420 return log_debug_errno(r, "sd-device: could not set devuid to '%s': %m", value);
422 r = device_set_devgid(device, value);
424 return log_debug_errno(r, "sd-device: could not set devgid to '%s': %m", value);
435 r = device_add_devlink(device, devlink);
437 return log_debug_errno(r, "sd-device: could not add devlink '%s': %m", devlink);
449 r = device_add_tag(device, tag);
451 return log_debug_errno(r, "sd-device: could not add tag '%s': %m", tag);
454 r = device_add_property_internal(device, key, value);
456 return log_debug_errno(r, "sd-device: could not add property '%s=%s': %m", key, value);
473 static int device_append(sd_device *device, char *key, const char **_major, const char **_minor, uint64_t *_seqnum,
481 assert(device);
490 log_debug("sd-device: not a key-value pair: '%s'", key);
516 r = device_amend(device, key, value);
536 void device_seal(sd_device *device) {
537 assert(device);
539 device->sealed = true;
542 static int device_verify(sd_device *device, DeviceAction action, uint64_t seqnum) {
543 assert(device);
545 if (!device->devpath || !device->subsystem || action == _DEVICE_ACTION_INVALID || seqnum == 0) {
546 log_debug("sd-device: device created from strv lacks devpath, subsystem, action or seqnum");
550 device->sealed = true;
556 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
566 r = device_new_aux(&device);
571 r = device_append(device, *key, &major, &minor, &seqnum, &action);
577 r = device_set_devnum(device, major, minor);
579 return log_debug_errno(r, "sd-device: could not set devnum %s:%s: %m", major, minor);
582 r = device_verify(device, action, seqnum);
586 *ret = device;
587 device = NULL;
593 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
604 r = device_new_aux(&device);
615 log_debug("sd-device: failed to parse nulstr");
620 r = device_append(device, key, &major, &minor, &seqnum, &action);
626 r = device_set_devnum(device, major, minor);
628 return log_debug_errno(r, "sd-device: could not set devnum %s:%s: %m", major, minor);
631 r = device_verify(device, action, seqnum);
635 *ret = device;
636 device = NULL;
641 static int device_update_properties_bufs(sd_device *device) {
648 assert(device);
650 if (!device->properties_buf_outdated)
653 FOREACH_DEVICE_PROPERTY(device, prop, val) {
678 free(device->properties_nulstr);
679 device->properties_nulstr = buf_nulstr;
681 device->properties_nulstr_len = nulstr_len;
682 free(device->properties_strv);
683 device->properties_strv = buf_strv;
686 device->properties_buf_outdated = false;
691 int device_get_properties_nulstr(sd_device *device, const uint8_t **nulstr, size_t *len) {
694 assert(device);
698 r = device_update_properties_bufs(device);
702 *nulstr = device->properties_nulstr;
703 *len = device->properties_nulstr_len;
708 int device_get_properties_strv(sd_device *device, char ***strv) {
711 assert(device);
714 r = device_update_properties_bufs(device);
718 *strv = device->properties_strv;
723 int device_get_devlink_priority(sd_device *device, int *priority) {
726 assert(device);
729 r = device_read_db(device);
733 *priority = device->devlink_priority;
738 int device_get_watch_handle(sd_device *device, int *handle) {
741 assert(device);
744 r = device_read_db(device);
748 *handle = device->watch_handle;
753 void device_set_watch_handle(sd_device *device, int handle) {
754 assert(device);
756 device->watch_handle = handle;
759 int device_rename(sd_device *device, const char *name) {
765 assert(device);
768 dirname = dirname_malloc(device->syspath);
775 r = device_set_syspath(device, new_syspath, false);
779 r = sd_device_get_property_value(device, "INTERFACE", &interface);
781 r = device_add_property_internal(device, "INTERFACE", name);
786 r = device_add_property_internal(device, "INTERFACE_OLD", interface);
887 void device_cleanup_tags(sd_device *device) {
888 assert(device);
890 set_free_free(device->tags);
891 device->tags = NULL;
892 device->property_tags_outdated = true;
893 device->tags_generation ++;
896 void device_cleanup_devlinks(sd_device *device) {
897 assert(device);
899 set_free_free(device->devlinks);
900 device->devlinks = NULL;
901 device->property_devlinks_outdated = true;
902 device->devlinks_generation ++;
905 void device_remove_tag(sd_device *device, const char *tag) {
906 assert(device);
909 free(set_remove(device->tags, tag));
910 device->property_tags_outdated = true;
911 device->tags_generation ++;
914 static int device_tag(sd_device *device, const char *tag, bool add) {
919 assert(device);
922 r = device_get_id_filename(device, &id);
941 int device_tag_index(sd_device *device, sd_device *device_old, bool add) {
948 if (!sd_device_has_tag(device, tag)) {
956 FOREACH_DEVICE_TAG(device, tag) {
957 k = device_tag(device, tag, add);
965 static bool device_has_info(sd_device *device) {
966 assert(device);
968 if (!set_isempty(device->devlinks))
971 if (device->devlink_priority != 0)
974 if (!ordered_hashmap_isempty(device->properties_db))
977 if (!set_isempty(device->tags))
980 if (device->watch_handle >= 0)
986 void device_set_db_persist(sd_device *device) {
987 assert(device);
989 device->db_persist = true;
992 int device_update_db(sd_device *device) {
1000 assert(device);
1002 has_info = device_has_info(device);
1004 r = device_get_id_filename(device, &id);
1011 if (!has_info && major(device->devnum) == 0 && device->ifindex == 0) {
1032 if (device->db_persist) {
1050 if (major(device->devnum) > 0) {
1053 FOREACH_DEVICE_DEVLINK(device, devlink)
1056 if (device->devlink_priority != 0)
1057 fprintf(f, "L:%i\n", device->devlink_priority);
1059 if (device->watch_handle >= 0)
1060 fprintf(f, "W:%i\n", device->watch_handle);
1063 if (device->usec_initialized > 0)
1064 fprintf(f, "I:"USEC_FMT"\n", device->usec_initialized);
1066 ORDERED_HASHMAP_FOREACH_KEY(value, property, device->properties_db, i)
1069 FOREACH_DEVICE_TAG(device, tag)
1084 path, device->devpath);
1092 return log_error_errno(r, "failed to create %s file '%s' for '%s'", has_info ? "db" : "empty", path, device->devpath);
1095 int device_delete_db(sd_device *device) {
1100 assert(device);
1102 r = device_get_id_filename(device, &id);
1115 int device_read_db_force(sd_device *device) {
1116 assert(device);
1118 return device_read_db_aux(device, true);