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