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