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