2 * Copyright (C) 2003-2010 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/>.
35 #define PREALLOC_TOKEN 2048
36 #define PREALLOC_STRBUF 32 * 1024
37 #define PREALLOC_TRIE 256
40 unsigned int name_off
;
48 /* this node's first child */
49 unsigned int child_idx
;
50 /* the next child of our parent node's child list */
51 unsigned int next_child_idx
;
52 /* this node's last child (shortcut for append) */
53 unsigned int last_child_idx
;
54 unsigned int value_off
;
55 unsigned short value_len
;
63 /* every key in the rules file becomes a token */
65 unsigned int token_cur
;
66 unsigned int token_max
;
68 /* all key strings are copied to a single string buffer */
72 unsigned int buf_count
;
74 /* during rule parsing, strings are indexed to find duplicates */
75 struct trie_node
*trie_nodes
;
76 unsigned int trie_nodes_cur
;
77 unsigned int trie_nodes_max
;
79 /* during rule parsing, uid/gid lookup results are cached */
81 unsigned int uids_cur
;
82 unsigned int uids_max
;
84 unsigned int gids_cur
;
85 unsigned int gids_max
;
88 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
101 enum string_glob_type
{
103 GL_PLAIN
, /* no special chars */
104 GL_GLOB
, /* shell globs ?,*,[] */
105 GL_SPLIT
, /* multi-value A|B */
106 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
107 GL_SOMETHING
, /* commonly used "?*" */
110 enum string_subst_type
{
117 /* tokens of a rule are sorted/handled in this order */
122 TK_M_ACTION
, /* val */
123 TK_M_DEVPATH
, /* val */
124 TK_M_KERNEL
, /* val */
125 TK_M_DEVLINK
, /* val */
127 TK_M_ENV
, /* val, attr */
129 TK_M_SUBSYSTEM
, /* val */
130 TK_M_DRIVER
, /* val */
131 TK_M_WAITFOR
, /* val */
132 TK_M_ATTR
, /* val, attr */
135 TK_M_KERNELS
, /* val */
136 TK_M_SUBSYSTEMS
, /* val */
137 TK_M_DRIVERS
, /* val */
138 TK_M_ATTRS
, /* val, attr */
142 TK_M_TEST
, /* val, mode_t */
143 TK_M_EVENT_TIMEOUT
, /* int */
144 TK_M_PROGRAM
, /* val */
145 TK_M_IMPORT_FILE
, /* val */
146 TK_M_IMPORT_PROG
, /* val */
147 TK_M_IMPORT_BUILTIN
, /* val */
148 TK_M_IMPORT_DB
, /* val */
149 TK_M_IMPORT_CMDLINE
, /* val */
150 TK_M_IMPORT_PARENT
, /* val */
151 TK_M_RESULT
, /* val */
154 TK_A_STRING_ESCAPE_NONE
,
155 TK_A_STRING_ESCAPE_REPLACE
,
157 TK_A_INOTIFY_WATCH
, /* int */
158 TK_A_DEVLINK_PRIO
, /* int */
159 TK_A_OWNER
, /* val */
160 TK_A_GROUP
, /* val */
162 TK_A_OWNER_ID
, /* uid_t */
163 TK_A_GROUP_ID
, /* gid_t */
164 TK_A_MODE_ID
, /* mode_t */
165 TK_A_STATIC_NODE
, /* val */
166 TK_A_ENV
, /* val, attr */
169 TK_A_DEVLINK
, /* val */
170 TK_A_ATTR
, /* val, attr */
171 TK_A_RUN_BUILTIN
, /* val, bool */
172 TK_A_RUN_PROGRAM
, /* val, bool */
173 TK_A_GOTO
, /* size_t */
178 /* we try to pack stuff in a way that we take only 12 bytes per token */
181 unsigned char type
; /* same in rule and key */
183 enum token_type type
:8;
185 bool has_static_node
:1;
186 unsigned int unused
:6;
187 unsigned short token_count
;
188 unsigned int label_off
;
189 unsigned short filename_off
;
190 unsigned short filename_line
;
193 enum token_type type
:8;
194 enum operation_type op
:8;
195 enum string_glob_type glob
:8;
196 enum string_subst_type subst
:4;
197 enum string_subst_type attrsubst
:4;
198 unsigned int value_off
;
200 unsigned int attr_off
;
202 unsigned int rule_goto
;
209 enum udev_builtin_cmd builtin_cmd
;
217 struct udev_rules
*rules
;
219 struct token token
[MAX_TK
];
220 unsigned int token_cur
;
224 static const char *operation_str(enum operation_type type
)
226 static const char *operation_strs
[] = {
227 [OP_UNSET
] = "UNSET",
228 [OP_MATCH
] = "match",
229 [OP_NOMATCH
] = "nomatch",
230 [OP_MATCH_MAX
] = "MATCH_MAX",
233 [OP_ASSIGN
] = "assign",
234 [OP_ASSIGN_FINAL
] = "assign-final",
237 return operation_strs
[type
];
240 static const char *string_glob_str(enum string_glob_type type
)
242 static const char *string_glob_strs
[] = {
243 [GL_UNSET
] = "UNSET",
244 [GL_PLAIN
] = "plain",
246 [GL_SPLIT
] = "split",
247 [GL_SPLIT_GLOB
] = "split-glob",
248 [GL_SOMETHING
] = "split-glob",
251 return string_glob_strs
[type
];
254 static const char *token_str(enum token_type type
)
256 static const char *token_strs
[] = {
257 [TK_UNSET
] = "UNSET",
260 [TK_M_ACTION
] = "M ACTION",
261 [TK_M_DEVPATH
] = "M DEVPATH",
262 [TK_M_KERNEL
] = "M KERNEL",
263 [TK_M_DEVLINK
] = "M DEVLINK",
264 [TK_M_NAME
] = "M NAME",
265 [TK_M_ENV
] = "M ENV",
266 [TK_M_TAG
] = "M TAG",
267 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
268 [TK_M_DRIVER
] = "M DRIVER",
269 [TK_M_WAITFOR
] = "M WAITFOR",
270 [TK_M_ATTR
] = "M ATTR",
272 [TK_M_PARENTS_MIN
] = "M PARENTS_MIN",
273 [TK_M_KERNELS
] = "M KERNELS",
274 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
275 [TK_M_DRIVERS
] = "M DRIVERS",
276 [TK_M_ATTRS
] = "M ATTRS",
277 [TK_M_TAGS
] = "M TAGS",
278 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
280 [TK_M_TEST
] = "M TEST",
281 [TK_M_EVENT_TIMEOUT
] = "M EVENT_TIMEOUT",
282 [TK_M_PROGRAM
] = "M PROGRAM",
283 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
284 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
285 [TK_M_IMPORT_BUILTIN
] = "M IMPORT_BUILTIN",
286 [TK_M_IMPORT_DB
] = "M IMPORT_DB",
287 [TK_M_IMPORT_CMDLINE
] = "M IMPORT_CMDLINE",
288 [TK_M_IMPORT_PARENT
] = "M IMPORT_PARENT",
289 [TK_M_RESULT
] = "M RESULT",
290 [TK_M_MAX
] = "M MAX",
292 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
293 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
294 [TK_A_DB_PERSIST
] = "A DB_PERSIST",
295 [TK_A_INOTIFY_WATCH
] = "A INOTIFY_WATCH",
296 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
297 [TK_A_OWNER
] = "A OWNER",
298 [TK_A_GROUP
] = "A GROUP",
299 [TK_A_MODE
] = "A MODE",
300 [TK_A_OWNER_ID
] = "A OWNER_ID",
301 [TK_A_GROUP_ID
] = "A GROUP_ID",
302 [TK_A_STATIC_NODE
] = "A STATIC_NODE",
303 [TK_A_MODE_ID
] = "A MODE_ID",
304 [TK_A_ENV
] = "A ENV",
305 [TK_A_TAG
] = "A ENV",
306 [TK_A_NAME
] = "A NAME",
307 [TK_A_DEVLINK
] = "A DEVLINK",
308 [TK_A_ATTR
] = "A ATTR",
309 [TK_A_RUN_BUILTIN
] = "A RUN_BUILTIN",
310 [TK_A_RUN_PROGRAM
] = "A RUN_PROGRAM",
311 [TK_A_GOTO
] = "A GOTO",
316 return token_strs
[type
];
319 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
321 enum token_type type
= token
->type
;
322 enum operation_type op
= token
->key
.op
;
323 enum string_glob_type glob
= token
->key
.glob
;
324 const char *value
= &rules
->buf
[token
->key
.value_off
];
325 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
330 const char *tks_ptr
= (char *)rules
->tokens
;
331 const char *tk_ptr
= (char *)token
;
332 unsigned int idx
= (tk_ptr
- tks_ptr
) / sizeof(struct token
);
334 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
335 &rules
->buf
[token
->rule
.filename_off
], token
->rule
.filename_line
,
336 idx
, token
->rule
.token_count
,
337 &rules
->buf
[token
->rule
.label_off
]);
349 case TK_M_SUBSYSTEMS
:
353 case TK_M_IMPORT_FILE
:
354 case TK_M_IMPORT_PROG
:
356 case TK_M_IMPORT_CMDLINE
:
357 case TK_M_IMPORT_PARENT
:
364 case TK_A_RUN_BUILTIN
:
365 case TK_A_RUN_PROGRAM
:
366 log_debug("%s %s '%s'(%s)\n",
367 token_str(type
), operation_str(op
), value
, string_glob_str(glob
));
369 case TK_M_IMPORT_BUILTIN
:
370 log_debug("%s %i '%s'\n", token_str(type
), token
->key
.builtin_cmd
, value
);
377 log_debug("%s %s '%s' '%s'(%s)\n",
378 token_str(type
), operation_str(op
), attr
, value
, string_glob_str(glob
));
382 log_debug("%s %s '%s'\n", token_str(type
), operation_str(op
), value
);
384 case TK_A_STRING_ESCAPE_NONE
:
385 case TK_A_STRING_ESCAPE_REPLACE
:
386 case TK_A_DB_PERSIST
:
387 log_debug("%s\n", token_str(type
));
390 log_debug("%s %s '%s'(%s) %#o\n",
391 token_str(type
), operation_str(op
), value
, string_glob_str(glob
), token
->key
.mode
);
393 case TK_A_INOTIFY_WATCH
:
394 log_debug("%s %u\n", token_str(type
), token
->key
.watch
);
396 case TK_A_DEVLINK_PRIO
:
397 log_debug("%s %u\n", token_str(type
), token
->key
.devlink_prio
);
400 log_debug("%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.uid
);
403 log_debug("%s %s %u\n", token_str(type
), operation_str(op
), token
->key
.gid
);
406 log_debug("%s %s %#o\n", token_str(type
), operation_str(op
), token
->key
.mode
);
408 case TK_A_STATIC_NODE
:
409 log_debug("%s '%s'\n", token_str(type
), value
);
411 case TK_M_EVENT_TIMEOUT
:
412 log_debug("%s %u\n", token_str(type
), token
->key
.event_timeout
);
415 log_debug("%s '%s' %u\n", token_str(type
), value
, token
->key
.rule_goto
);
418 log_debug("* %s\n", token_str(type
));
420 case TK_M_PARENTS_MIN
:
421 case TK_M_PARENTS_MAX
:
424 log_debug("unknown type %u\n", type
);
429 static void dump_rules(struct udev_rules
*rules
)
433 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
435 rules
->token_cur
* sizeof(struct token
),
438 for(i
= 0; i
< rules
->token_cur
; i
++)
439 dump_token(rules
, &rules
->tokens
[i
]);
442 static inline const char *operation_str(enum operation_type type
) { return NULL
; }
443 static inline const char *token_str(enum token_type type
) { return NULL
; }
444 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
445 static inline void dump_rules(struct udev_rules
*rules
) {}
448 static int add_new_string(struct udev_rules
*rules
, const char *str
, size_t bytes
)
452 /* grow buffer if needed */
453 if (rules
->buf_cur
+ bytes
+1 >= rules
->buf_max
) {
457 /* double the buffer size */
458 add
= rules
->buf_max
;
462 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
466 rules
->buf_max
+= add
;
468 off
= rules
->buf_cur
;
469 memcpy(&rules
->buf
[rules
->buf_cur
], str
, bytes
);
470 rules
->buf_cur
+= bytes
;
475 static int add_string(struct udev_rules
*rules
, const char *str
)
477 unsigned int node_idx
;
478 struct trie_node
*new_node
;
479 unsigned int new_node_idx
;
484 struct trie_node
*parent
;
486 /* walk trie, start from last character of str to find matching tails */
490 for (depth
= 0; depth
<= len
; depth
++) {
491 struct trie_node
*node
;
492 unsigned int child_idx
;
494 node
= &rules
->trie_nodes
[node_idx
];
495 off
= node
->value_off
+ node
->value_len
- len
;
497 /* match against current node */
498 if (depth
== len
|| (node
->value_len
>= len
&& memcmp(&rules
->buf
[off
], str
, len
) == 0))
501 /* lookup child node */
502 key
= str
[len
- 1 - depth
];
503 child_idx
= node
->child_idx
;
504 while (child_idx
> 0) {
505 struct trie_node
*child
;
507 child
= &rules
->trie_nodes
[child_idx
];
508 if (child
->key
== key
)
510 child_idx
= child
->next_child_idx
;
514 node_idx
= child_idx
;
517 /* string not found, add it */
518 off
= add_new_string(rules
, str
, len
+ 1);
520 /* grow trie nodes if needed */
521 if (rules
->trie_nodes_cur
>= rules
->trie_nodes_max
) {
522 struct trie_node
*nodes
;
525 /* double the buffer size */
526 add
= rules
->trie_nodes_max
;
530 nodes
= realloc(rules
->trie_nodes
, (rules
->trie_nodes_max
+ add
) * sizeof(struct trie_node
));
533 rules
->trie_nodes
= nodes
;
534 rules
->trie_nodes_max
+= add
;
538 new_node_idx
= rules
->trie_nodes_cur
;
539 rules
->trie_nodes_cur
++;
540 new_node
= &rules
->trie_nodes
[new_node_idx
];
541 memset(new_node
, 0x00, sizeof(struct trie_node
));
542 new_node
->value_off
= off
;
543 new_node
->value_len
= len
;
546 /* join the parent's child list */
547 parent
= &rules
->trie_nodes
[node_idx
];
548 if (parent
->child_idx
== 0) {
549 parent
->child_idx
= new_node_idx
;
551 struct trie_node
*last_child
;
553 last_child
= &rules
->trie_nodes
[parent
->last_child_idx
];
554 last_child
->next_child_idx
= new_node_idx
;
556 parent
->last_child_idx
= new_node_idx
;
560 static int add_token(struct udev_rules
*rules
, struct token
*token
)
562 /* grow buffer if needed */
563 if (rules
->token_cur
+1 >= rules
->token_max
) {
564 struct token
*tokens
;
567 /* double the buffer size */
568 add
= rules
->token_max
;
572 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
575 rules
->tokens
= tokens
;
576 rules
->token_max
+= add
;
578 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
583 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
589 /* lookup, if we know it already */
590 for (i
= 0; i
< rules
->uids_cur
; i
++) {
591 off
= rules
->uids
[i
].name_off
;
592 if (strcmp(&rules
->buf
[off
], owner
) == 0) {
593 uid
= rules
->uids
[i
].uid
;
597 uid
= util_lookup_user(rules
->udev
, owner
);
599 /* grow buffer if needed */
600 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
601 struct uid_gid
*uids
;
604 /* double the buffer size */
605 add
= rules
->uids_max
;
609 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
613 rules
->uids_max
+= add
;
615 rules
->uids
[rules
->uids_cur
].uid
= uid
;
616 off
= add_string(rules
, owner
);
619 rules
->uids
[rules
->uids_cur
].name_off
= off
;
624 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
630 /* lookup, if we know it already */
631 for (i
= 0; i
< rules
->gids_cur
; i
++) {
632 off
= rules
->gids
[i
].name_off
;
633 if (strcmp(&rules
->buf
[off
], group
) == 0) {
634 gid
= rules
->gids
[i
].gid
;
638 gid
= util_lookup_group(rules
->udev
, group
);
640 /* grow buffer if needed */
641 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
642 struct uid_gid
*gids
;
645 /* double the buffer size */
646 add
= rules
->gids_max
;
650 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
654 rules
->gids_max
+= add
;
656 rules
->gids
[rules
->gids_cur
].gid
= gid
;
657 off
= add_string(rules
, group
);
660 rules
->gids
[rules
->gids_cur
].name_off
= off
;
665 static int import_property_from_string(struct udev_device
*dev
, char *line
)
667 struct udev
*udev
= udev_device_get_udev(dev
);
674 while (isspace(key
[0]))
677 /* comment or empty line */
678 if (key
[0] == '#' || key
[0] == '\0')
681 /* split key/value */
682 val
= strchr(key
, '=');
689 while (isspace(val
[0]))
696 while (isspace(key
[len
-1]))
700 /* terminate value */
704 while (isspace(val
[len
-1]))
712 if (val
[0] == '"' || val
[0] == '\'') {
713 if (val
[len
-1] != val
[0]) {
714 log_debug("inconsistent quoting: '%s', skip\n", line
);
721 /* handle device, renamed by external tool, returning new path */
722 if (strcmp(key
, "DEVPATH") == 0) {
723 char syspath
[UTIL_PATH_SIZE
];
725 log_debug("updating devpath from '%s' to '%s'\n",
726 udev_device_get_devpath(dev
), val
);
727 util_strscpyl(syspath
, sizeof(syspath
), udev_get_sys_path(udev
), val
, NULL
);
728 udev_device_set_syspath(dev
, syspath
);
730 struct udev_list_entry
*entry
;
732 entry
= udev_device_add_property(dev
, key
, val
);
733 /* store in db, skip private keys */
735 udev_list_entry_set_num(entry
, true);
740 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
743 char line
[UTIL_LINE_SIZE
];
745 f
= fopen(filename
, "r");
748 while (fgets(line
, sizeof(line
), f
) != NULL
)
749 import_property_from_string(dev
, line
);
754 static int import_program_into_properties(struct udev_event
*event
, const char *program
, const sigset_t
*sigmask
)
756 struct udev_device
*dev
= event
->dev
;
758 char result
[UTIL_LINE_SIZE
];
762 envp
= udev_device_get_properties_envp(dev
);
763 err
= udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
));
768 while (line
!= NULL
) {
771 pos
= strchr(line
, '\n');
776 import_property_from_string(dev
, line
);
782 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
784 struct udev_device
*dev_parent
;
785 struct udev_list_entry
*list_entry
;
787 dev_parent
= udev_device_get_parent(dev
);
788 if (dev_parent
== NULL
)
791 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
792 const char *key
= udev_list_entry_get_name(list_entry
);
793 const char *val
= udev_list_entry_get_value(list_entry
);
795 if (fnmatch(filter
, key
, 0) == 0) {
796 struct udev_list_entry
*entry
;
798 entry
= udev_device_add_property(dev
, key
, val
);
799 /* store in db, skip private keys */
801 udev_list_entry_set_num(entry
, true);
807 #define WAIT_LOOP_PER_SECOND 50
808 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
810 struct udev
*udev
= udev_device_get_udev(dev
);
811 char filepath
[UTIL_PATH_SIZE
];
812 char devicepath
[UTIL_PATH_SIZE
];
814 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
816 /* a relative path is a device attribute */
817 devicepath
[0] = '\0';
818 if (file
[0] != '/') {
819 util_strscpyl(devicepath
, sizeof(devicepath
),
820 udev_get_sys_path(udev
), udev_device_get_devpath(dev
), NULL
);
821 util_strscpyl(filepath
, sizeof(filepath
), devicepath
, "/", file
, NULL
);
826 const struct timespec duration
= { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND
};
829 if (stat(file
, &stats
) == 0) {
830 log_debug("file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
833 /* make sure, the device did not disappear in the meantime */
834 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
835 log_debug("device disappeared while waiting for '%s'\n", file
);
838 log_debug("wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
839 nanosleep(&duration
, NULL
);
841 log_debug("waiting for '%s' failed\n", file
);
845 static int attr_subst_subdir(char *attr
, size_t len
)
849 if (strstr(attr
, "/*/")) {
851 char dirname
[UTIL_PATH_SIZE
];
855 util_strscpy(dirname
, sizeof(dirname
), attr
);
856 pos
= strstr(dirname
, "/*/");
861 dir
= opendir(dirname
);
865 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
868 if (dent
->d_name
[0] == '.')
870 util_strscpyl(attr
, len
, dirname
, "/", dent
->d_name
, tail
, NULL
);
871 if (stat(attr
, &stats
) == 0) {
883 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
889 if (linepos
== NULL
|| linepos
[0] == '\0')
892 /* skip whitespace */
893 while (isspace(linepos
[0]) || linepos
[0] == ',')
897 if (linepos
[0] == '\0')
903 if (linepos
[0] == '\0')
905 if (isspace(linepos
[0]))
907 if (linepos
[0] == '=')
909 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
910 if (linepos
[1] == '=')
914 /* remember end of key */
917 /* skip whitespace after key */
918 while (isspace(linepos
[0]))
920 if (linepos
[0] == '\0')
923 /* get operation type */
924 if (linepos
[0] == '=' && linepos
[1] == '=') {
927 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
930 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
933 } else if (linepos
[0] == '=') {
936 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
937 *op
= OP_ASSIGN_FINAL
;
945 /* skip whitespace after operator */
946 while (isspace(linepos
[0]))
948 if (linepos
[0] == '\0')
952 if (linepos
[0] == '"')
959 temp
= strchr(linepos
, '"');
965 /* move line to next key */
970 /* extract possible KEY{attr} */
971 static const char *get_key_attribute(struct udev
*udev
, char *str
)
976 attr
= strchr(str
, '{');
979 pos
= strchr(attr
, '}');
981 log_error("missing closing brace for format\n");
990 static int rule_add_key(struct rule_tmp
*rule_tmp
, enum token_type type
,
991 enum operation_type op
,
992 const char *value
, const void *data
)
994 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
995 const char *attr
= NULL
;
997 memset(token
, 0x00, sizeof(struct token
));
1003 case TK_M_SUBSYSTEM
:
1009 case TK_M_SUBSYSTEMS
:
1013 case TK_M_IMPORT_FILE
:
1014 case TK_M_IMPORT_PROG
:
1015 case TK_M_IMPORT_DB
:
1016 case TK_M_IMPORT_CMDLINE
:
1017 case TK_M_IMPORT_PARENT
:
1026 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1028 case TK_M_IMPORT_BUILTIN
:
1029 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1030 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
1038 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1039 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
1042 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1043 token
->key
.devlink_unique
= *(int *)data
;
1046 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1048 token
->key
.mode
= *(mode_t
*)data
;
1050 case TK_A_STRING_ESCAPE_NONE
:
1051 case TK_A_STRING_ESCAPE_REPLACE
:
1052 case TK_A_DB_PERSIST
:
1054 case TK_A_RUN_BUILTIN
:
1055 case TK_A_RUN_PROGRAM
:
1056 token
->key
.builtin_cmd
= *(enum udev_builtin_cmd
*)data
;
1057 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1059 case TK_A_INOTIFY_WATCH
:
1060 case TK_A_DEVLINK_PRIO
:
1061 token
->key
.devlink_prio
= *(int *)data
;
1064 token
->key
.uid
= *(uid_t
*)data
;
1067 token
->key
.gid
= *(gid_t
*)data
;
1070 token
->key
.mode
= *(mode_t
*)data
;
1072 case TK_A_STATIC_NODE
:
1073 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
1075 case TK_M_EVENT_TIMEOUT
:
1076 token
->key
.event_timeout
= *(int *)data
;
1079 case TK_M_PARENTS_MIN
:
1080 case TK_M_PARENTS_MAX
:
1084 log_error("wrong type %u\n", type
);
1088 if (value
!= NULL
&& type
< TK_M_MAX
) {
1089 /* check if we need to split or call fnmatch() while matching rules */
1090 enum string_glob_type glob
;
1094 has_split
= (strchr(value
, '|') != NULL
);
1095 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
|| strchr(value
, '[') != NULL
);
1096 if (has_split
&& has_glob
) {
1097 glob
= GL_SPLIT_GLOB
;
1098 } else if (has_split
) {
1100 } else if (has_glob
) {
1101 if (strcmp(value
, "?*") == 0)
1102 glob
= GL_SOMETHING
;
1108 token
->key
.glob
= glob
;
1111 if (value
!= NULL
&& type
> TK_M_MAX
) {
1112 /* check if assigned value has substitution chars */
1113 if (value
[0] == '[')
1114 token
->key
.subst
= SB_SUBSYS
;
1115 else if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
1116 token
->key
.subst
= SB_FORMAT
;
1118 token
->key
.subst
= SB_NONE
;
1122 /* check if property/attribut name has substitution chars */
1124 token
->key
.attrsubst
= SB_SUBSYS
;
1125 else if (strchr(attr
, '%') != NULL
|| strchr(attr
, '$') != NULL
)
1126 token
->key
.attrsubst
= SB_FORMAT
;
1128 token
->key
.attrsubst
= SB_NONE
;
1131 token
->key
.type
= type
;
1133 rule_tmp
->token_cur
++;
1134 if (rule_tmp
->token_cur
>= ARRAY_SIZE(rule_tmp
->token
)) {
1135 log_error("temporary rule array too small\n");
1141 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
1144 unsigned int start
= 0;
1145 unsigned int end
= rule_tmp
->token_cur
;
1147 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
1148 enum token_type next_val
= TK_UNSET
;
1149 unsigned int next_idx
= 0;
1152 /* find smallest value */
1153 for (j
= start
; j
< end
; j
++) {
1154 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
1156 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
1157 next_val
= rule_tmp
->token
[j
].type
;
1162 /* add token and mark done */
1163 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
1165 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
1168 if (next_idx
== start
)
1170 if (next_idx
+1 == end
)
1176 static int add_rule(struct udev_rules
*rules
, char *line
,
1177 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1181 struct rule_tmp rule_tmp
;
1183 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1184 rule_tmp
.rules
= rules
;
1185 rule_tmp
.rule
.type
= TK_RULE
;
1186 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1187 rule_tmp
.rule
.rule
.filename_line
= lineno
;
1193 enum operation_type op
;
1195 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1198 if (strcmp(key
, "ACTION") == 0) {
1199 if (op
> OP_MATCH_MAX
) {
1200 log_error("invalid ACTION operation\n");
1203 rule_add_key(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1207 if (strcmp(key
, "DEVPATH") == 0) {
1208 if (op
> OP_MATCH_MAX
) {
1209 log_error("invalid DEVPATH operation\n");
1212 rule_add_key(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1216 if (strcmp(key
, "KERNEL") == 0) {
1217 if (op
> OP_MATCH_MAX
) {
1218 log_error("invalid KERNEL operation\n");
1221 rule_add_key(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1225 if (strcmp(key
, "SUBSYSTEM") == 0) {
1226 if (op
> OP_MATCH_MAX
) {
1227 log_error("invalid SUBSYSTEM operation\n");
1230 /* bus, class, subsystem events should all be the same */
1231 if (strcmp(value
, "subsystem") == 0 ||
1232 strcmp(value
, "bus") == 0 ||
1233 strcmp(value
, "class") == 0) {
1234 if (strcmp(value
, "bus") == 0 || strcmp(value
, "class") == 0)
1235 log_error("'%s' must be specified as 'subsystem' \n"
1236 "please fix it in %s:%u", value
, filename
, lineno
);
1237 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1239 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1243 if (strcmp(key
, "DRIVER") == 0) {
1244 if (op
> OP_MATCH_MAX
) {
1245 log_error("invalid DRIVER operation\n");
1248 rule_add_key(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1252 if (strncmp(key
, "ATTR{", sizeof("ATTR{")-1) == 0) {
1253 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1255 log_error("error parsing ATTR attribute\n");
1258 if (op
< OP_MATCH_MAX
) {
1259 rule_add_key(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1261 rule_add_key(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1266 if (strcmp(key
, "KERNELS") == 0) {
1267 if (op
> OP_MATCH_MAX
) {
1268 log_error("invalid KERNELS operation\n");
1271 rule_add_key(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1275 if (strcmp(key
, "SUBSYSTEMS") == 0) {
1276 if (op
> OP_MATCH_MAX
) {
1277 log_error("invalid SUBSYSTEMS operation\n");
1280 rule_add_key(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1284 if (strcmp(key
, "DRIVERS") == 0) {
1285 if (op
> OP_MATCH_MAX
) {
1286 log_error("invalid DRIVERS operation\n");
1289 rule_add_key(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1293 if (strncmp(key
, "ATTRS{", sizeof("ATTRS{")-1) == 0) {
1294 if (op
> OP_MATCH_MAX
) {
1295 log_error("invalid ATTRS operation\n");
1298 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1300 log_error("error parsing ATTRS attribute\n");
1303 if (strncmp(attr
, "device/", 7) == 0)
1304 log_error("the 'device' link may not be available in a future kernel, "
1305 "please fix it in %s:%u", filename
, lineno
);
1306 else if (strstr(attr
, "../") != NULL
)
1307 log_error("do not reference parent sysfs directories directly, "
1308 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1309 rule_add_key(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1313 if (strcmp(key
, "TAGS") == 0) {
1314 if (op
> OP_MATCH_MAX
) {
1315 log_error("invalid TAGS operation\n");
1318 rule_add_key(&rule_tmp
, TK_M_TAGS
, op
, value
, NULL
);
1322 if (strncmp(key
, "ENV{", sizeof("ENV{")-1) == 0) {
1323 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1325 log_error("error parsing ENV attribute\n");
1328 if (op
< OP_MATCH_MAX
) {
1329 if (rule_add_key(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1332 static const char *blacklist
[] = {
1347 for (i
= 0; i
< ARRAY_SIZE(blacklist
); i
++)
1348 if (strcmp(attr
, blacklist
[i
]) == 0) {
1349 log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr
, filename
, lineno
);
1352 if (rule_add_key(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1358 if (strcmp(key
, "TAG") == 0) {
1359 if (op
< OP_MATCH_MAX
)
1360 rule_add_key(&rule_tmp
, TK_M_TAG
, op
, value
, NULL
);
1362 rule_add_key(&rule_tmp
, TK_A_TAG
, op
, value
, NULL
);
1366 if (strcmp(key
, "PROGRAM") == 0) {
1367 rule_add_key(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1371 if (strcmp(key
, "RESULT") == 0) {
1372 if (op
> OP_MATCH_MAX
) {
1373 log_error("invalid RESULT operation\n");
1376 rule_add_key(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1380 if (strncmp(key
, "IMPORT", sizeof("IMPORT")-1) == 0) {
1381 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1383 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename
, lineno
);
1386 if (strcmp(attr
, "program") == 0) {
1387 /* find known built-in command */
1388 if (value
[0] != '/') {
1389 enum udev_builtin_cmd cmd
;
1391 cmd
= udev_builtin_lookup(value
);
1392 if (cmd
< UDEV_BUILTIN_MAX
) {
1393 log_debug("IMPORT found builtin '%s', replacing %s:%u\n",
1394 value
, filename
, lineno
);
1395 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1399 rule_add_key(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1400 } else if (strcmp(attr
, "builtin") == 0) {
1401 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1403 if (cmd
< UDEV_BUILTIN_MAX
)
1404 rule_add_key(&rule_tmp
, TK_M_IMPORT_BUILTIN
, op
, value
, &cmd
);
1406 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value
, filename
, lineno
);
1407 } else if (strcmp(attr
, "file") == 0) {
1408 rule_add_key(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1409 } else if (strcmp(attr
, "db") == 0) {
1410 rule_add_key(&rule_tmp
, TK_M_IMPORT_DB
, op
, value
, NULL
);
1411 } else if (strcmp(attr
, "cmdline") == 0) {
1412 rule_add_key(&rule_tmp
, TK_M_IMPORT_CMDLINE
, op
, value
, NULL
);
1413 } else if (strcmp(attr
, "parent") == 0) {
1414 rule_add_key(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1416 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u\n", filename
, lineno
);
1420 if (strncmp(key
, "TEST", sizeof("TEST")-1) == 0) {
1423 if (op
> OP_MATCH_MAX
) {
1424 log_error("invalid TEST operation\n");
1427 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1429 mode
= strtol(attr
, NULL
, 8);
1430 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1432 rule_add_key(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1437 if (strncmp(key
, "RUN", sizeof("RUN")-1) == 0) {
1438 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1442 if (strcmp(attr
, "builtin") == 0) {
1443 enum udev_builtin_cmd cmd
= udev_builtin_lookup(value
);
1445 if (cmd
< UDEV_BUILTIN_MAX
)
1446 rule_add_key(&rule_tmp
, TK_A_RUN_BUILTIN
, op
, value
, &cmd
);
1448 log_error("IMPORT{builtin}: '%s' unknown %s:%u\n", value
, filename
, lineno
);
1449 } else if (strcmp(attr
, "program") == 0) {
1450 enum udev_builtin_cmd cmd
= UDEV_BUILTIN_MAX
;
1452 rule_add_key(&rule_tmp
, TK_A_RUN_PROGRAM
, op
, value
, &cmd
);
1454 log_error("RUN{} unknown type, ignoring RUN %s:%u\n", filename
, lineno
);
1460 if (strcmp(key
, "WAIT_FOR") == 0 || strcmp(key
, "WAIT_FOR_SYSFS") == 0) {
1461 rule_add_key(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1465 if (strcmp(key
, "LABEL") == 0) {
1466 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1470 if (strcmp(key
, "GOTO") == 0) {
1471 rule_add_key(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1475 if (strncmp(key
, "NAME", sizeof("NAME")-1) == 0) {
1476 if (op
< OP_MATCH_MAX
) {
1477 rule_add_key(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1479 if (strcmp(value
, "%k") == 0) {
1480 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
1481 "please remove it from %s:%u\n", filename
, lineno
);
1484 if (value
[0] == '\0') {
1485 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1486 "please remove it from %s:%u\n", filename
, lineno
);
1489 rule_add_key(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1491 rule_tmp
.rule
.rule
.can_set_name
= true;
1495 if (strncmp(key
, "SYMLINK", sizeof("SYMLINK")-1) == 0) {
1496 if (op
< OP_MATCH_MAX
) {
1497 rule_add_key(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1501 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("SYMLINK")-1);
1502 if (attr
!= NULL
&& strstr(attr
, "unique") != NULL
)
1504 rule_add_key(&rule_tmp
, TK_A_DEVLINK
, op
, value
, &flag
);
1506 rule_tmp
.rule
.rule
.can_set_name
= true;
1510 if (strcmp(key
, "OWNER") == 0) {
1514 uid
= strtoul(value
, &endptr
, 10);
1515 if (endptr
[0] == '\0') {
1516 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1517 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1518 uid
= add_uid(rules
, value
);
1519 rule_add_key(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1520 } else if (rules
->resolve_names
>= 0) {
1521 rule_add_key(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1523 rule_tmp
.rule
.rule
.can_set_name
= true;
1527 if (strcmp(key
, "GROUP") == 0) {
1531 gid
= strtoul(value
, &endptr
, 10);
1532 if (endptr
[0] == '\0') {
1533 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1534 } else if ((rules
->resolve_names
> 0) && strchr("$%", value
[0]) == NULL
) {
1535 gid
= add_gid(rules
, value
);
1536 rule_add_key(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1537 } else if (rules
->resolve_names
>= 0) {
1538 rule_add_key(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1540 rule_tmp
.rule
.rule
.can_set_name
= true;
1544 if (strcmp(key
, "MODE") == 0) {
1548 mode
= strtol(value
, &endptr
, 8);
1549 if (endptr
[0] == '\0')
1550 rule_add_key(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1552 rule_add_key(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1553 rule_tmp
.rule
.rule
.can_set_name
= true;
1557 if (strcmp(key
, "OPTIONS") == 0) {
1560 pos
= strstr(value
, "link_priority=");
1562 int prio
= atoi(&pos
[strlen("link_priority=")]);
1564 rule_add_key(&rule_tmp
, TK_A_DEVLINK_PRIO
, op
, NULL
, &prio
);
1567 pos
= strstr(value
, "event_timeout=");
1569 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1571 rule_add_key(&rule_tmp
, TK_M_EVENT_TIMEOUT
, op
, NULL
, &tout
);
1574 pos
= strstr(value
, "string_escape=");
1576 pos
= &pos
[strlen("string_escape=")];
1577 if (strncmp(pos
, "none", strlen("none")) == 0)
1578 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, op
, NULL
, NULL
);
1579 else if (strncmp(pos
, "replace", strlen("replace")) == 0)
1580 rule_add_key(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, op
, NULL
, NULL
);
1583 pos
= strstr(value
, "db_persist");
1585 rule_add_key(&rule_tmp
, TK_A_DB_PERSIST
, op
, NULL
, NULL
);
1587 pos
= strstr(value
, "nowatch");
1591 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &off
);
1593 pos
= strstr(value
, "watch");
1597 rule_add_key(&rule_tmp
, TK_A_INOTIFY_WATCH
, op
, NULL
, &on
);
1601 pos
= strstr(value
, "static_node=");
1603 rule_add_key(&rule_tmp
, TK_A_STATIC_NODE
, op
, &pos
[strlen("static_node=")], NULL
);
1604 rule_tmp
.rule
.rule
.has_static_node
= true;
1610 log_error("unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1614 /* add rule token */
1615 rule_tmp
.rule
.rule
.token_count
= 1 + rule_tmp
.token_cur
;
1616 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1619 /* add tokens to list, sorted by type */
1620 if (sort_token(rules
, &rule_tmp
) != 0)
1625 log_error("invalid rule '%s:%u'\n", filename
, lineno
);
1629 static int parse_file(struct udev_rules
*rules
, const char *filename
, unsigned short filename_off
)
1632 unsigned int first_token
;
1633 char line
[UTIL_LINE_SIZE
];
1637 log_debug("reading '%s' as rules file\n", filename
);
1639 f
= fopen(filename
, "r");
1643 first_token
= rules
->token_cur
;
1645 while (fgets(line
, sizeof(line
), f
) != NULL
) {
1649 /* skip whitespace */
1652 while (isspace(key
[0]))
1663 /* continue reading if backslash+newline is found */
1664 while (line
[len
-2] == '\\') {
1665 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1667 if (strlen(&line
[len
-2]) < 2)
1673 if (len
+1 >= sizeof(line
)) {
1674 log_error("line too long '%s':%u, ignored\n", filename
, line_nr
);
1677 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1681 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1682 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1683 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1684 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1687 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1688 if (rules
->tokens
[j
].type
!= TK_RULE
)
1690 if (rules
->tokens
[j
].rule
.label_off
== 0)
1692 if (strcmp(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]) != 0)
1694 rules
->tokens
[i
].key
.rule_goto
= j
;
1697 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1698 log_error("GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1704 static int add_matching_files(struct udev
*udev
, struct udev_list
*file_list
, const char *dirname
, const char *suffix
)
1707 struct dirent
*dent
;
1708 char filename
[UTIL_PATH_SIZE
];
1710 dir
= opendir(dirname
);
1712 log_debug("unable to open '%s': %m\n", dirname
);
1716 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
1717 if (dent
->d_name
[0] == '.')
1720 /* look for file matching with specified suffix */
1721 if (suffix
!= NULL
) {
1724 ext
= strrchr(dent
->d_name
, '.');
1727 if (strcmp(ext
, suffix
) != 0)
1730 util_strscpyl(filename
, sizeof(filename
), dirname
, "/", dent
->d_name
, NULL
);
1732 * the basename is the key, the filename the value
1733 * identical basenames from different directories override each other
1734 * entries are sorted after basename
1736 udev_list_entry_add(file_list
, dent
->d_name
, filename
);
1743 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1745 struct udev_rules
*rules
;
1746 struct udev_list file_list
;
1747 struct udev_list_entry
*file_loop
;
1748 struct token end_token
;
1751 rules
= calloc(1, sizeof(struct udev_rules
));
1755 rules
->resolve_names
= resolve_names
;
1756 udev_list_init(udev
, &file_list
, true);
1758 /* init token array and string buffer */
1759 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1760 if (rules
->tokens
== NULL
) {
1764 rules
->token_max
= PREALLOC_TOKEN
;
1766 rules
->buf
= malloc(PREALLOC_STRBUF
);
1767 if (rules
->buf
== NULL
) {
1768 free(rules
->tokens
);
1772 rules
->buf_max
= PREALLOC_STRBUF
;
1773 /* offset 0 is always '\0' */
1774 rules
->buf
[0] = '\0';
1777 rules
->trie_nodes
= malloc(PREALLOC_TRIE
* sizeof(struct trie_node
));
1778 if (rules
->trie_nodes
== NULL
) {
1780 free(rules
->tokens
);
1784 rules
->trie_nodes_max
= PREALLOC_TRIE
;
1785 /* offset 0 is the trie root, with an empty string */
1786 memset(rules
->trie_nodes
, 0x00, sizeof(struct trie_node
));
1787 rules
->trie_nodes_cur
= 1;
1789 for (udev_get_rules_path(udev
, &s
, NULL
); *s
!= NULL
; s
++)
1790 add_matching_files(udev
, &file_list
, *s
, ".rules");
1792 /* add all filenames to the string buffer */
1793 udev_list_entry_foreach(file_loop
, udev_list_get_entry(&file_list
)) {
1794 const char *filename
= udev_list_entry_get_value(file_loop
);
1795 unsigned int filename_off
;
1797 filename_off
= add_string(rules
, filename
);
1798 /* the offset in the rule is limited to unsigned short */
1799 if (filename_off
< USHRT_MAX
)
1800 udev_list_entry_set_num(file_loop
, filename_off
);
1803 /* parse all rules files */
1804 udev_list_entry_foreach(file_loop
, udev_list_get_entry(&file_list
)) {
1805 const char *filename
= udev_list_entry_get_value(file_loop
);
1806 unsigned int filename_off
= udev_list_entry_get_num(file_loop
);
1809 if (stat(filename
, &st
) != 0) {
1810 log_error("can not find '%s': %m\n", filename
);
1813 if (S_ISREG(st
.st_mode
) && st
.st_size
<= 0) {
1814 log_debug("ignore empty '%s'\n", filename
);
1817 if (S_ISCHR(st
.st_mode
)) {
1818 log_debug("ignore masked '%s'\n", filename
);
1821 parse_file(rules
, filename
, filename_off
);
1823 udev_list_cleanup(&file_list
);
1825 memset(&end_token
, 0x00, sizeof(struct token
));
1826 end_token
.type
= TK_END
;
1827 add_token(rules
, &end_token
);
1829 /* shrink allocated token and string buffer */
1830 if (rules
->token_cur
< rules
->token_max
) {
1831 struct token
*tokens
;
1833 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1834 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1835 rules
->tokens
= tokens
;
1836 rules
->token_max
= rules
->token_cur
;
1839 if (rules
->buf_cur
< rules
->buf_max
) {
1842 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1843 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1845 rules
->buf_max
= rules
->buf_cur
;
1848 log_debug("rules use %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1849 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1850 log_debug("temporary index used %zu bytes (%u * %zu bytes)\n",
1851 rules
->trie_nodes_cur
* sizeof(struct trie_node
),
1852 rules
->trie_nodes_cur
, sizeof(struct trie_node
));
1855 free(rules
->trie_nodes
);
1856 rules
->trie_nodes
= NULL
;
1857 rules
->trie_nodes_cur
= 0;
1858 rules
->trie_nodes_max
= 0;
1860 /* cleanup uid/gid cache */
1863 rules
->uids_cur
= 0;
1864 rules
->uids_max
= 0;
1867 rules
->gids_cur
= 0;
1868 rules
->gids_max
= 0;
1874 struct udev_rules
*udev_rules_unref(struct udev_rules
*rules
)
1878 free(rules
->tokens
);
1880 free(rules
->trie_nodes
);
1887 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1889 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1896 switch (token
->key
.glob
) {
1898 match
= (strcmp(key_value
, val
) == 0);
1901 match
= (fnmatch(key_value
, val
, 0) == 0);
1908 split
= &rules
->buf
[token
->key
.value_off
];
1913 next
= strchr(split
, '|');
1915 size_t matchlen
= (size_t)(next
- split
);
1917 match
= (matchlen
== len
&& strncmp(split
, val
, matchlen
) == 0);
1921 match
= (strcmp(split
, val
) == 0);
1930 char value
[UTIL_PATH_SIZE
];
1932 util_strscpy(value
, sizeof(value
), &rules
->buf
[token
->key
.value_off
]);
1934 while (key_value
!= NULL
) {
1935 pos
= strchr(key_value
, '|');
1940 match
= (fnmatch(key_value
, val
, 0) == 0);
1948 match
= (val
[0] != '\0');
1954 if (match
&& (token
->key
.op
== OP_MATCH
))
1956 if (!match
&& (token
->key
.op
== OP_NOMATCH
))
1961 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1964 char nbuf
[UTIL_NAME_SIZE
];
1966 char vbuf
[UTIL_NAME_SIZE
];
1969 name
= &rules
->buf
[cur
->key
.attr_off
];
1970 switch (cur
->key
.attrsubst
) {
1972 udev_event_apply_format(event
, name
, nbuf
, sizeof(nbuf
));
1976 value
= udev_device_get_sysattr_value(dev
, name
);
1981 if (util_resolve_subsys_kernel(event
->udev
, name
, vbuf
, sizeof(vbuf
), 1) != 0)
1989 /* remove trailing whitespace, if not asked to match for it */
1990 len
= strlen(value
);
1991 if (len
> 0 && isspace(value
[len
-1])) {
1992 const char *key_value
;
1995 key_value
= &rules
->buf
[cur
->key
.value_off
];
1996 klen
= strlen(key_value
);
1997 if (klen
> 0 && !isspace(key_value
[klen
-1])) {
1998 if (value
!= vbuf
) {
1999 util_strscpy(vbuf
, sizeof(vbuf
), value
);
2002 while (len
> 0 && isspace(vbuf
[--len
]))
2007 return match_key(rules
, cur
, value
);
2016 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
, const sigset_t
*sigmask
)
2020 enum escape_type esc
= ESCAPE_UNSET
;
2023 if (rules
->tokens
== NULL
)
2026 can_set_name
= ((strcmp(udev_device_get_action(event
->dev
), "remove") != 0) &&
2027 (major(udev_device_get_devnum(event
->dev
)) > 0 ||
2028 udev_device_get_ifindex(event
->dev
) > 0));
2030 /* loop through token list, match, run actions or forward to next rule */
2031 cur
= &rules
->tokens
[0];
2034 dump_token(rules
, cur
);
2035 switch (cur
->type
) {
2039 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
2040 if (!can_set_name
&& rule
->rule
.can_set_name
)
2045 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
2049 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
2053 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
2056 case TK_M_DEVLINK
: {
2057 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
2058 struct udev_list_entry
*list_entry
;
2061 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
2062 const char *devlink
;
2064 devlink
= &udev_list_entry_get_name(list_entry
)[devlen
];
2065 if (match_key(rules
, cur
, devlink
) == 0) {
2075 if (match_key(rules
, cur
, event
->name
) != 0)
2079 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2082 value
= udev_device_get_property_value(event
->dev
, key_name
);
2085 if (match_key(rules
, cur
, value
))
2090 struct udev_list_entry
*list_entry
;
2093 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(event
->dev
)) {
2094 if (strcmp(&rules
->buf
[cur
->key
.value_off
], udev_list_entry_get_name(list_entry
)) == 0) {
2099 if (!match
&& (cur
->key
.op
!= OP_NOMATCH
))
2103 case TK_M_SUBSYSTEM
:
2104 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
2108 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
2111 case TK_M_WAITFOR
: {
2112 char filename
[UTIL_PATH_SIZE
];
2115 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2116 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
2117 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
2122 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
2126 case TK_M_SUBSYSTEMS
:
2132 /* get whole sequence of parent matches */
2134 while (next
->type
> TK_M_PARENTS_MIN
&& next
->type
< TK_M_PARENTS_MAX
)
2137 /* loop over parents */
2138 event
->dev_parent
= event
->dev
;
2142 /* loop over sequence of parent match keys */
2143 for (key
= cur
; key
< next
; key
++ ) {
2144 dump_token(rules
, key
);
2147 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
2150 case TK_M_SUBSYSTEMS
:
2151 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
2155 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
2159 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
2163 bool match
= udev_device_has_tag(event
->dev_parent
, &rules
->buf
[cur
->key
.value_off
]);
2165 if (match
&& key
->key
.op
== OP_NOMATCH
)
2167 if (!match
&& key
->key
.op
== OP_MATCH
)
2178 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
2179 if (event
->dev_parent
== NULL
)
2182 /* move behind our sequence of parent match keys */
2187 char filename
[UTIL_PATH_SIZE
];
2188 struct stat statbuf
;
2191 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], filename
, sizeof(filename
));
2192 if (util_resolve_subsys_kernel(event
->udev
, filename
, filename
, sizeof(filename
), 0) != 0) {
2193 if (filename
[0] != '/') {
2194 char tmp
[UTIL_PATH_SIZE
];
2196 util_strscpy(tmp
, sizeof(tmp
), filename
);
2197 util_strscpyl(filename
, sizeof(filename
),
2198 udev_device_get_syspath(event
->dev
), "/", tmp
, NULL
);
2201 attr_subst_subdir(filename
, sizeof(filename
));
2203 match
= (stat(filename
, &statbuf
) == 0);
2204 if (match
&& cur
->key
.mode
> 0)
2205 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2206 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2208 if (!match
&& cur
->key
.op
== OP_MATCH
)
2212 case TK_M_EVENT_TIMEOUT
:
2213 log_debug("OPTIONS event_timeout=%u\n", cur
->key
.event_timeout
);
2214 event
->timeout_usec
= cur
->key
.event_timeout
* 1000 * 1000;
2216 case TK_M_PROGRAM
: {
2217 char program
[UTIL_PATH_SIZE
];
2219 char result
[UTIL_PATH_SIZE
];
2221 free(event
->program_result
);
2222 event
->program_result
= NULL
;
2223 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], program
, sizeof(program
));
2224 envp
= udev_device_get_properties_envp(event
->dev
);
2225 log_debug("PROGRAM '%s' %s:%u\n",
2227 &rules
->buf
[rule
->rule
.filename_off
],
2228 rule
->rule
.filename_line
);
2230 if (udev_event_spawn(event
, program
, envp
, sigmask
, result
, sizeof(result
)) < 0) {
2231 if (cur
->key
.op
!= OP_NOMATCH
)
2236 util_remove_trailing_chars(result
, '\n');
2237 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2238 count
= util_replace_chars(result
, UDEV_ALLOWED_CHARS_INPUT
);
2240 log_debug("%i character(s) replaced\n" , count
);
2242 event
->program_result
= strdup(result
);
2243 if (cur
->key
.op
== OP_NOMATCH
)
2248 case TK_M_IMPORT_FILE
: {
2249 char import
[UTIL_PATH_SIZE
];
2251 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2252 if (import_file_into_properties(event
->dev
, import
) != 0)
2253 if (cur
->key
.op
!= OP_NOMATCH
)
2257 case TK_M_IMPORT_PROG
: {
2258 char import
[UTIL_PATH_SIZE
];
2260 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2261 log_debug("IMPORT '%s' %s:%u\n",
2263 &rules
->buf
[rule
->rule
.filename_off
],
2264 rule
->rule
.filename_line
);
2266 if (import_program_into_properties(event
, import
, sigmask
) != 0)
2267 if (cur
->key
.op
!= OP_NOMATCH
)
2271 case TK_M_IMPORT_BUILTIN
: {
2272 char command
[UTIL_PATH_SIZE
];
2274 if (udev_builtin_run_once(cur
->key
.builtin_cmd
)) {
2275 /* check if we ran already */
2276 if (event
->builtin_run
& (1 << cur
->key
.builtin_cmd
)) {
2277 log_debug("IMPORT builtin skip '%s' %s:%u\n",
2278 udev_builtin_name(cur
->key
.builtin_cmd
),
2279 &rules
->buf
[rule
->rule
.filename_off
],
2280 rule
->rule
.filename_line
);
2281 /* return the result from earlier run */
2282 if (event
->builtin_ret
& (1 << cur
->key
.builtin_cmd
))
2283 if (cur
->key
.op
!= OP_NOMATCH
)
2288 event
->builtin_run
|= (1 << cur
->key
.builtin_cmd
);
2291 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], command
, sizeof(command
));
2292 log_debug("IMPORT builtin '%s' %s:%u\n",
2293 udev_builtin_name(cur
->key
.builtin_cmd
),
2294 &rules
->buf
[rule
->rule
.filename_off
],
2295 rule
->rule
.filename_line
);
2297 if (udev_builtin_run(event
->dev
, cur
->key
.builtin_cmd
, command
, false) != 0) {
2298 /* remember failure */
2299 log_debug("IMPORT builtin '%s' returned non-zero\n",
2300 udev_builtin_name(cur
->key
.builtin_cmd
));
2301 event
->builtin_ret
|= (1 << cur
->key
.builtin_cmd
);
2302 if (cur
->key
.op
!= OP_NOMATCH
)
2307 case TK_M_IMPORT_DB
: {
2308 const char *key
= &rules
->buf
[cur
->key
.value_off
];
2311 value
= udev_device_get_property_value(event
->dev_db
, key
);
2312 if (value
!= NULL
) {
2313 struct udev_list_entry
*entry
;
2315 entry
= udev_device_add_property(event
->dev
, key
, value
);
2316 udev_list_entry_set_num(entry
, true);
2318 if (cur
->key
.op
!= OP_NOMATCH
)
2323 case TK_M_IMPORT_CMDLINE
: {
2325 bool imported
= false;
2327 f
= fopen("/proc/cmdline", "r");
2331 if (fgets(cmdline
, sizeof(cmdline
), f
) != NULL
) {
2332 const char *key
= &rules
->buf
[cur
->key
.value_off
];
2335 pos
= strstr(cmdline
, key
);
2337 struct udev_list_entry
*entry
;
2340 if (pos
[0] == '\0' || isspace(pos
[0])) {
2341 /* we import simple flags as 'FLAG=1' */
2342 entry
= udev_device_add_property(event
->dev
, key
, "1");
2343 udev_list_entry_set_num(entry
, true);
2345 } else if (pos
[0] == '=') {
2350 while (pos
[0] != '\0' && !isspace(pos
[0]))
2353 entry
= udev_device_add_property(event
->dev
, key
, value
);
2354 udev_list_entry_set_num(entry
, true);
2361 if (!imported
&& cur
->key
.op
!= OP_NOMATCH
)
2365 case TK_M_IMPORT_PARENT
: {
2366 char import
[UTIL_PATH_SIZE
];
2368 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], import
, sizeof(import
));
2369 if (import_parent_into_properties(event
->dev
, import
) != 0)
2370 if (cur
->key
.op
!= OP_NOMATCH
)
2375 if (match_key(rules
, cur
, event
->program_result
) != 0)
2378 case TK_A_STRING_ESCAPE_NONE
:
2381 case TK_A_STRING_ESCAPE_REPLACE
:
2382 esc
= ESCAPE_REPLACE
;
2384 case TK_A_DB_PERSIST
:
2385 udev_device_set_db_persist(event
->dev
);
2387 case TK_A_INOTIFY_WATCH
:
2388 if (event
->inotify_watch_final
)
2390 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2391 event
->inotify_watch_final
= true;
2392 event
->inotify_watch
= cur
->key
.watch
;
2394 case TK_A_DEVLINK_PRIO
:
2395 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2398 char owner
[UTIL_NAME_SIZE
];
2400 if (event
->owner_final
)
2402 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2403 event
->owner_final
= true;
2404 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], owner
, sizeof(owner
));
2405 event
->uid
= util_lookup_user(event
->udev
, owner
);
2406 log_debug("OWNER %u %s:%u\n",
2408 &rules
->buf
[rule
->rule
.filename_off
],
2409 rule
->rule
.filename_line
);
2413 char group
[UTIL_NAME_SIZE
];
2415 if (event
->group_final
)
2417 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2418 event
->group_final
= true;
2419 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], group
, sizeof(group
));
2420 event
->gid
= util_lookup_group(event
->udev
, group
);
2421 log_debug("GROUP %u %s:%u\n",
2423 &rules
->buf
[rule
->rule
.filename_off
],
2424 rule
->rule
.filename_line
);
2428 char mode_str
[UTIL_NAME_SIZE
];
2432 if (event
->mode_final
)
2434 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], mode_str
, sizeof(mode_str
));
2435 mode
= strtol(mode_str
, &endptr
, 8);
2436 if (endptr
[0] != '\0') {
2437 log_error("ignoring invalid mode '%s'\n", mode_str
);
2440 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2441 event
->mode_final
= true;
2442 event
->mode_set
= true;
2444 log_debug("MODE %#o %s:%u\n",
2446 &rules
->buf
[rule
->rule
.filename_off
],
2447 rule
->rule
.filename_line
);
2451 if (event
->owner_final
)
2453 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2454 event
->owner_final
= true;
2455 event
->uid
= cur
->key
.uid
;
2456 log_debug("OWNER %u %s:%u\n",
2458 &rules
->buf
[rule
->rule
.filename_off
],
2459 rule
->rule
.filename_line
);
2462 if (event
->group_final
)
2464 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2465 event
->group_final
= true;
2466 event
->gid
= cur
->key
.gid
;
2467 log_debug("GROUP %u %s:%u\n",
2469 &rules
->buf
[rule
->rule
.filename_off
],
2470 rule
->rule
.filename_line
);
2473 if (event
->mode_final
)
2475 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2476 event
->mode_final
= true;
2477 event
->mode_set
= true;
2478 event
->mode
= cur
->key
.mode
;
2479 log_debug("MODE %#o %s:%u\n",
2481 &rules
->buf
[rule
->rule
.filename_off
],
2482 rule
->rule
.filename_line
);
2485 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2486 char *value
= &rules
->buf
[cur
->key
.value_off
];
2488 if (value
[0] != '\0') {
2489 char temp_value
[UTIL_NAME_SIZE
];
2490 struct udev_list_entry
*entry
;
2492 udev_event_apply_format(event
, value
, temp_value
, sizeof(temp_value
));
2493 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2494 /* store in db, skip private keys */
2496 udev_list_entry_set_num(entry
, true);
2498 udev_device_add_property(event
->dev
, name
, NULL
);
2503 char tag
[UTIL_PATH_SIZE
];
2506 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], tag
, sizeof(tag
));
2507 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2508 udev_device_cleanup_tags_list(event
->dev
);
2509 for (p
= tag
; *p
!= '\0'; p
++) {
2510 if ((*p
>= 'a' && *p
<= 'z') ||
2511 (*p
>= 'A' && *p
<= 'Z') ||
2512 (*p
>= '0' && *p
<= '9') ||
2513 *p
== '-' || *p
== '_')
2515 log_error("ignoring invalid tag name '%s'\n", tag
);
2518 udev_device_add_tag(event
->dev
, tag
);
2522 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2524 char name_str
[UTIL_PATH_SIZE
];
2527 if (event
->name_final
)
2529 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2530 event
->name_final
= true;
2531 udev_event_apply_format(event
, name
, name_str
, sizeof(name_str
));
2532 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2533 count
= util_replace_chars(name_str
, "/");
2535 log_debug("%i character(s) replaced\n", count
);
2537 if (major(udev_device_get_devnum(event
->dev
))) {
2538 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
2540 if (strcmp(name_str
, &udev_device_get_devnode(event
->dev
)[devlen
]) != 0) {
2541 log_error("NAME=\"%s\" ignored, kernel device nodes "
2542 "can not be renamed; please fix it in %s:%u\n", name
,
2543 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2548 event
->name
= strdup(name_str
);
2549 log_debug("NAME '%s' %s:%u\n",
2551 &rules
->buf
[rule
->rule
.filename_off
],
2552 rule
->rule
.filename_line
);
2555 case TK_A_DEVLINK
: {
2556 char temp
[UTIL_PATH_SIZE
];
2557 char filename
[UTIL_PATH_SIZE
];
2561 if (event
->devlink_final
)
2563 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2565 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2566 event
->devlink_final
= true;
2567 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2568 udev_device_cleanup_devlinks_list(event
->dev
);
2570 /* allow multiple symlinks separated by spaces */
2571 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], temp
, sizeof(temp
));
2572 if (esc
== ESCAPE_UNSET
)
2573 count
= util_replace_chars(temp
, "/ ");
2574 else if (esc
== ESCAPE_REPLACE
)
2575 count
= util_replace_chars(temp
, "/");
2577 log_debug("%i character(s) replaced\n" , count
);
2579 while (isspace(pos
[0]))
2581 next
= strchr(pos
, ' ');
2582 while (next
!= NULL
) {
2584 log_debug("LINK '%s' %s:%u\n", pos
,
2585 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2586 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2587 udev_device_add_devlink(event
->dev
, filename
, cur
->key
.devlink_unique
);
2588 while (isspace(next
[1]))
2591 next
= strchr(pos
, ' ');
2593 if (pos
[0] != '\0') {
2594 log_debug("LINK '%s' %s:%u\n", pos
,
2595 &rules
->buf
[rule
->rule
.filename_off
], rule
->rule
.filename_line
);
2596 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(event
->udev
), "/", pos
, NULL
);
2597 udev_device_add_devlink(event
->dev
, filename
, cur
->key
.devlink_unique
);
2602 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2603 char attr
[UTIL_PATH_SIZE
];
2604 char value
[UTIL_NAME_SIZE
];
2607 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0)
2608 util_strscpyl(attr
, sizeof(attr
), udev_device_get_syspath(event
->dev
), "/", key_name
, NULL
);
2609 attr_subst_subdir(attr
, sizeof(attr
));
2611 udev_event_apply_format(event
, &rules
->buf
[cur
->key
.value_off
], value
, sizeof(value
));
2612 log_debug("ATTR '%s' writing '%s' %s:%u\n", attr
, value
,
2613 &rules
->buf
[rule
->rule
.filename_off
],
2614 rule
->rule
.filename_line
);
2615 f
= fopen(attr
, "w");
2617 if (fprintf(f
, "%s", value
) <= 0)
2618 log_error("error writing ATTR{%s}: %m\n", attr
);
2621 log_error("error opening ATTR{%s} for writing: %m\n", attr
);
2625 case TK_A_RUN_BUILTIN
:
2626 case TK_A_RUN_PROGRAM
: {
2627 struct udev_list_entry
*entry
;
2629 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2630 udev_list_cleanup(&event
->run_list
);
2631 log_debug("RUN '%s' %s:%u\n",
2632 &rules
->buf
[cur
->key
.value_off
],
2633 &rules
->buf
[rule
->rule
.filename_off
],
2634 rule
->rule
.filename_line
);
2635 entry
= udev_list_entry_add(&event
->run_list
, &rules
->buf
[cur
->key
.value_off
], NULL
);
2636 udev_list_entry_set_num(entry
, cur
->key
.builtin_cmd
);
2640 if (cur
->key
.rule_goto
== 0)
2642 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2647 case TK_M_PARENTS_MIN
:
2648 case TK_M_PARENTS_MAX
:
2651 log_error("wrong type %u\n", cur
->type
);
2658 /* fast-forward to next rule */
2659 cur
= rule
+ rule
->rule
.token_count
;
2663 void udev_rules_apply_static_dev_perms(struct udev_rules
*rules
)
2671 if (rules
->tokens
== NULL
)
2674 cur
= &rules
->tokens
[0];
2677 switch (cur
->type
) {
2682 /* skip rules without a static_node tag */
2683 if (!rule
->rule
.has_static_node
)
2697 mode
= cur
->key
.mode
;
2699 case TK_A_STATIC_NODE
: {
2700 char filename
[UTIL_PATH_SIZE
];
2703 /* we assure, that the permissions tokens are sorted before the static token */
2704 if (mode
== 0 && uid
== 0 && gid
== 0)
2706 util_strscpyl(filename
, sizeof(filename
), udev_get_dev_path(rules
->udev
), "/",
2707 &rules
->buf
[cur
->key
.value_off
], NULL
);
2708 if (stat(filename
, &stats
) != 0)
2710 if (!S_ISBLK(stats
.st_mode
) && !S_ISCHR(stats
.st_mode
))
2718 if (mode
!= (stats
.st_mode
& 01777)) {
2719 chmod(filename
, mode
);
2720 log_debug("chmod '%s' %#o\n", filename
, mode
);
2723 if ((uid
!= 0 && uid
!= stats
.st_uid
) || (gid
!= 0 && gid
!= stats
.st_gid
)) {
2724 chown(filename
, uid
, gid
);
2725 log_debug("chown '%s' %u %u\n", filename
, uid
, gid
);
2728 utimensat(AT_FDCWD
, filename
, NULL
, 0);
2738 /* fast-forward to next rule */
2739 cur
= rule
+ rule
->rule
.token_count
;