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