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