2 * Copyright (C) 2003-2009 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
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.
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.
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/>.
34 #define PREALLOC_TOKEN 2048
35 #define PREALLOC_STRBUF 32 * 1024
36 #define PREALLOC_TRIE 256
39 unsigned int name_off
;
47 /* this node's first child */
48 unsigned int child_idx
;
49 /* the next child of our parent node's child list */
50 unsigned int next_child_idx
;
51 /* this node's last child (shortcut for append) */
52 unsigned int last_child_idx
;
53 unsigned int value_off
;
54 unsigned short value_len
;
62 /* every key in the rules file becomes a token */
64 unsigned int token_cur
;
65 unsigned int token_max
;
67 /* all key strings are copied to a single string buffer */
71 unsigned int buf_count
;
73 /* during rule parsing, strings are indexed to find duplicates */
74 struct trie_node
*trie_nodes
;
75 unsigned int trie_nodes_cur
;
76 unsigned int trie_nodes_max
;
78 /* during rule parsing, uid/gid lookup results are cached */
80 unsigned int uids_cur
;
81 unsigned int uids_max
;
83 unsigned int gids_cur
;
84 unsigned int gids_max
;
87 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
100 enum string_glob_type
{
102 GL_PLAIN
, /* no special chars */
103 GL_GLOB
, /* shell globs ?,*,[] */
104 GL_SPLIT
, /* multi-value A|B */
105 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
106 GL_SOMETHING
, /* commonly used "?*" */
109 enum string_subst_type
{
116 /* tokens of a rule are sorted/handled in this order */
121 TK_M_ACTION
, /* val */
122 TK_M_DEVPATH
, /* val */
123 TK_M_KERNEL
, /* val */
124 TK_M_DEVLINK
, /* val */
126 TK_M_ENV
, /* val, attr */
127 TK_M_SUBSYSTEM
, /* val */
128 TK_M_DRIVER
, /* val */
129 TK_M_WAITFOR
, /* val */
130 TK_M_ATTR
, /* val, attr */
133 TK_M_KERNELS
, /* val */
134 TK_M_SUBSYSTEMS
, /* val */
135 TK_M_DRIVERS
, /* val */
136 TK_M_ATTRS
, /* val, attr */
139 TK_M_TEST
, /* val, mode_t */
140 TK_M_PROGRAM
, /* val */
141 TK_M_IMPORT_FILE
, /* val */
142 TK_M_IMPORT_PROG
, /* val */
143 TK_M_IMPORT_PARENT
, /* val */
144 TK_M_RESULT
, /* val */
148 TK_A_STRING_ESCAPE_NONE
,
149 TK_A_STRING_ESCAPE_REPLACE
,
150 TK_A_INOTIFY_WATCH
, /* int */
151 TK_A_NUM_FAKE_PART
, /* int */
152 TK_A_DEVLINK_PRIO
, /* int */
153 TK_A_OWNER
, /* val */
154 TK_A_GROUP
, /* val */
156 TK_A_OWNER_ID
, /* uid_t */
157 TK_A_GROUP_ID
, /* gid_t */
158 TK_A_MODE_ID
, /* mode_t */
159 TK_A_ENV
, /* val, attr */
161 TK_A_DEVLINK
, /* val */
162 TK_A_EVENT_TIMEOUT
, /* int */
164 TK_A_ATTR
, /* val, attr */
165 TK_A_RUN
, /* val, bool */
166 TK_A_GOTO
, /* size_t */
172 /* we try to pack stuff in a way that we take only 12 bytes per token */
175 unsigned char type
; /* same as in rule and key */
177 enum token_type type
:8;
178 unsigned int flags
:8;
179 unsigned short token_count
;
180 unsigned int label_off
;
181 unsigned short filename_off
;
182 unsigned short filename_line
;
185 enum token_type type
:8;
186 enum operation_type op
:8;
187 enum string_glob_type glob
:8;
188 enum string_subst_type subst
:4;
189 enum string_subst_type attrsubst
:4;
190 unsigned int value_off
;
192 unsigned int attr_off
;
194 unsigned int rule_goto
;
209 struct udev_rules
*rules
;
211 struct token token
[MAX_TK
];
212 unsigned int token_cur
;
216 static const char *operation_str(enum operation_type type
)
218 static const char *operation_strs
[] = {
219 [OP_UNSET
] = "UNSET",
220 [OP_MATCH
] = "match",
221 [OP_NOMATCH
] = "nomatch",
222 [OP_MATCH_MAX
] = "MATCH_MAX",
225 [OP_ASSIGN
] = "assign",
226 [OP_ASSIGN_FINAL
] = "assign-final",
229 return operation_strs
[type
];
232 static const char *string_glob_str(enum string_glob_type type
)
234 static const char *string_glob_strs
[] = {
235 [GL_UNSET
] = "UNSET",
236 [GL_PLAIN
] = "plain",
238 [GL_SPLIT
] = "split",
239 [GL_SPLIT_GLOB
] = "split-glob",
240 [GL_SOMETHING
] = "split-glob",
243 return string_glob_strs
[type
];
246 static const char *token_str(enum token_type type
)
248 static const char *token_strs
[] = {
249 [TK_UNSET
] = "UNSET",
252 [TK_M_ACTION
] = "M ACTION",
253 [TK_M_DEVPATH
] = "M DEVPATH",
254 [TK_M_KERNEL
] = "M KERNEL",
255 [TK_M_DEVLINK
] = "M DEVLINK",
256 [TK_M_NAME
] = "M NAME",
257 [TK_M_ENV
] = "M ENV",
258 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
259 [TK_M_DRIVER
] = "M DRIVER",
260 [TK_M_WAITFOR
] = "M WAITFOR",
261 [TK_M_ATTR
] = "M ATTR",
263 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
264 [TK_M_KERNELS
] = "M KERNELS",
265 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
266 [TK_M_DRIVERS
] = "M DRIVERS",
267 [TK_M_ATTRS
] = "M ATTRS",
268 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
270 [TK_M_TEST
] = "M TEST",
271 [TK_M_PROGRAM
] = "M PROGRAM",
272 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
273 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
274 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
275 [TK_M_RESULT
] = "M RESULT",
276 [TK_M_MAX
] = "M MAX",
278 [TK_A_IGNORE_DEVICE
] = "A IGNORE_DEVICE",
279 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
280 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
281 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
282 [TK_A_NUM_FAKE_PART
] = "A NUM_FAKE_PART",
283 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
284 [TK_A_OWNER
] = "A OWNER",
285 [TK_A_GROUP
] = "A GROUP",
286 [TK_A_MODE
] = "A MODE",
287 [TK_A_OWNER_ID
] = "A OWNER_ID",
288 [TK_A_GROUP_ID
] = "A GROUP_ID",
289 [TK_A_MODE_ID
] = "A MODE_ID",
290 [TK_A_ENV
] = "A ENV",
291 [TK_A_NAME
] = "A NAME",
292 [TK_A_DEVLINK
] = "A DEVLINK",
293 [TK_A_EVENT_TIMEOUT
] = "A EVENT_TIMEOUT",
294 [TK_A_IGNORE_REMOVE
] = "A IGNORE_REMOVE",
295 [TK_A_ATTR
] = "A ATTR",
296 [TK_A_RUN
] = "A RUN",
297 [TK_A_GOTO
] = "A GOTO",
298 [TK_A_LAST_RULE
] = "A LAST_RULE",
303 return token_strs
[type
];
306 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
308 enum token_type type
= token
->type
;
309 enum operation_type op
= token
->key
.op
;
310 enum string_glob_type glob
= token
->key
.glob
;
311 const char *value
= &rules
->buf
[token
->key
.value_off
];
312 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
317 const char *tks_ptr
= (char *)rules
->tokens
;
318 const char *tk_ptr
= (char *)token
;
319 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
321 dbg(rules
->udev
, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
322 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
323 idx
, token
->rule
.token_count
,
324 &rules
->buf
[token
->rule
.label_off
],
337 case TK_M_SUBSYSTEMS
:
340 case TK_M_IMPORT_FILE
:
341 case TK_M_IMPORT_PROG
:
342 case TK_M_IMPORT_PARENT
:
350 dbg(rules
->udev
, "%s %s '%s'(%s)\n",
351 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
358 dbg(rules
->udev
, "%s %s '%s' '%s'(%s)\n",
359 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
361 case TK_A_IGNORE_DEVICE
:
362 case TK_A_STRING_ESCAPE_NONE
:
363 case TK_A_STRING_ESCAPE_REPLACE
:
365 case TK_A_IGNORE_REMOVE
:
366 dbg(rules
->udev
, "%s\n", token_str(type
));
369 dbg(rules
->udev
, "%s %s '%s'(%s) %#o\n",
370 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
372 case TK_A_INOTIFY_WATCH
:
373 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.watch
);
375 case TK_A_NUM_FAKE_PART
:
376 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.num_fake_part
);
378 case TK_A_DEVLINK_PRIO
:
379 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.devlink_prio
);
382 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.uid
);
385 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.gid
);
388 dbg(rules
->udev
, "%s %s %#o\n", token_str(type
), operation_str(op
), token
->key
.mode
);
390 case TK_A_EVENT_TIMEOUT
:
391 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.event_timeout
);
394 dbg(rules
->udev
, "%s '%s' %u\n", token_str(type
), value
, token
->key
.rule_goto
);
397 dbg(rules
->udev
, "* %s\n", token_str(type
));
399 case TK_M_PARENTS_MIN
:
400 case TK_M_PARENTS_MAX
:
403 dbg(rules
->udev
, "unknown type %u\n", type
);
408 static void dump_rules(struct udev_rules
*rules
)
412 dbg(rules
->udev
, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
414 rules
->token_cur
* sizeof(struct token
),
417 for(i
= 0; i
< rules
->token_cur
; i
++)
418 dump_token(rules
, &rules
->tokens
[i
]);
421 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
422 static inline const char *token_str(enum token_type type
) { return NULL
; }
423 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
424 static inline void dump_rules(struct udev_rules
*rules
) {}
425 #endif /* ENABLE_DEBUG */
427 static int add_new_string(struct udev_rules
*rules
, const char *str
, size_t bytes
)
431 /* grow buffer if needed */
432 if (rules
->buf_cur
+ bytes
+1 >= rules
->buf_max
) {
436 /* double the buffer size */
437 add
= rules
->buf_max
;
441 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
444 dbg(rules
->udev
, "extend buffer from %zu to %zu\n", rules
->buf_max
, rules
->buf_max
+ add
);
446 rules
->buf_max
+= add
;
448 off
= rules
->buf_cur
;
449 memcpy(&rules
->buf
[rules
->buf_cur
], str
, bytes
);
450 rules
->buf_cur
+= bytes
;
455 static int add_string(struct udev_rules
*rules
, const char *str
)
457 unsigned int node_idx
;
458 struct trie_node
*new_node
;
459 unsigned int new_node_idx
;
464 struct trie_node
*parent
;
466 /* walk trie, start from last character of str to find matching tails */
470 for (depth
= 0; depth
<= len
; depth
++) {
471 struct trie_node
*node
;
472 unsigned int child_idx
;
474 node
= &rules
->trie_nodes
[node_idx
];
475 off
= node
->value_off
+ node
->value_len
- len
;
477 /* match against current node */
478 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(&rules
->buf
[off
], str
, len
) == 0))
481 /* lookup child node */
482 key
= str
[len
- 1 - depth
];
483 child_idx
= node
->child_idx
;
484 while (child_idx
> 0) {
485 struct trie_node
*child
;
487 child
= &rules
->trie_nodes
[child_idx
];
488 if (child
->key
== key
)
490 child_idx
= child
->next_child_idx
;
494 node_idx
= child_idx
;
497 /* string not found, add it */
498 off
= add_new_string(rules
, str
, len
+ 1);
500 /* grow trie nodes if needed */
501 if (rules
->trie_nodes_cur
>= rules
->trie_nodes_max
) {
502 struct trie_node
*nodes
;
505 /* double the buffer size */
506 add
= rules
->trie_nodes_max
;
510 nodes
= realloc(rules
->trie_nodes
, (rules
->trie_nodes_max
+ add
) * sizeof(struct trie_node
));
513 dbg(rules
->udev
, "extend trie nodes from %u to %u\n",
514 rules
->trie_nodes_max
, rules
->trie_nodes_max
+ add
);
515 rules
->trie_nodes
= nodes
;
516 rules
->trie_nodes_max
+= add
;
520 new_node_idx
= rules
->trie_nodes_cur
;
521 rules
->trie_nodes_cur
++;
522 new_node
= &rules
->trie_nodes
[new_node_idx
];
523 memset(new_node
, 0x00, sizeof(struct trie_node
));
524 new_node
->value_off
= off
;
525 new_node
->value_len
= len
;
528 /* join the parent's child list */
529 parent
= &rules
->trie_nodes
[node_idx
];
530 if (parent
->child_idx
== 0) {
531 parent
->child_idx
= new_node_idx
;
533 struct trie_node
*last_child
;
535 last_child
= &rules
->trie_nodes
[parent
->last_child_idx
];
536 last_child
->next_child_idx
= new_node_idx
;
538 parent
->last_child_idx
= new_node_idx
;
542 static int add_token(struct udev_rules
*rules
, struct token
*token
)
544 /* grow buffer if needed */
545 if (rules
->token_cur
+1 >= rules
->token_max
) {
546 struct token
*tokens
;
549 /* double the buffer size */
550 add
= rules
->token_max
;
554 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
557 dbg(rules
->udev
, "extend tokens from %u to %u\n", rules
->token_max
, rules
->token_max
+ add
);
558 rules
->tokens
= tokens
;
559 rules
->token_max
+= add
;
561 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
566 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
572 /* lookup, if we know it already */
573 for (i
= 0; i
< rules
->uids_cur
; i
++) {
574 off
= rules
->uids
[i
].name_off
;
575 if (strcmp(&rules
->buf
[off
], owner
) == 0) {
576 uid
= rules
->uids
[i
].uid
;
577 dbg(rules
->udev
, "return existing %u for '%s'\n", uid
, owner
);
581 uid
= util_lookup_user(rules
->udev
, owner
);
583 /* grow buffer if needed */
584 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
585 struct uid_gid
*uids
;
588 /* double the buffer size */
589 add
= rules
->uids_max
;
593 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
596 dbg(rules
->udev
, "extend uids from %u to %u\n", rules
->uids_max
, rules
->uids_max
+ add
);
598 rules
->uids_max
+= add
;
600 rules
->uids
[rules
->uids_cur
].uid
= uid
;
601 off
= add_string(rules
, owner
);
604 rules
->uids
[rules
->uids_cur
].name_off
= off
;
609 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
615 /* lookup, if we know it already */
616 for (i
= 0; i
< rules
->gids_cur
; i
++) {
617 off
= rules
->gids
[i
].name_off
;
618 if (strcmp(&rules
->buf
[off
], group
) == 0) {
619 gid
= rules
->gids
[i
].gid
;
620 dbg(rules
->udev
, "return existing %u for '%s'\n", gid
, group
);
624 gid
= util_lookup_group(rules
->udev
, group
);
626 /* grow buffer if needed */
627 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
628 struct uid_gid
*gids
;
631 /* double the buffer size */
632 add
= rules
->gids_max
;
636 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
639 dbg(rules
->udev
, "extend gids from %u to %u\n", rules
->gids_max
, rules
->gids_max
+ add
);
641 rules
->gids_max
+= add
;
643 rules
->gids
[rules
->gids_cur
].gid
= gid
;
644 off
= add_string(rules
, group
);
647 rules
->gids
[rules
->gids_cur
].name_off
= off
;
652 static int import_property_from_string(struct udev_device
*dev
, char *line
)
654 struct udev
*udev
= udev_device_get_udev(dev
);
661 while (isspace(key
[0]))
664 /* comment or empty line */
665 if (key
[0] == '#' || key
[0] == '\0')
668 /* split key/value */
669 val
= strchr(key
, '=');
676 while (isspace(val
[0]))
683 while (isspace(key
[len
-1]))
687 /* terminate value */
691 while (isspace(val
[len
-1]))
699 if (val
[0] == '"' || val
[0] == '\'') {
700 if (val
[len
-1] != val
[0]) {
701 info(udev
, "inconsistent quoting: '%s', skip\n", line
);
708 dbg(udev
, "adding '%s'='%s'\n", key
, val
);
710 /* handle device, renamed by external tool, returning new path */
711 if (strcmp(key
, "DEVPATH") == 0) {
712 char syspath
[UTIL_PATH_SIZE
];
714 info(udev
, "updating devpath from '%s' to '%s'\n",
715 udev_device_get_devpath(dev
), val
);
716 util_strscpyl(syspath
, sizeof(syspath
), udev_get_sys_path(udev
), val
, NULL
);
717 udev_device_set_syspath(dev
, syspath
);
719 struct udev_list_entry
*entry
;
721 entry
= udev_device_add_property(dev
, key
, val
);
722 /* store in db, skip private keys */
724 udev_list_entry_set_flag(entry
, 1);
729 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
732 char line
[UTIL_LINE_SIZE
];
734 f
= fopen(filename
, "r");
737 while (fgets(line
, sizeof(line
), f
) != NULL
)
738 import_property_from_string(dev
, line
);
743 static int import_program_into_properties(struct udev_device
*dev
, const char *program
)
745 struct udev
*udev
= udev_device_get_udev(dev
);
751 envp
= udev_device_get_properties_envp(dev
);
752 if (util_run_program(udev
, program
, envp
, result
, sizeof(result
), &reslen
, NULL
) != 0)
756 while (line
!= NULL
) {
759 pos
= strchr(line
, '\n');
764 import_property_from_string(dev
, line
);
770 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
772 struct udev
*udev
= udev_device_get_udev(dev
);
773 struct udev_device
*dev_parent
;
774 struct udev_list_entry
*list_entry
;
776 dev_parent
= udev_device_get_parent(dev
);
777 if (dev_parent
== NULL
)
780 dbg(udev
, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent
));
781 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
782 const char *key
= udev_list_entry_get_name(list_entry
);
783 const char *val
= udev_list_entry_get_value(list_entry
);
785 if (fnmatch(filter
, key
, 0) == 0) {
786 struct udev_list_entry
*entry
;
788 dbg(udev
, "import key '%s=%s'\n", key
, val
);
789 entry
= udev_device_add_property(dev
, key
, val
);
790 /* store in db, skip private keys */
792 udev_list_entry_set_flag(entry
, 1);
798 #define WAIT_LOOP_PER_SECOND 50
799 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
801 struct udev
*udev
= udev_device_get_udev(dev
);
802 char filepath
[UTIL_PATH_SIZE
];
803 char devicepath
[UTIL_PATH_SIZE
];
805 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
807 /* a relative path is a device attribute */
808 devicepath
[0] = '\0';
809 if (file
[0] != '/') {
810 util_strscpyl(devicepath
, sizeof(devicepath
),
811 udev_get_sys_path(udev
), udev_device_get_devpath(dev
), NULL
);
812 util_strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
816 dbg(udev
, "will wait %i sec for '%s'\n", timeout
, file
);
818 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
821 if (stat(file
, &stats
) == 0) {
822 info(udev
, "file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
825 /* make sure, the device did not disappear in the meantime */
826 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
827 info(udev
, "device disappeared while waiting for '%s'\n", file
);
830 info(udev
, "wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
831 nanosleep(&duration
, NULL
);
833 info(udev
, "waiting for '%s' failed\n", file
);
837 static int attr_subst_subdir(char *attr
, size_t len
)
841 if (strstr(attr
, "/*/")) {
843 char dirname
[UTIL_PATH_SIZE
];
847 util_strscpy(dirname
, sizeof(dirname
), attr
);
848 pos
= strstr(dirname
, "/*/");
853 dir
= opendir(dirname
);
857 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
860 if (dent
->d_name
[0] == '.')
862 util_strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
863 if (stat(attr
, &stats
) == 0) {
875 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
881 if (linepos
== NULL
|| linepos
[0] == '\0')
884 /* skip whitespace */
885 while (isspace(linepos
[0]) || linepos
[0] == ',')
889 if (linepos
[0] == '\0')
895 if (linepos
[0] == '\0')
897 if (isspace(linepos
[0]))
899 if (linepos
[0] == '=')
901 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
902 if (linepos
[1] == '=')
906 /* remember end of key */
909 /* skip whitespace after key */
910 while (isspace(linepos
[0]))
912 if (linepos
[0] == '\0')
915 /* get operation type */
916 if (linepos
[0] == '=' && linepos
[1] == '=') {
919 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
922 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
925 } else if (linepos
[0] == '=') {
928 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
929 *op
= OP_ASSIGN_FINAL
;
937 /* skip whitespace after operator */
938 while (isspace(linepos
[0]))
940 if (linepos
[0] == '\0')
944 if (linepos
[0] == '"')
951 temp
= strchr(linepos
, '"');
956 dbg(udev
, "%s '%s'-'%s'\n", operation_str(*op
), *key
, *value
);
958 /* move line to next key */
963 /* extract possible KEY{attr} */
964 static char *get_key_attribute(struct udev
*udev
, char *str
)
969 attr
= strchr(str
, '{');
972 pos
= strchr(attr
, '}');
974 err(udev
, "missing closing brace for format\n");
978 dbg(udev
, "attribute='%s'\n", attr
);
984 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
985 enum operation_type op
,
986 const char *value
, const void *data
)
988 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
989 const char *attr
= NULL
;
991 memset(token
, 0x00, sizeof(struct token
));
1003 case TK_M_SUBSYSTEMS
:
1006 case TK_M_IMPORT_FILE
:
1007 case TK_M_IMPORT_PROG
:
1008 case TK_M_IMPORT_PARENT
:
1016 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1024 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1025 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
1028 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1030 token
->key
.mode
= *(mode_t
*)data
;
1032 case TK_A_IGNORE_DEVICE
:
1033 case TK_A_STRING_ESCAPE_NONE
:
1034 case TK_A_STRING_ESCAPE_REPLACE
:
1035 case TK_A_IGNORE_REMOVE
:
1036 case TK_A_LAST_RULE
:
1039 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1040 token
->key
.ignore_error
= *(int *)data
;
1042 case TK_A_INOTIFY_WATCH
:
1043 case TK_A_NUM_FAKE_PART
:
1044 case TK_A_DEVLINK_PRIO
:
1045 token
->key
.devlink_prio
= *(int *)data
;
1048 token
->key
.uid
= *(uid_t
*)data
;
1051 token
->key
.gid
= *(gid_t
*)data
;
1054 token
->key
.mode
= *(mode_t
*)data
;
1056 case TK_A_EVENT_TIMEOUT
:
1057 token
->key
.event_timeout
= *(int *)data
;
1060 case TK_M_PARENTS_MIN
:
1061 case TK_M_PARENTS_MAX
:
1065 err(rule_tmp
->rules
->udev
, "wrong type %u\n", type
);
1069 if (value
!= NULL
&& type
< TK_M_MAX
) {
1070 /* check if we need to split or call fnmatch() while matching rules */
1071 enum string_glob_type glob
;
1075 has_split
= (strchr(value
, '|') != NULL
);
1076 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
|| strchr(value
, '[') != NULL
);
1077 if (has_split
&& has_glob
) {
1078 glob
= GL_SPLIT_GLOB
;
1079 } else if (has_split
) {
1081 } else if (has_glob
) {
1082 if (strcmp(value
, "?*") == 0)
1083 glob
= GL_SOMETHING
;
1089 token
->key
.glob
= glob
;
1092 if (value
!= NULL
&& type
> TK_M_MAX
) {
1093 /* check if assigned value has substitution chars */
1094 if (value
[0] == '[')
1095 token
->key
.subst
= SB_SUBSYS
;
1096 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
1097 token
->key
.subst
= SB_FORMAT
;
1099 token
->key
.subst
= SB_NONE
;
1103 /* check if property/attribut name has substitution chars */
1105 token
->key
.attrsubst
= SB_SUBSYS
;
1106 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
1107 token
->key
.attrsubst
= SB_FORMAT
;
1109 token
->key
.attrsubst
= SB_NONE
;
1112 token
->key
.type
= type
;
1114 rule_tmp
->token_cur
++;
1115 if (rule_tmp
->token_cur
>= ARRAY_SIZE(rule_tmp
->token
)) {
1116 err(rule_tmp
->rules
->udev
, "temporary rule array too small\n");
1122 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1125 unsigned int start
= 0;
1126 unsigned int end
= rule_tmp
->token_cur
;
1128 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1129 enum token_type next_val
= TK_UNSET
;
1130 unsigned int next_idx
= 0;
1133 /* find smallest value */
1134 for (j
= start
; j
< end
; j
++) {
1135 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1137 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1138 next_val
= rule_tmp
->token
[j
].type
;
1143 /* add token and mark done */
1144 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1146 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1149 if (next_idx
== start
)
1151 if (next_idx
+1 == end
)
1157 static int add_rule(struct udev_rules
*rules
, char *line
,
1158 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1162 struct rule_tmp rule_tmp
;
1164 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1165 rule_tmp
.rules
= rules
;
1166 rule_tmp
.rule
.type
= TK_RULE
;
1167 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1168 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1174 enum operation_type op
;
1176 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1179 if (strcmp(key
, "ACTION") == 0) {
1180 if (op
> OP_MATCH_MAX
) {
1181 err(rules
->udev
, "invalid ACTION operation\n");
1184 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1188 if (strcmp(key
, "DEVPATH") == 0) {
1189 if (op
> OP_MATCH_MAX
) {
1190 err(rules
->udev
, "invalid DEVPATH operation\n");
1193 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1197 if (strcmp(key
, "KERNEL") == 0) {
1198 if (op
> OP_MATCH_MAX
) {
1199 err(rules
->udev
, "invalid KERNEL operation\n");
1202 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1206 if (strcmp(key
, "SUBSYSTEM") == 0) {
1207 if (op
> OP_MATCH_MAX
) {
1208 err(rules
->udev
, "invalid SUBSYSTEM operation\n");
1211 /* bus, class, subsystem events should all be the same */
1212 if (strcmp(value
, "subsystem") == 0 ||
1213 strcmp(value
, "bus") == 0 ||
1214 strcmp(value
, "class") == 0) {
1215 if (strcmp(value
, "bus") == 0 || strcmp(value
, "class") == 0)
1216 err(rules
->udev
, "'%s' must be specified as 'subsystem' \n"
1217 "please fix it in %s:%u", value
, filename
, lineno
);
1218 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1220 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1224 if (strcmp(key
, "DRIVER") == 0) {
1225 if (op
> OP_MATCH_MAX
) {
1226 err(rules
->udev
, "invalid DRIVER operation\n");
1229 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1233 if (strncmp(key
, "ATTR{", sizeof("ATTR{")-1) == 0) {
1234 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1236 err(rules
->udev
, "error parsing ATTR attribute\n");
1239 if (op
< OP_MATCH_MAX
) {
1240 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1242 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1247 if (strcmp(key
, "KERNELS") == 0 ||
1248 strcmp(key
, "ID") == 0) {
1249 if (op
> OP_MATCH_MAX
) {
1250 err(rules
->udev
, "invalid KERNELS operation\n");
1253 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1257 if (strcmp(key
, "SUBSYSTEMS") == 0 ||
1258 strcmp(key
, "BUS") == 0) {
1259 if (op
> OP_MATCH_MAX
) {
1260 err(rules
->udev
, "invalid SUBSYSTEMS operation\n");
1263 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1267 if (strcmp(key
, "DRIVERS") == 0) {
1268 if (op
> OP_MATCH_MAX
) {
1269 err(rules
->udev
, "invalid DRIVERS operation\n");
1272 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1276 if (strncmp(key
, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1277 strncmp(key
, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1278 if (op
> OP_MATCH_MAX
) {
1279 err(rules
->udev
, "invalid ATTRS operation\n");
1282 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1284 err(rules
->udev
, "error parsing ATTRS attribute\n");
1287 if (strncmp(attr
, "device/", 7) == 0)
1288 err(rules
->udev
, "the 'device' link may not be available in a future kernel, "
1289 "please fix it in %s:%u", filename
, lineno
);
1290 else if (strstr(attr
, "../") != NULL
)
1291 err(rules
->udev
, "do not reference parent sysfs directories directly, "
1292 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1293 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1297 if (strncmp(key
, "ENV{", sizeof("ENV{")-1) == 0) {
1298 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1300 err(rules
->udev
, "error parsing ENV attribute\n");
1303 if (op
< OP_MATCH_MAX
) {
1304 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1307 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1313 if (strcmp(key
, "PROGRAM") == 0) {
1314 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1318 if (strcmp(key
, "RESULT") == 0) {
1319 if (op
> OP_MATCH_MAX
) {
1320 err(rules
->udev
, "invalid RESULT operation\n");
1323 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1327 if (strncmp(key
, "IMPORT", sizeof("IMPORT")-1) == 0) {
1328 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1329 if (attr
!= NULL
&& strstr(attr
, "program")) {
1330 dbg(rules
->udev
, "IMPORT will be executed\n");
1331 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1332 } else if (attr
!= NULL
&& strstr(attr
, "file")) {
1333 dbg(rules
->udev
, "IMPORT will be included as file\n");
1334 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1335 } else if (attr
!= NULL
&& strstr(attr
, "parent")) {
1336 dbg(rules
->udev
, "IMPORT will include the parent values\n");
1337 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1339 /* figure it out if it is executable */
1340 char file
[UTIL_PATH_SIZE
];
1342 struct stat statbuf
;
1344 /* allow programs in /lib/udev called without the path */
1345 if (value
[0] != '/')
1346 util_strscpyl(file
, sizeof(file
), LIBEXECDIR
"/", value
, NULL
);
1348 util_strscpy(file
, sizeof(file
), value
);
1349 pos
= strchr(file
, ' ');
1352 dbg(rules
->udev
, "IMPORT auto mode for '%s'\n", file
);
1353 if (stat(file
, &statbuf
) == 0 && (statbuf
.st_mode
& S_IXUSR
)) {
1354 dbg(rules
->udev
, "IMPORT will be executed (autotype)\n");
1355 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1357 dbg(rules
->udev
, "IMPORT will be included as file (autotype)\n");
1358 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1364 if (strncmp(key
, "TEST", sizeof("TEST")-1) == 0) {
1367 if (op
> OP_MATCH_MAX
) {
1368 err(rules
->udev
, "invalid TEST operation\n");
1371 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1373 mode
= strtol(attr
, NULL
, 8);
1374 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1376 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1381 if (strncmp(key
, "RUN", sizeof("RUN")-1) == 0) {
1384 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1385 if (attr
!= NULL
&& strstr(attr
, "fail_event_on_error"))
1387 rule_add_key(&rule_tmp
, TK_A_RUN
, op
, value
, &flag
);
1391 if (strcmp(key
, "WAIT_FOR") == 0 || strcmp(key
, "WAIT_FOR_SYSFS") == 0) {
1392 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1396 if (strcmp(key
, "LABEL") == 0) {
1397 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1401 if (strcmp(key
, "GOTO") == 0) {
1402 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1406 if (strncmp(key
, "NAME", sizeof("NAME")-1) == 0) {
1407 if (op
< OP_MATCH_MAX
) {
1408 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1410 if (value
[0] == '\0')
1411 info(rules
->udev
, "name empty, node creation suppressed\n");
1412 else if (strcmp(value
, "%k") == 0)
1413 err(rules
->udev
, "NAME=\"%%k\" is superfluous and breaks "
1414 "kernel supplied names, please remove it from %s:%u\n", filename
, lineno
);
1415 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1416 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("NAME")-1);
1418 if (strstr(attr
, "all_partitions") != NULL
) {
1419 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1421 dbg(rules
->udev
, "creation of partition nodes requested\n");
1422 rule_add_key(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1424 if (strstr(attr
, "ignore_remove") != NULL
) {
1425 dbg(rules
->udev
, "remove event should be ignored\n");
1426 rule_add_key(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1430 rule_tmp
.rule
.rule
.flags
= 1;
1434 if (strcmp(key
, "SYMLINK") == 0) {
1435 if (op
< OP_MATCH_MAX
)
1436 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1438 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1439 rule_tmp
.rule
.rule
.flags
= 1;
1443 if (strcmp(key
, "OWNER") == 0) {
1447 uid
= strtoul(value
, &endptr
, 10);
1448 if (endptr
[0] == '\0') {
1449 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1450 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1451 uid
= add_uid(rules
, value
);
1452 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1453 } else if (rules
->resolve_names
>= 0) {
1454 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1456 rule_tmp
.rule
.rule
.flags
= 1;
1460 if (strcmp(key
, "GROUP") == 0) {
1464 gid
= strtoul(value
, &endptr
, 10);
1465 if (endptr
[0] == '\0') {
1466 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1467 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1468 gid
= add_gid(rules
, value
);
1469 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1470 } else if (rules
->resolve_names
>= 0) {
1471 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1473 rule_tmp
.rule
.rule
.flags
= 1;
1477 if (strcmp(key
, "MODE") == 0) {
1481 mode
= strtol(value
, &endptr
, 8);
1482 if (endptr
[0] == '\0')
1483 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1485 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1486 rule_tmp
.rule
.rule
.flags
= 1;
1490 if (strcmp(key
, "OPTIONS") == 0) {
1493 if (strstr(value
, "last_rule") != NULL
) {
1494 dbg(rules
->udev
, "last rule to be applied\n");
1495 rule_add_key(&rule_tmp
, TK_A_LAST_RULE
, 0, NULL
, NULL
);
1497 if (strstr(value
, "ignore_device") != NULL
) {
1498 dbg(rules
->udev
, "device should be ignored\n");
1499 rule_add_key(&rule_tmp
, TK_A_IGNORE_DEVICE
, 0, NULL
, NULL
);
1501 if (strstr(value
, "ignore_remove") != NULL
) {
1502 dbg(rules
->udev
, "remove event should be ignored\n");
1503 rule_add_key(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1505 pos
= strstr(value
, "link_priority=");
1507 int prio
= atoi(&pos
[strlen("link_priority=")]);
1509 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, 0, NULL
, &prio
);
1510 dbg(rules
->udev
, "link priority=%i\n", prio
);
1512 pos
= strstr(value
, "event_timeout=");
1514 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1516 rule_add_key(&rule_tmp
, TK_A_EVENT_TIMEOUT
, 0, NULL
, &tout
);
1517 dbg(rules
->udev
, "event timeout=%i\n", tout
);
1519 pos
= strstr(value
, "string_escape=");
1521 pos
= &pos
[strlen("string_escape=")];
1522 if (strncmp(pos
, "none", strlen("none")) == 0)
1523 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, 0, NULL
, NULL
);
1524 else if (strncmp(pos
, "replace", strlen("replace")) == 0)
1525 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, 0, NULL
, NULL
);
1527 if (strstr(value
, "all_partitions") != NULL
) {
1528 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1530 rule_add_key(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1531 dbg(rules
->udev
, "creation of partition nodes requested\n");
1533 pos
= strstr(value
, "nowatch");
1537 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, 0, NULL
, &off
);
1538 dbg(rules
->udev
, "inotify watch of device disabled\n");
1540 pos
= strstr(value
, "watch");
1544 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, 0, NULL
, &on
);
1545 dbg(rules
->udev
, "inotify watch of device requested\n");
1550 err(rules
->udev
, "unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1553 /* add rule token */
1554 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1555 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1558 /* add tokens to list, sorted by type */
1559 if (sort_token(rules
, &rule_tmp
) != 0)
1564 err(rules
->udev
, "invalid rule '%s:%u'\n", filename
, lineno
);
1568 static int parse_file(struct udev_rules
*rules
, const char *filename
, unsigned short filename_off
)
1571 unsigned int first_token
;
1572 char line
[UTIL_LINE_SIZE
];
1576 info(rules
->udev
, "reading '%s' as rules file\n", filename
);
1578 f
= fopen(filename
, "r");
1582 first_token
= rules
->token_cur
;
1584 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1588 /* skip whitespace */
1591 while (isspace(key
[0]))
1602 /* continue reading if backslash+newline is found */
1603 while (line
[len
-2] == '\\') {
1604 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1606 if (strlen(&line
[len
-2]) < 2)
1612 if (len
+1 >= sizeof(line
)) {
1613 err(rules
->udev
, "line too long '%s':%u, ignored\n", filename
, line_nr
);
1616 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1620 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1621 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1622 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1623 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1626 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1627 if (rules
->tokens
[j
].type
!= TK_RULE
)
1629 if (rules
->tokens
[j
].rule
.label_off
== 0)
1631 if (strcmp(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]) != 0)
1633 rules
->tokens
[i
].key
.rule_goto
= j
;
1636 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1637 err(rules
->udev
, "GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1643 static int add_matching_files(struct udev
*udev
, struct udev_list_node
*file_list
, const char *dirname
, const char *suffix
)
1646 char filename
[UTIL_PATH_SIZE
];
1648 dbg(udev
, "open directory '%s'\n", dirname
);
1649 dir
= opendir(dirname
);
1651 info(udev
, "unable to open '%s': %m\n", dirname
);
1656 struct dirent
*dent
;
1658 dent
= readdir(dir
);
1659 if (dent
== NULL
|| dent
->d_name
[0] == '\0')
1662 if (dent
->d_name
[0] == '.')
1665 /* look for file matching with specified suffix */
1666 if (suffix
!= NULL
) {
1669 ext
= strrchr(dent
->d_name
, '.');
1672 if (strcmp(ext
, suffix
) != 0)
1675 util_strscpyl(filename
, sizeof(filename
), dirname
, "/", dent
->d_name
, NULL
);
1676 dbg(udev
, "put file '%s' into list\n", filename
);
1677 udev_list_entry_add(udev
, file_list
, filename
, NULL
, 1, 1);
1684 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1686 struct udev_rules
*rules
;
1687 struct stat statbuf
;
1688 struct udev_list_node file_list
;
1689 struct udev_list_entry
*file_loop
, *file_tmp
;
1690 struct token end_token
;
1692 rules
= malloc(sizeof(struct udev_rules
));
1695 memset(rules
, 0x00, sizeof(struct udev_rules
));
1697 rules
->resolve_names
= resolve_names
;
1698 udev_list_init(&file_list
);
1700 /* init token array and string buffer */
1701 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1702 if (rules
->tokens
== NULL
)
1704 rules
->token_max
= PREALLOC_TOKEN
;
1706 rules
->buf
= malloc(PREALLOC_STRBUF
);
1707 if (rules
->buf
== NULL
)
1709 rules
->buf_max
= PREALLOC_STRBUF
;
1710 /* offset 0 is always '\0' */
1711 rules
->buf
[0] = '\0';
1713 dbg(udev
, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1714 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1716 rules
->trie_nodes
= malloc(PREALLOC_TRIE
* sizeof(struct trie_node
));
1717 if (rules
->trie_nodes
== NULL
)
1719 rules
->trie_nodes_max
= PREALLOC_TRIE
;
1720 /* offset 0 is the trie root, with an empty string */
1721 memset(rules
->trie_nodes
, 0x00, sizeof(struct trie_node
));
1722 rules
->trie_nodes_cur
= 1;
1724 if (udev_get_rules_path(udev
) != NULL
) {
1725 /* custom rules location for testing */
1726 add_matching_files(udev
, &file_list
, udev_get_rules_path(udev
), ".rules");
1728 char filename
[PATH_MAX
];
1729 struct udev_list_node sort_list
;
1730 struct udev_list_entry
*sort_loop
, *sort_tmp
;
1732 /* read user/custom rules */
1733 add_matching_files(udev
, &file_list
, SYSCONFDIR
"/udev/rules.d", ".rules");
1735 /* read dynamic/temporary rules */
1736 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(udev
), "/.udev/rules.d", NULL
);
1737 if (stat(filename
, &statbuf
) != 0) {
1738 util_create_path(udev
, filename
);
1739 udev_selinux_setfscreatecon(udev
, filename
, S_IFDIR
|0755);
1740 mkdir(filename
, 0755);
1741 udev_selinux_resetfscreatecon(udev
);
1743 udev_list_init(&sort_list
);
1744 add_matching_files(udev
, &sort_list
, filename
, ".rules");
1746 /* read default rules */
1747 add_matching_files(udev
, &sort_list
, LIBEXECDIR
"/rules.d", ".rules");
1749 /* sort all rules files by basename into list of files */
1750 udev_list_entry_foreach_safe(sort_loop
, sort_tmp
, udev_list_get_entry(&sort_list
)) {
1751 const char *sort_name
= udev_list_entry_get_name(sort_loop
);
1752 const char *sort_base
= strrchr(sort_name
, '/');
1754 if (sort_base
== NULL
)
1756 /* sort entry into existing list */
1757 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1758 const char *file_name
= udev_list_entry_get_name(file_loop
);
1759 const char *file_base
= strrchr(file_name
, '/');
1761 if (file_base
== NULL
)
1763 if (strcmp(file_base
, sort_base
) == 0) {
1764 info(udev
, "rule file basename '%s' already added, ignoring '%s'\n",
1765 file_name
, sort_name
);
1766 udev_list_entry_delete(sort_loop
);
1770 if (strcmp(file_base
, sort_base
) > 0) {
1771 /* found later file, insert before */
1772 udev_list_entry_remove(sort_loop
);
1773 udev_list_entry_insert_before(sort_loop
, file_loop
);
1778 /* current file already handled */
1779 if (sort_loop
== NULL
)
1781 /* no later file, append to end of list */
1782 udev_list_entry_remove(sort_loop
);
1783 udev_list_entry_append(sort_loop
, &file_list
);
1787 /* add all filenames to the string buffer */
1788 udev_list_entry_foreach(file_loop
, udev_list_get_entry(&file_list
)) {
1789 const char *filename
= udev_list_entry_get_name(file_loop
);
1790 unsigned int filename_off
;
1792 filename_off
= add_string(rules
, filename
);
1793 /* the offset in the rule is limited to unsigned short */
1794 if (filename_off
< USHRT_MAX
)
1795 udev_list_entry_set_flag(file_loop
, filename_off
);
1798 /* parse list of files */
1799 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1800 const char *filename
= udev_list_entry_get_name(file_loop
);
1801 unsigned int filename_off
= udev_list_entry_get_flag(file_loop
);
1803 if (stat(filename
, &statbuf
) == 0 && statbuf
.st_size
> 0)
1804 parse_file(rules
, filename
, filename_off
);
1806 err(udev
, "can not read '%s'\n", filename
);
1807 udev_list_entry_delete(file_loop
);
1810 memset(&end_token
, 0x00, sizeof(struct token
));
1811 end_token
.type
= TK_END
;
1812 add_token(rules
, &end_token
);
1814 /* shrink allocated token and string buffer */
1815 if (rules
->token_cur
< rules
->token_max
) {
1816 struct token
*tokens
;
1818 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1819 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1820 rules
->tokens
= tokens
;
1821 rules
->token_max
= rules
->token_cur
;
1824 if (rules
->buf_cur
< rules
->buf_max
) {
1827 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1828 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1830 rules
->buf_max
= rules
->buf_cur
;
1833 info(udev
, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1834 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1835 info(udev
, "temporary index used %zu bytes (%u * %zu bytes)\n",
1836 rules
->trie_nodes_cur
* sizeof(struct trie_node
),
1837 rules
->trie_nodes_cur
, sizeof(struct trie_node
));
1840 free(rules
->trie_nodes
);
1841 rules
->trie_nodes
= NULL
;
1842 rules
->trie_nodes_cur
= 0;
1843 rules
->trie_nodes_max
= 0;
1845 /* cleanup uid/gid cache */
1848 rules
->uids_cur
= 0;
1849 rules
->uids_max
= 0;
1852 rules
->gids_cur
= 0;
1853 rules
->gids_max
= 0;
1859 void udev_rules_unref(struct udev_rules
*rules
)
1863 free(rules
->tokens
);
1865 free(rules
->trie_nodes
);
1871 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1873 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1880 switch (token
->key
.glob
) {
1882 match
= (strcmp(key_value
, val
) == 0);
1885 match
= (fnmatch(key_value
, val
, 0) == 0);
1892 split
= &rules
->buf
[token
->key
.value_off
];
1897 next
= strchr(split
, '|');
1899 size_t matchlen
= (size_t)(next
- split
);
1901 match
= (matchlen
== len
&& strncmp(split
, val
, matchlen
) == 0);
1905 match
= (strcmp(split
, val
) == 0);
1914 char value
[UTIL_PATH_SIZE
];
1916 util_strscpy(value
, sizeof(value
), &rules
->buf
[token
->key
.value_off
]);
1918 while (key_value
!= NULL
) {
1919 pos
= strchr(key_value
, '|');
1924 dbg(rules
->udev
, "match %s '%s' <-> '%s'\n", token_str(token
->type
), key_value
, val
);
1925 match
= (fnmatch(key_value
, val
, 0) == 0);
1933 match
= (val
[0] != '\0');
1939 if (match
&& (token
->key
.op
== OP_MATCH
)) {
1940 dbg(rules
->udev
, "%s is true (matching value)\n", token_str(token
->type
));
1943 if (!match
&& (token
->key
.op
== OP_NOMATCH
)) {
1944 dbg(rules
->udev
, "%s is true (non-matching value)\n", token_str(token
->type
));
1947 dbg(rules
->udev
, "%s is not true\n", token_str(token
->type
));
1951 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1954 char nbuf
[UTIL_NAME_SIZE
];
1956 char vbuf
[UTIL_NAME_SIZE
];
1959 name
= &rules
->buf
[cur
->key
.attr_off
];
1960 switch (cur
->key
.attrsubst
) {
1962 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1966 value
= udev_device_get_sysattr_value(dev
, name
);
1971 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1979 /* remove trailing whitespace, if not asked to match for it */
1980 len
= strlen(value
);
1981 if (len
> 0 && isspace(value
[len
-1])) {
1982 const char *key_value
;
1985 key_value
= &rules
->buf
[cur
->key
.value_off
];
1986 klen
= strlen(key_value
);
1987 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1988 if (value
!= vbuf
) {
1989 util_strscpy(vbuf
, sizeof(vbuf
), value
);
1992 while (len
> 0 && isspace(vbuf
[--len
]))
1994 dbg(rules
->udev
, "removed trailing whitespace from '%s'\n", value
);
1998 return match_key(rules
, cur
, value
);
2007 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
)
2011 enum escape_type esc
= ESCAPE_UNSET
;
2014 if (rules
->tokens
== NULL
)
2017 can_set_name
= ((strcmp(udev_device_get_action(event
->dev
), "add") == 0 ||
2018 strcmp(udev_device_get_action(event
->dev
), "change") == 0) &&
2019 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
2020 strcmp(udev_device_get_subsystem(event
->dev
), "net") == 0));
2022 /* loop through token list, match, run actions or forward to next rule */
2023 cur
= &rules
->tokens
[0];
2026 dump_token(rules
, cur
);
2027 switch (cur
->type
) {
2031 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2032 if (!can_set_name
&& rule
->rule
.flags
)
2037 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
2041 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
2045 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
2050 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
2051 struct udev_list_entry
*list_entry
;
2054 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
2055 const char *devlink
;
2057 devlink
= &udev_list_entry_get_name(list_entry
)[devlen
];
2058 if (match_key(rules
, cur
, devlink
) == 0) {
2068 if (match_key(rules
, cur
, event
->name
) != 0)
2073 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2076 value
= udev_device_get_property_value(event
->dev
, key_name
);
2077 if (value
== NULL
) {
2078 dbg(event
->udev
, "ENV{%s} is not set, treat as empty\n", key_name
);
2081 if (match_key(rules
, cur
, value
))
2085 case TK_M_SUBSYSTEM
:
2086 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
2090 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2095 char filename
[UTIL_PATH_SIZE
];
2098 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2099 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2100 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2105 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2109 case TK_M_SUBSYSTEMS
:
2115 /* get whole sequence of parent matches */
2117 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2120 /* loop over parents */
2121 event
->dev_parent
= event
->dev
;
2125 dbg(event
->udev
, "parent: '%s'\n", udev_device_get_syspath(event
->dev_parent
));
2126 /* loop over sequence of parent match keys */
2127 for (key
= cur
; key
< next
; key
++ ) {
2128 dump_token(rules
, key
);
2131 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2134 case TK_M_SUBSYSTEMS
:
2135 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2139 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2143 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2149 dbg(event
->udev
, "parent key matched\n");
2151 dbg(event
->udev
, "all parent keys matched\n");
2152 /* all keys matched */
2156 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2157 if (event
->dev_parent
== NULL
)
2160 /* move behind our sequence of parent match keys */
2166 char filename
[UTIL_PATH_SIZE
];
2167 struct stat statbuf
;
2170 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2171 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2172 if (filename
[0] != '/') {
2173 char tmp
[UTIL_PATH_SIZE
];
2175 util_strscpy(tmp
, sizeof(tmp
), filename
);
2176 util_strscpyl(filename
, sizeof(filename
),
2177 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2180 attr_subst_subdir(filename
, sizeof(filename
));
2182 match
= (stat(filename
, &statbuf
) == 0);
2183 dbg(event
->udev
, "'%s' %s", filename
, match
? "exists\n" : "does not exist\n");
2184 if (match
&& cur
->key
.mode
> 0) {
2185 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2186 dbg(event
->udev
, "'%s' has mode=%#o and %s %#o\n", filename
, statbuf
.st_mode
,
2187 match
? "matches" : "does not match", cur
->key
.mode
);
2189 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2191 if (!match
&& cur
->key
.op
== OP_MATCH
)
2197 char program
[UTIL_PATH_SIZE
];
2199 char result
[UTIL_PATH_SIZE
];
2201 free(event
->program_result
);
2202 event
->program_result
= NULL
;
2203 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], program
, sizeof(program
));
2204 envp
= udev_device_get_properties_envp(event
->dev
);
2205 info(event
->udev
, "PROGRAM '%s' %s:%u\n",
2207 &rules
->buf
[rule
->rule
.filename_off
],
2208 rule
->rule
.filename_line
);
2209 if (util_run_program(event
->udev
, program
, envp
, result
, sizeof(result
), NULL
, NULL
) != 0) {
2210 if (cur
->key
.op
!= OP_NOMATCH
)
2215 util_remove_trailing_chars(result
, '\n');
2216 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2217 count
= udev_util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2219 info(event
->udev
, "%i character(s) replaced\n" , count
);
2221 event
->program_result
= strdup(result
);
2222 dbg(event
->udev
, "storing result '%s'\n", event
->program_result
);
2223 if (cur
->key
.op
== OP_NOMATCH
)
2228 case TK_M_IMPORT_FILE
:
2230 char import
[UTIL_PATH_SIZE
];
2232 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2233 if (import_file_into_properties(event
->dev
, import
) != 0)
2234 if (cur
->key
.op
!= OP_NOMATCH
)
2238 case TK_M_IMPORT_PROG
:
2240 char import
[UTIL_PATH_SIZE
];
2242 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2243 info(event
->udev
, "IMPORT '%s' %s:%u\n",
2245 &rules
->buf
[rule
->rule
.filename_off
],
2246 rule
->rule
.filename_line
);
2247 if (import_program_into_properties(event
->dev
, import
) != 0)
2248 if (cur
->key
.op
!= OP_NOMATCH
)
2252 case TK_M_IMPORT_PARENT
:
2254 char import
[UTIL_PATH_SIZE
];
2256 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2257 if (import_parent_into_properties(event
->dev
, import
) != 0)
2258 if (cur
->key
.op
!= OP_NOMATCH
)
2263 if (match_key(rules
, cur
, event
->program_result
) != 0)
2267 case TK_A_IGNORE_DEVICE
:
2268 event
->ignore_device
= 1;
2271 case TK_A_STRING_ESCAPE_NONE
:
2274 case TK_A_STRING_ESCAPE_REPLACE
:
2275 esc
= ESCAPE_REPLACE
;
2277 case TK_A_NUM_FAKE_PART
:
2278 if (strcmp(udev_device_get_subsystem(event
->dev
), "block") != 0)
2280 if (udev_device_get_sysnum(event
->dev
) != NULL
)
2282 udev_device_set_num_fake_partitions(event
->dev
, cur
->key
.num_fake_part
);
2284 case TK_A_INOTIFY_WATCH
:
2285 event
->inotify_watch
= cur
->key
.watch
;
2287 case TK_A_DEVLINK_PRIO
:
2288 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2292 char owner
[UTIL_NAME_SIZE
];
2294 if (event
->owner_final
)
2296 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2297 event
->owner_final
= 1;
2298 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], owner
, sizeof(owner
));
2299 event
->uid
= util_lookup_user(event
->udev
, owner
);
2300 info(event
->udev
, "OWNER %u %s:%u\n",
2302 &rules
->buf
[rule
->rule
.filename_off
],
2303 rule
->rule
.filename_line
);
2308 char group
[UTIL_NAME_SIZE
];
2310 if (event
->group_final
)
2312 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2313 event
->group_final
= 1;
2314 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], group
, sizeof(group
));
2315 event
->gid
= util_lookup_group(event
->udev
, group
);
2316 info(event
->udev
, "GROUP %u %s:%u\n",
2318 &rules
->buf
[rule
->rule
.filename_off
],
2319 rule
->rule
.filename_line
);
2324 char mode
[UTIL_NAME_SIZE
];
2327 if (event
->mode_final
)
2329 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2330 event
->mode_final
= 1;
2331 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], mode
, sizeof(mode
));
2332 event
->mode
= strtol(mode
, &endptr
, 8);
2333 if (endptr
[0] != '\0') {
2334 err(event
->udev
, "invalide mode '%s' set default mode 0660\n", mode
);
2337 info(event
->udev
, "MODE %#o %s:%u\n",
2339 &rules
->buf
[rule
->rule
.filename_off
],
2340 rule
->rule
.filename_line
);
2344 if (event
->owner_final
)
2346 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2347 event
->owner_final
= 1;
2348 event
->uid
= cur
->key
.uid
;
2349 info(event
->udev
, "OWNER %u %s:%u\n",
2351 &rules
->buf
[rule
->rule
.filename_off
],
2352 rule
->rule
.filename_line
);
2355 if (event
->group_final
)
2357 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2358 event
->group_final
= 1;
2359 event
->gid
= cur
->key
.gid
;
2360 info(event
->udev
, "GROUP %u %s:%u\n",
2362 &rules
->buf
[rule
->rule
.filename_off
],
2363 rule
->rule
.filename_line
);
2366 if (event
->mode_final
)
2368 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2369 event
->mode_final
= 1;
2370 event
->mode
= cur
->key
.mode
;
2371 info(event
->udev
, "MODE %#o %s:%u\n",
2373 &rules
->buf
[rule
->rule
.filename_off
],
2374 rule
->rule
.filename_line
);
2378 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2379 char *value
= &rules
->buf
[cur
->key
.value_off
];
2381 if (value
[0] != '\0') {
2382 char temp_value
[UTIL_NAME_SIZE
];
2383 struct udev_list_entry
*entry
;
2385 udev_event_apply_format(event
, value
, temp_value
, sizeof(temp_value
));
2386 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2387 /* store in db, skip private keys */
2389 udev_list_entry_set_flag(entry
, 1);
2391 udev_device_add_property(event
->dev
, name
, NULL
);
2397 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2398 char name_str
[UTIL_PATH_SIZE
];
2401 if (event
->name_final
)
2403 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2404 event
->name_final
= 1;
2405 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2406 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2407 count
= udev_util_replace_chars(name_str
, "/");
2409 info(event
->udev
, "%i character(s) replaced\n", count
);
2412 event
->name
= strdup(name_str
);
2413 info(event
->udev
, "NAME '%s' %s:%u\n",
2415 &rules
->buf
[rule
->rule
.filename_off
],
2416 rule
->rule
.filename_line
);
2421 char temp
[UTIL_PATH_SIZE
];
2422 char filename
[UTIL_PATH_SIZE
];
2426 if (event
->devlink_final
)
2428 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2430 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2431 event
->devlink_final
= 1;
2432 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2433 udev_device_cleanup_devlinks_list(event
->dev
);
2435 /* allow multiple symlinks separated by spaces */
2436 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], temp
, sizeof(temp
));
2437 if (esc
== ESCAPE_UNSET
)
2438 count
= udev_util_replace_chars(temp
, "/ ");
2439 else if (esc
== ESCAPE_REPLACE
)
2440 count
= udev_util_replace_chars(temp
, "/");
2442 info(event
->udev
, "%i character(s) replaced\n" , count
);
2443 dbg(event
->udev
, "rule applied, added symlink(s) '%s'\n", temp
);
2445 while (isspace(pos
[0]))
2447 next
= strchr(pos
, ' ');
2450 info(event
->udev
, "LINK '%s' %s:%u\n",
2452 &rules
->buf
[rule
->rule
.filename_off
],
2453 rule
->rule
.filename_line
);
2454 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2455 udev_device_add_devlink(event
->dev
, filename
);
2456 while (isspace(next
[1]))
2459 next
= strchr(pos
, ' ');
2461 if (pos
[0] != '\0') {
2462 info(event
->udev
, "LINK '%s' %s:%u\n",
2464 &rules
->buf
[rule
->rule
.filename_off
],
2465 rule
->rule
.filename_line
);
2466 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2467 udev_device_add_devlink(event
->dev
, filename
);
2471 case TK_A_EVENT_TIMEOUT
:
2472 udev_device_set_event_timeout(event
->dev
, cur
->key
.event_timeout
);
2474 case TK_A_IGNORE_REMOVE
:
2475 udev_device_set_ignore_remove(event
->dev
, 1);
2479 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2480 char attr
[UTIL_PATH_SIZE
];
2481 char value
[UTIL_NAME_SIZE
];
2484 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2485 util_strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2486 attr_subst_subdir(attr
, sizeof(attr
));
2488 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], value
, sizeof(value
));
2489 info(event
->udev
, "ATTR '%s' writing '%s' %s:%u\n", attr
, value
,
2490 &rules
->buf
[rule
->rule
.filename_off
],
2491 rule
->rule
.filename_line
);
2492 f
= fopen(attr
, "w");
2494 if (fprintf(f
, "%s", value
) <= 0)
2495 err(event
->udev
, "error writing ATTR{%s}: %m\n", attr
);
2498 err(event
->udev
, "error opening ATTR{%s} for writing: %m\n", attr
);
2504 struct udev_list_entry
*list_entry
;
2506 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2507 udev_list_cleanup_entries(event
->udev
, &event
->run_list
);
2508 info(event
->udev
, "RUN '%s' %s:%u\n",
2509 &rules
->buf
[cur
->key
.value_off
],
2510 &rules
->buf
[rule
->rule
.filename_off
],
2511 rule
->rule
.filename_line
);
2512 list_entry
= udev_list_entry_add(event
->udev
, &event
->run_list
,
2513 &rules
->buf
[cur
->key
.value_off
], NULL
, 1, 0);
2514 if (cur
->key
.ignore_error
)
2515 udev_list_entry_set_flag(list_entry
, 1);
2519 if (cur
->key
.rule_goto
== 0)
2521 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2523 case TK_A_LAST_RULE
:
2527 case TK_M_PARENTS_MIN
:
2528 case TK_M_PARENTS_MAX
:
2531 err(rules
->udev
, "wrong type %u\n", cur
->type
);
2538 /* fast-forward to next rule */
2539 cur
= rule
+ rule
->rule
.token_count
;
2540 dbg(rules
->udev
, "forward to rule: %u\n",
2541 (unsigned int) (cur
- rules
->tokens
));