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