2 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #define PREALLOC_TOKEN 2048
32 #define PREALLOC_STRBUF 32 * 1024
34 /* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
47 static const char *operation_str
[] = {
50 [OP_NOMATCH
] = "nomatch",
51 [OP_MATCH_MAX
] = "MATCH_MAX",
54 [OP_ASSIGN
] = "assign",
55 [OP_ASSIGN_FINAL
] = "assign-final",
58 enum string_glob_type
{
60 GL_PLAIN
, /* no special chars */
61 GL_GLOB
, /* shell globs ?,*,[] */
62 GL_SPLIT
, /* multi-value A|B */
63 GL_SPLIT_GLOB
, /* multi-value with glob A*|B* */
64 GL_SOMETHING
, /* commonly used "?*" */
69 static const char *string_glob_str
[] = {
74 [GL_SPLIT_GLOB
] = "split-glob",
75 [GL_SOMETHING
] = "split-glob",
76 [GL_FORMAT
] = "format",
80 /* tokens of a rule are sorted/handled in this order */
85 TK_M_ACTION
, /* val */
86 TK_M_DEVPATH
, /* val */
87 TK_M_KERNEL
, /* val */
88 TK_M_DEVLINK
, /* val */
90 TK_M_ENV
, /* val, attr */
91 TK_M_SUBSYSTEM
, /* val */
92 TK_M_DRIVER
, /* val */
93 TK_M_WAITFOR
, /* val */
94 TK_M_ATTR
, /* val, attr */
96 TK_M_KERNELS
, /* val */
97 TK_M_SUBSYSTEMS
, /* val */
98 TK_M_DRIVERS
, /* val */
99 TK_M_ATTRS
, /* val, attr */
102 TK_M_TEST
, /* val, mode_t */
103 TK_M_PROGRAM
, /* val */
104 TK_M_IMPORT_FILE
, /* val */
105 TK_M_IMPORT_PROG
, /* val */
106 TK_M_IMPORT_PARENT
, /* val */
107 TK_M_RESULT
, /* val */
111 TK_A_STRING_ESCAPE_NONE
,
112 TK_A_STRING_ESCAPE_REPLACE
,
113 TK_A_NUM_FAKE_PART
, /* int */
114 TK_A_DEVLINK_PRIO
, /* int */
115 TK_A_OWNER
, /* val */
116 TK_A_GROUP
, /* val */
118 TK_A_OWNER_ID
, /* uid_t */
119 TK_A_GROUP_ID
, /* gid_t */
120 TK_A_MODE_ID
, /* mode_t */
121 TK_A_ENV
, /* val, attr */
123 TK_A_DEVLINK
, /* val */
124 TK_A_EVENT_TIMEOUT
, /* int */
126 TK_A_ATTR
, /* val, attr */
127 TK_A_RUN
, /* val, bool */
128 TK_A_GOTO
, /* size_t */
134 static const char *token_str
[] = {
135 [TK_UNSET
] = "UNSET",
138 [TK_M_ACTION
] = "M ACTION",
139 [TK_M_DEVPATH
] = "M DEVPATH",
140 [TK_M_KERNEL
] = "M KERNEL",
141 [TK_M_DEVLINK
] = "M DEVLINK",
142 [TK_M_NAME
] = "M NAME",
143 [TK_M_ENV
] = "M ENV",
144 [TK_M_SUBSYSTEM
] = "M SUBSYSTEM",
145 [TK_M_DRIVER
] = "M DRIVER",
146 [TK_M_WAITFOR
] = "M WAITFOR",
147 [TK_M_ATTR
] = "M ATTR",
149 [TK_M_KERNELS
] = "M KERNELS",
150 [TK_M_SUBSYSTEMS
] = "M SUBSYSTEMS",
151 [TK_M_DRIVERS
] = "M DRIVERS",
152 [TK_M_ATTRS
] = "M ATTRS",
153 [TK_M_PARENTS_MAX
] = "M PARENTS_MAX",
155 [TK_M_TEST
] = "M TEST",
156 [TK_M_PROGRAM
] = "M PROGRAM",
157 [TK_M_IMPORT_FILE
] = "M IMPORT_FILE",
158 [TK_M_IMPORT_PROG
] = "M IMPORT_PROG",
159 [TK_M_IMPORT_PARENT
] = "M MPORT_PARENT",
160 [TK_M_RESULT
] = "M RESULT",
161 [TK_M_MAX
] = "M MAX",
163 [TK_A_IGNORE_DEVICE
] = "A IGNORE_DEVICE",
164 [TK_A_STRING_ESCAPE_NONE
] = "A STRING_ESCAPE_NONE",
165 [TK_A_STRING_ESCAPE_REPLACE
] = "A STRING_ESCAPE_REPLACE",
166 [TK_A_NUM_FAKE_PART
] = "A NUM_FAKE_PART",
167 [TK_A_DEVLINK_PRIO
] = "A DEVLINK_PRIO",
168 [TK_A_OWNER
] = "A OWNER",
169 [TK_A_GROUP
] = "A GROUP",
170 [TK_A_MODE
] = "A MODE",
171 [TK_A_OWNER_ID
] = "A OWNER_ID",
172 [TK_A_GROUP_ID
] = "A GROUP_ID",
173 [TK_A_MODE_ID
] = "A MODE_ID",
174 [TK_A_ENV
] = "A ENV",
175 [TK_A_NAME
] = "A NAME",
176 [TK_A_DEVLINK
] = "A DEVLINK",
177 [TK_A_EVENT_TIMEOUT
] = "A EVENT_TIMEOUT",
178 [TK_A_IGNORE_REMOVE
] = "A IGNORE_REMOVE",
179 [TK_A_ATTR
] = "A ATTR",
180 [TK_A_RUN
] = "A RUN",
181 [TK_A_GOTO
] = "A GOTO",
182 [TK_A_LAST_RULE
] = "A LAST_RULE",
188 enum token_type type
;
191 unsigned int next_rule
;
192 unsigned int label_off
;
193 unsigned int filename_off
;
198 unsigned int value_off
;
200 unsigned int attr_off
;
203 unsigned int rule_goto
;
217 struct udev_rules
*rules
;
219 struct token token
[MAX_TK
];
220 unsigned int token_cur
;
224 unsigned int name_off
;
235 /* every key in the rules file becomes a token */
236 struct token
*tokens
;
237 unsigned int token_cur
;
238 unsigned int token_max
;
240 /* all key strings are copied to a single string buffer */
244 unsigned int buf_count
;
246 /* during rule parsing, we cache uid/gid lookup results */
247 struct uid_gid
*uids
;
248 unsigned int uids_cur
;
249 unsigned int uids_max
;
250 struct uid_gid
*gids
;
251 unsigned int gids_cur
;
252 unsigned int gids_max
;
255 /* NOTE: we could lookup and return existing strings, or tails of strings */
256 static int add_string(struct udev_rules
*rules
, const char *str
)
258 size_t len
= strlen(str
)+1;
261 /* offset 0 is always '\0' */
265 /* grow buffer if needed */
266 if (rules
->buf_cur
+ len
+1 >= rules
->buf_max
) {
270 /* double the buffer size */
271 add
= rules
->buf_max
;
275 buf
= realloc(rules
->buf
, rules
->buf_max
+ add
);
278 info(rules
->udev
, "extend buffer from %zu to %zu\n", rules
->buf_max
, rules
->buf_max
+ add
);
280 rules
->buf_max
+= add
;
282 off
= rules
->buf_cur
;
283 memcpy(&rules
->buf
[rules
->buf_cur
], str
, len
);
284 rules
->buf_cur
+= len
;
289 static int add_token(struct udev_rules
*rules
, struct token
*token
)
292 /* grow buffer if needed */
293 if (rules
->token_cur
+1 >= rules
->token_max
) {
294 struct token
*tokens
;
297 /* double the buffer size */
298 add
= rules
->token_max
;
302 tokens
= realloc(rules
->tokens
, (rules
->token_max
+ add
) * sizeof(struct token
));
305 info(rules
->udev
, "extend tokens from %u to %u\n", rules
->token_max
, rules
->token_max
+ add
);
306 rules
->tokens
= tokens
;
307 rules
->token_max
+= add
;
309 memcpy(&rules
->tokens
[rules
->token_cur
], token
, sizeof(struct token
));
314 static uid_t
add_uid(struct udev_rules
*rules
, const char *owner
)
320 /* lookup, if we know it already */
321 for (i
= 0; i
< rules
->uids_cur
; i
++) {
322 off
= rules
->uids
[i
].name_off
;
323 if (strcmp(&rules
->buf
[off
], owner
) == 0) {
324 uid
= rules
->uids
[i
].uid
;
325 dbg(rules
->udev
, "return existing %u for '%s'\n", uid
, owner
);
329 uid
= util_lookup_user(rules
->udev
, owner
);
331 /* grow buffer if needed */
332 if (rules
->uids_cur
+1 >= rules
->uids_max
) {
333 struct uid_gid
*uids
;
336 /* double the buffer size */
337 add
= rules
->uids_max
;
341 uids
= realloc(rules
->uids
, (rules
->uids_max
+ add
) * sizeof(struct uid_gid
));
344 info(rules
->udev
, "extend uids from %u to %u\n", rules
->uids_max
, rules
->uids_max
+ add
);
346 rules
->uids_max
+= add
;
348 rules
->uids
[rules
->uids_cur
].uid
= uid
;
349 off
= add_string(rules
, owner
);
352 rules
->uids
[rules
->uids_cur
].name_off
= off
;
357 static gid_t
add_gid(struct udev_rules
*rules
, const char *group
)
363 /* lookup, if we know it already */
364 for (i
= 0; i
< rules
->gids_cur
; i
++) {
365 off
= rules
->gids
[i
].name_off
;
366 if (strcmp(&rules
->buf
[off
], group
) == 0) {
367 gid
= rules
->gids
[i
].gid
;
368 dbg(rules
->udev
, "return existing %u for '%s'\n", gid
, group
);
372 gid
= util_lookup_group(rules
->udev
, group
);
374 /* grow buffer if needed */
375 if (rules
->gids_cur
+1 >= rules
->gids_max
) {
376 struct uid_gid
*gids
;
379 /* double the buffer size */
380 add
= rules
->gids_max
;
384 gids
= realloc(rules
->gids
, (rules
->gids_max
+ add
) * sizeof(struct uid_gid
));
387 info(rules
->udev
, "extend gids from %u to %u\n", rules
->gids_max
, rules
->gids_max
+ add
);
389 rules
->gids_max
+= add
;
391 rules
->gids
[rules
->gids_cur
].gid
= gid
;
392 off
= add_string(rules
, group
);
395 rules
->gids
[rules
->gids_cur
].name_off
= off
;
400 static int import_property_from_string(struct udev_device
*dev
, char *line
)
402 struct udev
*udev
= udev_device_get_udev(dev
);
409 while (isspace(key
[0]))
412 /* comment or empty line */
413 if (key
[0] == '#' || key
[0] == '\0')
416 /* split key/value */
417 val
= strchr(key
, '=');
424 while (isspace(val
[0]))
431 while (isspace(key
[len
-1]))
435 /* terminate value */
439 while (isspace(val
[len
-1]))
447 if (val
[0] == '"' || val
[0] == '\'') {
448 if (val
[len
-1] != val
[0]) {
449 info(udev
, "inconsistent quoting: '%s', skip\n", line
);
456 info(udev
, "adding '%s'='%s'\n", key
, val
);
458 /* handle device, renamed by external tool, returning new path */
459 if (strcmp(key
, "DEVPATH") == 0) {
460 char syspath
[UTIL_PATH_SIZE
];
462 info(udev
, "updating devpath from '%s' to '%s'\n",
463 udev_device_get_devpath(dev
), val
);
464 util_strlcpy(syspath
, udev_get_sys_path(udev
), sizeof(syspath
));
465 util_strlcat(syspath
, val
, sizeof(syspath
));
466 udev_device_set_syspath(dev
, syspath
);
468 struct udev_list_entry
*entry
;
470 entry
= udev_device_add_property(dev
, key
, val
);
472 udev_list_entry_set_flag(entry
, 1);
477 static int import_file_into_properties(struct udev_device
*dev
, const char *filename
)
480 char line
[UTIL_LINE_SIZE
];
482 f
= fopen(filename
, "r");
485 while (fgets(line
, sizeof(line
), f
) != NULL
)
486 import_property_from_string(dev
, line
);
491 static int import_program_into_properties(struct udev_device
*dev
, const char *program
)
493 struct udev
*udev
= udev_device_get_udev(dev
);
499 envp
= udev_device_get_properties_envp(dev
);
500 if (util_run_program(udev
, program
, envp
, result
, sizeof(result
), &reslen
) != 0)
504 while (line
!= NULL
) {
507 pos
= strchr(line
, '\n');
512 import_property_from_string(dev
, line
);
518 static int import_parent_into_properties(struct udev_device
*dev
, const char *filter
)
520 struct udev
*udev
= udev_device_get_udev(dev
);
521 struct udev_device
*dev_parent
;
522 struct udev_list_entry
*list_entry
;
524 dev_parent
= udev_device_get_parent(dev
);
525 if (dev_parent
== NULL
)
528 dbg(udev
, "found parent '%s', get the node name\n", udev_device_get_syspath(dev_parent
));
529 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(dev_parent
)) {
530 const char *key
= udev_list_entry_get_name(list_entry
);
531 const char *val
= udev_list_entry_get_value(list_entry
);
533 if (fnmatch(filter
, key
, 0) == 0) {
534 struct udev_list_entry
*entry
;
536 dbg(udev
, "import key '%s=%s'\n", key
, val
);
537 entry
= udev_device_add_property(dev
, key
, val
);
539 udev_list_entry_set_flag(entry
, 1);
545 #define WAIT_LOOP_PER_SECOND 50
546 static int wait_for_file(struct udev_device
*dev
, const char *file
, int timeout
)
548 struct udev
*udev
= udev_device_get_udev(dev
);
549 char filepath
[UTIL_PATH_SIZE
];
550 char devicepath
[UTIL_PATH_SIZE
];
552 int loop
= timeout
* WAIT_LOOP_PER_SECOND
;
554 /* a relative path is a device attribute */
555 devicepath
[0] = '\0';
556 if (file
[0] != '/') {
557 util_strlcpy(devicepath
, udev_get_sys_path(udev
), sizeof(devicepath
));
558 util_strlcat(devicepath
, udev_device_get_devpath(dev
), sizeof(devicepath
));
560 util_strlcpy(filepath
, devicepath
, sizeof(filepath
));
561 util_strlcat(filepath
, "/", sizeof(filepath
));
562 util_strlcat(filepath
, file
, sizeof(filepath
));
566 dbg(udev
, "will wait %i sec for '%s'\n", timeout
, file
);
569 if (stat(file
, &stats
) == 0) {
570 info(udev
, "file '%s' appeared after %i loops\n", file
, (timeout
* WAIT_LOOP_PER_SECOND
) - loop
-1);
573 /* make sure, the device did not disappear in the meantime */
574 if (devicepath
[0] != '\0' && stat(devicepath
, &stats
) != 0) {
575 info(udev
, "device disappeared while waiting for '%s'\n", file
);
578 info(udev
, "wait for '%s' for %i mseconds\n", file
, 1000 / WAIT_LOOP_PER_SECOND
);
579 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
581 info(udev
, "waiting for '%s' failed\n", file
);
585 static int attr_subst_subdir(char *attr
, size_t len
)
590 pos
= strstr(attr
, "/*/");
592 char str
[UTIL_PATH_SIZE
];
596 util_strlcpy(str
, &pos
[2], sizeof(str
));
601 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
604 if (dent
->d_name
[0] == '.')
606 util_strlcat(attr
, dent
->d_name
, len
);
607 util_strlcat(attr
, str
, len
);
608 if (stat(attr
, &stats
) == 0) {
617 util_strlcat(attr
, str
, len
);
623 static int get_key(struct udev
*udev
, char **line
, char **key
, enum operation_type
*op
, char **value
)
629 if (linepos
== NULL
&& linepos
[0] == '\0')
632 /* skip whitespace */
633 while (isspace(linepos
[0]) || linepos
[0] == ',')
637 if (linepos
[0] == '\0')
643 if (linepos
[0] == '\0')
645 if (isspace(linepos
[0]))
647 if (linepos
[0] == '=')
649 if ((linepos
[0] == '+') || (linepos
[0] == '!') || (linepos
[0] == ':'))
650 if (linepos
[1] == '=')
654 /* remember end of key */
657 /* skip whitespace after key */
658 while (isspace(linepos
[0]))
660 if (linepos
[0] == '\0')
663 /* get operation type */
664 if (linepos
[0] == '=' && linepos
[1] == '=') {
667 } else if (linepos
[0] == '!' && linepos
[1] == '=') {
670 } else if (linepos
[0] == '+' && linepos
[1] == '=') {
673 } else if (linepos
[0] == '=') {
676 } else if (linepos
[0] == ':' && linepos
[1] == '=') {
677 *op
= OP_ASSIGN_FINAL
;
685 /* skip whitespace after operator */
686 while (isspace(linepos
[0]))
688 if (linepos
[0] == '\0')
692 if (linepos
[0] == '"')
699 temp
= strchr(linepos
, '"');
704 dbg(udev
, "%s '%s'-'%s'\n", operation_str
[*op
], *key
, *value
);
706 /* move line to next key */
711 /* extract possible KEY{attr} */
712 static char *get_key_attribute(struct udev
*udev
, char *str
)
717 attr
= strchr(str
, '{');
720 pos
= strchr(attr
, '}');
722 err(udev
, "missing closing brace for format\n");
726 dbg(udev
, "attribute='%s'\n", attr
);
732 static int rule_add_token(struct rule_tmp
*rule_tmp
, enum token_type type
,
733 enum operation_type op
,
734 const char *value
, const void *data
)
736 struct token
*token
= &rule_tmp
->token
[rule_tmp
->token_cur
];
737 const char *attr
= data
;
738 enum string_glob_type glob
;
740 memset(token
, 0x00, sizeof(struct token
));
752 case TK_M_SUBSYSTEMS
:
755 case TK_M_IMPORT_FILE
:
756 case TK_M_IMPORT_PROG
:
757 case TK_M_IMPORT_PARENT
:
765 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
772 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
773 token
->key
.attr_off
= add_string(rule_tmp
->rules
, attr
);
776 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
778 token
->key
.mode
= *(mode_t
*)data
;
780 case TK_A_IGNORE_DEVICE
:
781 case TK_A_STRING_ESCAPE_NONE
:
782 case TK_A_STRING_ESCAPE_REPLACE
:
783 case TK_A_IGNORE_REMOVE
:
787 token
->key
.value_off
= add_string(rule_tmp
->rules
, value
);
788 token
->key
.ignore_error
= *(int *)data
;
790 case TK_A_NUM_FAKE_PART
:
791 token
->key
.num_fake_part
= *(int *)data
;
793 case TK_A_DEVLINK_PRIO
:
794 token
->key
.devlink_prio
= *(int *)data
;
797 token
->key
.uid
= *(uid_t
*)data
;
800 token
->key
.gid
= *(gid_t
*)data
;
803 token
->key
.mode
= *(mode_t
*)data
;
805 case TK_A_EVENT_TIMEOUT
:
806 token
->key
.event_timeout
= *(int *)data
;
809 case TK_M_PARENTS_MAX
:
813 err(rule_tmp
->rules
->udev
, "wrong type %u\n", type
);
819 if (type
< TK_M_MAX
) {
820 /* check if we need to split or call fnmatch() while matching rules */
824 has_split
= (strchr(value
, '|') != NULL
);
825 has_glob
= (strchr(value
, '*') != NULL
|| strchr(value
, '?') != NULL
||
826 strchr(value
, '[') != NULL
|| strchr(value
, ']') != NULL
);
827 if (has_split
&& has_glob
) {
828 glob
= GL_SPLIT_GLOB
;
829 } else if (has_split
) {
831 } else if (has_glob
) {
832 if (strcmp(value
, "?*") == 0)
838 /* check if we need to substitute format strings for matching rules */
839 if (strchr(value
, '%') != NULL
|| strchr(value
, '$') != NULL
)
846 token
->key
.glob
= glob
;
847 rule_tmp
->token_cur
++;
848 if (rule_tmp
->token_cur
>= ARRAY_SIZE(rule_tmp
->token
)) {
849 err(rule_tmp
->rules
->udev
, "temporary rule array too small\n");
856 static void dump_token(struct udev_rules
*rules
, struct token
*token
)
858 enum token_type type
= token
->type
;
859 enum operation_type op
= token
->key
.op
;
860 enum string_glob_type glob
= token
->key
.glob
;
861 const char *value
= &rules
->buf
[token
->key
.value_off
];
862 const char *attr
= &rules
->buf
[token
->key
.attr_off
];
867 const char *tks_ptr
= (char *)rules
->tokens
;
868 const char *tk_ptr
= (char *)token
;
869 unsigned int off
= tk_ptr
- tks_ptr
;
871 dbg(rules
->udev
, "* RULE '%s', off: %u(%u), next: %u, label: '%s'\n",
872 &rules
->buf
[token
->rule
.filename_off
],
873 off
/ (unsigned int) sizeof(struct token
), off
,
874 token
->rule
.next_rule
,
875 &rules
->buf
[token
->rule
.label_off
]);
887 case TK_M_SUBSYSTEMS
:
890 case TK_M_IMPORT_FILE
:
891 case TK_M_IMPORT_PROG
:
892 case TK_M_IMPORT_PARENT
:
900 dbg(rules
->udev
, "%s %s '%s'(%s)\n",
901 token_str
[type
], operation_str
[op
], value
, string_glob_str
[glob
]);
908 dbg(rules
->udev
, "%s %s '%s' '%s'(%s)\n",
909 token_str
[type
], operation_str
[op
], attr
, value
, string_glob_str
[glob
]);
911 case TK_A_IGNORE_DEVICE
:
912 case TK_A_STRING_ESCAPE_NONE
:
913 case TK_A_STRING_ESCAPE_REPLACE
:
915 case TK_A_IGNORE_REMOVE
:
916 dbg(rules
->udev
, "%s\n", token_str
[type
]);
919 dbg(rules
->udev
, "%s %s '%s'(%s) %#o\n",
920 token_str
[type
], operation_str
[op
], value
, string_glob_str
[glob
], token
->key
.mode
);
922 case TK_A_NUM_FAKE_PART
:
923 dbg(rules
->udev
, "%s %u\n", token_str
[type
], token
->key
.num_fake_part
);
925 case TK_A_DEVLINK_PRIO
:
926 dbg(rules
->udev
, "%s %s %u\n", token_str
[type
], operation_str
[op
], token
->key
.devlink_prio
);
929 dbg(rules
->udev
, "%s %s %u\n", token_str
[type
], operation_str
[op
], token
->key
.uid
);
932 dbg(rules
->udev
, "%s %s %u\n", token_str
[type
], operation_str
[op
], token
->key
.gid
);
935 dbg(rules
->udev
, "%s %s %#o\n", token_str
[type
], operation_str
[op
], token
->key
.mode
);
937 case TK_A_EVENT_TIMEOUT
:
938 dbg(rules
->udev
, "%s %s %u\n", token_str
[type
], operation_str
[op
], token
->key
.event_timeout
);
941 dbg(rules
->udev
, "%s '%s' %u\n", token_str
[type
], value
, token
->key
.rule_goto
);
944 dbg(rules
->udev
, "* %s\n", token_str
[type
]);
946 case TK_M_PARENTS_MAX
:
949 dbg(rules
->udev
, "unknown type %u\n", type
);
954 static void dump_rules(struct udev_rules
*rules
)
958 dbg(rules
->udev
, "dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
960 rules
->token_cur
* sizeof(struct token
),
963 for(i
= 0; i
< rules
->token_cur
; i
++)
964 dump_token(rules
, &rules
->tokens
[i
]);
967 static inline void dump_token(struct udev_rules
*rules
, struct token
*token
) {}
968 static inline void dump_rules(struct udev_rules
*rules
) {}
971 static int sort_token(struct udev_rules
*rules
, struct rule_tmp
*rule_tmp
)
974 unsigned int start
= 0;
975 unsigned int end
= rule_tmp
->token_cur
;
977 for (i
= 0; i
< rule_tmp
->token_cur
; i
++) {
978 enum token_type next_val
= TK_UNSET
;
979 unsigned int next_idx
;
982 /* find smallest value */
983 for (j
= start
; j
< end
; j
++) {
984 if (rule_tmp
->token
[j
].type
== TK_UNSET
)
986 if (next_val
== TK_UNSET
|| rule_tmp
->token
[j
].type
< next_val
) {
987 next_val
= rule_tmp
->token
[j
].type
;
992 /* add token and mark done */
993 if (add_token(rules
, &rule_tmp
->token
[next_idx
]) != 0)
995 rule_tmp
->token
[next_idx
].type
= TK_UNSET
;
998 if (next_idx
== start
)
1000 if (next_idx
+1 == end
)
1006 static int add_rule(struct udev_rules
*rules
, char *line
,
1007 const char *filename
, unsigned int filename_off
, unsigned int lineno
)
1013 struct rule_tmp rule_tmp
;
1015 memset(&rule_tmp
, 0x00, sizeof(struct rule_tmp
));
1016 rule_tmp
.rules
= rules
;
1017 rule_tmp
.rule
.type
= TK_RULE
;
1018 rule_tmp
.rule
.rule
.filename_off
= filename_off
;
1024 enum operation_type op
;
1026 if (get_key(rules
->udev
, &linepos
, &key
, &op
, &value
) != 0)
1029 if (strcasecmp(key
, "ACTION") == 0) {
1030 if (op
> OP_MATCH_MAX
) {
1031 err(rules
->udev
, "invalid ACTION operation\n");
1034 rule_add_token(&rule_tmp
, TK_M_ACTION
, op
, value
, NULL
);
1039 if (strcasecmp(key
, "DEVPATH") == 0) {
1040 if (op
> OP_MATCH_MAX
) {
1041 err(rules
->udev
, "invalid DEVPATH operation\n");
1044 rule_add_token(&rule_tmp
, TK_M_DEVPATH
, op
, value
, NULL
);
1049 if (strcasecmp(key
, "KERNEL") == 0) {
1050 if (op
> OP_MATCH_MAX
) {
1051 err(rules
->udev
, "invalid KERNEL operation\n");
1054 rule_add_token(&rule_tmp
, TK_M_KERNEL
, op
, value
, NULL
);
1059 if (strcasecmp(key
, "SUBSYSTEM") == 0) {
1060 if (op
> OP_MATCH_MAX
) {
1061 err(rules
->udev
, "invalid SUBSYSTEM operation\n");
1064 /* bus, class, subsystem events should all be the same */
1065 if (strcmp(value
, "subsystem") == 0 ||
1066 strcmp(value
, "bus") == 0 ||
1067 strcmp(value
, "class") == 0) {
1068 if (strcmp(value
, "bus") == 0 || strcmp(value
, "class") == 0)
1069 err(rules
->udev
, "'%s' must be specified as 'subsystem' \n"
1070 "please fix it in %s:%u", value
, filename
, lineno
);
1071 rule_add_token(&rule_tmp
, TK_M_SUBSYSTEM
, op
, "subsystem|class|bus", NULL
);
1073 rule_add_token(&rule_tmp
, TK_M_SUBSYSTEM
, op
, value
, NULL
);
1078 if (strcasecmp(key
, "DRIVER") == 0) {
1079 if (op
> OP_MATCH_MAX
) {
1080 err(rules
->udev
, "invalid DRIVER operation\n");
1083 rule_add_token(&rule_tmp
, TK_M_DRIVER
, op
, value
, NULL
);
1088 if (strncasecmp(key
, "ATTR{", sizeof("ATTR{")-1) == 0) {
1089 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTR")-1);
1091 err(rules
->udev
, "error parsing ATTR attribute\n");
1094 if (op
< OP_MATCH_MAX
) {
1095 rule_add_token(&rule_tmp
, TK_M_ATTR
, op
, value
, attr
);
1097 rule_add_token(&rule_tmp
, TK_A_ATTR
, op
, value
, attr
);
1103 if (strcasecmp(key
, "KERNELS") == 0 ||
1104 strcasecmp(key
, "ID") == 0) {
1105 if (op
> OP_MATCH_MAX
) {
1106 err(rules
->udev
, "invalid KERNELS operation\n");
1109 rule_add_token(&rule_tmp
, TK_M_KERNELS
, op
, value
, NULL
);
1114 if (strcasecmp(key
, "SUBSYSTEMS") == 0 ||
1115 strcasecmp(key
, "BUS") == 0) {
1116 if (op
> OP_MATCH_MAX
) {
1117 err(rules
->udev
, "invalid SUBSYSTEMS operation\n");
1120 rule_add_token(&rule_tmp
, TK_M_SUBSYSTEMS
, op
, value
, NULL
);
1125 if (strcasecmp(key
, "DRIVERS") == 0) {
1126 if (op
> OP_MATCH_MAX
) {
1127 err(rules
->udev
, "invalid DRIVERS operation\n");
1130 rule_add_token(&rule_tmp
, TK_M_DRIVERS
, op
, value
, NULL
);
1135 if (strncasecmp(key
, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
1136 strncasecmp(key
, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
1137 if (op
> OP_MATCH_MAX
) {
1138 err(rules
->udev
, "invalid ATTRS operation\n");
1141 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ATTRS")-1);
1143 err(rules
->udev
, "error parsing ATTRS attribute\n");
1146 if (strncmp(attr
, "device/", 7) == 0)
1147 err(rules
->udev
, "the 'device' link may not be available in a future kernel, "
1148 "please fix it in %s:%u", filename
, lineno
);
1149 else if (strstr(attr
, "../") != NULL
)
1150 err(rules
->udev
, "do not reference parent sysfs directories directly, "
1151 "it may break with a future kernel, please fix it in %s:%u", filename
, lineno
);
1152 rule_add_token(&rule_tmp
, TK_M_ATTRS
, op
, value
, attr
);
1157 if (strncasecmp(key
, "ENV{", sizeof("ENV{")-1) == 0) {
1158 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("ENV")-1);
1160 err(rules
->udev
, "error parsing ENV attribute\n");
1163 if (strncmp(attr
, "PHYSDEV", 7) == 0)
1165 if (op
< OP_MATCH_MAX
) {
1166 if (rule_add_token(&rule_tmp
, TK_M_ENV
, op
, value
, attr
) != 0)
1169 if (rule_add_token(&rule_tmp
, TK_A_ENV
, op
, value
, attr
) != 0)
1176 if (strcasecmp(key
, "PROGRAM") == 0) {
1177 rule_add_token(&rule_tmp
, TK_M_PROGRAM
, op
, value
, NULL
);
1182 if (strcasecmp(key
, "RESULT") == 0) {
1183 if (op
> OP_MATCH_MAX
) {
1184 err(rules
->udev
, "invalid RESULT operation\n");
1187 rule_add_token(&rule_tmp
, TK_M_RESULT
, op
, value
, NULL
);
1192 if (strncasecmp(key
, "IMPORT", sizeof("IMPORT")-1) == 0) {
1193 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("IMPORT")-1);
1194 if (attr
!= NULL
&& strstr(attr
, "program")) {
1195 dbg(rules
->udev
, "IMPORT will be executed\n");
1196 rule_add_token(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1198 } else if (attr
!= NULL
&& strstr(attr
, "file")) {
1199 dbg(rules
->udev
, "IMPORT will be included as file\n");
1200 rule_add_token(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1202 } else if (attr
!= NULL
&& strstr(attr
, "parent")) {
1203 dbg(rules
->udev
, "IMPORT will include the parent values\n");
1204 rule_add_token(&rule_tmp
, TK_M_IMPORT_PARENT
, op
, value
, NULL
);
1207 /* figure it out if it is executable */
1208 char file
[UTIL_PATH_SIZE
];
1210 struct stat statbuf
;
1212 util_strlcpy(file
, value
, sizeof(file
));
1213 pos
= strchr(file
, ' ');
1217 /* allow programs in /lib/udev called without the path */
1218 if (strchr(file
, '/') == NULL
) {
1219 util_strlcpy(file
, UDEV_PREFIX
"/lib/udev/", sizeof(file
));
1220 util_strlcat(file
, value
, sizeof(file
));
1221 pos
= strchr(file
, ' ');
1226 dbg(rules
->udev
, "IMPORT auto mode for '%s'\n", file
);
1227 if (!lstat(file
, &statbuf
) && (statbuf
.st_mode
& S_IXUSR
)) {
1228 dbg(rules
->udev
, "IMPORT will be executed (autotype)\n");
1229 rule_add_token(&rule_tmp
, TK_M_IMPORT_PROG
, op
, value
, NULL
);
1232 dbg(rules
->udev
, "IMPORT will be included as file (autotype)\n");
1233 rule_add_token(&rule_tmp
, TK_M_IMPORT_FILE
, op
, value
, NULL
);
1240 if (strncasecmp(key
, "TEST", sizeof("TEST")-1) == 0) {
1243 if (op
> OP_MATCH_MAX
) {
1244 err(rules
->udev
, "invalid TEST operation\n");
1247 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("TEST")-1);
1249 mode
= strtol(attr
, NULL
, 8);
1250 rule_add_token(&rule_tmp
, TK_M_TEST
, op
, value
, &mode
);
1252 rule_add_token(&rule_tmp
, TK_M_TEST
, op
, value
, NULL
);
1258 if (strncasecmp(key
, "RUN", sizeof("RUN")-1) == 0) {
1261 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("RUN")-1);
1262 if (attr
!= NULL
&& strstr(attr
, "ignore_error"))
1264 rule_add_token(&rule_tmp
, TK_A_RUN
, op
, value
, &flag
);
1269 if (strcasecmp(key
, "WAIT_FOR") == 0 || strcasecmp(key
, "WAIT_FOR_SYSFS") == 0) {
1270 rule_add_token(&rule_tmp
, TK_M_WAITFOR
, 0, value
, NULL
);
1275 if (strcasecmp(key
, "LABEL") == 0) {
1276 rule_tmp
.rule
.rule
.label_off
= add_string(rules
, value
);
1281 if (strcasecmp(key
, "GOTO") == 0) {
1282 rule_add_token(&rule_tmp
, TK_A_GOTO
, 0, value
, NULL
);
1287 if (strncasecmp(key
, "NAME", sizeof("NAME")-1) == 0) {
1288 if (op
< OP_MATCH_MAX
) {
1289 rule_add_token(&rule_tmp
, TK_M_NAME
, op
, value
, NULL
);
1291 if (value
[0] == '\0')
1292 dbg(rules
->udev
, "name empty, node creation suppressed\n");
1293 rule_add_token(&rule_tmp
, TK_A_NAME
, op
, value
, NULL
);
1294 attr
= get_key_attribute(rules
->udev
, key
+ sizeof("NAME")-1);
1296 if (strstr(attr
, "all_partitions") != NULL
) {
1297 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1299 dbg(rules
->udev
, "creation of partition nodes requested\n");
1300 rule_add_token(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1302 if (strstr(attr
, "ignore_remove") != NULL
) {
1303 dbg(rules
->udev
, "remove event should be ignored\n");
1304 rule_add_token(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1311 if (strcasecmp(key
, "SYMLINK") == 0) {
1312 if (op
< OP_MATCH_MAX
)
1313 rule_add_token(&rule_tmp
, TK_M_DEVLINK
, op
, value
, NULL
);
1315 rule_add_token(&rule_tmp
, TK_A_DEVLINK
, op
, value
, NULL
);
1320 if (strcasecmp(key
, "OWNER") == 0) {
1324 uid
= strtoul(value
, &endptr
, 10);
1325 if (endptr
[0] == '\0') {
1326 rule_add_token(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1327 } else if (rules
->resolve_names
&& strchr("$%", value
[0]) == NULL
) {
1328 uid
= add_uid(rules
, value
);
1329 rule_add_token(&rule_tmp
, TK_A_OWNER_ID
, op
, NULL
, &uid
);
1331 rule_add_token(&rule_tmp
, TK_A_OWNER
, op
, value
, NULL
);
1337 if (strcasecmp(key
, "GROUP") == 0) {
1341 gid
= strtoul(value
, &endptr
, 10);
1342 if (endptr
[0] == '\0') {
1343 rule_add_token(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1344 } else if (rules
->resolve_names
&& strchr("$%", value
[0]) == NULL
) {
1345 gid
= add_gid(rules
, value
);
1346 rule_add_token(&rule_tmp
, TK_A_GROUP_ID
, op
, NULL
, &gid
);
1348 rule_add_token(&rule_tmp
, TK_A_GROUP
, op
, value
, NULL
);
1354 if (strcasecmp(key
, "MODE") == 0) {
1358 mode
= strtol(value
, &endptr
, 8);
1359 if (endptr
[0] == '\0')
1360 rule_add_token(&rule_tmp
, TK_A_MODE_ID
, op
, NULL
, &mode
);
1362 rule_add_token(&rule_tmp
, TK_A_MODE
, op
, value
, NULL
);
1367 if (strcasecmp(key
, "OPTIONS") == 0) {
1370 if (strstr(value
, "last_rule") != NULL
) {
1371 dbg(rules
->udev
, "last rule to be applied\n");
1372 rule_add_token(&rule_tmp
, TK_A_LAST_RULE
, 0, NULL
, NULL
);
1374 if (strstr(value
, "ignore_device") != NULL
) {
1375 dbg(rules
->udev
, "device should be ignored\n");
1376 rule_add_token(&rule_tmp
, TK_A_IGNORE_DEVICE
, 0, NULL
, NULL
);
1378 if (strstr(value
, "ignore_remove") != NULL
) {
1379 dbg(rules
->udev
, "remove event should be ignored\n");
1380 rule_add_token(&rule_tmp
, TK_A_IGNORE_REMOVE
, 0, NULL
, NULL
);
1382 pos
= strstr(value
, "link_priority=");
1384 int prio
= atoi(&pos
[strlen("link_priority=")]);
1386 rule_add_token(&rule_tmp
, TK_A_DEVLINK_PRIO
, 0, NULL
, &prio
);
1387 dbg(rules
->udev
, "link priority=%i\n", prio
);
1389 pos
= strstr(value
, "event_timeout=");
1391 int tout
= atoi(&pos
[strlen("event_timeout=")]);
1393 rule_add_token(&rule_tmp
, TK_A_EVENT_TIMEOUT
, 0, NULL
, &tout
);
1394 dbg(rules
->udev
, "event timout=%i\n", tout
);
1396 pos
= strstr(value
, "string_escape=");
1398 pos
= &pos
[strlen("string_escape=")];
1399 if (strncmp(pos
, "none", strlen("none")) == 0)
1400 rule_add_token(&rule_tmp
, TK_A_STRING_ESCAPE_NONE
, 0, NULL
, NULL
);
1401 else if (strncmp(pos
, "replace", strlen("replace")) == 0)
1402 rule_add_token(&rule_tmp
, TK_A_STRING_ESCAPE_REPLACE
, 0, NULL
, NULL
);
1404 if (strstr(value
, "all_partitions") != NULL
) {
1405 int num
= DEFAULT_FAKE_PARTITIONS_COUNT
;
1407 rule_add_token(&rule_tmp
, TK_A_NUM_FAKE_PART
, 0, NULL
, &num
);
1408 dbg(rules
->udev
, "creation of partition nodes requested\n");
1413 err(rules
->udev
, "unknown key '%s' in %s:%u\n", key
, filename
, lineno
);
1417 err(rules
->udev
, "PHYSDEV* values are deprecated and not available on recent kernels, \n"
1418 "please fix it in %s:%u", filename
, lineno
);
1420 /* skip line if not any valid key was found */
1424 /* add rule token */
1425 if (add_token(rules
, &rule_tmp
.rule
) != 0)
1428 /* add tokens to list, sorted by type */
1429 if (sort_token(rules
, &rule_tmp
) != 0)
1433 err(rules
->udev
, "invalid rule '%s:%u'\n", filename
, lineno
);
1437 static int parse_file(struct udev_rules
*rules
, const char *filename
)
1440 unsigned int filename_off
;
1441 unsigned int first_token
;
1442 char line
[UTIL_LINE_SIZE
];
1446 info(rules
->udev
, "reading '%s' as rules file\n", filename
);
1448 f
= fopen(filename
, "r");
1452 filename_off
= add_string(rules
, filename
);
1453 first_token
= rules
->token_cur
;
1455 while(fgets(line
, sizeof(line
), f
) != NULL
) {
1459 /* skip whitespace */
1462 while (isspace(key
[0]))
1473 /* continue reading if backslash+newline is found */
1474 while (line
[len
-2] == '\\') {
1475 if (fgets(&line
[len
-2], (sizeof(line
)-len
)+2, f
) == NULL
)
1481 if (len
+1 >= sizeof(line
)) {
1482 err(rules
->udev
, "line too long '%s':%u, ignored\n", filename
, line_nr
);
1485 add_rule(rules
, key
, filename
, filename_off
, line_nr
);
1489 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1490 for (i
= first_token
+1; i
< rules
->token_cur
; i
++) {
1491 if (rules
->tokens
[i
].type
== TK_A_GOTO
) {
1492 char *label
= &rules
->buf
[rules
->tokens
[i
].key
.value_off
];
1495 for (j
= i
+1; j
< rules
->token_cur
; j
++) {
1496 if (rules
->tokens
[j
].type
!= TK_RULE
)
1498 if (rules
->tokens
[j
].rule
.label_off
== 0)
1500 if (strcmp(label
, &rules
->buf
[rules
->tokens
[j
].rule
.label_off
]) != 0)
1502 rules
->tokens
[i
].key
.rule_goto
= j
;
1504 if (rules
->tokens
[i
].key
.rule_goto
== 0)
1505 err(rules
->udev
, "GOTO '%s' has no matching label in: '%s'\n", label
, filename
);
1511 static int add_matching_files(struct udev
*udev
, struct udev_list_node
*file_list
, const char *dirname
, const char *suffix
)
1515 char filename
[UTIL_PATH_SIZE
];
1517 dbg(udev
, "open directory '%s'\n", dirname
);
1518 dir
= opendir(dirname
);
1520 err(udev
, "unable to open '%s': %m\n", dirname
);
1526 if (ent
== NULL
|| ent
->d_name
[0] == '\0')
1529 if ((ent
->d_name
[0] == '.') || (ent
->d_name
[0] == '#'))
1532 /* look for file matching with specified suffix */
1533 if (suffix
!= NULL
) {
1536 ext
= strrchr(ent
->d_name
, '.');
1539 if (strcmp(ext
, suffix
) != 0)
1542 dbg(udev
, "put file '%s/%s' into list\n", dirname
, ent
->d_name
);
1544 snprintf(filename
, sizeof(filename
), "%s/%s", dirname
, ent
->d_name
);
1545 filename
[sizeof(filename
)-1] = '\0';
1546 udev_list_entry_add(udev
, file_list
, filename
, NULL
, 1, 1);
1553 struct udev_rules
*udev_rules_new(struct udev
*udev
, int resolve_names
)
1555 struct udev_rules
*rules
;
1556 struct stat statbuf
;
1557 char filename
[PATH_MAX
];
1558 struct udev_list_node file_list
;
1559 struct udev_list_entry
*file_loop
, *file_tmp
;
1560 unsigned int prev_rule
;
1561 struct token end_token
;
1564 rules
= malloc(sizeof(struct udev_rules
));
1567 memset(rules
, 0x00, sizeof(struct udev_rules
));
1569 rules
->resolve_names
= resolve_names
;
1570 udev_list_init(&file_list
);
1572 /* init token array and string buffer */
1573 rules
->tokens
= malloc(PREALLOC_TOKEN
* sizeof(struct token
));
1574 if (rules
->tokens
== NULL
)
1576 rules
->token_max
= PREALLOC_TOKEN
;
1577 rules
->buf
= malloc(PREALLOC_STRBUF
);
1578 if (rules
->buf
== NULL
)
1580 rules
->buf_max
= PREALLOC_STRBUF
;
1581 /* offset 0 is always '\0' */
1582 rules
->buf
[0] = '\0';
1584 info(udev
, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1585 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1587 if (udev_get_rules_path(udev
) != NULL
) {
1588 /* custom rules location for testing */
1589 add_matching_files(udev
, &file_list
, udev_get_rules_path(udev
), ".rules");
1591 struct udev_list_node sort_list
;
1592 struct udev_list_entry
*sort_loop
, *sort_tmp
;
1594 /* read user/custom rules */
1595 add_matching_files(udev
, &file_list
, SYSCONFDIR
"/udev/rules.d", ".rules");
1597 /* read dynamic/temporary rules */
1598 util_strlcpy(filename
, udev_get_dev_path(udev
), sizeof(filename
));
1599 util_strlcat(filename
, "/.udev/rules.d", sizeof(filename
));
1600 if (stat(filename
, &statbuf
) != 0) {
1601 util_create_path(udev
, filename
);
1602 udev_selinux_setfscreatecon(udev
, filename
, S_IFDIR
|0755);
1603 mkdir(filename
, 0755);
1604 udev_selinux_resetfscreatecon(udev
);
1606 udev_list_init(&sort_list
);
1607 add_matching_files(udev
, &sort_list
, filename
, ".rules");
1609 /* read default rules */
1610 add_matching_files(udev
, &sort_list
, UDEV_PREFIX
"/lib/udev/rules.d", ".rules");
1612 /* sort all rules files by basename into list of files */
1613 udev_list_entry_foreach_safe(sort_loop
, sort_tmp
, udev_list_get_entry(&sort_list
)) {
1614 const char *sort_name
= udev_list_entry_get_name(sort_loop
);
1615 const char *sort_base
= strrchr(sort_name
, '/');
1617 if (sort_base
== NULL
)
1620 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1621 const char *file_name
= udev_list_entry_get_name(file_loop
);
1622 const char *file_base
= strrchr(file_name
, '/');
1624 if (file_base
== NULL
)
1626 if (strcmp(file_base
, sort_base
) == 0) {
1627 info(udev
, "rule file basename '%s' already added, ignoring '%s'\n",
1628 file_name
, sort_name
);
1629 udev_list_entry_remove(sort_loop
);
1633 if (strcmp(file_base
, sort_base
) > 0)
1636 if (sort_loop
!= NULL
)
1637 udev_list_entry_move_before(sort_loop
, file_loop
);
1641 /* parse list of files */
1642 udev_list_entry_foreach_safe(file_loop
, file_tmp
, udev_list_get_entry(&file_list
)) {
1643 const char *file_name
= udev_list_entry_get_name(file_loop
);
1645 if (stat(file_name
, &statbuf
) == 0 && statbuf
.st_size
> 0)
1646 parse_file(rules
, file_name
);
1648 info(udev
, "can not read '%s'\n", file_name
);
1649 udev_list_entry_remove(file_loop
);
1652 memset(&end_token
, 0x00, sizeof(struct token
));
1653 end_token
.type
= TK_END
;
1654 add_token(rules
, &end_token
);
1656 /* link all TK_RULE tokens to be able to fast-forward to next TK_RULE */
1658 for (i
= 1; i
< rules
->token_cur
; i
++) {
1659 if (rules
->tokens
[i
].type
== TK_RULE
) {
1660 rules
->tokens
[prev_rule
].rule
.next_rule
= i
;
1665 /* shrink allocated token and string buffer */
1666 if (rules
->token_cur
< rules
->token_max
) {
1667 struct token
*tokens
;
1669 tokens
= realloc(rules
->tokens
, rules
->token_cur
* sizeof(struct token
));
1670 if (tokens
!= NULL
|| rules
->token_cur
== 0) {
1671 rules
->tokens
= tokens
;
1672 rules
->token_max
= rules
->token_cur
;
1675 if (rules
->buf_cur
< rules
->buf_max
) {
1678 buf
= realloc(rules
->buf
, rules
->buf_cur
);
1679 if (buf
!= NULL
|| rules
->buf_cur
== 0) {
1681 rules
->buf_max
= rules
->buf_cur
;
1684 info(udev
, "shrunk to %lu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
1685 rules
->token_max
* sizeof(struct token
), rules
->token_max
, sizeof(struct token
), rules
->buf_max
);
1687 /* cleanup uid/gid cache */
1690 rules
->uids_cur
= 0;
1691 rules
->uids_max
= 0;
1694 rules
->gids_cur
= 0;
1695 rules
->gids_max
= 0;
1701 void udev_rules_unref(struct udev_rules
*rules
)
1705 free(rules
->tokens
);
1712 static int match_key(struct udev_rules
*rules
, struct token
*token
, const char *val
)
1714 char *key_value
= &rules
->buf
[token
->key
.value_off
];
1721 switch (token
->key
.glob
) {
1723 match
= (strcmp(key_value
, val
) == 0);
1726 match
= (fnmatch(key_value
, val
, 0) == 0);
1733 split
= &rules
->buf
[token
->key
.value_off
];
1738 next
= strchr(split
, '|');
1740 size_t matchlen
= (size_t)(next
- split
);
1742 match
= (matchlen
== len
&& strncmp(split
, val
, matchlen
) == 0);
1746 match
= (strcmp(split
, val
) == 0);
1755 char value
[UTIL_PATH_SIZE
];
1757 util_strlcpy(value
, &rules
->buf
[token
->key
.value_off
], sizeof(value
));
1759 while (key_value
!= NULL
) {
1760 pos
= strchr(key_value
, '|');
1765 dbg(rules
->udev
, "match %s '%s' <-> '%s'\n", token_str
[token
->type
], key_value
, val
);
1766 match
= (fnmatch(key_value
, val
, 0) == 0);
1774 match
= (val
[0] != '\0');
1781 if (match
&& (token
->key
.op
== OP_MATCH
)) {
1782 dbg(rules
->udev
, "%s is true (matching value)\n", token_str
[token
->type
]);
1785 if (!match
&& (token
->key
.op
== OP_NOMATCH
)) {
1786 dbg(rules
->udev
, "%s is true (non-matching value)\n", token_str
[token
->type
]);
1789 dbg(rules
->udev
, "%s is not true\n", token_str
[token
->type
]);
1793 static int match_attr(struct udev_rules
*rules
, struct udev_device
*dev
, struct udev_event
*event
, struct token
*cur
)
1795 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
1796 const char *key_value
= &rules
->buf
[cur
->key
.value_off
];
1797 char value
[UTIL_NAME_SIZE
];
1801 if (key_name
[0] == '[') {
1802 char attr
[UTIL_PATH_SIZE
];
1804 util_strlcpy(attr
, key_name
, sizeof(attr
));
1805 util_resolve_subsys_kernel(event
->udev
, attr
, value
, sizeof(value
), 1);
1807 if (value
[0] == '\0') {
1810 val
= udev_device_get_sysattr_value(dev
, key_name
);
1812 util_strlcpy(value
, val
, sizeof(value
));
1814 if (value
[0] == '\0')
1817 /* strip trailing whitespace of value, if not asked to match for it */
1818 len
= strlen(key_value
);
1819 if (len
> 0 && !isspace(key_value
[len
-1])) {
1820 len
= strlen(value
);
1821 while (len
> 0 && isspace(value
[--len
]))
1823 dbg(rules
->udev
, "removed trailing whitespace from '%s'\n", value
);
1825 return match_key(rules
, cur
, value
);
1834 int udev_rules_apply_to_event(struct udev_rules
*rules
, struct udev_event
*event
)
1838 enum escape_type esc
= ESCAPE_UNSET
;
1840 if (rules
->tokens
== NULL
)
1843 /* loop through token list, match, run actions or forward to next rule */
1844 cur
= &rules
->tokens
[0];
1849 dump_token(rules
, cur
);
1850 switch (cur
->type
) {
1857 if (match_key(rules
, cur
, udev_device_get_action(event
->dev
)) != 0)
1861 if (match_key(rules
, cur
, udev_device_get_devpath(event
->dev
)) != 0)
1865 if (match_key(rules
, cur
, udev_device_get_sysname(event
->dev
)) != 0)
1870 size_t devlen
= strlen(udev_get_dev_path(event
->udev
))+1;
1871 struct udev_list_entry
*list_entry
;
1874 udev_list_entry_foreach(list_entry
, udev_device_get_devlinks_list_entry(event
->dev
)) {
1875 const char *devlink
;
1877 devlink
= &udev_list_entry_get_name(list_entry
)[devlen
];
1878 if (match_key(rules
, cur
, devlink
) == 0) {
1888 if (match_key(rules
, cur
, event
->name
) != 0)
1893 struct udev_list_entry
*list_entry
;
1894 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
1897 list_entry
= udev_device_get_properties_list_entry(event
->dev
);
1898 list_entry
= udev_list_entry_get_by_name(list_entry
, key_name
);
1899 value
= udev_list_entry_get_value(list_entry
);
1900 if (value
== NULL
) {
1901 dbg(event
->udev
, "ENV{%s} is not set, treat as empty\n", key_name
);
1904 if (match_key(rules
, cur
, value
))
1908 case TK_M_SUBSYSTEM
:
1909 if (match_key(rules
, cur
, udev_device_get_subsystem(event
->dev
)) != 0)
1913 if (match_key(rules
, cur
, udev_device_get_driver(event
->dev
)) != 0)
1918 char filename
[UTIL_PATH_SIZE
];
1921 util_strlcpy(filename
, &rules
->buf
[cur
->key
.value_off
], sizeof(filename
));
1922 udev_event_apply_format(event
, filename
, sizeof(filename
));
1923 found
= (wait_for_file(event
->dev
, filename
, 10) == 0);
1924 if (!found
&& (cur
->key
.op
!= OP_NOMATCH
))
1929 if (match_attr(rules
, event
->dev
, event
, cur
) != 0)
1933 case TK_M_SUBSYSTEMS
:
1939 /* get whole sequence of parent matches */
1941 while (next
->type
< TK_M_PARENTS_MAX
)
1944 /* loop over parents */
1945 event
->dev_parent
= event
->dev
;
1949 dbg(event
->udev
, "parent: '%s'\n", udev_device_get_syspath(event
->dev_parent
));
1950 /* loop over sequence of parent match keys */
1951 for (key
= cur
; key
< next
; key
++ ) {
1952 dump_token(rules
, key
);
1955 if (match_key(rules
, key
, udev_device_get_sysname(event
->dev_parent
)) != 0)
1958 case TK_M_SUBSYSTEMS
:
1959 if (match_key(rules
, key
, udev_device_get_subsystem(event
->dev_parent
)) != 0)
1963 if (match_key(rules
, key
, udev_device_get_driver(event
->dev_parent
)) != 0)
1967 if (match_attr(rules
, event
->dev_parent
, event
, key
) != 0)
1973 dbg(event
->udev
, "parent key matched\n");
1975 dbg(event
->udev
, "all parent keys matched\n");
1976 /* all keys matched */
1980 event
->dev_parent
= udev_device_get_parent(event
->dev_parent
);
1981 if (event
->dev_parent
== NULL
)
1984 /* move behind our sequence of parent match keys */
1990 char filename
[UTIL_PATH_SIZE
];
1991 struct stat statbuf
;
1994 util_strlcpy(filename
, &rules
->buf
[cur
->key
.value_off
], sizeof(filename
));
1995 udev_event_apply_format(event
, filename
, sizeof(filename
));
1996 if (util_resolve_subsys_kernel(event
->udev
, NULL
, filename
, sizeof(filename
), 0) != 0)
1997 if (filename
[0] != '/') {
1998 char tmp
[UTIL_PATH_SIZE
];
2000 util_strlcpy(tmp
, udev_device_get_syspath(event
->dev
), sizeof(tmp
));
2001 util_strlcat(tmp
, "/", sizeof(tmp
));
2002 util_strlcat(tmp
, filename
, sizeof(tmp
));
2003 util_strlcpy(filename
, tmp
, sizeof(filename
));
2006 attr_subst_subdir(filename
, sizeof(filename
));
2008 match
= (stat(filename
, &statbuf
) == 0);
2009 info(event
->udev
, "'%s' %s", filename
, match
? "exists\n" : "does not exist\n");
2010 if (match
&& cur
->key
.mode
> 0) {
2011 match
= ((statbuf
.st_mode
& cur
->key
.mode
) > 0);
2012 info(event
->udev
, "'%s' has mode=%#o and %s %#o\n", filename
, statbuf
.st_mode
,
2013 match
? "matches" : "does not match", cur
->key
.mode
);
2015 if (match
&& cur
->key
.op
== OP_NOMATCH
)
2017 if (!match
&& cur
->key
.op
== OP_MATCH
)
2023 char program
[UTIL_PATH_SIZE
];
2025 char result
[UTIL_PATH_SIZE
];
2027 free(event
->program_result
);
2028 event
->program_result
= NULL
;
2029 util_strlcpy(program
, &rules
->buf
[cur
->key
.value_off
], sizeof(program
));
2030 udev_event_apply_format(event
, program
, sizeof(program
));
2031 envp
= udev_device_get_properties_envp(event
->dev
);
2032 if (util_run_program(event
->udev
, program
, envp
, result
, sizeof(result
), NULL
) != 0) {
2033 if (cur
->key
.op
!= OP_NOMATCH
)
2038 util_remove_trailing_chars(result
, '\n');
2039 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2040 count
= util_replace_chars(result
, ALLOWED_CHARS_INPUT
);
2042 info(event
->udev
, "%i character(s) replaced\n" , count
);
2044 event
->program_result
= strdup(result
);
2045 dbg(event
->udev
, "storing result '%s'\n", event
->program_result
);
2046 if (cur
->key
.op
== OP_NOMATCH
)
2051 case TK_M_IMPORT_FILE
:
2053 char import
[UTIL_PATH_SIZE
];
2055 util_strlcpy(import
, &rules
->buf
[cur
->key
.value_off
], sizeof(import
));
2056 udev_event_apply_format(event
, import
, sizeof(import
));
2057 if (import_file_into_properties(event
->dev
, import
) != 0)
2058 if (cur
->key
.op
!= OP_NOMATCH
)
2062 case TK_M_IMPORT_PROG
:
2064 char import
[UTIL_PATH_SIZE
];
2066 util_strlcpy(import
, &rules
->buf
[cur
->key
.value_off
], sizeof(import
));
2067 udev_event_apply_format(event
, import
, sizeof(import
));
2068 if (import_program_into_properties(event
->dev
, import
) != 0)
2069 if (cur
->key
.op
!= OP_NOMATCH
)
2073 case TK_M_IMPORT_PARENT
:
2075 char import
[UTIL_PATH_SIZE
];
2077 util_strlcpy(import
, &rules
->buf
[cur
->key
.value_off
], sizeof(import
));
2078 udev_event_apply_format(event
, import
, sizeof(import
));
2079 if (import_parent_into_properties(event
->dev
, import
) != 0)
2080 if (cur
->key
.op
!= OP_NOMATCH
)
2085 if (match_key(rules
, cur
, event
->program_result
) != 0)
2089 case TK_A_IGNORE_DEVICE
:
2090 event
->ignore_device
= 1;
2093 case TK_A_STRING_ESCAPE_NONE
:
2096 case TK_A_STRING_ESCAPE_REPLACE
:
2097 esc
= ESCAPE_REPLACE
;
2099 case TK_A_NUM_FAKE_PART
:
2100 if (strcmp(udev_device_get_subsystem(event
->dev
), "block") != 0)
2102 if (udev_device_get_sysnum(event
->dev
) != NULL
)
2104 udev_device_set_num_fake_partitions(event
->dev
, cur
->key
.num_fake_part
);
2106 case TK_A_DEVLINK_PRIO
:
2107 udev_device_set_devlink_priority(event
->dev
, cur
->key
.devlink_prio
);
2111 char owner
[UTIL_NAME_SIZE
];
2113 if (event
->owner_final
)
2115 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2116 event
->owner_final
= 1;
2117 util_strlcpy(owner
, &rules
->buf
[cur
->key
.value_off
], sizeof(owner
));
2118 udev_event_apply_format(event
, owner
, sizeof(owner
));
2119 event
->uid
= util_lookup_user(event
->udev
, owner
);
2124 char group
[UTIL_NAME_SIZE
];
2126 if (event
->group_final
)
2128 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2129 event
->group_final
= 1;
2130 util_strlcpy(group
, &rules
->buf
[cur
->key
.value_off
], sizeof(group
));
2131 udev_event_apply_format(event
, group
, sizeof(group
));
2132 event
->gid
= util_lookup_group(event
->udev
, group
);
2137 char mode
[UTIL_NAME_SIZE
];
2140 if (event
->mode_final
)
2142 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2143 event
->mode_final
= 1;
2144 util_strlcpy(mode
, &rules
->buf
[cur
->key
.value_off
], sizeof(mode
));
2145 udev_event_apply_format(event
, mode
, sizeof(mode
));
2146 event
->mode
= strtol(mode
, &endptr
, 8);
2147 if (endptr
[0] != '\0') {
2148 err(event
->udev
, "invalide mode '%s' set default mode 0660\n", mode
);
2154 if (event
->owner_final
)
2156 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2157 event
->owner_final
= 1;
2158 event
->uid
= cur
->key
.uid
;
2161 if (event
->group_final
)
2163 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2164 event
->group_final
= 1;
2165 event
->gid
= cur
->key
.gid
;
2168 if (event
->mode_final
)
2170 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2171 event
->mode_final
= 1;
2172 event
->mode
= cur
->key
.mode
;
2176 const char *name
= &rules
->buf
[cur
->key
.attr_off
];
2177 char *value
= &rules
->buf
[cur
->key
.value_off
];
2179 if (value
[0] != '\0') {
2180 char temp_value
[UTIL_NAME_SIZE
];
2181 struct udev_list_entry
*entry
;
2183 util_strlcpy(temp_value
, value
, sizeof(temp_value
));
2184 udev_event_apply_format(event
, temp_value
, sizeof(temp_value
));
2185 entry
= udev_device_add_property(event
->dev
, name
, temp_value
);
2187 udev_list_entry_set_flag(entry
, 1);
2189 udev_device_add_property(event
->dev
, name
, NULL
);
2195 const char *name
= &rules
->buf
[cur
->key
.value_off
];
2196 char name_str
[UTIL_PATH_SIZE
];
2199 if (event
->name_final
)
2201 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2202 event
->name_final
= 1;
2203 if (name
[0] == '\0') {
2208 util_strlcpy(name_str
, name
, sizeof(name_str
));
2209 udev_event_apply_format(event
, name_str
, sizeof(name_str
));
2210 if (esc
== ESCAPE_UNSET
|| esc
== ESCAPE_REPLACE
) {
2211 count
= util_replace_chars(name_str
, ALLOWED_CHARS_FILE
);
2213 info(event
->udev
, "%i character(s) replaced\n", count
);
2215 event
->name
= strdup(name_str
);
2221 char temp
[UTIL_PATH_SIZE
];
2222 char filename
[UTIL_PATH_SIZE
];
2226 if (event
->devlink_final
)
2228 if (major(udev_device_get_devnum(event
->dev
)) == 0)
2230 if (cur
->key
.op
== OP_ASSIGN_FINAL
)
2231 event
->devlink_final
= 1;
2232 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2233 udev_device_cleanup_devlinks_list(event
->dev
);
2235 /* allow multiple symlinks separated by spaces */
2236 util_strlcpy(temp
, &rules
->buf
[cur
->key
.value_off
], sizeof(temp
));
2237 udev_event_apply_format(event
, temp
, sizeof(temp
));
2238 if (esc
== ESCAPE_UNSET
)
2239 count
= util_replace_chars(temp
, ALLOWED_CHARS_FILE
" ");
2240 else if (esc
== ESCAPE_REPLACE
)
2241 count
= util_replace_chars(temp
, ALLOWED_CHARS_FILE
);
2243 info(event
->udev
, "%i character(s) replaced\n" , count
);
2244 dbg(event
->udev
, "rule applied, added symlink(s) '%s'\n", temp
);
2246 while (isspace(pos
[0]))
2248 next
= strchr(pos
, ' ');
2251 info(event
->udev
, "add symlink '%s'\n", pos
);
2252 util_strlcpy(filename
, udev_get_dev_path(event
->udev
), sizeof(filename
));
2253 util_strlcat(filename
, "/", sizeof(filename
));
2254 util_strlcat(filename
, pos
, sizeof(filename
));
2255 udev_device_add_devlink(event
->dev
, filename
);
2256 while (isspace(next
[1]))
2259 next
= strchr(pos
, ' ');
2261 if (pos
[0] != '\0') {
2262 info(event
->udev
, "add symlink '%s'\n", pos
);
2263 util_strlcpy(filename
, udev_get_dev_path(event
->udev
), sizeof(filename
));
2264 util_strlcat(filename
, "/", sizeof(filename
));
2265 util_strlcat(filename
, pos
, sizeof(filename
));
2266 udev_device_add_devlink(event
->dev
, filename
);
2270 case TK_A_EVENT_TIMEOUT
:
2271 udev_device_set_event_timeout(event
->dev
, cur
->key
.event_timeout
);
2273 case TK_A_IGNORE_REMOVE
:
2274 udev_device_set_ignore_remove(event
->dev
, 1);
2278 const char *key_name
= &rules
->buf
[cur
->key
.attr_off
];
2279 char attr
[UTIL_PATH_SIZE
];
2280 char value
[UTIL_NAME_SIZE
];
2283 util_strlcpy(attr
, key_name
, sizeof(attr
));
2284 if (util_resolve_subsys_kernel(event
->udev
, key_name
, attr
, sizeof(attr
), 0) != 0) {
2285 util_strlcpy(attr
, udev_device_get_syspath(event
->dev
), sizeof(attr
));
2286 util_strlcat(attr
, "/", sizeof(attr
));
2287 util_strlcat(attr
, key_name
, sizeof(attr
));
2290 attr_subst_subdir(attr
, sizeof(attr
));
2292 util_strlcpy(value
, &rules
->buf
[cur
->key
.value_off
], sizeof(value
));
2293 udev_event_apply_format(event
, value
, sizeof(value
));
2294 info(event
->udev
, "writing '%s' to sysfs file '%s'\n", value
, attr
);
2295 f
= fopen(attr
, "w");
2298 if (fprintf(f
, "%s", value
) <= 0)
2299 err(event
->udev
, "error writing ATTR{%s}: %m\n", attr
);
2302 err(event
->udev
, "error opening ATTR{%s} for writing: %m\n", attr
);
2308 struct udev_list_entry
*list_entry
;
2310 if (cur
->key
.op
== OP_ASSIGN
|| cur
->key
.op
== OP_ASSIGN_FINAL
)
2311 udev_list_cleanup_entries(event
->udev
, &event
->run_list
);
2312 list_entry
= udev_list_entry_add(event
->udev
, &event
->run_list
,
2313 &rules
->buf
[cur
->key
.value_off
], NULL
, 1, 0);
2314 if (cur
->key
.ignore_error
)
2315 udev_list_entry_set_flag(list_entry
, 1);
2319 cur
= &rules
->tokens
[cur
->key
.rule_goto
];
2321 case TK_A_LAST_RULE
:
2325 case TK_M_PARENTS_MAX
:
2328 err(rules
->udev
, "wrong type %u\n", cur
->type
);
2335 /* fast-forward to next rule */
2336 idx
= rule
->rule
.next_rule
;
2339 dbg(rules
->udev
, "forward to rule: %u\n", idx
);
2340 cur
= &rules
->tokens
[idx
];