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