2 * libudev - interface to udev device information
4 * Copyright (C) 2008 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.
22 #include "libudev-private.h"
24 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
30 l = util_strpcpyl(&s, len, udev_get_dev_path(udev), "/.udev/db/", NULL);
31 return util_path_encode(devpath, s, l);
34 int udev_device_update_db(struct udev_device *udev_device)
36 struct udev *udev = udev_device_get_udev(udev_device);
37 char filename[UTIL_PATH_SIZE];
39 char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
40 size_t devlen = strlen(udev_get_dev_path(udev))+1;
43 struct udev_list_entry *list_entry;
46 devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
47 util_create_path(udev, filename);
50 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
51 if (udev_list_entry_get_flag(list_entry))
53 if (udev_device_get_num_fake_partitions(udev_device) != 0)
55 if (udev_device_get_ignore_remove(udev_device))
57 if (udev_device_get_devlink_priority(udev_device) != 0)
59 if (udev_device_get_event_timeout(udev_device) >= 0)
61 if (udev_device_get_watch_handle(udev_device) >= 0)
63 if (udev_device_get_devnode(udev_device) == NULL)
67 * if we have only the node and symlinks to store, try not to waste
68 * tmpfs memory -- store values, if they fit, in a symlink target
71 l = util_strpcpy(&s, sizeof(target), &udev_device_get_devnode(udev_device)[devlen]);
72 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
73 l = util_strpcpyl(&s, l, " ", &udev_list_entry_get_name(list_entry)[devlen], NULL);
75 info(udev, "size of links too large, create file\n");
79 info(udev, "create db link (%s)\n", target);
80 udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
81 ret = symlink(target, filename);
82 udev_selinux_resetfscreatecon(udev);
86 f = fopen(filename, "w");
88 err(udev, "unable to create db file '%s': %m\n", filename);
91 info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
93 if (udev_device_get_devnode(udev_device) != NULL) {
94 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
95 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
96 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
98 if (udev_device_get_devlink_priority(udev_device) != 0)
99 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
100 if (udev_device_get_event_timeout(udev_device) >= 0)
101 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
102 if (udev_device_get_num_fake_partitions(udev_device) != 0)
103 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
104 if (udev_device_get_ignore_remove(udev_device))
105 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
106 if (udev_device_get_watch_handle(udev_device) >= 0)
107 fprintf(f, "W:%u\n", udev_device_get_watch_handle(udev_device));
108 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
109 if (!udev_list_entry_get_flag(list_entry))
111 fprintf(f, "E:%s=%s\n",
112 udev_list_entry_get_name(list_entry),
113 udev_list_entry_get_value(list_entry));
120 int udev_device_delete_db(struct udev_device *udev_device)
122 struct udev *udev = udev_device_get_udev(udev_device);
123 char filename[UTIL_PATH_SIZE];
125 devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
130 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
132 struct udev *udev = udev_device_get_udev(udev_device);
133 char filename_old[UTIL_PATH_SIZE];
134 char filename[UTIL_PATH_SIZE];
136 devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old));
137 devpath_to_db_path(udev, udev_device_get_devpath(udev_device), filename, sizeof(filename));
138 return rename(filename_old, filename);