chiark / gitweb /
[PATCH] If a CALLOUT rule has a BUS id, then we must check to see if the device is...
[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         { .bus = "pci",         .file = "vendor" },
256         {}
257 };
258
259 #define SECONDS_TO_WAIT_FOR_FILE        10
260 static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device)
261 {
262         /* sleep until we see the file for this specific bus type show up this
263          * is needed because we can easily out-run the kernel in looking for
264          * these files before the paticular subsystem has created them in the
265          * sysfs tree properly.
266          *
267          * And people thought that the /sbin/hotplug event system was going to
268          * be slow, poo on you for arguing that before even testing it...
269          */
270         struct bus_file *b = &bus_files[0];
271         struct sysfs_attribute *tmpattr;
272         int loop;
273
274         while (1) {
275                 if (b->bus == NULL)
276                         break;
277                 if (strcmp(sysfs_device->bus, b->bus) == 0) {
278                         tmpattr = NULL;
279                         loop = SECONDS_TO_WAIT_FOR_FILE;
280                         while (loop--) {
281                                 dbg("looking for file '%s' on bus '%s'", b->file, b->bus);
282                                 tmpattr = sysfs_get_device_attr(sysfs_device, b->file);
283                                 if (tmpattr) {
284                                         /* found it! */
285                                         goto exit;
286                                 }
287                                 /* sleep to give the kernel a chance to create the file */
288                                 sleep(1);
289                         }
290                         dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file);
291                         goto exit;
292                 }
293                 b++;
294         }
295         dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus);
296 exit:
297         return; /* here to prevent compiler warning... */
298 }
299
300 static int exec_callout(struct config_device *dev, char *value, int len)
301 {
302         int retval;
303         int res;
304         int status;
305         int fds[2];
306         pid_t pid;
307         int value_set = 0;
308         char buffer[256];
309         char *pos;
310         char *args[CALLOUT_MAXARG];
311         int i;
312
313         dbg("callout to '%s'", dev->exec_program);
314         retval = pipe(fds);
315         if (retval != 0) {
316                 dbg("pipe failed");
317                 return -1;
318         }
319         pid = fork();
320         if (pid == -1) {
321                 dbg("fork failed");
322                 return -1;
323         }
324
325         if (pid == 0) {
326                 /* child */
327                 close(STDOUT_FILENO);
328                 dup(fds[1]);    /* dup write side of pipe to STDOUT */
329                 if (strchr(dev->exec_program, ' ')) {
330                         /* callout with arguments */
331                         pos = dev->exec_program;
332                         for (i=0; i < CALLOUT_MAXARG-1; i++) {
333                                 args[i] = strsep(&pos, " ");
334                                 if (args[i] == NULL)
335                                         break;
336                         }
337                         if (args[i]) {
338                                 dbg("too many args - %d", i);
339                                 args[i] = NULL;
340                         }
341                         retval = execve(args[0], args, main_envp);
342                 } else {
343                         retval = execve(dev->exec_program, main_argv, main_envp);
344                 }
345                 if (retval != 0) {
346                         dbg("child execve failed");
347                         exit(1);
348                 }
349                 return -1; /* avoid compiler warning */
350         } else {
351                 /* parent reads from fds[0] */
352                 close(fds[1]);
353                 retval = 0;
354                 while (1) {
355                         res = read(fds[0], buffer, sizeof(buffer) - 1);
356                         if (res <= 0)
357                                 break;
358                         buffer[res] = '\0';
359                         if (res > len) {
360                                 dbg("callout len %d too short", len);
361                                 retval = -1;
362                         }
363                         if (value_set) {
364                                 dbg("callout value already set");
365                                 retval = -1;
366                         } else {
367                                 value_set = 1;
368                                 strncpy(value, buffer, len);
369                                 pos = value + strlen(value)-1;
370                                 if (pos[0] == '\n')
371                                 pos[0] = '\0';
372                                 dbg("callout returned '%s'", value);
373                         }
374                 }
375                 close(fds[0]);
376                 res = wait(&status);
377                 if (res < 0) {
378                         dbg("wait failed result %d", res);
379                         retval = -1;
380                 }
381
382 #ifndef __KLIBC__
383                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
384                         dbg("callout program status 0x%x", status);
385                         retval = -1;
386                 }
387 #endif
388         }
389         return retval;
390 }
391
392 static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
393 {
394         struct config_device *dev;
395         struct list_head *tmp;
396
397         list_for_each(tmp, &config_device_list) {
398                 dev = list_entry(tmp, struct config_device, node);
399                 if (dev->type != CALLOUT)
400                         continue;
401
402                 if (dev->bus[0] != '\0') {
403                         /* as the user specified a bus, we must match it up */
404                         if (!sysfs_device)
405                                 continue;
406                         dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
407                         if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
408                                 continue;
409                 }
410
411                 /* substitute anything that needs to be in the program name */
412                 apply_format(udev, dev->exec_program);
413                 if (exec_callout(dev, udev->callout_value, NAME_SIZE))
414                         continue;
415                 if (strcmp_pattern(dev->id, udev->callout_value) != 0)
416                         continue;
417                 strfieldcpy(udev->name, dev->name);
418                 strfieldcpy(udev->symlink, dev->symlink);
419                 dbg("callout returned matching value '%s', '%s' becomes '%s'",
420                     dev->id, class_dev->name, udev->name);
421                 return 0;
422         }
423         return -ENODEV;
424 }
425
426 static int match_pair(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
427 {
428         struct sysfs_attribute *tmpattr = NULL;
429         char *c;
430
431         if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
432                 return -ENODEV;
433
434         dbg("look for device attribute '%s'", pair->file);
435         /* try to find the attribute in the class device directory */
436         tmpattr = sysfs_get_classdev_attr(class_dev, pair->file);
437         if (tmpattr)
438                 goto label_found;
439
440         /* look in the class device directory if present */
441         if (sysfs_device) {
442                 tmpattr = sysfs_get_device_attr(sysfs_device, pair->file);
443                 if (tmpattr)
444                         goto label_found;
445         }
446
447         return -ENODEV;
448
449 label_found:
450         c = tmpattr->value + strlen(tmpattr->value)-1;
451         if (*c == '\n')
452                 *c = 0x00;
453         dbg("compare attribute '%s' value '%s' with '%s'",
454                   pair->file, tmpattr->value, pair->value);
455         if (strcmp_pattern(pair->value, tmpattr->value) != 0)
456                 return -ENODEV;
457
458         dbg("found matching attribute '%s' with value '%s'",
459             pair->file, pair->value);
460         return 0;
461 }
462
463 static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
464 {
465         struct sysfs_pair *pair;
466         struct config_device *dev;
467         struct list_head *tmp;
468         int i;
469         int match;
470
471         list_for_each(tmp, &config_device_list) {
472                 dev = list_entry(tmp, struct config_device, node);
473                 if (dev->type != LABEL)
474                         continue;
475
476                 if (sysfs_device) {
477                         dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
478                         if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
479                                 continue;
480                 }
481
482                 match = 1;
483                 for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
484                         pair = &dev->sysfs_pair[i];
485                         if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
486                                 break;
487                         if (match_pair(class_dev, sysfs_device, pair) != 0) {
488                                 match = 0;
489                                 break;
490                         }
491                 }
492                 if (match == 0)
493                         continue;
494
495                 /* found match */
496                 strfieldcpy(udev->name, dev->name);
497                 strfieldcpy(udev->symlink, dev->symlink);
498                 dbg("found matching attribute, '%s' becomes '%s' ",
499                     class_dev->name, udev->name);
500
501                 return 0;
502         }
503         return -ENODEV;
504 }
505
506 static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
507 {
508         struct config_device *dev;
509         struct list_head *tmp;
510         char path[SYSFS_PATH_MAX];
511         int found;
512         char *temp = NULL;
513
514         /* we have to have a sysfs device for NUMBER to work */
515         if (!sysfs_device)
516                 return -ENODEV;
517
518         list_for_each(tmp, &config_device_list) {
519                 dev = list_entry(tmp, struct config_device, node);
520                 if (dev->type != NUMBER)
521                         continue;
522
523                 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
524                 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
525                         continue;
526
527                 found = 0;
528                 strfieldcpy(path, sysfs_device->path);
529                 temp = strrchr(path, '/');
530                 dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
531                 if (strstr(temp, dev->id) != NULL) {
532                         found = 1;
533                 } else {
534                         *temp = 0x00;
535                         temp = strrchr(path, '/');
536                         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
537                         if (strstr(temp, dev->id) != NULL)
538                                 found = 1;
539                 }
540                 if (!found)
541                         continue;
542                 strfieldcpy(udev->name, dev->name);
543                 strfieldcpy(udev->symlink, dev->symlink);
544                 dbg("found matching id '%s', '%s' becomes '%s'",
545                     dev->id, class_dev->name, udev->name);
546                 return 0;
547         }
548         return -ENODEV;
549 }
550
551 static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
552 {
553         struct config_device *dev;
554         struct list_head *tmp;
555         char path[SYSFS_PATH_MAX];
556         int found;
557         char *temp = NULL;
558
559         /* we have to have a sysfs device for TOPOLOGY to work */
560         if (!sysfs_device)
561                 return -ENODEV;
562
563         list_for_each(tmp, &config_device_list) {
564                 dev = list_entry(tmp, struct config_device, node);
565                 if (dev->type != TOPOLOGY)
566                         continue;
567
568                 dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus);
569                 if (strcasecmp(dev->bus, sysfs_device->bus) != 0)
570                         continue;
571
572                 found = 0;
573                 strfieldcpy(path, sysfs_device->path);
574                 temp = strrchr(path, '/');
575                 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
576                 if (strstr(temp, dev->place) != NULL) {
577                         found = 1;
578                 } else {
579                         *temp = 0x00;
580                         temp = strrchr(path, '/');
581                         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
582                         if (strstr(temp, dev->place) != NULL)
583                                 found = 1;
584                 }
585                 if (!found)
586                         continue;
587
588                 strfieldcpy(udev->name, dev->name);
589                 strfieldcpy(udev->symlink, dev->symlink);
590                 dbg("found matching place '%s', '%s' becomes '%s'",
591                     dev->place, class_dev->name, udev->name);
592                 return 0;
593         }
594         return -ENODEV;
595 }
596
597 static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device)
598 {
599         struct config_device *dev;
600         struct list_head *tmp;
601
602         list_for_each(tmp, &config_device_list) {
603                 dev = list_entry(tmp, struct config_device, node);
604                 if (dev->type != REPLACE)
605                         continue;
606
607                 dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name);
608                 if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0)
609                         continue;
610
611                 strfieldcpy(udev->name, dev->name);
612                 strfieldcpy(udev->symlink, dev->symlink);
613                 dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
614                 
615                 return 0;
616         }
617         return -ENODEV;
618 }
619
620 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
621 {
622         /* heh, this is pretty simple... */
623         strfieldcpy(udev->name, class_dev->name);
624 }
625
626 static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev)
627 {
628         struct sysfs_device *sysfs_device;
629         struct sysfs_class_device *class_dev_parent;
630         int loop;
631         char filename[SYSFS_PATH_MAX + 6];
632         int retval;
633         char *temp;
634         int partition = 0;
635
636         /* Figure out where the device symlink is at.  For char devices this will
637          * always be in the class_dev->path.  But for block devices, it's different.
638          * The main block device will have the device symlink in it's path, but
639          * all partitions have the symlink in its parent directory.
640          * But we need to watch out for block devices that do not have parents, yet
641          * look like a partition (fd0, loop0, etc.)  They all do not have a device
642          * symlink yet.  We do sit and spin on waiting for them right now, we should
643          * possibly have a whitelist for these devices here...
644          */
645         strcpy(filename, class_dev->path);
646         dbg("filename = %s", filename);
647         if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) {
648                 if (isdigit(class_dev->path[strlen(class_dev->path)-1])) {
649                         temp = strrchr(filename, '/');
650                         if (temp) {
651                                 partition = 1;
652                                 *temp = 0x00;
653                                 char *temp2 = strrchr(filename, '/');
654                                 dbg("temp2 = %s", temp2);
655                                 if (temp2 && (strcmp(temp2, "/block") == 0)) {
656                                         /* oops, we have no parent block device, so go back to original directory */
657                                         strcpy(filename, class_dev->path);
658                                         partition = 0;
659                                 }
660                         }
661                 }
662         }
663         strcat(filename, "/device");
664
665         loop = 2;
666         while (loop--) {
667                 struct stat buf;
668                 dbg("looking for '%s'", filename);
669                 retval = stat(filename, &buf);
670                 if (!retval)
671                         break;
672                 /* sleep to give the kernel a chance to create the device file */
673                 sleep(1);
674         }
675
676         loop = 1;       /* FIXME put a real value in here for when everything is fixed... */
677         while (loop--) {
678                 /* find the sysfs_device for this class device */
679                 /* Wouldn't it really be nice if libsysfs could do this for us? */
680                 sysfs_device = sysfs_get_classdev_device(class_dev);
681                 if (sysfs_device != NULL)
682                         goto exit;
683
684                 /* if it's a partition, we need to get the parent device */
685                 if (partition) {
686                         /* FIXME  HACK HACK HACK HACK
687                          * for some reason partitions need this extra sleep here, in order
688                          * to wait for the device properly.  Once the libsysfs code is
689                          * fixed properly, this sleep should go away, and we can just loop above.
690                          */
691                         sleep(1);
692                         dbg("really is a partition");
693                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
694                         if (class_dev_parent == NULL) {
695                                 dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name);
696                         } else {
697                                 dbg("class_dev_parent->name='%s'", class_dev_parent->name);
698                                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
699                                 if (sysfs_device != NULL)
700                                         goto exit;
701                         }
702                 }
703                 /* sleep to give the kernel a chance to create the link */
704                 /* FIXME remove comment...
705                 sleep(1); */
706         }
707         dbg("Timed out waiting for device symlink, continuing on anyway...");
708 exit:
709         return sysfs_device;
710 }
711
712 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
713 {
714         struct sysfs_device *sysfs_device = NULL;
715         int retval = 0;
716         struct perm_device *perm;
717         char *pos;
718
719         udev->mode = 0;
720
721         /* find the sysfs_device associated with this class device */
722         sysfs_device = get_sysfs_device(class_dev);
723         if (sysfs_device) {
724                 dbg("sysfs_device->path='%s'", sysfs_device->path);
725                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
726                 dbg("sysfs_device->bus='%s'", sysfs_device->bus);
727                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
728                 wait_for_device_to_initialize(sysfs_device);
729         } else {
730                 dbg("class_dev->name = '%s'", class_dev->name);
731         }
732
733         strfieldcpy(udev->kernel_name, class_dev->name);
734
735         /* get kernel number */
736         pos = class_dev->name + strlen(class_dev->name);
737         while (isdigit(*(pos-1)))
738                 pos--;
739         strfieldcpy(udev->kernel_number, pos);
740         dbg("kernel_number='%s'", udev->kernel_number);
741
742         /* rules are looked at in priority order */
743         retval = do_callout(class_dev, udev, sysfs_device);
744         if (retval == 0)
745                 goto found;
746
747         retval = do_label(class_dev, udev, sysfs_device);
748         if (retval == 0)
749                 goto found;
750
751         retval = do_number(class_dev, udev, sysfs_device);
752         if (retval == 0)
753                 goto found;
754
755         retval = do_topology(class_dev, udev, sysfs_device);
756         if (retval == 0)
757                 goto found;
758
759         retval = do_replace(class_dev, udev, sysfs_device);
760         if (retval == 0)
761                 goto found;
762
763         do_kernelname(class_dev, udev);
764         goto done;
765
766 found:
767         /* substitute placeholder */
768         apply_format(udev, udev->name);
769         apply_format(udev, udev->symlink);
770
771 done:
772         perm = find_perm(udev->name);
773         if (perm) {
774                 udev->mode = perm->mode;
775                 strfieldcpy(udev->owner, perm->owner);
776                 strfieldcpy(udev->group, perm->group);
777         } else {
778                 /* no matching perms found :( */
779                 udev->mode = get_default_mode(class_dev);
780                 udev->owner[0] = 0x00;
781                 udev->group[0] = 0x00;
782         }
783         dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
784             udev->name, udev->owner, udev->group, udev->mode);
785
786         return 0;
787 }
788
789 int namedev_init(void)
790 {
791         int retval;
792
793         retval = namedev_init_rules();
794         if (retval)
795                 return retval;
796
797         retval = namedev_init_permissions();
798         if (retval)
799                 return retval;
800
801         dump_config_dev_list();
802         dump_perm_dev_list();
803         return retval;
804 }