chiark / gitweb /
[PATCH] udev - Makefile error
[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
35 #include "list.h"
36 #include "udev.h"
37 #include "udev_version.h"
38 #include "namedev.h"
39 #include "libsysfs/libsysfs.h"
40 #include "klibc_fixups.h"
41
42 LIST_HEAD(config_device_list);
43 LIST_HEAD(perm_device_list);
44
45 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
46 static int strcmp_pattern(const char *p, const char *s)
47 {
48         if (*s == '\0') {
49                 while (*p == '*')
50                         p++;
51                 return (*p != '\0');
52         }
53         switch (*p) {
54         case '[':
55                 {
56                         int not = 0;
57                         p++;
58                         if (*p == '!') {
59                                 not = 1;
60                                 p++;
61                         }
62                         while (*p && (*p != ']')) {
63                                 int match = 0;
64                                 if (p[1] == '-') {
65                                         if ((*s >= *p) && (*s <= p[2]))
66                                                 match = 1;
67                                         p += 3;
68                                 } else {
69                                         match = (*p == *s);
70                                         p++;
71                                 }
72                                 if (match ^ not) {
73                                         while (*p && (*p != ']'))
74                                                 p++;
75                                         return strcmp_pattern(p+1, s+1);
76                                 }
77                         }
78                 }
79                 break;
80         case '*':
81                 if (strcmp_pattern(p, s+1))
82                         return strcmp_pattern(p+1, s);
83                 return 0;
84         case '\0':
85                 if (*s == '\0') {
86                         return 0;
87                 }
88                 break;
89         default:
90                 if ((*p == *s) || (*p == '?'))
91                         return strcmp_pattern(p+1, s+1);
92                 break;
93         }
94         return 1;
95 }
96
97 #define copy_var(a, b, var)             \
98         if (b->var)                     \
99                 a->var = b->var;
100
101 #define copy_string(a, b, var)          \
102         if (strlen(b->var))             \
103                 strcpy(a->var, b->var);
104
105 int add_perm_dev(struct perm_device *new_dev)
106 {
107         struct perm_device *dev;
108         struct perm_device *tmp_dev;
109
110         /* update the values if we already have the device */
111         list_for_each_entry(dev, &perm_device_list, node) {
112                 if (strcmp_pattern(new_dev->name, dev->name))
113                         continue;
114                 copy_var(dev, new_dev, mode);
115                 copy_string(dev, new_dev, owner);
116                 copy_string(dev, new_dev, group);
117                 return 0;
118         }
119
120         /* not found, add new structure to the perm list */
121         tmp_dev = malloc(sizeof(*tmp_dev));
122         if (!tmp_dev)
123                 return -ENOMEM;
124         memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
125         list_add_tail(&tmp_dev->node, &perm_device_list);
126         //dump_perm_dev(tmp_dev);
127         return 0;
128 }
129
130 static struct perm_device *find_perm(char *name)
131 {
132         struct perm_device *perm;
133
134         list_for_each_entry(perm, &perm_device_list, node) {
135                 if (strcmp_pattern(perm->name, name))
136                         continue;
137                 return perm;
138         }
139         return NULL;
140 }
141
142 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
143 {
144         mode_t mode = 0600;     /* default to owner rw only */
145
146         if (strlen(default_mode_str) != 0) {
147                 mode = strtol(default_mode_str, NULL, 8);
148         }
149         return mode;
150 }
151
152 static void apply_format(struct udevice *udev, unsigned char *string)
153 {
154         char name[NAME_SIZE];
155         char temp[NAME_SIZE];
156         char *tail;
157         char *pos;
158         char *pos2;
159         char *pos3;
160         int num;
161
162         while (1) {
163                 num = 0;
164                 pos = strchr(string, '%');
165
166                 if (pos) {
167                         *pos = '\0';
168                         tail = pos+1;
169                         if (isdigit(tail[0])) {
170                                 num = (int) strtoul(&pos[1], &tail, 10);
171                                 if (tail == NULL) {
172                                         dbg("format parsing error '%s'", pos+1);
173                                         break;
174                                 }
175                         }
176                         strfieldcpy(name, tail+1);
177
178                         switch (tail[0]) {
179                         case 'b':
180                                 if (strlen(udev->bus_id) == 0)
181                                         break;
182                                 strcat(pos, udev->bus_id);
183                                 dbg("substitute bus_id '%s'", udev->bus_id);
184                                 break;
185                         case 'k':
186                                 if (strlen(udev->kernel_name) == 0)
187                                         break;
188                                 strcat(pos, udev->kernel_name);
189                                 dbg("substitute kernel name '%s'", udev->kernel_name);
190                                 break;
191                         case 'n':
192                                 if (strlen(udev->kernel_number) == 0)
193                                         break;
194                                 strcat(pos, udev->kernel_number);
195                                 dbg("substitute kernel number '%s'", udev->kernel_number);
196                                 break;
197                         case 'D':
198                                 if (strlen(udev->kernel_number) == 0) {
199                                         strcat(pos, "disc");
200                                         dbg("substitute devfs disc");
201                                         break;
202                                 }
203                                 strcat(pos, "part");
204                                 strcat(pos, udev->kernel_number);
205                                 dbg("substitute devfs part '%s'", udev->kernel_number);
206                                 break;
207                         case 'm':
208                                 sprintf(pos, "%u", udev->minor);
209                                 dbg("substitute minor number '%u'", udev->minor);
210                                 break;
211                         case 'M':
212                                 sprintf(pos, "%u", udev->major);
213                                 dbg("substitute major number '%u'", udev->major);
214                                 break;
215                         case 'c':
216                                 if (strlen(udev->callout_value) == 0)
217                                         break;
218                                 if (num) {
219                                         /* get part of return string */
220                                         strncpy(temp, udev->callout_value, sizeof(temp));
221                                         pos2 = temp;
222                                         while (num) {
223                                                 num--;
224                                                 pos3 = strsep(&pos2, " ");
225                                                 if (pos3 == NULL) {
226                                                         dbg("requested part of callout string not found");
227                                                         break;
228                                                 }
229                                         }
230                                         if (pos3) {
231                                                 strcat(pos, pos3);
232                                                 dbg("substitute partial callout output '%s'", pos3);
233                                         }
234                                 } else {
235                                         strcat(pos, udev->callout_value);
236                                         dbg("substitute callout output '%s'", udev->callout_value);
237                                 }
238                                 break;
239                         default:
240                                 dbg("unknown substitution type '%%%c'", pos[1]);
241                                 break;
242                         }
243                         strcat(string, name);
244                 } else
245                         break;
246         }
247 }
248
249 static struct bus_file {
250         char *bus;
251         char *file;
252 } bus_files[] = {
253         { .bus = "scsi",        .file = "vendor" },
254         { .bus = "usb",         .file = "idVendor" },
255         { .bus = "ide",         .file = "detach_state" },
256         { .bus = "pci",         .file = "vendor" },
257         {}
258 };
259
260 #define SECONDS_TO_WAIT_FOR_FILE        10
261 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
262 {
263         /* sleep until we see the file for this specific bus type show up this
264          * is needed because we can easily out-run the kernel in looking for
265          * these files before the paticular subsystem has created them in the
266          * sysfs tree properly.
267          *
268          * And people thought that the /sbin/hotplug event system was going to
269          * be slow, poo on you for arguing that before even testing it...
270          */
271         struct bus_file *b = &bus_files[0];
272         struct sysfs_attribute *tmpattr;
273         int loop;
274
275         while (1) {
276                 if (b->bus == NULL)
277                         break;
278                 if (strcmp(sysfs_device->bus, b->bus) == 0) {
279                         tmpattr = NULL;
280                         loop = SECONDS_TO_WAIT_FOR_FILE;
281                         while (loop--) {
282                                 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
283                                 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
284                                 if (tmpattr) {
285                                         /* found it! */
286                                         goto exit;
287                                 }
288                                 /* sleep to give the kernel a chance to create the file */
289                                 sleep(1);
290                         }
291                         dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file);
292                         goto exit;
293                 }
294                 b++;
295         }
296         dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
297 exit:
298         return; /* here to prevent compiler warning... */
299 }
300
301 static int do_ignore(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
302 {
303         struct config_device *dev;
304         struct list_head *tmp;
305
306         list_for_each(tmp, &config_device_list) {
307                 dev = list_entry(tmp, struct config_device, node);
308                 if (dev->type != IGNORE)
309                         continue;
310
311                 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
312                 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
313                         continue;
314
315                 dbg("found name, '%s' will be ignored", dev->kernel_name);
316
317                 return 0;
318         }
319         return -ENODEV;
320 }
321
322 static int exec_program(char *path, char *value, int len)
323 {
324         int retval;
325         int res;
326         int status;
327         int fds[2];
328         pid_t pid;
329         int value_set = 0;
330         char buffer[256];
331         char *pos;
332         char *args[CALLOUT_MAXARG];
333         int i;
334
335         dbg("callout to '%s'", path);
336         retval = pipe(fds);
337         if (retval != 0) {
338                 dbg("pipe failed");
339                 return -1;
340         }
341         pid = fork();
342         if (pid == -1) {
343                 dbg("fork failed");
344                 return -1;
345         }
346
347         if (pid == 0) {
348                 /* child */
349                 close(STDOUT_FILENO);
350                 dup(fds[1]);    /* dup write side of pipe to STDOUT */
351                 if (strchr(path, ' ')) {
352                         /* callout with arguments */
353                         pos = path;
354                         for (i=0; i < CALLOUT_MAXARG-1; i++) {
355                                 args[i] = strsep(&pos, " ");
356                                 if (args[i] == NULL)
357                                         break;
358                         }
359                         if (args[i]) {
360                                 dbg("too many args - %d", i);
361                                 args[i] = NULL;
362                         }
363                         retval = execve(args[0], args, main_envp);
364                 } else {
365                         retval = execve(path, main_argv, main_envp);
366                 }
367                 if (retval != 0) {
368                         dbg("child execve failed");
369                         exit(1);
370                 }
371                 return -1; /* avoid compiler warning */
372         } else {
373                 /* parent reads from fds[0] */
374                 close(fds[1]);
375                 retval = 0;
376                 while (1) {
377                         res = read(fds[0], buffer, sizeof(buffer) - 1);
378                         if (res <= 0)
379                                 break;
380                         buffer[res] = '\0';
381                         if (res > len) {
382                                 dbg("callout len %d too short", len);
383                                 retval = -1;
384                         }
385                         if (value_set) {
386                                 dbg("callout value already set");
387                                 retval = -1;
388                         } else {
389                                 value_set = 1;
390                                 strncpy(value, buffer, len);
391                                 pos = value + strlen(value)-1;
392                                 if (pos[0] == '\n')
393                                 pos[0] = '\0';
394                                 dbg("callout returned '%s'", value);
395                         }
396                 }
397                 close(fds[0]);
398                 res = wait(&status);
399                 if (res < 0) {
400                         dbg("wait failed result %d", res);
401                         retval = -1;
402                 }
403
404                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
405                         dbg("callout program status 0x%x", status);
406                         retval = -1;
407                 }
408         }
409         return retval;
410 }
411
412 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
413 {
414         struct config_device *dev;
415
416         list_for_each_entry(dev, &config_device_list, node) {
417                 if (dev->type != CALLOUT)
418                         continue;
419
420                 if (dev->bus[0] != '\0') {
421                         /* as the user specified a bus, we must match it up */
422                         if (!sysfs_device)
423                                 continue;
424                         dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
425                         if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
426                                 continue;
427                 }
428
429                 /* substitute anything that needs to be in the program name */
430                 apply_format(udev, dev->exec_program);
431                 if (exec_program(dev->exec_program, udev->callout_value, NAME_SIZE))
432                         continue;
433                 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
434                         continue;
435                 strfieldcpy(udev->name, dev->name);
436                 strfieldcpy(udev->symlink, dev->symlink);
437                 dbg("callout returned matching value '%s', '%s' becomes '%s'",
438                     dev->id, class_dev->name, udev->name);
439                 return 0;
440         }
441         return -ENODEV;
442 }
443
444 static int match_pair(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
445 {
446         struct sysfs_attribute *tmpattr = NULL;
447         char *c;
448
449         if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
450                 return -ENODEV;
451
452         dbg("look for device attribute '%s'", pair->file);
453         /* try to find the attribute in the class device directory */
454         tmpattr = sysfs_get_classdev_attr(class_dev, pair->file);
455         if (tmpattr)
456                 goto label_found;
457
458         /* look in the class device directory if present */
459         if (sysfs_device) {
460                 tmpattr = sysfs_get_device_attr(sysfs_device, pair->file);
461                 if (tmpattr)
462                         goto label_found;
463         }
464
465         return -ENODEV;
466
467 label_found:
468         c = tmpattr->value + strlen(tmpattr->value)-1;
469         if (*c == '\n')
470                 *c = 0x00;
471         dbg("compare attribute '%s' value '%s' with '%s'",
472                   pair->file, tmpattr->value, pair->value);
473         if (strcmp_pattern(pair->value, tmpattr->value) != 0)
474                 return -ENODEV;
475
476         dbg("found matching attribute '%s' with value '%s'",
477             pair->file, pair->value);
478         return 0;
479 }
480
481 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
482 {
483         struct sysfs_pair *pair;
484         struct config_device *dev;
485         int i;
486         int match;
487
488         list_for_each_entry(dev, &config_device_list, node) {
489                 if (dev->type != LABEL)
490                         continue;
491
492                 if (dev->bus[0] != '\0') {
493                         /* as the user specified a bus, we must match it up */
494                         if (!sysfs_device)
495                                 continue;
496                         dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
497                         if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
498                                 continue;
499                 }
500
501                 match = 1;
502                 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
503                         pair = &dev->sysfs_pair[i];
504                         if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
505                                 break;
506                         if (match_pair(class_dev, sysfs_device, pair) != 0) {
507                                 match = 0;
508                                 break;
509                         }
510                 }
511                 if (match == 0)
512                         continue;
513
514                 /* found match */
515                 strfieldcpy(udev->name, dev->name);
516                 strfieldcpy(udev->symlink, dev->symlink);
517                 dbg("found matching attribute, '%s' becomes '%s' ",
518                     class_dev->name, udev->name);
519
520                 return 0;
521         }
522         return -ENODEV;
523 }
524
525 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
526 {
527         struct config_device *dev;
528         char path[SYSFS_PATH_MAX];
529         int found;
530         char *temp = NULL;
531
532         /* we have to have a sysfs device for NUMBER to work */
533         if (!sysfs_device)
534                 return -ENODEV;
535
536         list_for_each_entry(dev, &config_device_list, node) {
537                 if (dev->type != NUMBER)
538                         continue;
539
540                 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
541                 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
542                         continue;
543
544                 found = 0;
545                 strfieldcpy(path, sysfs_device->path);
546                 temp = strrchr(path, '/');
547                 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
548                 if (strstr(temp, dev->id) != NULL) {
549                         found = 1;
550                 } else {
551                         *temp = 0x00;
552                         temp = strrchr(path, '/');
553                         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
554                         if (strstr(temp, dev->id) != NULL)
555                                 found = 1;
556                 }
557                 if (!found)
558                         continue;
559                 strfieldcpy(udev->name, dev->name);
560                 strfieldcpy(udev->symlink, dev->symlink);
561                 dbg("found matching id '%s', '%s' becomes '%s'",
562                     dev->id, class_dev->name, udev->name);
563                 return 0;
564         }
565         return -ENODEV;
566 }
567
568 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
569 {
570         struct config_device *dev;
571         char path[SYSFS_PATH_MAX];
572         int found;
573         char *temp = NULL;
574
575         /* we have to have a sysfs device for TOPOLOGY to work */
576         if (!sysfs_device)
577                 return -ENODEV;
578
579         list_for_each_entry(dev, &config_device_list, node) {
580                 if (dev->type != TOPOLOGY)
581                         continue;
582
583                 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
584                 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
585                         continue;
586
587                 found = 0;
588                 strfieldcpy(path, sysfs_device->path);
589                 temp = strrchr(path, '/');
590                 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
591                 if (strstr(temp, dev->place) != NULL) {
592                         found = 1;
593                 } else {
594                         *temp = 0x00;
595                         temp = strrchr(path, '/');
596                         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
597                         if (strstr(temp, dev->place) != NULL)
598                                 found = 1;
599                 }
600                 if (!found)
601                         continue;
602
603                 strfieldcpy(udev->name, dev->name);
604                 strfieldcpy(udev->symlink, dev->symlink);
605                 dbg("found matching place '%s', '%s' becomes '%s'",
606                     dev->place, class_dev->name, udev->name);
607                 return 0;
608         }
609         return -ENODEV;
610 }
611
612 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
613 {
614         struct config_device *dev;
615
616         list_for_each_entry(dev, &config_device_list, node) {
617                 if (dev->type != REPLACE)
618                         continue;
619
620                 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
621                 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
622                         continue;
623
624                 strfieldcpy(udev->name, dev->name);
625                 strfieldcpy(udev->symlink, dev->symlink);
626                 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
627                 
628                 return 0;
629         }
630         return -ENODEV;
631 }
632
633 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
634 {
635         /* heh, this is pretty simple... */
636         strfieldcpy(udev->name, class_dev->name);
637 }
638
639 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
640 {
641         struct sysfs_device *sysfs_device;
642         struct sysfs_class_device *class_dev_parent;
643         int loop;
644         char filename[SYSFS_PATH_MAX + 6];
645         int retval;
646         char *temp;
647         int partition = 0;
648
649         /* Figure out where the device symlink is at.  For char devices this will
650          * always be in the class_dev->path.  But for block devices, it's different.
651          * The main block device will have the device symlink in it's path, but
652          * all partitions have the symlink in its parent directory.
653          * But we need to watch out for block devices that do not have parents, yet
654          * look like a partition (fd0, loop0, etc.)  They all do not have a device
655          * symlink yet.  We do sit and spin on waiting for them right now, we should
656          * possibly have a whitelist for these devices here...
657          */
658         strcpy(filename, class_dev->path);
659         dbg("filename = %s", filename);
660         if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
661                 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
662                         temp = strrchr(filename, '/');
663                         if (temp) {
664                                 char *temp2 = strrchr(filename, '/');
665                                 partition = 1;
666                                 *temp = 0x00;
667                                 dbg("temp2 = %s", temp2);
668                                 if (temp2 && (strcmp(temp2, "/block") == 0)) {
669                                         /* oops, we have no parent block device, so go back to original directory */
670                                         strcpy(filename, class_dev->path);
671                                         partition = 0;
672                                 }
673                         }
674                 }
675         }
676         strcat(filename, "/device");
677
678         loop = 2;
679         while (loop--) {
680                 struct stat buf;
681                 dbg("looking for '%s'", filename);
682                 retval = stat(filename, &buf);
683                 if (!retval)
684                         break;
685                 /* sleep to give the kernel a chance to create the device file */
686                 sleep(1);
687         }
688
689         loop = 1;       /* FIXME put a real value in here for when everything is fixed... */
690         while (loop--) {
691                 /* find the sysfs_device for this class device */
692                 /* Wouldn't it really be nice if libsysfs could do this for us? */
693                 sysfs_device = sysfs_get_classdev_device(class_dev);
694                 if (sysfs_device != NULL)
695                         goto exit;
696
697                 /* if it's a partition, we need to get the parent device */
698                 if (partition) {
699                         /* FIXME  HACK HACK HACK HACK
700                          * for some reason partitions need this extra sleep here, in order
701                          * to wait for the device properly.  Once the libsysfs code is
702                          * fixed properly, this sleep should go away, and we can just loop above.
703                          */
704                         sleep(1);
705                         dbg("really is a partition");
706                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
707                         if (class_dev_parent == NULL) {
708                                 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
709                         } else {
710                                 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
711                                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
712                                 if (sysfs_device != NULL)
713                                         goto exit;
714                         }
715                 }
716                 /* sleep to give the kernel a chance to create the link */
717                 /* FIXME remove comment...
718                 sleep(1); */
719         }
720         dbg("Timed out waiting for device symlink, continuing on anyway...");
721 exit:
722         return sysfs_device;
723 }
724
725 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
726 {
727         struct sysfs_device *sysfs_device = NULL;
728         int retval = 0;
729         struct perm_device *perm;
730         char *pos;
731
732         udev->mode = 0;
733
734         /* find the sysfs_device associated with this class device */
735         sysfs_device = get_sysfs_device(class_dev);
736         if (sysfs_device) {
737                 dbg("sysfs_device->path='%s'", sysfs_device->path);
738                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
739                 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
740                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
741                 wait_for_device_to_initialize(sysfs_device);
742         } else {
743                 dbg("class_dev->name = '%s'", class_dev->name);
744         }
745
746         strfieldcpy(udev->kernel_name, class_dev->name);
747
748         /* get kernel number */
749         pos = class_dev->name + strlen(class_dev->name);
750         while (isdigit(*(pos-1)))
751                 pos--;
752         strfieldcpy(udev->kernel_number, pos);
753         dbg("kernel_number='%s'", udev->kernel_number);
754
755         /* rules are looked at in priority order */
756         retval = do_ignore(class_dev, udev, sysfs_device);
757         if (retval == 0) {
758                 dbg("name, '%s' is being ignored", class_dev->name);
759                 return 1;
760         }
761
762         retval = do_callout(class_dev, udev, sysfs_device);
763         if (retval == 0)
764                 goto found;
765
766         retval = do_label(class_dev, udev, sysfs_device);
767         if (retval == 0)
768                 goto found;
769
770         retval = do_number(class_dev, udev, sysfs_device);
771         if (retval == 0)
772                 goto found;
773
774         retval = do_topology(class_dev, udev, sysfs_device);
775         if (retval == 0)
776                 goto found;
777
778         retval = do_replace(class_dev, udev, sysfs_device);
779         if (retval == 0)
780                 goto found;
781
782         do_kernelname(class_dev, udev);
783         goto done;
784
785 found:
786         /* substitute placeholder */
787         apply_format(udev, udev->name);
788         apply_format(udev, udev->symlink);
789
790 done:
791         perm = find_perm(udev->name);
792         if (perm) {
793                 udev->mode = perm->mode;
794                 strfieldcpy(udev->owner, perm->owner);
795                 strfieldcpy(udev->group, perm->group);
796         } else {
797                 /* no matching perms found :( */
798                 udev->mode = get_default_mode(class_dev);
799                 udev->owner[0] = 0x00;
800                 udev->group[0] = 0x00;
801         }
802         dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
803             udev->name, udev->owner, udev->group, udev->mode);
804
805         return 0;
806 }
807
808 int namedev_init(void)
809 {
810         int retval;
811
812         retval = namedev_init_rules();
813         if (retval)
814                 return retval;
815
816         retval = namedev_init_permissions();
817         if (retval)
818                 return retval;
819
820         dump_config_dev_list();
821         dump_perm_dev_list();
822         return retval;
823 }