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