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>
36 static size_t devpath_to_db_path(const char *devpath, char *filename, size_t len)
40 /* translate to location of db file */
41 strlcpy(filename, udev_root, len);
42 start = strlcat(filename, "/"DB_DIR"/", len);
43 strlcat(filename, devpath, len);
44 return path_encode(&filename[start], len - start);
47 /* reverse mapping from the device file name to the devpath */
48 static int name_index(const char *devpath, const char *name, int add)
50 char device[PATH_SIZE];
51 char filename[PATH_SIZE * 2];
55 /* directory with device name */
56 strlcpy(filename, udev_root, sizeof(filename));
57 start = strlcat(filename, "/"DB_NAME_INDEX_DIR"/", sizeof(filename));
58 strlcat(filename, name, sizeof(filename));
59 path_encode(&filename[start], sizeof(filename) - start);
60 /* entry with the devpath */
61 strlcpy(device, devpath, sizeof(device));
62 path_encode(device, sizeof(device));
63 strlcat(filename, "/", sizeof(filename));
64 strlcat(filename, device, sizeof(filename));
67 info("creating index: '%s'", filename);
68 create_path(filename);
69 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
73 info("removing index: '%s'", filename);
75 delete_path(filename);
80 int udev_db_get_devices_by_name(const char *name, struct list_head *name_list)
82 char dirname[PATH_MAX];
87 strlcpy(dirname, udev_root, sizeof(dirname));
88 start = strlcat(dirname, "/"DB_NAME_INDEX_DIR"/", sizeof(dirname));
89 strlcat(dirname, name, sizeof(dirname));
90 path_encode(&dirname[start], sizeof(dirname) - start);
92 dir = opendir(dirname);
94 info("no index directory '%s': %s", dirname, strerror(errno));
99 info("found index directory '%s'", dirname);
102 char device[PATH_SIZE];
105 if (ent == NULL || ent->d_name[0] == '\0')
107 if (ent->d_name[0] == '.')
110 strlcpy(device, ent->d_name, sizeof(device));
112 name_list_add(name_list, device, 0);
120 int udev_db_rename(const char *devpath_old, const char *devpath)
122 char filename[PATH_SIZE];
123 char filename_old[PATH_SIZE];
125 devpath_to_db_path(devpath_old, filename_old, sizeof(filename_old));
126 devpath_to_db_path(devpath, filename, sizeof(filename));
127 return rename(filename_old, filename);
130 int udev_db_add_device(struct udevice *udev)
132 char filename[PATH_SIZE];
137 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
138 create_path(filename);
142 * don't waste tmpfs memory pages, if we don't have any data to store
143 * create fake db-file; store the node-name in a symlink target
145 if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
146 !udev->partitions && !udev->ignore_remove) {
148 dbg("nothing interesting to store, create symlink");
149 selinux_setfscreatecon(filename, NULL, S_IFLNK);
150 ret = symlink(udev->name, filename);
151 selinux_resetfscreatecon();
153 err("unable to create db link '%s': %s", filename, strerror(errno));
158 struct name_entry *name_loop;
160 f = fopen(filename, "w");
162 err("unable to create db file '%s': %s", filename, strerror(errno));
165 dbg("storing data for device '%s' in '%s'", udev->dev->devpath, filename);
167 fprintf(f, "N:%s\n", udev->name);
168 list_for_each_entry(name_loop, &udev->symlink_list, node) {
169 fprintf(f, "S:%s\n", name_loop->name);
170 /* add symlink-name to index */
171 name_index(udev->dev->devpath, name_loop->name, 1);
173 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
174 if (udev->link_priority != 0)
175 fprintf(f, "L:%u\n", udev->link_priority);
176 if (udev->partitions != 0)
177 fprintf(f, "A:%u\n", udev->partitions);
178 if (udev->ignore_remove)
179 fprintf(f, "R:%u\n", udev->ignore_remove);
180 list_for_each_entry(name_loop, &udev->env_list, node)
181 fprintf(f, "E:%s\n", name_loop->name);
185 /* add name to index */
186 name_index(udev->dev->devpath, udev->name, 1);
191 int udev_db_get_device(struct udevice *udev, const char *devpath)
194 char filename[PATH_SIZE];
195 char line[PATH_SIZE];
196 unsigned int maj, min;
203 sysfs_device_set_values(udev->dev, devpath, NULL, NULL);
204 devpath_to_db_path(devpath, filename, sizeof(filename));
206 if (lstat(filename, &stats) != 0) {
207 info("no db file to read %s: %s", filename, strerror(errno));
210 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
211 char target[NAME_SIZE];
214 info("found a symlink as db file");
215 target_len = readlink(filename, target, sizeof(target));
217 target[target_len] = '\0';
219 info("error reading db link %s: %s", filename, strerror(errno));
222 dbg("db link points to '%s'", target);
223 strlcpy(udev->name, target, sizeof(udev->name));
227 if (file_map(filename, &buf, &bufsize) != 0) {
228 info("error reading db file %s: %s", filename, strerror(errno));
233 while (cur < bufsize) {
234 count = buf_get_line(buf, bufsize, cur);
240 if (count > sizeof(udev->name))
241 count = sizeof(udev->name);
242 memcpy(udev->name, &bufline[2], count-2);
243 udev->name[count-2] = '\0';
246 if (count > sizeof(line))
247 count = sizeof(line);
248 memcpy(line, &bufline[2], count-2);
249 line[count-2] = '\0';
250 sscanf(line, "%u:%u", &maj, &min);
251 udev->devt = makedev(maj, min);
254 if (count > sizeof(line))
255 count = sizeof(line);
256 memcpy(line, &bufline[2], count-2);
257 line[count-2] = '\0';
258 name_list_add(&udev->symlink_list, line, 0);
261 if (count > sizeof(line))
262 count = sizeof(line);
263 memcpy(line, &bufline[2], count-2);
264 line[count-2] = '\0';
265 udev->link_priority = atoi(line);
268 if (count > sizeof(line))
269 count = sizeof(line);
270 memcpy(line, &bufline[2], count-2);
271 line[count-2] = '\0';
272 udev->partitions = atoi(line);
275 if (count > sizeof(line))
276 count = sizeof(line);
277 memcpy(line, &bufline[2], count-2);
278 line[count-2] = '\0';
279 udev->ignore_remove = atoi(line);
282 if (count > sizeof(line))
283 count = sizeof(line);
284 memcpy(line, &bufline[2], count-2);
285 line[count-2] = '\0';
286 name_list_add(&udev->env_list, line, 0);
290 file_unmap(buf, bufsize);
292 if (udev->name[0] == '\0')
298 int udev_db_delete_device(struct udevice *udev)
300 char filename[PATH_SIZE];
301 struct name_entry *name_loop;
306 devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename));
309 name_index(udev->dev->devpath, udev->name, 0);
310 list_for_each_entry(name_loop, &udev->symlink_list, node)
311 name_index(udev->dev->devpath, name_loop->name, 0);
316 int udev_db_get_all_entries(struct list_head *name_list)
318 char dbpath[PATH_MAX];
321 strlcpy(dbpath, udev_root, sizeof(dbpath));
322 strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
323 dir = opendir(dbpath);
325 info("no udev_db available '%s': %s", dbpath, strerror(errno));
331 char device[PATH_SIZE];
334 if (ent == NULL || ent->d_name[0] == '\0')
336 if (ent->d_name[0] == '.')
339 strlcpy(device, ent->d_name, sizeof(device));
341 name_list_add(name_list, device, 1);
342 dbg("added '%s'", device);