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