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