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