chiark / gitweb /
Also merge into the top-level Makefile.am the simpler extras.
[elogind.git] / udev / udev-rules.c
1 /*
2  * Copyright (C) 2003-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 ENABLE_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 /* ENABLE_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, skip private keys */
722                 if (key[0] != '.')
723                         udev_list_entry_set_flag(entry, 1);
724         }
725         return 0;
726 }
727
728 static int import_file_into_properties(struct udev_device *dev, const char *filename)
729 {
730         FILE *f;
731         char line[UTIL_LINE_SIZE];
732
733         f = fopen(filename, "r");
734         if (f == NULL)
735                 return -1;
736         while (fgets(line, sizeof(line), f) != NULL)
737                 import_property_from_string(dev, line);
738         fclose(f);
739         return 0;
740 }
741
742 static int import_program_into_properties(struct udev_device *dev, const char *program)
743 {
744         struct udev *udev = udev_device_get_udev(dev);
745         char **envp;
746         char result[4096];
747         size_t reslen;
748         char *line;
749
750         envp = udev_device_get_properties_envp(dev);
751         if (util_run_program(udev, program, envp, result, sizeof(result), &reslen) != 0)
752                 return -1;
753
754         line = result;
755         while (line != NULL) {
756                 char *pos;
757
758                 pos = strchr(line, '\n');
759                 if (pos != NULL) {
760                         pos[0] = '\0';
761                         pos = &pos[1];
762                 }
763                 import_property_from_string(dev, line);
764                 line = pos;
765         }
766         return 0;
767 }
768
769 static int import_parent_into_properties(struct udev_device *dev, const char *filter)
770 {
771         struct udev *udev = udev_device_get_udev(dev);
772         struct udev_device *dev_parent;
773         struct udev_list_entry *list_entry;
774
775         dev_parent = udev_device_get_parent(dev);
776         if (dev_parent == NULL)
777                 return -1;
778
779         dbg(udev, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent));
780         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
781                 const char *key = udev_list_entry_get_name(list_entry);
782                 const char *val = udev_list_entry_get_value(list_entry);
783
784                 if (fnmatch(filter, key, 0) == 0) {
785                         struct udev_list_entry *entry;
786
787                         dbg(udev, "import key '%s=%s'\n", key, val);
788                         entry = udev_device_add_property(dev, key, val);
789                         /* store in db, skip private keys */
790                         if (key[0] != '.')
791                                 udev_list_entry_set_flag(entry, 1);
792                 }
793         }
794         return 0;
795 }
796
797 #define WAIT_LOOP_PER_SECOND            50
798 static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
799 {
800         struct udev *udev = udev_device_get_udev(dev);
801         char filepath[UTIL_PATH_SIZE];
802         char devicepath[UTIL_PATH_SIZE];
803         struct stat stats;
804         int loop = timeout * WAIT_LOOP_PER_SECOND;
805
806         /* a relative path is a device attribute */
807         devicepath[0] = '\0';
808         if (file[0] != '/') {
809                 util_strscpyl(devicepath, sizeof(devicepath),
810                               udev_get_sys_path(udev), udev_device_get_devpath(dev), NULL);
811                 util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
812                 file = filepath;
813         }
814
815         dbg(udev, "will wait %i sec for '%s'\n", timeout, file);
816         while (--loop) {
817                 /* lookup file */
818                 if (stat(file, &stats) == 0) {
819                         info(udev, "file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
820                         return 0;
821                 }
822                 /* make sure, the device did not disappear in the meantime */
823                 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
824                         info(udev, "device disappeared while waiting for '%s'\n", file);
825                         return -2;
826                 }
827                 info(udev, "wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
828                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
829         }
830         info(udev, "waiting for '%s' failed\n", file);
831         return -1;
832 }
833
834 static int attr_subst_subdir(char *attr, size_t len)
835 {
836         int found = 0;
837
838         if (strstr(attr, "/*/")) {
839                 char *pos;
840                 char dirname[UTIL_PATH_SIZE];
841                 const char *tail;
842                 DIR *dir;
843
844                 util_strscpy(dirname, sizeof(dirname), attr);
845                 pos = strstr(dirname, "/*/");
846                 if (pos == NULL)
847                         return -1;
848                 pos[0] = '\0';
849                 tail = &pos[2];
850                 dir = opendir(dirname);
851                 if (dir != NULL) {
852                         struct dirent *dent;
853
854                         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
855                                 struct stat stats;
856
857                                 if (dent->d_name[0] == '.')
858                                         continue;
859                                 util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
860                                 if (stat(attr, &stats) == 0) {
861                                         found = 1;
862                                         break;
863                                 }
864                         }
865                         closedir(dir);
866                 }
867         }
868
869         return found;
870 }
871
872 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
873 {
874         char *linepos;
875         char *temp;
876
877         linepos = *line;
878         if (linepos == NULL && linepos[0] == '\0')
879                 return -1;
880
881         /* skip whitespace */
882         while (isspace(linepos[0]) || linepos[0] == ',')
883                 linepos++;
884
885         /* get the key */
886         if (linepos[0] == '\0')
887                 return -1;
888         *key = linepos;
889
890         while (1) {
891                 linepos++;
892                 if (linepos[0] == '\0')
893                         return -1;
894                 if (isspace(linepos[0]))
895                         break;
896                 if (linepos[0] == '=')
897                         break;
898                 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
899                         if (linepos[1] == '=')
900                                 break;
901         }
902
903         /* remember end of key */
904         temp = linepos;
905
906         /* skip whitespace after key */
907         while (isspace(linepos[0]))
908                 linepos++;
909         if (linepos[0] == '\0')
910                 return -1;
911
912         /* get operation type */
913         if (linepos[0] == '=' && linepos[1] == '=') {
914                 *op = OP_MATCH;
915                 linepos += 2;
916         } else if (linepos[0] == '!' && linepos[1] == '=') {
917                 *op = OP_NOMATCH;
918                 linepos += 2;
919         } else if (linepos[0] == '+' && linepos[1] == '=') {
920                 *op = OP_ADD;
921                 linepos += 2;
922         } else if (linepos[0] == '=') {
923                 *op = OP_ASSIGN;
924                 linepos++;
925         } else if (linepos[0] == ':' && linepos[1] == '=') {
926                 *op = OP_ASSIGN_FINAL;
927                 linepos += 2;
928         } else
929                 return -1;
930
931         /* terminate key */
932         temp[0] = '\0';
933
934         /* skip whitespace after operator */
935         while (isspace(linepos[0]))
936                 linepos++;
937         if (linepos[0] == '\0')
938                 return -1;
939
940         /* get the value */
941         if (linepos[0] == '"')
942                 linepos++;
943         else
944                 return -1;
945         *value = linepos;
946
947         /* terminate */
948         temp = strchr(linepos, '"');
949         if (!temp)
950                 return -1;
951         temp[0] = '\0';
952         temp++;
953         dbg(udev, "%s '%s'-'%s'\n", operation_str(*op), *key, *value);
954
955         /* move line to next key */
956         *line = temp;
957         return 0;
958 }
959
960 /* extract possible KEY{attr} */
961 static char *get_key_attribute(struct udev *udev, char *str)
962 {
963         char *pos;
964         char *attr;
965
966         attr = strchr(str, '{');
967         if (attr != NULL) {
968                 attr++;
969                 pos = strchr(attr, '}');
970                 if (pos == NULL) {
971                         err(udev, "missing closing brace for format\n");
972                         return NULL;
973                 }
974                 pos[0] = '\0';
975                 dbg(udev, "attribute='%s'\n", attr);
976                 return attr;
977         }
978         return NULL;
979 }
980
981 static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
982                         enum operation_type op,
983                         const char *value, const void *data)
984 {
985         struct token *token = &rule_tmp->token[rule_tmp->token_cur];
986         const char *attr = NULL;
987
988         memset(token, 0x00, sizeof(struct token));
989
990         switch (type) {
991         case TK_M_ACTION:
992         case TK_M_DEVPATH:
993         case TK_M_KERNEL:
994         case TK_M_SUBSYSTEM:
995         case TK_M_DRIVER:
996         case TK_M_WAITFOR:
997         case TK_M_DEVLINK:
998         case TK_M_NAME:
999         case TK_M_KERNELS:
1000         case TK_M_SUBSYSTEMS:
1001         case TK_M_DRIVERS:
1002         case TK_M_PROGRAM:
1003         case TK_M_IMPORT_FILE:
1004         case TK_M_IMPORT_PROG:
1005         case TK_M_IMPORT_PARENT:
1006         case TK_M_RESULT:
1007         case TK_A_OWNER:
1008         case TK_A_GROUP:
1009         case TK_A_MODE:
1010         case TK_A_NAME:
1011         case TK_A_DEVLINK:
1012         case TK_A_GOTO:
1013                 token->key.value_off = add_string(rule_tmp->rules, value);
1014                 break;
1015         case TK_M_ENV:
1016         case TK_M_ATTR:
1017         case TK_M_ATTRS:
1018         case TK_A_ATTR:
1019         case TK_A_ENV:
1020                 attr = data;
1021                 token->key.value_off = add_string(rule_tmp->rules, value);
1022                 token->key.attr_off = add_string(rule_tmp->rules, attr);
1023                 break;
1024         case TK_M_TEST:
1025                 token->key.value_off = add_string(rule_tmp->rules, value);
1026                 if (data != NULL)
1027                         token->key.mode = *(mode_t *)data;
1028                 break;
1029         case TK_A_IGNORE_DEVICE:
1030         case TK_A_STRING_ESCAPE_NONE:
1031         case TK_A_STRING_ESCAPE_REPLACE:
1032         case TK_A_IGNORE_REMOVE:
1033         case TK_A_LAST_RULE:
1034                 break;
1035         case TK_A_RUN:
1036                 token->key.value_off = add_string(rule_tmp->rules, value);
1037                 token->key.ignore_error = *(int *)data;
1038                 break;
1039         case TK_A_INOTIFY_WATCH:
1040         case TK_A_NUM_FAKE_PART:
1041         case TK_A_DEVLINK_PRIO:
1042                 token->key.devlink_prio = *(int *)data;
1043                 break;
1044         case TK_A_OWNER_ID:
1045                 token->key.uid = *(uid_t *)data;
1046                 break;
1047         case TK_A_GROUP_ID:
1048                 token->key.gid = *(gid_t *)data;
1049                 break;
1050         case TK_A_MODE_ID:
1051                 token->key.mode = *(mode_t *)data;
1052                 break;
1053         case TK_A_EVENT_TIMEOUT:
1054                 token->key.event_timeout = *(int *)data;
1055                 break;
1056         case TK_RULE:
1057         case TK_M_PARENTS_MIN:
1058         case TK_M_PARENTS_MAX:
1059         case TK_M_MAX:
1060         case TK_END:
1061         case TK_UNSET:
1062                 err(rule_tmp->rules->udev, "wrong type %u\n", type);
1063                 return -1;
1064         }
1065
1066         if (value != NULL && type < TK_M_MAX) {
1067                 /* check if we need to split or call fnmatch() while matching rules */
1068                 enum string_glob_type glob;
1069                 int has_split;
1070                 int has_glob;
1071
1072                 has_split = (strchr(value, '|') != NULL);
1073                 has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
1074                 if (has_split && has_glob) {
1075                         glob = GL_SPLIT_GLOB;
1076                 } else if (has_split) {
1077                         glob = GL_SPLIT;
1078                 } else if (has_glob) {
1079                         if (strcmp(value, "?*") == 0)
1080                                 glob = GL_SOMETHING;
1081                         else
1082                                 glob = GL_GLOB;
1083                 } else {
1084                         glob = GL_PLAIN;
1085                 }
1086                 token->key.glob = glob;
1087         }
1088
1089         if (value != NULL && type > TK_M_MAX) {
1090                 /* check if assigned value has substitution chars */
1091                 if (value[0] == '[')
1092                         token->key.subst = SB_SUBSYS;
1093                 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
1094                         token->key.subst = SB_FORMAT;
1095                 else
1096                         token->key.subst = SB_NONE;
1097         }
1098
1099         if (attr != NULL) {
1100                 /* check if property/attribut name has substitution chars */
1101                 if (attr[0] == '[')
1102                         token->key.attrsubst = SB_SUBSYS;
1103                 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
1104                         token->key.attrsubst = SB_FORMAT;
1105                 else
1106                         token->key.attrsubst = SB_NONE;
1107         }
1108
1109         token->key.type = type;
1110         token->key.op = op;
1111         rule_tmp->token_cur++;
1112         if (rule_tmp->token_cur >= ARRAY_SIZE(rule_tmp->token)) {
1113                 err(rule_tmp->rules->udev, "temporary rule array too small\n");
1114                 return -1;
1115         }
1116         return 0;
1117 }
1118
1119 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1120 {
1121         unsigned int i;
1122         unsigned int start = 0;
1123         unsigned int end = rule_tmp->token_cur;
1124
1125         for (i = 0; i < rule_tmp->token_cur; i++) {
1126                 enum token_type next_val = TK_UNSET;
1127                 unsigned int next_idx;
1128                 unsigned int j;
1129
1130                 /* find smallest value */
1131                 for (j = start; j < end; j++) {
1132                         if (rule_tmp->token[j].type == TK_UNSET)
1133                                 continue;
1134                         if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1135                                 next_val = rule_tmp->token[j].type;
1136                                 next_idx = j;
1137                         }
1138                 }
1139
1140                 /* add token and mark done */
1141                 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1142                         return -1;
1143                 rule_tmp->token[next_idx].type = TK_UNSET;
1144
1145                 /* shrink range */
1146                 if (next_idx == start)
1147                         start++;
1148                 if (next_idx+1 == end)
1149                         end--;
1150         }
1151         return 0;
1152 }
1153
1154 static int add_rule(struct udev_rules *rules, char *line,
1155                     const char *filename, unsigned int filename_off, unsigned int lineno)
1156 {
1157         char *linepos;
1158         char *attr;
1159         struct rule_tmp rule_tmp;
1160
1161         memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
1162         rule_tmp.rules = rules;
1163         rule_tmp.rule.type = TK_RULE;
1164         rule_tmp.rule.rule.filename_off = filename_off;
1165         rule_tmp.rule.rule.filename_line = lineno;
1166
1167         linepos = line;
1168         while (1) {
1169                 char *key;
1170                 char *value;
1171                 enum operation_type op;
1172
1173                 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
1174                         break;
1175
1176                 if (strcmp(key, "ACTION") == 0) {
1177                         if (op > OP_MATCH_MAX) {
1178                                 err(rules->udev, "invalid ACTION operation\n");
1179                                 goto invalid;
1180                         }
1181                         rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1182                         continue;
1183                 }
1184
1185                 if (strcmp(key, "DEVPATH") == 0) {
1186                         if (op > OP_MATCH_MAX) {
1187                                 err(rules->udev, "invalid DEVPATH operation\n");
1188                                 goto invalid;
1189                         }
1190                         rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1191                         continue;
1192                 }
1193
1194                 if (strcmp(key, "KERNEL") == 0) {
1195                         if (op > OP_MATCH_MAX) {
1196                                 err(rules->udev, "invalid KERNEL operation\n");
1197                                 goto invalid;
1198                         }
1199                         rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1200                         continue;
1201                 }
1202
1203                 if (strcmp(key, "SUBSYSTEM") == 0) {
1204                         if (op > OP_MATCH_MAX) {
1205                                 err(rules->udev, "invalid SUBSYSTEM operation\n");
1206                                 goto invalid;
1207                         }
1208                         /* bus, class, subsystem events should all be the same */
1209                         if (strcmp(value, "subsystem") == 0 ||
1210                             strcmp(value, "bus") == 0 ||
1211                             strcmp(value, "class") == 0) {
1212                                 if (strcmp(value, "bus") == 0 || strcmp(value, "class") == 0)
1213                                         err(rules->udev, "'%s' must be specified as 'subsystem' \n"
1214                                             "please fix it in %s:%u", value, filename, lineno);
1215                                 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1216                         } else
1217                                 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1218                         continue;
1219                 }
1220
1221                 if (strcmp(key, "DRIVER") == 0) {
1222                         if (op > OP_MATCH_MAX) {
1223                                 err(rules->udev, "invalid DRIVER operation\n");
1224                                 goto invalid;
1225                         }
1226                         rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1227                         continue;
1228                 }
1229
1230                 if (strncmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
1231                         attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
1232                         if (attr == NULL) {
1233                                 err(rules->udev, "error parsing ATTR attribute\n");
1234                                 goto invalid;
1235                         }
1236                         if (op < OP_MATCH_MAX) {
1237                                 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1238                         } else {
1239                                 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1240                         }
1241                         continue;
1242                 }
1243
1244                 if (strcmp(key, "KERNELS") == 0 ||
1245                     strcmp(key, "ID") == 0) {
1246                         if (op > OP_MATCH_MAX) {
1247                                 err(rules->udev, "invalid KERNELS operation\n");
1248                                 goto invalid;
1249                         }
1250                         rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1251                         continue;
1252                 }
1253
1254                 if (strcmp(key, "SUBSYSTEMS") == 0 ||
1255                     strcmp(key, "BUS") == 0) {
1256                         if (op > OP_MATCH_MAX) {
1257                                 err(rules->udev, "invalid SUBSYSTEMS operation\n");
1258                                 goto invalid;
1259                         }
1260                         rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1261                         continue;
1262                 }
1263
1264                 if (strcmp(key, "DRIVERS") == 0) {
1265                         if (op > OP_MATCH_MAX) {
1266                                 err(rules->udev, "invalid DRIVERS operation\n");
1267                                 goto invalid;
1268                         }
1269                         rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1270                         continue;
1271                 }
1272
1273                 if (strncmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1274                     strncmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1275                         if (op > OP_MATCH_MAX) {
1276                                 err(rules->udev, "invalid ATTRS operation\n");
1277                                 goto invalid;
1278                         }
1279                         attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
1280                         if (attr == NULL) {
1281                                 err(rules->udev, "error parsing ATTRS attribute\n");
1282                                 goto invalid;
1283                         }
1284                         if (strncmp(attr, "device/", 7) == 0)
1285                                 err(rules->udev, "the 'device' link may not be available in a future kernel, "
1286                                     "please fix it in %s:%u", filename, lineno);
1287                         else if (strstr(attr, "../") != NULL)
1288                                 err(rules->udev, "do not reference parent sysfs directories directly, "
1289                                     "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1290                         rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1291                         continue;
1292                 }
1293
1294                 if (strncmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
1295                         attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
1296                         if (attr == NULL) {
1297                                 err(rules->udev, "error parsing ENV attribute\n");
1298                                 goto invalid;
1299                         }
1300                         if (op < OP_MATCH_MAX) {
1301                                 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1302                                         goto invalid;
1303                         } else {
1304                                 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1305                                         goto invalid;
1306                         }
1307                         continue;
1308                 }
1309
1310                 if (strcmp(key, "PROGRAM") == 0) {
1311                         rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1312                         continue;
1313                 }
1314
1315                 if (strcmp(key, "RESULT") == 0) {
1316                         if (op > OP_MATCH_MAX) {
1317                                 err(rules->udev, "invalid RESULT operation\n");
1318                                 goto invalid;
1319                         }
1320                         rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1321                         continue;
1322                 }
1323
1324                 if (strncmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
1325                         attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
1326                         if (attr != NULL && strstr(attr, "program")) {
1327                                 dbg(rules->udev, "IMPORT will be executed\n");
1328                                 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1329                         } else if (attr != NULL && strstr(attr, "file")) {
1330                                 dbg(rules->udev, "IMPORT will be included as file\n");
1331                                 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1332                         } else if (attr != NULL && strstr(attr, "parent")) {
1333                                 dbg(rules->udev, "IMPORT will include the parent values\n");
1334                                 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1335                         } else {
1336                                 /* figure it out if it is executable */
1337                                 char file[UTIL_PATH_SIZE];
1338                                 char *pos;
1339                                 struct stat statbuf;
1340
1341                                 /* allow programs in /lib/udev called without the path */
1342                                 if (value[0] != '/')
1343                                         util_strscpyl(file, sizeof(file), LIBEXECDIR "/", value, NULL);
1344                                 else
1345                                         util_strscpy(file, sizeof(file), value);
1346                                 pos = strchr(file, ' ');
1347                                 if (pos)
1348                                         pos[0] = '\0';
1349                                 dbg(rules->udev, "IMPORT auto mode for '%s'\n", file);
1350                                 if (stat(file, &statbuf) == 0 && (statbuf.st_mode & S_IXUSR)) {
1351                                         dbg(rules->udev, "IMPORT will be executed (autotype)\n");
1352                                         rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1353                                 } else {
1354                                         dbg(rules->udev, "IMPORT will be included as file (autotype)\n");
1355                                         rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1356                                 }
1357                         }
1358                         continue;
1359                 }
1360
1361                 if (strncmp(key, "TEST", sizeof("TEST")-1) == 0) {
1362                         mode_t mode = 0;
1363
1364                         if (op > OP_MATCH_MAX) {
1365                                 err(rules->udev, "invalid TEST operation\n");
1366                                 goto invalid;
1367                         }
1368                         attr = get_key_attribute(rules->udev, key + sizeof("TEST")-1);
1369                         if (attr != NULL) {
1370                                 mode = strtol(attr, NULL, 8);
1371                                 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1372                         } else {
1373                                 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1374                         }
1375                         continue;
1376                 }
1377
1378                 if (strncmp(key, "RUN", sizeof("RUN")-1) == 0) {
1379                         int flag = 0;
1380
1381                         attr = get_key_attribute(rules->udev, key + sizeof("RUN")-1);
1382                         if (attr != NULL && strstr(attr, "fail_event_on_error"))
1383                                 flag = 1;
1384                         rule_add_key(&rule_tmp, TK_A_RUN, op, value, &flag);
1385                         continue;
1386                 }
1387
1388                 if (strcmp(key, "WAIT_FOR") == 0 || strcmp(key, "WAIT_FOR_SYSFS") == 0) {
1389                         rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1390                         continue;
1391                 }
1392
1393                 if (strcmp(key, "LABEL") == 0) {
1394                         rule_tmp.rule.rule.label_off = add_string(rules, value);
1395                         continue;
1396                 }
1397
1398                 if (strcmp(key, "GOTO") == 0) {
1399                         rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1400                         continue;
1401                 }
1402
1403                 if (strncmp(key, "NAME", sizeof("NAME")-1) == 0) {
1404                         if (op < OP_MATCH_MAX) {
1405                                 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1406                         } else {
1407                                 if (value[0] == '\0')
1408                                         info(rules->udev, "name empty, node creation suppressed\n");
1409                                 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1410                                 attr = get_key_attribute(rules->udev, key + sizeof("NAME")-1);
1411                                 if (attr != NULL) {
1412                                         if (strstr(attr, "all_partitions") != NULL) {
1413                                                 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1414
1415                                                 dbg(rules->udev, "creation of partition nodes requested\n");
1416                                                 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1417                                         }
1418                                         if (strstr(attr, "ignore_remove") != NULL) {
1419                                                 dbg(rules->udev, "remove event should be ignored\n");
1420                                                 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1421                                         }
1422                                 }
1423                         }
1424                         rule_tmp.rule.rule.flags = 1;
1425                         continue;
1426                 }
1427
1428                 if (strcmp(key, "SYMLINK") == 0) {
1429                         if (op < OP_MATCH_MAX)
1430                                 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1431                         else
1432                                 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1433                         rule_tmp.rule.rule.flags = 1;
1434                         continue;
1435                 }
1436
1437                 if (strcmp(key, "OWNER") == 0) {
1438                         uid_t uid;
1439                         char *endptr;
1440
1441                         uid = strtoul(value, &endptr, 10);
1442                         if (endptr[0] == '\0') {
1443                                 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1444                         } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1445                                 uid = add_uid(rules, value);
1446                                 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1447                         } else if (rules->resolve_names >= 0) {
1448                                 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1449                         }
1450                         rule_tmp.rule.rule.flags = 1;
1451                         continue;
1452                 }
1453
1454                 if (strcmp(key, "GROUP") == 0) {
1455                         gid_t gid;
1456                         char *endptr;
1457
1458                         gid = strtoul(value, &endptr, 10);
1459                         if (endptr[0] == '\0') {
1460                                 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1461                         } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1462                                 gid = add_gid(rules, value);
1463                                 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1464                         } else if (rules->resolve_names >= 0) {
1465                                 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1466                         }
1467                         rule_tmp.rule.rule.flags = 1;
1468                         continue;
1469                 }
1470
1471                 if (strcmp(key, "MODE") == 0) {
1472                         mode_t mode;
1473                         char *endptr;
1474
1475                         mode = strtol(value, &endptr, 8);
1476                         if (endptr[0] == '\0')
1477                                 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1478                         else
1479                                 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1480                         rule_tmp.rule.rule.flags = 1;
1481                         continue;
1482                 }
1483
1484                 if (strcmp(key, "OPTIONS") == 0) {
1485                         const char *pos;
1486
1487                         if (strstr(value, "last_rule") != NULL) {
1488                                 dbg(rules->udev, "last rule to be applied\n");
1489                                 rule_add_key(&rule_tmp, TK_A_LAST_RULE, 0, NULL, NULL);
1490                         }
1491                         if (strstr(value, "ignore_device") != NULL) {
1492                                 dbg(rules->udev, "device should be ignored\n");
1493                                 rule_add_key(&rule_tmp, TK_A_IGNORE_DEVICE, 0, NULL, NULL);
1494                         }
1495                         if (strstr(value, "ignore_remove") != NULL) {
1496                                 dbg(rules->udev, "remove event should be ignored\n");
1497                                 rule_add_key(&rule_tmp, TK_A_IGNORE_REMOVE, 0, NULL, NULL);
1498                         }
1499                         pos = strstr(value, "link_priority=");
1500                         if (pos != NULL) {
1501                                 int prio = atoi(&pos[strlen("link_priority=")]);
1502
1503                                 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, 0, NULL, &prio);
1504                                 dbg(rules->udev, "link priority=%i\n", prio);
1505                         }
1506                         pos = strstr(value, "event_timeout=");
1507                         if (pos != NULL) {
1508                                 int tout = atoi(&pos[strlen("event_timeout=")]);
1509
1510                                 rule_add_key(&rule_tmp, TK_A_EVENT_TIMEOUT, 0, NULL, &tout);
1511                                 dbg(rules->udev, "event timout=%i\n", tout);
1512                         }
1513                         pos = strstr(value, "string_escape=");
1514                         if (pos != NULL) {
1515                                 pos = &pos[strlen("string_escape=")];
1516                                 if (strncmp(pos, "none", strlen("none")) == 0)
1517                                         rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, 0, NULL, NULL);
1518                                 else if (strncmp(pos, "replace", strlen("replace")) == 0)
1519                                         rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, 0, NULL, NULL);
1520                         }
1521                         if (strstr(value, "all_partitions") != NULL) {
1522                                 int num = DEFAULT_FAKE_PARTITIONS_COUNT;
1523
1524                                 rule_add_key(&rule_tmp, TK_A_NUM_FAKE_PART, 0, NULL, &num);
1525                                 dbg(rules->udev, "creation of partition nodes requested\n");
1526                         }
1527                         pos = strstr(value, "nowatch");
1528                         if (pos != NULL) {
1529                                 const int off = 0;
1530
1531                                 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, 0, NULL, &off);
1532                                 dbg(rules->udev, "inotify watch of device disabled\n");
1533                         } else {
1534                                 pos = strstr(value, "watch");
1535                                 if (pos != NULL) {
1536                                         const int on = 1;
1537
1538                                         rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, 0, NULL, &on);
1539                                         dbg(rules->udev, "inotify watch of device requested\n");
1540                                 }
1541                         }
1542                         continue;
1543                 }
1544                 err(rules->udev, "unknown key '%s' in %s:%u\n", key, filename, lineno);
1545         }
1546
1547         /* add rule token */
1548         rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1549         if (add_token(rules, &rule_tmp.rule) != 0)
1550                 goto invalid;
1551
1552         /* add tokens to list, sorted by type */
1553         if (sort_token(rules, &rule_tmp) != 0)
1554                 goto invalid;
1555
1556         return 0;
1557 invalid:
1558         err(rules->udev, "invalid rule '%s:%u'\n", filename, lineno);
1559         return -1;
1560 }
1561
1562 static int parse_file(struct udev_rules *rules, const char *filename, unsigned short filename_off)
1563 {
1564         FILE *f;
1565         unsigned int first_token;
1566         char line[UTIL_LINE_SIZE];
1567         int line_nr = 0;
1568         unsigned int i;
1569
1570         info(rules->udev, "reading '%s' as rules file\n", filename);
1571
1572         f = fopen(filename, "r");
1573         if (f == NULL)
1574                 return -1;
1575
1576         first_token = rules->token_cur;
1577
1578         while(fgets(line, sizeof(line), f) != NULL) {
1579                 char *key;
1580                 size_t len;
1581
1582                 /* skip whitespace */
1583                 line_nr++;
1584                 key = line;
1585                 while (isspace(key[0]))
1586                         key++;
1587
1588                 /* comment */
1589                 if (key[0] == '#')
1590                         continue;
1591
1592                 len = strlen(line);
1593                 if (len < 3)
1594                         continue;
1595
1596                 /* continue reading if backslash+newline is found */
1597                 while (line[len-2] == '\\') {
1598                         if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1599                                 break;
1600                         line_nr++;
1601                         len = strlen(line);
1602                 }
1603
1604                 if (len+1 >= sizeof(line)) {
1605                         err(rules->udev, "line too long '%s':%u, ignored\n", filename, line_nr);
1606                         continue;
1607                 }
1608                 add_rule(rules, key, filename, filename_off, line_nr);
1609         }
1610         fclose(f);
1611
1612         /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1613         for (i = first_token+1; i < rules->token_cur; i++) {
1614                 if (rules->tokens[i].type == TK_A_GOTO) {
1615                         char *label = &rules->buf[rules->tokens[i].key.value_off];
1616                         unsigned int j;
1617
1618                         for (j = i+1; j < rules->token_cur; j++) {
1619                                 if (rules->tokens[j].type != TK_RULE)
1620                                         continue;
1621                                 if (rules->tokens[j].rule.label_off == 0)
1622                                         continue;
1623                                 if (strcmp(label, &rules->buf[rules->tokens[j].rule.label_off]) != 0)
1624                                         continue;
1625                                 rules->tokens[i].key.rule_goto = j;
1626                                 break;
1627                         }
1628                         if (rules->tokens[i].key.rule_goto == 0)
1629                                 err(rules->udev, "GOTO '%s' has no matching label in: '%s'\n", label, filename);
1630                 }
1631         }
1632         return 0;
1633 }
1634
1635 static int add_matching_files(struct udev *udev, struct udev_list_node *file_list, const char *dirname, const char *suffix)
1636 {
1637         DIR *dir;
1638         char filename[UTIL_PATH_SIZE];
1639
1640         dbg(udev, "open directory '%s'\n", dirname);
1641         dir = opendir(dirname);
1642         if (dir == NULL) {
1643                 info(udev, "unable to open '%s': %m\n", dirname);
1644                 return -1;
1645         }
1646
1647         while (1) {
1648                 struct dirent *dent;
1649
1650                 dent = readdir(dir);
1651                 if (dent == NULL || dent->d_name[0] == '\0')
1652                         break;
1653
1654                 if (dent->d_name[0] == '.')
1655                         continue;
1656
1657                 /* look for file matching with specified suffix */
1658                 if (suffix != NULL) {
1659                         const char *ext;
1660
1661                         ext = strrchr(dent->d_name, '.');
1662                         if (ext == NULL)
1663                                 continue;
1664                         if (strcmp(ext, suffix) != 0)
1665                                 continue;
1666                 }
1667                 util_strscpyl(filename, sizeof(filename), dirname, "/", dent->d_name, NULL);
1668                 dbg(udev, "put file '%s' into list\n", filename);
1669                 udev_list_entry_add(udev, file_list, filename, NULL, 1, 1);
1670         }
1671
1672         closedir(dir);
1673         return 0;
1674 }
1675
1676 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
1677 {
1678         struct udev_rules *rules;
1679         struct stat statbuf;
1680         struct udev_list_node file_list;
1681         struct udev_list_entry *file_loop, *file_tmp;
1682         struct token end_token;
1683
1684         rules = malloc(sizeof(struct udev_rules));
1685         if (rules == NULL)
1686                 return NULL;
1687         memset(rules, 0x00, sizeof(struct udev_rules));
1688         rules->udev = udev;
1689         rules->resolve_names = resolve_names;
1690         udev_list_init(&file_list);
1691
1692         /* init token array and string buffer */
1693         rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1694         if (rules->tokens == NULL)
1695                 return NULL;
1696         rules->token_max = PREALLOC_TOKEN;
1697
1698         rules->buf = malloc(PREALLOC_STRBUF);
1699         if (rules->buf == NULL)
1700                 return NULL;
1701         rules->buf_max = PREALLOC_STRBUF;
1702         /* offset 0 is always '\0' */
1703         rules->buf[0] = '\0';
1704         rules->buf_cur = 1;
1705         dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1706             rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1707
1708         rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
1709         if (rules->trie_nodes == NULL)
1710                 return NULL;
1711         rules->trie_nodes_max = PREALLOC_TRIE;
1712         /* offset 0 is the trie root, with an empty string */
1713         memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
1714         rules->trie_nodes_cur = 1;
1715
1716         if (udev_get_rules_path(udev) != NULL) {
1717                 /* custom rules location for testing */
1718                 add_matching_files(udev, &file_list, udev_get_rules_path(udev), ".rules");
1719         } else {
1720                 char filename[PATH_MAX];
1721                 struct udev_list_node sort_list;
1722                 struct udev_list_entry *sort_loop, *sort_tmp;
1723
1724                 /* read user/custom rules */
1725                 add_matching_files(udev, &file_list, SYSCONFDIR "/udev/rules.d", ".rules");
1726
1727                 /* read dynamic/temporary rules */
1728                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
1729                 if (stat(filename, &statbuf) != 0) {
1730                         util_create_path(udev, filename);
1731                         udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
1732                         mkdir(filename, 0755);
1733                         udev_selinux_resetfscreatecon(udev);
1734                 }
1735                 udev_list_init(&sort_list);
1736                 add_matching_files(udev, &sort_list, filename, ".rules");
1737
1738                 /* read default rules */
1739                 add_matching_files(udev, &sort_list, LIBEXECDIR "/rules.d", ".rules");
1740
1741                 /* sort all rules files by basename into list of files */
1742                 udev_list_entry_foreach_safe(sort_loop, sort_tmp, udev_list_get_entry(&sort_list)) {
1743                         const char *sort_name = udev_list_entry_get_name(sort_loop);
1744                         const char *sort_base = strrchr(sort_name, '/');
1745
1746                         if (sort_base == NULL)
1747                                 continue;
1748                         /* sort entry into existing list */
1749                         udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1750                                 const char *file_name = udev_list_entry_get_name(file_loop);
1751                                 const char *file_base = strrchr(file_name, '/');
1752
1753                                 if (file_base == NULL)
1754                                         continue;
1755                                 if (strcmp(file_base, sort_base) == 0) {
1756                                         info(udev, "rule file basename '%s' already added, ignoring '%s'\n",
1757                                              file_name, sort_name);
1758                                         udev_list_entry_delete(sort_loop);
1759                                         sort_loop = NULL;
1760                                         break;
1761                                 }
1762                                 if (strcmp(file_base, sort_base) > 0) {
1763                                         /* found later file, insert before */
1764                                         udev_list_entry_remove(sort_loop);
1765                                         udev_list_entry_insert_before(sort_loop, file_loop);
1766                                         sort_loop = NULL;
1767                                         break;
1768                                 }
1769                         }
1770                         /* current file already handled */
1771                         if (sort_loop == NULL)
1772                                 continue;
1773                         /* no later file, append to end of list */
1774                         udev_list_entry_remove(sort_loop);
1775                         udev_list_entry_append(sort_loop, &file_list);
1776                 }
1777         }
1778
1779         /* add all filenames to the string buffer */
1780         udev_list_entry_foreach(file_loop, udev_list_get_entry(&file_list)) {
1781                 const char *filename = udev_list_entry_get_name(file_loop);
1782                 unsigned int filename_off;
1783
1784                 filename_off = add_string(rules, filename);
1785                 /* the offset in the rule is limited to unsigned short */
1786                 if (filename_off < USHRT_MAX)
1787                         udev_list_entry_set_flag(file_loop, filename_off);
1788         }
1789
1790         /* parse list of files */
1791         udev_list_entry_foreach_safe(file_loop, file_tmp, udev_list_get_entry(&file_list)) {
1792                 const char *filename = udev_list_entry_get_name(file_loop);
1793                 unsigned int filename_off = udev_list_entry_get_flag(file_loop);
1794
1795                 if (stat(filename, &statbuf) == 0 && statbuf.st_size > 0)
1796                         parse_file(rules, filename, filename_off);
1797                 else
1798                         info(udev, "can not read '%s'\n", filename);
1799                 udev_list_entry_delete(file_loop);
1800         }
1801
1802         memset(&end_token, 0x00, sizeof(struct token));
1803         end_token.type = TK_END;
1804         add_token(rules, &end_token);
1805
1806         /* shrink allocated token and string buffer */
1807         if (rules->token_cur < rules->token_max) {
1808                 struct token *tokens;
1809
1810                 tokens = realloc(rules->tokens, rules->token_cur * sizeof(struct token));
1811                 if (tokens != NULL || rules->token_cur == 0) {
1812                         rules->tokens = tokens;
1813                         rules->token_max = rules->token_cur;
1814                 }
1815         }
1816         if (rules->buf_cur < rules->buf_max) {
1817                 char *buf;
1818
1819                 buf = realloc(rules->buf, rules->buf_cur);
1820                 if (buf != NULL || rules->buf_cur == 0) {
1821                         rules->buf = buf;
1822                         rules->buf_max = rules->buf_cur;
1823                 }
1824         }
1825         info(udev, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1826              rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
1827         info(udev, "temporary index used %zu bytes (%u * %zu bytes)\n",
1828              rules->trie_nodes_cur * sizeof(struct trie_node),
1829              rules->trie_nodes_cur, sizeof(struct trie_node));
1830
1831         /* cleanup trie */
1832         free(rules->trie_nodes);
1833         rules->trie_nodes = NULL;
1834         rules->trie_nodes_cur = 0;
1835         rules->trie_nodes_max = 0;
1836
1837         /* cleanup uid/gid cache */
1838         free(rules->uids);
1839         rules->uids = NULL;
1840         rules->uids_cur = 0;
1841         rules->uids_max = 0;
1842         free(rules->gids);
1843         rules->gids = NULL;
1844         rules->gids_cur = 0;
1845         rules->gids_max = 0;
1846
1847         dump_rules(rules);
1848         return rules;
1849 }
1850
1851 void udev_rules_unref(struct udev_rules *rules)
1852 {
1853         if (rules == NULL)
1854                 return;
1855         free(rules->tokens);
1856         free(rules->buf);
1857         free(rules->trie_nodes);
1858         free(rules->uids);
1859         free(rules->gids);
1860         free(rules);
1861 }
1862
1863 static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1864 {
1865         char *key_value = &rules->buf[token->key.value_off];
1866         char *pos;
1867         int match = 0;
1868
1869         if (val == NULL)
1870                 val = "";
1871
1872         switch (token->key.glob) {
1873         case GL_PLAIN:
1874                 match = (strcmp(key_value, val) == 0);
1875                 break;
1876         case GL_GLOB:
1877                 match = (fnmatch(key_value, val, 0) == 0);
1878                 break;
1879         case GL_SPLIT:
1880                 {
1881                         const char *split;
1882                         size_t len;
1883
1884                         split = &rules->buf[token->key.value_off];
1885                         len = strlen(val);
1886                         while (1) {
1887                                 const char *next;
1888
1889                                 next = strchr(split, '|');
1890                                 if (next != NULL) {
1891                                         size_t matchlen = (size_t)(next - split);
1892
1893                                         match = (matchlen == len && strncmp(split, val, matchlen) == 0);
1894                                         if (match)
1895                                                 break;
1896                                 } else {
1897                                         match = (strcmp(split, val) == 0);
1898                                         break;
1899                                 }
1900                                 split = &next[1];
1901                         }
1902                         break;
1903                 }
1904         case GL_SPLIT_GLOB:
1905                 {
1906                         char value[UTIL_PATH_SIZE];
1907
1908                         util_strscpy(value, sizeof(value), &rules->buf[token->key.value_off]);
1909                         key_value = value;
1910                         while (key_value != NULL) {
1911                                 pos = strchr(key_value, '|');
1912                                 if (pos != NULL) {
1913                                         pos[0] = '\0';
1914                                         pos = &pos[1];
1915                                 }
1916                                 dbg(rules->udev, "match %s '%s' <-> '%s'\n", token_str(token->type), key_value, val);
1917                                 match = (fnmatch(key_value, val, 0) == 0);
1918                                 if (match)
1919                                         break;
1920                                 key_value = pos;
1921                         }
1922                         break;
1923                 }
1924         case GL_SOMETHING:
1925                 match = (val[0] != '\0');
1926                 break;
1927         case GL_UNSET:
1928                 return -1;
1929         }
1930
1931         if (match && (token->key.op == OP_MATCH)) {
1932                 dbg(rules->udev, "%s is true (matching value)\n", token_str(token->type));
1933                 return 0;
1934         }
1935         if (!match && (token->key.op == OP_NOMATCH)) {
1936                 dbg(rules->udev, "%s is true (non-matching value)\n", token_str(token->type));
1937                 return 0;
1938         }
1939         dbg(rules->udev, "%s is not true\n", token_str(token->type));
1940         return -1;
1941 }
1942
1943 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1944 {
1945         const char *name;
1946         char nbuf[UTIL_NAME_SIZE];
1947         const char *value;
1948         char vbuf[UTIL_NAME_SIZE];
1949         size_t len;
1950
1951         name = &rules->buf[cur->key.attr_off];
1952         switch (cur->key.attrsubst) {
1953         case SB_FORMAT:
1954                 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1955                 name = nbuf;
1956                 /* fall through */
1957         case SB_NONE:
1958                 value = udev_device_get_sysattr_value(dev, name);
1959                 if (value == NULL)
1960                         return -1;
1961                 break;
1962         case SB_SUBSYS:
1963                 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1964                         return -1;
1965                 value = vbuf;
1966                 break;
1967         default:
1968                 return -1;
1969         }
1970
1971         /* remove trailing whitespace, if not asked to match for it */
1972         len = strlen(value);
1973         if (len > 0 && isspace(value[len-1])) {
1974                 const char *key_value;
1975                 size_t klen;
1976
1977                 key_value = &rules->buf[cur->key.value_off];
1978                 klen = strlen(key_value);
1979                 if (klen > 0 && !isspace(key_value[klen-1])) {
1980                         if (value != vbuf) {
1981                                 util_strscpy(vbuf, sizeof(vbuf), value);
1982                                 value = vbuf;
1983                         }
1984                         while (len > 0 && isspace(vbuf[--len]))
1985                                 vbuf[len] = '\0';
1986                         dbg(rules->udev, "removed trailing whitespace from '%s'\n", value);
1987                 }
1988         }
1989
1990         return match_key(rules, cur, value);
1991 }
1992
1993 enum escape_type {
1994         ESCAPE_UNSET,
1995         ESCAPE_NONE,
1996         ESCAPE_REPLACE,
1997 };
1998
1999 int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event)
2000 {
2001         struct token *cur;
2002         struct token *rule;
2003         enum escape_type esc = ESCAPE_UNSET;
2004         int can_set_name;
2005
2006         if (rules->tokens == NULL)
2007                 return -1;
2008
2009         can_set_name = ((strcmp(udev_device_get_action(event->dev), "add") == 0 ||
2010                          strcmp(udev_device_get_action(event->dev), "change") == 0) &&
2011                         (major(udev_device_get_devnum(event->dev)) > 0 ||
2012                          strcmp(udev_device_get_subsystem(event->dev), "net") == 0));
2013
2014         /* loop through token list, match, run actions or forward to next rule */
2015         cur = &rules->tokens[0];
2016         rule = cur;
2017         while (1) {
2018                 dump_token(rules, cur);
2019                 switch (cur->type) {
2020                 case TK_RULE:
2021                         /* current rule */
2022                         rule = cur;
2023                         /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2024                         if (!can_set_name && rule->rule.flags)
2025                                 goto nomatch;
2026                         esc = ESCAPE_UNSET;
2027                         break;
2028                 case TK_M_ACTION:
2029                         if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
2030                                 goto nomatch;
2031                         break;
2032                 case TK_M_DEVPATH:
2033                         if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
2034                                 goto nomatch;
2035                         break;
2036                 case TK_M_KERNEL:
2037                         if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
2038                                 goto nomatch;
2039                         break;
2040                 case TK_M_DEVLINK:
2041                         {
2042                                 size_t devlen = strlen(udev_get_dev_path(event->udev))+1;
2043                                 struct udev_list_entry *list_entry;
2044                                 int match = 0;
2045
2046                                 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
2047                                         const char *devlink;
2048
2049                                         devlink =  &udev_list_entry_get_name(list_entry)[devlen];
2050                                         if (match_key(rules, cur, devlink) == 0) {
2051                                                 match = 1;
2052                                                 break;
2053                                         }
2054                                 }
2055                                 if (!match)
2056                                         goto nomatch;
2057                                 break;
2058                         }
2059                 case TK_M_NAME:
2060                         if (match_key(rules, cur, event->name) != 0)
2061                                 goto nomatch;
2062                         break;
2063                 case TK_M_ENV:
2064                         {
2065                                 const char *key_name = &rules->buf[cur->key.attr_off];
2066                                 const char *value;
2067
2068                                 value = udev_device_get_property_value(event->dev, key_name);
2069                                 if (value == NULL) {
2070                                         dbg(event->udev, "ENV{%s} is not set, treat as empty\n", key_name);
2071                                         value = "";
2072                                 }
2073                                 if (match_key(rules, cur, value))
2074                                         goto nomatch;
2075                                 break;
2076                         }
2077                 case TK_M_SUBSYSTEM:
2078                         if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
2079                                 goto nomatch;
2080                         break;
2081                 case TK_M_DRIVER:
2082                         if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2083                                 goto nomatch;
2084                         break;
2085                 case TK_M_WAITFOR:
2086                         {
2087                                 char filename[UTIL_PATH_SIZE];
2088                                 int found;
2089
2090                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2091                                 found = (wait_for_file(event->dev, filename, 10) == 0);
2092                                 if (!found && (cur->key.op != OP_NOMATCH))
2093                                         goto nomatch;
2094                                 break;
2095                         }
2096                 case TK_M_ATTR:
2097                         if (match_attr(rules, event->dev, event, cur) != 0)
2098                                 goto nomatch;
2099                         break;
2100                 case TK_M_KERNELS:
2101                 case TK_M_SUBSYSTEMS:
2102                 case TK_M_DRIVERS:
2103                 case TK_M_ATTRS:
2104                         {
2105                                 struct token *next;
2106
2107                                 /* get whole sequence of parent matches */
2108                                 next = cur;
2109                                 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2110                                         next++;
2111
2112                                 /* loop over parents */
2113                                 event->dev_parent = event->dev;
2114                                 while (1) {
2115                                         struct token *key;
2116
2117                                         dbg(event->udev, "parent: '%s'\n", udev_device_get_syspath(event->dev_parent));
2118                                         /* loop over sequence of parent match keys */
2119                                         for (key = cur; key < next; key++ ) {
2120                                                 dump_token(rules, key);
2121                                                 switch(key->type) {
2122                                                 case TK_M_KERNELS:
2123                                                         if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2124                                                                 goto try_parent;
2125                                                         break;
2126                                                 case TK_M_SUBSYSTEMS:
2127                                                         if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2128                                                                 goto try_parent;
2129                                                         break;
2130                                                 case TK_M_DRIVERS:
2131                                                         if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2132                                                                 goto try_parent;
2133                                                         break;
2134                                                 case TK_M_ATTRS:
2135                                                         if (match_attr(rules, event->dev_parent, event, key) != 0)
2136                                                                 goto try_parent;
2137                                                         break;
2138                                                 default:
2139                                                         goto nomatch;
2140                                                 }
2141                                                 dbg(event->udev, "parent key matched\n");
2142                                         }
2143                                         dbg(event->udev, "all parent keys matched\n");
2144                                         /* all keys matched */
2145                                         break;
2146
2147                                 try_parent:
2148                                         event->dev_parent = udev_device_get_parent(event->dev_parent);
2149                                         if (event->dev_parent == NULL)
2150                                                 goto nomatch;
2151                                 }
2152                                 /* move behind our sequence of parent match keys */
2153                                 cur = next;
2154                                 continue;
2155                         }
2156                 case TK_M_TEST:
2157                         {
2158                                 char filename[UTIL_PATH_SIZE];
2159                                 struct stat statbuf;
2160                                 int match;
2161
2162                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], filename, sizeof(filename));
2163                                 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2164                                         if (filename[0] != '/') {
2165                                                 char tmp[UTIL_PATH_SIZE];
2166
2167                                                 util_strscpy(tmp, sizeof(tmp), filename);
2168                                                 util_strscpyl(filename, sizeof(filename),
2169                                                               udev_device_get_syspath(event->dev), "/", tmp, NULL);
2170                                         }
2171                                 }
2172                                 attr_subst_subdir(filename, sizeof(filename));
2173
2174                                 match = (stat(filename, &statbuf) == 0);
2175                                 dbg(event->udev, "'%s' %s", filename, match ? "exists\n" : "does not exist\n");
2176                                 if (match && cur->key.mode > 0) {
2177                                         match = ((statbuf.st_mode & cur->key.mode) > 0);
2178                                         dbg(event->udev, "'%s' has mode=%#o and %s %#o\n", filename, statbuf.st_mode,
2179                                             match ? "matches" : "does not match", cur->key.mode);
2180                                 }
2181                                 if (match && cur->key.op == OP_NOMATCH)
2182                                         goto nomatch;
2183                                 if (!match && cur->key.op == OP_MATCH)
2184                                         goto nomatch;
2185                                 break;
2186                         }
2187                 case TK_M_PROGRAM:
2188                         {
2189                                 char program[UTIL_PATH_SIZE];
2190                                 char **envp;
2191                                 char result[UTIL_PATH_SIZE];
2192
2193                                 free(event->program_result);
2194                                 event->program_result = NULL;
2195                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], program, sizeof(program));
2196                                 envp = udev_device_get_properties_envp(event->dev);
2197                                 info(event->udev, "PROGRAM '%s' %s:%u\n",
2198                                      program,
2199                                      &rules->buf[rule->rule.filename_off],
2200                                      rule->rule.filename_line);
2201                                 if (util_run_program(event->udev, program, envp, result, sizeof(result), NULL) != 0) {
2202                                         if (cur->key.op != OP_NOMATCH)
2203                                                 goto nomatch;
2204                                 } else {
2205                                         int count;
2206
2207                                         util_remove_trailing_chars(result, '\n');
2208                                         if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2209                                                 count = udev_util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2210                                                 if (count > 0)
2211                                                         info(event->udev, "%i character(s) replaced\n" , count);
2212                                         }
2213                                         event->program_result = strdup(result);
2214                                         dbg(event->udev, "storing result '%s'\n", event->program_result);
2215                                         if (cur->key.op == OP_NOMATCH)
2216                                                 goto nomatch;
2217                                 }
2218                                 break;
2219                         }
2220                 case TK_M_IMPORT_FILE:
2221                         {
2222                                 char import[UTIL_PATH_SIZE];
2223
2224                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2225                                 if (import_file_into_properties(event->dev, import) != 0)
2226                                         if (cur->key.op != OP_NOMATCH)
2227                                                 goto nomatch;
2228                                 break;
2229                         }
2230                 case TK_M_IMPORT_PROG:
2231                         {
2232                                 char import[UTIL_PATH_SIZE];
2233
2234                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2235                                 info(event->udev, "IMPORT '%s' %s:%u\n",
2236                                      import,
2237                                      &rules->buf[rule->rule.filename_off],
2238                                      rule->rule.filename_line);
2239                                 if (import_program_into_properties(event->dev, import) != 0)
2240                                         if (cur->key.op != OP_NOMATCH)
2241                                                 goto nomatch;
2242                                 break;
2243                         }
2244                 case TK_M_IMPORT_PARENT:
2245                         {
2246                                 char import[UTIL_PATH_SIZE];
2247
2248                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], import, sizeof(import));
2249                                 if (import_parent_into_properties(event->dev, import) != 0)
2250                                         if (cur->key.op != OP_NOMATCH)
2251                                                 goto nomatch;
2252                                 break;
2253                         }
2254                 case TK_M_RESULT:
2255                         if (match_key(rules, cur, event->program_result) != 0)
2256                                 goto nomatch;
2257                         break;
2258
2259                 case TK_A_IGNORE_DEVICE:
2260                         event->ignore_device = 1;
2261                         return 0;
2262                         break;
2263                 case TK_A_STRING_ESCAPE_NONE:
2264                         esc = ESCAPE_NONE;
2265                         break;
2266                 case TK_A_STRING_ESCAPE_REPLACE:
2267                         esc = ESCAPE_REPLACE;
2268                         break;
2269                 case TK_A_NUM_FAKE_PART:
2270                         if (strcmp(udev_device_get_subsystem(event->dev), "block") != 0)
2271                                 break;
2272                         if (udev_device_get_sysnum(event->dev) != NULL)
2273                                 break;
2274                         udev_device_set_num_fake_partitions(event->dev, cur->key.num_fake_part);
2275                         break;
2276                 case TK_A_INOTIFY_WATCH:
2277                         event->inotify_watch = cur->key.watch;
2278                         break;
2279                 case TK_A_DEVLINK_PRIO:
2280                         udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2281                         break;
2282                 case TK_A_OWNER:
2283                         {
2284                                 char owner[UTIL_NAME_SIZE];
2285
2286                                 if (event->owner_final)
2287                                         break;
2288                                 if (cur->key.op == OP_ASSIGN_FINAL)
2289                                         event->owner_final = 1;
2290                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], owner, sizeof(owner));
2291                                 event->uid = util_lookup_user(event->udev, owner);
2292                                 info(event->udev, "OWNER %u %s:%u\n",
2293                                      event->uid,
2294                                      &rules->buf[rule->rule.filename_off],
2295                                      rule->rule.filename_line);
2296                                 break;
2297                         }
2298                 case TK_A_GROUP:
2299                         {
2300                                 char group[UTIL_NAME_SIZE];
2301
2302                                 if (event->group_final)
2303                                         break;
2304                                 if (cur->key.op == OP_ASSIGN_FINAL)
2305                                         event->group_final = 1;
2306                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], group, sizeof(group));
2307                                 event->gid = util_lookup_group(event->udev, group);
2308                                 info(event->udev, "GROUP %u %s:%u\n",
2309                                      event->gid,
2310                                      &rules->buf[rule->rule.filename_off],
2311                                      rule->rule.filename_line);
2312                                 break;
2313                         }
2314                 case TK_A_MODE:
2315                         {
2316                                 char mode[UTIL_NAME_SIZE];
2317                                 char *endptr;
2318
2319                                 if (event->mode_final)
2320                                         break;
2321                                 if (cur->key.op == OP_ASSIGN_FINAL)
2322                                         event->mode_final = 1;
2323                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], mode, sizeof(mode));
2324                                 event->mode = strtol(mode, &endptr, 8);
2325                                 if (endptr[0] != '\0') {
2326                                         err(event->udev, "invalide mode '%s' set default mode 0660\n", mode);
2327                                         event->mode = 0660;
2328                                 }
2329                                 info(event->udev, "MODE %#o %s:%u\n",
2330                                      event->mode,
2331                                      &rules->buf[rule->rule.filename_off],
2332                                      rule->rule.filename_line);
2333                                 break;
2334                         }
2335                 case TK_A_OWNER_ID:
2336                         if (event->owner_final)
2337                                 break;
2338                         if (cur->key.op == OP_ASSIGN_FINAL)
2339                                 event->owner_final = 1;
2340                         event->uid = cur->key.uid;
2341                         info(event->udev, "OWNER %u %s:%u\n",
2342                              event->uid,
2343                              &rules->buf[rule->rule.filename_off],
2344                              rule->rule.filename_line);
2345                         break;
2346                 case TK_A_GROUP_ID:
2347                         if (event->group_final)
2348                                 break;
2349                         if (cur->key.op == OP_ASSIGN_FINAL)
2350                                 event->group_final = 1;
2351                         event->gid = cur->key.gid;
2352                         info(event->udev, "GROUP %u %s:%u\n",
2353                              event->gid,
2354                              &rules->buf[rule->rule.filename_off],
2355                              rule->rule.filename_line);
2356                         break;
2357                 case TK_A_MODE_ID:
2358                         if (event->mode_final)
2359                                 break;
2360                         if (cur->key.op == OP_ASSIGN_FINAL)
2361                                 event->mode_final = 1;
2362                         event->mode = cur->key.mode;
2363                         info(event->udev, "MODE %#o %s:%u\n",
2364                              event->mode,
2365                              &rules->buf[rule->rule.filename_off],
2366                              rule->rule.filename_line);
2367                         break;
2368                 case TK_A_ENV:
2369                         {
2370                                 const char *name = &rules->buf[cur->key.attr_off];
2371                                 char *value = &rules->buf[cur->key.value_off];
2372
2373                                 if (value[0] != '\0') {
2374                                         char temp_value[UTIL_NAME_SIZE];
2375                                         struct udev_list_entry *entry;
2376
2377                                         udev_event_apply_format(event, value, temp_value, sizeof(temp_value));
2378                                         entry = udev_device_add_property(event->dev, name, temp_value);
2379                                         /* store in db, skip private keys */
2380                                         if (name[0] != '.')
2381                                                 udev_list_entry_set_flag(entry, 1);
2382                                 } else {
2383                                         udev_device_add_property(event->dev, name, NULL);
2384                                 }
2385                                 break;
2386                         }
2387                 case TK_A_NAME:
2388                         {
2389                                 const char *name  = &rules->buf[cur->key.value_off];
2390                                 char name_str[UTIL_PATH_SIZE];
2391                                 int count;
2392
2393                                 if (event->name_final)
2394                                         break;
2395                                 if (cur->key.op == OP_ASSIGN_FINAL)
2396                                         event->name_final = 1;
2397                                 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2398                                 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2399                                         count = udev_util_replace_chars(name_str, "/");
2400                                         if (count > 0)
2401                                                 info(event->udev, "%i character(s) replaced\n", count);
2402                                 }
2403                                 free(event->name);
2404                                 event->name = strdup(name_str);
2405                                 info(event->udev, "NAME '%s' %s:%u\n",
2406                                      event->name,
2407                                      &rules->buf[rule->rule.filename_off],
2408                                      rule->rule.filename_line);
2409                                 break;
2410                         }
2411                 case TK_A_DEVLINK:
2412                         {
2413                                 char temp[UTIL_PATH_SIZE];
2414                                 char filename[UTIL_PATH_SIZE];
2415                                 char *pos, *next;
2416                                 int count = 0;
2417
2418                                 if (event->devlink_final)
2419                                         break;
2420                                 if (major(udev_device_get_devnum(event->dev)) == 0)
2421                                         break;
2422                                 if (cur->key.op == OP_ASSIGN_FINAL)
2423                                         event->devlink_final = 1;
2424                                 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2425                                         udev_device_cleanup_devlinks_list(event->dev);
2426
2427                                 /* allow  multiple symlinks separated by spaces */
2428                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], temp, sizeof(temp));
2429                                 if (esc == ESCAPE_UNSET)
2430                                         count = udev_util_replace_chars(temp, "/ ");
2431                                 else if (esc == ESCAPE_REPLACE)
2432                                         count = udev_util_replace_chars(temp, "/");
2433                                 if (count > 0)
2434                                         info(event->udev, "%i character(s) replaced\n" , count);
2435                                 dbg(event->udev, "rule applied, added symlink(s) '%s'\n", temp);
2436                                 pos = temp;
2437                                 while (isspace(pos[0]))
2438                                         pos++;
2439                                 next = strchr(pos, ' ');
2440                                 while (next) {
2441                                         next[0] = '\0';
2442                                         info(event->udev, "LINK '%s' %s:%u\n",
2443                                              pos,
2444                                              &rules->buf[rule->rule.filename_off],
2445                                              rule->rule.filename_line);
2446                                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2447                                         udev_device_add_devlink(event->dev, filename);
2448                                         while (isspace(next[1]))
2449                                                 next++;
2450                                         pos = &next[1];
2451                                         next = strchr(pos, ' ');
2452                                 }
2453                                 if (pos[0] != '\0') {
2454                                         info(event->udev, "LINK '%s' %s:%u\n",
2455                                              pos,
2456                                              &rules->buf[rule->rule.filename_off],
2457                                              rule->rule.filename_line);
2458                                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(event->udev), "/", pos, NULL);
2459                                         udev_device_add_devlink(event->dev, filename);
2460                                 }
2461                         }
2462                         break;
2463                 case TK_A_EVENT_TIMEOUT:
2464                         udev_device_set_event_timeout(event->dev, cur->key.event_timeout);
2465                         break;
2466                 case TK_A_IGNORE_REMOVE:
2467                         udev_device_set_ignore_remove(event->dev, 1);
2468                         break;
2469                 case TK_A_ATTR:
2470                         {
2471                                 const char *key_name = &rules->buf[cur->key.attr_off];
2472                                 char attr[UTIL_PATH_SIZE];
2473                                 char value[UTIL_NAME_SIZE];
2474                                 FILE *f;
2475
2476                                 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2477                                         util_strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2478                                 attr_subst_subdir(attr, sizeof(attr));
2479
2480                                 udev_event_apply_format(event, &rules->buf[cur->key.value_off], value, sizeof(value));
2481                                 info(event->udev, "ATTR '%s' writing '%s' %s:%u\n", attr, value,
2482                                      &rules->buf[rule->rule.filename_off],
2483                                      rule->rule.filename_line);
2484                                 f = fopen(attr, "w");
2485                                 if (f != NULL) {
2486                                         if (fprintf(f, "%s", value) <= 0)
2487                                                 err(event->udev, "error writing ATTR{%s}: %m\n", attr);
2488                                         fclose(f);
2489                                 } else {
2490                                         err(event->udev, "error opening ATTR{%s} for writing: %m\n", attr);
2491                                 }
2492                                 break;
2493                         }
2494                 case TK_A_RUN:
2495                         {
2496                                 struct udev_list_entry *list_entry;
2497
2498                                 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2499                                         udev_list_cleanup_entries(event->udev, &event->run_list);
2500                                 info(event->udev, "RUN '%s' %s:%u\n",
2501                                      &rules->buf[cur->key.value_off],
2502                                      &rules->buf[rule->rule.filename_off],
2503                                      rule->rule.filename_line);
2504                                 list_entry = udev_list_entry_add(event->udev, &event->run_list,
2505                                                                  &rules->buf[cur->key.value_off], NULL, 1, 0);
2506                                 if (cur->key.ignore_error)
2507                                         udev_list_entry_set_flag(list_entry, 1);
2508                                 break;
2509                         }
2510                 case TK_A_GOTO:
2511                         if (cur->key.rule_goto == 0)
2512                                 break;
2513                         cur = &rules->tokens[cur->key.rule_goto];
2514                         continue;
2515                 case TK_A_LAST_RULE:
2516                 case TK_END:
2517                         return 0;
2518
2519                 case TK_M_PARENTS_MIN:
2520                 case TK_M_PARENTS_MAX:
2521                 case TK_M_MAX:
2522                 case TK_UNSET:
2523                         err(rules->udev, "wrong type %u\n", cur->type);
2524                         goto nomatch;
2525                 }
2526
2527                 cur++;
2528                 continue;
2529         nomatch:
2530                 /* fast-forward to next rule */
2531                 cur = rule + rule->rule.token_count;
2532                 dbg(rules->udev, "forward to rule: %u\n",
2533                                  (unsigned int) (cur - rules->tokens));
2534         }
2535 }