chiark / gitweb /
unify execute_command() and execute_program()
[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 static int get_key(char **line, char **key, char **value)
139 {
140         char *linepos;
141         char *temp;
142
143         linepos = *line;
144         if (!linepos)
145                 return -1;
146
147         if (strchr(linepos, '\\')) {
148                 dbg("escaped characters are not supported, skip");
149                 return -1;
150         }
151
152         /* skip whitespace */
153         while (isspace(linepos[0]))
154                 linepos++;
155
156         /* get the key */
157         *key = linepos;
158         while (1) {
159                 linepos++;
160                 if (linepos[0] == '\0')
161                         return -1;
162                 if (isspace(linepos[0]))
163                         break;
164                 if (linepos[0] == '=')
165                         break;
166         }
167
168         /* terminate key */
169         linepos[0] = '\0';
170         linepos++;
171
172         /* skip whitespace */
173         while (isspace(linepos[0]))
174                 linepos++;
175
176         /* get the value*/
177         if (linepos[0] == '\0')
178                 return -1;
179
180         if (linepos[0] == '"') {
181                 linepos++;
182                 temp = strchr(linepos, '"');
183                 if (!temp)
184                         return -1;
185                 temp[0] = '\0';
186         } else if (linepos[0] == '\'') {
187                 linepos++;
188                 temp = strchr(linepos, '\'');
189                 if (!temp)
190                         return -1;
191                 temp[0] = '\0';
192         } else {
193                 temp = linepos;
194                 while (temp[0] && !isspace(temp[0]))
195                         temp++;
196                 temp[0] = '\0';
197         }
198         *value = linepos;
199
200         return 0;
201 }
202
203 static int import_keys_into_env(const char *buf, size_t bufsize)
204 {
205         char line[LINE_SIZE];
206         const char *bufline;
207         char *linepos;
208         char *variable;
209         char *value;
210         size_t cur;
211         size_t count;
212         int lineno;
213
214         /* loop through the whole file */
215         lineno = 0;
216         cur = 0;
217         while (cur < bufsize) {
218                 count = buf_get_line(buf, bufsize, cur);
219                 bufline = &buf[cur];
220                 cur += count+1;
221                 lineno++;
222
223                 if (count >= sizeof(line)) {
224                         err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
225                         continue;
226                 }
227
228                 /* eat the whitespace */
229                 while ((count > 0) && isspace(bufline[0])) {
230                         bufline++;
231                         count--;
232                 }
233                 if (count == 0)
234                         continue;
235
236                 /* see if this is a comment */
237                 if (bufline[0] == COMMENT_CHARACTER)
238                         continue;
239
240                 strlcpy(line, bufline, count+1);
241
242                 linepos = line;
243                 if (get_key(&linepos, &variable, &value) == 0) {
244                         dbg("import %s=%s", variable, value);
245                         setenv(variable, value, 0);
246                 }
247         }
248
249         return 0;
250 }
251
252 static int import_file_into_env(const char *filename)
253 {
254         char *buf;
255         size_t bufsize;
256
257         if (file_map(filename, &buf, &bufsize) != 0) {
258                 err("can't open '%s'", filename);
259                 return -1;
260         }
261         import_keys_into_env(buf, bufsize);
262         file_unmap(buf, bufsize);
263
264         return 0;
265 }
266
267 /** Finds the lowest positive N such that <name>N isn't present in 
268  *  $(udevroot) either as a file or a symlink.
269  *
270  *  @param  name                Name to check for
271  *  @return                     0 if <name> didn't exist and N otherwise.
272  */
273 static int find_free_number(struct udevice *udev, const char *name)
274 {
275         char devpath[PATH_SIZE];
276         char filename[PATH_SIZE];
277         int num = 0;
278
279         strlcpy(filename, name, sizeof(filename));
280         while (1) {
281                 dbg("look for existing node '%s'", filename);
282                 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
283                         dbg("free num=%d", num);
284                         return num;
285                 }
286
287                 num++;
288                 if (num > 1000) {
289                         info("find_free_number gone crazy (num=%d), aborted", num);
290                         return -1;
291                 }
292                 snprintf(filename, sizeof(filename), "%s%d", name, num);
293                 filename[sizeof(filename)-1] = '\0';
294         }
295 }
296
297 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
298                                 const char *name, char *value, size_t len)
299 {
300         struct sysfs_attribute *tmpattr;
301
302         dbg("look for device attribute '%s'", name);
303         if (class_dev) {
304                 dbg("look for class attribute '%s/%s'", class_dev->path, name);
305                 tmpattr = sysfs_get_classdev_attr(class_dev, name);
306                 if (tmpattr)
307                         goto attr_found;
308         }
309         if (sysfs_device) {
310                 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
311                 tmpattr = sysfs_get_device_attr(sysfs_device, name);
312                 if (tmpattr)
313                         goto attr_found;
314         }
315         return -1;
316
317 attr_found:
318         strlcpy(value, tmpattr->value, len);
319         remove_trailing_char(value, '\n');
320
321         dbg("found attribute '%s'", tmpattr->path);
322         return 0;
323 }
324
325 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
326                          struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
327 {
328         char temp[PATH_SIZE];
329         char temp2[PATH_SIZE];
330         char *head, *tail, *pos, *cpos, *attr, *rest;
331         int len;
332         int i;
333         unsigned int next_free_number;
334         struct sysfs_class_device *class_dev_parent;
335         enum subst_type {
336                 SUBST_UNKNOWN,
337                 SUBST_DEVPATH,
338                 SUBST_ID,
339                 SUBST_KERNEL_NUMBER,
340                 SUBST_KERNEL_NAME,
341                 SUBST_MAJOR,
342                 SUBST_MINOR,
343                 SUBST_RESULT,
344                 SUBST_SYSFS,
345                 SUBST_ENUM,
346                 SUBST_PARENT,
347                 SUBST_TEMP_NODE,
348                 SUBST_ROOT,
349                 SUBST_MODALIAS,
350                 SUBST_ENV,
351         };
352         static const struct subst_map {
353                 char *name;
354                 char fmt;
355                 enum subst_type type;
356         } map[] = {
357                 { .name = "devpath",            .fmt = 'p',     .type = SUBST_DEVPATH },
358                 { .name = "id",                 .fmt = 'b',     .type = SUBST_ID },
359                 { .name = "number",             .fmt = 'n',     .type = SUBST_KERNEL_NUMBER },
360                 { .name = "kernel",             .fmt = 'k',     .type = SUBST_KERNEL_NAME },
361                 { .name = "major",              .fmt = 'M',     .type = SUBST_MAJOR },
362                 { .name = "minor",              .fmt = 'm',     .type = SUBST_MINOR },
363                 { .name = "result",             .fmt = 'c',     .type = SUBST_RESULT },
364                 { .name = "sysfs",              .fmt = 's',     .type = SUBST_SYSFS },
365                 { .name = "enum",               .fmt = 'e',     .type = SUBST_ENUM },
366                 { .name = "parent",             .fmt = 'P',     .type = SUBST_PARENT },
367                 { .name = "tempnode",           .fmt = 'N',     .type = SUBST_TEMP_NODE },
368                 { .name = "root",               .fmt = 'r',     .type = SUBST_ROOT },
369                 { .name = "modalias",           .fmt = 'A',     .type = SUBST_MODALIAS },
370                 { .name = "env",                .fmt = 'E',     .type = SUBST_ENV },
371                 {}
372         };
373         enum subst_type type;
374         const struct subst_map *subst;
375
376         head = string;
377         while (1) {
378                 len = -1;
379                 while (head[0] != '\0') {
380                         if (head[0] == '$') {
381                                 /* substitute named variable */
382                                 if (head[1] == '\0')
383                                         break;
384                                 if (head[1] == '$') {
385                                         strlcpy(temp, head+2, sizeof(temp));
386                                         strlcpy(head+1, temp, maxsize);
387                                         head++;
388                                         continue;
389                                 }
390                                 head[0] = '\0';
391                                 for (subst = map; subst->name; subst++) {
392                                         if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
393                                                 type = subst->type;
394                                                 tail = head + strlen(subst->name)+1;
395                                                 dbg("will substitute format name '%s'", subst->name);
396                                                 goto found;
397                                         }
398                                 }
399                         }
400                         else if (head[0] == '%') {
401                                 /* substitute format char */
402                                 if (head[1] == '\0')
403                                         break;
404                                 if (head[1] == '%') {
405                                         strlcpy(temp, head+2, sizeof(temp));
406                                         strlcpy(head+1, temp, maxsize);
407                                         head++;
408                                         continue;
409                                 }
410                                 head[0] = '\0';
411                                 tail = head+1;
412                                 len = get_format_len(&tail);
413                                 for (subst = map; subst->name; subst++) {
414                                         if (tail[0] == subst->fmt) {
415                                                 type = subst->type;
416                                                 tail++;
417                                                 dbg("will substitute format char '%c'", subst->fmt);
418                                                 goto found;
419                                         }
420                                 }
421                         }
422                         head++;
423                 }
424                 break;
425 found:
426                 attr = get_format_attribute(&tail);
427                 strlcpy(temp, tail, sizeof(temp));
428                 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
429                     type ,string, tail, class_dev, sysfs_device);
430
431                 switch (type) {
432                 case SUBST_DEVPATH:
433                         strlcat(string, udev->devpath, maxsize);
434                         dbg("substitute devpath '%s'", udev->devpath);
435                         break;
436                 case SUBST_ID:
437                         strlcat(string, udev->bus_id, maxsize);
438                         dbg("substitute bus_id '%s'", udev->bus_id);
439                         break;
440                 case SUBST_KERNEL_NAME:
441                         strlcat(string, udev->kernel_name, maxsize);
442                         dbg("substitute kernel name '%s'", udev->kernel_name);
443                         break;
444                 case SUBST_KERNEL_NUMBER:
445                         strlcat(string, udev->kernel_number, maxsize);
446                         dbg("substitute kernel number '%s'", udev->kernel_number);
447                         break;
448                 case SUBST_MAJOR:
449                         sprintf(temp2, "%d", major(udev->devt));
450                         strlcat(string, temp2, maxsize);
451                         dbg("substitute major number '%s'", temp2);
452                         break;
453                 case SUBST_MINOR:
454                         sprintf(temp2, "%d", minor(udev->devt));
455                         strlcat(string, temp2, maxsize);
456                         dbg("substitute minor number '%s'", temp2);
457                         break;
458                 case SUBST_RESULT:
459                         if (udev->program_result[0] == '\0')
460                                 break;
461                         /* get part part of the result string */
462                         i = 0;
463                         if (attr != NULL)
464                                 i = strtoul(attr, &rest, 10);
465                         if (i > 0) {
466                                 dbg("request part #%d of result string", i);
467                                 cpos = udev->program_result;
468                                 while (--i) {
469                                         while (cpos[0] != '\0' && !isspace(cpos[0]))
470                                                 cpos++;
471                                         while (isspace(cpos[0]))
472                                                 cpos++;
473                                 }
474                                 if (i > 0) {
475                                         err("requested part of result string not found");
476                                         break;
477                                 }
478                                 strlcpy(temp2, cpos, sizeof(temp2));
479                                 /* %{2+}c copies the whole string from the second part on */
480                                 if (rest[0] != '+') {
481                                         cpos = strchr(temp2, ' ');
482                                         if (cpos)
483                                                 cpos[0] = '\0';
484                                 }
485                                 strlcat(string, temp2, maxsize);
486                                 dbg("substitute part of result string '%s'", temp2);
487                         } else {
488                                 strlcat(string, udev->program_result, maxsize);
489                                 dbg("substitute result string '%s'", udev->program_result);
490                         }
491                         break;
492                 case SUBST_SYSFS:
493                         if (attr == NULL) {
494                                 dbg("missing attribute");
495                                 break;
496                         }
497                         if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
498                                 struct sysfs_device *parent_device;
499
500                                 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
501                                 parent_device = sysfs_get_device_parent(sysfs_device);
502                                 while (parent_device) {
503                                         dbg("looking at '%s'", parent_device->path);
504                                         if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
505                                                 break;
506                                         parent_device = sysfs_get_device_parent(parent_device);
507                                 }
508                                 if (!parent_device)
509                                         break;
510                         }
511                         /* strip trailing whitespace of sysfs value */
512                         i = strlen(temp2);
513                         while (i > 0 && isspace(temp2[i-1]))
514                                 temp2[--i] = '\0';
515                         replace_untrusted_chars(temp2);
516                         strlcat(string, temp2, maxsize);
517                         dbg("substitute sysfs value '%s'", temp2);
518                         break;
519                 case SUBST_ENUM:
520                         next_free_number = find_free_number(udev, string);
521                         if (next_free_number > 0) {
522                                 sprintf(temp2, "%d", next_free_number);
523                                 strlcat(string, temp2, maxsize);
524                         }
525                         break;
526                 case SUBST_PARENT:
527                         if (!class_dev)
528                                 break;
529                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
530                         if (class_dev_parent != NULL) {
531                                 struct udevice udev_parent;
532
533                                 dbg("found parent '%s', get the node name", class_dev_parent->path);
534                                 udev_init_device(&udev_parent, NULL, NULL, NULL);
535                                 /* lookup the name in the udev_db with the DEVPATH of the parent */
536                                 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
537                                         strlcat(string, udev_parent.name, maxsize);
538                                         dbg("substitute parent node name'%s'", udev_parent.name);
539                                 } else
540                                         dbg("parent not found in database");
541                                 udev_cleanup_device(&udev_parent);
542                         }
543                         break;
544                 case SUBST_TEMP_NODE:
545                         if (udev->tmp_node[0] == '\0') {
546                                 dbg("create temporary device node for callout");
547                                 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
548                                          udev_root, major(udev->devt), minor(udev->devt));
549                                 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
550                                 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
551                         }
552                         strlcat(string, udev->tmp_node, maxsize);
553                         dbg("substitute temporary device node name '%s'", udev->tmp_node);
554                         break;
555                 case SUBST_ROOT:
556                         strlcat(string, udev_root, maxsize);
557                         dbg("substitute udev_root '%s'", udev_root);
558                         break;
559                 case SUBST_MODALIAS:
560                         if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
561                                 break;
562                         strlcat(string, temp2, maxsize);
563                         dbg("substitute MODALIAS '%s'", temp2);
564                         break;
565                 case SUBST_ENV:
566                         if (attr == NULL) {
567                                 dbg("missing attribute");
568                                 break;
569                         }
570                         pos = getenv(attr);
571                         if (pos == NULL)
572                                 break;
573                         strlcat(string, pos, maxsize);
574                         dbg("substitute env '%s=%s'", attr, pos);
575                         break;
576                 default:
577                         err("unknown substitution type=%i", type);
578                         break;
579                 }
580                 /* possibly truncate to format-char specified length */
581                 if (len != -1) {
582                         head[len] = '\0';
583                         dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
584                 }
585                 strlcat(string, temp, maxsize);
586         }
587 }
588
589 static int match_rule(struct udevice *udev, struct udev_rule *rule,
590                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
591 {
592         struct sysfs_device *parent_device = sysfs_device;
593
594         if (rule->action_operation != KEY_OP_UNSET) {
595                 dbg("check for " KEY_ACTION " rule->action='%s' udev->action='%s'",
596                     rule->action, udev->action);
597                 if (strcmp_pattern(rule->action, udev->action) != 0) {
598                         dbg(KEY_ACTION " is not matching");
599                         if (rule->action_operation != KEY_OP_NOMATCH)
600                                 goto exit;
601                 } else {
602                         dbg(KEY_ACTION " matches");
603                         if (rule->action_operation == KEY_OP_NOMATCH)
604                                 goto exit;
605                 }
606                 dbg(KEY_ACTION " key is true");
607         }
608
609         if (rule->kernel_operation != KEY_OP_UNSET) {
610                 dbg("check for " KEY_KERNEL " rule->kernel='%s' udev_kernel_name='%s'",
611                     rule->kernel, udev->kernel_name);
612                 if (strcmp_pattern(rule->kernel, udev->kernel_name) != 0) {
613                         dbg(KEY_KERNEL " is not matching");
614                         if (rule->kernel_operation != KEY_OP_NOMATCH)
615                                 goto exit;
616                 } else {
617                         dbg(KEY_KERNEL " matches");
618                         if (rule->kernel_operation == KEY_OP_NOMATCH)
619                                 goto exit;
620                 }
621                 dbg(KEY_KERNEL " key is true");
622         }
623
624         if (rule->subsystem_operation != KEY_OP_UNSET) {
625                 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' udev->subsystem='%s'",
626                     rule->subsystem, udev->subsystem);
627                 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
628                         dbg(KEY_SUBSYSTEM " is not matching");
629                         if (rule->subsystem_operation != KEY_OP_NOMATCH)
630                                 goto exit;
631                 } else {
632                         dbg(KEY_SUBSYSTEM " matches");
633                         if (rule->subsystem_operation == KEY_OP_NOMATCH)
634                                 goto exit;
635                 }
636                 dbg(KEY_SUBSYSTEM " key is true");
637         }
638
639         if (rule->devpath_operation != KEY_OP_UNSET) {
640                 dbg("check for " KEY_DEVPATH " rule->devpath='%s' udev->devpath='%s'",
641                     rule->devpath, udev->devpath);
642                 if (strcmp_pattern(rule->devpath, udev->devpath) != 0) {
643                         dbg(KEY_DEVPATH " is not matching");
644                         if (rule->devpath_operation != KEY_OP_NOMATCH)
645                                 goto exit;
646                 } else {
647                         dbg(KEY_DEVPATH " matches");
648                         if (rule->devpath_operation == KEY_OP_NOMATCH)
649                                 goto exit;
650                 }
651                 dbg(KEY_DEVPATH " key is true");
652         }
653
654         if (rule->modalias_operation != KEY_OP_UNSET) {
655                 char value[NAME_SIZE];
656
657                 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
658                         dbg(KEY_MODALIAS " value not found");
659                         goto exit;
660                 }
661                 dbg("check for " KEY_MODALIAS " rule->modalias='%s' modalias='%s'",
662                     rule->modalias, value);
663                 if (strcmp_pattern(rule->modalias, value) != 0) {
664                         dbg(KEY_MODALIAS " is not matching");
665                         if (rule->modalias_operation != KEY_OP_NOMATCH)
666                                 goto exit;
667                 } else {
668                         dbg(KEY_MODALIAS " matches");
669                         if (rule->modalias_operation == KEY_OP_NOMATCH)
670                                 goto exit;
671                 }
672                 dbg(KEY_MODALIAS " key is true");
673         }
674
675         if (rule->env_pair_count) {
676                 int i;
677
678                 dbg("check for " KEY_ENV " pairs");
679                 for (i = 0; i < rule->env_pair_count; i++) {
680                         struct key_pair *pair;
681                         const char *value;
682
683                         pair = &rule->env_pair[i];
684                         value = getenv(pair->name);
685                         if (!value) {
686                                 dbg(KEY_ENV "{'%s'} is not found", pair->name);
687                                 goto exit;
688                         }
689                         if (strcmp_pattern(pair->value, value) != 0) {
690                                 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
691                                 if (pair->operation != KEY_OP_NOMATCH)
692                                         goto exit;
693                         } else {
694                                 dbg(KEY_ENV "{'%s'} matches", pair->name);
695                                 if (pair->operation == KEY_OP_NOMATCH)
696                                         goto exit;
697                         }
698                 }
699                 dbg(KEY_ENV " key is true");
700         }
701
702         /* walk up the chain of physical devices and find a match */
703         while (1) {
704                 /* check for matching driver */
705                 if (rule->driver_operation != KEY_OP_UNSET) {
706                         if (parent_device == NULL) {
707                                 dbg("device has no sysfs_device");
708                                 goto exit;
709                         }
710                         dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
711                             rule->driver, parent_device->driver_name);
712                         if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
713                                 dbg(KEY_DRIVER " is not matching");
714                                 if (rule->driver_operation != KEY_OP_NOMATCH)
715                                         goto try_parent;
716                         } else {
717                                 dbg(KEY_DRIVER " matches");
718                                 if (rule->driver_operation == KEY_OP_NOMATCH)
719                                         goto try_parent;
720                         }
721                         dbg(KEY_DRIVER " key is true");
722                 }
723
724                 /* check for matching bus value */
725                 if (rule->bus_operation != KEY_OP_UNSET) {
726                         if (parent_device == NULL) {
727                                 dbg("device has no sysfs_device");
728                                 goto exit;
729                         }
730                         dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
731                             rule->bus, parent_device->bus);
732                         if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
733                                 dbg(KEY_BUS " is not matching");
734                                 if (rule->bus_operation != KEY_OP_NOMATCH)
735                                         goto try_parent;
736                         } else {
737                                 dbg(KEY_BUS " matches");
738                                 if (rule->bus_operation == KEY_OP_NOMATCH)
739                                         goto try_parent;
740                         }
741                         dbg(KEY_BUS " key is true");
742                 }
743
744                 /* check for matching bus id */
745                 if (rule->id_operation != KEY_OP_UNSET) {
746                         if (parent_device == NULL) {
747                                 dbg("device has no sysfs_device");
748                                 goto exit;
749                         }
750                         dbg("check " KEY_ID);
751                         if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
752                                 dbg(KEY_ID " is not matching");
753                                 if (rule->id_operation != KEY_OP_NOMATCH)
754                                         goto try_parent;
755                         } else {
756                                 dbg(KEY_ID " matches");
757                                 if (rule->id_operation == KEY_OP_NOMATCH)
758                                         goto try_parent;
759                         }
760                         dbg(KEY_ID " key is true");
761                 }
762
763                 /* check for matching sysfs pairs */
764                 if (rule->sysfs_pair_count) {
765                         int i;
766
767                         dbg("check " KEY_SYSFS " pairs");
768                         for (i = 0; i < rule->sysfs_pair_count; i++) {
769                                 struct key_pair *pair;
770                                 char value[VALUE_SIZE];
771                                 size_t len;
772
773                                 pair = &rule->sysfs_pair[i];
774                                 if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
775                                         goto try_parent;
776
777                                 /* strip trailing whitespace of value, if not asked to match for it */
778                                 len = strlen(pair->value);
779                                 if (len && !isspace(pair->value[len-1])) {
780                                         len = strlen(value);
781                                         while (len > 0 && isspace(value[len-1]))
782                                                 value[--len] = '\0';
783                                         dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
784                                 }
785
786                                 dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
787                                 if (strcmp_pattern(pair->value, value) != 0) {
788                                         dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
789                                         if (pair->operation != KEY_OP_NOMATCH)
790                                                 goto try_parent;
791                                 } else {
792                                         dbg(KEY_SYSFS "{'%s'} matches", pair->name);
793                                         if (pair->operation == KEY_OP_NOMATCH)
794                                                 goto try_parent;
795                                 }
796                         }
797                         dbg(KEY_SYSFS " keys are true");
798                 }
799
800                 /* found matching physical device  */
801                 break;
802 try_parent:
803                 dbg("try parent sysfs device");
804                 parent_device = sysfs_get_device_parent(parent_device);
805                 if (parent_device == NULL)
806                         goto exit;
807                 dbg("look at sysfs_device->path='%s'", parent_device->path);
808                 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
809         }
810
811         /* import variables from file into environment */
812         if (rule->import_operation != KEY_OP_UNSET) {
813                 char import[PATH_SIZE];
814
815                 strlcpy(import, rule->import, sizeof(import));
816                 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
817                 dbg("check for " KEY_IMPORT " import='%s", import);
818                 if (import_file_into_env(import) != 0) {
819                         dbg(KEY_IMPORT " failed");
820                         if (rule->import_operation != KEY_OP_NOMATCH)
821                                 goto exit;
822                 } else
823                         dbg(KEY_IMPORT " file '%s' imported", rule->import);
824                 dbg(KEY_IMPORT " key is true");
825         }
826
827         /* execute external program */
828         if (rule->program_operation != KEY_OP_UNSET) {
829                 char program[PATH_SIZE];
830                 char result[PATH_SIZE];
831
832                 strlcpy(program, rule->program, sizeof(program));
833                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
834                 dbg("check for " KEY_PROGRAM " program='%s", program);
835                 if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
836                         dbg(KEY_PROGRAM " is not matching");
837                         if (rule->program_operation != KEY_OP_NOMATCH)
838                                 goto exit;
839                 } else {
840                         dbg(KEY_PROGRAM " matches");
841                         remove_trailing_char(result, '\n');
842                         replace_untrusted_chars(result);
843                         dbg("result is '%s'", result);
844                         strlcpy(udev->program_result, result, sizeof(udev->program_result));
845                         dbg(KEY_PROGRAM " returned successful");
846                         if (rule->program_operation == KEY_OP_NOMATCH)
847                                 goto exit;
848                 }
849                 dbg(KEY_PROGRAM " key is true");
850         }
851
852         /* check for matching result of external program */
853         if (rule->result_operation != KEY_OP_UNSET) {
854                 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
855                    rule->result, udev->program_result);
856                 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
857                         dbg(KEY_RESULT " is not matching");
858                         if (rule->result_operation != KEY_OP_NOMATCH)
859                                 goto exit;
860                 } else {
861                         dbg(KEY_RESULT " matches");
862                         if (rule->result_operation == KEY_OP_NOMATCH)
863                                 goto exit;
864                 }
865                 dbg(KEY_RESULT " key is true");
866         }
867
868         /* rule matches */
869         return 0;
870
871 exit:
872         return -1;
873 }
874
875 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
876 {
877         struct sysfs_class_device *class_dev_parent;
878         struct sysfs_device *sysfs_device = NULL;
879         struct udev_rule *rule;
880
881         dbg("class_dev->name='%s'", class_dev->name);
882
883         /* Figure out where the "device"-symlink is at.  For char devices this will
884          * always be in the class_dev->path.  On block devices, only the main block
885          * device will have the device symlink in it's path. All partition devices
886          * need to look at the symlink in its parent directory.
887          */
888         class_dev_parent = sysfs_get_classdev_parent(class_dev);
889         if (class_dev_parent != NULL) {
890                 dbg("given class device has a parent, use this instead");
891                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
892         } else {
893                 sysfs_device = sysfs_get_classdev_device(class_dev);
894         }
895
896         if (sysfs_device) {
897                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
898                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
899                 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
900         }
901
902         dbg("udev->kernel_name='%s'", udev->kernel_name);
903
904         /* look for a matching rule to apply */
905         udev_rules_iter_init();
906         while (1) {
907                 rule = udev_rules_iter_next();
908                 if (rule == NULL)
909                         break;
910
911                 if (udev->name_set && rule->name_operation != KEY_OP_UNSET) {
912                         dbg("node name already set, rule ignored");
913                         continue;
914                 }
915
916                 dbg("process rule");
917                 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
918                         /* apply options */
919                         if (rule->ignore_device) {
920                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
921                                      rule->config_file, rule->config_line, udev->kernel_name);
922                                 udev->ignore_device = 1;
923                                 return 0;
924                         }
925                         if (rule->ignore_remove) {
926                                 udev->ignore_remove = 1;
927                                 dbg("remove event should be ignored");
928                         }
929                         /* apply all_partitions option only at a main block device */
930                         if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
931                                 udev->partitions = rule->partitions;
932                                 dbg("creation of partition nodes requested");
933                         }
934
935                         /* apply permissions */
936                         if (!udev->mode_final && rule->mode != 0000) {
937                                 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
938                                         udev->mode_final = 1;
939                                 udev->mode = rule->mode;
940                                 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
941                         }
942                         if (!udev->owner_final && rule->owner[0] != '\0') {
943                                 if (rule->owner_operation == KEY_OP_ASSIGN_FINAL)
944                                         udev->owner_final = 1;
945                                 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
946                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
947                                 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
948                         }
949                         if (!udev->group_final && rule->group[0] != '\0') {
950                                 if (rule->group_operation == KEY_OP_ASSIGN_FINAL)
951                                         udev->group_final = 1;
952                                 strlcpy(udev->group, rule->group, sizeof(udev->group));
953                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
954                                 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
955                         }
956
957                         /* collect symlinks */
958                         if (!udev->symlink_final && rule->symlink_operation != KEY_OP_UNSET) {
959                                 char temp[PATH_SIZE];
960                                 char *pos, *next;
961
962                                 if (rule->symlink_operation == KEY_OP_ASSIGN_FINAL)
963                                         udev->symlink_final = 1;
964                                 if (rule->symlink_operation == KEY_OP_ASSIGN || rule->symlink_operation == KEY_OP_ASSIGN_FINAL) {
965                                         struct name_entry *name_loop;
966                                         struct name_entry *temp_loop;
967
968                                         info("reset symlink list");
969                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
970                                                 list_del(&name_loop->node);
971                                                 free(name_loop);
972                                         }
973                                 }
974                                 if (rule->symlink[0] != '\0') {
975                                         info("configured rule in '%s[%i]' applied, added symlink '%s'",
976                                              rule->config_file, rule->config_line, rule->symlink);
977                                         strlcpy(temp, rule->symlink, sizeof(temp));
978                                         apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
979
980                                         /* add multiple symlinks separated by spaces */
981                                         pos = temp;
982                                         next = strchr(temp, ' ');
983                                         while (next) {
984                                                 next[0] = '\0';
985                                                 info("add symlink '%s'", pos);
986                                                 name_list_add(&udev->symlink_list, pos, 0);
987                                                 pos = &next[1];
988                                                 next = strchr(pos, ' ');
989                                         }
990                                         info("add symlink '%s'", pos);
991                                         name_list_add(&udev->symlink_list, pos, 0);
992                                 }
993                         }
994
995                         /* set name, later rules with name set will be ignored */
996                         if (rule->name_operation != KEY_OP_UNSET) {
997                                 udev->name_set = 1;
998                                 if (rule->name[0] == '\0') {
999                                         info("configured rule in '%s[%i]' applied, node handling for '%s' supressed",
1000                                              rule->config_file, rule->config_line, udev->kernel_name);
1001                                 } else {
1002                                         strlcpy(udev->name, rule->name, sizeof(udev->name));
1003                                         apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
1004                                         strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
1005                                         udev->config_line = rule->config_line;
1006
1007                                         info("configured rule in '%s:%i' applied, '%s' becomes '%s'",
1008                                              rule->config_file, rule->config_line, udev->kernel_name, rule->name);
1009                                         if (udev->type != DEV_NET)
1010                                                 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1011                                                     udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1012                                 }
1013                         }
1014
1015                         if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
1016                                 char program[PATH_SIZE];
1017
1018                                 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
1019                                         udev->run_final = 1;
1020                                 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
1021                                         struct name_entry *name_loop;
1022                                         struct name_entry *temp_loop;
1023
1024                                         info("reset run list");
1025                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1026                                                 list_del(&name_loop->node);
1027                                                 free(name_loop);
1028                                         }
1029                                 }
1030                                 if (rule->run[0] != '\0') {
1031                                         strlcpy(program, rule->run, sizeof(program));
1032                                         apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1033                                         dbg("add run '%s'", program);
1034                                         name_list_add(&udev->run_list, program, 0);
1035                                 }
1036                         }
1037
1038                         if (rule->last_rule) {
1039                                 dbg("last rule to be applied");
1040                                 break;
1041                         }
1042                 }
1043         }
1044
1045         if (udev->name[0] == '\0') {
1046                 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1047                 info("no rule found, use kernel name '%s'", udev->name);
1048         }
1049
1050         if (udev->tmp_node[0] != '\0') {
1051                 dbg("removing temporary device node");
1052                 unlink_secure(udev->tmp_node);
1053                 udev->tmp_node[0] = '\0';
1054         }
1055
1056         return 0;
1057 }
1058
1059 int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device)
1060 {
1061         struct udev_rule *rule;
1062
1063         /* look for a matching rule to apply */
1064         udev_rules_iter_init();
1065         while (1) {
1066                 rule = udev_rules_iter_next();
1067                 if (rule == NULL)
1068                         break;
1069
1070                 dbg("process rule");
1071                 if (rule->run_operation == KEY_OP_UNSET)
1072                         continue;
1073
1074                 if (rule->name_operation != KEY_OP_UNSET || rule->symlink_operation != KEY_OP_UNSET ||
1075                     rule->mode != 0000 || rule->owner[0] != '\0' || rule->group[0] != '\0') {
1076                         dbg("skip rule that names a device");
1077                         continue;
1078                 }
1079
1080                 if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
1081                         if (rule->ignore_device) {
1082                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
1083                                      rule->config_file, rule->config_line, udev->kernel_name);
1084                                 udev->ignore_device = 1;
1085                                 return 0;
1086                         }
1087
1088                         if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
1089                                 char program[PATH_SIZE];
1090
1091                                 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
1092                                         struct name_entry *name_loop;
1093                                         struct name_entry *temp_loop;
1094
1095                                         info("reset run list");
1096                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1097                                                 list_del(&name_loop->node);
1098                                                 free(name_loop);
1099                                         }
1100                                 }
1101                                 if (rule->run[0] != '\0') {
1102                                         strlcpy(program, rule->run, sizeof(program));
1103                                         apply_format(udev, program, sizeof(program), NULL, sysfs_device);
1104                                         dbg("add run '%s'", program);
1105                                         name_list_add(&udev->run_list, program, 0);
1106                                 }
1107                                 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
1108                                         break;
1109                         }
1110
1111                         if (rule->last_rule) {
1112                                 dbg("last rule to be applied");
1113                                 break;
1114                         }
1115                 }
1116         }
1117
1118         return 0;
1119 }