2 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
33 /* translate to location of db file */
34 util_strlcpy(filename, udev_get_dev_path(udev), len);
35 start = util_strlcat(filename, "/.udev/db/", len);
36 util_strlcat(filename, devpath, len);
37 return util_path_encode(&filename[start], len - start);
40 int udev_device_update_db(struct udev_device *udev_device)
42 struct udev *udev = udev_device_get_udev(udev_device);
43 char filename[UTIL_PATH_SIZE];
45 char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
46 size_t devlen = strlen(udev_get_dev_path(udev))+1;
47 struct udev_list_entry *list_entry;
50 devpath_to_db_path(udev,
51 udev_device_get_devpath(udev_device),
52 filename, sizeof(filename));
53 util_create_path(udev, filename);
56 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
57 if (udev_list_entry_get_flag(list_entry))
59 if (udev_device_get_num_fake_partitions(udev_device))
61 if (udev_device_get_ignore_remove(udev_device))
63 /* try not to waste tmpfs memory; store values, if they fit, in a symlink target */
64 util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target));
65 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
68 util_strlcat(target, " ", sizeof(target));
69 len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target));
70 if (len >= sizeof(target)) {
71 info(udev, "size of links too large, create file\n");
75 info(udev, "create db link (%s)\n", target);
76 udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
77 ret = symlink(target, filename);
78 udev_selinux_resetfscreatecon(udev);
82 f = fopen(filename, "w");
84 err(udev, "unable to create db file '%s': %m\n", filename);
87 info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
89 fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
90 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
91 fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
92 if (udev_device_get_devlink_priority(udev_device) != 0)
93 fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
94 if (udev_device_get_event_timeout(udev_device) >= 0)
95 fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
96 if (udev_device_get_num_fake_partitions(udev_device) != 0)
97 fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
98 if (udev_device_get_ignore_remove(udev_device))
99 fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
100 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
101 if (!udev_list_entry_get_flag(list_entry))
103 fprintf(f, "E:%s=%s\n",
104 udev_list_entry_get_name(list_entry),
105 udev_list_entry_get_value(list_entry));
112 int udev_device_delete_db(struct udev_device *udev_device)
114 char filename[UTIL_PATH_SIZE];
116 devpath_to_db_path(udev_device_get_udev(udev_device),
117 udev_device_get_devpath(udev_device),
118 filename, sizeof(filename));
123 int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
125 char filename_old[UTIL_PATH_SIZE];
126 char filename[UTIL_PATH_SIZE];
128 devpath_to_db_path(udev_device_get_udev(udev_device),
130 filename_old, sizeof(filename_old));
131 devpath_to_db_path(udev_device_get_udev(udev_device),
132 udev_device_get_devpath(udev_device),
133 filename, sizeof(filename));
134 return rename(filename_old, filename);