2 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <sys/types.h>
34 #include "udev_selinux.h"
37 static size_t devpath_to_db_path(const char *devpath, char *filename, size_t len)
41 /* translate to location of db file */
42 strlcpy(filename, udev_root, len);
43 start = strlcat(filename, "/.udev/db/", len);
44 strlcat(filename, devpath, len);
45 return path_encode(&filename[start], len - start);
48 /* reverse mapping from the device file name to the devpath */
49 static int name_index(const char *devpath, const char *name, int add)
51 char device[PATH_SIZE];
52 char filename[PATH_SIZE * 2];
56 /* directory with device name */
57 strlcpy(filename, udev_root, sizeof(filename));
58 start = strlcat(filename, "/.udev/names/", sizeof(filename));
59 strlcat(filename, name, sizeof(filename));
60 path_encode(&filename[start], sizeof(filename) - start);
61 /* entry with the devpath */
62 strlcpy(device, devpath, sizeof(device));
63 path_encode(device, sizeof(device));
64 strlcat(filename, "/", sizeof(filename));
65 strlcat(filename, device, sizeof(filename));
68 info("creating index: '%s'\n", filename);
69 create_path(filename);
70 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
74 info("removing index: '%s'\n", filename);
76 delete_path(filename);
81 int udev_db_get_devices_by_name(const char *name, struct list_head *name_list)
83 char dirname[PATH_MAX];
88 strlcpy(dirname, udev_root, sizeof(dirname));
89 start = strlcat(dirname, "/.udev/names/", sizeof(dirname));
90 strlcat(dirname, name, sizeof(dirname));
91 path_encode(&dirname[start], sizeof(dirname) - start);
93 dir = opendir(dirname);
95 info("no index directory '%s': %s\n", dirname, strerror(errno));
100 info("found index directory '%s'\n", dirname);
103 char device[PATH_SIZE];
106 if (ent == NULL || ent->d_name[0] == '\0')
108 if (ent->d_name[0] == '.')
111 strlcpy(device, ent->d_name, sizeof(device));
113 name_list_add(name_list, device, 0);
121 int udev_db_rename(const char *devpath_old, const char *devpath)
123 char filename[PATH_SIZE];
124 char filename_old[PATH_SIZE];
126 devpath_to_db_path(devpath_old, filename_old, sizeof(filename_old));
127 devpath_to_db_path(devpath, filename, sizeof(filename));
128 return rename(filename_old, filename);
131 int udev_db_add_device(struct udevice *udev)
133 char filename[PATH_SIZE];
138 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
139 create_path(filename);
143 * don't waste tmpfs memory pages, if we don't have any data to store
144 * create fake db-file; store the node-name in a symlink target
146 if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
147 !udev->partitions && !udev->ignore_remove) {
149 dbg("nothing interesting to store, create symlink\n");
150 selinux_setfscreatecon(filename, NULL, S_IFLNK);
151 ret = symlink(udev->name, filename);
152 selinux_resetfscreatecon();
154 err("unable to create db link '%s': %s\n", filename, strerror(errno));
159 struct name_entry *name_loop;
161 f = fopen(filename, "w");
163 err("unable to create db file '%s': %s\n", filename, strerror(errno));
166 dbg("storing data for device '%s' in '%s'\n", udev->dev->devpath, filename);
168 fprintf(f, "N:%s\n", udev->name);
169 list_for_each_entry(name_loop, &udev->symlink_list, node) {
170 fprintf(f, "S:%s\n", name_loop->name);
171 /* add symlink-name to index */
172 name_index(udev->dev->devpath, name_loop->name, 1);
174 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
175 if (udev->link_priority != 0)
176 fprintf(f, "L:%u\n", udev->link_priority);
177 if (udev->event_timeout >= 0)
178 fprintf(f, "T:%u\n", udev->event_timeout);
179 if (udev->partitions != 0)
180 fprintf(f, "A:%u\n", udev->partitions);
181 if (udev->ignore_remove)
182 fprintf(f, "R:%u\n", udev->ignore_remove);
183 list_for_each_entry(name_loop, &udev->env_list, node)
184 fprintf(f, "E:%s\n", name_loop->name);
188 /* add name to index */
189 name_index(udev->dev->devpath, udev->name, 1);
194 int udev_db_get_device(struct udevice *udev, const char *devpath)
197 char filename[PATH_SIZE];
198 char line[PATH_SIZE];
199 unsigned int maj, min;
206 sysfs_device_set_values(udev->dev, devpath, NULL, NULL);
207 devpath_to_db_path(devpath, filename, sizeof(filename));
209 if (lstat(filename, &stats) != 0) {
210 info("no db file to read %s: %s\n", filename, strerror(errno));
213 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
214 char target[NAME_SIZE];
217 info("found a symlink as db file\n");
218 target_len = readlink(filename, target, sizeof(target));
220 target[target_len] = '\0';
222 info("error reading db link %s: %s\n", filename, strerror(errno));
225 dbg("db link points to '%s'\n", target);
226 strlcpy(udev->name, target, sizeof(udev->name));
230 if (file_map(filename, &buf, &bufsize) != 0) {
231 info("error reading db file %s: %s\n", filename, strerror(errno));
236 while (cur < bufsize) {
237 count = buf_get_line(buf, bufsize, cur);
241 if (count > sizeof(line))
242 count = sizeof(line);
243 memcpy(line, &bufline[2], count-2);
244 line[count-2] = '\0';
248 strlcpy(udev->name, line, sizeof(udev->name));
251 sscanf(line, "%u:%u", &maj, &min);
252 udev->devt = makedev(maj, min);
255 name_list_add(&udev->symlink_list, line, 0);
258 udev->link_priority = atoi(line);
261 udev->event_timeout = atoi(line);
264 udev->partitions = atoi(line);
267 udev->ignore_remove = atoi(line);
270 name_list_add(&udev->env_list, line, 0);
274 file_unmap(buf, bufsize);
276 if (udev->name[0] == '\0')
282 int udev_db_delete_device(struct udevice *udev)
284 char filename[PATH_SIZE];
285 struct name_entry *name_loop;
290 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
293 name_index(udev->dev->devpath, udev->name, 0);
294 list_for_each_entry(name_loop, &udev->symlink_list, node)
295 name_index(udev->dev->devpath, name_loop->name, 0);
300 int udev_db_get_all_entries(struct list_head *name_list)
302 char dbpath[PATH_MAX];
305 strlcpy(dbpath, udev_root, sizeof(dbpath));
306 strlcat(dbpath, "/.udev/db", sizeof(dbpath));
307 dir = opendir(dbpath);
309 info("no udev_db available '%s': %s\n", dbpath, strerror(errno));
315 char device[PATH_SIZE];
318 if (ent == NULL || ent->d_name[0] == '\0')
320 if (ent->d_name[0] == '.')
323 strlcpy(device, ent->d_name, sizeof(device));
325 name_list_add(name_list, device, 1);
326 dbg("added '%s'\n", device);