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