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