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