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