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/>.
33 #define PREALLOC_TOKEN 2048
34 #define PREALLOC_STRBUF 32 * 1024
35 #define PREALLOC_TRIE 256
38 unsigned int name_off
;
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
;
61 /* every key in the rules file becomes a token */
63 unsigned int token_cur
;
64 unsigned int token_max
;
66 /* all key strings are copied to a single string buffer */
70 unsigned int buf_count
;
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
;
77 /* during rule parsing, uid/gid lookup results are cached */
79 unsigned int uids_cur
;
80 unsigned int uids_max
;
82 unsigned int gids_cur
;
83 unsigned int gids_max
;
86 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
99 enum string_glob_type
{
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 "?*" */
108 enum string_subst_type
{
115 /* tokens of a rule are sorted/handled in this order */
120 TK_M_ACTION
, /* val */
121 TK_M_DEVPATH
, /* val */
122 TK_M_KERNEL
, /* val */
123 TK_M_DEVLINK
, /* val */
125 TK_M_ENV
, /* val, attr */
126 TK_M_SUBSYSTEM
, /* val */
127 TK_M_DRIVER
, /* val */
128 TK_M_WAITFOR
, /* val */
129 TK_M_ATTR
, /* val, attr */
132 TK_M_KERNELS
, /* val */
133 TK_M_SUBSYSTEMS
, /* val */
134 TK_M_DRIVERS
, /* val */
135 TK_M_ATTRS
, /* val, attr */
138 TK_M_TEST
, /* val, mode_t */
139 TK_M_PROGRAM
, /* val */
140 TK_M_IMPORT_FILE
, /* val */
141 TK_M_IMPORT_PROG
, /* val */
142 TK_M_IMPORT_PARENT
, /* val */
143 TK_M_RESULT
, /* val */
147 TK_A_STRING_ESCAPE_NONE
,
148 TK_A_STRING_ESCAPE_REPLACE
,
149 TK_A_INOTIFY_WATCH
, /* int */
150 TK_A_NUM_FAKE_PART
, /* int */
151 TK_A_DEVLINK_PRIO
, /* int */
152 TK_A_OWNER
, /* val */
153 TK_A_GROUP
, /* val */
155 TK_A_OWNER_ID
, /* uid_t */
156 TK_A_GROUP_ID
, /* gid_t */
157 TK_A_MODE_ID
, /* mode_t */
158 TK_A_ENV
, /* val, attr */
160 TK_A_DEVLINK
, /* val */
161 TK_A_EVENT_TIMEOUT
, /* int */
163 TK_A_ATTR
, /* val, attr */
164 TK_A_RUN
, /* val, bool */
165 TK_A_GOTO
, /* size_t */
171 /* we try to pack stuff in a way that we take only 12 bytes per token */
174 unsigned char type
; /* same as in rule and key */
176 enum token_type type
:8;
177 unsigned int flags
:8;
178 unsigned short token_count
;
179 unsigned int label_off
;
180 unsigned short filename_off
;
181 unsigned short filename_line
;
184 enum token_type type
:8;
185 enum operation_type op
:8;
186 enum string_glob_type glob
:8;
187 enum string_subst_type subst
:4;
188 enum string_subst_type attrsubst
:4;
189 unsigned int value_off
;
191 unsigned int attr_off
;
193 unsigned int rule_goto
;
208 struct udev_rules
*rules
;
210 struct token token
[MAX_TK
];
211 unsigned int token_cur
;
215 static const char *operation_str(enum operation_type type
)
217 static const char *operation_strs
[] = {
218 [OP_UNSET
] = "UNSET",
219 [OP_MATCH
] = "match",
220 [OP_NOMATCH
] = "nomatch",
221 [OP_MATCH_MAX
] = "MATCH_MAX",
224 [OP_ASSIGN
] = "assign",
225 [OP_ASSIGN_FINAL
] = "assign-final",
228 return operation_strs
[type
];
231 static const char *string_glob_str(enum string_glob_type type
)
233 static const char *string_glob_strs
[] = {
234 [GL_UNSET
] = "UNSET",
235 [GL_PLAIN
] = "plain",
237 [GL_SPLIT
] = "split",
238 [GL_SPLIT_GLOB
] = "split-glob",
239 [GL_SOMETHING
] = "split-glob",
242 return string_glob_strs
[type
];
245 static const char *token_str(enum token_type type
)
247 static const char *token_strs
[] = {
248 [TK_UNSET
] = "UNSET",
251 [TK_M_ACTION
] = "M ACTION",
252 [TK_M_DEVPATH
] = "M DEVPATH",
253 [TK_M_KERNEL
] = "M KERNEL",
254 [TK_M_DEVLINK
] = "M DEVLINK",
255 [TK_M_NAME
] = "M NAME",
256 [TK_M_ENV
] = "M ENV",
257 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
258 [TK_M_DRIVER
] = "M DRIVER",
259 [TK_M_WAITFOR
] = "M WAITFOR",
260 [TK_M_ATTR
] = "M ATTR",
262 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
263 [TK_M_KERNELS
] = "M KERNELS",
264 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
265 [TK_M_DRIVERS
] = "M DRIVERS",
266 [TK_M_ATTRS
] = "M ATTRS",
267 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
269 [TK_M_TEST
] = "M TEST",
270 [TK_M_PROGRAM
] = "M PROGRAM",
271 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
272 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
273 [TK_M_IMPORT_PARENT
] = "M MPORT_PARENT",
274 [TK_M_RESULT
] = "M RESULT",
275 [TK_M_MAX
] = "M MAX",
277 [TK_A_IGNORE_DEVICE
] = "A IGNORE_DEVICE",
278 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
279 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
280 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
281 [TK_A_NUM_FAKE_PART
] = "A NUM_FAKE_PART",
282 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
283 [TK_A_OWNER
] = "A OWNER",
284 [TK_A_GROUP
] = "A GROUP",
285 [TK_A_MODE
] = "A MODE",
286 [TK_A_OWNER_ID
] = "A OWNER_ID",
287 [TK_A_GROUP_ID
] = "A GROUP_ID",
288 [TK_A_MODE_ID
] = "A MODE_ID",
289 [TK_A_ENV
] = "A ENV",
290 [TK_A_NAME
] = "A NAME",
291 [TK_A_DEVLINK
] = "A DEVLINK",
292 [TK_A_EVENT_TIMEOUT
] = "A EVENT_TIMEOUT",
293 [TK_A_IGNORE_REMOVE
] = "A IGNORE_REMOVE",
294 [TK_A_ATTR
] = "A ATTR",
295 [TK_A_RUN
] = "A RUN",
296 [TK_A_GOTO
] = "A GOTO",
297 [TK_A_LAST_RULE
] = "A LAST_RULE",
302 return token_strs
[type
];
305 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
307 enum token_type type
= token
->type
;
308 enum operation_type op
= token
->key
.op
;
309 enum string_glob_type glob
= token
->key
.glob
;
310 const char *value
= &rules
->buf
[token
->key
.value_off
];
311 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
316 const char *tks_ptr
= (char *)rules
->tokens
;
317 const char *tk_ptr
= (char *)token
;
318 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
320 dbg(rules
->udev
, "* RULE %s:%u, token: %u, count: %u, label: '%s', flags: 0x%02x\n",
321 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
322 idx
, token
->rule
.token_count
,
323 &rules
->buf
[token
->rule
.label_off
],
336 case TK_M_SUBSYSTEMS
:
339 case TK_M_IMPORT_FILE
:
340 case TK_M_IMPORT_PROG
:
341 case TK_M_IMPORT_PARENT
:
349 dbg(rules
->udev
, "%s %s '%s'(%s)\n",
350 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
357 dbg(rules
->udev
, "%s %s '%s' '%s'(%s)\n",
358 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
360 case TK_A_IGNORE_DEVICE
:
361 case TK_A_STRING_ESCAPE_NONE
:
362 case TK_A_STRING_ESCAPE_REPLACE
:
364 case TK_A_IGNORE_REMOVE
:
365 dbg(rules
->udev
, "%s\n", token_str(type
));
368 dbg(rules
->udev
, "%s %s '%s'(%s) %#o\n",
369 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
371 case TK_A_INOTIFY_WATCH
:
372 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.watch
);
374 case TK_A_NUM_FAKE_PART
:
375 dbg(rules
->udev
, "%s %u\n", token_str(type
), token
->key
.num_fake_part
);
377 case TK_A_DEVLINK_PRIO
:
378 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.devlink_prio
);
381 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.uid
);
384 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.gid
);
387 dbg(rules
->udev
, "%s %s %#o\n", token_str(type
), operation_str(op
), token
->key
.mode
);
389 case TK_A_EVENT_TIMEOUT
:
390 dbg(rules
->udev
, "%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.event_timeout
);
393 dbg(rules
->udev
, "%s '%s' %u\n", token_str(type
), value
, token
->key
.rule_goto
);
396 dbg(rules
->udev
, "* %s\n", token_str(type
));
398 case TK_M_PARENTS_MIN
:
399 case TK_M_PARENTS_MAX
:
402 dbg(rules
->udev
, "unknown type %u\n", type
);
407 static void dump_rules(struct udev_rules
*rules
)
411 dbg(rules
->udev
, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
413 rules
->token_cur
* sizeof(struct token
),
416 for(i
= 0; i
< rules
->token_cur
; i
++)
417 dump_token(rules
, &rules
->tokens
[i
]);
420 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
421 static inline const char *token_str(enum token_type type
) { return NULL
; }
422 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
423 static inline void dump_rules(struct udev_rules
*rules
) {}
424 #endif /* ENABLE_DEBUG */
426 static int add_new_string(struct udev_rules
*rules
, const char *str
, size_t bytes
)
430 /* grow buffer if needed */
431 if (rules
->buf_cur
+ bytes
+1 >= rules
->buf_max
) {
435 /* double the buffer size */
436 add
= rules
->buf_max
;
440 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
443 dbg(rules
->udev
, "extend buffer from %zu to %zu\n", rules
->buf_max
, rules
->buf_max
+ add
);
445 rules
->buf_max
+= add
;
447 off
= rules
->buf_cur
;
448 memcpy(&rules
->buf
[rules
->buf_cur
], str
, bytes
);
449 rules
->buf_cur
+= bytes
;
454 static int add_string(struct udev_rules
*rules
, const char *str
)
456 unsigned int node_idx
;
457 struct trie_node
*new_node
;
458 unsigned int new_node_idx
;
463 struct trie_node
*parent
;
465 /* walk trie, start from last character of str to find matching tails */
469 for (depth
= 0; depth
<= len
; depth
++) {
470 struct trie_node
*node
;
471 unsigned int child_idx
;
473 node
= &rules
->trie_nodes
[node_idx
];
474 off
= node
->value_off
+ node
->value_len
- len
;
476 /* match against current node */
477 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(&rules
->buf
[off
], str
, len
) == 0))
480 /* lookup child node */
481 key
= str
[len
- 1 - depth
];
482 child_idx
= node
->child_idx
;
483 while (child_idx
> 0) {
484 struct trie_node
*child
;
486 child
= &rules
->trie_nodes
[child_idx
];
487 if (child
->key
== key
)
489 child_idx
= child
->next_child_idx
;
493 node_idx
= child_idx
;
496 /* string not found, add it */
497 off
= add_new_string(rules
, str
, len
+ 1);
499 /* grow trie nodes if needed */
500 if (rules
->trie_nodes_cur
>= rules
->trie_nodes_max
) {
501 struct trie_node
*nodes
;
504 /* double the buffer size */
505 add
= rules
->trie_nodes_max
;
509 nodes
= realloc(rules
->trie_nodes
, (rules
->trie_nodes_max
+ add
) * sizeof(struct trie_node
));
512 dbg(rules
->udev
, "extend trie nodes from %u to %u\n",
513 rules
->trie_nodes_max
, rules
->trie_nodes_max
+ add
);
514 rules
->trie_nodes
= nodes
;
515 rules
->trie_nodes_max
+= add
;
519 new_node_idx
= rules
->trie_nodes_cur
;
520 rules
->trie_nodes_cur
++;
521 new_node
= &rules
->trie_nodes
[new_node_idx
];
522 memset(new_node
, 0x00, sizeof(struct trie_node
));
523 new_node
->value_off
= off
;
524 new_node
->value_len
= len
;
527 /* join the parent's child list */
528 parent
= &rules
->trie_nodes
[node_idx
];
529 if (parent
->child_idx
== 0) {
530 parent
->child_idx
= new_node_idx
;
532 struct trie_node
*last_child
;
534 last_child
= &rules
->trie_nodes
[parent
->last_child_idx
];
535 last_child
->next_child_idx
= new_node_idx
;
537 parent
->last_child_idx
= new_node_idx
;
541 static int add_token(struct udev_rules
*rules
, struct token
*token
)
543 /* grow buffer if needed */
544 if (rules
->token_cur
+1 >= rules
->token_max
) {
545 struct token
*tokens
;
548 /* double the buffer size */
549 add
= rules
->token_max
;
553 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
556 dbg(rules
->udev
, "extend tokens from %u to %u\n", rules
->token_max
, rules
->token_max
+ add
);
557 rules
->tokens
= tokens
;
558 rules
->token_max
+= add
;
560 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
565 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
571 /* lookup, if we know it already */
572 for (i
= 0; i
< rules
->uids_cur
; i
++) {
573 off
= rules
->uids
[i
].name_off
;
574 if (strcmp(&rules
->buf
[off
], owner
) == 0) {
575 uid
= rules
->uids
[i
].uid
;
576 dbg(rules
->udev
, "return existing %u for '%s'\n", uid
, owner
);
580 uid
= util_lookup_user(rules
->udev
, owner
);
582 /* grow buffer if needed */
583 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
584 struct uid_gid
*uids
;
587 /* double the buffer size */
588 add
= rules
->uids_max
;
592 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
595 dbg(rules
->udev
, "extend uids from %u to %u\n", rules
->uids_max
, rules
->uids_max
+ add
);
597 rules
->uids_max
+= add
;
599 rules
->uids
[rules
->uids_cur
].uid
= uid
;
600 off
= add_string(rules
, owner
);
603 rules
->uids
[rules
->uids_cur
].name_off
= off
;
608 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
614 /* lookup, if we know it already */
615 for (i
= 0; i
< rules
->gids_cur
; i
++) {
616 off
= rules
->gids
[i
].name_off
;
617 if (strcmp(&rules
->buf
[off
], group
) == 0) {
618 gid
= rules
->gids
[i
].gid
;
619 dbg(rules
->udev
, "return existing %u for '%s'\n", gid
, group
);
623 gid
= util_lookup_group(rules
->udev
, group
);
625 /* grow buffer if needed */
626 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
627 struct uid_gid
*gids
;
630 /* double the buffer size */
631 add
= rules
->gids_max
;
635 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
638 dbg(rules
->udev
, "extend gids from %u to %u\n", rules
->gids_max
, rules
->gids_max
+ add
);
640 rules
->gids_max
+= add
;
642 rules
->gids
[rules
->gids_cur
].gid
= gid
;
643 off
= add_string(rules
, group
);
646 rules
->gids
[rules
->gids_cur
].name_off
= off
;
651 static int import_property_from_string(struct udev_device
*dev
, char *line
)
653 struct udev
*udev
= udev_device_get_udev(dev
);
660 while (isspace(key
[0]))
663 /* comment or empty line */
664 if (key
[0] == '#' || key
[0] == '\0')
667 /* split key/value */
668 val
= strchr(key
, '=');
675 while (isspace(val
[0]))
682 while (isspace(key
[len
-1]))
686 /* terminate value */
690 while (isspace(val
[len
-1]))
698 if (val
[0] == '"' || val
[0] == '\'') {
699 if (val
[len
-1] != val
[0]) {
700 info(udev
, "inconsistent quoting: '%s', skip\n", line
);
707 dbg(udev
, "adding '%s'='%s'\n", key
, val
);
709 /* handle device, renamed by external tool, returning new path */
710 if (strcmp(key
, "DEVPATH") == 0) {
711 char syspath
[UTIL_PATH_SIZE
];
713 info(udev
, "updating devpath from '%s' to '%s'\n",
714 udev_device_get_devpath(dev
), val
);
715 util_strscpyl(syspath
, sizeof(syspath
), udev_get_sys_path(udev
), val
, NULL
);
716 udev_device_set_syspath(dev
, syspath
);
718 struct udev_list_entry
*entry
;
720 entry
= udev_device_add_property(dev
, key
, val
);
721 /* store in db, skip private keys */
723 udev_list_entry_set_flag(entry
, 1);
728 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
731 char line
[UTIL_LINE_SIZE
];
733 f
= fopen(filename
, "r");
736 while (fgets(line
, sizeof(line
), f
) != NULL
)
737 import_property_from_string(dev
, line
);
742 static int import_program_into_properties(struct udev_device
*dev
, const char *program
)
744 struct udev
*udev
= udev_device_get_udev(dev
);
750 envp
= udev_device_get_properties_envp(dev
);
751 if (util_run_program(udev
, program
, envp
, result
, sizeof(result
), &reslen
) != 0)
755 while (line
!= NULL
) {
758 pos
= strchr(line
, '\n');
763 import_property_from_string(dev
, line
);
769 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
771 struct udev
*udev
= udev_device_get_udev(dev
);
772 struct udev_device
*dev_parent
;
773 struct udev_list_entry
*list_entry
;
775 dev_parent
= udev_device_get_parent(dev
);
776 if (dev_parent
== NULL
)
779 dbg(udev
, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent
));
780 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
781 const char *key
= udev_list_entry_get_name(list_entry
);
782 const char *val
= udev_list_entry_get_value(list_entry
);
784 if (fnmatch(filter
, key
, 0) == 0) {
785 struct udev_list_entry
*entry
;
787 dbg(udev
, "import key '%s=%s'\n", key
, val
);
788 entry
= udev_device_add_property(dev
, key
, val
);
789 /* store in db, skip private keys */
791 udev_list_entry_set_flag(entry
, 1);
797 #define WAIT_LOOP_PER_SECOND 50
798 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
800 struct udev
*udev
= udev_device_get_udev(dev
);
801 char filepath
[UTIL_PATH_SIZE
];
802 char devicepath
[UTIL_PATH_SIZE
];
804 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
806 /* a relative path is a device attribute */
807 devicepath
[0] = '\0';
808 if (file
[0] != '/') {
809 util_strscpyl(devicepath
, sizeof(devicepath
),
810 udev_get_sys_path(udev
), udev_device_get_devpath(dev
), NULL
);
811 util_strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
815 dbg(udev
, "will wait %i sec for '%s'\n", timeout
, file
);
818 if (stat(file
, &stats
) == 0) {
819 info(udev
, "file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
822 /* make sure, the device did not disappear in the meantime */
823 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
824 info(udev
, "device disappeared while waiting for '%s'\n", file
);
827 info(udev
, "wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
828 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
830 info(udev
, "waiting for '%s' failed\n", file
);
834 static int attr_subst_subdir(char *attr
, size_t len
)
838 if (strstr(attr
, "/*/")) {
840 char dirname
[UTIL_PATH_SIZE
];
844 util_strscpy(dirname
, sizeof(dirname
), attr
);
845 pos
= strstr(dirname
, "/*/");
850 dir
= opendir(dirname
);
854 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
857 if (dent
->d_name
[0] == '.')
859 util_strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
860 if (stat(attr
, &stats
) == 0) {
872 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
878 if (linepos
== NULL
&& linepos
[0] == '\0')
881 /* skip whitespace */
882 while (isspace(linepos
[0]) || linepos
[0] == ',')
886 if (linepos
[0] == '\0')
892 if (linepos
[0] == '\0')
894 if (isspace(linepos
[0]))
896 if (linepos
[0] == '=')
898 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
899 if (linepos
[1] == '=')
903 /* remember end of key */
906 /* skip whitespace after key */
907 while (isspace(linepos
[0]))
909 if (linepos
[0] == '\0')
912 /* get operation type */
913 if (linepos
[0] == '=' && linepos
[1] == '=') {
916 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
919 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
922 } else if (linepos
[0] == '=') {
925 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
926 *op
= OP_ASSIGN_FINAL
;
934 /* skip whitespace after operator */
935 while (isspace(linepos
[0]))
937 if (linepos
[0] == '\0')
941 if (linepos
[0] == '"')
948 temp
= strchr(linepos
, '"');
953 dbg(udev
, "%s '%s'-'%s'\n", operation_str(*op
), *key
, *value
);
955 /* move line to next key */
960 /* extract possible KEY{attr} */
961 static char *get_key_attribute(struct udev
*udev
, char *str
)
966 attr
= strchr(str
, '{');
969 pos
= strchr(attr
, '}');
971 err(udev
, "missing closing brace for format\n");
975 dbg(udev
, "attribute='%s'\n", attr
);
981 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
982 enum operation_type op
,
983 const char *value
, const void *data
)
985 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
986 const char *attr
= NULL
;
988 memset(token
, 0x00, sizeof(struct token
));
1000 case TK_M_SUBSYSTEMS
:
1003 case TK_M_IMPORT_FILE
:
1004 case TK_M_IMPORT_PROG
:
1005 case TK_M_IMPORT_PARENT
:
1013 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1021 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1022 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
1025 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1027 token
->key
.mode
= *(mode_t
*)data
;
1029 case TK_A_IGNORE_DEVICE
:
1030 case TK_A_STRING_ESCAPE_NONE
:
1031 case TK_A_STRING_ESCAPE_REPLACE
:
1032 case TK_A_IGNORE_REMOVE
:
1033 case TK_A_LAST_RULE
:
1036 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1037 token
->key
.ignore_error
= *(int *)data
;
1039 case TK_A_INOTIFY_WATCH
:
1040 case TK_A_NUM_FAKE_PART
:
1041 case TK_A_DEVLINK_PRIO
:
1042 token
->key
.devlink_prio
= *(int *)data
;
1045 token
->key
.uid
= *(uid_t
*)data
;
1048 token
->key
.gid
= *(gid_t
*)data
;
1051 token
->key
.mode
= *(mode_t
*)data
;
1053 case TK_A_EVENT_TIMEOUT
:
1054 token
->key
.event_timeout
= *(int *)data
;
1057 case TK_M_PARENTS_MIN
:
1058 case TK_M_PARENTS_MAX
:
1062 err(rule_tmp
->rules
->udev
, "wrong type %u\n", type
);
1066 if (value
!= NULL
&& type
< TK_M_MAX
) {
1067 /* check if we need to split or call fnmatch() while matching rules */
1068 enum string_glob_type glob
;
1072 has_split
= (strchr(value
, '|') != NULL
);
1073 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
|| strchr(value
, '[') != NULL
);
1074 if (has_split
&& has_glob
) {
1075 glob
= GL_SPLIT_GLOB
;
1076 } else if (has_split
) {
1078 } else if (has_glob
) {
1079 if (strcmp(value
, "?*") == 0)
1080 glob
= GL_SOMETHING
;
1086 token
->key
.glob
= glob
;
1089 if (value
!= NULL
&& type
> TK_M_MAX
) {
1090 /* check if assigned value has substitution chars */
1091 if (value
[0] == '[')
1092 token
->key
.subst
= SB_SUBSYS
;
1093 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
1094 token
->key
.subst
= SB_FORMAT
;
1096 token
->key
.subst
= SB_NONE
;
1100 /* check if property/attribut name has substitution chars */
1102 token
->key
.attrsubst
= SB_SUBSYS
;
1103 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
1104 token
->key
.attrsubst
= SB_FORMAT
;
1106 token
->key
.attrsubst
= SB_NONE
;
1109 token
->key
.type
= type
;
1111 rule_tmp
->token_cur
++;
1112 if (rule_tmp
->token_cur
>= ARRAY_SIZE(rule_tmp
->token
)) {
1113 err(rule_tmp
->rules
->udev
, "temporary rule array too small\n");
1119 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1122 unsigned int start
= 0;
1123 unsigned int end
= rule_tmp
->token_cur
;
1125 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1126 enum token_type next_val
= TK_UNSET
;
1127 unsigned int next_idx
;
1130 /* find smallest value */
1131 for (j
= start
; j
< end
; j
++) {
1132 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1134 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1135 next_val
= rule_tmp
->token
[j
].type
;
1140 /* add token and mark done */
1141 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1143 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1146 if (next_idx
== start
)
1148 if (next_idx
+1 == end
)
1154 static int add_rule(struct udev_rules
*rules
, char *line
,
1155 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1159 struct rule_tmp rule_tmp
;
1161 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1162 rule_tmp
.rules
= rules
;
1163 rule_tmp
.rule
.type
= TK_RULE
;
1164 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1165 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1171 enum operation_type op
;
1173 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1176 if (strcmp(key
, "ACTION") == 0) {
1177 if (op
> OP_MATCH_MAX
) {
1178 err(rules
->udev
, "invalid ACTION operation\n");
1181 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1185 if (strcmp(key
, "DEVPATH") == 0) {
1186 if (op
> OP_MATCH_MAX
) {
1187 err(rules
->udev
, "invalid DEVPATH operation\n");
1190 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1194 if (strcmp(key
, "KERNEL") == 0) {
1195 if (op
> OP_MATCH_MAX
) {
1196 err(rules
->udev
, "invalid KERNEL operation\n");
1199 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1203 if (strcmp(key
, "SUBSYSTEM") == 0) {
1204 if (op
> OP_MATCH_MAX
) {
1205 err(rules
->udev
, "invalid SUBSYSTEM operation\n");
1208 /* bus, class, subsystem events should all be the same */
1209 if (strcmp(value
, "subsystem") == 0 ||
1210 strcmp(value
, "bus") == 0 ||
1211 strcmp(value
, "class") == 0) {
1212 if (strcmp(value
, "bus") == 0 || strcmp(value
, "class") == 0)
1213 err(rules
->udev
, "'%s' must be specified as 'subsystem' \n"
1214 "please fix it in %s:%u", value
, filename
, lineno
);
1215 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1217 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1221 if (strcmp(key
, "DRIVER") == 0) {
1222 if (op
> OP_MATCH_MAX
) {
1223 err(rules
->udev
, "invalid DRIVER operation\n");
1226 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1230 if (strncmp(key
, "ATTR{", sizeof("ATTR{")-1) == 0) {
1231 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1233 err(rules
->udev
, "error parsing ATTR attribute\n");
1236 if (op
< OP_MATCH_MAX
) {
1237 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1239 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1244 if (strcmp(key
, "KERNELS") == 0 ||
1245 strcmp(key
, "ID") == 0) {
1246 if (op
> OP_MATCH_MAX
) {
1247 err(rules
->udev
, "invalid KERNELS operation\n");
1250 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1254 if (strcmp(key
, "SUBSYSTEMS") == 0 ||
1255 strcmp(key
, "BUS") == 0) {
1256 if (op
> OP_MATCH_MAX
) {
1257 err(rules
->udev
, "invalid SUBSYSTEMS operation\n");
1260 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1264 if (strcmp(key
, "DRIVERS") == 0) {
1265 if (op
> OP_MATCH_MAX
) {
1266 err(rules
->udev
, "invalid DRIVERS operation\n");
1269 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1273 if (strncmp(key
, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1274 strncmp(key
, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1275 if (op
> OP_MATCH_MAX
) {
1276 err(rules
->udev
, "invalid ATTRS operation\n");
1279 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1281 err(rules
->udev
, "error parsing ATTRS attribute\n");
1284 if (strncmp(attr
, "device/", 7) == 0)
1285 err(rules
->udev
, "the 'device' link may not be available in a future kernel, "
1286 "please fix it in %s:%u", filename
, lineno
);
1287 else if (strstr(attr
, "../") != NULL
)
1288 err(rules
->udev
, "do not reference parent sysfs directories directly, "
1289 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1290 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1294 if (strncmp(key
, "ENV{", sizeof("ENV{")-1) == 0) {
1295 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1297 err(rules
->udev
, "error parsing ENV attribute\n");
1300 if (op
< OP_MATCH_MAX
) {
1301 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1304 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1310 if (strcmp(key
, "PROGRAM") == 0) {
1311 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1315 if (strcmp(key
, "RESULT") == 0) {
1316 if (op
> OP_MATCH_MAX
) {
1317 err(rules
->udev
, "invalid RESULT operation\n");
1320 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1324 if (strncmp(key
, "IMPORT", sizeof("IMPORT")-1) == 0) {
1325 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1326 if (attr
!= NULL
&& strstr(attr
, "program")) {
1327 dbg(rules
->udev
, "IMPORT will be executed\n");
1328 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1329 } else if (attr
!= NULL
&& strstr(attr
, "file")) {
1330 dbg(rules
->udev
, "IMPORT will be included as file\n");
1331 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1332 } else if (attr
!= NULL
&& strstr(attr
, "parent")) {
1333 dbg(rules
->udev
, "IMPORT will include the parent values\n");
1334 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1336 /* figure it out if it is executable */
1337 char file
[UTIL_PATH_SIZE
];
1339 struct stat statbuf
;
1341 /* allow programs in /lib/udev called without the path */
1342 if (value
[0] != '/')
1343 util_strscpyl(file
, sizeof(file
), LIBEXECDIR
"/", value
, NULL
);
1345 util_strscpy(file
, sizeof(file
), value
);
1346 pos
= strchr(file
, ' ');
1349 dbg(rules
->udev
, "IMPORT auto mode for '%s'\n", file
);
1350 if (stat(file
, &statbuf
) == 0 && (statbuf
.st_mode
& S_IXUSR
)) {
1351 dbg(rules
->udev
, "IMPORT will be executed (autotype)\n");
1352 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1354 dbg(rules
->udev
, "IMPORT will be included as file (autotype)\n");
1355 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1361 if (strncmp(key
, "TEST", sizeof("TEST")-1) == 0) {
1364 if (op
> OP_MATCH_MAX
) {
1365 err(rules
->udev
, "invalid TEST operation\n");
1368 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1370 mode
= strtol(attr
, NULL
, 8);
1371 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1373 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1378 if (strncmp(key
, "RUN", sizeof("RUN")-1) == 0) {
1381 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1382 if (attr
!= NULL
&& strstr(attr
, "fail_event_on_error"))
1384 rule_add_key(&rule_tmp
, TK_A_RUN
, op
, value
, &flag
);
1388 if (strcmp(key
, "WAIT_FOR") == 0 || strcmp(key
, "WAIT_FOR_SYSFS") == 0) {
1389 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1393 if (strcmp(key
, "LABEL") == 0) {
1394 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1398 if (strcmp(key
, "GOTO") == 0) {
1399 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1403 if (strncmp(key
, "NAME", sizeof("NAME")-1) == 0) {
1404 if (op
< OP_MATCH_MAX
) {
1405 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1407 if (value
[0] == '\0')
1408 info(rules
->udev
, "name empty, node creation suppressed\n");
1409 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1410 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("NAME")-1);
1412 if (strstr(attr
, "all_partitions") != NULL
) {
1413 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1415 dbg(rules
->udev
, "creation of partition nodes requested\n");
1416 rule_add_key(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1418 if (strstr(attr
, "ignore_remove") != NULL
) {
1419 dbg(rules
->udev
, "remove event should be ignored\n");
1420 rule_add_key(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1424 rule_tmp
.rule
.rule
.flags
= 1;
1428 if (strcmp(key
, "SYMLINK") == 0) {
1429 if (op
< OP_MATCH_MAX
)
1430 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1432 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1433 rule_tmp
.rule
.rule
.flags
= 1;
1437 if (strcmp(key
, "OWNER") == 0) {
1441 uid
= strtoul(value
, &endptr
, 10);
1442 if (endptr
[0] == '\0') {
1443 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1444 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1445 uid
= add_uid(rules
, value
);
1446 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1447 } else if (rules
->resolve_names
>= 0) {
1448 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1450 rule_tmp
.rule
.rule
.flags
= 1;
1454 if (strcmp(key
, "GROUP") == 0) {
1458 gid
= strtoul(value
, &endptr
, 10);
1459 if (endptr
[0] == '\0') {
1460 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1461 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1462 gid
= add_gid(rules
, value
);
1463 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1464 } else if (rules
->resolve_names
>= 0) {
1465 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1467 rule_tmp
.rule
.rule
.flags
= 1;
1471 if (strcmp(key
, "MODE") == 0) {
1475 mode
= strtol(value
, &endptr
, 8);
1476 if (endptr
[0] == '\0')
1477 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1479 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1480 rule_tmp
.rule
.rule
.flags
= 1;
1484 if (strcmp(key
, "OPTIONS") == 0) {
1487 if (strstr(value
, "last_rule") != NULL
) {
1488 dbg(rules
->udev
, "last rule to be applied\n");
1489 rule_add_key(&rule_tmp
, TK_A_LAST_RULE
, 0, NULL
, NULL
);
1491 if (strstr(value
, "ignore_device") != NULL
) {
1492 dbg(rules
->udev
, "device should be ignored\n");
1493 rule_add_key(&rule_tmp
, TK_A_IGNORE_DEVICE
, 0, NULL
, NULL
);
1495 if (strstr(value
, "ignore_remove") != NULL
) {
1496 dbg(rules
->udev
, "remove event should be ignored\n");
1497 rule_add_key(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1499 pos
= strstr(value
, "link_priority=");
1501 int prio
= atoi(&pos
[strlen("link_priority=")]);
1503 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, 0, NULL
, &prio
);
1504 dbg(rules
->udev
, "link priority=%i\n", prio
);
1506 pos
= strstr(value
, "event_timeout=");
1508 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1510 rule_add_key(&rule_tmp
, TK_A_EVENT_TIMEOUT
, 0, NULL
, &tout
);
1511 dbg(rules
->udev
, "event timout=%i\n", tout
);
1513 pos
= strstr(value
, "string_escape=");
1515 pos
= &pos
[strlen("string_escape=")];
1516 if (strncmp(pos
, "none", strlen("none")) == 0)
1517 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, 0, NULL
, NULL
);
1518 else if (strncmp(pos
, "replace", strlen("replace")) == 0)
1519 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, 0, NULL
, NULL
);
1521 if (strstr(value
, "all_partitions") != NULL
) {
1522 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1524 rule_add_key(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1525 dbg(rules
->udev
, "creation of partition nodes requested\n");
1527 pos
= strstr(value
, "nowatch");
1531 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, 0, NULL
, &off
);
1532 dbg(rules
->udev
, "inotify watch of device disabled\n");
1534 pos
= strstr(value
, "watch");
1538 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, 0, NULL
, &on
);
1539 dbg(rules
->udev
, "inotify watch of device requested\n");
1544 err(rules
->udev
, "unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1547 /* add rule token */
1548 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1549 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1552 /* add tokens to list, sorted by type */
1553 if (sort_token(rules
, &rule_tmp
) != 0)
1558 err(rules
->udev
, "invalid rule '%s:%u'\n", filename
, lineno
);
1562 static int parse_file(struct udev_rules
*rules
, const char *filename
, unsigned short filename_off
)
1565 unsigned int first_token
;
1566 char line
[UTIL_LINE_SIZE
];
1570 info(rules
->udev
, "reading '%s' as rules file\n", filename
);
1572 f
= fopen(filename
, "r");
1576 first_token
= rules
->token_cur
;
1578 while(fgets(line
, sizeof(line
), f
) != NULL
) {
1582 /* skip whitespace */
1585 while (isspace(key
[0]))
1596 /* continue reading if backslash+newline is found */
1597 while (line
[len
-2] == '\\') {
1598 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1604 if (len
+1 >= sizeof(line
)) {
1605 err(rules
->udev
, "line too long '%s':%u, ignored\n", filename
, line_nr
);
1608 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1612 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1613 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1614 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1615 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1618 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1619 if (rules
->tokens
[j
].type
!= TK_RULE
)
1621 if (rules
->tokens
[j
].rule
.label_off
== 0)
1623 if (strcmp(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]) != 0)
1625 rules
->tokens
[i
].key
.rule_goto
= j
;
1628 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1629 err(rules
->udev
, "GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1635 static int add_matching_files(struct udev
*udev
, struct udev_list_node
*file_list
, const char *dirname
, const char *suffix
)
1638 char filename
[UTIL_PATH_SIZE
];
1640 dbg(udev
, "open directory '%s'\n", dirname
);
1641 dir
= opendir(dirname
);
1643 info(udev
, "unable to open '%s': %m\n", dirname
);
1648 struct dirent
*dent
;
1650 dent
= readdir(dir
);
1651 if (dent
== NULL
|| dent
->d_name
[0] == '\0')
1654 if (dent
->d_name
[0] == '.')
1657 /* look for file matching with specified suffix */
1658 if (suffix
!= NULL
) {
1661 ext
= strrchr(dent
->d_name
, '.');
1664 if (strcmp(ext
, suffix
) != 0)
1667 util_strscpyl(filename
, sizeof(filename
), dirname
, "/", dent
->d_name
, NULL
);
1668 dbg(udev
, "put file '%s' into list\n", filename
);
1669 udev_list_entry_add(udev
, file_list
, filename
, NULL
, 1, 1);
1676 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1678 struct udev_rules
*rules
;
1679 struct stat statbuf
;
1680 struct udev_list_node file_list
;
1681 struct udev_list_entry
*file_loop
, *file_tmp
;
1682 struct token end_token
;
1684 rules
= malloc(sizeof(struct udev_rules
));
1687 memset(rules
, 0x00, sizeof(struct udev_rules
));
1689 rules
->resolve_names
= resolve_names
;
1690 udev_list_init(&file_list
);
1692 /* init token array and string buffer */
1693 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1694 if (rules
->tokens
== NULL
)
1696 rules
->token_max
= PREALLOC_TOKEN
;
1698 rules
->buf
= malloc(PREALLOC_STRBUF
);
1699 if (rules
->buf
== NULL
)
1701 rules
->buf_max
= PREALLOC_STRBUF
;
1702 /* offset 0 is always '\0' */
1703 rules
->buf
[0] = '\0';
1705 dbg(udev
, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1706 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1708 rules
->trie_nodes
= malloc(PREALLOC_TRIE
* sizeof(struct trie_node
));
1709 if (rules
->trie_nodes
== NULL
)
1711 rules
->trie_nodes_max
= PREALLOC_TRIE
;
1712 /* offset 0 is the trie root, with an empty string */
1713 memset(rules
->trie_nodes
, 0x00, sizeof(struct trie_node
));
1714 rules
->trie_nodes_cur
= 1;
1716 if (udev_get_rules_path(udev
) != NULL
) {
1717 /* custom rules location for testing */
1718 add_matching_files(udev
, &file_list
, udev_get_rules_path(udev
), ".rules");
1720 char filename
[PATH_MAX
];
1721 struct udev_list_node sort_list
;
1722 struct udev_list_entry
*sort_loop
, *sort_tmp
;
1724 /* read user/custom rules */
1725 add_matching_files(udev
, &file_list
, SYSCONFDIR
"/udev/rules.d", ".rules");
1727 /* read dynamic/temporary rules */
1728 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(udev
), "/.udev/rules.d", NULL
);
1729 if (stat(filename
, &statbuf
) != 0) {
1730 util_create_path(udev
, filename
);
1731 udev_selinux_setfscreatecon(udev
, filename
, S_IFDIR
|0755);
1732 mkdir(filename
, 0755);
1733 udev_selinux_resetfscreatecon(udev
);
1735 udev_list_init(&sort_list
);
1736 add_matching_files(udev
, &sort_list
, filename
, ".rules");
1738 /* read default rules */
1739 add_matching_files(udev
, &sort_list
, LIBEXECDIR
"/rules.d", ".rules");
1741 /* sort all rules files by basename into list of files */
1742 udev_list_entry_foreach_safe(sort_loop
, sort_tmp
, udev_list_get_entry(&sort_list
)) {
1743 const char *sort_name
= udev_list_entry_get_name(sort_loop
);
1744 const char *sort_base
= strrchr(sort_name
, '/');
1746 if (sort_base
== NULL
)
1748 /* sort entry into existing list */
1749 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1750 const char *file_name
= udev_list_entry_get_name(file_loop
);
1751 const char *file_base
= strrchr(file_name
, '/');
1753 if (file_base
== NULL
)
1755 if (strcmp(file_base
, sort_base
) == 0) {
1756 info(udev
, "rule file basename '%s' already added, ignoring '%s'\n",
1757 file_name
, sort_name
);
1758 udev_list_entry_delete(sort_loop
);
1762 if (strcmp(file_base
, sort_base
) > 0) {
1763 /* found later file, insert before */
1764 udev_list_entry_remove(sort_loop
);
1765 udev_list_entry_insert_before(sort_loop
, file_loop
);
1770 /* current file already handled */
1771 if (sort_loop
== NULL
)
1773 /* no later file, append to end of list */
1774 udev_list_entry_remove(sort_loop
);
1775 udev_list_entry_append(sort_loop
, &file_list
);
1779 /* add all filenames to the string buffer */
1780 udev_list_entry_foreach(file_loop
, udev_list_get_entry(&file_list
)) {
1781 const char *filename
= udev_list_entry_get_name(file_loop
);
1782 unsigned int filename_off
;
1784 filename_off
= add_string(rules
, filename
);
1785 /* the offset in the rule is limited to unsigned short */
1786 if (filename_off
< USHRT_MAX
)
1787 udev_list_entry_set_flag(file_loop
, filename_off
);
1790 /* parse list of files */
1791 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1792 const char *filename
= udev_list_entry_get_name(file_loop
);
1793 unsigned int filename_off
= udev_list_entry_get_flag(file_loop
);
1795 if (stat(filename
, &statbuf
) == 0 && statbuf
.st_size
> 0)
1796 parse_file(rules
, filename
, filename_off
);
1798 info(udev
, "can not read '%s'\n", filename
);
1799 udev_list_entry_delete(file_loop
);
1802 memset(&end_token
, 0x00, sizeof(struct token
));
1803 end_token
.type
= TK_END
;
1804 add_token(rules
, &end_token
);
1806 /* shrink allocated token and string buffer */
1807 if (rules
->token_cur
< rules
->token_max
) {
1808 struct token
*tokens
;
1810 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1811 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1812 rules
->tokens
= tokens
;
1813 rules
->token_max
= rules
->token_cur
;
1816 if (rules
->buf_cur
< rules
->buf_max
) {
1819 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1820 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1822 rules
->buf_max
= rules
->buf_cur
;
1825 info(udev
, "rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1826 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1827 info(udev
, "temporary index used %zu bytes (%u * %zu bytes)\n",
1828 rules
->trie_nodes_cur
* sizeof(struct trie_node
),
1829 rules
->trie_nodes_cur
, sizeof(struct trie_node
));
1832 free(rules
->trie_nodes
);
1833 rules
->trie_nodes
= NULL
;
1834 rules
->trie_nodes_cur
= 0;
1835 rules
->trie_nodes_max
= 0;
1837 /* cleanup uid/gid cache */
1840 rules
->uids_cur
= 0;
1841 rules
->uids_max
= 0;
1844 rules
->gids_cur
= 0;
1845 rules
->gids_max
= 0;
1851 void udev_rules_unref(struct udev_rules
*rules
)
1855 free(rules
->tokens
);
1857 free(rules
->trie_nodes
);
1863 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1865 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1872 switch (token
->key
.glob
) {
1874 match
= (strcmp(key_value
, val
) == 0);
1877 match
= (fnmatch(key_value
, val
, 0) == 0);
1884 split
= &rules
->buf
[token
->key
.value_off
];
1889 next
= strchr(split
, '|');
1891 size_t matchlen
= (size_t)(next
- split
);
1893 match
= (matchlen
== len
&& strncmp(split
, val
, matchlen
) == 0);
1897 match
= (strcmp(split
, val
) == 0);
1906 char value
[UTIL_PATH_SIZE
];
1908 util_strscpy(value
, sizeof(value
), &rules
->buf
[token
->key
.value_off
]);
1910 while (key_value
!= NULL
) {
1911 pos
= strchr(key_value
, '|');
1916 dbg(rules
->udev
, "match %s '%s' <-> '%s'\n", token_str(token
->type
), key_value
, val
);
1917 match
= (fnmatch(key_value
, val
, 0) == 0);
1925 match
= (val
[0] != '\0');
1931 if (match
&& (token
->key
.op
== OP_MATCH
)) {
1932 dbg(rules
->udev
, "%s is true (matching value)\n", token_str(token
->type
));
1935 if (!match
&& (token
->key
.op
== OP_NOMATCH
)) {
1936 dbg(rules
->udev
, "%s is true (non-matching value)\n", token_str(token
->type
));
1939 dbg(rules
->udev
, "%s is not true\n", token_str(token
->type
));
1943 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1946 char nbuf
[UTIL_NAME_SIZE
];
1948 char vbuf
[UTIL_NAME_SIZE
];
1951 name
= &rules
->buf
[cur
->key
.attr_off
];
1952 switch (cur
->key
.attrsubst
) {
1954 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1958 value
= udev_device_get_sysattr_value(dev
, name
);
1963 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1971 /* remove trailing whitespace, if not asked to match for it */
1972 len
= strlen(value
);
1973 if (len
> 0 && isspace(value
[len
-1])) {
1974 const char *key_value
;
1977 key_value
= &rules
->buf
[cur
->key
.value_off
];
1978 klen
= strlen(key_value
);
1979 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1980 if (value
!= vbuf
) {
1981 util_strscpy(vbuf
, sizeof(vbuf
), value
);
1984 while (len
> 0 && isspace(vbuf
[--len
]))
1986 dbg(rules
->udev
, "removed trailing whitespace from '%s'\n", value
);
1990 return match_key(rules
, cur
, value
);
1999 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
)
2003 enum escape_type esc
= ESCAPE_UNSET
;
2006 if (rules
->tokens
== NULL
)
2009 can_set_name
= ((strcmp(udev_device_get_action(event
->dev
), "add") == 0 ||
2010 strcmp(udev_device_get_action(event
->dev
), "change") == 0) &&
2011 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
2012 strcmp(udev_device_get_subsystem(event
->dev
), "net") == 0));
2014 /* loop through token list, match, run actions or forward to next rule */
2015 cur
= &rules
->tokens
[0];
2018 dump_token(rules
, cur
);
2019 switch (cur
->type
) {
2023 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2024 if (!can_set_name
&& rule
->rule
.flags
)
2029 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
2033 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
2037 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
2042 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
2043 struct udev_list_entry
*list_entry
;
2046 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
2047 const char *devlink
;
2049 devlink
= &udev_list_entry_get_name(list_entry
)[devlen
];
2050 if (match_key(rules
, cur
, devlink
) == 0) {
2060 if (match_key(rules
, cur
, event
->name
) != 0)
2065 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2068 value
= udev_device_get_property_value(event
->dev
, key_name
);
2069 if (value
== NULL
) {
2070 dbg(event
->udev
, "ENV{%s} is not set, treat as empty\n", key_name
);
2073 if (match_key(rules
, cur
, value
))
2077 case TK_M_SUBSYSTEM
:
2078 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
2082 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2087 char filename
[UTIL_PATH_SIZE
];
2090 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2091 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2092 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2097 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2101 case TK_M_SUBSYSTEMS
:
2107 /* get whole sequence of parent matches */
2109 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2112 /* loop over parents */
2113 event
->dev_parent
= event
->dev
;
2117 dbg(event
->udev
, "parent: '%s'\n", udev_device_get_syspath(event
->dev_parent
));
2118 /* loop over sequence of parent match keys */
2119 for (key
= cur
; key
< next
; key
++ ) {
2120 dump_token(rules
, key
);
2123 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2126 case TK_M_SUBSYSTEMS
:
2127 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2131 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2135 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2141 dbg(event
->udev
, "parent key matched\n");
2143 dbg(event
->udev
, "all parent keys matched\n");
2144 /* all keys matched */
2148 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2149 if (event
->dev_parent
== NULL
)
2152 /* move behind our sequence of parent match keys */
2158 char filename
[UTIL_PATH_SIZE
];
2159 struct stat statbuf
;
2162 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2163 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2164 if (filename
[0] != '/') {
2165 char tmp
[UTIL_PATH_SIZE
];
2167 util_strscpy(tmp
, sizeof(tmp
), filename
);
2168 util_strscpyl(filename
, sizeof(filename
),
2169 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2172 attr_subst_subdir(filename
, sizeof(filename
));
2174 match
= (stat(filename
, &statbuf
) == 0);
2175 dbg(event
->udev
, "'%s' %s", filename
, match
? "exists\n" : "does not exist\n");
2176 if (match
&& cur
->key
.mode
> 0) {
2177 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2178 dbg(event
->udev
, "'%s' has mode=%#o and %s %#o\n", filename
, statbuf
.st_mode
,
2179 match
? "matches" : "does not match", cur
->key
.mode
);
2181 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2183 if (!match
&& cur
->key
.op
== OP_MATCH
)
2189 char program
[UTIL_PATH_SIZE
];
2191 char result
[UTIL_PATH_SIZE
];
2193 free(event
->program_result
);
2194 event
->program_result
= NULL
;
2195 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], program
, sizeof(program
));
2196 envp
= udev_device_get_properties_envp(event
->dev
);
2197 info(event
->udev
, "PROGRAM '%s' %s:%u\n",
2199 &rules
->buf
[rule
->rule
.filename_off
],
2200 rule
->rule
.filename_line
);
2201 if (util_run_program(event
->udev
, program
, envp
, result
, sizeof(result
), NULL
) != 0) {
2202 if (cur
->key
.op
!= OP_NOMATCH
)
2207 util_remove_trailing_chars(result
, '\n');
2208 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2209 count
= udev_util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2211 info(event
->udev
, "%i character(s) replaced\n" , count
);
2213 event
->program_result
= strdup(result
);
2214 dbg(event
->udev
, "storing result '%s'\n", event
->program_result
);
2215 if (cur
->key
.op
== OP_NOMATCH
)
2220 case TK_M_IMPORT_FILE
:
2222 char import
[UTIL_PATH_SIZE
];
2224 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2225 if (import_file_into_properties(event
->dev
, import
) != 0)
2226 if (cur
->key
.op
!= OP_NOMATCH
)
2230 case TK_M_IMPORT_PROG
:
2232 char import
[UTIL_PATH_SIZE
];
2234 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2235 info(event
->udev
, "IMPORT '%s' %s:%u\n",
2237 &rules
->buf
[rule
->rule
.filename_off
],
2238 rule
->rule
.filename_line
);
2239 if (import_program_into_properties(event
->dev
, import
) != 0)
2240 if (cur
->key
.op
!= OP_NOMATCH
)
2244 case TK_M_IMPORT_PARENT
:
2246 char import
[UTIL_PATH_SIZE
];
2248 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2249 if (import_parent_into_properties(event
->dev
, import
) != 0)
2250 if (cur
->key
.op
!= OP_NOMATCH
)
2255 if (match_key(rules
, cur
, event
->program_result
) != 0)
2259 case TK_A_IGNORE_DEVICE
:
2260 event
->ignore_device
= 1;
2263 case TK_A_STRING_ESCAPE_NONE
:
2266 case TK_A_STRING_ESCAPE_REPLACE
:
2267 esc
= ESCAPE_REPLACE
;
2269 case TK_A_NUM_FAKE_PART
:
2270 if (strcmp(udev_device_get_subsystem(event
->dev
), "block") != 0)
2272 if (udev_device_get_sysnum(event
->dev
) != NULL
)
2274 udev_device_set_num_fake_partitions(event
->dev
, cur
->key
.num_fake_part
);
2276 case TK_A_INOTIFY_WATCH
:
2277 event
->inotify_watch
= cur
->key
.watch
;
2279 case TK_A_DEVLINK_PRIO
:
2280 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2284 char owner
[UTIL_NAME_SIZE
];
2286 if (event
->owner_final
)
2288 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2289 event
->owner_final
= 1;
2290 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], owner
, sizeof(owner
));
2291 event
->uid
= util_lookup_user(event
->udev
, owner
);
2292 info(event
->udev
, "OWNER %u %s:%u\n",
2294 &rules
->buf
[rule
->rule
.filename_off
],
2295 rule
->rule
.filename_line
);
2300 char group
[UTIL_NAME_SIZE
];
2302 if (event
->group_final
)
2304 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2305 event
->group_final
= 1;
2306 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], group
, sizeof(group
));
2307 event
->gid
= util_lookup_group(event
->udev
, group
);
2308 info(event
->udev
, "GROUP %u %s:%u\n",
2310 &rules
->buf
[rule
->rule
.filename_off
],
2311 rule
->rule
.filename_line
);
2316 char mode
[UTIL_NAME_SIZE
];
2319 if (event
->mode_final
)
2321 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2322 event
->mode_final
= 1;
2323 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], mode
, sizeof(mode
));
2324 event
->mode
= strtol(mode
, &endptr
, 8);
2325 if (endptr
[0] != '\0') {
2326 err(event
->udev
, "invalide mode '%s' set default mode 0660\n", mode
);
2329 info(event
->udev
, "MODE %#o %s:%u\n",
2331 &rules
->buf
[rule
->rule
.filename_off
],
2332 rule
->rule
.filename_line
);
2336 if (event
->owner_final
)
2338 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2339 event
->owner_final
= 1;
2340 event
->uid
= cur
->key
.uid
;
2341 info(event
->udev
, "OWNER %u %s:%u\n",
2343 &rules
->buf
[rule
->rule
.filename_off
],
2344 rule
->rule
.filename_line
);
2347 if (event
->group_final
)
2349 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2350 event
->group_final
= 1;
2351 event
->gid
= cur
->key
.gid
;
2352 info(event
->udev
, "GROUP %u %s:%u\n",
2354 &rules
->buf
[rule
->rule
.filename_off
],
2355 rule
->rule
.filename_line
);
2358 if (event
->mode_final
)
2360 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2361 event
->mode_final
= 1;
2362 event
->mode
= cur
->key
.mode
;
2363 info(event
->udev
, "MODE %#o %s:%u\n",
2365 &rules
->buf
[rule
->rule
.filename_off
],
2366 rule
->rule
.filename_line
);
2370 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2371 char *value
= &rules
->buf
[cur
->key
.value_off
];
2373 if (value
[0] != '\0') {
2374 char temp_value
[UTIL_NAME_SIZE
];
2375 struct udev_list_entry
*entry
;
2377 udev_event_apply_format(event
, value
, temp_value
, sizeof(temp_value
));
2378 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2379 /* store in db, skip private keys */
2381 udev_list_entry_set_flag(entry
, 1);
2383 udev_device_add_property(event
->dev
, name
, NULL
);
2389 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2390 char name_str
[UTIL_PATH_SIZE
];
2393 if (event
->name_final
)
2395 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2396 event
->name_final
= 1;
2397 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2398 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2399 count
= udev_util_replace_chars(name_str
, "/");
2401 info(event
->udev
, "%i character(s) replaced\n", count
);
2404 event
->name
= strdup(name_str
);
2405 info(event
->udev
, "NAME '%s' %s:%u\n",
2407 &rules
->buf
[rule
->rule
.filename_off
],
2408 rule
->rule
.filename_line
);
2413 char temp
[UTIL_PATH_SIZE
];
2414 char filename
[UTIL_PATH_SIZE
];
2418 if (event
->devlink_final
)
2420 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2422 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2423 event
->devlink_final
= 1;
2424 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2425 udev_device_cleanup_devlinks_list(event
->dev
);
2427 /* allow multiple symlinks separated by spaces */
2428 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], temp
, sizeof(temp
));
2429 if (esc
== ESCAPE_UNSET
)
2430 count
= udev_util_replace_chars(temp
, "/ ");
2431 else if (esc
== ESCAPE_REPLACE
)
2432 count
= udev_util_replace_chars(temp
, "/");
2434 info(event
->udev
, "%i character(s) replaced\n" , count
);
2435 dbg(event
->udev
, "rule applied, added symlink(s) '%s'\n", temp
);
2437 while (isspace(pos
[0]))
2439 next
= strchr(pos
, ' ');
2442 info(event
->udev
, "LINK '%s' %s:%u\n",
2444 &rules
->buf
[rule
->rule
.filename_off
],
2445 rule
->rule
.filename_line
);
2446 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2447 udev_device_add_devlink(event
->dev
, filename
);
2448 while (isspace(next
[1]))
2451 next
= strchr(pos
, ' ');
2453 if (pos
[0] != '\0') {
2454 info(event
->udev
, "LINK '%s' %s:%u\n",
2456 &rules
->buf
[rule
->rule
.filename_off
],
2457 rule
->rule
.filename_line
);
2458 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2459 udev_device_add_devlink(event
->dev
, filename
);
2463 case TK_A_EVENT_TIMEOUT
:
2464 udev_device_set_event_timeout(event
->dev
, cur
->key
.event_timeout
);
2466 case TK_A_IGNORE_REMOVE
:
2467 udev_device_set_ignore_remove(event
->dev
, 1);
2471 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2472 char attr
[UTIL_PATH_SIZE
];
2473 char value
[UTIL_NAME_SIZE
];
2476 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2477 util_strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2478 attr_subst_subdir(attr
, sizeof(attr
));
2480 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], value
, sizeof(value
));
2481 info(event
->udev
, "ATTR '%s' writing '%s' %s:%u\n", attr
, value
,
2482 &rules
->buf
[rule
->rule
.filename_off
],
2483 rule
->rule
.filename_line
);
2484 f
= fopen(attr
, "w");
2486 if (fprintf(f
, "%s", value
) <= 0)
2487 err(event
->udev
, "error writing ATTR{%s}: %m\n", attr
);
2490 err(event
->udev
, "error opening ATTR{%s} for writing: %m\n", attr
);
2496 struct udev_list_entry
*list_entry
;
2498 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2499 udev_list_cleanup_entries(event
->udev
, &event
->run_list
);
2500 info(event
->udev
, "RUN '%s' %s:%u\n",
2501 &rules
->buf
[cur
->key
.value_off
],
2502 &rules
->buf
[rule
->rule
.filename_off
],
2503 rule
->rule
.filename_line
);
2504 list_entry
= udev_list_entry_add(event
->udev
, &event
->run_list
,
2505 &rules
->buf
[cur
->key
.value_off
], NULL
, 1, 0);
2506 if (cur
->key
.ignore_error
)
2507 udev_list_entry_set_flag(list_entry
, 1);
2511 if (cur
->key
.rule_goto
== 0)
2513 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2515 case TK_A_LAST_RULE
:
2519 case TK_M_PARENTS_MIN
:
2520 case TK_M_PARENTS_MAX
:
2523 err(rules
->udev
, "wrong type %u\n", cur
->type
);
2530 /* fast-forward to next rule */
2531 cur
= rule
+ rule
->rule
.token_count
;
2532 dbg(rules
->udev
, "forward to rule: %u\n",
2533 (unsigned int) (cur
- rules
->tokens
));