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