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