chiark / gitweb /
Merge rsync://rsync.kernel.org/pub/scm/linux/hotplug/udev
[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_DEBUG)) != 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
671         if (match_key("ACTION", rule, &rule->action, udev->action))
672                 goto exit;
673
674         if (match_key("KERNEL", rule, &rule->kernel_name, udev->kernel_name))
675                 goto exit;
676
677         if (match_key("SUBSYSTEM", rule, &rule->subsystem, udev->subsystem))
678                 goto exit;
679
680         if (match_key("DEVPATH", rule, &rule->devpath, udev->devpath))
681                 goto exit;
682
683         if (rule->modalias.operation != KEY_OP_UNSET) {
684                 char value[NAME_SIZE];
685
686                 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
687                         dbg("MODALIAS value not found");
688                         goto exit;
689                 }
690                 if (match_key("MODALIAS", rule, &rule->modalias, value))
691                         goto exit;
692         }
693
694         if (rule->env.count) {
695                 int i;
696
697                 dbg("check %i ENV keys", rule->env.count);
698                 for (i = 0; i < rule->env.count; i++) {
699                         struct key_pair *pair = &rule->env.keys[i];
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", key_name);
705                                 value = "";
706                         }
707                         if (match_key("ENV", rule, &pair->key, value))
708                                 goto exit;
709                 }
710                 dbg("all %i ENV keys matched", rule->env.count);
711         }
712
713         if (rule->wait_for_sysfs.operation != KEY_OP_UNSET) {
714                 int match;
715
716                 match = (wait_for_sysfs(udev, key_val(rule, &rule->wait_for_sysfs), 3) == 0);
717                 if (match && (rule->wait_for_sysfs.operation != KEY_OP_NOMATCH)) {
718                         dbg("WAIT_FOR_SYSFS is true (matching value)");
719                         return 0;
720                 }
721                 if (!match && (rule->wait_for_sysfs.operation == KEY_OP_NOMATCH)) {
722                         dbg("WAIT_FOR_SYSFS is true, (non matching value)");
723                         return 0;
724                 }
725                 dbg("WAIT_FOR_SYSFS is false");
726                 return -1;
727         }
728
729         /* walk up the chain of physical devices and find a match */
730         while (1) {
731                 /* check for matching driver */
732                 if (rule->driver.operation != KEY_OP_UNSET) {
733                         if (parent_device == NULL) {
734                                 dbg("device has no sysfs_device");
735                                 goto exit;
736                         }
737                         if (match_key("DRIVER", rule, &rule->driver, parent_device->driver_name))
738                                 goto try_parent;
739                 }
740
741                 /* check for matching bus value */
742                 if (rule->bus.operation != KEY_OP_UNSET) {
743                         if (parent_device == NULL) {
744                                 dbg("device has no sysfs_device");
745                                 goto exit;
746                         }
747                         if (match_key("BUS", rule, &rule->bus, parent_device->bus))
748                                 goto try_parent;
749                 }
750
751                 /* check for matching bus id */
752                 if (rule->id.operation != KEY_OP_UNSET) {
753                         if (parent_device == NULL) {
754                                 dbg("device has no sysfs_device");
755                                 goto exit;
756                         }
757                         if (match_key("ID", rule, &rule->id, parent_device->bus_id))
758                                 goto try_parent;
759                 }
760
761                 /* check for matching sysfs pairs */
762                 if (rule->sysfs.count) {
763                         int i;
764
765                         dbg("check %i SYSFS keys", rule->sysfs.count);
766                         for (i = 0; i < rule->sysfs.count; i++) {
767                                 struct key_pair *pair = &rule->sysfs.keys[i];
768                                 const char *key_name = key_pair_name(rule, pair);
769                                 const char *key_value = key_val(rule, &pair->key);
770                                 char value[VALUE_SIZE];
771                                 size_t len;
772
773                                 if (find_sysfs_attribute(class_dev, parent_device, key_name, value, sizeof(value)) != 0)
774                                         goto try_parent;
775
776                                 /* strip trailing whitespace of value, if not asked to match for it */
777                                 len = strlen(key_value);
778                                 if (len && !isspace(key_value[len-1])) {
779                                         len = strlen(value);
780                                         while (len > 0 && isspace(value[len-1]))
781                                                 value[--len] = '\0';
782                                         dbg("removed %zi trailing whitespace chars from '%s'", strlen(value)-len, value);
783                                 }
784
785                                 if (match_key("SYSFS", rule, &pair->key, value))
786                                         goto try_parent;
787                         }
788                         dbg("all %i SYSFS keys matched", rule->sysfs.count);
789                 }
790
791                 /* found matching physical device  */
792                 break;
793 try_parent:
794                 dbg("try parent sysfs device");
795                 parent_device = sysfs_get_device_parent(parent_device);
796                 if (parent_device == NULL)
797                         goto exit;
798                 dbg("look at sysfs_device->path='%s'", parent_device->path);
799                 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
800         }
801
802         /* import variables from file into environment */
803         if (rule->import.operation != KEY_OP_UNSET) {
804                 char import[PATH_SIZE];
805                 int rc = -1;
806
807                 strlcpy(import, key_val(rule, &rule->import), sizeof(import));
808                 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
809                 dbg("check for IMPORT import='%s'", import);
810                 if (rule->import_type == IMPORT_PROGRAM) {
811                         info("IMPORT executes '%s'", import);
812                         rc = import_program_into_env(udev, import);
813                 } else if (rule->import_type == IMPORT_FILE) {
814                         dbg("import file import='%s'", import);
815                         rc = import_file_into_env(udev, import);
816                 } else if (rule->import_type == IMPORT_PARENT && class_dev) {
817                         dbg("import parent import='%s'", import);
818                         rc = import_parent_into_env(udev, class_dev, import);
819                 }
820                 if (rc) {
821                         dbg("IMPORT failed");
822                         if (rule->import.operation != KEY_OP_NOMATCH)
823                                 goto exit;
824                 } else
825                         dbg("IMPORT '%s' imported", key_val(rule, &rule->import));
826                 dbg("IMPORT key is true");
827         }
828
829         /* execute external program */
830         if (rule->program.operation != KEY_OP_UNSET) {
831                 char program[PATH_SIZE];
832                 char result[PATH_SIZE];
833
834                 strlcpy(program, key_val(rule, &rule->program), sizeof(program));
835                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
836                 info("PROGRAM key executes '%s", program);
837                 if (run_program(program, udev->subsystem, result, sizeof(result), NULL, (udev_log_priority >= LOG_DEBUG)) != 0) {
838                         dbg("PROGRAM is false");
839                         udev->program_result[0] = '\0';
840                         if (rule->program.operation != KEY_OP_NOMATCH)
841                                 goto exit;
842                 } else {
843                         dbg("PROGRAM matches");
844                         remove_trailing_char(result, '\n');
845                         replace_untrusted_chars(result);
846                         dbg("result is '%s'", result);
847                         strlcpy(udev->program_result, result, sizeof(udev->program_result));
848                         dbg("PROGRAM returned successful");
849                         if (rule->program.operation == KEY_OP_NOMATCH)
850                                 goto exit;
851                 }
852                 dbg("PROGRAM key is true");
853         }
854
855         /* check for matching result of external program */
856         if (match_key("RESULT", rule, &rule->result, udev->program_result))
857                 goto exit;
858
859         /* rule matches */
860         return 0;
861
862 exit:
863         return -1;
864 }
865
866 int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev, struct sysfs_class_device *class_dev)
867 {
868         struct sysfs_class_device *class_dev_parent;
869         struct sysfs_device *sysfs_device = NULL;
870         struct udev_rule *rule;
871         int name_set = 0;
872
873         dbg("class_dev->name='%s'", class_dev->name);
874
875         /* Figure out where the "device"-symlink is at.  For char devices this will
876          * always be in the class_dev->path.  On block devices, only the main block
877          * device will have the device symlink in it's path. All partition devices
878          * need to look at the symlink in its parent directory.
879          */
880         class_dev_parent = sysfs_get_classdev_parent(class_dev);
881         if (class_dev_parent != NULL) {
882                 dbg("given class device has a parent, use this instead");
883                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
884         } else {
885                 sysfs_device = sysfs_get_classdev_device(class_dev);
886         }
887
888         if (sysfs_device) {
889                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
890                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
891                 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
892         }
893
894         dbg("udev->kernel_name='%s'", udev->kernel_name);
895
896         /* look for a matching rule to apply */
897         udev_rules_iter_init(rules);
898         while (1) {
899                 rule = udev_rules_iter_next(rules);
900                 if (rule == NULL)
901                         break;
902
903                 if (name_set && rule->name.operation != KEY_OP_UNSET) {
904                         dbg("node name already set, rule ignored");
905                         continue;
906                 }
907
908                 dbg("process rule");
909                 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
910                         /* apply options */
911                         if (rule->ignore_device) {
912                                 info("rule applied, '%s' is ignored", udev->kernel_name);
913                                 udev->ignore_device = 1;
914                                 return 0;
915                         }
916                         if (rule->ignore_remove) {
917                                 udev->ignore_remove = 1;
918                                 dbg("remove event should be ignored");
919                         }
920                         /* apply all_partitions option only at a main block device */
921                         if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
922                                 udev->partitions = rule->partitions;
923                                 dbg("creation of partition nodes requested");
924                         }
925
926                         /* apply permissions */
927                         if (!udev->mode_final && rule->mode != 0000) {
928                                 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
929                                         udev->mode_final = 1;
930                                 udev->mode = rule->mode;
931                                 dbg("applied mode=%#o to '%s'", rule->mode, udev->kernel_name);
932                         }
933                         if (!udev->owner_final && rule->owner.operation != KEY_OP_UNSET) {
934                                 if (rule->owner.operation == KEY_OP_ASSIGN_FINAL)
935                                         udev->owner_final = 1;
936                                 strlcpy(udev->owner, key_val(rule, &rule->owner), sizeof(udev->owner));
937                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
938                                 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
939                         }
940                         if (!udev->group_final && rule->group.operation != KEY_OP_UNSET) {
941                                 if (rule->group.operation == KEY_OP_ASSIGN_FINAL)
942                                         udev->group_final = 1;
943                                 strlcpy(udev->group, key_val(rule, &rule->group), sizeof(udev->group));
944                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
945                                 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
946                         }
947
948                         /* collect symlinks */
949                         if (!udev->symlink_final && rule->symlink.operation != KEY_OP_UNSET) {
950                                 char temp[PATH_SIZE];
951                                 char *pos, *next;
952
953                                 if (rule->symlink.operation == KEY_OP_ASSIGN_FINAL)
954                                         udev->symlink_final = 1;
955                                 if (rule->symlink.operation == KEY_OP_ASSIGN || rule->symlink.operation == KEY_OP_ASSIGN_FINAL) {
956                                         struct name_entry *name_loop;
957                                         struct name_entry *temp_loop;
958
959                                         info("reset symlink list");
960                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
961                                                 list_del(&name_loop->node);
962                                                 free(name_loop);
963                                         }
964                                 }
965                                 strlcpy(temp, key_val(rule, &rule->symlink), sizeof(temp));
966                                 apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
967                                 info("rule applied, added symlink '%s'", temp);
968
969                                 /* add multiple symlinks separated by spaces */
970                                 pos = temp;
971                                 while (isspace(pos[0]))
972                                         pos++;
973                                 next = strchr(pos, ' ');
974                                 while (next) {
975                                         next[0] = '\0';
976                                         info("add symlink '%s'", pos);
977                                         name_list_add(&udev->symlink_list, pos, 0);
978                                         while (isspace(next[1]))
979                                                 next++;
980                                         pos = &next[1];
981                                         next = strchr(pos, ' ');
982                                 }
983                                 if (pos[0] != '\0') {
984                                         info("add symlink '%s'", pos);
985                                         name_list_add(&udev->symlink_list, pos, 0);
986                                 }
987                         }
988
989                         /* set name, later rules with name set will be ignored */
990                         if (rule->name.operation != KEY_OP_UNSET) {
991                                 name_set = 1;
992                                 strlcpy(udev->name, key_val(rule, &rule->name), sizeof(udev->name));
993                                 apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
994
995                                 info("rule applied, '%s' becomes '%s'", udev->kernel_name, udev->name);
996                                 if (udev->type != DEV_NET)
997                                         dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
998                                             udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
999                         }
1000
1001                         if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1002                                 char program[PATH_SIZE];
1003
1004                                 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1005                                         udev->run_final = 1;
1006                                 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1007                                         struct name_entry *name_loop;
1008                                         struct name_entry *temp_loop;
1009
1010                                         info("reset run list");
1011                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1012                                                 list_del(&name_loop->node);
1013                                                 free(name_loop);
1014                                         }
1015                                 }
1016                                 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1017                                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1018                                 dbg("add run '%s'", program);
1019                                 name_list_add(&udev->run_list, program, 0);
1020                         }
1021
1022                         if (rule->last_rule) {
1023                                 dbg("last rule to be applied");
1024                                 break;
1025                         }
1026
1027                         if (rule->goto_label.operation != KEY_OP_UNSET) {
1028                                 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1029                                 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1030                         }
1031                 }
1032         }
1033
1034         if (!name_set) {
1035                 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1036                 info("no node name set, will use kernel name '%s'", udev->name);
1037         }
1038
1039         if (udev->tmp_node[0] != '\0') {
1040                 dbg("removing temporary device node");
1041                 unlink_secure(udev->tmp_node);
1042                 udev->tmp_node[0] = '\0';
1043         }
1044
1045         return 0;
1046 }
1047
1048 int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev,
1049                        struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_dev)
1050 {
1051         struct udev_rule *rule;
1052
1053         if (class_dev && !sysfs_dev)
1054                 sysfs_dev = sysfs_get_classdev_device(class_dev);
1055         if (sysfs_dev) {
1056                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
1057                     sysfs_dev->path, sysfs_dev->bus_id, sysfs_dev->bus);
1058                 strlcpy(udev->bus_id, sysfs_dev->bus_id, sizeof(udev->bus_id));
1059         }
1060
1061         dbg("udev->kernel_name='%s'", udev->kernel_name);
1062
1063         /* look for a matching rule to apply */
1064         udev_rules_iter_init(rules);
1065         while (1) {
1066                 rule = udev_rules_iter_next(rules);
1067                 if (rule == NULL)
1068                         break;
1069
1070                 dbg("process rule");
1071                 if (rule->name.operation != KEY_OP_UNSET || rule->symlink.operation != KEY_OP_UNSET ||
1072                     rule->mode_operation != KEY_OP_UNSET || rule->owner.operation != KEY_OP_UNSET || rule->group.operation != KEY_OP_UNSET) {
1073                         dbg("skip rule that names a device");
1074                         continue;
1075                 }
1076
1077                 if (match_rule(udev, rule, class_dev, sysfs_dev) == 0) {
1078                         if (rule->ignore_device) {
1079                                 info("rule applied, '%s' is ignored", udev->kernel_name);
1080                                 udev->ignore_device = 1;
1081                                 return 0;
1082                         }
1083
1084                         if (!udev->run_final && rule->run.operation != KEY_OP_UNSET) {
1085                                 char program[PATH_SIZE];
1086
1087                                 if (rule->run.operation == KEY_OP_ASSIGN || rule->run.operation == KEY_OP_ASSIGN_FINAL) {
1088                                         struct name_entry *name_loop;
1089                                         struct name_entry *temp_loop;
1090
1091                                         info("reset run list");
1092                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1093                                                 list_del(&name_loop->node);
1094                                                 free(name_loop);
1095                                         }
1096                                 }
1097                                 strlcpy(program, key_val(rule, &rule->run), sizeof(program));
1098                                 apply_format(udev, program, sizeof(program), class_dev, sysfs_dev);
1099                                 dbg("add run '%s'", program);
1100                                 name_list_add(&udev->run_list, program, 0);
1101                                 if (rule->run.operation == KEY_OP_ASSIGN_FINAL)
1102                                         break;
1103                         }
1104
1105                         if (rule->last_rule) {
1106                                 dbg("last rule to be applied");
1107                                 break;
1108                         }
1109
1110                         if (rule->goto_label.operation != KEY_OP_UNSET) {
1111                                 dbg("moving forward to label '%s'", key_val(rule, &rule->goto_label));
1112                                 udev_rules_iter_label(rules, key_val(rule, &rule->goto_label));
1113                         }
1114                 }
1115         }
1116
1117         return 0;
1118 }