chiark / gitweb /
move syslog wrapper to libudev
[elogind.git] / udev / udev-rules.c
1 /*
2  * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
3  * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stddef.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <dirent.h>
29 #include <fnmatch.h>
30
31 #include "udev.h"
32
33 #define PREALLOC_TOKEN                  2048
34 #define PREALLOC_STRBUF                 32 * 1024
35 #define PREALLOC_TRIE                   256
36
37 struct uid_gid {
38         unsigned int name_off;
39         union {
40                 uid_t   uid;
41                 gid_t   gid;
42         };
43 };
44
45 struct trie_node {
46         /* this node's first child */
47         unsigned int child_idx;
48         /* the next child of our parent node's child list */
49         unsigned int next_child_idx;
50         /* this node's last child (shortcut for append) */
51         unsigned int last_child_idx;
52         unsigned int value_off;
53         unsigned short value_len;
54         unsigned char key;
55 };
56
57 struct udev_rules {
58         struct udev *udev;
59         int resolve_names;
60
61         /* every key in the rules file becomes a token */
62         struct token *tokens;
63         unsigned int token_cur;
64         unsigned int token_max;
65
66         /* all key strings are copied to a single string buffer */
67         char *buf;
68         size_t buf_cur;
69         size_t buf_max;
70         unsigned int buf_count;
71
72         /* during rule parsing, strings are indexed to find duplicates */
73         struct trie_node *trie_nodes;
74         unsigned int trie_nodes_cur;
75         unsigned int trie_nodes_max;
76
77         /* during rule parsing, uid/gid lookup results are cached */
78         struct uid_gid *uids;
79         unsigned int uids_cur;
80         unsigned int uids_max;
81         struct uid_gid *gids;
82         unsigned int gids_cur;
83         unsigned int gids_max;
84 };
85
86 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
87 enum operation_type {
88         OP_UNSET,
89
90         OP_MATCH,
91         OP_NOMATCH,
92         OP_MATCH_MAX,
93
94         OP_ADD,
95         OP_ASSIGN,
96         OP_ASSIGN_FINAL,
97 };
98
99 enum string_glob_type {
100         GL_UNSET,
101         GL_PLAIN,                       /* no special chars */
102         GL_GLOB,                        /* shell globs ?,*,[] */
103         GL_SPLIT,                       /* multi-value A|B */
104         GL_SPLIT_GLOB,                  /* multi-value with glob A*|B* */
105         GL_SOMETHING,                   /* commonly used "?*" */
106 };
107
108 enum string_subst_type {
109         SB_UNSET,
110         SB_NONE,
111         SB_FORMAT,
112         SB_SUBSYS,
113 };
114
115 /* tokens of a rule are sorted/handled in this order */
116 enum token_type {
117         TK_UNSET,
118         TK_RULE,
119
120         TK_M_ACTION,                    /* val */
121         TK_M_DEVPATH,                   /* val */
122         TK_M_KERNEL,                    /* val */
123         TK_M_DEVLINK,                   /* val */
124         TK_M_NAME,                      /* val */
125         TK_M_ENV,                       /* val, attr */
126         TK_M_SUBSYSTEM,                 /* val */
127         TK_M_DRIVER,                    /* val */
128         TK_M_WAITFOR,                   /* val */
129         TK_M_ATTR,                      /* val, attr */
130
131         TK_M_PARENTS_MIN,
132         TK_M_KERNELS,                   /* val */
133         TK_M_SUBSYSTEMS,                /* val */
134         TK_M_DRIVERS,                   /* val */
135         TK_M_ATTRS,                     /* val, attr */
136         TK_M_PARENTS_MAX,
137
138         TK_M_TEST,                      /* val, mode_t */
139         TK_M_PROGRAM,                   /* val */
140         TK_M_IMPORT_FILE,               /* val */
141         TK_M_IMPORT_PROG,               /* val */
142         TK_M_IMPORT_PARENT,             /* val */
143         TK_M_RESULT,                    /* val */
144         TK_M_MAX,
145
146         TK_A_IGNORE_DEVICE,
147         TK_A_STRING_ESCAPE_NONE,
148         TK_A_STRING_ESCAPE_REPLACE,
149         TK_A_INOTIFY_WATCH,             /* int */
150         TK_A_NUM_FAKE_PART,             /* int */
151         TK_A_DEVLINK_PRIO,              /* int */
152         TK_A_OWNER,                     /* val */
153         TK_A_GROUP,                     /* val */
154         TK_A_MODE,                      /* val */
155         TK_A_OWNER_ID,                  /* uid_t */
156         TK_A_GROUP_ID,                  /* gid_t */
157         TK_A_MODE_ID,                   /* mode_t */
158         TK_A_ENV,                       /* val, attr */
159         TK_A_NAME,                      /* val */
160         TK_A_DEVLINK,                   /* val */
161         TK_A_EVENT_TIMEOUT,             /* int */
162         TK_A_IGNORE_REMOVE,
163         TK_A_ATTR,                      /* val, attr */
164         TK_A_RUN,                       /* val, bool */
165         TK_A_GOTO,                      /* size_t */
166         TK_A_LAST_RULE,
167
168         TK_END,
169 };
170
171 /* we try to pack stuff in a way that we take only 12 bytes per token */
172 struct token {
173         union {
174                 unsigned char type;             /* same as in rule and key */
175                 struct {
176                         enum token_type type:8;
177                         unsigned int flags:8;
178                         unsigned short token_count;
179                         unsigned int label_off;
180                         unsigned short filename_off;
181                         unsigned short filename_line;
182                 } rule;
183                 struct {
184                         enum token_type type:8;
185                         enum operation_type op:8;
186                         enum string_glob_type glob:8;
187                         enum string_subst_type subst:4;
188                         enum string_subst_type attrsubst:4;
189                         unsigned int value_off;
190                         union {
191                                 unsigned int attr_off;
192                                 int ignore_error;
193                                 unsigned int rule_goto;
194                                 mode_t  mode;
195                                 uid_t uid;
196                                 gid_t gid;
197                                 int num_fake_part;
198                                 int devlink_prio;
199                                 int event_timeout;
200                                 int watch;
201                         };
202                 } key;
203         };
204 };
205
206 #define MAX_TK          64
207 struct rule_tmp {
208         struct udev_rules *rules;
209         struct token rule;
210         struct token token[MAX_TK];
211         unsigned int token_cur;
212 };
213
214 #ifdef DEBUG
215 static const char *operation_str(enum operation_type type)
216 {
217         static const char *operation_strs[] = {
218                 [OP_UNSET] =            "UNSET",
219                 [OP_MATCH] =            "match",
220                 [OP_NOMATCH] =          "nomatch",
221                 [OP_MATCH_MAX] =        "MATCH_MAX",
222
223                 [OP_ADD] =              "add",
224                 [OP_ASSIGN] =           "assign",
225                 [OP_ASSIGN_FINAL] =     "assign-final",
226 }       ;
227
228         return operation_strs[type];
229 }
230
231 static const char *string_glob_str(enum string_glob_type type)
232 {
233         static const char *string_glob_strs[] = {
234                 [GL_UNSET] =            "UNSET",
235                 [GL_PLAIN] =            "plain",
236                 [GL_GLOB] =             "glob",
237                 [GL_SPLIT] =            "split",
238                 [GL_SPLIT_GLOB] =       "split-glob",
239                 [GL_SOMETHING] =        "split-glob",
240         };
241
242         return string_glob_strs[type];
243 }
244
245 static const char *token_str(enum token_type type)
246 {
247         static const char *token_strs[] = {
248                 [TK_UNSET] =                    "UNSET",
249                 [TK_RULE] =                     "RULE",
250
251                 [TK_M_ACTION] =                 "M ACTION",
252                 [TK_M_DEVPATH] =                "M DEVPATH",
253                 [TK_M_KERNEL] =                 "M KERNEL",
254                 [TK_M_DEVLINK] =                "M DEVLINK",
255                 [TK_M_NAME] =                   "M NAME",
256                 [TK_M_ENV] =                    "M ENV",
257                 [TK_M_SUBSYSTEM] =              "M SUBSYSTEM",
258                 [TK_M_DRIVER] =                 "M DRIVER",
259                 [TK_M_WAITFOR] =                "M WAITFOR",
260                 [TK_M_ATTR] =                   "M ATTR",
261
262                 [TK_M_PARENTS_MIN] =            "M PARENTS_MIN",
263                 [TK_M_KERNELS] =                "M KERNELS",
264                 [TK_M_SUBSYSTEMS] =             "M SUBSYSTEMS",
265                 [TK_M_DRIVERS] =                "M DRIVERS",
266                 [TK_M_ATTRS] =                  "M ATTRS",
267                 [TK_M_PARENTS_MAX] =            "M PARENTS_MAX",
268
269                 [TK_M_TEST] =                   "M TEST",
270                 [TK_M_PROGRAM] =                "M PROGRAM",
271                 [TK_M_IMPORT_FILE] =            "M IMPORT_FILE",
272                 [TK_M_IMPORT_PROG] =            "M IMPORT_PROG",
273                 [TK_M_IMPORT_PARENT] =          "M MPORT_PARENT",
274                 [TK_M_RESULT] =                 "M RESULT",
275                 [TK_M_MAX] =                    "M MAX",
276
277                 [TK_A_IGNORE_DEVICE] =          "A IGNORE_DEVICE",
278                 [TK_A_STRING_ESCAPE_NONE] =     "A STRING_ESCAPE_NONE",
279                 [TK_A_STRING_ESCAPE_REPLACE] =  "A STRING_ESCAPE_REPLACE",
280                 [TK_A_INOTIFY_WATCH] =          "A INOTIFY_WATCH",
281                 [TK_A_NUM_FAKE_PART] =          "A NUM_FAKE_PART",
282                 [TK_A_DEVLINK_PRIO] =           "A DEVLINK_PRIO",
283                 [TK_A_OWNER] =                  "A OWNER",
284                 [TK_A_GROUP] =                  "A GROUP",
285                 [TK_A_MODE] =                   "A MODE",
286                 [TK_A_OWNER_ID] =               "A OWNER_ID",
287                 [TK_A_GROUP_ID] =               "A GROUP_ID",
288                 [TK_A_MODE_ID] =                "A MODE_ID",
289                 [TK_A_ENV] =                    "A ENV",
290                 [TK_A_NAME] =                   "A NAME",
291                 [TK_A_DEVLINK] =                "A DEVLINK",
292                 [TK_A_EVENT_TIMEOUT] =          "A EVENT_TIMEOUT",
293                 [TK_A_IGNORE_REMOVE] =          "A IGNORE_REMOVE",
294                 [TK_A_ATTR] =                   "A ATTR",
295                 [TK_A_RUN] =                    "A RUN",
296                 [TK_A_GOTO] =                   "A GOTO",
297                 [TK_A_LAST_RULE] =              "A LAST_RULE",
298
299                 [TK_END] =                      "END",
300         };
301
302         return token_strs[type];
303 }
304
305 static void dump_token(struct udev_rules *rules, struct token *token)
306 {
307         enum token_type type = token->type;
308         enum operation_type op = token->key.op;
309         enum string_glob_type glob = token->key.glob;
310         const char *value = &rules->buf[token->key.value_off];
311         const char *attr = &rules->buf[token->key.attr_off];
312
313         switch (type) {
314         case TK_RULE:
315                 {
316                         const char *tks_ptr = (char *)rules->tokens;
317                         const char *tk_ptr = (char *)token;
318                         unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
319
320                         dbg(rules->udev, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
321                             &rules->buf[token->rule.filename_off], token->rule.filename_line,
322                             idx, token->rule.token_count,
323                             &rules->buf[token->rule.label_off],
324                             token->rule.flags);
325                         break;
326                 }
327         case TK_M_ACTION:
328         case TK_M_DEVPATH:
329         case TK_M_KERNEL:
330         case TK_M_SUBSYSTEM:
331         case TK_M_DRIVER:
332         case TK_M_WAITFOR:
333         case TK_M_DEVLINK:
334         case TK_M_NAME:
335         case TK_M_KERNELS:
336         case TK_M_SUBSYSTEMS:
337         case TK_M_DRIVERS:
338         case TK_M_PROGRAM:
339         case TK_M_IMPORT_FILE:
340         case TK_M_IMPORT_PROG:
341         case TK_M_IMPORT_PARENT:
342         case TK_M_RESULT:
343         case TK_A_NAME:
344         case TK_A_DEVLINK:
345         case TK_A_OWNER:
346         case TK_A_GROUP:
347         case TK_A_MODE:
348         case TK_A_RUN:
349                 dbg(rules->udev, "%s %s '%s'(%s)\n",
350                     token_str(type), operation_str(op), value, string_glob_str(glob));
351                 break;
352         case TK_M_ATTR:
353         case TK_M_ATTRS:
354         case TK_M_ENV:
355         case TK_A_ATTR:
356         case TK_A_ENV:
357                 dbg(rules->udev, "%s %s '%s' '%s'(%s)\n",
358                     token_str(type), operation_str(op), attr, value, string_glob_str(glob));
359                 break;
360         case TK_A_IGNORE_DEVICE:
361         case TK_A_STRING_ESCAPE_NONE:
362         case TK_A_STRING_ESCAPE_REPLACE:
363         case TK_A_LAST_RULE:
364         case TK_A_IGNORE_REMOVE:
365                 dbg(rules->udev, "%s\n", token_str(type));
366                 break;
367         case TK_M_TEST:
368                 dbg(rules->udev, "%s %s '%s'(%s) %#o\n",
369                     token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
370                 break;
371         case TK_A_INOTIFY_WATCH:
372                 dbg(rules->udev, "%s %u\n", token_str(type), token->key.watch);
373                 break;
374         case TK_A_NUM_FAKE_PART:
375                 dbg(rules->udev, "%s %u\n", token_str(type), token->key.num_fake_part);
376                 break;
377         case TK_A_DEVLINK_PRIO:
378                 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.devlink_prio);
379                 break;
380         case TK_A_OWNER_ID:
381                 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
382                 break;
383         case TK_A_GROUP_ID:
384                 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
385                 break;
386         case TK_A_MODE_ID:
387                 dbg(rules->udev, "%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
388                 break;
389         case TK_A_EVENT_TIMEOUT:
390                 dbg(rules->udev, "%s %s %u\n", token_str(type), operation_str(op), token->key.event_timeout);
391                 break;
392         case TK_A_GOTO:
393                 dbg(rules->udev, "%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
394                 break;
395         case TK_END:
396                 dbg(rules->udev, "* %s\n", token_str(type));
397                 break;
398         case TK_M_PARENTS_MIN:
399         case TK_M_PARENTS_MAX:
400         case TK_M_MAX:
401         case TK_UNSET:
402                 dbg(rules->udev, "unknown type %u\n", type);
403                 break;
404         }
405 }
406
407 static void dump_rules(struct udev_rules *rules)
408 {
409         unsigned int i;
410
411         dbg(rules->udev, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
412             rules->token_cur,
413             rules->token_cur * sizeof(struct token),
414             rules->buf_count,
415             rules->buf_cur);
416         for(i = 0; i < rules->token_cur; i++)
417                 dump_token(rules, &rules->tokens[i]);
418 }
419 #else
420 static inline const char *operation_str(enum operation_type type) { return NULL; }
421 static inline const char *token_str(enum token_type type) { return NULL; }
422 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
423 static inline void dump_rules(struct udev_rules *rules) {}
424 #endif /* DEBUG */
425
426 static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
427 {
428         int off;
429
430         /* grow buffer if needed */
431         if (rules->buf_cur + bytes+1 >= rules->buf_max) {
432                 char *buf;
433                 unsigned int add;
434
435                 /* double the buffer size */
436                 add = rules->buf_max;
437                 if (add < bytes * 8)
438                         add = bytes * 8;
439
440                 buf = realloc(rules->buf, rules->buf_max + add);
441                 if (buf == NULL)
442                         return -1;
443                 dbg(rules->udev, "extend buffer from %zu to %zu\n", rules->buf_max, rules->buf_max + add);
444                 rules->buf = buf;
445                 rules->buf_max += add;
446         }
447         off = rules->buf_cur;
448         memcpy(&rules->buf[rules->buf_cur], str, bytes);
449         rules->buf_cur += bytes;
450         rules->buf_count++;
451         return off;
452 }
453
454 static int add_string(struct udev_rules *rules, const char *str)
455 {
456         unsigned int node_idx;
457         struct trie_node *new_node;
458         unsigned int new_node_idx;
459         unsigned char key;
460         unsigned short len;
461         unsigned int depth;
462         unsigned int off;
463         struct trie_node *parent;
464
465         /* walk trie, start from last character of str to find matching tails */
466         len = strlen(str);
467         key = str[len-1];
468         node_idx = 0;
469         for (depth = 0; depth <= len; depth++) {
470                 struct trie_node *node;
471                 unsigned int child_idx;
472
473                 node = &rules->trie_nodes[node_idx];
474                 off = node->value_off + node->value_len - len;
475
476                 /* match against current node */
477                 if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
478                         return off;
479
480                 /* lookup child node */
481                 key = str[len - 1 - depth];
482                 child_idx = node->child_idx;
483                 while (child_idx > 0) {
484                         struct trie_node *child;
485
486                         child = &rules->trie_nodes[child_idx];
487                         if (child->key == key)
488                                 break;
489                         child_idx = child->next_child_idx;
490                 }
491                 if (child_idx == 0)
492                         break;
493                 node_idx = child_idx;
494         }
495
496         /* string not found, add it */
497         off = add_new_string(rules, str, len + 1);
498
499         /* grow trie nodes if needed */
500         if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
501                 struct trie_node *nodes;
502                 unsigned int add;
503
504                 /* double the buffer size */
505                 add = rules->trie_nodes_max;
506                 if (add < 8)
507                         add = 8;
508
509                 nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
510                 if (nodes == NULL)
511                         return -1;
512                 dbg(rules->udev, "extend trie nodes from %u to %u\n",
513                     rules->trie_nodes_max, rules->trie_nodes_max + add);
514                 rules->trie_nodes = nodes;
515                 rules->trie_nodes_max += add;
516         }
517
518         /* get a new node */
519         new_node_idx = rules->trie_nodes_cur;
520         rules->trie_nodes_cur++;
521         new_node = &rules->trie_nodes[new_node_idx];
522         memset(new_node, 0x00, sizeof(struct trie_node));
523         new_node->value_off = off;
524         new_node->value_len = len;
525         new_node->key = key;
526
527         /* join the parent's child list */
528         parent = &rules->trie_nodes[node_idx];
529         if (parent->child_idx == 0) {
530                 parent->child_idx = new_node_idx;
531         } else {
532                 struct trie_node *last_child;
533
534                 last_child = &rules->trie_nodes[parent->last_child_idx];
535                 last_child->next_child_idx = new_node_idx;
536         }
537         parent->last_child_idx = new_node_idx;
538         return off;
539 }
540
541 static int add_token(struct udev_rules *rules, struct token *token)
542 {
543         /* grow buffer if needed */
544         if (rules->token_cur+1 >= rules->token_max) {
545                 struct token *tokens;
546                 unsigned int add;
547
548                 /* double the buffer size */
549                 add = rules->token_max;
550                 if (add < 8)
551                         add = 8;
552
553                 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
554                 if (tokens == NULL)
555                         return -1;
556                 dbg(rules->udev, "extend tokens from %u to %u\n", rules->token_max, rules->token_max + add);
557                 rules->tokens = tokens;
558                 rules->token_max += add;
559         }
560         memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
561         rules->token_cur++;
562         return 0;
563 }
564
565 static uid_t add_uid(struct udev_rules *rules, const char *owner)
566 {
567         unsigned int i;
568         uid_t uid;
569         unsigned int off;
570
571         /* lookup, if we know it already */
572         for (i = 0; i < rules->uids_cur; i++) {
573                 off = rules->uids[i].name_off;
574                 if (strcmp(&rules->buf[off], owner) == 0) {
575                         uid = rules->uids[i].uid;
576                         dbg(rules->udev, "return existing %u for '%s'\n", uid, owner);
577                         return uid;
578                 }
579         }
580         uid = util_lookup_user(rules->udev, owner);
581
582         /* grow buffer if needed */
583         if (rules->uids_cur+1 >= rules->uids_max) {
584                 struct uid_gid *uids;
585                 unsigned int add;
586
587                 /* double the buffer size */
588                 add = rules->uids_max;
589                 if (add < 1)
590                         add = 8;
591
592                 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
593                 if (uids == NULL)
594                         return uid;
595                 dbg(rules->udev, "extend uids from %u to %u\n", rules->uids_max, rules->uids_max + add);
596                 rules->uids = uids;
597                 rules->uids_max += add;
598         }
599         rules->uids[rules->uids_cur].uid = uid;
600         off = add_string(rules, owner);
601         if (off <= 0)
602                 return uid;
603         rules->uids[rules->uids_cur].name_off = off;
604         rules->uids_cur++;
605         return uid;
606 }
607
608 static gid_t add_gid(struct udev_rules *rules, const char *group)
609 {
610         unsigned int i;
611         gid_t gid;
612         unsigned int off;
613
614         /* lookup, if we know it already */
615         for (i = 0; i < rules->gids_cur; i++) {
616                 off = rules->gids[i].name_off;
617                 if (strcmp(&rules->buf[off], group) == 0) {
618                         gid = rules->gids[i].gid;
619                         dbg(rules->udev, "return existing %u for '%s'\n", gid, group);
620                         return gid;
621                 }
622         }
623         gid = util_lookup_group(rules->udev, group);
624
625         /* grow buffer if needed */
626         if (rules->gids_cur+1 >= rules->gids_max) {
627                 struct uid_gid *gids;
628                 unsigned int add;
629
630                 /* double the buffer size */
631                 add = rules->gids_max;
632                 if (add < 1)
633                         add = 8;
634
635                 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
636                 if (gids == NULL)
637                         return gid;
638                 dbg(rules->udev, "extend gids from %u to %u\n", rules->gids_max, rules->gids_max + add);
639                 rules->gids = gids;
640                 rules->gids_max += add;
641         }
642         rules->gids[rules->gids_cur].gid = gid;
643         off = add_string(rules, group);
644         if (off <= 0)
645                 return gid;
646         rules->gids[rules->gids_cur].name_off = off;
647         rules->gids_cur++;
648         return gid;
649 }
650
651 static int import_property_from_string(struct udev_device *dev, char *line)
652 {
653         struct udev *udev = udev_device_get_udev(dev);
654         char *key;
655         char *val;
656         size_t len;
657
658         /* find key */
659         key = line;
660         while (isspace(key[0]))
661                 key++;
662
663         /* comment or empty line */
664         if (key[0] == '#' || key[0] == '\0')
665                 return -1;
666
667         /* split key/value */
668         val = strchr(key, '=');
669         if (val == NULL)
670                 return -1;
671         val[0] = '\0';
672         val++;
673
674         /* find value */
675         while (isspace(val[0]))
676                 val++;
677
678         /* terminate key */
679         len = strlen(key);
680         if (len == 0)
681                 return -1;
682         while (isspace(key[len-1]))
683                 len--;
684         key[len] = '\0';
685
686         /* terminate value */
687         len = strlen(val);
688         if (len == 0)
689                 return -1;
690         while (isspace(val[len-1]))
691                 len--;
692         val[len] = '\0';
693
694         if (len == 0)
695                 return -1;
696
697         /* unquote */
698         if (val[0] == '"' || val[0] == '\'') {
699                 if (val[len-1] != val[0]) {
700                         info(udev, "inconsistent quoting: '%s', skip\n", line);
701                         return -1;
702                 }
703                 val[len-1] = '\0';
704                 val++;
705         }
706
707         dbg(udev, "adding '%s'='%s'\n", key, val);
708
709         /* handle device, renamed by external tool, returning new path */
710         if (strcmp(key, "DEVPATH") == 0) {
711                 char syspath[UTIL_PATH_SIZE];
712
713                 info(udev, "updating devpath from '%s' to '%s'\n",
714                      udev_device_get_devpath(dev), val);
715                 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), val, NULL);
716                 udev_device_set_syspath(dev, syspath);
717         } else {
718                 struct udev_list_entry *entry;
719
720                 entry = udev_device_add_property(dev, key, val);
721                 /* store in db */
722                 udev_list_entry_set_flag(entry, 1);
723         }
724         return 0;
725 }
726
727 static int import_file_into_properties(struct udev_device *dev, const char *filename)
728 {
729         FILE *f;
730         char line[UTIL_LINE_SIZE];
731
732         f = fopen(filename, "r");
733         if (f == NULL)
734                 return -1;
735         while (fgets(line, sizeof(line), f) != NULL)
736                 import_property_from_string(dev, line);
737         fclose(f);
738         return 0;
739 }
740
741 static int import_program_into_properties(struct udev_device *dev, const char *program)
742 {
743         struct udev *udev = udev_device_get_udev(dev);
744         char **envp;
745         char result[4096];
746         size_t reslen;
747         char *line;
748
749         envp = udev_device_get_properties_envp(dev);
750         if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
751                 return -1;
752
753         line = result;
754         while (line != NULL) {
755                 char *pos;
756
757                 pos = strchr(line, '\n');
758                 if (pos != NULL) {
759                         pos[0] = '\0';
760                         pos = &pos[1];
761                 }
762                 import_property_from_string(dev, line);
763                 line = pos;
764         }
765         return 0;
766 }
767
768 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
769 {
770         struct udev *udev = udev_device_get_udev(dev);
771         struct udev_device *dev_parent;
772         struct udev_list_entry *list_entry;
773
774         dev_parent = udev_device_get_parent(dev);
775         if (dev_parent == NULL)
776                 return -1;
777
778         dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
779         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
780                 const char *key = udev_list_entry_get_name(list_entry);
781                 const char *val = udev_list_entry_get_value(list_entry);
782
783                 if (fnmatch(filter, key, 0) == 0) {
784                         struct udev_list_entry *entry;
785
786                         dbg(udev, "import key '%s=%s'\n", key, val);
787                         entry = udev_device_add_property(dev, key, val);
788                         /* store in db */
789                         udev_list_entry_set_flag(entry, 1);
790                 }
791         }
792         return 0;
793 }
794
795 #define WAIT_LOOP_PER_SECOND            50
796 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
797 {
798         struct udev *udev = udev_device_get_udev(dev);
799         char filepath[UTIL_PATH_SIZE];
800         char devicepath[UTIL_PATH_SIZE];
801         struct stat stats;
802         int loop = timeout * WAIT_LOOP_PER_SECOND;
803
804         /* a relative path is a device attribute */
805         devicepath[0] = '\0';
806         if (file[0] != '/') {
807                 util_strscpyl(devicepath, sizeof(devicepath),
808                               udev_get_sys_path(udev), udev_device_get_devpath(dev), NULL);
809                 util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
810                 file = filepath;
811         }
812
813         dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
814         while (--loop) {
815                 /* lookup file */
816                 if (stat(file, &stats) == 0) {
817                         info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
818                         return 0;
819                 }
820                 /* make sure, the device did not disappear in the meantime */
821                 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
822                         info(udev, "device disappeared while waiting for '%s'\n", file);
823                         return -2;
824                 }
825                 info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
826                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
827         }
828         info(udev, "waiting for '%s' failed\n", file);
829         return -1;
830 }
831
832 static int attr_subst_subdir(char *attr, size_t len)
833 {
834         int found = 0;
835
836         if (strstr(attr, "/*/")) {
837                 char *pos;
838                 char dirname[UTIL_PATH_SIZE];
839                 const char *tail;
840                 DIR *dir;
841
842                 util_strscpy(dirname, sizeof(dirname), attr);
843                 pos = strstr(dirname, "/*/");
844                 if (pos == NULL)
845                         return -1;
846                 pos[0] = '\0';
847                 tail = &pos[2];
848                 dir = opendir(dirname);
849                 if (dir != NULL) {
850                         struct dirent *dent;
851
852                         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
853                                 struct stat stats;
854
855                                 if (dent->d_name[0] == '.')
856                                         continue;
857                                 util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
858                                 if (stat(attr, &stats) == 0) {
859                                         found = 1;
860                                         break;
861                                 }
862                         }
863                         closedir(dir);
864                 }
865         }
866
867         return found;
868 }
869
870 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
871 {
872         char *linepos;
873         char *temp;
874
875         linepos = *line;
876         if (linepos == NULL && linepos[0] == '\0')
877                 return -1;
878
879         /* skip whitespace */
880         while (isspace(linepos[0]) || linepos[0] == ',')
881                 linepos++;
882
883         /* get the key */
884         if (linepos[0] == '\0')
885                 return -1;
886         *key = linepos;
887
888         while (1) {
889                 linepos++;
890                 if (linepos[0] == '\0')
891                         return -1;
892                 if (isspace(linepos[0]))
893                         break;
894                 if (linepos[0] == '=')
895                         break;
896                 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
897                         if (linepos[1] == '=')
898                                 break;
899         }
900
901         /* remember end of key */
902         temp = linepos;
903
904         /* skip whitespace after key */
905         while (isspace(linepos[0]))
906                 linepos++;
907         if (linepos[0] == '\0')
908                 return -1;
909
910         /* get operation type */
911         if (linepos[0] == '=' && linepos[1] == '=') {
912                 *op = OP_MATCH;
913                 linepos += 2;
914         } else if (linepos[0] == '!' && linepos[1] == '=') {
915                 *op = OP_NOMATCH;
916                 linepos += 2;
917         } else if (linepos[0] == '+' && linepos[1] == '=') {
918                 *op = OP_ADD;
919                 linepos += 2;
920         } else if (linepos[0] == '=') {
921                 *op = OP_ASSIGN;
922                 linepos++;
923         } else if (linepos[0] == ':' && linepos[1] == '=') {
924                 *op = OP_ASSIGN_FINAL;
925                 linepos += 2;
926         } else
927                 return -1;
928
929         /* terminate key */
930         temp[0] = '\0';
931
932         /* skip whitespace after operator */
933         while (isspace(linepos[0]))
934                 linepos++;
935         if (linepos[0] == '\0')
936                 return -1;
937
938         /* get the value */
939         if (linepos[0] == '"')
940                 linepos++;
941         else
942                 return -1;
943         *value = linepos;
944
945         /* terminate */
946         temp = strchr(linepos, '"');
947         if (!temp)
948                 return -1;
949         temp[0] = '\0';
950         temp++;
951         dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value);
952
953         /* move line to next key */
954         *line = temp;
955         return 0;
956 }
957
958 /* extract possible KEY{attr} */
959 static char *get_key_attribute(struct udev *udev, char *str)
960 {
961         char *pos;
962         char *attr;
963
964         attr = strchr(str, '{');
965         if (attr != NULL) {
966                 attr++;
967                 pos = strchr(attr, '}');
968                 if (pos == NULL) {
969                         err(udev, "missing closing brace for format\n");
970                         return NULL;
971                 }
972                 pos[0] = '\0';
973                 dbg(udev, "attribute='%s'\n", attr);
974                 return attr;
975         }
976         return NULL;
977 }
978
979 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
980                         enum operation_type op,
981                         const char *value, const void *data)
982 {
983         struct token *token = &rule_tmp->token[rule_tmp->token_cur];
984         const char *attr = NULL;
985
986         memset(token, 0x00, sizeof(struct token));
987
988         switch (type) {
989         case TK_M_ACTION:
990         case TK_M_DEVPATH:
991         case TK_M_KERNEL:
992         case TK_M_SUBSYSTEM:
993         case TK_M_DRIVER:
994         case TK_M_WAITFOR:
995         case TK_M_DEVLINK:
996         case TK_M_NAME:
997         case TK_M_KERNELS:
998         case TK_M_SUBSYSTEMS:
999         case TK_M_DRIVERS:
1000         case TK_M_PROGRAM:
1001         case TK_M_IMPORT_FILE:
1002         case TK_M_IMPORT_PROG:
1003         case TK_M_IMPORT_PARENT:
1004         case TK_M_RESULT:
1005         case TK_A_OWNER:
1006         case TK_A_GROUP:
1007         case TK_A_MODE:
1008         case TK_A_NAME:
1009         case TK_A_DEVLINK:
1010         case TK_A_GOTO:
1011                 token->key.value_off = add_string(rule_tmp->rules, value);
1012                 break;
1013         case TK_M_ENV:
1014         case TK_M_ATTR:
1015         case TK_M_ATTRS:
1016         case TK_A_ATTR:
1017         case TK_A_ENV:
1018                 attr = data;
1019                 token->key.value_off = add_string(rule_tmp->rules, value);
1020                 token->key.attr_off = add_string(rule_tmp->rules, attr);
1021                 break;
1022         case TK_M_TEST:
1023                 token->key.value_off = add_string(rule_tmp->rules, value);
1024                 if (data != NULL)
1025                         token->key.mode = *(mode_t *)data;
1026                 break;
1027         case TK_A_IGNORE_DEVICE:
1028         case TK_A_STRING_ESCAPE_NONE:
1029         case TK_A_STRING_ESCAPE_REPLACE:
1030         case TK_A_IGNORE_REMOVE:
1031         case TK_A_LAST_RULE:
1032                 break;
1033         case TK_A_RUN:
1034                 token->key.value_off = add_string(rule_tmp->rules, value);
1035                 token->key.ignore_error = *(int *)data;
1036                 break;
1037         case TK_A_INOTIFY_WATCH:
1038         case TK_A_NUM_FAKE_PART:
1039         case TK_A_DEVLINK_PRIO:
1040                 token->key.devlink_prio = *(int *)data;
1041                 break;
1042         case TK_A_OWNER_ID:
1043                 token->key.uid = *(uid_t *)data;
1044                 break;
1045         case TK_A_GROUP_ID:
1046                 token->key.gid = *(gid_t *)data;
1047                 break;
1048         case TK_A_MODE_ID:
1049                 token->key.mode = *(mode_t *)data;
1050                 break;
1051         case TK_A_EVENT_TIMEOUT:
1052                 token->key.event_timeout = *(int *)data;
1053                 break;
1054         case TK_RULE:
1055         case TK_M_PARENTS_MIN:
1056         case TK_M_PARENTS_MAX:
1057         case TK_M_MAX:
1058         case TK_END:
1059         case TK_UNSET:
1060                 err(rule_tmp->rules->udev, "wrong type %u\n", type);
1061                 return -1;
1062         }
1063
1064         if (value != NULL && type < TK_M_MAX) {
1065                 /* check if we need to split or call fnmatch() while matching rules */
1066                 enum string_glob_type glob;
1067                 int has_split;
1068                 int has_glob;
1069
1070                 has_split = (strchr(value, '|') != NULL);
1071                 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
1072                 if (has_split && has_glob) {
1073                         glob = GL_SPLIT_GLOB;
1074                 } else if (has_split) {
1075                         glob = GL_SPLIT;
1076                 } else if (has_glob) {
1077                         if (strcmp(value, "?*") == 0)
1078                                 glob = GL_SOMETHING;
1079                         else
1080                                 glob = GL_GLOB;
1081                 } else {
1082                         glob = GL_PLAIN;
1083                 }
1084                 token->key.glob = glob;
1085         }
1086
1087         if (value != NULL && type > TK_M_MAX) {
1088                 /* check if assigned value has substitution chars */
1089                 if (value[0] == '[')
1090                         token->key.subst = SB_SUBSYS;
1091                 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1092                         token->key.subst = SB_FORMAT;
1093                 else
1094                         token->key.subst = SB_NONE;
1095         }
1096
1097         if (attr != NULL) {
1098                 /* check if property/attribut name has substitution chars */
1099                 if (attr[0] == '[')
1100                         token->key.attrsubst = SB_SUBSYS;
1101                 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
1102                         token->key.attrsubst = SB_FORMAT;
1103                 else
1104                         token->key.attrsubst = SB_NONE;
1105         }
1106
1107         token->key.type = type;
1108         token->key.op = op;
1109         rule_tmp->token_cur++;
1110         if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
1111                 err(rule_tmp->rules->udev, "temporary rule array too small\n");
1112                 return -1;
1113         }
1114         return 0;
1115 }
1116
1117 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1118 {
1119         unsigned int i;
1120         unsigned int start = 0;
1121         unsigned int end = rule_tmp->token_cur;
1122
1123         for (i = 0; i < rule_tmp->token_cur; i++) {
1124                 enum token_type next_val = TK_UNSET;
1125                 unsigned int next_idx;
1126                 unsigned int j;
1127
1128                 /* find smallest value */
1129                 for (j = start; j < end; j++) {
1130                         if (rule_tmp->token[j].type == TK_UNSET)
1131                                 continue;
1132                         if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1133                                 next_val = rule_tmp->token[j].type;
1134                                 next_idx = j;
1135                         }
1136                 }
1137
1138                 /* add token and mark done */
1139                 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1140                         return -1;
1141                 rule_tmp->token[next_idx].type = TK_UNSET;
1142
1143                 /* shrink range */
1144                 if (next_idx == start)
1145                         start++;
1146                 if (next_idx+1 == end)
1147                         end--;
1148         }
1149         return 0;
1150 }
1151
1152 static int add_rule(struct udev_rules *rules, char *line,
1153                     const char *filename, unsigned int filename_off, unsigned int lineno)
1154 {
1155         char *linepos;
1156         char *attr;
1157         struct rule_tmp rule_tmp;
1158
1159         memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1160         rule_tmp.rules = rules;
1161         rule_tmp.rule.type = TK_RULE;
1162         rule_tmp.rule.rule.filename_off = filename_off;
1163         rule_tmp.rule.rule.filename_line = lineno;
1164
1165         linepos = line;
1166         while (1) {
1167                 char *key;
1168                 char *value;
1169                 enum operation_type op;
1170
1171                 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1172                         break;
1173
1174                 if (strcmp(key, "ACTION") == 0) {
1175                         if (op > OP_MATCH_MAX) {
1176                                 err(rules->udev, "invalid ACTION operation\n");
1177                                 goto invalid;
1178                         }
1179                         rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1180                         continue;
1181                 }
1182
1183                 if (strcmp(key, "DEVPATH") == 0) {
1184                         if (op > OP_MATCH_MAX) {
1185                                 err(rules->udev, "invalid DEVPATH operation\n");
1186                                 goto invalid;
1187                         }
1188                         rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1189                         continue;
1190                 }
1191
1192                 if (strcmp(key, "KERNEL") == 0) {
1193                         if (op > OP_MATCH_MAX) {
1194                                 err(rules->udev, "invalid KERNEL operation\n");
1195                                 goto invalid;
1196                         }
1197                         rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1198                         continue;
1199                 }
1200
1201                 if (strcmp(key, "SUBSYSTEM") == 0) {
1202                         if (op > OP_MATCH_MAX) {
1203                                 err(rules->udev, "invalid SUBSYSTEM operation\n");
1204                                 goto invalid;
1205                         }
1206                         /* bus, class, subsystem events should all be the same */
1207                         if (strcmp(value, "subsystem") == 0 ||
1208                             strcmp(value, "bus") == 0 ||
1209                             strcmp(value, "class") == 0) {
1210                                 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1211                                         err(rules->udev, "'%s' must be specified as 'subsystem' \n"
1212                                             "please fix it in %s:%u", value, filename, lineno);
1213                                 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1214                         } else
1215                                 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1216                         continue;
1217                 }
1218
1219                 if (strcmp(key, "DRIVER") == 0) {
1220                         if (op > OP_MATCH_MAX) {
1221                                 err(rules->udev, "invalid DRIVER operation\n");
1222                                 goto invalid;
1223                         }
1224                         rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1225                         continue;
1226                 }
1227
1228                 if (strncmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1229                         attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1230                         if (attr == NULL) {
1231                                 err(rules->udev, "error parsing ATTR attribute\n");
1232                                 goto invalid;
1233                         }
1234                         if (op < OP_MATCH_MAX) {
1235                                 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1236                         } else {
1237                                 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1238                         }
1239                         continue;
1240                 }
1241
1242                 if (strcmp(key, "KERNELS") == 0 ||
1243                     strcmp(key, "ID") == 0) {
1244                         if (op > OP_MATCH_MAX) {
1245                                 err(rules->udev, "invalid KERNELS operation\n");
1246                                 goto invalid;
1247                         }
1248                         rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1249                         continue;
1250                 }
1251
1252                 if (strcmp(key, "SUBSYSTEMS") == 0 ||
1253                     strcmp(key, "BUS") == 0) {
1254                         if (op > OP_MATCH_MAX) {
1255                                 err(rules->udev, "invalid SUBSYSTEMS operation\n");
1256                                 goto invalid;
1257                         }
1258                         rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1259                         continue;
1260                 }
1261
1262                 if (strcmp(key, "DRIVERS") == 0) {
1263                         if (op > OP_MATCH_MAX) {
1264                                 err(rules->udev, "invalid DRIVERS operation\n");
1265                                 goto invalid;
1266                         }
1267                         rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1268                         continue;
1269                 }
1270
1271                 if (strncmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1272                     strncmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1273                         if (op > OP_MATCH_MAX) {
1274                                 err(rules->udev, "invalid ATTRS operation\n");
1275                                 goto invalid;
1276                         }
1277                         attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1278                         if (attr == NULL) {
1279                                 err(rules->udev, "error parsing ATTRS attribute\n");
1280                                 goto invalid;
1281                         }
1282                         if (strncmp(attr, "device/", 7) == 0)
1283                                 err(rules->udev, "the 'device' link may not be available in a future kernel, "
1284                                     "please fix it in %s:%u", filename, lineno);
1285                         else if (strstr(attr, "../") != NULL)
1286                                 err(rules->udev, "do not reference parent sysfs directories directly, "
1287                                     "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1288                         rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1289                         continue;
1290                 }
1291
1292                 if (strncmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1293                         attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1294                         if (attr == NULL) {
1295                                 err(rules->udev, "error parsing ENV attribute\n");
1296                                 goto invalid;
1297                         }
1298                         if (op < OP_MATCH_MAX) {
1299                                 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1300                                         goto invalid;
1301                         } else {
1302                                 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1303                                         goto invalid;
1304                         }
1305                         continue;
1306                 }
1307
1308                 if (strcmp(key, "PROGRAM") == 0) {
1309                         rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1310                         continue;
1311                 }
1312
1313                 if (strcmp(key, "RESULT") == 0) {
1314                         if (op > OP_MATCH_MAX) {
1315                                 err(rules->udev, "invalid RESULT operation\n");
1316                                 goto invalid;
1317                         }
1318                         rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1319                         continue;
1320                 }
1321
1322                 if (strncmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1323                         attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1324                         if (attr != NULL && strstr(attr, "program")) {
1325                                 dbg(rules->udev, "IMPORT will be executed\n");
1326                                 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1327                         } else if (attr != NULL && strstr(attr, "file")) {
1328                                 dbg(rules->udev, "IMPORT will be included as file\n");
1329                                 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1330                         } else if (attr != NULL && strstr(attr, "parent")) {
1331                                 dbg(rules->udev, "IMPORT will include the parent values\n");
1332                                 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1333                         } else {
1334                                 /* figure it out if it is executable */
1335                                 char file[UTIL_PATH_SIZE];
1336                                 char *pos;
1337                                 struct stat statbuf;
1338
1339                                 /* allow programs in /lib/udev called without the path */
1340                                 if (value[0] != '/')
1341                                         util_strscpyl(file, sizeof(file), UDEV_PREFIX "/lib/udev/", value, NULL);
1342                                 else
1343                                         util_strscpy(file, sizeof(file), value);
1344                                 pos = strchr(file, ' ');
1345                                 if (pos)
1346                                         pos[0] = '\0';
1347                                 dbg(rules->udev, "IMPORT auto mode for '%s'\n", file);
1348                                 if (stat(file, &statbuf) == 0 && (statbuf.st_mode & S_IXUSR)) {
1349                                         dbg(rules->udev, "IMPORT will be executed (autotype)\n");
1350                                         rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1351                                 } else {
1352                                         dbg(rules->udev, "IMPORT will be included as file (autotype)\n");
1353                                         rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1354                                 }
1355                         }
1356                         continue;
1357                 }
1358
1359                 if (strncmp(key, "TEST", sizeof("TEST")-1) == 0) {
1360                         mode_t mode = 0;
1361
1362                         if (op > OP_MATCH_MAX) {
1363                                 err(rules->udev, "invalid TEST operation\n");
1364                                 goto invalid;
1365                         }
1366                         attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1367                         if (attr != NULL) {
1368                                 mode = strtol(attr, NULL, 8);
1369                                 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1370                         } else {
1371                                 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1372                         }
1373                         continue;
1374                 }
1375
1376                 if (strncmp(key, "RUN", sizeof("RUN")-1) == 0) {
1377                         int flag = 0;
1378
1379                         attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1380                         if (attr != NULL && strstr(attr, "ignore_error"))
1381                                 flag = 1;
1382                         rule_add_key(&rule_tmp, TK_A_RUN, op, value, &flag);
1383                         continue;
1384                 }
1385
1386                 if (strcmp(key, "WAIT_FOR") == 0 || strcmp(key, "WAIT_FOR_SYSFS") == 0) {
1387                         rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1388                         continue;
1389                 }
1390
1391                 if (strcmp(key, "LABEL") == 0) {
1392                         rule_tmp.rule.rule.label_off = add_string(rules, value);
1393                         continue;
1394                 }
1395
1396                 if (strcmp(key, "GOTO") == 0) {
1397                         rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1398                         continue;
1399                 }
1400
1401                 if (strncmp(key, "NAME", sizeof("NAME")-1) == 0) {
1402                         if (op < OP_MATCH_MAX) {
1403                                 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1404                         } else {
1405                                 if (value[0] == '\0')
1406                                         info(rules->udev, "name empty, node creation suppressed\n");
1407                                 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1408                                 attr = get_key_attribute(rules->udev, key + sizeof("NAME")-1);
1409                                 if (attr != NULL) {
1410                                         if (strstr(attr, "all_partitions") != NULL) {
1411                                                 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1412
1413                                                 dbg(rules->udev, "creation of partition nodes requested\n");
1414                                                 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1415                                         }
1416                                         if (strstr(attr, "ignore_remove") != NULL) {
1417                                                 dbg(rules->udev, "remove event should be ignored\n");
1418                                                 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1419                                         }
1420                                 }
1421                         }
1422                         rule_tmp.rule.rule.flags = 1;
1423                         continue;
1424                 }
1425
1426                 if (strcmp(key, "SYMLINK") == 0) {
1427                         if (op < OP_MATCH_MAX)
1428                                 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1429                         else
1430                                 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1431                         rule_tmp.rule.rule.flags = 1;
1432                         continue;
1433                 }
1434
1435                 if (strcmp(key, "OWNER") == 0) {
1436                         uid_t uid;
1437                         char *endptr;
1438
1439                         uid = strtoul(value, &endptr, 10);
1440                         if (endptr[0] == '\0') {
1441                                 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1442                         } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1443                                 uid = add_uid(rules, value);
1444                                 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1445                         } else if (rules->resolve_names >= 0) {
1446                                 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1447                         }
1448                         rule_tmp.rule.rule.flags = 1;
1449                         continue;
1450                 }
1451
1452                 if (strcmp(key, "GROUP") == 0) {
1453                         gid_t gid;
1454                         char *endptr;
1455
1456                         gid = strtoul(value, &endptr, 10);
1457                         if (endptr[0] == '\0') {
1458                                 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1459                         } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1460                                 gid = add_gid(rules, value);
1461                                 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1462                         } else if (rules->resolve_names >= 0) {
1463                                 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1464                         }
1465                         rule_tmp.rule.rule.flags = 1;
1466                         continue;
1467                 }
1468
1469                 if (strcmp(key, "MODE") == 0) {
1470                         mode_t mode;
1471                         char *endptr;
1472
1473                         mode = strtol(value, &endptr, 8);
1474                         if (endptr[0] == '\0')
1475                                 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1476                         else
1477                                 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1478                         rule_tmp.rule.rule.flags = 1;
1479                         continue;
1480                 }
1481
1482                 if (strcmp(key, "OPTIONS") == 0) {
1483                         const char *pos;
1484
1485                         if (strstr(value, "last_rule") != NULL) {
1486                                 dbg(rules->udev, "last rule to be applied\n");
1487                                 rule_add_key(&rule_tmp, TK_A_LAST_RULE, 0, NULL, NULL);
1488                         }
1489                         if (strstr(value, "ignore_device") != NULL) {
1490                                 dbg(rules->udev, "device should be ignored\n");
1491                                 rule_add_key(&rule_tmp, TK_A_IGNORE_DEVICE, 0, NULL, NULL);
1492                         }
1493                         if (strstr(value, "ignore_remove") != NULL) {
1494                                 dbg(rules->udev, "remove event should be ignored\n");
1495                                 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1496                         }
1497                         pos = strstr(value, "link_priority=");
1498                         if (pos != NULL) {
1499                                 int prio = atoi(&pos[strlen("link_priority=")]);
1500
1501                                 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, 0, NULL, &prio);
1502                                 dbg(rules->udev, "link priority=%i\n", prio);
1503                         }
1504                         pos = strstr(value, "event_timeout=");
1505                         if (pos != NULL) {
1506                                 int tout = atoi(&pos[strlen("event_timeout=")]);
1507
1508                                 rule_add_key(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout);
1509                                 dbg(rules->udev, "event timout=%i\n", tout);
1510                         }
1511                         pos = strstr(value, "string_escape=");
1512                         if (pos != NULL) {
1513                                 pos = &pos[strlen("string_escape=")];
1514                                 if (strncmp(pos, "none", strlen("none")) == 0)
1515                                         rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, 0, NULL, NULL);
1516                                 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1517                                         rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, 0, NULL, NULL);
1518                         }
1519                         if (strstr(value, "all_partitions") != NULL) {
1520                                 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1521
1522                                 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1523                                 dbg(rules->udev, "creation of partition nodes requested\n");
1524                         }
1525                         pos = strstr(value, "nowatch");
1526                         if (pos != NULL) {
1527                                 const int off = 0;
1528
1529                                 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, 0, NULL, &off);
1530                                 dbg(rules->udev, "inotify watch of device disabled\n");
1531                         } else {
1532                                 pos = strstr(value, "watch");
1533                                 if (pos != NULL) {
1534                                         const int on = 1;
1535
1536                                         rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, 0, NULL, &on);
1537                                         dbg(rules->udev, "inotify watch of device requested\n");
1538                                 }
1539                         }
1540                         continue;
1541                 }
1542                 err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
1543         }
1544
1545         /* add rule token */
1546         rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1547         if (add_token(rules, &rule_tmp.rule) != 0)
1548                 goto invalid;
1549
1550         /* add tokens to list, sorted by type */
1551         if (sort_token(rules, &rule_tmp) != 0)
1552                 goto invalid;
1553
1554         return 0;
1555 invalid:
1556         err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
1557         return -1;
1558 }
1559
1560 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
1561 {
1562         FILE *f;
1563         unsigned int first_token;
1564         char line[UTIL_LINE_SIZE];
1565         int line_nr = 0;
1566         unsigned int i;
1567
1568         info(rules->udev, "reading '%s' as rules file\n", filename);
1569
1570         f = fopen(filename, "r");
1571         if (f == NULL)
1572                 return -1;
1573
1574         first_token = rules->token_cur;
1575
1576         while(fgets(line, sizeof(line), f) != NULL) {
1577                 char *key;
1578                 size_t len;
1579
1580                 /* skip whitespace */
1581                 line_nr++;
1582                 key = line;
1583                 while (isspace(key[0]))
1584                         key++;
1585
1586                 /* comment */
1587                 if (key[0] == '#')
1588                         continue;
1589
1590                 len = strlen(line);
1591                 if (len < 3)
1592                         continue;
1593
1594                 /* continue reading if backslash+newline is found */
1595                 while (line[len-2] == '\\') {
1596                         if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1597                                 break;
1598                         line_nr++;
1599                         len = strlen(line);
1600                 }
1601
1602                 if (len+1 >= sizeof(line)) {
1603                         err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr);
1604                         continue;
1605                 }
1606                 add_rule(rules, key, filename, filename_off, line_nr);
1607         }
1608         fclose(f);
1609
1610         /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1611         for (i = first_token+1; i < rules->token_cur; i++) {
1612                 if (rules->tokens[i].type == TK_A_GOTO) {
1613                         char *label = &rules->buf[rules->tokens[i].key.value_off];
1614                         unsigned int j;
1615
1616                         for (j = i+1; j < rules->token_cur; j++) {
1617                                 if (rules->tokens[j].type != TK_RULE)
1618                                         continue;
1619                                 if (rules->tokens[j].rule.label_off == 0)
1620                                         continue;
1621                                 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1622                                         continue;
1623                                 rules->tokens[i].key.rule_goto = j;
1624                                 break;
1625                         }
1626                         if (rules->tokens[i].key.rule_goto == 0)
1627                                 err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename);
1628                 }
1629         }
1630         return 0;
1631 }
1632
1633 static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix)
1634 {
1635         DIR *dir;
1636         char filename[UTIL_PATH_SIZE];
1637
1638         dbg(udev, "open directory '%s'\n", dirname);
1639         dir = opendir(dirname);
1640         if (dir == NULL) {
1641                 info(udev, "unable to open '%s': %m\n", dirname);
1642                 return -1;
1643         }
1644
1645         while (1) {
1646                 struct dirent *dent;
1647
1648                 dent = readdir(dir);
1649                 if (dent == NULL || dent->d_name[0] == '\0')
1650                         break;
1651
1652                 if (dent->d_name[0] == '.')
1653                         continue;
1654
1655                 /* look for file matching with specified suffix */
1656                 if (suffix != NULL) {
1657                         const char *ext;
1658
1659                         ext = strrchr(dent->d_name, '.');
1660                         if (ext == NULL)
1661                                 continue;
1662                         if (strcmp(ext, suffix) != 0)
1663                                 continue;
1664                 }
1665                 util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL);
1666                 dbg(udev, "put file '%s' into list\n", filename);
1667                 udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
1668         }
1669
1670         closedir(dir);
1671         return 0;
1672 }
1673
1674 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1675 {
1676         struct udev_rules *rules;
1677         struct stat statbuf;
1678         struct udev_list_node file_list;
1679         struct udev_list_entry *file_loop, *file_tmp;
1680         struct token end_token;
1681
1682         rules = malloc(sizeof(struct udev_rules));
1683         if (rules == NULL)
1684                 return NULL;
1685         memset(rules, 0x00, sizeof(struct udev_rules));
1686         rules->udev = udev;
1687         rules->resolve_names = resolve_names;
1688         udev_list_init(&file_list);
1689
1690         /* init token array and string buffer */
1691         rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1692         if (rules->tokens == NULL)
1693                 return NULL;
1694         rules->token_max = PREALLOC_TOKEN;
1695
1696         rules->buf = malloc(PREALLOC_STRBUF);
1697         if (rules->buf == NULL)
1698                 return NULL;
1699         rules->buf_max = PREALLOC_STRBUF;
1700         /* offset 0 is always '\0' */
1701         rules->buf[0] = '\0';
1702         rules->buf_cur = 1;
1703         dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1704             rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1705
1706         rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1707         if (rules->trie_nodes == NULL)
1708                 return NULL;
1709         rules->trie_nodes_max = PREALLOC_TRIE;
1710         /* offset 0 is the trie root, with an empty string */
1711         memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
1712         rules->trie_nodes_cur = 1;
1713
1714         if (udev_get_rules_path(udev) != NULL) {
1715                 /* custom rules location for testing */
1716                 add_matching_files(udev, &file_list, udev_get_rules_path(udev), ".rules");
1717         } else {
1718                 char filename[PATH_MAX];
1719                 struct udev_list_node sort_list;
1720                 struct udev_list_entry *sort_loop, *sort_tmp;
1721
1722                 /* read user/custom rules */
1723                 add_matching_files(udev, &file_list, SYSCONFDIR "/udev/rules.d", ".rules");
1724
1725                 /* read dynamic/temporary rules */
1726                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
1727                 if (stat(filename, &statbuf) != 0) {
1728                         util_create_path(udev, filename);
1729                         udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1730                         mkdir(filename, 0755);
1731                         udev_selinux_resetfscreatecon(udev);
1732                 }
1733                 udev_list_init(&sort_list);
1734                 add_matching_files(udev, &sort_list, filename, ".rules");
1735
1736                 /* read default rules */
1737                 add_matching_files(udev, &sort_list, UDEV_PREFIX "/lib/udev/rules.d", ".rules");
1738
1739                 /* sort all rules files by basename into list of files */
1740                 udev_list_entry_foreach_safe(sort_loop, sort_tmp, udev_list_get_entry(&sort_list)) {
1741                         const char *sort_name = udev_list_entry_get_name(sort_loop);
1742                         const char *sort_base = strrchr(sort_name, '/');
1743
1744                         if (sort_base == NULL)
1745                                 continue;
1746                         /* sort entry into existing list */
1747                         udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1748                                 const char *file_name = udev_list_entry_get_name(file_loop);
1749                                 const char *file_base = strrchr(file_name, '/');
1750
1751                                 if (file_base == NULL)
1752                                         continue;
1753                                 if (strcmp(file_base, sort_base) == 0) {
1754                                         info(udev, "rule file basename '%s' already added, ignoring '%s'\n",
1755                                              file_name, sort_name);
1756                                         udev_list_entry_delete(sort_loop);
1757                                         sort_loop = NULL;
1758                                         break;
1759                                 }
1760                                 if (strcmp(file_base, sort_base) > 0) {
1761                                         /* found later file, insert before */
1762                                         udev_list_entry_remove(sort_loop);
1763                                         udev_list_entry_insert_before(sort_loop, file_loop);
1764                                         sort_loop = NULL;
1765                                         break;
1766                                 }
1767                         }
1768                         /* current file already handled */
1769                         if (sort_loop == NULL)
1770                                 continue;
1771                         /* no later file, append to end of list */
1772                         udev_list_entry_remove(sort_loop);
1773                         udev_list_entry_append(sort_loop, &file_list);
1774                 }
1775         }
1776
1777         /* add all filenames to the string buffer */
1778         udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1779                 const char *filename = udev_list_entry_get_name(file_loop);
1780                 unsigned int filename_off;
1781
1782                 filename_off = add_string(rules, filename);
1783                 /* the offset in the rule is limited to unsigned short */
1784                 if (filename_off < USHRT_MAX)
1785                         udev_list_entry_set_flag(file_loop, filename_off);
1786         }
1787
1788         /* parse list of files */
1789         udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1790                 const char *filename = udev_list_entry_get_name(file_loop);
1791                 unsigned int filename_off = udev_list_entry_get_flag(file_loop);
1792
1793                 if (stat(filename, &statbuf) == 0 && statbuf.st_size > 0)
1794                         parse_file(rules, filename, filename_off);
1795                 else
1796                         info(udev, "can not read '%s'\n", filename);
1797                 udev_list_entry_delete(file_loop);
1798         }
1799
1800         memset(&end_token, 0x00, sizeof(struct token));
1801         end_token.type = TK_END;
1802         add_token(rules, &end_token);
1803
1804         /* shrink allocated token and string buffer */
1805         if (rules->token_cur < rules->token_max) {
1806                 struct token *tokens;
1807
1808                 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1809                 if (tokens != NULL || rules->token_cur == 0) {
1810                         rules->tokens = tokens;
1811                         rules->token_max = rules->token_cur;
1812                 }
1813         }
1814         if (rules->buf_cur < rules->buf_max) {
1815                 char *buf;
1816
1817                 buf = realloc(rules->buf, rules->buf_cur);
1818                 if (buf != NULL || rules->buf_cur == 0) {
1819                         rules->buf = buf;
1820                         rules->buf_max = rules->buf_cur;
1821                 }
1822         }
1823         info(udev, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1824              rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1825         info(udev, "temporary index used %zu bytes (%u * %zu bytes)\n",
1826              rules->trie_nodes_cur * sizeof(struct trie_node),
1827              rules->trie_nodes_cur, sizeof(struct trie_node));
1828
1829         /* cleanup trie */
1830         free(rules->trie_nodes);
1831         rules->trie_nodes = NULL;
1832         rules->trie_nodes_cur = 0;
1833         rules->trie_nodes_max = 0;
1834
1835         /* cleanup uid/gid cache */
1836         free(rules->uids);
1837         rules->uids = NULL;
1838         rules->uids_cur = 0;
1839         rules->uids_max = 0;
1840         free(rules->gids);
1841         rules->gids = NULL;
1842         rules->gids_cur = 0;
1843         rules->gids_max = 0;
1844
1845         dump_rules(rules);
1846         return rules;
1847 }
1848
1849 void udev_rules_unref(struct udev_rules *rules)
1850 {
1851         if (rules == NULL)
1852                 return;
1853         free(rules->tokens);
1854         free(rules->buf);
1855         free(rules->trie_nodes);
1856         free(rules->uids);
1857         free(rules->gids);
1858         free(rules);
1859 }
1860
1861 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1862 {
1863         char *key_value = &rules->buf[token->key.value_off];
1864         char *pos;
1865         int match = 0;
1866
1867         if (val == NULL)
1868                 val = "";
1869
1870         switch (token->key.glob) {
1871         case GL_PLAIN:
1872                 match = (strcmp(key_value, val) == 0);
1873                 break;
1874         case GL_GLOB:
1875                 match = (fnmatch(key_value, val, 0) == 0);
1876                 break;
1877         case GL_SPLIT:
1878                 {
1879                         const char *split;
1880                         size_t len;
1881
1882                         split = &rules->buf[token->key.value_off];
1883                         len = strlen(val);
1884                         while (1) {
1885                                 const char *next;
1886
1887                                 next = strchr(split, '|');
1888                                 if (next != NULL) {
1889                                         size_t matchlen = (size_t)(next - split);
1890
1891                                         match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1892                                         if (match)
1893                                                 break;
1894                                 } else {
1895                                         match = (strcmp(split, val) == 0);
1896                                         break;
1897                                 }
1898                                 split = &next[1];
1899                         }
1900                         break;
1901                 }
1902         case GL_SPLIT_GLOB:
1903                 {
1904                         char value[UTIL_PATH_SIZE];
1905
1906                         util_strscpy(value, sizeof(value), &rules->buf[token->key.value_off]);
1907                         key_value = value;
1908                         while (key_value != NULL) {
1909                                 pos = strchr(key_value, '|');
1910                                 if (pos != NULL) {
1911                                         pos[0] = '\0';
1912                                         pos = &pos[1];
1913                                 }
1914                                 dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val);
1915                                 match = (fnmatch(key_value, val, 0) == 0);
1916                                 if (match)
1917                                         break;
1918                                 key_value = pos;
1919                         }
1920                         break;
1921                 }
1922         case GL_SOMETHING:
1923                 match = (val[0] != '\0');
1924                 break;
1925         case GL_UNSET:
1926                 return -1;
1927         }
1928
1929         if (match && (token->key.op == OP_MATCH)) {
1930                 dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type));
1931                 return 0;
1932         }
1933         if (!match && (token->key.op == OP_NOMATCH)) {
1934                 dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type));
1935                 return 0;
1936         }
1937         dbg(rules->udev, "%s is not true\n", token_str(token->type));
1938         return -1;
1939 }
1940
1941 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1942 {
1943         const char *name;
1944         char nbuf[UTIL_NAME_SIZE];
1945         const char *value;
1946         char vbuf[UTIL_NAME_SIZE];
1947         size_t len;
1948
1949         name = &rules->buf[cur->key.attr_off];
1950         switch (cur->key.attrsubst) {
1951         case SB_FORMAT:
1952                 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1953                 name = nbuf;
1954                 /* fall through */
1955         case SB_NONE:
1956                 value = udev_device_get_sysattr_value(dev, name);
1957                 if (value == NULL)
1958                         return -1;
1959                 break;
1960         case SB_SUBSYS:
1961                 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1962                         return -1;
1963                 value = vbuf;
1964                 break;
1965         default:
1966                 return -1;
1967         }
1968
1969         /* remove trailing whitespace, if not asked to match for it */
1970         len = strlen(value);
1971         if (len > 0 && isspace(value[len-1])) {
1972                 const char *key_value;
1973                 size_t klen;
1974
1975                 key_value = &rules->buf[cur->key.value_off];
1976                 klen = strlen(key_value);
1977                 if (klen > 0 && !isspace(key_value[klen-1])) {
1978                         if (value != vbuf) {
1979                                 util_strscpy(vbuf, sizeof(vbuf), value);
1980                                 value = vbuf;
1981                         }
1982                         while (len > 0 && isspace(vbuf[--len]))
1983                                 vbuf[len] = '\0';
1984                         dbg(rules->udev, "removed trailing whitespace from '%s'\n", value);
1985                 }
1986         }
1987
1988         return match_key(rules, cur, value);
1989 }
1990
1991 enum escape_type {
1992         ESCAPE_UNSET,
1993         ESCAPE_NONE,
1994         ESCAPE_REPLACE,
1995 };
1996
1997 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event)
1998 {
1999         struct token *cur;
2000         struct token *rule;
2001         enum escape_type esc = ESCAPE_UNSET;
2002         int can_set_name;
2003
2004         if (rules->tokens == NULL)
2005                 return -1;
2006
2007         can_set_name = ((strcmp(udev_device_get_action(event->dev), "add") == 0 ||
2008                          strcmp(udev_device_get_action(event->dev), "change") == 0) &&
2009                         (major(udev_device_get_devnum(event->dev)) > 0 ||
2010                          strcmp(udev_device_get_subsystem(event->dev), "net") == 0));
2011
2012         /* loop through token list, match, run actions or forward to next rule */
2013         cur = &rules->tokens[0];
2014         rule = cur;
2015         while (1) {
2016                 dump_token(rules, cur);
2017                 switch (cur->type) {
2018                 case TK_RULE:
2019                         /* current rule */
2020                         rule = cur;
2021                         /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2022                         if (!can_set_name && rule->rule.flags)
2023                                 goto nomatch;
2024                         esc = ESCAPE_UNSET;
2025                         break;
2026                 case TK_M_ACTION:
2027                         if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
2028                                 goto nomatch;
2029                         break;
2030                 case TK_M_DEVPATH:
2031                         if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
2032                                 goto nomatch;
2033                         break;
2034                 case TK_M_KERNEL:
2035                         if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
2036                                 goto nomatch;
2037                         break;
2038                 case TK_M_DEVLINK:
2039                         {
2040                                 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2041                                 struct udev_list_entry *list_entry;
2042                                 int match = 0;
2043
2044                                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2045                                         const char *devlink;
2046
2047                                         devlink =  &udev_list_entry_get_name(list_entry)[devlen];
2048                                         if (match_key(rules, cur, devlink) == 0) {
2049                                                 match = 1;
2050                                                 break;
2051                                         }
2052                                 }
2053                                 if (!match)
2054                                         goto nomatch;
2055                                 break;
2056                         }
2057                 case TK_M_NAME:
2058                         if (match_key(rules, cur, event->name) != 0)
2059                                 goto nomatch;
2060                         break;
2061                 case TK_M_ENV:
2062                         {
2063                                 const char *key_name = &rules->buf[cur->key.attr_off];
2064                                 const char *value;
2065
2066                                 value = udev_device_get_property_value(event->dev, key_name);
2067                                 if (value == NULL) {
2068                                         dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
2069                                         value = "";
2070                                 }
2071                                 if (match_key(rules, cur, value))
2072                                         goto nomatch;
2073                                 break;
2074                         }
2075                 case TK_M_SUBSYSTEM:
2076                         if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2077                                 goto nomatch;
2078                         break;
2079                 case TK_M_DRIVER:
2080                         if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2081                                 goto nomatch;
2082                         break;
2083                 case TK_M_WAITFOR:
2084                         {
2085                                 char filename[UTIL_PATH_SIZE];
2086                                 int found;
2087
2088                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2089                                 found = (wait_for_file(event->dev, filename, 10) == 0);
2090                                 if (!found && (cur->key.op != OP_NOMATCH))
2091                                         goto nomatch;
2092                                 break;
2093                         }
2094                 case TK_M_ATTR:
2095                         if (match_attr(rules, event->dev, event, cur) != 0)
2096                                 goto nomatch;
2097                         break;
2098                 case TK_M_KERNELS:
2099                 case TK_M_SUBSYSTEMS:
2100                 case TK_M_DRIVERS:
2101                 case TK_M_ATTRS:
2102                         {
2103                                 struct token *next;
2104
2105                                 /* get whole sequence of parent matches */
2106                                 next = cur;
2107                                 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2108                                         next++;
2109
2110                                 /* loop over parents */
2111                                 event->dev_parent = event->dev;
2112                                 while (1) {
2113                                         struct token *key;
2114
2115                                         dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent));
2116                                         /* loop over sequence of parent match keys */
2117                                         for (key = cur; key < next; key++ ) {
2118                                                 dump_token(rules, key);
2119                                                 switch(key->type) {
2120                                                 case TK_M_KERNELS:
2121                                                         if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2122                                                                 goto try_parent;
2123                                                         break;
2124                                                 case TK_M_SUBSYSTEMS:
2125                                                         if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2126                                                                 goto try_parent;
2127                                                         break;
2128                                                 case TK_M_DRIVERS:
2129                                                         if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2130                                                                 goto try_parent;
2131                                                         break;
2132                                                 case TK_M_ATTRS:
2133                                                         if (match_attr(rules, event->dev_parent, event, key) != 0)
2134                                                                 goto try_parent;
2135                                                         break;
2136                                                 default:
2137                                                         goto nomatch;
2138                                                 }
2139                                                 dbg(event->udev, "parent key matched\n");
2140                                         }
2141                                         dbg(event->udev, "all parent keys matched\n");
2142                                         /* all keys matched */
2143                                         break;
2144
2145                                 try_parent:
2146                                         event->dev_parent = udev_device_get_parent(event->dev_parent);
2147                                         if (event->dev_parent == NULL)
2148                                                 goto nomatch;
2149                                 }
2150                                 /* move behind our sequence of parent match keys */
2151                                 cur = next;
2152                                 continue;
2153                         }
2154                 case TK_M_TEST:
2155                         {
2156                                 char filename[UTIL_PATH_SIZE];
2157                                 struct stat statbuf;
2158                                 int match;
2159
2160                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2161                                 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2162                                         if (filename[0] != '/') {
2163                                                 char tmp[UTIL_PATH_SIZE];
2164
2165                                                 util_strscpy(tmp, sizeof(tmp), filename);
2166                                                 util_strscpyl(filename, sizeof(filename),
2167                                                               udev_device_get_syspath(event->dev), "/", tmp, NULL);
2168                                         }
2169                                 }
2170                                 attr_subst_subdir(filename, sizeof(filename));
2171
2172                                 match = (stat(filename, &statbuf) == 0);
2173                                 dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
2174                                 if (match && cur->key.mode > 0) {
2175                                         match = ((statbuf.st_mode & cur->key.mode) > 0);
2176                                         dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
2177                                             match ? "matches" : "does not match", cur->key.mode);
2178                                 }
2179                                 if (match && cur->key.op == OP_NOMATCH)
2180                                         goto nomatch;
2181                                 if (!match && cur->key.op == OP_MATCH)
2182                                         goto nomatch;
2183                                 break;
2184                         }
2185                 case TK_M_PROGRAM:
2186                         {
2187                                 char program[UTIL_PATH_SIZE];
2188                                 char **envp;
2189                                 char result[UTIL_PATH_SIZE];
2190
2191                                 free(event->program_result);
2192                                 event->program_result = NULL;
2193                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], program, sizeof(program));
2194                                 envp = udev_device_get_properties_envp(event->dev);
2195                                 info(event->udev, "PROGRAM '%s' %s:%u\n",
2196                                      program,
2197                                      &rules->buf[rule->rule.filename_off],
2198                                      rule->rule.filename_line);
2199                                 if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
2200                                         if (cur->key.op != OP_NOMATCH)
2201                                                 goto nomatch;
2202                                 } else {
2203                                         int count;
2204
2205                                         util_remove_trailing_chars(result, '\n');
2206                                         if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2207                                                 count = udev_util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2208                                                 if (count > 0)
2209                                                         info(event->udev, "%i character(s) replaced\n" , count);
2210                                         }
2211                                         event->program_result = strdup(result);
2212                                         dbg(event->udev, "storing result '%s'\n", event->program_result);
2213                                         if (cur->key.op == OP_NOMATCH)
2214                                                 goto nomatch;
2215                                 }
2216                                 break;
2217                         }
2218                 case TK_M_IMPORT_FILE:
2219                         {
2220                                 char import[UTIL_PATH_SIZE];
2221
2222                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2223                                 if (import_file_into_properties(event->dev, import) != 0)
2224                                         if (cur->key.op != OP_NOMATCH)
2225                                                 goto nomatch;
2226                                 break;
2227                         }
2228                 case TK_M_IMPORT_PROG:
2229                         {
2230                                 char import[UTIL_PATH_SIZE];
2231
2232                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2233                                 info(event->udev, "IMPORT '%s' %s:%u\n",
2234                                      import,
2235                                      &rules->buf[rule->rule.filename_off],
2236                                      rule->rule.filename_line);
2237                                 if (import_program_into_properties(event->dev, import) != 0)
2238                                         if (cur->key.op != OP_NOMATCH)
2239                                                 goto nomatch;
2240                                 break;
2241                         }
2242                 case TK_M_IMPORT_PARENT:
2243                         {
2244                                 char import[UTIL_PATH_SIZE];
2245
2246                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2247                                 if (import_parent_into_properties(event->dev, import) != 0)
2248                                         if (cur->key.op != OP_NOMATCH)
2249                                                 goto nomatch;
2250                                 break;
2251                         }
2252                 case TK_M_RESULT:
2253                         if (match_key(rules, cur, event->program_result) != 0)
2254                                 goto nomatch;
2255                         break;
2256
2257                 case TK_A_IGNORE_DEVICE:
2258                         event->ignore_device = 1;
2259                         return 0;
2260                         break;
2261                 case TK_A_STRING_ESCAPE_NONE:
2262                         esc = ESCAPE_NONE;
2263                         break;
2264                 case TK_A_STRING_ESCAPE_REPLACE:
2265                         esc = ESCAPE_REPLACE;
2266                         break;
2267                 case TK_A_NUM_FAKE_PART:
2268                         if (strcmp(udev_device_get_subsystem(event->dev), "block") != 0)
2269                                 break;
2270                         if (udev_device_get_sysnum(event->dev) != NULL)
2271                                 break;
2272                         udev_device_set_num_fake_partitions(event->dev, cur->key.num_fake_part);
2273                         break;
2274                 case TK_A_INOTIFY_WATCH:
2275                         event->inotify_watch = cur->key.watch;
2276                         break;
2277                 case TK_A_DEVLINK_PRIO:
2278                         udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2279                         break;
2280                 case TK_A_OWNER:
2281                         {
2282                                 char owner[UTIL_NAME_SIZE];
2283
2284                                 if (event->owner_final)
2285                                         break;
2286                                 if (cur->key.op == OP_ASSIGN_FINAL)
2287                                         event->owner_final = 1;
2288                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], owner, sizeof(owner));
2289                                 event->uid = util_lookup_user(event->udev, owner);
2290                                 info(event->udev, "OWNER %u %s:%u\n",
2291                                      event->uid,
2292                                      &rules->buf[rule->rule.filename_off],
2293                                      rule->rule.filename_line);
2294                                 break;
2295                         }
2296                 case TK_A_GROUP:
2297                         {
2298                                 char group[UTIL_NAME_SIZE];
2299
2300                                 if (event->group_final)
2301                                         break;
2302                                 if (cur->key.op == OP_ASSIGN_FINAL)
2303                                         event->group_final = 1;
2304                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], group, sizeof(group));
2305                                 event->gid = util_lookup_group(event->udev, group);
2306                                 info(event->udev, "GROUP %u %s:%u\n",
2307                                      event->gid,
2308                                      &rules->buf[rule->rule.filename_off],
2309                                      rule->rule.filename_line);
2310                                 break;
2311                         }
2312                 case TK_A_MODE:
2313                         {
2314                                 char mode[UTIL_NAME_SIZE];
2315                                 char *endptr;
2316
2317                                 if (event->mode_final)
2318                                         break;
2319                                 if (cur->key.op == OP_ASSIGN_FINAL)
2320                                         event->mode_final = 1;
2321                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], mode, sizeof(mode));
2322                                 event->mode = strtol(mode, &endptr, 8);
2323                                 if (endptr[0] != '\0') {
2324                                         err(event->udev, "invalide mode '%s' set default mode 0660\n", mode);
2325                                         event->mode = 0660;
2326                                 }
2327                                 info(event->udev, "MODE %#o %s:%u\n",
2328                                      event->mode,
2329                                      &rules->buf[rule->rule.filename_off],
2330                                      rule->rule.filename_line);
2331                                 break;
2332                         }
2333                 case TK_A_OWNER_ID:
2334                         if (event->owner_final)
2335                                 break;
2336                         if (cur->key.op == OP_ASSIGN_FINAL)
2337                                 event->owner_final = 1;
2338                         event->uid = cur->key.uid;
2339                         info(event->udev, "OWNER %u %s:%u\n",
2340                              event->uid,
2341                              &rules->buf[rule->rule.filename_off],
2342                              rule->rule.filename_line);
2343                         break;
2344                 case TK_A_GROUP_ID:
2345                         if (event->group_final)
2346                                 break;
2347                         if (cur->key.op == OP_ASSIGN_FINAL)
2348                                 event->group_final = 1;
2349                         event->gid = cur->key.gid;
2350                         info(event->udev, "GROUP %u %s:%u\n",
2351                              event->gid,
2352                              &rules->buf[rule->rule.filename_off],
2353                              rule->rule.filename_line);
2354                         break;
2355                 case TK_A_MODE_ID:
2356                         if (event->mode_final)
2357                                 break;
2358                         if (cur->key.op == OP_ASSIGN_FINAL)
2359                                 event->mode_final = 1;
2360                         event->mode = cur->key.mode;
2361                         info(event->udev, "MODE %#o %s:%u\n",
2362                              event->mode,
2363                              &rules->buf[rule->rule.filename_off],
2364                              rule->rule.filename_line);
2365                         break;
2366                 case TK_A_ENV:
2367                         {
2368                                 const char *name = &rules->buf[cur->key.attr_off];
2369                                 char *value = &rules->buf[cur->key.value_off];
2370
2371                                 if (value[0] != '\0') {
2372                                         char temp_value[UTIL_NAME_SIZE];
2373                                         struct udev_list_entry *entry;
2374
2375                                         udev_event_apply_format(event, value, temp_value, sizeof(temp_value));
2376                                         entry = udev_device_add_property(event->dev, name, temp_value);
2377                                         /* store in db */
2378                                         udev_list_entry_set_flag(entry, 1);
2379                                 } else {
2380                                         udev_device_add_property(event->dev, name, NULL);
2381                                 }
2382                                 break;
2383                         }
2384                 case TK_A_NAME:
2385                         {
2386                                 const char *name  = &rules->buf[cur->key.value_off];
2387                                 char name_str[UTIL_PATH_SIZE];
2388                                 int count;
2389
2390                                 if (event->name_final)
2391                                         break;
2392                                 if (cur->key.op == OP_ASSIGN_FINAL)
2393                                         event->name_final = 1;
2394                                 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2395                                 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2396                                         count = udev_util_replace_chars(name_str, "/");
2397                                         if (count > 0)
2398                                                 info(event->udev, "%i character(s) replaced\n", count);
2399                                 }
2400                                 free(event->name);
2401                                 event->name = strdup(name_str);
2402                                 info(event->udev, "NAME '%s' %s:%u\n",
2403                                      event->name,
2404                                      &rules->buf[rule->rule.filename_off],
2405                                      rule->rule.filename_line);
2406                                 break;
2407                         }
2408                 case TK_A_DEVLINK:
2409                         {
2410                                 char temp[UTIL_PATH_SIZE];
2411                                 char filename[UTIL_PATH_SIZE];
2412                                 char *pos, *next;
2413                                 int count = 0;
2414
2415                                 if (event->devlink_final)
2416                                         break;
2417                                 if (major(udev_device_get_devnum(event->dev)) == 0)
2418                                         break;
2419                                 if (cur->key.op == OP_ASSIGN_FINAL)
2420                                         event->devlink_final = 1;
2421                                 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2422                                         udev_device_cleanup_devlinks_list(event->dev);
2423
2424                                 /* allow  multiple symlinks separated by spaces */
2425                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], temp, sizeof(temp));
2426                                 if (esc == ESCAPE_UNSET)
2427                                         count = udev_util_replace_chars(temp, "/ ");
2428                                 else if (esc == ESCAPE_REPLACE)
2429                                         count = udev_util_replace_chars(temp, "/");
2430                                 if (count > 0)
2431                                         info(event->udev, "%i character(s) replaced\n" , count);
2432                                 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
2433                                 pos = temp;
2434                                 while (isspace(pos[0]))
2435                                         pos++;
2436                                 next = strchr(pos, ' ');
2437                                 while (next) {
2438                                         next[0] = '\0';
2439                                         info(event->udev, "LINK '%s' %s:%u\n",
2440                                              pos,
2441                                              &rules->buf[rule->rule.filename_off],
2442                                              rule->rule.filename_line);
2443                                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2444                                         udev_device_add_devlink(event->dev, filename);
2445                                         while (isspace(next[1]))
2446                                                 next++;
2447                                         pos = &next[1];
2448                                         next = strchr(pos, ' ');
2449                                 }
2450                                 if (pos[0] != '\0') {
2451                                         info(event->udev, "LINK '%s' %s:%u\n",
2452                                              pos,
2453                                              &rules->buf[rule->rule.filename_off],
2454                                              rule->rule.filename_line);
2455                                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2456                                         udev_device_add_devlink(event->dev, filename);
2457                                 }
2458                         }
2459                         break;
2460                 case TK_A_EVENT_TIMEOUT:
2461                         udev_device_set_event_timeout(event->dev, cur->key.event_timeout);
2462                         break;
2463                 case TK_A_IGNORE_REMOVE:
2464                         udev_device_set_ignore_remove(event->dev, 1);
2465                         break;
2466                 case TK_A_ATTR:
2467                         {
2468                                 const char *key_name = &rules->buf[cur->key.attr_off];
2469                                 char attr[UTIL_PATH_SIZE];
2470                                 char value[UTIL_NAME_SIZE];
2471                                 FILE *f;
2472
2473                                 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2474                                         util_strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2475                                 attr_subst_subdir(attr, sizeof(attr));
2476
2477                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], value, sizeof(value));
2478                                 info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value,
2479                                      &rules->buf[rule->rule.filename_off],
2480                                      rule->rule.filename_line);
2481                                 f = fopen(attr, "w");
2482                                 if (f != NULL) {
2483                                         if (fprintf(f, "%s", value) <= 0)
2484                                                 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
2485                                         fclose(f);
2486                                 } else {
2487                                         err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
2488                                 }
2489                                 break;
2490                         }
2491                 case TK_A_RUN:
2492                         {
2493                                 struct udev_list_entry *list_entry;
2494
2495                                 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2496                                         udev_list_cleanup_entries(event->udev, &event->run_list);
2497                                 info(event->udev, "RUN '%s' %s:%u\n",
2498                                      &rules->buf[cur->key.value_off],
2499                                      &rules->buf[rule->rule.filename_off],
2500                                      rule->rule.filename_line);
2501                                 list_entry = udev_list_entry_add(event->udev, &event->run_list,
2502                                                                  &rules->buf[cur->key.value_off], NULL, 1, 0);
2503                                 if (cur->key.ignore_error)
2504                                         udev_list_entry_set_flag(list_entry, 1);
2505                                 break;
2506                         }
2507                 case TK_A_GOTO:
2508                         if (cur->key.rule_goto == 0)
2509                                 break;
2510                         cur = &rules->tokens[cur->key.rule_goto];
2511                         continue;
2512                 case TK_A_LAST_RULE:
2513                 case TK_END:
2514                         return 0;
2515
2516                 case TK_M_PARENTS_MIN:
2517                 case TK_M_PARENTS_MAX:
2518                 case TK_M_MAX:
2519                 case TK_UNSET:
2520                         err(rules->udev, "wrong type %u\n", cur->type);
2521                         goto nomatch;
2522                 }
2523
2524                 cur++;
2525                 continue;
2526         nomatch:
2527                 /* fast-forward to next rule */
2528                 cur = rule + rule->rule.token_count;
2529                 dbg(rules->udev, "forward to rule: %u\n",
2530                                  (unsigned int) (cur - rules->tokens));
2531         }
2532 }