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