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