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