chiark / gitweb /
[PATCH] 049 release
[elogind.git] / namedev.c
1 /*
2  * namedev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/wait.h>
33 #include <sys/stat.h>
34
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "list.h"
37 #include "udev.h"
38 #include "udev_utils.h"
39 #include "udev_version.h"
40 #include "logging.h"
41 #include "namedev.h"
42 #include "udev_db.h"
43
44 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr);
45
46 LIST_HEAD(config_device_list);
47 LIST_HEAD(perm_device_list);
48
49
50 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
51 static int strcmp_pattern(const char *p, const char *s)
52 {
53         if (s[0] == '\0') {
54                 while (p[0] == '*')
55                         p++;
56                 return (p[0] != '\0');
57         }
58         switch (p[0]) {
59         case '[':
60                 {
61                         int not = 0;
62                         p++;
63                         if (p[0] == '!') {
64                                 not = 1;
65                                 p++;
66                         }
67                         while ((p[0] != '\0') && (p[0] != ']')) {
68                                 int match = 0;
69                                 if (p[1] == '-') {
70                                         if ((s[0] >= p[0]) && (s[0] <= p[2]))
71                                                 match = 1;
72                                         p += 3;
73                                 } else {
74                                         match = (p[0] == s[0]);
75                                         p++;
76                                 }
77                                 if (match ^ not) {
78                                         while ((p[0] != '\0') && (p[0] != ']'))
79                                                 p++;
80                                         if (p[0] == ']')
81                                                 return strcmp_pattern(p+1, s+1);
82                                 }
83                         }
84                 }
85                 break;
86         case '*':
87                 if (strcmp_pattern(p, s+1))
88                         return strcmp_pattern(p+1, s);
89                 return 0;
90         case '\0':
91                 if (s[0] == '\0') {
92                         return 0;
93                 }
94                 break;
95         default:
96                 if ((p[0] == s[0]) || (p[0] == '?'))
97                         return strcmp_pattern(p+1, s+1);
98                 break;
99         }
100         return 1;
101 }
102
103 static struct perm_device *find_perm_entry(const char *name)
104 {
105         struct perm_device *perm;
106
107         list_for_each_entry(perm, &perm_device_list, node) {
108                 if (strcmp_pattern(perm->name, name))
109                         continue;
110                 return perm;
111         }
112         return NULL;
113 }
114
115 /* extract possible {attr} and move str behind it */
116 static char *get_format_attribute(char **str)
117 {
118         char *pos;
119         char *attr = NULL;
120
121         if (*str[0] == '{') {
122                 pos = strchr(*str, '}');
123                 if (pos == NULL) {
124                         dbg("missing closing brace for format");
125                         return NULL;
126                 }
127                 pos[0] = '\0';
128                 attr = *str+1;
129                 *str = pos+1;
130                 dbg("attribute='%s', str='%s'", attr, *str);
131         }
132         return attr;
133 }
134
135 /* extract possible format length and move str behind it*/
136 static int get_format_len(char **str)
137 {
138         int num;
139         char *tail;
140
141         if (isdigit(*str[0])) {
142                 num = (int) strtoul(*str, &tail, 10);
143                 if (num > 0) {
144                         *str = tail;
145                         dbg("format length=%i", num);
146                         return num;
147                 } else {
148                         dbg("format parsing error '%s'", *str);
149                 }
150         }
151         return -1;
152 }
153
154 /** Finds the lowest positive N such that <name>N isn't present in 
155  *  $(udevroot) either as a file or a symlink.
156  *
157  *  @param  name                Name to check for
158  *  @return                     0 if <name> didn't exist and N otherwise.
159  */
160 static int find_free_number(struct udevice *udev, const char *name)
161 {
162         char filename[NAME_SIZE];
163         int num = 0;
164         struct udevice db_udev;
165
166         strfieldcpy(filename, name);
167         while (1) {
168                 dbg("look for existing node '%s'", filename);
169                 memset(&db_udev, 0x00, sizeof(struct udevice));
170                 if (udev_db_get_device_byname(&db_udev, filename) != 0) {
171                         dbg("free num=%d", num);
172                         return num;
173                 }
174
175                 num++;
176                 if (num > 1000) {
177                         info("find_free_number gone crazy (num=%d), aborted", num);
178                         return -1;
179                 }
180                 snprintf(filename, NAME_SIZE, "%s%d", name, num);
181                 filename[NAME_SIZE-1] = '\0';
182         }
183 }
184
185 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
186                          struct sysfs_class_device *class_dev,
187                          struct sysfs_device *sysfs_device)
188 {
189         char temp[NAME_SIZE];
190         char temp2[NAME_SIZE];
191         char *tail;
192         char *pos;
193         char *attr;
194         int len;
195         int i;
196         char c;
197         char *spos;
198         char *rest;
199         int slen;
200         struct sysfs_attribute *tmpattr;
201         unsigned int next_free_number;
202
203         pos = string;
204         while (1) {
205                 pos = strchr(pos, '%');
206                 if (pos == NULL)
207                         break;
208
209                 pos[0] = '\0';
210                 tail = pos+1;
211                 len = get_format_len(&tail);
212                 c = tail[0];
213                 strfieldcpy(temp, tail+1);
214                 tail = temp;
215                 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
216                 attr = get_format_attribute(&tail);
217
218
219                 switch (c) {
220                 case 'b':
221                         if (strlen(udev->bus_id) == 0)
222                                 break;
223                         strfieldcatmax(string, udev->bus_id, maxsize);
224                         dbg("substitute bus_id '%s'", udev->bus_id);
225                         break;
226                 case 'k':
227                         if (strlen(udev->kernel_name) == 0)
228                                 break;
229                         strfieldcatmax(string, udev->kernel_name, maxsize);
230                         dbg("substitute kernel name '%s'", udev->kernel_name);
231                         break;
232                 case 'n':
233                         if (strlen(udev->kernel_number) == 0)
234                                 break;
235                         strfieldcatmax(string, udev->kernel_number, maxsize);
236                         dbg("substitute kernel number '%s'", udev->kernel_number);
237                                 break;
238                 case 'm':
239                         strintcatmax(string, udev->minor, maxsize);
240                         dbg("substitute minor number '%u'", udev->minor);
241                         break;
242                 case 'M':
243                         strintcatmax(string, udev->major, maxsize);
244                         dbg("substitute major number '%u'", udev->major);
245                         break;
246                 case 'c':
247                         if (strlen(udev->program_result) == 0)
248                                 break;
249                         /* get part part of the result string */
250                         i = 0;
251                         if (attr != NULL)
252                                 i = strtoul(attr, &rest, 10);
253                         if (i > 0) {
254                                 foreach_strpart(udev->program_result, " \n\r", spos, slen) {
255                                         i--;
256                                         if (i == 0)
257                                                 break;
258                                 }
259                                 if (i > 0) {
260                                         dbg("requested part of result string not found");
261                                         break;
262                                 }
263                                 if (rest[0] == '+')
264                                         strfieldcpy(temp2, spos);
265                                 else
266                                         strfieldcpymax(temp2, spos, slen+1);
267                                 strfieldcatmax(string, temp2, maxsize);
268                                 dbg("substitute part of result string '%s'", temp2);
269                         } else {
270                                 strfieldcatmax(string, udev->program_result, maxsize);
271                                 dbg("substitute result string '%s'", udev->program_result);
272                         }
273                         break;
274                 case 's':
275                         if (attr != NULL) {
276                                 tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
277                                 if (tmpattr == NULL) {
278                                         dbg("sysfa attribute '%s' not found", attr);
279                                         break;
280                                 }
281                                 /* strip trailing whitespace of matching value */
282                                 if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
283                                         i = len = strlen(tmpattr->value);
284                                         while (i > 0 &&  isspace(tmpattr->value[i-1]))
285                                                 i--;
286                                         if (i < len) {
287                                                 tmpattr->value[i] = '\0';
288                                                 dbg("remove %i trailing whitespace chars from '%s'",
289                                                          len - i, tmpattr->value);
290                                         }
291                                 }
292                                 strfieldcatmax(string, tmpattr->value, maxsize);
293                                 dbg("substitute sysfs value '%s'", tmpattr->value);
294                         } else {
295                                 dbg("missing attribute");
296                         }
297                         break;
298                 case '%':
299                         strfieldcatmax(string, "%", maxsize);
300                         pos++;
301                         break;
302                 case 'e':
303                         next_free_number = find_free_number(udev, string);
304                         if (next_free_number > 0) {
305                                 sprintf(temp2, "%d", next_free_number);
306                                 strfieldcatmax(string, temp2, maxsize);
307                         }
308                         break;
309                 default:
310                         dbg("unknown substitution type '%%%c'", c);
311                         break;
312                 }
313                 /* truncate to specified length */
314                 if (len > 0)
315                         pos[len] = '\0';
316
317                 strfieldcatmax(string, tail, maxsize);
318         }
319 }
320
321 static void fix_kernel_name(struct udevice *udev)
322 {
323         char *temp = udev->kernel_name;
324
325         while (*temp != 0x00) {
326                 /* Some block devices have a ! in their name, 
327                  * we need to change that to / */
328                 if (*temp == '!')
329                         *temp = '/';
330                 ++temp;
331         }
332 }
333
334 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
335 {
336         int retval;
337         int count;
338         int status;
339         int fds[2];
340         pid_t pid;
341         char *pos;
342         char arg[PROGRAM_SIZE];
343         char *argv[(PROGRAM_SIZE / 2) + 1];
344         int i;
345
346         strfieldcpy(arg, path);
347         i = 0;
348         if (strchr(path, ' ')) {
349                 pos = arg;
350                 while (pos != NULL) {
351                         if (pos[0] == '\'') {
352                                 /* don't separate if in apostrophes */
353                                 pos++;
354                                 argv[i] = strsep(&pos, "\'");
355                                 while (pos && pos[0] == ' ')
356                                         pos++;
357                         } else {
358                                 argv[i] = strsep(&pos, " ");
359                         }
360                         dbg("arg[%i] '%s'", i, argv[i]);
361                         i++;
362                 }
363                 argv[i] =  NULL;
364                 dbg("execute '%s' with parsed arguments", arg);
365         } else {
366                 argv[0] = arg;
367                 argv[1] = udev->subsystem;
368                 argv[2] = NULL;
369                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
370         }
371
372         retval = pipe(fds);
373         if (retval != 0) {
374                 dbg("pipe failed");
375                 return -1;
376         }
377
378         pid = fork();
379         switch(pid) {
380         case 0:
381                 /* child */
382                 /* dup2 write side of pipe to STDOUT */
383                 dup2(fds[1], STDOUT_FILENO);
384                 retval = execv(arg, argv);
385
386                 info(FIELD_PROGRAM " execution of '%s' failed", path);
387                 exit(1);
388         case -1:
389                 dbg("fork failed");
390                 return -1;
391         default:
392                 /* parent reads from fds[0] */
393                 close(fds[1]);
394                 retval = 0;
395                 i = 0;
396                 while (1) {
397                         count = read(fds[0], value + i, len - i-1);
398                         if (count <= 0)
399                                 break;
400
401                         i += count;
402                         if (i >= len-1) {
403                                 dbg("result len %d too short", len);
404                                 retval = -1;
405                                 break;
406                         }
407                 }
408
409                 if (count < 0) {
410                         dbg("read failed with '%s'", strerror(errno));
411                         retval = -1;
412                 }
413
414                 if (i > 0 && value[i-1] == '\n')
415                         i--;
416                 value[i] = '\0';
417                 dbg("result is '%s'", value);
418
419                 close(fds[0]);
420                 waitpid(pid, &status, 0);
421
422                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
423                         dbg("exec program status 0x%x", status);
424                         retval = -1;
425                 }
426         }
427         return retval;
428 }
429
430 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
431 {
432         struct sysfs_attribute *tmpattr = NULL;
433         char *c;
434
435         dbg("look for device attribute '%s'", attr);
436         /* try to find the attribute in the class device directory */
437         tmpattr = sysfs_get_classdev_attr(class_dev, attr);
438         if (tmpattr)
439                 goto attr_found;
440
441         /* look in the class device directory if present */
442         if (sysfs_device) {
443                 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
444                 if (tmpattr)
445                         goto attr_found;
446         }
447
448         return NULL;
449
450 attr_found:
451         c = strchr(tmpattr->value, '\n');
452         if (c != NULL)
453                 c[0] = '\0';
454
455         dbg("found attribute '%s'", tmpattr->path);
456         return tmpattr;
457 }
458
459 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
460 {
461         struct sysfs_attribute *tmpattr;
462         int i;
463         int len;
464
465         if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
466                 return -ENODEV;
467
468         tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
469         if (tmpattr == NULL)
470                 return -ENODEV;
471
472         /* strip trailing whitespace of value, if not asked to match for it */
473         if (! isspace(pair->value[strlen(pair->value)-1])) {
474                 i = len = strlen(tmpattr->value);
475                 while (i > 0 &&  isspace(tmpattr->value[i-1]))
476                         i--;
477                 if (i < len) {
478                         tmpattr->value[i] = '\0';
479                         dbg("remove %i trailing whitespace chars from '%s'",
480                             len - i, tmpattr->value);
481                 }
482         }
483
484         dbg("compare attribute '%s' value '%s' with '%s'",
485                   pair->file, tmpattr->value, pair->value);
486         if (strcmp_pattern(pair->value, tmpattr->value) != 0)
487                 return -ENODEV;
488
489         dbg("found matching attribute '%s' with value '%s'",
490             pair->file, pair->value);
491         return 0;
492 }
493
494 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
495 {
496         struct sysfs_pair *pair;
497         int i;
498
499         for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
500                 pair = &dev->sysfs_pair[i];
501                 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
502                         break;
503                 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
504                         dbg("sysfs attribute doesn't match");
505                         return -ENODEV;
506                 }
507         }
508
509         return 0;
510 }
511
512 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
513 {
514         char path[SYSFS_PATH_MAX];
515         char *temp = NULL;
516
517         /* we have to have a sysfs device for ID to work */
518         if (!sysfs_device)
519                 return -ENODEV;
520
521         strfieldcpy(path, sysfs_device->path);
522         temp = strrchr(path, '/');
523         temp++;
524         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
525         if (strcmp_pattern(dev->id, temp) != 0)
526                 return -ENODEV;
527         else
528                 return 0;
529 }
530
531 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
532 {
533         char path[SYSFS_PATH_MAX];
534         int found;
535         char *temp = NULL;
536
537         /* we have to have a sysfs device for PLACE to work */
538         if (!sysfs_device)
539                 return -ENODEV;
540
541         found = 0;
542         strfieldcpy(path, sysfs_device->path);
543         temp = strrchr(path, '/');
544         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
545         if (strstr(temp, dev->place) != NULL) {
546                 found = 1;
547         } else {
548                 *temp = 0x00;
549                 temp = strrchr(path, '/');
550                 dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
551                 if (strstr(temp, dev->place) != NULL)
552                         found = 1;
553         }
554         if (!found) {
555                 dbg("place doesn't match");
556                 return -ENODEV;
557         }
558
559         return 0;
560 }
561
562 static int match_rule(struct udevice *udev, struct config_device *dev,
563                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
564 {
565         while (1) {
566                 /* check for matching bus value */
567                 if (dev->bus[0] != '\0') {
568                         if (sysfs_device == NULL) {
569                                 dbg("device has no bus");
570                                 goto try_parent;
571                         }
572                         dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
573                             dev->bus, sysfs_device->bus);
574                         if (strcmp_pattern(dev->bus, sysfs_device->bus) != 0) {
575                                 dbg(FIELD_BUS " is not matching");
576                                 goto try_parent;
577                         } else {
578                                 dbg(FIELD_BUS " matches");
579                         }
580                 }
581
582                 /* check for matching kernel name */
583                 if (dev->kernel[0] != '\0') {
584                         dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
585                             dev->kernel, class_dev->name);
586                         if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
587                                 dbg(FIELD_KERNEL " is not matching");
588                                 goto try_parent;
589                         } else {
590                                 dbg(FIELD_KERNEL " matches");
591                         }
592                 }
593
594                 /* check for matching subsystem */
595                 if (dev->subsystem[0] != '\0') {
596                         dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
597                             dev->subsystem, class_dev->name);
598                         if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
599                                 dbg(FIELD_SUBSYSTEM " is not matching");
600                                 goto try_parent;
601                         } else {
602                                 dbg(FIELD_SUBSYSTEM " matches");
603                         }
604                 }
605
606                 /* check for matching driver */
607                 if (dev->driver[0] != '\0') {
608                         dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
609                             dev->driver, sysfs_device->driver_name);
610                         if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
611                                 dbg(FIELD_DRIVER " is not matching");
612                                 goto try_parent;
613                         } else {
614                                 dbg(FIELD_DRIVER " matches");
615                         }
616                 }
617
618                 /* check for matching bus id */
619                 if (dev->id[0] != '\0') {
620                         dbg("check " FIELD_ID);
621                         if (match_id(dev, class_dev, sysfs_device) != 0) {
622                                 dbg(FIELD_ID " is not matching");
623                                 goto try_parent;
624                         } else {
625                                 dbg(FIELD_ID " matches");
626                         }
627                 }
628
629                 /* check for matching place of device */
630                 if (dev->place[0] != '\0') {
631                         dbg("check " FIELD_PLACE);
632                         if (match_place(dev, class_dev, sysfs_device) != 0) {
633                                 dbg(FIELD_PLACE " is not matching");
634                                 goto try_parent;
635                         } else {
636                                 dbg(FIELD_PLACE " matches");
637                         }
638                 }
639
640                 /* check for matching sysfs pairs */
641                 if (dev->sysfs_pair[0].file[0] != '\0') {
642                         dbg("check " FIELD_SYSFS " pairs");
643                         if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
644                                 dbg(FIELD_SYSFS " is not matching");
645                                 goto try_parent;
646                         } else {
647                                 dbg(FIELD_SYSFS " matches");
648                         }
649                 }
650
651                 /* execute external program */
652                 if (dev->program[0] != '\0') {
653                         char program[PROGRAM_SIZE];
654
655                         dbg("check " FIELD_PROGRAM);
656                         strfieldcpy(program, dev->program);
657                         apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
658                         if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
659                                 dbg(FIELD_PROGRAM " returned nonzero");
660                                 goto try_parent;
661                         } else {
662                                 dbg(FIELD_PROGRAM " returned successful");
663                         }
664                 }
665
666                 /* check for matching result of external program */
667                 if (dev->result[0] != '\0') {
668                         dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
669                             dev->result, udev->program_result);
670                         if (strcmp_pattern(dev->result, udev->program_result) != 0) {
671                                 dbg(FIELD_RESULT " is not matching");
672                                 goto try_parent;
673                         } else {
674                                 dbg(FIELD_RESULT " matches");
675                         }
676                 }
677
678                 /* Yeah, we matched! */
679                 return 0;
680
681 try_parent:
682                 dbg("try parent sysfs device");
683                 sysfs_device = sysfs_get_device_parent(sysfs_device);
684                 if (sysfs_device == NULL)
685                         return -ENODEV;
686                 dbg("sysfs_device->path='%s'", sysfs_device->path);
687                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
688         }
689
690 }
691
692 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
693 {
694         struct sysfs_class_device *class_dev_parent;
695         struct sysfs_device *sysfs_device = NULL;
696         struct config_device *dev;
697         struct perm_device *perm;
698         char *pos;
699
700         udev->mode = 0;
701         dbg("class_dev->name='%s'", class_dev->name);
702
703         /* Figure out where the "device"-symlink is at.  For char devices this will
704          * always be in the class_dev->path.  On block devices, only the main block
705          * device will have the device symlink in it's path. All partition devices
706          * need to look at the symlink in its parent directory.
707          */
708         class_dev_parent = sysfs_get_classdev_parent(class_dev);
709         if (class_dev_parent != NULL) {
710                 dbg("given class device has a parent, use this instead");
711                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
712         } else {
713                 sysfs_device = sysfs_get_classdev_device(class_dev);
714         }
715
716         if (sysfs_device) {
717                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
718                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
719                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
720         }
721
722         strfieldcpy(udev->kernel_name, class_dev->name);
723         fix_kernel_name(udev);
724         dbg("udev->kernel_name = '%s'", udev->kernel_name);
725
726         /* get kernel number */
727         pos = class_dev->name + strlen(class_dev->name);
728         while (isdigit(*(pos-1)))
729                 pos--;
730         strfieldcpy(udev->kernel_number, pos);
731         dbg("kernel_number='%s'", udev->kernel_number);
732
733         /* look for a matching rule to apply */
734         list_for_each_entry(dev, &config_device_list, node) {
735                 dbg("process rule");
736                 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
737                         /* empty name and symlink will not create any node */
738                         if (dev->name[0] == '\0' && dev->symlink[0] == '\0') {
739                                 info("configured rule in '%s' at line %i applied, '%s' is ignored",
740                                      dev->config_file, dev->config_line, udev->kernel_name);
741                                 return -1;
742                         }
743
744                         /* collect symlinks for the final matching rule */
745                         if (dev->symlink[0] != '\0') {
746                                 char temp[NAME_SIZE];
747
748                                 info("configured rule in '%s' at line %i applied, added symlink '%s'",
749                                      dev->config_file, dev->config_line, dev->symlink);
750                                 strfieldcpy(temp, dev->symlink);
751                                 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
752                                 if (udev->symlink[0] != '\0')
753                                         strfieldcat(udev->symlink, " ");
754                                 strfieldcat(udev->symlink, temp);
755                         }
756
757                         /* rule matches */
758                         if (dev->name[0] != '\0') {
759                                 /* apply all_partitions flag only at a main block device */
760                                 if (dev->partitions > 0 &&
761                                     (udev->type != 'b' || udev->kernel_number[0] != '\0'))
762                                         continue;
763
764                                 info("configured rule in '%s' at line %i applied, '%s' becomes '%s'",
765                                      dev->config_file, dev->config_line, udev->kernel_name, dev->name);
766
767                                 strfieldcpy(udev->name, dev->name);
768                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
769                                 strfieldcpy(udev->config_file, dev->config_file);
770                                 udev->config_line = dev->config_line;
771                                 udev->ignore_remove = dev->ignore_remove;
772
773                                 if (udev->type == 'n')
774                                         goto exit;
775
776                                 udev->partitions = dev->partitions;
777                                 udev->mode = dev->mode;
778                                 strfieldcpy(udev->owner, dev->owner);
779                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
780                                 strfieldcpy(udev->group, dev->group);
781                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
782
783                                 goto perms;
784                         }
785                 }
786         }
787
788         /* no rule matched, so we use the kernel name */
789         strfieldcpy(udev->name, udev->kernel_name);
790
791         if (udev->type == 'n')
792                 goto exit;
793
794 perms:
795         /* apply permissions from permissions file to empty fields */
796         perm = find_perm_entry(udev->name);
797         if (perm != NULL) {
798                 if (udev->mode == 0000)
799                         udev->mode = perm->mode;
800                 if (udev->owner[0] == '\0')
801                         strfieldcpy(udev->owner, perm->owner);
802                 if (udev->group[0] == '\0')
803                         strfieldcpy(udev->group, perm->group);
804         }
805
806         /* apply permissions from config to empty fields */
807         if (udev->mode == 0000)
808                 udev->mode = default_mode;
809         if (udev->owner[0] == '\0')
810                 strfieldcpy(udev->owner, default_owner);
811         if (udev->group[0] == '\0')
812                 strfieldcpy(udev->group, default_group);
813
814         dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
815             udev->name, udev->owner, udev->group, udev->mode);
816
817 exit:
818         return 0;
819 }
820
821 int namedev_init(void)
822 {
823         int retval;
824
825         retval = namedev_init_rules();
826         if (retval)
827                 return retval;
828
829         retval = namedev_init_permissions();
830         if (retval)
831                 return retval;
832
833         dump_config_dev_list();
834         dump_perm_dev_list();
835         return retval;
836 }