chiark / gitweb /
[PATCH] fix up segfaulting binaries with new klibc
[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 devpath[NAME_SIZE];
148         char filename[NAME_SIZE];
149         int num = 0;
150
151         strfieldcpy(filename, name);
152         while (1) {
153                 dbg("look for existing node '%s'", filename);
154                 if (udev_db_search_name(devpath, DEVPATH_SIZE, filename) != 0) {
155                         dbg("free num=%d", num);
156                         return num;
157                 }
158
159                 num++;
160                 if (num > 1000) {
161                         info("find_free_number gone crazy (num=%d), aborted", num);
162                         return -1;
163                 }
164                 snprintf(filename, NAME_SIZE, "%s%d", name, num);
165                 filename[NAME_SIZE-1] = '\0';
166         }
167 }
168
169 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
170                          struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
171 {
172         char temp[NAME_SIZE];
173         char temp2[NAME_SIZE];
174         char *tail, *pos, *cpos, *attr, *rest;
175         int len;
176         int i;
177         char c;
178         struct sysfs_attribute *tmpattr;
179         unsigned int next_free_number;
180         struct sysfs_class_device *class_dev_parent;
181
182         pos = string;
183         while (1) {
184                 pos = strchr(pos, '%');
185                 if (pos == NULL)
186                         break;
187
188                 pos[0] = '\0';
189                 tail = pos+1;
190                 len = get_format_len(&tail);
191                 c = tail[0];
192                 strfieldcpy(temp, tail+1);
193                 tail = temp;
194                 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
195                 attr = get_format_attribute(&tail);
196
197                 switch (c) {
198                 case 'p':
199                         strfieldcatmax(string, udev->devpath, maxsize);
200                         dbg("substitute kernel name '%s'", udev->kernel_name);
201                         break;
202                 case 'b':
203                         strfieldcatmax(string, udev->bus_id, maxsize);
204                         dbg("substitute bus_id '%s'", udev->bus_id);
205                         break;
206                 case 'k':
207                         strfieldcatmax(string, udev->kernel_name, maxsize);
208                         dbg("substitute kernel name '%s'", udev->kernel_name);
209                         break;
210                 case 'n':
211                         strfieldcatmax(string, udev->kernel_number, maxsize);
212                         dbg("substitute kernel number '%s'", udev->kernel_number);
213                                 break;
214                 case 'm':
215                         sprintf(temp2, "%d", minor(udev->devt));
216                         strfieldcatmax(string, temp2, maxsize);
217                         dbg("substitute minor number '%s'", temp2);
218                         break;
219                 case 'M':
220                         sprintf(temp2, "%d", major(udev->devt));
221                         strfieldcatmax(string, temp2, maxsize);
222                         dbg("substitute major number '%s'", temp2);
223                         break;
224                 case 'c':
225                         if (udev->program_result[0] == '\0')
226                                 break;
227                         /* get part part of the result string */
228                         i = 0;
229                         if (attr != NULL)
230                                 i = strtoul(attr, &rest, 10);
231                         if (i > 0) {
232                                 dbg("request part #%d of result string", i);
233                                 cpos = udev->program_result;
234                                 while (--i) {
235                                         while (cpos[0] != '\0' && !isspace(cpos[0]))
236                                                 cpos++;
237                                         while (isspace(cpos[0]))
238                                                 cpos++;
239                                 }
240                                 if (i > 0) {
241                                         dbg("requested part of result string not found");
242                                         break;
243                                 }
244                                 strfieldcpy(temp2, cpos);
245                                 /* %{2+}c copies the whole string from the second part on */
246                                 if (rest[0] != '+') {
247                                         cpos = strchr(temp2, ' ');
248                                         if (cpos)
249                                                 cpos[0] = '\0';
250                                 }
251                                 strfieldcatmax(string, temp2, maxsize);
252                                 dbg("substitute part of result string '%s'", temp2);
253                         } else {
254                                 strfieldcatmax(string, udev->program_result, maxsize);
255                                 dbg("substitute result string '%s'", udev->program_result);
256                         }
257                         break;
258                 case 's':
259                         if (!class_dev)
260                                 break;
261                         if (attr == NULL) {
262                                 dbg("missing attribute");
263                                 break;
264                         }
265                         tmpattr = find_sysfs_attribute(class_dev, sysfs_device, attr);
266                         if (tmpattr == NULL) {
267                                 dbg("sysfa attribute '%s' not found", attr);
268                                 break;
269                         }
270                         /* strip trailing whitespace of matching value */
271                         if (isspace(tmpattr->value[strlen(tmpattr->value)-1])) {
272                                 i = len = strlen(tmpattr->value);
273                                 while (i > 0 &&  isspace(tmpattr->value[i-1]))
274                                         i--;
275                                 if (i < len) {
276                                         tmpattr->value[i] = '\0';
277                                         dbg("remove %i trailing whitespace chars from '%s'",
278                                                  len - i, tmpattr->value);
279                                 }
280                         }
281                         strfieldcatmax(string, tmpattr->value, maxsize);
282                         dbg("substitute sysfs value '%s'", tmpattr->value);
283                         break;
284                 case '%':
285                         strfieldcatmax(string, "%", maxsize);
286                         pos++;
287                         break;
288                 case 'e':
289                         next_free_number = find_free_number(udev, string);
290                         if (next_free_number > 0) {
291                                 sprintf(temp2, "%d", next_free_number);
292                                 strfieldcatmax(string, temp2, maxsize);
293                         }
294                         break;
295                 case 'P':
296                         if (!class_dev)
297                                 break;
298                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
299                         if (class_dev_parent != NULL) {
300                                 struct udevice udev_parent;
301
302                                 dbg("found parent '%s', get the node name", class_dev_parent->path);
303                                 udev_init_device(&udev_parent, NULL, NULL);
304                                 /* lookup the name in the udev_db with the DEVPATH of the parent */
305                                 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
306                                         strfieldcatmax(string, udev_parent.name, maxsize);
307                                         dbg("substitute parent node name'%s'", udev_parent.name);
308                                 } else
309                                         dbg("parent not found in database");
310                                 udev_cleanup_device(&udev_parent);
311                         }
312                         break;
313                 case 'N':
314                         if (udev->tmp_node[0] == '\0') {
315                                 dbg("create temporary device node for callout");
316                                 snprintf(udev->tmp_node, NAME_SIZE, "%s/.tmp-%u-%u", udev_root, major(udev->devt), minor(udev->devt));
317                                 udev->tmp_node[NAME_SIZE] = '\0';
318                                 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
319                         }
320                         strfieldcatmax(string, udev->tmp_node, maxsize);
321                         dbg("substitute temporary device node name '%s'", udev->tmp_node);
322                         break;
323                 case 'r':
324                         strfieldcatmax(string, udev_root, maxsize);
325                         dbg("substitute udev_root '%s'", udev_root);
326                         break;
327                 default:
328                         dbg("unknown substitution type '%%%c'", c);
329                         break;
330                 }
331                 /* truncate to specified length */
332                 if (len > 0)
333                         pos[len] = '\0';
334
335                 strfieldcatmax(string, tail, maxsize);
336         }
337 }
338
339 static int execute_program(struct udevice *udev, const char *path, char *value, int len)
340 {
341         int retval;
342         int count;
343         int status;
344         int fds[2];
345         pid_t pid;
346         char *pos;
347         char arg[PROGRAM_SIZE];
348         char *argv[(PROGRAM_SIZE / 2) + 1];
349         int i;
350
351         strfieldcpy(arg, path);
352         i = 0;
353         if (strchr(path, ' ')) {
354                 pos = arg;
355                 while (pos != NULL) {
356                         if (pos[0] == '\'') {
357                                 /* don't separate if in apostrophes */
358                                 pos++;
359                                 argv[i] = strsep(&pos, "\'");
360                                 while (pos && pos[0] == ' ')
361                                         pos++;
362                         } else {
363                                 argv[i] = strsep(&pos, " ");
364                         }
365                         dbg("arg[%i] '%s'", i, argv[i]);
366                         i++;
367                 }
368                 argv[i] =  NULL;
369                 dbg("execute '%s' with parsed arguments", arg);
370         } else {
371                 argv[0] = arg;
372                 argv[1] = udev->subsystem;
373                 argv[2] = NULL;
374                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
375         }
376
377         retval = pipe(fds);
378         if (retval != 0) {
379                 dbg("pipe failed");
380                 return -1;
381         }
382
383         pid = fork();
384         switch(pid) {
385         case 0:
386                 /* child */
387                 /* dup2 write side of pipe to STDOUT */
388                 dup2(fds[1], STDOUT_FILENO);
389                 retval = execv(arg, argv);
390
391                 info(FIELD_PROGRAM " execution of '%s' failed", path);
392                 exit(1);
393         case -1:
394                 dbg("fork failed");
395                 return -1;
396         default:
397                 /* parent reads from fds[0] */
398                 close(fds[1]);
399                 retval = 0;
400                 i = 0;
401                 while (1) {
402                         count = read(fds[0], value + i, len - i-1);
403                         if (count <= 0)
404                                 break;
405
406                         i += count;
407                         if (i >= len-1) {
408                                 dbg("result len %d too short", len);
409                                 retval = -1;
410                                 break;
411                         }
412                 }
413
414                 if (count < 0) {
415                         dbg("read failed with '%s'", strerror(errno));
416                         retval = -1;
417                 }
418
419                 if (i > 0 && value[i-1] == '\n')
420                         i--;
421                 value[i] = '\0';
422                 dbg("result is '%s'", value);
423
424                 close(fds[0]);
425                 waitpid(pid, &status, 0);
426
427                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
428                         dbg("exec program status 0x%x", status);
429                         retval = -1;
430                 }
431         }
432         return retval;
433 }
434
435 static struct sysfs_attribute *find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, char *attr)
436 {
437         struct sysfs_attribute *tmpattr = NULL;
438         char *c;
439
440         dbg("look for device attribute '%s'", attr);
441         /* try to find the attribute in the class device directory */
442         tmpattr = sysfs_get_classdev_attr(class_dev, attr);
443         if (tmpattr)
444                 goto attr_found;
445
446         /* look in the class device directory if present */
447         if (sysfs_device) {
448                 tmpattr = sysfs_get_device_attr(sysfs_device, attr);
449                 if (tmpattr)
450                         goto attr_found;
451         }
452
453         return NULL;
454
455 attr_found:
456         c = strchr(tmpattr->value, '\n');
457         if (c != NULL)
458                 c[0] = '\0';
459
460         dbg("found attribute '%s'", tmpattr->path);
461         return tmpattr;
462 }
463
464 static int compare_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair)
465 {
466         struct sysfs_attribute *tmpattr;
467         int i;
468         int len;
469
470         if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0'))
471                 return -ENODEV;
472
473         tmpattr = find_sysfs_attribute(class_dev, sysfs_device, pair->file);
474         if (tmpattr == NULL)
475                 return -ENODEV;
476
477         /* strip trailing whitespace of value, if not asked to match for it */
478         if (! isspace(pair->value[strlen(pair->value)-1])) {
479                 i = len = strlen(tmpattr->value);
480                 while (i > 0 &&  isspace(tmpattr->value[i-1]))
481                         i--;
482                 if (i < len) {
483                         tmpattr->value[i] = '\0';
484                         dbg("remove %i trailing whitespace chars from '%s'",
485                             len - i, tmpattr->value);
486                 }
487         }
488
489         dbg("compare attribute '%s' value '%s' with '%s'",
490                   pair->file, tmpattr->value, pair->value);
491         if (strcmp_pattern(pair->value, tmpattr->value) != 0)
492                 return -ENODEV;
493
494         dbg("found matching attribute '%s' with value '%s'",
495             pair->file, pair->value);
496         return 0;
497 }
498
499 static int match_sysfs_pairs(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
500 {
501         struct sysfs_pair *pair;
502         int i;
503
504         for (i = 0; i < MAX_SYSFS_PAIRS; ++i) {
505                 pair = &dev->sysfs_pair[i];
506                 if ((pair->file[0] == '\0') || (pair->value[0] == '\0'))
507                         break;
508                 if (compare_sysfs_attribute(class_dev, sysfs_device, pair) != 0) {
509                         dbg("sysfs attribute doesn't match");
510                         return -ENODEV;
511                 }
512         }
513
514         return 0;
515 }
516
517 static int match_id(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
518 {
519         char path[SYSFS_PATH_MAX];
520         char *temp;
521
522         /* we have to have a sysfs device for ID to work */
523         if (!sysfs_device)
524                 return -ENODEV;
525
526         strfieldcpy(path, sysfs_device->path);
527         temp = strrchr(path, '/');
528         temp++;
529         dbg("search '%s' in '%s', path='%s'", dev->id, temp, path);
530         if (strcmp_pattern(dev->id, temp) != 0)
531                 return -ENODEV;
532
533         return 0;
534 }
535
536 static int match_place(struct config_device *dev, struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
537 {
538         char path[SYSFS_PATH_MAX];
539         char *temp;
540
541         /* we have to have a sysfs device for PLACE to work */
542         if (!sysfs_device)
543                 return -ENODEV;
544
545         strfieldcpy(path, sysfs_device->path);
546         temp = strrchr(path, '/');
547         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
548         if (strstr(temp, dev->place) != NULL)
549                 return 0;
550
551         /* try the parent */
552         temp[0] = '\0';
553         temp = strrchr(path, '/');
554         dbg("search '%s' in '%s', path='%s'", dev->place, temp, path);
555         if (strstr(temp, dev->place) == NULL)
556                 return 0;
557
558         dbg("place doesn't match");
559         return -ENODEV;
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         if (dev->kernel[0] != '\0') {
566                 dbg("check for " FIELD_KERNEL " dev->kernel='%s' class_dev->name='%s'",
567                     dev->kernel, class_dev->name);
568                 if (strcmp_pattern(dev->kernel, class_dev->name) != 0) {
569                         dbg(FIELD_KERNEL " is not matching");
570                         goto exit;
571                 }
572                 dbg(FIELD_KERNEL " matches");
573         }
574
575         if (dev->subsystem[0] != '\0') {
576                 dbg("check for " FIELD_SUBSYSTEM " dev->subsystem='%s' class_dev->name='%s'",
577                     dev->subsystem, class_dev->name);
578                 if (strcmp_pattern(dev->subsystem, udev->subsystem) != 0) {
579                         dbg(FIELD_SUBSYSTEM " is not matching");
580                         goto exit;
581                 }
582                 dbg(FIELD_SUBSYSTEM " matches");
583         }
584
585         /* walk up the chain of physical devices and find a match */
586         while (1) {
587                 /* check for matching driver */
588                 if (dev->driver[0] != '\0') {
589                         dbg("check for " FIELD_DRIVER " dev->driver='%s' sysfs_device->driver_name='%s'",
590                             dev->driver, sysfs_device->driver_name);
591                         if (strcmp_pattern(dev->driver, sysfs_device->driver_name) != 0) {
592                                 dbg(FIELD_DRIVER " is not matching");
593                                 goto try_parent;
594                         }
595                         dbg(FIELD_DRIVER " matches");
596                 }
597
598                 /* check for matching bus value */
599                 if (dev->bus[0] != '\0') {
600                         if (sysfs_device == NULL) {
601                                 dbg("device has no bus");
602                                 goto try_parent;
603                         }
604                         dbg("check for " FIELD_BUS " dev->bus='%s' sysfs_device->bus='%s'",
605                             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                         }
610                         dbg(FIELD_BUS " matches");
611                 }
612
613                 /* check for matching bus id */
614                 if (dev->id[0] != '\0') {
615                         dbg("check " FIELD_ID);
616                         if (match_id(dev, class_dev, sysfs_device) != 0) {
617                                 dbg(FIELD_ID " is not matching");
618                                 goto try_parent;
619                         }
620                         dbg(FIELD_ID " matches");
621                 }
622
623                 /* check for matching place of device */
624                 if (dev->place[0] != '\0') {
625                         dbg("check " FIELD_PLACE);
626                         if (match_place(dev, class_dev, sysfs_device) != 0) {
627                                 dbg(FIELD_PLACE " is not matching");
628                                 goto try_parent;
629                         }
630                         dbg(FIELD_PLACE " matches");
631                 }
632
633                 /* check for matching sysfs pairs */
634                 if (dev->sysfs_pair[0].file[0] != '\0') {
635                         dbg("check " FIELD_SYSFS " pairs");
636                         if (match_sysfs_pairs(dev, class_dev, sysfs_device) != 0) {
637                                 dbg(FIELD_SYSFS " is not matching");
638                                 goto try_parent;
639                         }
640                         dbg(FIELD_SYSFS " matches");
641                 }
642
643                 /* found matching physical device  */
644                 break;
645 try_parent:
646                 dbg("try parent sysfs device");
647                 sysfs_device = sysfs_get_device_parent(sysfs_device);
648                 if (sysfs_device == NULL)
649                         goto exit;
650                 dbg("sysfs_device->path='%s'", sysfs_device->path);
651                 dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id);
652         }
653
654         /* execute external program */
655         if (dev->program[0] != '\0') {
656                 char program[PROGRAM_SIZE];
657
658                 dbg("check " FIELD_PROGRAM);
659                 strfieldcpy(program, dev->program);
660                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
661                 if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) {
662                         dbg(FIELD_PROGRAM " returned nonzero");
663                         goto try_parent;
664                 }
665                 dbg(FIELD_PROGRAM " returned successful");
666         }
667
668         /* check for matching result of external program */
669         if (dev->result[0] != '\0') {
670                 dbg("check for " FIELD_RESULT " dev->result='%s', udev->program_result='%s'",
671                     dev->result, udev->program_result);
672                 if (strcmp_pattern(dev->result, udev->program_result) != 0) {
673                         dbg(FIELD_RESULT " is not matching");
674                         goto try_parent;
675                 }
676                 dbg(FIELD_RESULT " matches");
677         }
678
679         /* rule matches */
680         return 0;
681
682 exit:
683         return -1;
684 }
685
686 int namedev_name_device(struct udevice *udev, struct sysfs_class_device *class_dev)
687 {
688         struct sysfs_class_device *class_dev_parent;
689         struct sysfs_device *sysfs_device = NULL;
690         struct config_device *dev;
691
692         dbg("class_dev->name='%s'", class_dev->name);
693
694         /* Figure out where the "device"-symlink is at.  For char devices this will
695          * always be in the class_dev->path.  On block devices, only the main block
696          * device will have the device symlink in it's path. All partition devices
697          * need to look at the symlink in its parent directory.
698          */
699         class_dev_parent = sysfs_get_classdev_parent(class_dev);
700         if (class_dev_parent != NULL) {
701                 dbg("given class device has a parent, use this instead");
702                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
703         } else {
704                 sysfs_device = sysfs_get_classdev_device(class_dev);
705         }
706
707         if (sysfs_device) {
708                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
709                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
710                 strfieldcpy(udev->bus_id, sysfs_device->bus_id);
711         }
712
713         dbg("udev->kernel_name='%s'", udev->kernel_name);
714
715         /* look for a matching rule to apply */
716         list_for_each_entry(dev, &config_device_list, node) {
717                 dbg("process rule");
718                 if (match_rule(udev, dev, class_dev, sysfs_device) == 0) {
719
720                         /* apply options */
721                         if (dev->ignore_device) {
722                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
723                                      dev->config_file, dev->config_line, udev->kernel_name);
724                                 return -1;
725                         }
726                         if (dev->ignore_remove) {
727                                 udev->ignore_remove = 1;
728                                 dbg_parse("remove event should be ignored");
729                         }
730                         /* apply all_partitions option only at a main block device */
731                         if (dev->partitions && udev->type == BLOCK && udev->kernel_number[0] == '\0') {
732                                 udev->partitions = dev->partitions;
733                                 dbg("creation of partition nodes requested");
734                         }
735
736                         /* apply permissions */
737                         if (dev->mode != 0000) {
738                                 udev->mode = dev->mode;
739                                 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
740                         }
741                         if (dev->owner[0] != '\0') {
742                                 strfieldcpy(udev->owner, dev->owner);
743                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
744                                 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
745                         }
746                         if (dev->group[0] != '\0') {
747                                 strfieldcpy(udev->group, dev->group);
748                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
749                                 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
750                         }
751
752                         /* collect symlinks */
753                         if (dev->symlink[0] != '\0') {
754                                 char temp[NAME_SIZE];
755                                 char *pos, *next;
756
757                                 info("configured rule in '%s[%i]' applied, added symlink '%s'",
758                                      dev->config_file, dev->config_line, dev->symlink);
759                                 strfieldcpy(temp, dev->symlink);
760                                 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
761
762                                 /* add multiple symlinks separated by spaces */
763                                 pos = temp;
764                                 next = strchr(temp, ' ');
765                                 while (next) {
766                                         next[0] = '\0';
767                                         dbg("add symlink '%s'", pos);
768                                         name_list_add(&udev->symlink_list, pos, 0);
769                                         pos = &next[1];
770                                         next = strchr(pos, ' ');
771                                 }
772                                 dbg("add symlink '%s'", pos);
773                                 name_list_add(&udev->symlink_list, pos, 0);
774                         }
775
776                         /* rule matches */
777                         if (dev->name[0] != '\0') {
778                                 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
779                                      dev->config_file, dev->config_line, udev->kernel_name, dev->name);
780
781                                 strfieldcpy(udev->name, dev->name);
782                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
783                                 strfieldcpy(udev->config_file, dev->config_file);
784                                 udev->config_line = dev->config_line;
785
786                                 if (udev->type != NET)
787                                         dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
788                                             udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
789
790                                 break;
791                         }
792                 }
793         }
794
795         if (udev->name[0] == '\0') {
796                 /* no rule matched, so we use the kernel name */
797                 strfieldcpy(udev->name, udev->kernel_name);
798                 dbg("no rule found, use kernel name '%s'", udev->name);
799         }
800
801         if (udev->tmp_node[0] != '\0') {
802                 dbg("removing temporary device node");
803                 unlink_secure(udev->tmp_node);
804                 udev->tmp_node[0] = '\0';
805         }
806
807         return 0;
808 }