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