]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev_db.c
fix usb_id and let scsi_id ignore "illegal request"
[thirdparty/systemd.git] / udev_db.c
1 /*
2 * udev_db.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stddef.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <dirent.h>
34
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "udev_libc_wrapper.h"
37 #include "udev.h"
38 #include "udev_utils.h"
39 #include "logging.h"
40 #include "udev_db.h"
41
42 #define PATH_TO_NAME_CHAR '@'
43
44 static int get_db_filename(const char *devpath, char *filename, int len)
45 {
46 char temp[PATH_SIZE];
47 char *pos;
48
49 /* replace '/' to transform path into a filename */
50 strlcpy(temp, devpath, sizeof(temp));
51 pos = strchr(&temp[1], '/');
52 while (pos) {
53 pos[0] = PATH_TO_NAME_CHAR;
54 pos = strchr(&pos[1], '/');
55 }
56 snprintf(filename, len, "%s%s", udev_db_path, temp);
57 filename[len-1] = '\0';
58
59 return 0;
60 }
61
62 int udev_db_add_device(struct udevice *udev)
63 {
64 char filename[PATH_SIZE];
65 struct name_entry *name_loop;
66 FILE *f;
67
68 if (udev->test_run)
69 return 0;
70
71 /* don't write anything if udev created only the node with the
72 * kernel name without any interesting data to remember
73 */
74 if (strcmp(udev->name, udev->kernel_name) == 0 &&
75 list_empty(&udev->symlink_list) && list_empty(&udev->env_list) &&
76 !udev->partitions && !udev->ignore_remove) {
77 dbg("nothing interesting to store in udevdb, skip");
78 goto exit;
79 }
80
81 get_db_filename(udev->devpath, filename, sizeof(filename));
82 create_path(filename);
83 f = fopen(filename, "w");
84 if (f == NULL) {
85 err("unable to create db file '%s'", filename);
86 return -1;
87 }
88 dbg("storing data for device '%s' in '%s'", udev->devpath, filename);
89
90 fprintf(f, "P:%s\n", udev->devpath);
91 fprintf(f, "N:%s\n", udev->name);
92 list_for_each_entry(name_loop, &udev->symlink_list, node)
93 fprintf(f, "S:%s\n", name_loop->name);
94 fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt));
95 if (udev->partitions)
96 fprintf(f, "A:%u\n", udev->partitions);
97 if (udev->ignore_remove)
98 fprintf(f, "R:%u\n", udev->ignore_remove);
99 list_for_each_entry(name_loop, &udev->env_list, node)
100 fprintf(f, "E:%s\n", name_loop->name);
101 fclose(f);
102
103 exit:
104 return 0;
105 }
106
107 static int parse_db_file(struct udevice *udev, const char *filename)
108 {
109 char line[PATH_SIZE];
110 unsigned int major, minor;
111 char *bufline;
112 char *buf;
113 size_t bufsize;
114 size_t cur;
115 size_t count;
116
117 if (file_map(filename, &buf, &bufsize) != 0) {
118 err("unable to read db file '%s'", filename);
119 return -1;
120 }
121
122 cur = 0;
123 while (cur < bufsize) {
124 count = buf_get_line(buf, bufsize, cur);
125 bufline = &buf[cur];
126 cur += count+1;
127
128 switch(bufline[0]) {
129 case 'P':
130 if (count > sizeof(udev->devpath))
131 count = sizeof(udev->devpath);
132 strlcpy(udev->devpath, &bufline[2], count-1);
133 break;
134 case 'N':
135 if (count > sizeof(udev->name))
136 count = sizeof(udev->name);
137 strlcpy(udev->name, &bufline[2], count-1);
138 break;
139 case 'M':
140 if (count > sizeof(line))
141 count = sizeof(line);
142 strlcpy(line, &bufline[2], count-1);
143 sscanf(line, "%u:%u", &major, &minor);
144 udev->devt = makedev(major, minor);
145 break;
146 case 'S':
147 if (count > sizeof(line))
148 count = sizeof(line);
149 strlcpy(line, &bufline[2], count-1);
150 name_list_add(&udev->symlink_list, line, 0);
151 break;
152 case 'A':
153 if (count > sizeof(line))
154 count = sizeof(line);
155 strlcpy(line, &bufline[2], count-1);
156 udev->partitions = atoi(line);
157 break;
158 case 'R':
159 if (count > sizeof(line))
160 count = sizeof(line);
161 strlcpy(line, &bufline[2], count-1);
162 udev->ignore_remove = atoi(line);
163 break;
164 case 'E':
165 if (count > sizeof(line))
166 count = sizeof(line);
167 strlcpy(line, &bufline[2], count-1);
168 name_list_add(&udev->env_list, line, 0);
169 break;
170 }
171 }
172 file_unmap(buf, bufsize);
173
174 if (udev->name[0] == '\0')
175 return -1;
176
177 return 0;
178 }
179
180 int udev_db_delete_device(struct udevice *udev)
181 {
182 char filename[PATH_SIZE];
183
184 get_db_filename(udev->devpath, filename, sizeof(filename));
185 unlink(filename);
186
187 return 0;
188 }
189
190 int udev_db_get_device(struct udevice *udev, const char *devpath)
191 {
192 char filename[PATH_SIZE];
193
194 get_db_filename(devpath, filename, sizeof(filename));
195
196 if (parse_db_file(udev, filename) != 0)
197 return -1;
198
199 return 0;
200 }
201
202 int udev_db_search_name(char *devpath, size_t len, const char *name)
203 {
204 DIR *dir;
205
206 dir = opendir(udev_db_path);
207 if (dir == NULL) {
208 err("unable to open udev_db '%s'", udev_db_path);
209 return -1;
210 }
211
212 while (1) {
213 struct dirent *ent;
214 char filename[PATH_SIZE];
215 char path[PATH_SIZE];
216 char nodename[PATH_SIZE];
217 char *bufline;
218 char *buf;
219 size_t bufsize;
220 size_t cur;
221 size_t count;
222
223 ent = readdir(dir);
224 if (ent == NULL || ent->d_name[0] == '\0')
225 break;
226
227 if (ent->d_name[0] == '.')
228 continue;
229
230 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
231 filename[sizeof(filename)-1] = '\0';
232 dbg("looking at '%s'", filename);
233
234 if (file_map(filename, &buf, &bufsize) != 0) {
235 err("unable to read db file '%s'", filename);
236 continue;
237 }
238
239 cur = 0;
240 while (cur < bufsize) {
241 count = buf_get_line(buf, bufsize, cur);
242 bufline = &buf[cur];
243 cur += count+1;
244
245 switch(bufline[0]) {
246 case 'P':
247 if (count > sizeof(path))
248 count = sizeof(path);
249 strlcpy(path, &bufline[2], count-1);
250 break;
251 case 'N':
252 case 'S':
253 if (count > sizeof(nodename))
254 count = sizeof(nodename);
255 strlcpy(nodename, &bufline[2], count-1);
256 dbg("compare '%s' '%s'", nodename, name);
257 if (strcmp(nodename, name) == 0) {
258 strlcpy(devpath, path, len);
259 file_unmap(buf, bufsize);
260 closedir(dir);
261 return 0;
262 }
263 break;
264 default:
265 continue;
266 }
267 }
268 file_unmap(buf, bufsize);
269 }
270
271 closedir(dir);
272 return -1;
273 }
274
275 int udev_db_dump_names(int (*handler_function)(const char *path, const char *name))
276 {
277 DIR *dir;
278
279 dir = opendir(udev_db_path);
280 if (dir == NULL) {
281 err("unable to open udev_db '%s'", udev_db_path);
282 return -1;
283 }
284
285 while (1) {
286 struct dirent *ent;
287 char filename[PATH_SIZE];
288 char path[PATH_SIZE];
289 char nodename[PATH_SIZE];
290 char *bufline;
291 char *buf;
292 size_t bufsize;
293 size_t cur;
294 size_t count;
295
296 ent = readdir(dir);
297 if (ent == NULL || ent->d_name[0] == '\0')
298 break;
299
300 if (ent->d_name[0] == '.')
301 continue;
302
303 snprintf(filename, sizeof(filename), "%s/%s", udev_db_path, ent->d_name);
304 filename[sizeof(filename)-1] = '\0';
305 dbg("looking at '%s'", filename);
306
307 if (file_map(filename, &buf, &bufsize) != 0) {
308 err("unable to read db file '%s'", filename);
309 continue;
310 }
311
312 path[0] = '\0';
313 nodename[0] = '\0';
314 cur = 0;
315 while (cur < bufsize) {
316 count = buf_get_line(buf, bufsize, cur);
317 bufline = &buf[cur];
318 cur += count+1;
319
320 switch(bufline[0]) {
321 case 'P':
322 if (count > sizeof(path))
323 count = sizeof(path);
324 strlcpy(path, &bufline[2], count-1);
325 break;
326 case 'N':
327 if (count > sizeof(nodename))
328 count = sizeof(nodename);
329 strlcpy(nodename, &bufline[2], count-1);
330 break;
331 default:
332 continue;
333 }
334 }
335 file_unmap(buf, bufsize);
336
337 if (path[0] == '\0' || nodename[0] == '\0')
338 continue;
339
340 if (handler_function(path, nodename) != 0)
341 break;
342 }
343
344 closedir(dir);
345 return 0;
346 }