2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
24 #include "libudev-private.h"
28 struct udev_device *parent_device;
42 size_t monitor_buf_len;
43 struct udev_list_node devlinks_list;
44 struct udev_list_node properties_list;
45 struct udev_list_node sysattr_list;
46 unsigned long long int seqnum;
49 int num_fake_partitions;
54 unsigned int parent_set:1;
55 unsigned int subsystem_set:1;
56 unsigned int devtype_set:1;
57 unsigned int devlinks_uptodate:1;
58 unsigned int envp_uptodate:1;
59 unsigned int driver_set:1;
60 unsigned int info_loaded:1;
61 unsigned int ignore_remove:1;
64 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
70 l = util_strpcpyl(&s, len, udev_get_dev_path(udev), "/.udev/db/", NULL);
71 return util_path_encode(devpath, s, l);
74 int udev_device_read_db(struct udev_device *udev_device)
77 char filename[UTIL_PATH_SIZE];
78 char line[UTIL_LINE_SIZE];
81 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
83 if (lstat(filename, &stats) != 0) {
84 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
87 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
88 char target[UTIL_PATH_SIZE];
89 char devnode[UTIL_PATH_SIZE];
93 target_len = readlink(filename, target, sizeof(target));
95 target[target_len] = '\0';
97 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
101 next = strchr(target, ' ');
106 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
107 udev_device_set_devnode(udev_device, devnode);
108 while (next != NULL) {
109 char devlink[UTIL_PATH_SIZE];
113 next = strchr(next, ' ');
118 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
119 udev_device_add_devlink(udev_device, devlink);
121 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
125 f = fopen(filename, "r");
127 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
130 while (fgets(line, sizeof(line), f)) {
141 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
142 udev_device_set_devnode(udev_device, filename);
145 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
146 udev_device_add_devlink(udev_device, filename);
149 udev_device_set_devlink_priority(udev_device, atoi(val));
152 udev_device_set_event_timeout(udev_device, atoi(val));
155 udev_device_set_num_fake_partitions(udev_device, atoi(val));
158 udev_device_set_ignore_remove(udev_device, atoi(val));
161 udev_device_add_property_from_string(udev_device, val);
164 udev_device_set_watch_handle(udev_device, atoi(val));
170 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
174 int udev_device_read_uevent_file(struct udev_device *udev_device)
176 char filename[UTIL_PATH_SIZE];
178 char line[UTIL_LINE_SIZE];
182 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
183 f = fopen(filename, "r");
187 while (fgets(line, sizeof(line), f)) {
190 pos = strchr(line, '\n');
195 if (strncmp(line, "DEVTYPE=", 8) == 0)
196 udev_device_set_devtype(udev_device, &line[8]);
197 else if (strncmp(line, "MAJOR=", 6) == 0)
198 maj = strtoull(&line[6], NULL, 10);
199 else if (strncmp(line, "MINOR=", 6) == 0)
200 min = strtoull(&line[6], NULL, 10);
201 else if (strncmp(line, "DEVNAME=", 8) == 0)
202 udev_device_set_knodename(udev_device, &line[8]);
204 udev_device_add_property_from_string(udev_device, line);
207 udev_device->devnum = makedev(maj, min);
213 static void device_load_info(struct udev_device *device)
215 device->info_loaded = 1;
216 udev_device_read_uevent_file(device);
217 udev_device_read_db(device);
220 void udev_device_set_info_loaded(struct udev_device *device)
222 device->info_loaded = 1;
225 struct udev_device *device_new(struct udev *udev)
227 struct udev_device *udev_device;
228 struct udev_list_entry *list_entry;
233 udev_device = calloc(1, sizeof(struct udev_device));
234 if (udev_device == NULL)
236 udev_device->refcount = 1;
237 udev_device->udev = udev;
238 udev_list_init(&udev_device->devlinks_list);
239 udev_list_init(&udev_device->properties_list);
240 udev_list_init(&udev_device->sysattr_list);
241 udev_device->event_timeout = -1;
242 udev_device->watch_handle = -1;
243 /* copy global properties */
244 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
245 udev_device_add_property(udev_device,
246 udev_list_entry_get_name(list_entry),
247 udev_list_entry_get_value(list_entry));
248 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
253 * udev_device_new_from_syspath:
254 * @udev: udev library context
255 * @syspath: sys device path including sys directory
257 * Create new udev device, and fill in information from the sys
258 * device and the udev database entry. The sypath is the absolute
259 * path to the device, including the sys mount point.
261 * The initial refcount is 1, and needs to be decremented to
262 * release the resources of the udev device.
264 * Returns: a new udev device, or #NULL, if it does not exist
266 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
270 char path[UTIL_PATH_SIZE];
273 struct udev_device *udev_device;
280 /* path starts in sys */
281 len = strlen(udev_get_sys_path(udev));
282 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
283 info(udev, "not in sys :%s\n", syspath);
287 /* path is not a root directory */
288 subdir = &syspath[len+1];
289 pos = strrchr(subdir, '/');
290 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
291 dbg(udev, "not a subdir :%s\n", syspath);
295 /* resolve possible symlink to real path */
296 util_strscpy(path, sizeof(path), syspath);
297 util_resolve_sys_link(udev, path, sizeof(path));
299 /* try to resolve the silly block layout if needed */
300 if (strncmp(&path[len], "/block/", 7) == 0) {
301 char block[UTIL_PATH_SIZE];
302 char part[UTIL_PATH_SIZE];
304 util_strscpy(block, sizeof(block), path);
305 pos = strrchr(block, '/');
308 util_strscpy(part, sizeof(part), pos);
310 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0)
311 util_strscpyl(path, sizeof(path), block, part, NULL);
314 /* path exists in sys */
315 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
316 strncmp(&syspath[len], "/class/", 7) == 0 ||
317 strncmp(&syspath[len], "/block/", 7) == 0) {
318 char file[UTIL_PATH_SIZE];
320 /* all "devices" require a "uevent" file */
321 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
322 if (stat(file, &statbuf) != 0) {
323 dbg(udev, "not a device: %s\n", syspath);
327 /* everything else just needs to be a directory */
328 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
329 dbg(udev, "directory not found: %s\n", syspath);
334 udev_device = device_new(udev);
335 if (udev_device == NULL)
338 udev_device_set_syspath(udev_device, path);
339 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
344 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
346 char path[UTIL_PATH_SIZE];
347 const char *type_str;
348 struct udev_enumerate *udev_enumerate;
349 struct udev_list_entry *list_entry;
350 struct udev_device *device = NULL;
354 else if (type == 'c')
359 /* /sys/dev/{block,char}/<maj>:<min> link */
360 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
361 type_str, major(devnum), minor(devnum));
362 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
363 return udev_device_new_from_syspath(udev, path);
365 udev_enumerate = udev_enumerate_new(udev);
366 if (udev_enumerate == NULL)
369 /* fallback to search sys devices for the major/minor */
371 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
372 else if (type == 'c')
373 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
374 udev_enumerate_scan_devices(udev_enumerate);
375 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
376 struct udev_device *device_loop;
378 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
379 if (device_loop != NULL) {
380 if (udev_device_get_devnum(device_loop) == devnum) {
381 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
383 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
385 device = device_loop;
388 udev_device_unref(device_loop);
391 udev_enumerate_unref(udev_enumerate);
395 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
397 char path_full[UTIL_PATH_SIZE];
403 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
405 if (strcmp(subsystem, "subsystem") == 0) {
406 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
407 if (stat(path_full, &statbuf) == 0)
410 util_strscpyl(path, l, "/bus/", sysname, NULL);
411 if (stat(path_full, &statbuf) == 0)
414 util_strscpyl(path, l, "/class/", sysname, NULL);
415 if (stat(path_full, &statbuf) == 0)
420 if (strcmp(subsystem, "module") == 0) {
421 util_strscpyl(path, l, "/module/", sysname, NULL);
422 if (stat(path_full, &statbuf) == 0)
427 if (strcmp(subsystem, "drivers") == 0) {
428 char subsys[UTIL_NAME_SIZE];
431 util_strscpy(subsys, sizeof(subsys), sysname);
432 driver = strchr(subsys, ':');
433 if (driver != NULL) {
437 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
438 if (stat(path_full, &statbuf) == 0)
441 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
442 if (stat(path_full, &statbuf) == 0)
448 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
449 if (stat(path_full, &statbuf) == 0)
452 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
453 if (stat(path_full, &statbuf) == 0)
456 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
457 if (stat(path_full, &statbuf) == 0)
462 return udev_device_new_from_syspath(udev, path_full);
465 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
467 struct udev_device *udev_device_parent = NULL;
468 char path[UTIL_PATH_SIZE];
471 /* follow "device" link in deprecated sys layout */
472 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
473 strncmp(udev_device->devpath, "/block/", 7) == 0) {
474 util_strscpyl(path, sizeof(path), udev_device->syspath, "/device", NULL);
475 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
476 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
477 if (udev_device_parent != NULL)
478 return udev_device_parent;
482 util_strscpy(path, sizeof(path), udev_device->syspath);
483 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
487 pos = strrchr(subdir, '/');
488 if (pos == NULL || pos < &subdir[2])
491 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
492 if (udev_device_parent != NULL)
493 return udev_device_parent;
498 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
500 if (udev_device == NULL)
502 if (!udev_device->parent_set) {
503 udev_device->parent_set = 1;
504 udev_device->parent_device = device_new_from_parent(udev_device);
506 if (udev_device->parent_device != NULL)
507 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
508 return udev_device->parent_device;
511 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
513 struct udev_device *parent;
515 if (subsystem == NULL)
518 parent = udev_device_get_parent(udev_device);
519 while (parent != NULL) {
520 const char *parent_subsystem;
521 const char *parent_devtype;
523 parent_subsystem = udev_device_get_subsystem(parent);
524 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
527 parent_devtype = udev_device_get_devtype(parent);
528 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
531 parent = udev_device_get_parent(parent);
537 * udev_device_get_udev:
538 * @udev_device: udev device
540 * Retrieve the udev library context the device was created with.
542 * Returns: the udev library context
544 struct udev *udev_device_get_udev(struct udev_device *udev_device)
546 if (udev_device == NULL)
548 return udev_device->udev;
553 * @udev_device: udev device
555 * Take a reference of a udev device.
557 * Returns: the passed udev device
559 struct udev_device *udev_device_ref(struct udev_device *udev_device)
561 if (udev_device == NULL)
563 udev_device->refcount++;
569 * @udev_device: udev device
571 * Drop a reference of a udev device. If the refcount reaches zero,
572 * the resources of the device will be released.
575 void udev_device_unref(struct udev_device *udev_device)
577 if (udev_device == NULL)
579 udev_device->refcount--;
580 if (udev_device->refcount > 0)
582 if (udev_device->parent_device != NULL)
583 udev_device_unref(udev_device->parent_device);
584 free(udev_device->syspath);
585 free(udev_device->sysname);
586 free(udev_device->devnode);
587 free(udev_device->subsystem);
588 free(udev_device->devtype);
589 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
590 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
591 free(udev_device->action);
592 free(udev_device->driver);
593 free(udev_device->devpath_old);
594 free(udev_device->knodename);
595 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
596 free(udev_device->envp);
597 free(udev_device->monitor_buf);
598 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
603 * udev_device_get_devpath:
604 * @udev_device: udev device
606 * Retrieve the kernel devpath value of the udev device. The path
607 * does not contain the sys mount point, and starts with a '/'.
609 * Returns: the devpath of the udev device
611 const char *udev_device_get_devpath(struct udev_device *udev_device)
613 if (udev_device == NULL)
615 return udev_device->devpath;
619 * udev_device_get_syspath:
620 * @udev_device: udev device
622 * Retrieve the sys path of the udev device. The path is an
623 * absolute path and starts with the sys mount point.
625 * Returns: the sys path of the udev device
627 const char *udev_device_get_syspath(struct udev_device *udev_device)
629 if (udev_device == NULL)
631 return udev_device->syspath;
634 const char *udev_device_get_sysname(struct udev_device *udev_device)
636 if (udev_device == NULL)
638 return udev_device->sysname;
641 const char *udev_device_get_sysnum(struct udev_device *udev_device)
643 if (udev_device == NULL)
645 return udev_device->sysnum;
649 * udev_device_get_devnode:
650 * @udev_device: udev device
652 * Retrieve the device node file name belonging to the udev device.
653 * The path is an absolute path, and starts with the device directory.
655 * Returns: the device node file name of the udev device, or #NULL if no device node exists
657 const char *udev_device_get_devnode(struct udev_device *udev_device)
659 if (udev_device == NULL)
661 if (!udev_device->info_loaded)
662 device_load_info(udev_device);
663 return udev_device->devnode;
667 * udev_device_get_subsystem:
668 * @udev_device: udev device
670 * Retrieve the subsystem string of the udev device. The string does not
673 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
675 const char *udev_device_get_subsystem(struct udev_device *udev_device)
677 char subsystem[UTIL_NAME_SIZE];
679 if (udev_device == NULL)
681 if (!udev_device->subsystem_set) {
682 udev_device->subsystem_set = 1;
683 /* read "subsytem" link */
684 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
685 udev_device_set_subsystem(udev_device, subsystem);
686 return udev_device->subsystem;
689 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
690 udev_device_set_subsystem(udev_device, "module");
691 return udev_device->subsystem;
693 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
694 udev_device_set_subsystem(udev_device, "drivers");
695 return udev_device->subsystem;
697 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
698 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
699 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
700 udev_device_set_subsystem(udev_device, "subsystem");
701 return udev_device->subsystem;
704 return udev_device->subsystem;
708 * udev_device_get_devtype:
709 * @udev_device: udev device
711 * Retrieve the devtype string of the udev device.
713 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
715 const char *udev_device_get_devtype(struct udev_device *udev_device)
717 if (udev_device == NULL)
719 if (!udev_device->devtype_set) {
720 udev_device->devtype_set = 1;
721 if (!udev_device->info_loaded)
722 udev_device_read_uevent_file(udev_device);
724 return udev_device->devtype;
728 * udev_device_get_devlinks_list_entry:
729 * @udev_device: udev device
731 * Retrieve the list of device links pointing to the device file of
732 * the udev device. The next list entry can be retrieved with
733 * udev_list_entry_next(), which returns #NULL if no more entries exist.
734 * The devlink path can be retrieved from the list entry by
735 * udev_list_entry_get_name(). The path is an absolute path, and starts with
736 * the device directory.
738 * Returns: the first entry of the device node link list
740 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
742 if (udev_device == NULL)
744 if (!udev_device->info_loaded)
745 device_load_info(udev_device);
746 return udev_list_get_entry(&udev_device->devlinks_list);
749 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
751 udev_device->devlinks_uptodate = 0;
752 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
756 * udev_device_get_properties_list_entry:
757 * @udev_device: udev device
759 * Retrieve the list of key/value device properties of the udev
760 * device. The next list entry can be retrieved with udev_list_entry_next(),
761 * which returns #NULL if no more entries exist. The property name
762 * can be retrieved from the list entry by udev_list_get_name(),
763 * the property value by udev_list_get_value().
765 * Returns: the first entry of the property list
767 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
769 if (udev_device == NULL)
771 if (!udev_device->info_loaded)
772 device_load_info(udev_device);
773 if (!udev_device->devlinks_uptodate) {
774 char symlinks[UTIL_PATH_SIZE];
775 struct udev_list_entry *list_entry;
777 udev_device->devlinks_uptodate = 1;
778 list_entry = udev_device_get_devlinks_list_entry(udev_device);
779 if (list_entry != NULL) {
784 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
785 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
786 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
787 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
790 return udev_list_get_entry(&udev_device->properties_list);
793 const char *udev_device_get_driver(struct udev_device *udev_device)
795 char driver[UTIL_NAME_SIZE];
797 if (udev_device == NULL)
799 if (!udev_device->driver_set) {
800 udev_device->driver_set = 1;
801 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
802 udev_device->driver = strdup(driver);
804 return udev_device->driver;
807 dev_t udev_device_get_devnum(struct udev_device *udev_device)
809 if (udev_device == NULL)
810 return makedev(0, 0);
811 if (!udev_device->info_loaded)
812 device_load_info(udev_device);
813 return udev_device->devnum;
816 const char *udev_device_get_action(struct udev_device *udev_device)
818 if (udev_device == NULL)
820 return udev_device->action;
823 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
825 if (udev_device == NULL)
827 return udev_device->seqnum;
830 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
832 struct udev_list_entry *list_entry;
833 char path[UTIL_PATH_SIZE];
838 const char *val = NULL;
840 if (udev_device == NULL)
845 /* look for possibly already cached result */
846 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
847 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
848 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
849 sysattr, udev_list_entry_get_value(list_entry));
850 return udev_list_entry_get_value(list_entry);
854 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
855 if (lstat(path, &statbuf) != 0) {
856 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
857 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
861 if (S_ISLNK(statbuf.st_mode)) {
862 char target[UTIL_NAME_SIZE];
866 /* some core links return the last element of the target path */
867 if (strcmp(sysattr, "driver") != 0 &&
868 strcmp(sysattr, "subsystem") != 0 &&
869 strcmp(sysattr, "module") != 0)
872 len = readlink(path, target, sizeof(target));
875 pos = strrchr(target, '/');
878 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
879 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
880 val = udev_list_entry_get_value(list_entry);
886 /* skip directories */
887 if (S_ISDIR(statbuf.st_mode))
890 /* skip non-readable files */
891 if ((statbuf.st_mode & S_IRUSR) == 0)
894 /* read attribute value */
895 fd = open(path, O_RDONLY);
897 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
900 size = read(fd, value, sizeof(value));
904 if (size == sizeof(value))
907 /* got a valid value, store it in cache and return it */
909 util_remove_trailing_chars(value, '\n');
910 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
911 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
912 val = udev_list_entry_get_value(list_entry);
917 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
922 free(udev_device->syspath);
923 udev_device->syspath = strdup(syspath);
924 if (udev_device->syspath == NULL)
926 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
927 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
929 pos = strrchr(udev_device->syspath, '/');
932 udev_device->sysname = strdup(&pos[1]);
933 if (udev_device->sysname == NULL)
936 /* some devices have '!' in their name, change that to '/' */
938 while (udev_device->sysname[len] != '\0') {
939 if (udev_device->sysname[len] == '!')
940 udev_device->sysname[len] = '/';
944 /* trailing number */
945 while (len > 0 && isdigit(udev_device->sysname[--len]))
946 udev_device->sysnum = &udev_device->sysname[len];
948 /* sysname is completely numeric */
950 udev_device->sysnum = NULL;
955 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
957 free(udev_device->subsystem);
958 udev_device->subsystem = strdup(subsystem);
959 if (udev_device->subsystem == NULL)
961 udev_device->subsystem_set = 1;
962 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
966 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
968 free(udev_device->devtype);
969 udev_device->devtype = strdup(devtype);
970 if (udev_device->devtype == NULL)
972 udev_device->devtype_set = 1;
973 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
977 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
979 free(udev_device->devnode);
980 udev_device->devnode = strdup(devnode);
983 if (udev_device->devnode == NULL)
985 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
989 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
991 udev_device->devlinks_uptodate = 0;
992 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
997 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
999 udev_device->envp_uptodate = 0;
1000 if (value == NULL) {
1001 struct udev_list_entry *list_entry;
1003 list_entry = udev_device_get_properties_list_entry(udev_device);
1004 list_entry = udev_list_entry_get_by_name(list_entry, key);
1005 if (list_entry != NULL)
1006 udev_list_entry_delete(list_entry);
1009 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1012 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1014 char name[UTIL_PATH_SIZE];
1017 util_strscpy(name, sizeof(name), property);
1018 val = strchr(name, '=');
1025 return udev_device_add_property(udev_device, name, val);
1028 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1030 struct udev_list_entry *list_entry;
1032 if (udev_device == NULL)
1037 list_entry = udev_device_get_properties_list_entry(udev_device);
1038 list_entry = udev_list_entry_get_by_name(list_entry, key);
1039 return udev_list_entry_get_value(list_entry);
1042 #define ENVP_SIZE 128
1043 #define MONITOR_BUF_SIZE 4096
1044 static int update_envp_monitor_buf(struct udev_device *udev_device)
1046 struct udev_list_entry *list_entry;
1051 /* monitor buffer of property strings */
1052 free(udev_device->monitor_buf);
1053 udev_device->monitor_buf_len = 0;
1054 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1055 if (udev_device->monitor_buf == NULL)
1058 /* envp array, strings will point into monitor buffer */
1059 if (udev_device->envp == NULL)
1060 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1061 if (udev_device->envp == NULL)
1065 s = udev_device->monitor_buf;
1066 l = MONITOR_BUF_SIZE;
1067 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1068 /* add string to envp array */
1069 udev_device->envp[i++] = s;
1070 if (i+1 >= ENVP_SIZE)
1073 /* add property string to monitor buffer */
1074 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), "=",
1075 udev_list_entry_get_value(list_entry), NULL);
1080 udev_device->envp[i] = NULL;
1081 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1082 udev_device->envp_uptodate = 1;
1083 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1084 i, udev_device->monitor_buf_len);
1088 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1090 if (!udev_device->envp_uptodate)
1091 if (update_envp_monitor_buf(udev_device) != 0)
1093 return udev_device->envp;
1096 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1098 if (!udev_device->envp_uptodate)
1099 if (update_envp_monitor_buf(udev_device) != 0)
1101 *buf = udev_device->monitor_buf;
1102 return udev_device->monitor_buf_len;
1105 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1107 free(udev_device->action);
1108 udev_device->action = strdup(action);
1109 if (udev_device->action == NULL)
1111 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1115 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1117 free(udev_device->driver);
1118 udev_device->driver = strdup(driver);
1119 if (udev_device->driver == NULL)
1121 udev_device->driver_set = 1;
1122 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1126 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1128 return udev_device->devpath_old;
1131 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1133 udev_device->devpath_old = strdup(devpath_old);
1134 if (udev_device->devpath_old == NULL)
1136 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1140 const char *udev_device_get_knodename(struct udev_device *udev_device)
1142 return udev_device->knodename;
1145 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1147 udev_device->knodename = strdup(knodename);
1148 if (udev_device->knodename == NULL)
1153 int udev_device_get_timeout(struct udev_device *udev_device)
1155 return udev_device->timeout;
1158 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1160 udev_device->timeout = timeout;
1163 int udev_device_get_event_timeout(struct udev_device *udev_device)
1165 if (!udev_device->info_loaded)
1166 device_load_info(udev_device);
1167 return udev_device->event_timeout;
1170 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1172 udev_device->event_timeout = event_timeout;
1176 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1180 udev_device->seqnum = seqnum;
1181 snprintf(num, sizeof(num), "%llu", seqnum);
1182 udev_device_add_property(udev_device, "SEQNUM", num);
1186 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1190 udev_device->devnum = devnum;
1192 snprintf(num, sizeof(num), "%u", major(devnum));
1193 udev_device_add_property(udev_device, "MAJOR", num);
1194 snprintf(num, sizeof(num), "%u", minor(devnum));
1195 udev_device_add_property(udev_device, "MINOR", num);
1199 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1201 if (!udev_device->info_loaded)
1202 device_load_info(udev_device);
1203 return udev_device->num_fake_partitions;
1206 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1208 udev_device->num_fake_partitions = num;
1212 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1214 if (!udev_device->info_loaded)
1215 device_load_info(udev_device);
1216 return udev_device->devlink_priority;
1219 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1221 udev_device->devlink_priority = prio;
1225 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1227 if (!udev_device->info_loaded)
1228 device_load_info(udev_device);
1229 return udev_device->ignore_remove;
1232 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1234 udev_device->ignore_remove = ignore;
1238 int udev_device_get_watch_handle(struct udev_device *udev_device)
1240 if (!udev_device->info_loaded)
1241 device_load_info(udev_device);
1242 return udev_device->watch_handle;
1245 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1247 udev_device->watch_handle = handle;