]>
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> |
2232cac8 GKH |
35 | |
36 | #include "list.h" | |
37 | #include "udev.h" | |
38 | #include "udev_version.h" | |
54988802 | 39 | #include "logging.h" |
2232cac8 | 40 | #include "namedev.h" |
185a35a4 | 41 | #include "libsysfs/libsysfs.h" |
2023350e | 42 | #include "klibc_fixups.h" |
2232cac8 | 43 | |
19feb351 | 44 | LIST_HEAD(config_device_list); |
61219c75 | 45 | LIST_HEAD(perm_device_list); |
2232cac8 | 46 | |
9f1da361 KS |
47 | /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ |
48 | static int strcmp_pattern(const char *p, const char *s) | |
c124eafa | 49 | { |
8a08e4b1 KS |
50 | if (s[0] == '\0') { |
51 | while (p[0] == '*') | |
9f1da361 | 52 | p++; |
8a08e4b1 | 53 | return (p[0] != '\0'); |
9f1da361 | 54 | } |
8a08e4b1 | 55 | switch (p[0]) { |
9f1da361 KS |
56 | case '[': |
57 | { | |
58 | int not = 0; | |
59 | p++; | |
8a08e4b1 | 60 | if (p[0] == '!') { |
9f1da361 KS |
61 | not = 1; |
62 | p++; | |
63 | } | |
8a08e4b1 | 64 | while ((p[0] != '\0') && (p[0] != ']')) { |
9f1da361 KS |
65 | int match = 0; |
66 | if (p[1] == '-') { | |
8a08e4b1 | 67 | if ((s[0] >= p[0]) && (s[0] <= p[2])) |
9f1da361 KS |
68 | match = 1; |
69 | p += 3; | |
70 | } else { | |
8a08e4b1 | 71 | match = (p[0] == s[0]); |
9f1da361 KS |
72 | p++; |
73 | } | |
74 | if (match ^ not) { | |
8a08e4b1 | 75 | while ((p[0] != '\0') && (p[0] != ']')) |
9f1da361 | 76 | p++; |
8a08e4b1 KS |
77 | if (p[0] == ']') |
78 | return strcmp_pattern(p+1, s+1); | |
9f1da361 KS |
79 | } |
80 | } | |
81 | } | |
82 | break; | |
83 | case '*': | |
84 | if (strcmp_pattern(p, s+1)) | |
85 | return strcmp_pattern(p+1, s); | |
86 | return 0; | |
87 | case '\0': | |
8a08e4b1 | 88 | if (s[0] == '\0') { |
9f1da361 KS |
89 | return 0; |
90 | } | |
91 | break; | |
92 | default: | |
8a08e4b1 | 93 | if ((p[0] == s[0]) || (p[0] == '?')) |
9f1da361 KS |
94 | return strcmp_pattern(p+1, s+1); |
95 | break; | |
96 | } | |
97 | return 1; | |
c124eafa KS |
98 | } |
99 | ||
2232cac8 GKH |
100 | #define copy_var(a, b, var) \ |
101 | if (b->var) \ | |
469c7cff | 102 | a->var = b->var; |
2232cac8 GKH |
103 | |
104 | #define copy_string(a, b, var) \ | |
105 | if (strlen(b->var)) \ | |
469c7cff | 106 | strcpy(a->var, b->var); |
2232cac8 | 107 | |
61219c75 GKH |
108 | int add_perm_dev(struct perm_device *new_dev) |
109 | { | |
843d1a84 | 110 | struct perm_device *dev; |
61219c75 GKH |
111 | struct perm_device *tmp_dev; |
112 | ||
113 | /* update the values if we already have the device */ | |
843d1a84 | 114 | list_for_each_entry(dev, &perm_device_list, node) { |
61219c75 GKH |
115 | if (strcmp_pattern(new_dev->name, dev->name)) |
116 | continue; | |
117 | copy_var(dev, new_dev, mode); | |
118 | copy_string(dev, new_dev, owner); | |
119 | copy_string(dev, new_dev, group); | |
120 | return 0; | |
121 | } | |
122 | ||
123 | /* not found, add new structure to the perm list */ | |
124 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
125 | if (!tmp_dev) | |
126 | return -ENOMEM; | |
127 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
128 | list_add_tail(&tmp_dev->node, &perm_device_list); | |
129 | //dump_perm_dev(tmp_dev); | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static struct perm_device *find_perm(char *name) | |
134 | { | |
843d1a84 | 135 | struct perm_device *perm; |
61219c75 | 136 | |
843d1a84 | 137 | list_for_each_entry(perm, &perm_device_list, node) { |
61219c75 GKH |
138 | if (strcmp_pattern(perm->name, name)) |
139 | continue; | |
140 | return perm; | |
141 | } | |
142 | return NULL; | |
143 | } | |
144 | ||
765cbd97 | 145 | static mode_t get_default_mode(void) |
185a35a4 | 146 | { |
89571022 GKH |
147 | mode_t mode = 0600; /* default to owner rw only */ |
148 | ||
267f534d | 149 | if (strlen(default_mode_str) != 0) |
89571022 | 150 | mode = strtol(default_mode_str, NULL, 8); |
267f534d | 151 | |
89571022 | 152 | return mode; |
185a35a4 GKH |
153 | } |
154 | ||
267f534d | 155 | static char *get_default_owner(void) |
74c73ef9 | 156 | { |
267f534d | 157 | if (strlen(default_owner_str) == 0) |
74c73ef9 | 158 | strncpy(default_owner_str, "root", OWNER_SIZE); |
267f534d | 159 | |
74c73ef9 | 160 | return default_owner_str; |
161 | } | |
162 | ||
267f534d | 163 | static char *get_default_group(void) |
74c73ef9 | 164 | { |
267f534d | 165 | if (strlen(default_group_str) == 0) |
74c73ef9 | 166 | strncpy(default_group_str, "root", GROUP_SIZE); |
267f534d | 167 | |
74c73ef9 | 168 | return default_group_str; |
169 | } | |
170 | ||
f3b04a2e GKH |
171 | static void apply_format(struct udevice *udev, unsigned char *string) |
172 | { | |
b1c5e333 | 173 | char temp[NAME_SIZE]; |
8ffb636f | 174 | char temp1[NAME_SIZE]; |
b1c5e333 | 175 | char *tail; |
f3b04a2e | 176 | char *pos; |
b1c5e333 KS |
177 | char *pos2; |
178 | char *pos3; | |
179 | int num; | |
f3b04a2e | 180 | |
8ffb636f | 181 | pos = string; |
f3b04a2e | 182 | while (1) { |
b1c5e333 | 183 | num = 0; |
8ffb636f | 184 | pos = strchr(pos, '%'); |
f3b04a2e GKH |
185 | |
186 | if (pos) { | |
8ffb636f | 187 | pos[0] = '\0'; |
b1c5e333 KS |
188 | tail = pos+1; |
189 | if (isdigit(tail[0])) { | |
190 | num = (int) strtoul(&pos[1], &tail, 10); | |
191 | if (tail == NULL) { | |
192 | dbg("format parsing error '%s'", pos+1); | |
193 | break; | |
194 | } | |
195 | } | |
8ffb636f | 196 | strfieldcpy(temp, tail+1); |
b1c5e333 KS |
197 | |
198 | switch (tail[0]) { | |
f3b04a2e GKH |
199 | case 'b': |
200 | if (strlen(udev->bus_id) == 0) | |
201 | break; | |
6968d494 | 202 | strcat(pos, udev->bus_id); |
f3b04a2e GKH |
203 | dbg("substitute bus_id '%s'", udev->bus_id); |
204 | break; | |
3e540368 KS |
205 | case 'k': |
206 | if (strlen(udev->kernel_name) == 0) | |
207 | break; | |
208 | strcat(pos, udev->kernel_name); | |
209 | dbg("substitute kernel name '%s'", udev->kernel_name); | |
210 | break; | |
f3b04a2e GKH |
211 | case 'n': |
212 | if (strlen(udev->kernel_number) == 0) | |
213 | break; | |
214 | strcat(pos, udev->kernel_number); | |
215 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
216 | break; | |
217 | case 'm': | |
218 | sprintf(pos, "%u", udev->minor); | |
219 | dbg("substitute minor number '%u'", udev->minor); | |
220 | break; | |
221 | case 'M': | |
222 | sprintf(pos, "%u", udev->major); | |
223 | dbg("substitute major number '%u'", udev->major); | |
224 | break; | |
225 | case 'c': | |
ac28b86d | 226 | if (strlen(udev->program_result) == 0) |
f3b04a2e | 227 | break; |
b1c5e333 KS |
228 | if (num) { |
229 | /* get part of return string */ | |
8ffb636f KS |
230 | strncpy(temp1, udev->program_result, sizeof(temp1)); |
231 | pos2 = temp1; | |
b1c5e333 KS |
232 | while (num) { |
233 | num--; | |
234 | pos3 = strsep(&pos2, " "); | |
235 | if (pos3 == NULL) { | |
ac28b86d | 236 | dbg("requested part of result string not found"); |
b1c5e333 KS |
237 | break; |
238 | } | |
239 | } | |
647c8fc4 KS |
240 | if (pos3) { |
241 | strcat(pos, pos3); | |
ac28b86d | 242 | dbg("substitute part of result string '%s'", pos3); |
647c8fc4 | 243 | } |
b1c5e333 | 244 | } else { |
ac28b86d KS |
245 | strcat(pos, udev->program_result); |
246 | dbg("substitute result string '%s'", udev->program_result); | |
b1c5e333 | 247 | } |
f3b04a2e | 248 | break; |
8ffb636f KS |
249 | case '%': |
250 | strcat(pos, "%"); | |
251 | pos++; | |
252 | break; | |
f3b04a2e GKH |
253 | default: |
254 | dbg("unknown substitution type '%%%c'", pos[1]); | |
255 | break; | |
256 | } | |
8ffb636f | 257 | strcat(string, temp); |
f3b04a2e GKH |
258 | } else |
259 | break; | |
260 | } | |
261 | } | |
262 | ||
dac056aa GKH |
263 | static struct bus_file { |
264 | char *bus; | |
265 | char *file; | |
266 | } bus_files[] = { | |
267 | { .bus = "scsi", .file = "vendor" }, | |
268 | { .bus = "usb", .file = "idVendor" }, | |
b7824727 | 269 | { .bus = "usb-serial", .file = "detach_state" }, |
2441c207 | 270 | { .bus = "ide", .file = "detach_state" }, |
48076332 | 271 | { .bus = "pci", .file = "vendor" }, |
dac056aa GKH |
272 | {} |
273 | }; | |
274 | ||
275 | #define SECONDS_TO_WAIT_FOR_FILE 10 | |
276 | static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device) | |
277 | { | |
278 | /* sleep until we see the file for this specific bus type show up this | |
279 | * is needed because we can easily out-run the kernel in looking for | |
280 | * these files before the paticular subsystem has created them in the | |
281 | * sysfs tree properly. | |
282 | * | |
283 | * And people thought that the /sbin/hotplug event system was going to | |
284 | * be slow, poo on you for arguing that before even testing it... | |
285 | */ | |
286 | struct bus_file *b = &bus_files[0]; | |
287 | struct sysfs_attribute *tmpattr; | |
288 | int loop; | |
289 | ||
290 | while (1) { | |
291 | if (b->bus == NULL) | |
292 | break; | |
293 | if (strcmp(sysfs_device->bus, b->bus) == 0) { | |
294 | tmpattr = NULL; | |
295 | loop = SECONDS_TO_WAIT_FOR_FILE; | |
296 | while (loop--) { | |
297 | dbg("looking for file '%s' on bus '%s'", b->file, b->bus); | |
298 | tmpattr = sysfs_get_device_attr(sysfs_device, b->file); | |
299 | if (tmpattr) { | |
300 | /* found it! */ | |
301 | goto exit; | |
302 | } | |
303 | /* sleep to give the kernel a chance to create the file */ | |
304 | sleep(1); | |
305 | } | |
54988802 | 306 | dbg("timed out waiting for '%s' file, continuing on anyway...", b->file); |
dac056aa GKH |
307 | goto exit; |
308 | } | |
309 | b++; | |
310 | } | |
54988802 | 311 | dbg("did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus); |
dac056aa GKH |
312 | exit: |
313 | return; /* here to prevent compiler warning... */ | |
314 | } | |
19dc5d4c | 315 | |
ac28b86d | 316 | static int execute_program(char *path, char *value, int len) |
c27e6911 PM |
317 | { |
318 | int retval; | |
319 | int res; | |
320 | int status; | |
321 | int fds[2]; | |
322 | pid_t pid; | |
323 | int value_set = 0; | |
324 | char buffer[256]; | |
20524642 | 325 | char *pos; |
7e5f7397 | 326 | char *args[PROGRAM_MAXARG]; |
bc434511 | 327 | int i; |
c27e6911 | 328 | |
ac28b86d | 329 | dbg("executing '%s'", path); |
c27e6911 PM |
330 | retval = pipe(fds); |
331 | if (retval != 0) { | |
332 | dbg("pipe failed"); | |
333 | return -1; | |
334 | } | |
335 | pid = fork(); | |
2a25816f KS |
336 | switch(pid) { |
337 | case 0: | |
8f43a65e | 338 | /* child */ |
c27e6911 PM |
339 | close(STDOUT_FILENO); |
340 | dup(fds[1]); /* dup write side of pipe to STDOUT */ | |
41397661 | 341 | if (strchr(path, ' ')) { |
ac28b86d | 342 | /* exec with arguments */ |
41397661 | 343 | pos = path; |
7e5f7397 | 344 | for (i=0; i < PROGRAM_MAXARG-1; i++) { |
20524642 | 345 | args[i] = strsep(&pos, " "); |
bc434511 KS |
346 | if (args[i] == NULL) |
347 | break; | |
348 | } | |
349 | if (args[i]) { | |
53dc383e | 350 | dbg("too many args - %d", i); |
bc434511 KS |
351 | args[i] = NULL; |
352 | } | |
2a25816f | 353 | retval = execv(args[0], args); |
bc434511 | 354 | } else { |
2a25816f | 355 | retval = execv(path, main_argv); |
bc434511 | 356 | } |
2a25816f KS |
357 | dbg("child execve failed"); |
358 | exit(1); | |
359 | case -1: | |
360 | dbg("fork failed"); | |
361 | return -1; | |
362 | default: | |
8f43a65e | 363 | /* parent reads from fds[0] */ |
c27e6911 PM |
364 | close(fds[1]); |
365 | retval = 0; | |
366 | while (1) { | |
367 | res = read(fds[0], buffer, sizeof(buffer) - 1); | |
368 | if (res <= 0) | |
369 | break; | |
370 | buffer[res] = '\0'; | |
371 | if (res > len) { | |
ac28b86d | 372 | dbg("result len %d too short", len); |
c27e6911 PM |
373 | retval = -1; |
374 | } | |
375 | if (value_set) { | |
ac28b86d | 376 | dbg("result value already set"); |
c27e6911 PM |
377 | retval = -1; |
378 | } else { | |
379 | value_set = 1; | |
380 | strncpy(value, buffer, len); | |
20524642 KS |
381 | pos = value + strlen(value)-1; |
382 | if (pos[0] == '\n') | |
383 | pos[0] = '\0'; | |
ac28b86d | 384 | dbg("result is '%s'", value); |
c27e6911 PM |
385 | } |
386 | } | |
387 | close(fds[0]); | |
388 | res = wait(&status); | |
389 | if (res < 0) { | |
390 | dbg("wait failed result %d", res); | |
391 | retval = -1; | |
392 | } | |
393 | ||
394 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { | |
ac28b86d | 395 | dbg("exec program status 0x%x", status); |
c27e6911 PM |
396 | retval = -1; |
397 | } | |
398 | } | |
399 | return retval; | |
400 | } | |
401 | ||
ac28b86d | 402 | static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair) |
ca1cc0fe GKH |
403 | { |
404 | struct sysfs_attribute *tmpattr = NULL; | |
a8b01705 GKH |
405 | char *c; |
406 | ||
407 | if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0')) | |
408 | return -ENODEV; | |
409 | ||
410 | dbg("look for device attribute '%s'", pair->file); | |
411 | /* try to find the attribute in the class device directory */ | |
412 | tmpattr = sysfs_get_classdev_attr(class_dev, pair->file); | |
413 | if (tmpattr) | |
414 | goto label_found; | |
415 | ||
416 | /* look in the class device directory if present */ | |
417 | if (sysfs_device) { | |
418 | tmpattr = sysfs_get_device_attr(sysfs_device, pair->file); | |
419 | if (tmpattr) | |
420 | goto label_found; | |
421 | } | |
a8b01705 GKH |
422 | return -ENODEV; |
423 | ||
424 | label_found: | |
425 | c = tmpattr->value + strlen(tmpattr->value)-1; | |
426 | if (*c == '\n') | |
427 | *c = 0x00; | |
428 | dbg("compare attribute '%s' value '%s' with '%s'", | |
429 | pair->file, tmpattr->value, pair->value); | |
430 | if (strcmp_pattern(pair->value, tmpattr->value) != 0) | |
431 | return -ENODEV; | |
432 | ||
433 | dbg("found matching attribute '%s' with value '%s'", | |
434 | pair->file, pair->value); | |
435 | return 0; | |
436 | } | |
437 | ||
ac28b86d | 438 | static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
a8b01705 GKH |
439 | { |
440 | struct sysfs_pair *pair; | |
a8b01705 | 441 | int i; |
137af0cc | 442 | |
ac28b86d KS |
443 | for (i = 0; i < MAX_SYSFS_PAIRS; ++i) { |
444 | pair = &dev->sysfs_pair[i]; | |
445 | if ((pair->file[0] == '\0') || (pair->value[0] == '\0')) | |
446 | break; | |
447 | if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) { | |
448 | dbg("sysfs attribute doesn't match"); | |
449 | return -ENODEV; | |
ca1cc0fe | 450 | } |
ca1cc0fe | 451 | } |
ac28b86d KS |
452 | |
453 | return 0; | |
ca1cc0fe GKH |
454 | } |
455 | ||
ac28b86d | 456 | static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
ca1cc0fe | 457 | { |
ca1cc0fe GKH |
458 | char path[SYSFS_PATH_MAX]; |
459 | int found; | |
460 | char *temp = NULL; | |
461 | ||
1f7148c7 | 462 | /* we have to have a sysfs device for ID to work */ |
ca1cc0fe GKH |
463 | if (!sysfs_device) |
464 | return -ENODEV; | |
465 | ||
ac28b86d KS |
466 | found = 0; |
467 | strfieldcpy(path, sysfs_device->path); | |
468 | temp = strrchr(path, '/'); | |
469 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); | |
470 | if (strstr(temp, dev->id) != NULL) { | |
471 | found = 1; | |
472 | } else { | |
473 | *temp = 0x00; | |
ca1cc0fe | 474 | temp = strrchr(path, '/'); |
19feb351 | 475 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ac28b86d | 476 | if (strstr(temp, dev->id) != NULL) |
ca1cc0fe | 477 | found = 1; |
ca1cc0fe | 478 | } |
ac28b86d KS |
479 | if (!found) { |
480 | dbg("id doesn't match"); | |
481 | return -ENODEV; | |
482 | } | |
483 | ||
484 | return 0; | |
ca1cc0fe GKH |
485 | } |
486 | ||
ac28b86d | 487 | static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device) |
8c51bbfe | 488 | { |
8c51bbfe GKH |
489 | char path[SYSFS_PATH_MAX]; |
490 | int found; | |
491 | char *temp = NULL; | |
492 | ||
1f7148c7 | 493 | /* we have to have a sysfs device for PLACE to work */ |
8c51bbfe GKH |
494 | if (!sysfs_device) |
495 | return -ENODEV; | |
496 | ||
ac28b86d KS |
497 | found = 0; |
498 | strfieldcpy(path, sysfs_device->path); | |
499 | temp = strrchr(path, '/'); | |
500 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); | |
501 | if (strstr(temp, dev->place) != NULL) { | |
502 | found = 1; | |
503 | } else { | |
504 | *temp = 0x00; | |
8c51bbfe | 505 | temp = strrchr(path, '/'); |
19feb351 | 506 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
ac28b86d | 507 | if (strstr(temp, dev->place) != NULL) |
8c51bbfe | 508 | found = 1; |
8c51bbfe | 509 | } |
ac28b86d KS |
510 | if (!found) { |
511 | dbg("place doesn't match"); | |
512 | return -ENODEV; | |
120d45d0 | 513 | } |
120d45d0 | 514 | |
ac28b86d | 515 | return 0; |
09e52d51 KS |
516 | } |
517 | ||
dac056aa | 518 | static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev) |
185a35a4 | 519 | { |
dac056aa GKH |
520 | struct sysfs_device *sysfs_device; |
521 | struct sysfs_class_device *class_dev_parent; | |
616a7078 | 522 | struct timespec tspec; |
dac056aa | 523 | int loop; |
07562d6e GKH |
524 | |
525 | /* Figure out where the device symlink is at. For char devices this will | |
526 | * always be in the class_dev->path. But for block devices, it's different. | |
527 | * The main block device will have the device symlink in it's path, but | |
528 | * all partitions have the symlink in its parent directory. | |
529 | * But we need to watch out for block devices that do not have parents, yet | |
530 | * look like a partition (fd0, loop0, etc.) They all do not have a device | |
531 | * symlink yet. We do sit and spin on waiting for them right now, we should | |
532 | * possibly have a whitelist for these devices here... | |
dac056aa | 533 | */ |
616a7078 AM |
534 | class_dev_parent = sysfs_get_classdev_parent(class_dev); |
535 | if (class_dev_parent) | |
536 | dbg("Really a partition"); | |
1edbb8d3 | 537 | |
616a7078 AM |
538 | tspec.tv_sec = 0; |
539 | tspec.tv_nsec = 10000000; /* sleep 10 millisec */ | |
540 | loop = 10; | |
dac056aa | 541 | while (loop--) { |
616a7078 AM |
542 | nanosleep(&tspec, NULL); |
543 | if (class_dev_parent) | |
544 | sysfs_device = sysfs_get_classdev_device(class_dev_parent); | |
545 | else | |
546 | sysfs_device = sysfs_get_classdev_device(class_dev); | |
07562d6e | 547 | |
dac056aa | 548 | if (sysfs_device != NULL) |
616a7078 | 549 | goto device_found; |
7bd22a78 | 550 | } |
54988802 | 551 | dbg("timed out waiting for device symlink, continuing on anyway..."); |
616a7078 AM |
552 | |
553 | device_found: | |
554 | /* We have another issue with just the wait above - the sysfs part of | |
555 | * the kernel may not be quick enough to have created the link to the | |
556 | * device under the "bus" subsystem. Due to this, the sysfs_device->bus | |
557 | * will not contain the actual bus name :( | |
558 | * | |
559 | * Libsysfs now provides a new API sysfs_get_device_bus(), so use it | |
560 | * if needed | |
561 | */ | |
562 | if (sysfs_device) { | |
616a7078 AM |
563 | if (sysfs_device->bus[0] != '\0') |
564 | goto bus_found; | |
267f534d | 565 | |
616a7078 AM |
566 | loop = 10; |
567 | tspec.tv_nsec = 10000000; | |
568 | while (loop--) { | |
569 | nanosleep(&tspec, NULL); | |
570 | sysfs_get_device_bus(sysfs_device); | |
571 | ||
572 | if (sysfs_device->bus[0] != '\0') | |
573 | goto bus_found; | |
574 | } | |
54988802 | 575 | dbg("timed out waiting to find the device bus, continuing on anyway"); |
616a7078 AM |
576 | goto exit; |
577 | bus_found: | |
54988802 | 578 | dbg("device %s is registered with bus '%s'", |
616a7078 AM |
579 | sysfs_device->name, sysfs_device->bus); |
580 | } | |
dac056aa GKH |
581 | exit: |
582 | return sysfs_device; | |
583 | } | |
584 | ||
724257d9 | 585 | static int match_rule(struct config_device *dev, struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
dac056aa | 586 | { |
724257d9 | 587 | while (1) { |
ac28b86d KS |
588 | /* check for matching bus value */ |
589 | if (dev->bus[0] != '\0') { | |
590 | if (sysfs_device == NULL) { | |
591 | dbg("device has no bus"); | |
724257d9 | 592 | goto no_good; |
ac28b86d KS |
593 | } |
594 | dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); | |
595 | if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) { | |
596 | dbg(FIELD_BUS " is not matching"); | |
724257d9 | 597 | goto no_good; |
ac28b86d KS |
598 | } else { |
599 | dbg(FIELD_BUS " matches"); | |
600 | } | |
601 | } | |
120d45d0 | 602 | |
ac28b86d KS |
603 | /* check for matching kernel name*/ |
604 | if (dev->kernel[0] != '\0') { | |
605 | dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'", dev->kernel, class_dev->name); | |
606 | if (strcmp_pattern(dev->kernel, class_dev->name) != 0) { | |
607 | dbg(FIELD_KERNEL " is not matching"); | |
724257d9 | 608 | goto no_good; |
ac28b86d KS |
609 | } else { |
610 | dbg(FIELD_KERNEL " matches"); | |
611 | } | |
612 | } | |
ca1cc0fe | 613 | |
ac28b86d KS |
614 | /* check for matching bus id */ |
615 | if (dev->id[0] != '\0') { | |
616 | dbg("check " FIELD_ID); | |
617 | if (match_id(dev, class_dev, sysfs_device) != 0) { | |
618 | dbg(FIELD_ID " is not matching"); | |
724257d9 | 619 | goto no_good; |
ac28b86d KS |
620 | } else { |
621 | dbg(FIELD_ID " matches"); | |
622 | } | |
623 | } | |
ca1cc0fe | 624 | |
ac28b86d KS |
625 | /* check for matching place of device */ |
626 | if (dev->place[0] != '\0') { | |
627 | dbg("check " FIELD_PLACE); | |
628 | if (match_place(dev, class_dev, sysfs_device) != 0) { | |
629 | dbg(FIELD_PLACE " is not matching"); | |
724257d9 | 630 | goto no_good; |
ac28b86d KS |
631 | } else { |
632 | dbg(FIELD_PLACE " matches"); | |
633 | } | |
634 | } | |
8c51bbfe | 635 | |
ac28b86d KS |
636 | /* check for matching sysfs pairs */ |
637 | if (dev->sysfs_pair[0].file[0] != '\0') { | |
638 | dbg("check " FIELD_SYSFS " pairs"); | |
639 | if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) { | |
640 | dbg(FIELD_SYSFS " is not matching"); | |
724257d9 | 641 | goto no_good; |
ac28b86d KS |
642 | } else { |
643 | dbg(FIELD_SYSFS " matches"); | |
644 | } | |
645 | } | |
646 | ||
647 | /* execute external program */ | |
648 | if (dev->program[0] != '\0') { | |
649 | dbg("check " FIELD_PROGRAM); | |
650 | apply_format(udev, dev->program); | |
651 | if (execute_program(dev->program, udev->program_result, NAME_SIZE) != 0) { | |
652 | dbg(FIELD_PROGRAM " returned nozero"); | |
724257d9 | 653 | goto no_good; |
ac28b86d KS |
654 | } else { |
655 | dbg(FIELD_PROGRAM " returned successful"); | |
656 | } | |
657 | } | |
658 | ||
659 | /* check for matching result of external program */ | |
660 | if (dev->result[0] != '\0') { | |
661 | dbg("check for " FIELD_RESULT | |
662 | " dev->result='%s', udev->program_result='%s'", | |
663 | dev->result, udev->program_result); | |
664 | if (strcmp_pattern(dev->result, udev->program_result) != 0) { | |
665 | dbg(FIELD_RESULT " is not matching"); | |
724257d9 | 666 | goto no_good; |
ac28b86d KS |
667 | } else { |
668 | dbg(FIELD_RESULT " matches"); | |
669 | } | |
670 | } | |
671 | ||
672 | /* check if we are instructed to ignore this device */ | |
673 | if (dev->name[0] == '\0') { | |
674 | dbg("instructed to ignore this device"); | |
675 | return -1; | |
676 | } | |
677 | ||
724257d9 GKH |
678 | /* Yeah, we matched! */ |
679 | return 0; | |
680 | ||
681 | no_good: | |
682 | sysfs_device = sysfs_get_device_parent(sysfs_device); | |
683 | if (sysfs_device == NULL) | |
684 | return -ENODEV; | |
685 | dbg("sysfs_device->path='%s'", sysfs_device->path); | |
686 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
687 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
688 | } | |
689 | ||
690 | } | |
691 | ||
692 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev) | |
693 | { | |
694 | struct sysfs_device *sysfs_device = NULL; | |
695 | struct config_device *dev; | |
696 | struct perm_device *perm; | |
697 | char *pos; | |
698 | ||
699 | udev->mode = 0; | |
700 | ||
701 | /* find the sysfs_device associated with this class device */ | |
702 | sysfs_device = get_sysfs_device(class_dev); | |
703 | if (sysfs_device) { | |
704 | dbg("sysfs_device->path='%s'", sysfs_device->path); | |
705 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
706 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
707 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); | |
708 | wait_for_device_to_initialize(sysfs_device); | |
709 | } else { | |
710 | dbg("class_dev->name = '%s'", class_dev->name); | |
711 | } | |
712 | ||
713 | strfieldcpy(udev->kernel_name, class_dev->name); | |
714 | ||
715 | /* get kernel number */ | |
716 | pos = class_dev->name + strlen(class_dev->name); | |
717 | while (isdigit(*(pos-1))) | |
718 | pos--; | |
719 | strfieldcpy(udev->kernel_number, pos); | |
720 | dbg("kernel_number='%s'", udev->kernel_number); | |
721 | ||
722 | /* look for a matching rule to apply */ | |
723 | list_for_each_entry(dev, &config_device_list, node) { | |
724 | dbg("process rule"); | |
725 | ||
726 | if (match_rule(dev, class_dev, udev, sysfs_device) == 0) { | |
727 | /* Yup, this rule belongs to us! */ | |
728 | info("configured rule in '%s' at line %i applied, '%s' becomes '%s'", | |
729 | udev_rules_filename, dev->config_line, udev->kernel_name, dev->name); | |
730 | strfieldcpy(udev->name, dev->name); | |
731 | strfieldcpy(udev->symlink, dev->symlink); | |
732 | goto found; | |
733 | } | |
ac28b86d | 734 | } |
120d45d0 | 735 | |
ac28b86d KS |
736 | /* no rule was found so we use the kernel name */ |
737 | strfieldcpy(udev->name, class_dev->name); | |
09e52d51 | 738 | goto done; |
c2405f50 | 739 | |
09e52d51 | 740 | found: |
3d150dfb | 741 | /* substitute placeholder */ |
f3b04a2e | 742 | apply_format(udev, udev->name); |
3d150dfb | 743 | apply_format(udev, udev->symlink); |
98b88dbf | 744 | |
09e52d51 | 745 | done: |
61219c75 GKH |
746 | perm = find_perm(udev->name); |
747 | if (perm) { | |
748 | udev->mode = perm->mode; | |
749 | strfieldcpy(udev->owner, perm->owner); | |
750 | strfieldcpy(udev->group, perm->group); | |
751 | } else { | |
752 | /* no matching perms found :( */ | |
765cbd97 | 753 | udev->mode = get_default_mode(); |
74c73ef9 | 754 | strncpy(udev->owner, get_default_owner(), OWNER_SIZE); |
755 | strncpy(udev->group, get_default_group(), GROUP_SIZE); | |
615e05f8 | 756 | } |
61219c75 GKH |
757 | dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", |
758 | udev->name, udev->owner, udev->group, udev->mode); | |
7bd22a78 | 759 | |
120d45d0 | 760 | return 0; |
185a35a4 GKH |
761 | } |
762 | ||
2232cac8 GKH |
763 | int namedev_init(void) |
764 | { | |
765 | int retval; | |
28d6536a | 766 | |
e8baccca | 767 | retval = namedev_init_rules(); |
2232cac8 GKH |
768 | if (retval) |
769 | return retval; | |
770 | ||
771 | retval = namedev_init_permissions(); | |
772 | if (retval) | |
773 | return retval; | |
774 | ||
19feb351 | 775 | dump_config_dev_list(); |
61219c75 | 776 | dump_perm_dev_list(); |
2232cac8 GKH |
777 | return retval; |
778 | } |