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