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