chiark / gitweb /
[PATCH] fix udevtest to print the error if logging is disabled
[elogind.git] / udev_rules.c
1 /*
2  * udev_rules.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
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "list.h"
37 #include "udev_libc_wrapper.h"
38 #include "udev.h"
39 #include "udev_utils.h"
40 #include "udev_version.h"
41 #include "logging.h"
42 #include "udev_rules.h"
43 #include "udev_db.h"
44
45
46 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
47 static int strcmp_pattern(const char *p, const char *s)
48 {
49         if (s[0] == '\0') {
50                 while (p[0] == '*')
51                         p++;
52                 return (p[0] != '\0');
53         }
54         switch (p[0]) {
55         case '[':
56                 {
57                         int not = 0;
58                         p++;
59                         if (p[0] == '!') {
60                                 not = 1;
61                                 p++;
62                         }
63                         while ((p[0] != '\0') && (p[0] != ']')) {
64                                 int match = 0;
65                                 if (p[1] == '-') {
66                                         if ((s[0] >= p[0]) && (s[0] <= p[2]))
67                                                 match = 1;
68                                         p += 3;
69                                 } else {
70                                         match = (p[0] == s[0]);
71                                         p++;
72                                 }
73                                 if (match ^ not) {
74                                         while ((p[0] != '\0') && (p[0] != ']'))
75                                                 p++;
76                                         if (p[0] == ']')
77                                                 return strcmp_pattern(p+1, s+1);
78                                 }
79                         }
80                 }
81                 break;
82         case '*':
83                 if (strcmp_pattern(p, s+1))
84                         return strcmp_pattern(p+1, s);
85                 return 0;
86         case '\0':
87                 if (s[0] == '\0') {
88                         return 0;
89                 }
90                 break;
91         default:
92                 if ((p[0] == s[0]) || (p[0] == '?'))
93                         return strcmp_pattern(p+1, s+1);
94                 break;
95         }
96         return 1;
97 }
98
99 /* extract possible {attr} and move str behind it */
100 static char *get_format_attribute(char **str)
101 {
102         char *pos;
103         char *attr = NULL;
104
105         if (*str[0] == '{') {
106                 pos = strchr(*str, '}');
107                 if (pos == NULL) {
108                         err("missing closing brace for format");
109                         return NULL;
110                 }
111                 pos[0] = '\0';
112                 attr = *str+1;
113                 *str = pos+1;
114                 dbg("attribute='%s', str='%s'", attr, *str);
115         }
116         return attr;
117 }
118
119 /* extract possible format length and move str behind it*/
120 static int get_format_len(char **str)
121 {
122         int num;
123         char *tail;
124
125         if (isdigit(*str[0])) {
126                 num = (int) strtoul(*str, &tail, 10);
127                 if (num > 0) {
128                         *str = tail;
129                         dbg("format length=%i", num);
130                         return num;
131                 } else {
132                         err("format parsing error '%s'", *str);
133                 }
134         }
135         return -1;
136 }
137
138 /** Finds the lowest positive N such that <name>N isn't present in 
139  *  $(udevroot) either as a file or a symlink.
140  *
141  *  @param  name                Name to check for
142  *  @return                     0 if <name> didn't exist and N otherwise.
143  */
144 static int find_free_number(struct udevice *udev, const char *name)
145 {
146         char devpath[PATH_SIZE];
147         char filename[PATH_SIZE];
148         int num = 0;
149
150         strlcpy(filename, name, sizeof(filename));
151         while (1) {
152                 dbg("look for existing node '%s'", filename);
153                 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
154                         dbg("free num=%d", num);
155                         return num;
156                 }
157
158                 num++;
159                 if (num > 1000) {
160                         info("find_free_number gone crazy (num=%d), aborted", num);
161                         return -1;
162                 }
163                 snprintf(filename, sizeof(filename), "%s%d", name, num);
164                 filename[sizeof(filename)-1] = '\0';
165         }
166 }
167
168 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
169                                 const char *name, char *value, size_t len)
170 {
171         struct sysfs_attribute *tmpattr;
172
173         dbg("look for device attribute '%s'", name);
174         if (class_dev) {
175                 tmpattr = sysfs_get_classdev_attr(class_dev, name);
176                 if (tmpattr)
177                         goto attr_found;
178         }
179         if (sysfs_device) {
180                 tmpattr = sysfs_get_device_attr(sysfs_device, name);
181                 if (tmpattr)
182                         goto attr_found;
183         }
184
185         return -1;
186
187 attr_found:
188         strlcpy(value, tmpattr->value, len);
189         remove_trailing_char(value, '\n');
190
191         dbg("found attribute '%s'", tmpattr->path);
192         return 0;
193 }
194
195 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
196                          struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
197 {
198         char temp[PATH_SIZE];
199         char temp2[PATH_SIZE];
200         char *tail, *pos, *cpos, *attr, *rest;
201         int len;
202         int i;
203         char c;
204         unsigned int next_free_number;
205         struct sysfs_class_device *class_dev_parent;
206
207         pos = string;
208         while (1) {
209                 pos = strchr(pos, '%');
210                 if (pos == NULL)
211                         break;
212
213                 pos[0] = '\0';
214                 tail = pos+1;
215                 len = get_format_len(&tail);
216                 c = tail[0];
217                 strlcpy(temp, tail+1, sizeof(temp));
218                 tail = temp;
219                 dbg("format=%c, string='%s', tail='%s'",c , string, tail);
220                 attr = get_format_attribute(&tail);
221
222                 switch (c) {
223                 case 'p':
224                         strlcat(string, udev->devpath, maxsize);
225                         dbg("substitute kernel name '%s'", udev->kernel_name);
226                         break;
227                 case 'b':
228                         strlcat(string, udev->bus_id, maxsize);
229                         dbg("substitute bus_id '%s'", udev->bus_id);
230                         break;
231                 case 'k':
232                         strlcat(string, udev->kernel_name, maxsize);
233                         dbg("substitute kernel name '%s'", udev->kernel_name);
234                         break;
235                 case 'n':
236                         strlcat(string, udev->kernel_number, maxsize);
237                         dbg("substitute kernel number '%s'", udev->kernel_number);
238                                 break;
239                 case 'm':
240                         sprintf(temp2, "%d", minor(udev->devt));
241                         strlcat(string, temp2, maxsize);
242                         dbg("substitute minor number '%s'", temp2);
243                         break;
244                 case 'M':
245                         sprintf(temp2, "%d", major(udev->devt));
246                         strlcat(string, temp2, maxsize);
247                         dbg("substitute major number '%s'", temp2);
248                         break;
249                 case 'c':
250                         if (udev->program_result[0] == '\0')
251                                 break;
252                         /* get part part of the result string */
253                         i = 0;
254                         if (attr != NULL)
255                                 i = strtoul(attr, &rest, 10);
256                         if (i > 0) {
257                                 dbg("request part #%d of result string", i);
258                                 cpos = udev->program_result;
259                                 while (--i) {
260                                         while (cpos[0] != '\0' && !isspace(cpos[0]))
261                                                 cpos++;
262                                         while (isspace(cpos[0]))
263                                                 cpos++;
264                                 }
265                                 if (i > 0) {
266                                         err("requested part of result string not found");
267                                         break;
268                                 }
269                                 strlcpy(temp2, cpos, sizeof(temp2));
270                                 /* %{2+}c copies the whole string from the second part on */
271                                 if (rest[0] != '+') {
272                                         cpos = strchr(temp2, ' ');
273                                         if (cpos)
274                                                 cpos[0] = '\0';
275                                 }
276                                 strlcat(string, temp2, maxsize);
277                                 dbg("substitute part of result string '%s'", temp2);
278                         } else {
279                                 strlcat(string, udev->program_result, maxsize);
280                                 dbg("substitute result string '%s'", udev->program_result);
281                         }
282                         break;
283                 case 's':
284                         if (attr == NULL) {
285                                 dbg("missing attribute");
286                                 break;
287                         }
288                         if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
289                                 struct sysfs_device *parent_device;
290
291                                 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
292                                 parent_device = sysfs_get_device_parent(sysfs_device);
293                                 while (parent_device) {
294                                         dbg("looking at '%s'", parent_device->path);
295                                         if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
296                                                 break;
297                                         parent_device = sysfs_get_device_parent(parent_device);
298                                 }
299                                 if (!parent_device)
300                                         break;
301                         }
302                         /* strip trailing whitespace of sysfs value */
303                         i = strlen(temp2);
304                         while (i > 0 && isspace(temp2[i-1]))
305                                 temp2[--i] = '\0';
306                         replace_untrusted_chars(temp2);
307                         strlcat(string, temp2, maxsize);
308                         dbg("substitute sysfs value '%s'", temp2);
309                         break;
310                 case '%':
311                         strlcat(string, "%", maxsize);
312                         pos++;
313                         break;
314                 case 'e':
315                         next_free_number = find_free_number(udev, string);
316                         if (next_free_number > 0) {
317                                 sprintf(temp2, "%d", next_free_number);
318                                 strlcat(string, temp2, maxsize);
319                         }
320                         break;
321                 case 'P':
322                         if (!class_dev)
323                                 break;
324                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
325                         if (class_dev_parent != NULL) {
326                                 struct udevice udev_parent;
327
328                                 dbg("found parent '%s', get the node name", class_dev_parent->path);
329                                 udev_init_device(&udev_parent, NULL, NULL, NULL);
330                                 /* lookup the name in the udev_db with the DEVPATH of the parent */
331                                 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
332                                         strlcat(string, udev_parent.name, maxsize);
333                                         dbg("substitute parent node name'%s'", udev_parent.name);
334                                 } else
335                                         dbg("parent not found in database");
336                                 udev_cleanup_device(&udev_parent);
337                         }
338                         break;
339                 case 'N':
340                         if (udev->tmp_node[0] == '\0') {
341                                 dbg("create temporary device node for callout");
342                                 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
343                                          udev_root, major(udev->devt), minor(udev->devt));
344                                 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
345                                 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
346                         }
347                         strlcat(string, udev->tmp_node, maxsize);
348                         dbg("substitute temporary device node name '%s'", udev->tmp_node);
349                         break;
350                 case 'r':
351                         strlcat(string, udev_root, maxsize);
352                         dbg("substitute udev_root '%s'", udev_root);
353                         break;
354                 default:
355                         err("unknown substitution type '%%%c'", c);
356                         break;
357                 }
358                 /* truncate to specified length */
359                 if (len > 0)
360                         pos[len] = '\0';
361
362                 strlcat(string, tail, maxsize);
363         }
364 }
365
366 static int execute_program_pipe(const char *command, const char *subsystem, char *value, int len)
367 {
368         int retval;
369         int count;
370         int status;
371         int pipefds[2];
372         pid_t pid;
373         char *pos;
374         char arg[PATH_SIZE];
375         char *argv[(sizeof(arg) / 2) + 1];
376         int devnull;
377         int i;
378
379         strlcpy(arg, command, sizeof(arg));
380         i = 0;
381         if (strchr(arg, ' ')) {
382                 pos = arg;
383                 while (pos != NULL) {
384                         if (pos[0] == '\'') {
385                                 /* don't separate if in apostrophes */
386                                 pos++;
387                                 argv[i] = strsep(&pos, "\'");
388                                 while (pos && pos[0] == ' ')
389                                         pos++;
390                         } else {
391                                 argv[i] = strsep(&pos, " ");
392                         }
393                         dbg("arg[%i] '%s'", i, argv[i]);
394                         i++;
395                 }
396                 argv[i] =  NULL;
397                 dbg("execute '%s' with parsed arguments", arg);
398         } else {
399                 argv[0] = arg;
400                 argv[1] = (char *) subsystem;
401                 argv[2] = NULL;
402                 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
403         }
404
405         retval = pipe(pipefds);
406         if (retval != 0) {
407                 err("pipe failed");
408                 return -1;
409         }
410
411         pid = fork();
412         switch(pid) {
413         case 0:
414                 /* child dup2 write side of pipe to STDOUT */
415                 devnull = open("/dev/null", O_RDWR);
416                 if (devnull >= 0) {
417                         dup2(devnull, STDIN_FILENO);
418                         dup2(devnull, STDERR_FILENO);
419                         close(devnull);
420                 }
421                 dup2(pipefds[1], STDOUT_FILENO);
422                 retval = execv(arg, argv);
423                 err("exec of program failed");
424                 _exit(1);
425         case -1:
426                 err("fork of '%s' failed", arg);
427                 retval = -1;
428                 break;
429         default:
430                 /* parent reads from pipefds[0] */
431                 close(pipefds[1]);
432                 retval = 0;
433                 i = 0;
434                 while (1) {
435                         count = read(pipefds[0], value + i, len - i-1);
436                         if (count < 0) {
437                                 err("read failed with '%s'", strerror(errno));
438                                 retval = -1;
439                         }
440
441                         if (count == 0)
442                                 break;
443
444                         i += count;
445                         if (i >= len-1) {
446                                 err("result len %d too short", len);
447                                 retval = -1;
448                                 break;
449                         }
450                 }
451                 value[i] = '\0';
452
453                 close(pipefds[0]);
454                 waitpid(pid, &status, 0);
455
456                 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
457                         dbg("exec program status 0x%x", status);
458                         retval = -1;
459                 }
460         }
461
462         if (!retval) {
463                 remove_trailing_char(value, '\n');
464                 dbg("result is '%s'", value);
465                 replace_untrusted_chars(value);
466         } else
467                 value[0] = '\0';
468
469         return retval;
470 }
471
472 static int match_rule(struct udevice *udev, struct udev_rule *rule,
473                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
474 {
475         struct sysfs_device *parent_device = sysfs_device;
476
477         if (rule->kernel_operation != KEY_OP_UNSET) {
478                 dbg("check for " KEY_KERNEL " rule->kernel='%s' udev_kernel_name='%s'",
479                     rule->kernel, udev->kernel_name);
480                 if (strcmp_pattern(rule->kernel, udev->kernel_name) != 0) {
481                         dbg(KEY_KERNEL " is not matching");
482                         if (rule->kernel_operation != KEY_OP_NOMATCH)
483                                 goto exit;
484                 } else {
485                         dbg(KEY_KERNEL " matches");
486                         if (rule->kernel_operation == KEY_OP_NOMATCH)
487                                 goto exit;
488                 }
489                 dbg(KEY_KERNEL " key is true");
490         }
491
492         if (rule->subsystem_operation != KEY_OP_UNSET) {
493                 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' udev->subsystem='%s'",
494                     rule->subsystem, udev->subsystem);
495                 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
496                         dbg(KEY_SUBSYSTEM " is not matching");
497                         if (rule->subsystem_operation != KEY_OP_NOMATCH)
498                                 goto exit;
499                 } else {
500                         dbg(KEY_SUBSYSTEM " matches");
501                         if (rule->subsystem_operation == KEY_OP_NOMATCH)
502                                 goto exit;
503                 }
504                 dbg(KEY_SUBSYSTEM " key is true");
505         }
506
507         if (rule->env_pair_count) {
508                 int i;
509
510                 dbg("check for " KEY_ENV " pairs");
511                 for (i = 0; i < rule->env_pair_count; i++) {
512                         struct key_pair *pair;
513                         const char *value;
514
515                         pair = &rule->env_pair[i];
516                         value = getenv(pair->name);
517                         if (!value) {
518                                 dbg(KEY_ENV "{'%s'} is not found", pair->name);
519                                 goto exit;
520                         }
521                         if (strcmp_pattern(pair->value, value) != 0) {
522                                 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
523                                 if (pair->operation != KEY_OP_NOMATCH)
524                                         goto exit;
525                         } else {
526                                 dbg(KEY_ENV "{'%s'} matches", pair->name);
527                                 if (pair->operation == KEY_OP_NOMATCH)
528                                         goto exit;
529                         }
530                 }
531                 dbg(KEY_ENV " key is true");
532         }
533
534         /* walk up the chain of physical devices and find a match */
535         while (1) {
536                 /* check for matching driver */
537                 if (rule->driver_operation != KEY_OP_UNSET) {
538                         if (parent_device == NULL) {
539                                 dbg("device has no sysfs_device");
540                                 goto exit;
541                         }
542                         dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
543                             rule->driver, parent_device->driver_name);
544                         if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
545                                 dbg(KEY_DRIVER " is not matching");
546                                 if (rule->driver_operation != KEY_OP_NOMATCH)
547                                         goto try_parent;
548                         } else {
549                                 dbg(KEY_DRIVER " matches");
550                                 if (rule->driver_operation == KEY_OP_NOMATCH)
551                                         goto try_parent;
552                         }
553                         dbg(KEY_DRIVER " key is true");
554                 }
555
556                 /* check for matching bus value */
557                 if (rule->bus_operation != KEY_OP_UNSET) {
558                         if (parent_device == NULL) {
559                                 dbg("device has no sysfs_device");
560                                 goto exit;
561                         }
562                         dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
563                             rule->bus, parent_device->bus);
564                         if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
565                                 dbg(KEY_BUS " is not matching");
566                                 if (rule->bus_operation != KEY_OP_NOMATCH)
567                                         goto try_parent;
568                         } else {
569                                 dbg(KEY_BUS " matches");
570                                 if (rule->bus_operation == KEY_OP_NOMATCH)
571                                         goto try_parent;
572                         }
573                         dbg(KEY_BUS " key is true");
574                 }
575
576                 /* check for matching bus id */
577                 if (rule->id_operation != KEY_OP_UNSET) {
578                         if (parent_device == NULL) {
579                                 dbg("device has no sysfs_device");
580                                 goto exit;
581                         }
582                         dbg("check " KEY_ID);
583                         if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
584                                 dbg(KEY_ID " is not matching");
585                                 if (rule->id_operation != KEY_OP_NOMATCH)
586                                         goto try_parent;
587                         } else {
588                                 dbg(KEY_ID " matches");
589                                 if (rule->id_operation == KEY_OP_NOMATCH)
590                                         goto try_parent;
591                         }
592                         dbg(KEY_ID " key is true");
593                 }
594
595                 /* check for matching sysfs pairs */
596                 if (rule->sysfs_pair_count) {
597                         int i;
598
599                         dbg("check " KEY_SYSFS " pairs");
600                         for (i = 0; i < rule->sysfs_pair_count; i++) {
601                                 struct key_pair *pair;
602                                 char value[VALUE_SIZE];
603                                 size_t len;
604
605                                 pair = &rule->sysfs_pair[i];
606                                 if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
607                                         goto try_parent;
608
609                                 /* strip trailing whitespace of value, if not asked to match for it */
610                                 len = strlen(pair->value);
611                                 if (len && !isspace(pair->value[len-1])) {
612                                         len = strlen(value);
613                                         while (len > 0 && isspace(value[len-1]))
614                                                 value[--len] = '\0';
615                                         dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
616                                 }
617
618                                 dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
619                                 if (strcmp_pattern(pair->value, value) != 0) {
620                                         dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
621                                         if (pair->operation != KEY_OP_NOMATCH)
622                                                 goto try_parent;
623                                 } else {
624                                         dbg(KEY_SYSFS "{'%s'} matches", pair->name);
625                                         if (pair->operation == KEY_OP_NOMATCH)
626                                                 goto try_parent;
627                                 }
628                         }
629                         dbg(KEY_SYSFS " keys are true");
630                 }
631
632                 /* found matching physical device  */
633                 break;
634 try_parent:
635                 dbg("try parent sysfs device");
636                 parent_device = sysfs_get_device_parent(parent_device);
637                 if (parent_device == NULL)
638                         goto exit;
639                 dbg("look at sysfs_device->path='%s'", parent_device->path);
640                 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
641         }
642
643         /* execute external program */
644         if (rule->program_operation != KEY_OP_UNSET) {
645                 char program[PATH_SIZE];
646
647                 dbg("check " KEY_PROGRAM);
648                 strlcpy(program, rule->program, sizeof(program));
649                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
650                 if (execute_program_pipe(program, udev->subsystem,
651                                          udev->program_result, sizeof(udev->program_result)) != 0) {
652                         dbg(KEY_PROGRAM " returned nonzero");
653                         if (rule->program_operation != KEY_OP_NOMATCH)
654                                 goto exit;
655                 } else {
656                         dbg(KEY_PROGRAM " returned successful");
657                         if (rule->program_operation == KEY_OP_NOMATCH)
658                                 goto exit;
659                 }
660                 dbg(KEY_PROGRAM " key is true");
661         }
662
663         /* check for matching result of external program */
664         if (rule->result_operation != KEY_OP_UNSET) {
665                 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
666                    rule->result, udev->program_result);
667                 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
668                         dbg(KEY_RESULT " is not matching");
669                         if (rule->result_operation != KEY_OP_NOMATCH)
670                                 goto exit;
671                 } else {
672                         dbg(KEY_RESULT " matches");
673                         if (rule->result_operation == KEY_OP_NOMATCH)
674                                 goto exit;
675                 }
676                 dbg(KEY_RESULT " key is true");
677         }
678
679         /* rule matches */
680         return 0;
681
682 exit:
683         return -1;
684 }
685
686 int udev_rules_get_name(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 udev_rule *rule;
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                 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->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(rule, &udev_rule_list, node) {
717                 dbg("process rule");
718                 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
719
720                         /* apply options */
721                         if (rule->ignore_device) {
722                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
723                                      rule->config_file, rule->config_line, udev->kernel_name);
724                                 return -1;
725                         }
726                         if (rule->ignore_remove) {
727                                 udev->ignore_remove = 1;
728                                 dbg("remove event should be ignored");
729                         }
730                         /* apply all_partitions option only at a main block device */
731                         if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
732                                 udev->partitions = rule->partitions;
733                                 dbg("creation of partition nodes requested");
734                         }
735
736                         /* apply permissions */
737                         if (rule->mode != 0000) {
738                                 udev->mode = rule->mode;
739                                 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
740                         }
741                         if (rule->owner[0] != '\0') {
742                                 strlcpy(udev->owner, rule->owner, sizeof(udev->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 (rule->group[0] != '\0') {
747                                 strlcpy(udev->group, rule->group, sizeof(udev->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 (rule->symlink[0] != '\0') {
754                                 char temp[PATH_SIZE];
755                                 char *pos, *next;
756
757                                 info("configured rule in '%s[%i]' applied, added symlink '%s'",
758                                      rule->config_file, rule->config_line, rule->symlink);
759                                 strlcpy(temp, rule->symlink, sizeof(temp));
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                                         info("add symlink '%s'", pos);
768                                         name_list_add(&udev->symlink_list, pos, 0);
769                                         pos = &next[1];
770                                         next = strchr(pos, ' ');
771                                 }
772                                 info("add symlink '%s'", pos);
773                                 name_list_add(&udev->symlink_list, pos, 0);
774                         }
775
776                         /* rule matches */
777                         if (rule->name[0] != '\0') {
778                                 info("configured rule in '%s[%i]' applied, '%s' becomes '%s'",
779                                      rule->config_file, rule->config_line, udev->kernel_name, rule->name);
780
781                                 strlcpy(udev->name, rule->name, sizeof(udev->name));
782                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
783                                 strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
784                                 udev->config_line = rule->config_line;
785
786                                 if (udev->type != DEV_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                         if (rule->last_rule) {
794                                 dbg("last rule to be applied");
795                                 break;
796                         }
797
798                 }
799         }
800
801         if (udev->name[0] == '\0') {
802                 /* no rule matched, so we use the kernel name */
803                 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
804                 info("no rule found, use kernel name '%s'", udev->name);
805         }
806
807         if (udev->tmp_node[0] != '\0') {
808                 dbg("removing temporary device node");
809                 unlink_secure(udev->tmp_node);
810                 udev->tmp_node[0] = '\0';
811         }
812
813         return 0;
814 }