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