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