]>
Commit | Line | Data |
---|---|---|
2232cac8 GKH |
1 | /* |
2 | * namedev.c | |
3 | * | |
4 | * Userspace devfs | |
5 | * | |
6 | * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> | |
7 | * | |
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 | #include <stddef.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <stdio.h> | |
28 | #include <fcntl.h> | |
29 | #include <ctype.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
616a7078 | 32 | #include <time.h> |
c27e6911 | 33 | #include <sys/wait.h> |
dac056aa | 34 | #include <sys/stat.h> |
bbbe503e | 35 | #include <sys/sysinfo.h> |
2232cac8 | 36 | |
c80da508 | 37 | #include "libsysfs/sysfs/libsysfs.h" |
2232cac8 GKH |
38 | #include "list.h" |
39 | #include "udev.h" | |
c81b35c0 | 40 | #include "udev_lib.h" |
2232cac8 | 41 | #include "udev_version.h" |
54988802 | 42 | #include "logging.h" |
2232cac8 | 43 | #include "namedev.h" |
2023350e | 44 | #include "klibc_fixups.h" |
2232cac8 | 45 | |
a27cd06c KS |
46 | static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr); |
47 | ||
19feb351 | 48 | LIST_HEAD(config_device_list); |
61219c75 | 49 | LIST_HEAD(perm_device_list); |
2232cac8 | 50 | |
e41016d3 | 51 | |
9f1da361 KS |
52 | /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ |
53 | static int strcmp_pattern(const char *p, const char *s) | |
c124eafa | 54 | { |
8a08e4b1 KS |
55 | if (s[0] == '\0') { |
56 | while (p[0] == '*') | |
9f1da361 | 57 | p++; |
8a08e4b1 | 58 | return (p[0] != '\0'); |
9f1da361 | 59 | } |
8a08e4b1 | 60 | switch (p[0]) { |
9f1da361 KS |
61 | case '[': |
62 | { | |
63 | int not = 0; | |
64 | p++; | |
8a08e4b1 | 65 | if (p[0] == '!') { |
9f1da361 KS |
66 | not = 1; |
67 | p++; | |
68 | } | |
8a08e4b1 | 69 | while ((p[0] != '\0') && (p[0] != ']')) { |
9f1da361 KS |
70 | int match = 0; |
71 | if (p[1] == '-') { | |
8a08e4b1 | 72 | if ((s[0] >= p[0]) && (s[0] <= p[2])) |
9f1da361 KS |
73 | match = 1; |
74 | p += 3; | |
75 | } else { | |
8a08e4b1 | 76 | match = (p[0] == s[0]); |
9f1da361 KS |
77 | p++; |
78 | } | |
79 | if (match ^ not) { | |
8a08e4b1 | 80 | while ((p[0] != '\0') && (p[0] != ']')) |
9f1da361 | 81 | p++; |
8a08e4b1 KS |
82 | if (p[0] == ']') |
83 | return strcmp_pattern(p+1, s+1); | |
9f1da361 KS |
84 | } |
85 | } | |
86 | } | |
87 | break; | |
88 | case '*': | |
89 | if (strcmp_pattern(p, s+1)) | |
90 | return strcmp_pattern(p+1, s); | |
91 | return 0; | |
92 | case '\0': | |
8a08e4b1 | 93 | if (s[0] == '\0') { |
9f1da361 KS |
94 | return 0; |
95 | } | |
96 | break; | |
97 | default: | |
8a08e4b1 | 98 | if ((p[0] == s[0]) || (p[0] == '?')) |
9f1da361 KS |
99 | return strcmp_pattern(p+1, s+1); |
100 | break; | |
101 | } | |
102 | return 1; | |
c124eafa KS |
103 | } |
104 | ||
61219c75 GKH |
105 | static struct perm_device *find_perm(char *name) |
106 | { | |
843d1a84 | 107 | struct perm_device *perm; |
61219c75 | 108 | |
843d1a84 | 109 | list_for_each_entry(perm, &perm_device_list, node) { |
61219c75 GKH |
110 | if (strcmp_pattern(perm->name, name)) |
111 | continue; | |
112 | return perm; | |
113 | } | |
114 | return NULL; | |
115 | } | |
116 | ||
765cbd97 | 117 | static mode_t get_default_mode(void) |
185a35a4 | 118 | { |
89571022 GKH |
119 | mode_t mode = 0600; /* default to owner rw only */ |
120 | ||
267f534d | 121 | if (strlen(default_mode_str) != 0) |
89571022 | 122 | mode = strtol(default_mode_str, NULL, 8); |
267f534d | 123 | |
89571022 | 124 | return mode; |
185a35a4 GKH |
125 | } |
126 | ||
267f534d | 127 | static char *get_default_owner(void) |
74c73ef9 | 128 | { |
267f534d | 129 | if (strlen(default_owner_str) == 0) |
c472e3c8 | 130 | strfieldcpy(default_owner_str, "root"); |
267f534d | 131 | |
74c73ef9 | 132 | return default_owner_str; |
133 | } | |
134 | ||
267f534d | 135 | static char *get_default_group(void) |
74c73ef9 | 136 | { |
267f534d | 137 | if (strlen(default_group_str) == 0) |
c472e3c8 | 138 | strfieldcpy(default_group_str, "root"); |
267f534d | 139 | |
74c73ef9 | 140 | return default_group_str; |
141 | } | |
142 | ||
a27cd06c KS |
143 | /* extract possible {attr} and move str behind it */ |
144 | static char *get_format_attribute(char **str) | |
145 | { | |
146 | char *pos; | |
147 | char *attr = NULL; | |
148 | ||
149 | if (*str[0] == '{') { | |
150 | pos = strchr(*str, '}'); | |
151 | if (pos == NULL) { | |
152 | dbg("missing closing brace for format"); | |
153 | return NULL; | |
154 | } | |
155 | pos[0] = '\0'; | |
156 | attr = *str+1; | |
157 | *str = pos+1; | |
158 | dbg("attribute='%s', str='%s'", attr, *str); | |
159 | } | |
160 | return attr; | |
161 | } | |
162 | ||
163 | /* extract possible format length and move str behind it*/ | |
164 | static int get_format_len(char **str) | |
165 | { | |
166 | int num; | |
167 | char *tail; | |
168 | ||
169 | if (isdigit(*str[0])) { | |
170 | num = (int) strtoul(*str, &tail, 10); | |
aebef544 | 171 | if (num > 0) { |
a27cd06c KS |
172 | *str = tail; |
173 | dbg("format length=%i", num); | |
174 | return num; | |
175 | } else { | |
176 | dbg("format parsing error '%s'", *str); | |
177 | } | |
178 | } | |
179 | return -1; | |
180 | } | |
181 | ||
831f800d KS |
182 | static void apply_format(struct udevice *udev, char *string, size_t maxsize, |
183 | struct sysfs_class_device *class_dev, | |
184 | struct sysfs_device *sysfs_device) | |
f3b04a2e | 185 | { |
b1c5e333 | 186 | char temp[NAME_SIZE]; |
27c3403d | 187 | char temp2[NAME_SIZE]; |
b1c5e333 | 188 | char *tail; |
f3b04a2e | 189 | char *pos; |
a27cd06c | 190 | char *attr; |
63ead27c | 191 | int len; |
88f09368 | 192 | int i; |
a27cd06c | 193 | char c; |
ef672b3d | 194 | char *spos; |
558f80ba | 195 | char *rest; |
ef672b3d | 196 | int slen; |
a27cd06c | 197 | struct sysfs_attribute *tmpattr; |
f3b04a2e | 198 | |
8ffb636f | 199 | pos = string; |
f3b04a2e | 200 | while (1) { |
a36a3c3a KS |
201 | pos = strchr(pos, '%'); |
202 | if (pos == NULL) | |
a27cd06c | 203 | break; |
b1c5e333 | 204 | |
a36a3c3a KS |
205 | pos[0] = '\0'; |
206 | tail = pos+1; | |
207 | len = get_format_len(&tail); | |
208 | c = tail[0]; | |
209 | strfieldcpy(temp, tail+1); | |
210 | tail = temp; | |
211 | dbg("format=%c, string='%s', tail='%s'",c , string, tail); | |
a27cd06c KS |
212 | attr = get_format_attribute(&tail); |
213 | ||
a36a3c3a | 214 | |
a27cd06c KS |
215 | switch (c) { |
216 | case 'b': | |
217 | if (strlen(udev->bus_id) == 0) | |
f3b04a2e | 218 | break; |
17794d77 | 219 | strfieldcatmax(string, udev->bus_id, maxsize); |
a27cd06c KS |
220 | dbg("substitute bus_id '%s'", udev->bus_id); |
221 | break; | |
222 | case 'k': | |
223 | if (strlen(udev->kernel_name) == 0) | |
3e540368 | 224 | break; |
17794d77 | 225 | strfieldcatmax(string, udev->kernel_name, maxsize); |
a27cd06c KS |
226 | dbg("substitute kernel name '%s'", udev->kernel_name); |
227 | break; | |
228 | case 'n': | |
229 | if (strlen(udev->kernel_number) == 0) | |
f3b04a2e | 230 | break; |
17794d77 | 231 | strfieldcatmax(string, udev->kernel_number, maxsize); |
a27cd06c | 232 | dbg("substitute kernel number '%s'", udev->kernel_number); |
f3b04a2e | 233 | break; |
a27cd06c | 234 | case 'm': |
17794d77 | 235 | strintcatmax(string, udev->minor, maxsize); |
a27cd06c KS |
236 | dbg("substitute minor number '%u'", udev->minor); |
237 | break; | |
c58e36c0 | 238 | case 'M': |
17794d77 | 239 | strintcatmax(string, udev->major, maxsize); |
a27cd06c KS |
240 | dbg("substitute major number '%u'", udev->major); |
241 | break; | |
242 | case 'c': | |
243 | if (strlen(udev->program_result) == 0) | |
f3b04a2e | 244 | break; |
88f09368 | 245 | /* get part part of the result string */ |
63ead27c | 246 | i = 0; |
88f09368 | 247 | if (attr != NULL) |
558f80ba | 248 | i = strtoul(attr, &rest, 10); |
88f09368 | 249 | if (i > 0) { |
9fe3f9a9 | 250 | foreach_strpart(udev->program_result, " \n\r", spos, slen) { |
88f09368 | 251 | i--; |
9fe3f9a9 | 252 | if (i == 0) |
a27cd06c | 253 | break; |
b1c5e333 | 254 | } |
9fe3f9a9 KS |
255 | if (i > 0) { |
256 | dbg("requested part of result string not found"); | |
257 | break; | |
a27cd06c | 258 | } |
558f80ba KS |
259 | if (rest[0] == '+') |
260 | strfieldcpy(temp2, spos); | |
261 | else | |
262 | strfieldcpymax(temp2, spos, slen+1); | |
17794d77 | 263 | strfieldcatmax(string, temp2, maxsize); |
27c3403d | 264 | dbg("substitute part of result string '%s'", temp2); |
a27cd06c | 265 | } else { |
17794d77 | 266 | strfieldcatmax(string, udev->program_result, maxsize); |
a27cd06c | 267 | dbg("substitute result string '%s'", udev->program_result); |
f3b04a2e | 268 | } |
f3b04a2e | 269 | break; |
a27cd06c KS |
270 | case 's': |
271 | if (attr != NULL) { | |
272 | tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr); | |
273 | if (tmpattr == NULL) { | |
274 | dbg("sysfa attribute '%s' not found", attr); | |
275 | break; | |
276 | } | |
17794d77 | 277 | strfieldcatmax(string, tmpattr->value, maxsize); |
a27cd06c KS |
278 | dbg("substitute sysfs value '%s'", tmpattr->value); |
279 | } else { | |
280 | dbg("missing attribute"); | |
281 | } | |
282 | break; | |
283 | case '%': | |
17794d77 | 284 | strfieldcatmax(string, "%", maxsize); |
a36a3c3a | 285 | pos++; |
a27cd06c KS |
286 | break; |
287 | default: | |
288 | dbg("unknown substitution type '%%%c'", c); | |
289 | break; | |
290 | } | |
63ead27c KS |
291 | /* truncate to specified length */ |
292 | if (len > 0) | |
293 | pos[len] = '\0'; | |
294 | ||
17794d77 | 295 | strfieldcatmax(string, tail, maxsize); |
f3b04a2e GKH |
296 | } |
297 | } | |
298 | ||
bb051f66 GKH |
299 | /* |
300 | * Note, we can have multiple files for different busses in here due | |
301 | * to the mess that USB has for its device tree... | |
302 | */ | |
dac056aa GKH |
303 | static struct bus_file { |
304 | char *bus; | |
305 | char *file; | |
306 | } bus_files[] = { | |
307 | { .bus = "scsi", .file = "vendor" }, | |
308 | { .bus = "usb", .file = "idVendor" }, | |
bb051f66 | 309 | { .bus = "usb", .file = "iInterface" }, |
b7824727 | 310 | { .bus = "usb-serial", .file = "detach_state" }, |
2441c207 | 311 | { .bus = "ide", .file = "detach_state" }, |
48076332 | 312 | { .bus = "pci", .file = "vendor" }, |
dac056aa GKH |
313 | {} |
314 | }; | |
315 | ||
316 | #define SECONDS_TO_WAIT_FOR_FILE 10 | |
317 | static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device) | |
318 | { | |
319 | /* sleep until we see the file for this specific bus type show up this | |
320 | * is needed because we can easily out-run the kernel in looking for | |
321 | * these files before the paticular subsystem has created them in the | |
322 | * sysfs tree properly. | |
323 | * | |
324 | * And people thought that the /sbin/hotplug event system was going to | |
325 | * be slow, poo on you for arguing that before even testing it... | |
326 | */ | |
327 | struct bus_file *b = &bus_files[0]; | |
328 | struct sysfs_attribute *tmpattr; | |
bb051f66 GKH |
329 | int found = 0; |
330 | int loop = SECONDS_TO_WAIT_FOR_FILE; | |
dac056aa GKH |
331 | |
332 | while (1) { | |
bb051f66 GKH |
333 | if (b->bus == NULL) { |
334 | if (!found) | |
335 | break; | |
336 | /* sleep to give the kernel a chance to create the file */ | |
337 | sleep(1); | |
338 | --loop; | |
339 | if (loop == 0) | |
340 | break; | |
341 | b = &bus_files[0]; | |
342 | } | |
dac056aa | 343 | if (strcmp(sysfs_device->bus, b->bus) == 0) { |
bb051f66 GKH |
344 | found = 1; |
345 | dbg("looking for file '%s' on bus '%s'", b->file, b->bus); | |
346 | tmpattr = sysfs_get_device_attr(sysfs_device, b->file); | |
347 | if (tmpattr) { | |
348 | /* found it! */ | |
349 | goto exit; | |
dac056aa | 350 | } |
bb051f66 | 351 | dbg("can't find '%s' file", b->file); |
dac056aa | 352 | } |
bb051f66 | 353 | ++b; |
dac056aa | 354 | } |
bb051f66 GKH |
355 | if (!found) |
356 | dbg("did not find bus type '%s' on list of bus_id_files, " | |
357 | "contact greg@kroah.com", sysfs_device->bus); | |
dac056aa GKH |
358 | exit: |
359 | return; /* here to prevent compiler warning... */ | |
360 | } | |
19dc5d4c | 361 | |
1b941027 GKH |
362 | static void fix_kernel_name(struct udevice *udev) |
363 | { | |
364 | char *temp = udev->kernel_name; | |
365 | ||
366 | while (*temp != 0x00) { | |
367 | /* Some block devices have a ! in their name, | |
368 | * we need to change that to / */ | |
369 | if (*temp == '!') | |
370 | *temp = '/'; | |
371 | ++temp; | |
372 | } | |
373 | } | |
374 | ||
ac28b86d | 375 | static int execute_program(char *path, char *value, int len) |
c27e6911 PM |
376 | { |
377 | int retval; | |
35b38379 | 378 | int count; |
c27e6911 PM |
379 | int status; |
380 | int fds[2]; | |
381 | pid_t pid; | |
20524642 | 382 | char *pos; |
35b38379 KS |
383 | char arg[PROGRAM_SIZE]; |
384 | char *argv[sizeof(arg) / 2]; | |
bc434511 | 385 | int i; |
c27e6911 | 386 | |
35b38379 KS |
387 | i = 0; |
388 | if (strchr(path, ' ')) { | |
389 | strfieldcpy(arg, path); | |
390 | pos = arg; | |
391 | while (pos != NULL) { | |
392 | if (pos[0] == '\'') { | |
393 | /* don't separate if in apostrophes */ | |
394 | pos++; | |
395 | argv[i] = strsep(&pos, "\'"); | |
396 | while (pos[0] == ' ') | |
397 | pos++; | |
398 | } else { | |
399 | argv[i] = strsep(&pos, " "); | |
400 | } | |
401 | dbg("arg[%i] '%s'", i, argv[i]); | |
402 | i++; | |
403 | } | |
404 | } | |
405 | argv[i] = NULL; | |
406 | ||
c27e6911 PM |
407 | retval = pipe(fds); |
408 | if (retval != 0) { | |
409 | dbg("pipe failed"); | |
410 | return -1; | |
411 | } | |
35b38379 | 412 | |
c27e6911 | 413 | pid = fork(); |
2a25816f KS |
414 | switch(pid) { |
415 | case 0: | |
8f43a65e | 416 | /* child */ |
c27e6911 | 417 | close(STDOUT_FILENO); |
dde05ccb GKH |
418 | |
419 | /* dup write side of pipe to STDOUT */ | |
420 | dup(fds[1]); | |
35b38379 KS |
421 | if (argv[0] != NULL) { |
422 | dbg("execute '%s' with given arguments", argv[0]); | |
423 | retval = execv(argv[0], argv); | |
bc434511 | 424 | } else { |
35b38379 | 425 | dbg("execute '%s' with main argument", path); |
2a25816f | 426 | retval = execv(path, main_argv); |
bc434511 | 427 | } |
35b38379 | 428 | |
dde05ccb | 429 | info(FIELD_PROGRAM " execution of '%s' failed", path); |
2a25816f KS |
430 | exit(1); |
431 | case -1: | |
432 | dbg("fork failed"); | |
433 | return -1; | |
434 | default: | |
8f43a65e | 435 | /* parent reads from fds[0] */ |
c27e6911 PM |
436 | close(fds[1]); |
437 | retval = 0; | |
35b38379 | 438 | i = 0; |
c27e6911 | 439 | while (1) { |
35b38379 KS |
440 | count = read(fds[0], value + i, len - i-1); |
441 | if (count <= 0) | |
c27e6911 | 442 | break; |
35b38379 KS |
443 | |
444 | i += count; | |
445 | if (i >= len-1) { | |
ac28b86d | 446 | dbg("result len %d too short", len); |
c27e6911 | 447 | retval = -1; |
35b38379 | 448 | break; |
c27e6911 PM |
449 | } |
450 | } | |
35b38379 KS |
451 | |
452 | if (count < 0) { | |
453 | dbg("read failed with '%s'", strerror(errno)); | |
c27e6911 PM |
454 | retval = -1; |
455 | } | |
456 | ||
bbbe503e | 457 | if (i > 0 && value[i-1] == '\n') |
35b38379 KS |
458 | i--; |
459 | value[i] = '\0'; | |
460 | dbg("result is '%s'", value); | |
461 | ||
462 | close(fds[0]); | |
463 | wait(&status); | |
464 | ||
c27e6911 | 465 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
ac28b86d | 466 | dbg("exec program status 0x%x", status); |
c27e6911 PM |
467 | retval = -1; |
468 | } | |
469 | } | |
470 | return retval; | |
471 | } | |
472 | ||
a27cd06c | 473 | static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr) |
ca1cc0fe GKH |
474 | { |
475 | struct sysfs_attribute *tmpattr = NULL; | |
a8b01705 GKH |
476 | char *c; |
477 | ||
a27cd06c | 478 | dbg("look for device attribute '%s'", attr); |
a8b01705 | 479 | /* try to find the attribute in the class device directory */ |
a27cd06c | 480 | tmpattr = sysfs_get_classdev_attr(class_dev, attr); |
a8b01705 | 481 | if (tmpattr) |
a27cd06c | 482 | goto attr_found; |
a8b01705 GKH |
483 | |
484 | /* look in the class device directory if present */ | |
485 | if (sysfs_device) { | |
a27cd06c | 486 | tmpattr = sysfs_get_device_attr(sysfs_device, attr); |
a8b01705 | 487 | if (tmpattr) |
a27cd06c | 488 | goto attr_found; |
a8b01705 | 489 | } |
a8b01705 | 490 | |
a27cd06c KS |
491 | return NULL; |
492 | ||
493 | attr_found: | |
494 | c = strchr(tmpattr->value, '\n'); | |
495 | if (c != NULL) | |
496 | c[0] = '\0'; | |
497 | ||
498 | dbg("found attribute '%s'", tmpattr->path); | |
499 | return tmpattr; | |
500 | } | |
501 | ||
502 | static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair) | |
503 | { | |
504 | struct sysfs_attribute *tmpattr; | |
d5f91372 KS |
505 | int i; |
506 | int len; | |
a27cd06c KS |
507 | |
508 | if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0')) | |
509 | return -ENODEV; | |
510 | ||
511 | tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file); | |
512 | if (tmpattr == NULL) | |
513 | return -ENODEV; | |
514 | ||
d5f91372 KS |
515 | /* strip trailing whitespace of value, if not asked to match for it */ |
516 | if (! isspace(pair->value[strlen(pair->value)-1])) { | |
517 | i = len = strlen(tmpattr->value); | |
518 | while (i > 0 && isspace(tmpattr->value[i-1])) | |
519 | i--; | |
520 | if (i < len) { | |
521 | tmpattr->value[i] = '\0'; | |
522 | dbg("remove %i trailing whitespace chars from '%s'", | |
523 | len - i, tmpattr->value); | |
524 | } | |
525 | } | |
526 | ||
a8b01705 GKH |
527 | dbg("compare attribute '%s' value '%s' with '%s'", |
528 | pair->file, tmpattr->value, pair->value); | |
529 | if (strcmp_pattern(pair->value, tmpattr->value) != 0) | |
530 | return -ENODEV; | |
531 | ||
532 | dbg("found matching attribute '%s' with value '%s'", | |
533 | pair->file, pair->value); | |
534 | return 0; | |
535 | } | |
536 | ||
ac28b86d | 537 | static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
a8b01705 GKH |
538 | { |
539 | struct sysfs_pair *pair; | |
a8b01705 | 540 | int i; |
137af0cc | 541 | |
ac28b86d KS |
542 | for (i = 0; i < MAX_SYSFS_PAIRS; ++i) { |
543 | pair = &dev->sysfs_pair[i]; | |
544 | if ((pair->file[0] == '\0') || (pair->value[0] == '\0')) | |
545 | break; | |
546 | if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) { | |
547 | dbg("sysfs attribute doesn't match"); | |
548 | return -ENODEV; | |
ca1cc0fe | 549 | } |
ca1cc0fe | 550 | } |
ac28b86d KS |
551 | |
552 | return 0; | |
ca1cc0fe GKH |
553 | } |
554 | ||
ac28b86d | 555 | static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
ca1cc0fe | 556 | { |
ca1cc0fe | 557 | char path[SYSFS_PATH_MAX]; |
ca1cc0fe GKH |
558 | char *temp = NULL; |
559 | ||
1f7148c7 | 560 | /* we have to have a sysfs device for ID to work */ |
ca1cc0fe GKH |
561 | if (!sysfs_device) |
562 | return -ENODEV; | |
563 | ||
ac28b86d KS |
564 | strfieldcpy(path, sysfs_device->path); |
565 | temp = strrchr(path, '/'); | |
3a8c51a7 | 566 | temp++; |
ac28b86d | 567 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
3a8c51a7 | 568 | if (strcmp_pattern(dev->id, temp) != 0) |
ac28b86d | 569 | return -ENODEV; |
5a42932b PM |
570 | else |
571 | return 0; | |
ca1cc0fe GKH |
572 | } |
573 | ||
ac28b86d | 574 | static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
8c51bbfe | 575 | { |
8c51bbfe GKH |
576 | char path[SYSFS_PATH_MAX]; |
577 | int found; | |
578 | char *temp = NULL; | |
579 | ||
1f7148c7 | 580 | /* we have to have a sysfs device for PLACE to work */ |
8c51bbfe GKH |
581 | if (!sysfs_device) |
582 | return -ENODEV; | |
583 | ||
ac28b86d KS |
584 | found = 0; |
585 | strfieldcpy(path, sysfs_device->path); | |
586 | temp = strrchr(path, '/'); | |
587 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); | |
588 | if (strstr(temp, dev->place) != NULL) { | |
589 | found = 1; | |
590 | } else { | |
591 | *temp = 0x00; | |
8c51bbfe | 592 | temp = strrchr(path, '/'); |
19feb351 | 593 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
ac28b86d | 594 | if (strstr(temp, dev->place) != NULL) |
8c51bbfe | 595 | found = 1; |
8c51bbfe | 596 | } |
ac28b86d KS |
597 | if (!found) { |
598 | dbg("place doesn't match"); | |
599 | return -ENODEV; | |
120d45d0 | 600 | } |
120d45d0 | 601 | |
ac28b86d | 602 | return 0; |
09e52d51 KS |
603 | } |
604 | ||
9a52390c | 605 | static int whitelist_search(struct sysfs_class_device *class_dev) |
606 | { | |
fcf3ce34 | 607 | char *sysblock = "/sys/block"; |
9a52390c | 608 | int i; |
9a52390c | 609 | |
610 | static char *list[] = { | |
611 | "nb", | |
612 | "ram", | |
613 | "loop", | |
614 | "fd", | |
615 | "md", | |
616 | "dos_cd", | |
617 | "double", | |
618 | "flash", | |
619 | "msd", | |
620 | "rflash", | |
621 | "rom", | |
622 | "rrom", | |
623 | "sbpcd", | |
624 | "pcd", | |
625 | "pf", | |
626 | "scd", | |
627 | "ubd", | |
fcf3ce34 | 628 | NULL, |
9a52390c | 629 | }; |
630 | ||
fcf3ce34 GKH |
631 | if (strncmp(class_dev->path, sysblock, strlen(sysblock))) |
632 | return 0; | |
633 | ||
634 | for (i=0; list[i] != NULL; i++) | |
635 | if (!strncmp(class_dev->name, list[i], strlen(list[i]))) | |
636 | return 1; | |
9a52390c | 637 | |
638 | return 0; | |
639 | } | |
640 | ||
dac056aa | 641 | static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev) |
185a35a4 | 642 | { |
dac056aa GKH |
643 | struct sysfs_device *sysfs_device; |
644 | struct sysfs_class_device *class_dev_parent; | |
616a7078 | 645 | struct timespec tspec; |
dac056aa | 646 | int loop; |
07562d6e GKH |
647 | |
648 | /* Figure out where the device symlink is at. For char devices this will | |
649 | * always be in the class_dev->path. But for block devices, it's different. | |
650 | * The main block device will have the device symlink in it's path, but | |
651 | * all partitions have the symlink in its parent directory. | |
652 | * But we need to watch out for block devices that do not have parents, yet | |
653 | * look like a partition (fd0, loop0, etc.) They all do not have a device | |
9a52390c | 654 | * symlink yet. We do sit and spin on waiting for them right now unless |
655 | * they happen to be in the whitelist in which case we exit. | |
dac056aa | 656 | */ |
616a7078 | 657 | class_dev_parent = sysfs_get_classdev_parent(class_dev); |
949e32f2 KS |
658 | if (class_dev_parent != NULL) |
659 | dbg("given class device has a parent, use this instead"); | |
1edbb8d3 | 660 | |
616a7078 AM |
661 | tspec.tv_sec = 0; |
662 | tspec.tv_nsec = 10000000; /* sleep 10 millisec */ | |
663 | loop = 10; | |
dac056aa | 664 | while (loop--) { |
9a52390c | 665 | if (udev_sleep) { |
666 | if (whitelist_search(class_dev)) { | |
667 | sysfs_device = NULL; | |
668 | goto exit; | |
669 | } | |
961e4784 | 670 | nanosleep(&tspec, NULL); |
9a52390c | 671 | } |
949e32f2 | 672 | |
616a7078 AM |
673 | if (class_dev_parent) |
674 | sysfs_device = sysfs_get_classdev_device(class_dev_parent); | |
675 | else | |
676 | sysfs_device = sysfs_get_classdev_device(class_dev); | |
dac056aa | 677 | if (sysfs_device != NULL) |
616a7078 | 678 | goto device_found; |
7bd22a78 | 679 | } |
54988802 | 680 | dbg("timed out waiting for device symlink, continuing on anyway..."); |
949e32f2 | 681 | |
616a7078 AM |
682 | device_found: |
683 | /* We have another issue with just the wait above - the sysfs part of | |
684 | * the kernel may not be quick enough to have created the link to the | |
685 | * device under the "bus" subsystem. Due to this, the sysfs_device->bus | |
686 | * will not contain the actual bus name :( | |
616a7078 AM |
687 | */ |
688 | if (sysfs_device) { | |
616a7078 AM |
689 | if (sysfs_device->bus[0] != '\0') |
690 | goto bus_found; | |
267f534d | 691 | |
616a7078 AM |
692 | loop = 10; |
693 | tspec.tv_nsec = 10000000; | |
694 | while (loop--) { | |
961e4784 GKH |
695 | if (udev_sleep) |
696 | nanosleep(&tspec, NULL); | |
616a7078 AM |
697 | sysfs_get_device_bus(sysfs_device); |
698 | ||
699 | if (sysfs_device->bus[0] != '\0') | |
700 | goto bus_found; | |
701 | } | |
54988802 | 702 | dbg("timed out waiting to find the device bus, continuing on anyway"); |
616a7078 AM |
703 | goto exit; |
704 | bus_found: | |
54988802 | 705 | dbg("device %s is registered with bus '%s'", |
616a7078 AM |
706 | sysfs_device->name, sysfs_device->bus); |
707 | } | |
dac056aa GKH |
708 | exit: |
709 | return sysfs_device; | |
710 | } | |
711 | ||
724257d9 | 712 | static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
dac056aa | 713 | { |
724257d9 | 714 | while (1) { |
ac28b86d KS |
715 | /* check for matching bus value */ |
716 | if (dev->bus[0] != '\0') { | |
717 | if (sysfs_device == NULL) { | |
718 | dbg("device has no bus"); | |
c5118442 | 719 | goto try_parent; |
ac28b86d KS |
720 | } |
721 | dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); | |
722 | if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) { | |
723 | dbg(FIELD_BUS " is not matching"); | |
c5118442 | 724 | goto try_parent; |
ac28b86d KS |
725 | } else { |
726 | dbg(FIELD_BUS " matches"); | |
727 | } | |
728 | } | |
120d45d0 | 729 | |
ac28b86d KS |
730 | /* check for matching kernel name*/ |
731 | if (dev->kernel[0] != '\0') { | |
732 | dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name); | |
733 | if (strcmp_pattern(dev->kernel, class_dev->name) != 0) { | |
734 | dbg(FIELD_KERNEL " is not matching"); | |
c5118442 | 735 | goto try_parent; |
ac28b86d KS |
736 | } else { |
737 | dbg(FIELD_KERNEL " matches"); | |
738 | } | |
739 | } | |
ca1cc0fe | 740 | |
ac28b86d KS |
741 | /* check for matching bus id */ |
742 | if (dev->id[0] != '\0') { | |
743 | dbg("check " FIELD_ID); | |
744 | if (match_id(dev, class_dev, sysfs_device) != 0) { | |
745 | dbg(FIELD_ID " is not matching"); | |
c5118442 | 746 | goto try_parent; |
ac28b86d KS |
747 | } else { |
748 | dbg(FIELD_ID " matches"); | |
749 | } | |
750 | } | |
ca1cc0fe | 751 | |
ac28b86d KS |
752 | /* check for matching place of device */ |
753 | if (dev->place[0] != '\0') { | |
754 | dbg("check " FIELD_PLACE); | |
755 | if (match_place(dev, class_dev, sysfs_device) != 0) { | |
756 | dbg(FIELD_PLACE " is not matching"); | |
c5118442 | 757 | goto try_parent; |
ac28b86d KS |
758 | } else { |
759 | dbg(FIELD_PLACE " matches"); | |
760 | } | |
761 | } | |
8c51bbfe | 762 | |
ac28b86d KS |
763 | /* check for matching sysfs pairs */ |
764 | if (dev->sysfs_pair[0].file[0] != '\0') { | |
765 | dbg("check " FIELD_SYSFS " pairs"); | |
766 | if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) { | |
767 | dbg(FIELD_SYSFS " is not matching"); | |
c5118442 | 768 | goto try_parent; |
ac28b86d KS |
769 | } else { |
770 | dbg(FIELD_SYSFS " matches"); | |
771 | } | |
772 | } | |
773 | ||
774 | /* execute external program */ | |
775 | if (dev->program[0] != '\0') { | |
776 | dbg("check " FIELD_PROGRAM); | |
831f800d KS |
777 | apply_format(udev, dev->program, sizeof(dev->program), |
778 | class_dev, sysfs_device); | |
ac28b86d | 779 | if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) { |
35b38379 | 780 | dbg(FIELD_PROGRAM " returned nonzero"); |
c5118442 | 781 | goto try_parent; |
ac28b86d KS |
782 | } else { |
783 | dbg(FIELD_PROGRAM " returned successful"); | |
784 | } | |
785 | } | |
786 | ||
787 | /* check for matching result of external program */ | |
788 | if (dev->result[0] != '\0') { | |
789 | dbg("check for " FIELD_RESULT | |
790 | " dev->result='%s', udev->program_result='%s'", | |
791 | dev->result, udev->program_result); | |
792 | if (strcmp_pattern(dev->result, udev->program_result) != 0) { | |
793 | dbg(FIELD_RESULT " is not matching"); | |
c5118442 | 794 | goto try_parent; |
ac28b86d KS |
795 | } else { |
796 | dbg(FIELD_RESULT " matches"); | |
797 | } | |
798 | } | |
799 | ||
724257d9 GKH |
800 | /* Yeah, we matched! */ |
801 | return 0; | |
802 | ||
c5118442 KS |
803 | try_parent: |
804 | dbg("try parent sysfs device"); | |
724257d9 GKH |
805 | sysfs_device = sysfs_get_device_parent(sysfs_device); |
806 | if (sysfs_device == NULL) | |
807 | return -ENODEV; | |
808 | dbg("sysfs_device->path='%s'", sysfs_device->path); | |
809 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
810 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
811 | } | |
812 | ||
813 | } | |
814 | ||
815 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev) | |
816 | { | |
817 | struct sysfs_device *sysfs_device = NULL; | |
818 | struct config_device *dev; | |
819 | struct perm_device *perm; | |
bbbe503e | 820 | struct sysinfo info; |
724257d9 GKH |
821 | char *pos; |
822 | ||
823 | udev->mode = 0; | |
824 | ||
825 | /* find the sysfs_device associated with this class device */ | |
826 | sysfs_device = get_sysfs_device(class_dev); | |
827 | if (sysfs_device) { | |
828 | dbg("sysfs_device->path='%s'", sysfs_device->path); | |
829 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
830 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
831 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); | |
832 | wait_for_device_to_initialize(sysfs_device); | |
724257d9 | 833 | } |
1b941027 | 834 | dbg("class_dev->name = '%s'", class_dev->name); |
724257d9 GKH |
835 | |
836 | strfieldcpy(udev->kernel_name, class_dev->name); | |
1b941027 GKH |
837 | fix_kernel_name(udev); |
838 | dbg("udev->kernel_name = '%s'", udev->kernel_name); | |
724257d9 GKH |
839 | |
840 | /* get kernel number */ | |
841 | pos = class_dev->name + strlen(class_dev->name); | |
842 | while (isdigit(*(pos-1))) | |
843 | pos--; | |
844 | strfieldcpy(udev->kernel_number, pos); | |
845 | dbg("kernel_number='%s'", udev->kernel_number); | |
846 | ||
847 | /* look for a matching rule to apply */ | |
848 | list_for_each_entry(dev, &config_device_list, node) { | |
849 | dbg("process rule"); | |
724257d9 | 850 | if (match_rule(dev, class_dev, udev, sysfs_device) == 0) { |
97ed02ee | 851 | if (dev->name[0] == '\0' && dev->symlink[0] == '\0') { |
c5118442 | 852 | info("configured rule in '%s' at line %i applied, '%s' is ignored", |
bd5f8e7c | 853 | dev->config_file, dev->config_line, udev->kernel_name); |
c5118442 KS |
854 | return -1; |
855 | } | |
856 | ||
97ed02ee | 857 | if (dev->symlink[0] != '\0') { |
615ba3e8 | 858 | char temp[NAME_SIZE]; |
ddd5b5dc | 859 | |
97ed02ee | 860 | info("configured rule in '%s' at line %i applied, added symlink '%s'", |
bd5f8e7c | 861 | dev->config_file, dev->config_line, dev->symlink); |
ddd5b5dc KS |
862 | strfieldcpy(temp, dev->symlink); |
863 | apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device); | |
0a5417a0 KS |
864 | if (udev->symlink[0] != '\0') |
865 | strfieldcat(udev->symlink, " "); | |
ddd5b5dc | 866 | strfieldcat(udev->symlink, temp); |
97ed02ee | 867 | } |
868 | ||
869 | if (dev->name[0] != '\0') { | |
949e32f2 KS |
870 | /* apply all_partitions flag only at a main block device */ |
871 | if (dev->partitions > 0 && | |
872 | (udev->type != 'b' || udev->kernel_number[0] != '\0')) | |
873 | continue; | |
874 | ||
97ed02ee | 875 | info("configured rule in '%s' at line %i applied, '%s' becomes '%s'", |
bd5f8e7c | 876 | dev->config_file, dev->config_line, udev->kernel_name, dev->name); |
97ed02ee | 877 | strfieldcpy(udev->name, dev->name); |
878 | goto found; | |
879 | } | |
724257d9 | 880 | } |
ac28b86d | 881 | } |
ac28b86d | 882 | /* no rule was found so we use the kernel name */ |
7a4877bf | 883 | strfieldcpy(udev->name, udev->kernel_name); |
bbbe503e KS |
884 | if (udev->type == 'n') |
885 | goto done; | |
886 | else | |
887 | goto perms; | |
c2405f50 | 888 | |
09e52d51 | 889 | found: |
0a5417a0 | 890 | apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device); |
1e799bab KS |
891 | strfieldcpy(udev->config_file, dev->config_file); |
892 | udev->config_line = dev->config_line; | |
f61d732a KS |
893 | |
894 | if (udev->type == 'n') | |
bbbe503e | 895 | goto done; |
f61d732a | 896 | |
50e5de03 | 897 | udev->partitions = dev->partitions; |
e41016d3 | 898 | |
e41016d3 KS |
899 | /* get permissions given in rule */ |
900 | set_empty_perms(udev, dev->mode, | |
901 | dev->owner, | |
902 | dev->group); | |
903 | ||
bbbe503e | 904 | perms: |
e41016d3 | 905 | /* get permissions given in config file or set defaults */ |
61219c75 | 906 | perm = find_perm(udev->name); |
e41016d3 KS |
907 | if (perm != NULL) { |
908 | set_empty_perms(udev, perm->mode, | |
909 | perm->owner, | |
910 | perm->group); | |
61219c75 | 911 | } else { |
e41016d3 KS |
912 | set_empty_perms(udev, get_default_mode(), |
913 | get_default_owner(), | |
914 | get_default_group()); | |
615e05f8 | 915 | } |
e41016d3 | 916 | |
61219c75 GKH |
917 | dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", |
918 | udev->name, udev->owner, udev->group, udev->mode); | |
7bd22a78 | 919 | |
bbbe503e | 920 | done: |
b608ade8 | 921 | /* store time of action */ |
bbbe503e KS |
922 | sysinfo(&info); |
923 | udev->config_uptime = info.uptime; | |
b608ade8 | 924 | |
120d45d0 | 925 | return 0; |
185a35a4 GKH |
926 | } |
927 | ||
2232cac8 GKH |
928 | int namedev_init(void) |
929 | { | |
930 | int retval; | |
28d6536a | 931 | |
e8baccca | 932 | retval = namedev_init_rules(); |
2232cac8 GKH |
933 | if (retval) |
934 | return retval; | |
935 | ||
936 | retval = namedev_init_permissions(); | |
937 | if (retval) | |
938 | return retval; | |
939 | ||
19feb351 | 940 | dump_config_dev_list(); |
61219c75 | 941 | dump_perm_dev_list(); |
2232cac8 GKH |
942 | return retval; |
943 | } |