2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
33 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
37 /* translate to location of db file */
38 util_strlcpy(filename, udev_get_dev_path(udev), len);
39 start = util_strlcat(filename, "/.udev/db/", len);
40 util_strlcat(filename, devpath, len);
41 return util_path_encode(&filename[start], len - start);
44 /* reverse mapping from the device file name to the devpath */
45 static int name_index(struct udev *udev, const char *devpath, const char *name, int add)
47 char device[UTIL_PATH_SIZE];
48 char filename[UTIL_PATH_SIZE * 2];
52 /* directory with device name */
53 util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
54 start = util_strlcat(filename, "/.udev/names/", sizeof(filename));
55 util_strlcat(filename, name, sizeof(filename));
56 util_path_encode(&filename[start], sizeof(filename) - start);
57 /* entry with the devpath */
58 util_strlcpy(device, devpath, sizeof(device));
59 util_path_encode(device, sizeof(device));
60 util_strlcat(filename, "/", sizeof(filename));
61 util_strlcat(filename, device, sizeof(filename));
64 info(udev, "creating index: '%s'\n", filename);
65 create_path(udev, filename);
66 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
70 info(udev, "removing index: '%s'\n", filename);
72 delete_path(udev, filename);
77 int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list)
79 char dirname[PATH_MAX];
84 util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
85 start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname));
86 util_strlcat(dirname, name, sizeof(dirname));
87 util_path_encode(&dirname[start], sizeof(dirname) - start);
89 dir = opendir(dirname);
91 info(udev, "no index directory '%s': %m\n", dirname);
96 info(udev, "found index directory '%s'\n", dirname);
99 char device[UTIL_PATH_SIZE];
102 if (ent == NULL || ent->d_name[0] == '\0')
104 if (ent->d_name[0] == '.')
107 util_strlcpy(device, ent->d_name, sizeof(device));
108 util_path_decode(device);
109 name_list_add(udev, name_list, device, 0);
117 int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath)
119 char filename[UTIL_PATH_SIZE];
120 char filename_old[UTIL_PATH_SIZE];
122 devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old));
123 devpath_to_db_path(udev, devpath, filename, sizeof(filename));
124 return rename(filename_old, filename);
127 int udev_db_add_device(struct udevice *udevice)
129 char filename[UTIL_PATH_SIZE];
131 if (udevice->test_run)
134 devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
135 create_path(udevice->udev, filename);
139 * don't waste tmpfs memory pages, if we don't have any data to store
140 * create fake db-file; store the node-name in a symlink target
142 if (list_empty(&udevice->symlink_list) && list_empty(&udevice->env_list) &&
143 !udevice->partitions && !udevice->ignore_remove) {
145 dbg(udevice->udev, "nothing interesting to store, create symlink\n");
146 udev_selinux_setfscreatecon(udevice->udev, filename, S_IFLNK);
147 ret = symlink(udevice->name, filename);
148 udev_selinux_resetfscreatecon(udevice->udev);
150 err(udevice->udev, "unable to create db link '%s': %m\n", filename);
155 struct name_entry *name_loop;
157 f = fopen(filename, "w");
159 err(udevice->udev, "unable to create db file '%s': %m\n", filename);
162 dbg(udevice->udev, "storing data for device '%s' in '%s'\n", udevice->dev->devpath, filename);
164 fprintf(f, "N:%s\n", udevice->name);
165 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
166 fprintf(f, "S:%s\n", name_loop->name);
167 /* add symlink-name to index */
168 name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1);
170 fprintf(f, "M:%u:%u\n", major(udevice->devt), minor(udevice->devt));
171 if (udevice->link_priority != 0)
172 fprintf(f, "L:%u\n", udevice->link_priority);
173 if (udevice->event_timeout >= 0)
174 fprintf(f, "T:%u\n", udevice->event_timeout);
175 if (udevice->partitions != 0)
176 fprintf(f, "A:%u\n", udevice->partitions);
177 if (udevice->ignore_remove)
178 fprintf(f, "R:%u\n", udevice->ignore_remove);
179 list_for_each_entry(name_loop, &udevice->env_list, node)
180 fprintf(f, "E:%s\n", name_loop->name);
184 /* add name to index */
185 name_index(udevice->udev, udevice->dev->devpath, udevice->name, 1);
190 int udev_db_get_device(struct udevice *udevice, const char *devpath)
193 char filename[UTIL_PATH_SIZE];
194 char line[UTIL_PATH_SIZE];
195 unsigned int maj, min;
202 sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
203 devpath_to_db_path(udevice->udev, devpath, filename, sizeof(filename));
205 if (lstat(filename, &stats) != 0) {
206 info(udevice->udev, "no db file to read %s: %m\n", filename);
209 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
210 char target[UTIL_NAME_SIZE];
213 info(udevice->udev, "found a symlink as db file\n");
214 target_len = readlink(filename, target, sizeof(target));
216 target[target_len] = '\0';
218 info(udevice->udev, "error reading db link %s: %m\n", filename);
221 dbg(udevice->udev, "db link points to '%s'\n", target);
222 util_strlcpy(udevice->name, target, sizeof(udevice->name));
226 if (file_map(filename, &buf, &bufsize) != 0) {
227 info(udevice->udev, "error reading db file %s: %m\n", filename);
232 while (cur < bufsize) {
233 count = buf_get_line(buf, bufsize, cur);
237 if (count > sizeof(line))
238 count = sizeof(line);
239 memcpy(line, &bufline[2], count-2);
240 line[count-2] = '\0';
244 util_strlcpy(udevice->name, line, sizeof(udevice->name));
247 sscanf(line, "%u:%u", &maj, &min);
248 udevice->devt = makedev(maj, min);
251 name_list_add(udevice->udev, &udevice->symlink_list, line, 0);
254 udevice->link_priority = atoi(line);
257 udevice->event_timeout = atoi(line);
260 udevice->partitions = atoi(line);
263 udevice->ignore_remove = atoi(line);
266 name_list_add(udevice->udev, &udevice->env_list, line, 0);
270 file_unmap(buf, bufsize);
272 if (udevice->name[0] == '\0')
278 int udev_db_delete_device(struct udevice *udevice)
280 char filename[UTIL_PATH_SIZE];
281 struct name_entry *name_loop;
283 if (udevice->test_run)
286 devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
289 name_index(udevice->udev, udevice->dev->devpath, udevice->name, 0);
290 list_for_each_entry(name_loop, &udevice->symlink_list, node)
291 name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 0);