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