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