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