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