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