]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-rules.c
udevd: add --event-timeout commandline option
[thirdparty/systemd.git] / src / udev / udev-rules.c
CommitLineData
2232cac8 1/*
1298001e 2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
2232cac8 3 *
55e9959b
KS
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.
3d6d12c6 8 *
55e9959b
KS
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.
2232cac8 13 *
55e9959b
KS
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/>.
2232cac8
GKH
16 */
17
18#include <stddef.h>
1449a55d 19#include <limits.h>
2232cac8 20#include <stdlib.h>
6d87ee2e 21#include <stdbool.h>
2232cac8
GKH
22#include <string.h>
23#include <stdio.h>
24#include <fcntl.h>
25#include <ctype.h>
26#include <unistd.h>
27#include <errno.h>
5ee7ecfb 28#include <dirent.h>
cea61f5c 29#include <fnmatch.h>
959e8b5d 30#include <time.h>
2232cac8 31
2232cac8 32#include "udev.h"
9eb977db 33#include "path-util.h"
2c21044f 34#include "conf-files.h"
915bf0f6 35#include "strbuf.h"
84b6ad70 36#include "strv.h"
1291bc89 37#include "util.h"
2232cac8 38
a1525d17 39#define PREALLOC_TOKEN 2048
c7521974 40
4052400f 41struct uid_gid {
912541b0
KS
42 unsigned int name_off;
43 union {
a1525d17
KS
44 uid_t uid;
45 gid_t gid;
912541b0 46 };
4052400f
KS
47};
48
2ad8416d
ZJS
49static const char* const rules_dirs[] = {
50 "/etc/udev/rules.d",
51 "/run/udev/rules.d",
52 UDEVLIBEXECDIR "/rules.d",
53 NULL};
54
4052400f 55struct udev_rules {
912541b0 56 struct udev *udev;
97f2d76d 57 usec_t dirs_ts_usec;
912541b0
KS
58 int resolve_names;
59
60 /* every key in the rules file becomes a token */
61 struct token *tokens;
62 unsigned int token_cur;
63 unsigned int token_max;
64
ab06eef8 65 /* all key strings are copied and de-duplicated in a single continuous string buffer */
915bf0f6 66 struct strbuf *strbuf;
912541b0
KS
67
68 /* during rule parsing, uid/gid lookup results are cached */
69 struct uid_gid *uids;
70 unsigned int uids_cur;
71 unsigned int uids_max;
72 struct uid_gid *gids;
73 unsigned int gids_cur;
74 unsigned int gids_max;
4052400f
KS
75};
76
915bf0f6
KS
77static char *rules_str(struct udev_rules *rules, unsigned int off) {
78 return rules->strbuf->buf + off;
79}
80
81static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
82 return strbuf_add_string(rules->strbuf, s, strlen(s));
83}
84
1449a55d 85/* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
b0f7409f 86enum operation_type {
912541b0 87 OP_UNSET,
b0f7409f 88
912541b0
KS
89 OP_MATCH,
90 OP_NOMATCH,
91 OP_MATCH_MAX,
b0f7409f 92
912541b0
KS
93 OP_ADD,
94 OP_ASSIGN,
95 OP_ASSIGN_FINAL,
c7521974
KS
96};
97
ac218d9c 98enum string_glob_type {
912541b0 99 GL_UNSET,
a1525d17 100 GL_PLAIN, /* no special chars */
912541b0 101 GL_GLOB, /* shell globs ?,*,[] */
a1525d17
KS
102 GL_SPLIT, /* multi-value A|B */
103 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
104 GL_SOMETHING, /* commonly used "?*" */
ac218d9c
KS
105};
106
32028733 107enum string_subst_type {
912541b0
KS
108 SB_UNSET,
109 SB_NONE,
110 SB_FORMAT,
111 SB_SUBSYS,
32028733
KS
112};
113
154a7b84 114/* tokens of a rule are sorted/handled in this order */
6880b25d 115enum token_type {
912541b0
KS
116 TK_UNSET,
117 TK_RULE,
118
a1525d17
KS
119 TK_M_ACTION, /* val */
120 TK_M_DEVPATH, /* val */
121 TK_M_KERNEL, /* val */
122 TK_M_DEVLINK, /* val */
123 TK_M_NAME, /* val */
124 TK_M_ENV, /* val, attr */
125 TK_M_TAG, /* val */
126 TK_M_SUBSYSTEM, /* val */
127 TK_M_DRIVER, /* val */
128 TK_M_WAITFOR, /* val */
129 TK_M_ATTR, /* val, attr */
912541b0
KS
130
131 TK_M_PARENTS_MIN,
a1525d17 132 TK_M_KERNELS, /* val */
912541b0 133 TK_M_SUBSYSTEMS, /* val */
a1525d17
KS
134 TK_M_DRIVERS, /* val */
135 TK_M_ATTRS, /* val, attr */
136 TK_M_TAGS, /* val */
912541b0
KS
137 TK_M_PARENTS_MAX,
138
a1525d17
KS
139 TK_M_TEST, /* val, mode_t */
140 TK_M_EVENT_TIMEOUT, /* int */
141 TK_M_PROGRAM, /* val */
142 TK_M_IMPORT_FILE, /* val */
143 TK_M_IMPORT_PROG, /* val */
144 TK_M_IMPORT_BUILTIN, /* val */
145 TK_M_IMPORT_DB, /* val */
146 TK_M_IMPORT_CMDLINE, /* val */
147 TK_M_IMPORT_PARENT, /* val */
148 TK_M_RESULT, /* val */
912541b0
KS
149 TK_M_MAX,
150
151 TK_A_STRING_ESCAPE_NONE,
152 TK_A_STRING_ESCAPE_REPLACE,
153 TK_A_DB_PERSIST,
a1525d17
KS
154 TK_A_INOTIFY_WATCH, /* int */
155 TK_A_DEVLINK_PRIO, /* int */
156 TK_A_OWNER, /* val */
157 TK_A_GROUP, /* val */
158 TK_A_MODE, /* val */
159 TK_A_OWNER_ID, /* uid_t */
160 TK_A_GROUP_ID, /* gid_t */
161 TK_A_MODE_ID, /* mode_t */
84b6ad70 162 TK_A_TAG, /* val */
a1525d17 163 TK_A_STATIC_NODE, /* val */
c26547d6 164 TK_A_SECLABEL, /* val, attr */
a1525d17 165 TK_A_ENV, /* val, attr */
a1525d17
KS
166 TK_A_NAME, /* val */
167 TK_A_DEVLINK, /* val */
168 TK_A_ATTR, /* val, attr */
83cd6b75
KS
169 TK_A_RUN_BUILTIN, /* val, bool */
170 TK_A_RUN_PROGRAM, /* val, bool */
a1525d17 171 TK_A_GOTO, /* size_t */
912541b0
KS
172
173 TK_END,
c7521974
KS
174};
175
3f3aa9f5 176/* we try to pack stuff in a way that we take only 12 bytes per token */
4052400f 177struct token {
912541b0
KS
178 union {
179 unsigned char type; /* same in rule and key */
180 struct {
181 enum token_type type:8;
182 bool can_set_name:1;
183 bool has_static_node:1;
184 unsigned int unused:6;
185 unsigned short token_count;
186 unsigned int label_off;
187 unsigned short filename_off;
188 unsigned short filename_line;
189 } rule;
190 struct {
191 enum token_type type:8;
192 enum operation_type op:8;
193 enum string_glob_type glob:8;
194 enum string_subst_type subst:4;
195 enum string_subst_type attrsubst:4;
196 unsigned int value_off;
197 union {
198 unsigned int attr_off;
912541b0
KS
199 unsigned int rule_goto;
200 mode_t mode;
201 uid_t uid;
202 gid_t gid;
203 int devlink_prio;
204 int event_timeout;
205 int watch;
206 enum udev_builtin_cmd builtin_cmd;
207 };
208 } key;
209 };
4052400f
KS
210};
211
912541b0 212#define MAX_TK 64
4052400f 213struct rule_tmp {
912541b0
KS
214 struct udev_rules *rules;
215 struct token rule;
216 struct token token[MAX_TK];
217 unsigned int token_cur;
4052400f
KS
218};
219
baa30fbc 220#ifdef DEBUG
5d6a1fa6
KS
221static const char *operation_str(enum operation_type type)
222{
912541b0 223 static const char *operation_strs[] = {
a1525d17
KS
224 [OP_UNSET] = "UNSET",
225 [OP_MATCH] = "match",
226 [OP_NOMATCH] = "nomatch",
912541b0
KS
227 [OP_MATCH_MAX] = "MATCH_MAX",
228
a1525d17
KS
229 [OP_ADD] = "add",
230 [OP_ASSIGN] = "assign",
231 [OP_ASSIGN_FINAL] = "assign-final",
912541b0
KS
232} ;
233
234 return operation_strs[type];
5d6a1fa6 235}
4052400f 236
5d6a1fa6
KS
237static const char *string_glob_str(enum string_glob_type type)
238{
912541b0 239 static const char *string_glob_strs[] = {
a1525d17
KS
240 [GL_UNSET] = "UNSET",
241 [GL_PLAIN] = "plain",
242 [GL_GLOB] = "glob",
243 [GL_SPLIT] = "split",
244 [GL_SPLIT_GLOB] = "split-glob",
245 [GL_SOMETHING] = "split-glob",
912541b0
KS
246 };
247
248 return string_glob_strs[type];
5d6a1fa6
KS
249}
250
251static const char *token_str(enum token_type type)
252{
912541b0 253 static const char *token_strs[] = {
a1525d17
KS
254 [TK_UNSET] = "UNSET",
255 [TK_RULE] = "RULE",
912541b0 256
a1525d17 257 [TK_M_ACTION] = "M ACTION",
912541b0 258 [TK_M_DEVPATH] = "M DEVPATH",
a1525d17 259 [TK_M_KERNEL] = "M KERNEL",
912541b0 260 [TK_M_DEVLINK] = "M DEVLINK",
a1525d17
KS
261 [TK_M_NAME] = "M NAME",
262 [TK_M_ENV] = "M ENV",
263 [TK_M_TAG] = "M TAG",
264 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
265 [TK_M_DRIVER] = "M DRIVER",
912541b0 266 [TK_M_WAITFOR] = "M WAITFOR",
a1525d17 267 [TK_M_ATTR] = "M ATTR",
912541b0 268
a1525d17 269 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
912541b0 270 [TK_M_KERNELS] = "M KERNELS",
a1525d17 271 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
912541b0 272 [TK_M_DRIVERS] = "M DRIVERS",
a1525d17
KS
273 [TK_M_ATTRS] = "M ATTRS",
274 [TK_M_TAGS] = "M TAGS",
275 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
912541b0 276
a1525d17
KS
277 [TK_M_TEST] = "M TEST",
278 [TK_M_EVENT_TIMEOUT] = "M EVENT_TIMEOUT",
912541b0 279 [TK_M_PROGRAM] = "M PROGRAM",
a1525d17
KS
280 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
281 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
282 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
283 [TK_M_IMPORT_DB] = "M IMPORT_DB",
284 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
285 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
286 [TK_M_RESULT] = "M RESULT",
287 [TK_M_MAX] = "M MAX",
288
289 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
290 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
291 [TK_A_DB_PERSIST] = "A DB_PERSIST",
292 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
293 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
294 [TK_A_OWNER] = "A OWNER",
295 [TK_A_GROUP] = "A GROUP",
296 [TK_A_MODE] = "A MODE",
297 [TK_A_OWNER_ID] = "A OWNER_ID",
298 [TK_A_GROUP_ID] = "A GROUP_ID",
299 [TK_A_STATIC_NODE] = "A STATIC_NODE",
c26547d6 300 [TK_A_SECLABEL] = "A SECLABEL",
912541b0 301 [TK_A_MODE_ID] = "A MODE_ID",
a1525d17
KS
302 [TK_A_ENV] = "A ENV",
303 [TK_A_TAG] = "A ENV",
304 [TK_A_NAME] = "A NAME",
912541b0 305 [TK_A_DEVLINK] = "A DEVLINK",
a1525d17 306 [TK_A_ATTR] = "A ATTR",
83cd6b75
KS
307 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
308 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
a1525d17 309 [TK_A_GOTO] = "A GOTO",
912541b0 310
a1525d17 311 [TK_END] = "END",
912541b0
KS
312 };
313
314 return token_strs[type];
5d6a1fa6 315}
c7521974 316
4052400f
KS
317static void dump_token(struct udev_rules *rules, struct token *token)
318{
912541b0
KS
319 enum token_type type = token->type;
320 enum operation_type op = token->key.op;
321 enum string_glob_type glob = token->key.glob;
915bf0f6 322 const char *value = str(rules, token->key.value_off);
912541b0
KS
323 const char *attr = &rules->buf[token->key.attr_off];
324
325 switch (type) {
326 case TK_RULE:
327 {
328 const char *tks_ptr = (char *)rules->tokens;
329 const char *tk_ptr = (char *)token;
330 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
331
9f6445e3 332 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
baa30fbc
KS
333 &rules->buf[token->rule.filename_off], token->rule.filename_line,
334 idx, token->rule.token_count,
335 &rules->buf[token->rule.label_off]);
912541b0
KS
336 break;
337 }
338 case TK_M_ACTION:
339 case TK_M_DEVPATH:
340 case TK_M_KERNEL:
341 case TK_M_SUBSYSTEM:
342 case TK_M_DRIVER:
343 case TK_M_WAITFOR:
344 case TK_M_DEVLINK:
345 case TK_M_NAME:
346 case TK_M_KERNELS:
347 case TK_M_SUBSYSTEMS:
348 case TK_M_DRIVERS:
349 case TK_M_TAGS:
350 case TK_M_PROGRAM:
351 case TK_M_IMPORT_FILE:
352 case TK_M_IMPORT_PROG:
353 case TK_M_IMPORT_DB:
354 case TK_M_IMPORT_CMDLINE:
355 case TK_M_IMPORT_PARENT:
356 case TK_M_RESULT:
357 case TK_A_NAME:
358 case TK_A_DEVLINK:
359 case TK_A_OWNER:
360 case TK_A_GROUP:
361 case TK_A_MODE:
83cd6b75
KS
362 case TK_A_RUN_BUILTIN:
363 case TK_A_RUN_PROGRAM:
9f6445e3 364 log_debug("%s %s '%s'(%s)",
baa30fbc 365 token_str(type), operation_str(op), value, string_glob_str(glob));
912541b0
KS
366 break;
367 case TK_M_IMPORT_BUILTIN:
9f6445e3 368 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
912541b0
KS
369 break;
370 case TK_M_ATTR:
371 case TK_M_ATTRS:
372 case TK_M_ENV:
373 case TK_A_ATTR:
374 case TK_A_ENV:
9f6445e3 375 log_debug("%s %s '%s' '%s'(%s)",
baa30fbc 376 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
912541b0
KS
377 break;
378 case TK_M_TAG:
379 case TK_A_TAG:
9f6445e3 380 log_debug("%s %s '%s'", token_str(type), operation_str(op), value);
912541b0
KS
381 break;
382 case TK_A_STRING_ESCAPE_NONE:
383 case TK_A_STRING_ESCAPE_REPLACE:
384 case TK_A_DB_PERSIST:
9f6445e3 385 log_debug("%s", token_str(type));
912541b0
KS
386 break;
387 case TK_M_TEST:
9f6445e3 388 log_debug("%s %s '%s'(%s) %#o",
baa30fbc 389 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
912541b0
KS
390 break;
391 case TK_A_INOTIFY_WATCH:
9f6445e3 392 log_debug("%s %u", token_str(type), token->key.watch);
912541b0
KS
393 break;
394 case TK_A_DEVLINK_PRIO:
9f6445e3 395 log_debug("%s %u", token_str(type), token->key.devlink_prio);
912541b0
KS
396 break;
397 case TK_A_OWNER_ID:
9f6445e3 398 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
912541b0
KS
399 break;
400 case TK_A_GROUP_ID:
9f6445e3 401 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
912541b0
KS
402 break;
403 case TK_A_MODE_ID:
9f6445e3 404 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
912541b0
KS
405 break;
406 case TK_A_STATIC_NODE:
9f6445e3 407 log_debug("%s '%s'", token_str(type), value);
912541b0 408 break;
c26547d6 409 case TK_A_SECLABEL:
9f6445e3 410 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
c26547d6 411 break;
912541b0 412 case TK_M_EVENT_TIMEOUT:
9f6445e3 413 log_debug("%s %u", token_str(type), token->key.event_timeout);
912541b0
KS
414 break;
415 case TK_A_GOTO:
9f6445e3 416 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
912541b0
KS
417 break;
418 case TK_END:
9f6445e3 419 log_debug("* %s", token_str(type));
912541b0
KS
420 break;
421 case TK_M_PARENTS_MIN:
422 case TK_M_PARENTS_MAX:
423 case TK_M_MAX:
424 case TK_UNSET:
9f6445e3 425 log_debug("unknown type %u", type);
912541b0
KS
426 break;
427 }
4052400f 428}
154a7b84 429
4052400f
KS
430static void dump_rules(struct udev_rules *rules)
431{
912541b0
KS
432 unsigned int i;
433
9f6445e3 434 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
baa30fbc
KS
435 rules->token_cur,
436 rules->token_cur * sizeof(struct token),
437 rules->buf_count,
438 rules->buf_cur);
f168c273 439 for (i = 0; i < rules->token_cur; i++)
912541b0 440 dump_token(rules, &rules->tokens[i]);
4052400f
KS
441}
442#else
4052400f
KS
443static inline void dump_token(struct udev_rules *rules, struct token *token) {}
444static inline void dump_rules(struct udev_rules *rules) {}
baa30fbc 445#endif /* DEBUG */
c7521974 446
1449a55d
AJ
447static int add_token(struct udev_rules *rules, struct token *token)
448{
912541b0
KS
449 /* grow buffer if needed */
450 if (rules->token_cur+1 >= rules->token_max) {
451 struct token *tokens;
452 unsigned int add;
453
454 /* double the buffer size */
455 add = rules->token_max;
456 if (add < 8)
457 add = 8;
458
459 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
460 if (tokens == NULL)
461 return -1;
912541b0
KS
462 rules->tokens = tokens;
463 rules->token_max += add;
464 }
465 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
466 rules->token_cur++;
467 return 0;
c7521974 468}
a27cd06c 469
154a7b84
KS
470static uid_t add_uid(struct udev_rules *rules, const char *owner)
471{
912541b0
KS
472 unsigned int i;
473 uid_t uid;
474 unsigned int off;
475
476 /* lookup, if we know it already */
477 for (i = 0; i < rules->uids_cur; i++) {
478 off = rules->uids[i].name_off;
915bf0f6 479 if (streq(rules_str(rules, off), owner)) {
912541b0 480 uid = rules->uids[i].uid;
912541b0
KS
481 return uid;
482 }
483 }
484 uid = util_lookup_user(rules->udev, owner);
485
486 /* grow buffer if needed */
487 if (rules->uids_cur+1 >= rules->uids_max) {
488 struct uid_gid *uids;
489 unsigned int add;
490
491 /* double the buffer size */
492 add = rules->uids_max;
493 if (add < 1)
494 add = 8;
495
496 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
497 if (uids == NULL)
498 return uid;
912541b0
KS
499 rules->uids = uids;
500 rules->uids_max += add;
501 }
502 rules->uids[rules->uids_cur].uid = uid;
915bf0f6 503 off = rules_add_string(rules, owner);
912541b0
KS
504 if (off <= 0)
505 return uid;
506 rules->uids[rules->uids_cur].name_off = off;
507 rules->uids_cur++;
508 return uid;
154a7b84
KS
509}
510
511static gid_t add_gid(struct udev_rules *rules, const char *group)
512{
912541b0
KS
513 unsigned int i;
514 gid_t gid;
515 unsigned int off;
516
517 /* lookup, if we know it already */
518 for (i = 0; i < rules->gids_cur; i++) {
519 off = rules->gids[i].name_off;
915bf0f6 520 if (streq(rules_str(rules, off), group)) {
912541b0 521 gid = rules->gids[i].gid;
912541b0
KS
522 return gid;
523 }
524 }
525 gid = util_lookup_group(rules->udev, group);
526
527 /* grow buffer if needed */
528 if (rules->gids_cur+1 >= rules->gids_max) {
529 struct uid_gid *gids;
530 unsigned int add;
531
532 /* double the buffer size */
533 add = rules->gids_max;
534 if (add < 1)
535 add = 8;
536
537 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
538 if (gids == NULL)
539 return gid;
912541b0
KS
540 rules->gids = gids;
541 rules->gids_max += add;
542 }
543 rules->gids[rules->gids_cur].gid = gid;
915bf0f6 544 off = rules_add_string(rules, group);
912541b0
KS
545 if (off <= 0)
546 return gid;
547 rules->gids[rules->gids_cur].name_off = off;
548 rules->gids_cur++;
549 return gid;
154a7b84
KS
550}
551
bb144ed1 552static int import_property_from_string(struct udev_device *dev, char *line)
bd0ed2ff 553{
912541b0
KS
554 char *key;
555 char *val;
556 size_t len;
0aa5b4d1 557 struct udev_list_entry *entry;
912541b0
KS
558
559 /* find key */
560 key = line;
561 while (isspace(key[0]))
562 key++;
563
564 /* comment or empty line */
565 if (key[0] == '#' || key[0] == '\0')
566 return -1;
567
568 /* split key/value */
569 val = strchr(key, '=');
570 if (val == NULL)
571 return -1;
572 val[0] = '\0';
573 val++;
574
575 /* find value */
576 while (isspace(val[0]))
577 val++;
578
579 /* terminate key */
580 len = strlen(key);
581 if (len == 0)
582 return -1;
583 while (isspace(key[len-1]))
584 len--;
585 key[len] = '\0';
586
587 /* terminate value */
588 len = strlen(val);
589 if (len == 0)
590 return -1;
591 while (isspace(val[len-1]))
592 len--;
593 val[len] = '\0';
594
595 if (len == 0)
596 return -1;
597
598 /* unquote */
599 if (val[0] == '"' || val[0] == '\'') {
600 if (val[len-1] != val[0]) {
9f6445e3 601 log_debug("inconsistent quoting: '%s', skip", line);
912541b0
KS
602 return -1;
603 }
604 val[len-1] = '\0';
605 val++;
606 }
607
0aa5b4d1
KS
608 entry = udev_device_add_property(dev, key, val);
609 /* store in db, skip private keys */
610 if (key[0] != '.')
611 udev_list_entry_set_num(entry, true);
612
912541b0 613 return 0;
be4bedd1
KS
614}
615
6880b25d 616static int import_file_into_properties(struct udev_device *dev, const char *filename)
be4bedd1 617{
912541b0
KS
618 FILE *f;
619 char line[UTIL_LINE_SIZE];
620
47ef94ac 621 f = fopen(filename, "re");
912541b0
KS
622 if (f == NULL)
623 return -1;
624 while (fgets(line, sizeof(line), f) != NULL)
625 import_property_from_string(dev, line);
626 fclose(f);
627 return 0;
bd0ed2ff
KS
628}
629
2181d30a 630static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
319c6700 631{
912541b0
KS
632 struct udev_device *dev = event->dev;
633 char **envp;
634 char result[UTIL_LINE_SIZE];
635 char *line;
636 int err;
637
638 envp = udev_device_get_properties_envp(dev);
639 err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
640 if (err < 0)
641 return err;
642
643 line = result;
644 while (line != NULL) {
645 char *pos;
646
647 pos = strchr(line, '\n');
648 if (pos != NULL) {
649 pos[0] = '\0';
650 pos = &pos[1];
651 }
652 import_property_from_string(dev, line);
653 line = pos;
654 }
655 return 0;
319c6700
KS
656}
657
6880b25d 658static int import_parent_into_properties(struct udev_device *dev, const char *filter)
0bfb84e1 659{
912541b0
KS
660 struct udev_device *dev_parent;
661 struct udev_list_entry *list_entry;
662
663 dev_parent = udev_device_get_parent(dev);
664 if (dev_parent == NULL)
665 return -1;
666
912541b0
KS
667 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
668 const char *key = udev_list_entry_get_name(list_entry);
669 const char *val = udev_list_entry_get_value(list_entry);
670
671 if (fnmatch(filter, key, 0) == 0) {
672 struct udev_list_entry *entry;
673
912541b0
KS
674 entry = udev_device_add_property(dev, key, val);
675 /* store in db, skip private keys */
676 if (key[0] != '.')
677 udev_list_entry_set_num(entry, true);
678 }
679 }
680 return 0;
e2ecb34f
KS
681}
682
912541b0 683#define WAIT_LOOP_PER_SECOND 50
6880b25d 684static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
b2fe4b9a 685{
912541b0
KS
686 char filepath[UTIL_PATH_SIZE];
687 char devicepath[UTIL_PATH_SIZE];
688 struct stat stats;
689 int loop = timeout * WAIT_LOOP_PER_SECOND;
690
691 /* a relative path is a device attribute */
692 devicepath[0] = '\0';
693 if (file[0] != '/') {
d5a89d7d
KS
694 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
695 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
912541b0
KS
696 file = filepath;
697 }
698
912541b0
KS
699 while (--loop) {
700 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
701
702 /* lookup file */
703 if (stat(file, &stats) == 0) {
9f6445e3 704 log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
912541b0
KS
705 return 0;
706 }
707 /* make sure, the device did not disappear in the meantime */
708 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
9f6445e3 709 log_debug("device disappeared while waiting for '%s'", file);
912541b0
KS
710 return -2;
711 }
9f6445e3 712 log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
912541b0
KS
713 nanosleep(&duration, NULL);
714 }
9f6445e3 715 log_debug("waiting for '%s' failed", file);
912541b0 716 return -1;
b2fe4b9a
KS
717}
718
0ea5e96e
KS
719static int attr_subst_subdir(char *attr, size_t len)
720{
912541b0
KS
721 bool found = false;
722
723 if (strstr(attr, "/*/")) {
724 char *pos;
725 char dirname[UTIL_PATH_SIZE];
726 const char *tail;
727 DIR *dir;
728
d5a89d7d 729 strscpy(dirname, sizeof(dirname), attr);
912541b0
KS
730 pos = strstr(dirname, "/*/");
731 if (pos == NULL)
732 return -1;
733 pos[0] = '\0';
734 tail = &pos[2];
735 dir = opendir(dirname);
736 if (dir != NULL) {
737 struct dirent *dent;
738
739 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
740 struct stat stats;
741
742 if (dent->d_name[0] == '.')
743 continue;
d5a89d7d 744 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
912541b0
KS
745 if (stat(attr, &stats) == 0) {
746 found = true;
747 break;
748 }
749 }
750 closedir(dir);
751 }
752 }
753
754 return found;
0ea5e96e
KS
755}
756
b0f7409f 757static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
a0e5382d 758{
912541b0
KS
759 char *linepos;
760 char *temp;
761
762 linepos = *line;
763 if (linepos == NULL || linepos[0] == '\0')
764 return -1;
765
766 /* skip whitespace */
767 while (isspace(linepos[0]) || linepos[0] == ',')
768 linepos++;
769
770 /* get the key */
771 if (linepos[0] == '\0')
772 return -1;
773 *key = linepos;
774
775 for (;;) {
776 linepos++;
777 if (linepos[0] == '\0')
778 return -1;
779 if (isspace(linepos[0]))
780 break;
781 if (linepos[0] == '=')
782 break;
783 if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
784 if (linepos[1] == '=')
785 break;
786 }
787
788 /* remember end of key */
789 temp = linepos;
790
791 /* skip whitespace after key */
792 while (isspace(linepos[0]))
793 linepos++;
794 if (linepos[0] == '\0')
795 return -1;
796
797 /* get operation type */
798 if (linepos[0] == '=' && linepos[1] == '=') {
799 *op = OP_MATCH;
800 linepos += 2;
801 } else if (linepos[0] == '!' && linepos[1] == '=') {
802 *op = OP_NOMATCH;
803 linepos += 2;
804 } else if (linepos[0] == '+' && linepos[1] == '=') {
805 *op = OP_ADD;
806 linepos += 2;
807 } else if (linepos[0] == '=') {
808 *op = OP_ASSIGN;
809 linepos++;
810 } else if (linepos[0] == ':' && linepos[1] == '=') {
811 *op = OP_ASSIGN_FINAL;
812 linepos += 2;
813 } else
814 return -1;
815
816 /* terminate key */
817 temp[0] = '\0';
818
819 /* skip whitespace after operator */
820 while (isspace(linepos[0]))
821 linepos++;
822 if (linepos[0] == '\0')
823 return -1;
824
825 /* get the value */
826 if (linepos[0] == '"')
827 linepos++;
828 else
829 return -1;
830 *value = linepos;
831
832 /* terminate */
833 temp = strchr(linepos, '"');
834 if (!temp)
835 return -1;
836 temp[0] = '\0';
837 temp++;
912541b0
KS
838
839 /* move line to next key */
840 *line = temp;
841 return 0;
6880b25d 842}
95776dc6 843
6880b25d 844/* extract possible KEY{attr} */
04a9d3a0 845static const char *get_key_attribute(struct udev *udev, char *str)
6880b25d 846{
912541b0
KS
847 char *pos;
848 char *attr;
849
850 attr = strchr(str, '{');
851 if (attr != NULL) {
852 attr++;
853 pos = strchr(attr, '}');
854 if (pos == NULL) {
9f6445e3 855 log_error("missing closing brace for format");
912541b0
KS
856 return NULL;
857 }
858 pos[0] = '\0';
912541b0
KS
859 return attr;
860 }
861 return NULL;
6880b25d 862}
95776dc6 863
0ef254d5 864static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
912541b0
KS
865 enum operation_type op,
866 const char *value, const void *data)
6880b25d 867{
912541b0
KS
868 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
869 const char *attr = NULL;
870
29804cc1 871 memzero(token, sizeof(struct token));
912541b0
KS
872
873 switch (type) {
874 case TK_M_ACTION:
875 case TK_M_DEVPATH:
876 case TK_M_KERNEL:
877 case TK_M_SUBSYSTEM:
878 case TK_M_DRIVER:
879 case TK_M_WAITFOR:
880 case TK_M_DEVLINK:
881 case TK_M_NAME:
882 case TK_M_KERNELS:
883 case TK_M_SUBSYSTEMS:
884 case TK_M_DRIVERS:
885 case TK_M_TAGS:
886 case TK_M_PROGRAM:
887 case TK_M_IMPORT_FILE:
888 case TK_M_IMPORT_PROG:
889 case TK_M_IMPORT_DB:
890 case TK_M_IMPORT_CMDLINE:
891 case TK_M_IMPORT_PARENT:
892 case TK_M_RESULT:
893 case TK_A_OWNER:
894 case TK_A_GROUP:
895 case TK_A_MODE:
8a173387 896 case TK_A_DEVLINK:
912541b0
KS
897 case TK_A_NAME:
898 case TK_A_GOTO:
899 case TK_M_TAG:
900 case TK_A_TAG:
d6f116a7 901 case TK_A_STATIC_NODE:
915bf0f6 902 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
903 break;
904 case TK_M_IMPORT_BUILTIN:
915bf0f6 905 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
906 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
907 break;
908 case TK_M_ENV:
909 case TK_M_ATTR:
910 case TK_M_ATTRS:
911 case TK_A_ATTR:
912 case TK_A_ENV:
c26547d6 913 case TK_A_SECLABEL:
912541b0 914 attr = data;
915bf0f6
KS
915 token->key.value_off = rules_add_string(rule_tmp->rules, value);
916 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
912541b0 917 break;
912541b0 918 case TK_M_TEST:
915bf0f6 919 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
920 if (data != NULL)
921 token->key.mode = *(mode_t *)data;
922 break;
923 case TK_A_STRING_ESCAPE_NONE:
924 case TK_A_STRING_ESCAPE_REPLACE:
925 case TK_A_DB_PERSIST:
926 break;
83cd6b75 927 case TK_A_RUN_BUILTIN:
83cd6b75 928 case TK_A_RUN_PROGRAM:
4590cfe4 929 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
915bf0f6 930 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
931 break;
932 case TK_A_INOTIFY_WATCH:
933 case TK_A_DEVLINK_PRIO:
934 token->key.devlink_prio = *(int *)data;
935 break;
936 case TK_A_OWNER_ID:
937 token->key.uid = *(uid_t *)data;
938 break;
939 case TK_A_GROUP_ID:
940 token->key.gid = *(gid_t *)data;
941 break;
942 case TK_A_MODE_ID:
943 token->key.mode = *(mode_t *)data;
944 break;
912541b0
KS
945 case TK_M_EVENT_TIMEOUT:
946 token->key.event_timeout = *(int *)data;
947 break;
948 case TK_RULE:
949 case TK_M_PARENTS_MIN:
950 case TK_M_PARENTS_MAX:
951 case TK_M_MAX:
952 case TK_END:
953 case TK_UNSET:
9f6445e3 954 log_error("wrong type %u", type);
912541b0
KS
955 return -1;
956 }
957
958 if (value != NULL && type < TK_M_MAX) {
959 /* check if we need to split or call fnmatch() while matching rules */
960 enum string_glob_type glob;
961 int has_split;
962 int has_glob;
963
964 has_split = (strchr(value, '|') != NULL);
e3e0314b 965 has_glob = string_is_glob(value);
912541b0
KS
966 if (has_split && has_glob) {
967 glob = GL_SPLIT_GLOB;
968 } else if (has_split) {
969 glob = GL_SPLIT;
970 } else if (has_glob) {
33502ffe 971 if (streq(value, "?*"))
912541b0
KS
972 glob = GL_SOMETHING;
973 else
974 glob = GL_GLOB;
975 } else {
976 glob = GL_PLAIN;
977 }
978 token->key.glob = glob;
979 }
980
981 if (value != NULL && type > TK_M_MAX) {
982 /* check if assigned value has substitution chars */
983 if (value[0] == '[')
984 token->key.subst = SB_SUBSYS;
985 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
986 token->key.subst = SB_FORMAT;
987 else
988 token->key.subst = SB_NONE;
989 }
990
991 if (attr != NULL) {
d6f116a7 992 /* check if property/attribute name has substitution chars */
912541b0
KS
993 if (attr[0] == '[')
994 token->key.attrsubst = SB_SUBSYS;
995 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
996 token->key.attrsubst = SB_FORMAT;
997 else
998 token->key.attrsubst = SB_NONE;
999 }
1000
1001 token->key.type = type;
1002 token->key.op = op;
1003 rule_tmp->token_cur++;
8fef0ff2 1004 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
9f6445e3 1005 log_error("temporary rule array too small");
912541b0
KS
1006 return -1;
1007 }
1008 return 0;
6880b25d 1009}
e57e7bc1 1010
6880b25d
KS
1011static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
1012{
912541b0
KS
1013 unsigned int i;
1014 unsigned int start = 0;
1015 unsigned int end = rule_tmp->token_cur;
1016
1017 for (i = 0; i < rule_tmp->token_cur; i++) {
1018 enum token_type next_val = TK_UNSET;
1019 unsigned int next_idx = 0;
1020 unsigned int j;
1021
1022 /* find smallest value */
1023 for (j = start; j < end; j++) {
1024 if (rule_tmp->token[j].type == TK_UNSET)
1025 continue;
1026 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1027 next_val = rule_tmp->token[j].type;
1028 next_idx = j;
1029 }
1030 }
1031
1032 /* add token and mark done */
1033 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1034 return -1;
1035 rule_tmp->token[next_idx].type = TK_UNSET;
1036
1037 /* shrink range */
1038 if (next_idx == start)
1039 start++;
1040 if (next_idx+1 == end)
1041 end--;
1042 }
1043 return 0;
724257d9
GKH
1044}
1045
6880b25d 1046static int add_rule(struct udev_rules *rules, char *line,
912541b0 1047 const char *filename, unsigned int filename_off, unsigned int lineno)
724257d9 1048{
912541b0 1049 char *linepos;
04a9d3a0 1050 const char *attr;
912541b0
KS
1051 struct rule_tmp rule_tmp;
1052
29804cc1 1053 memzero(&rule_tmp, sizeof(struct rule_tmp));
912541b0
KS
1054 rule_tmp.rules = rules;
1055 rule_tmp.rule.type = TK_RULE;
775f8b3c
KS
1056 /* the offset in the rule is limited to unsigned short */
1057 if (filename_off < USHRT_MAX)
1058 rule_tmp.rule.rule.filename_off = filename_off;
912541b0
KS
1059 rule_tmp.rule.rule.filename_line = lineno;
1060
1061 linepos = line;
1062 for (;;) {
1063 char *key;
1064 char *value;
1065 enum operation_type op;
1066
e736cf35 1067 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
3cf0f8f7
DR
1068 /* Avoid erroring on trailing whitespace. This is probably rare
1069 * so save the work for the error case instead of always trying
1070 * to strip the trailing whitespace with strstrip(). */
1071 while (isblank(*linepos))
1072 linepos++;
1073
e736cf35
DR
1074 /* If we aren't at the end of the line, this is a parsing error.
1075 * Make a best effort to describe where the problem is. */
1291bc89
ZJS
1076 if (*linepos != '\n') {
1077 char buf[2] = {linepos[1]};
1078 _cleanup_free_ char *tmp;
1079
1080 tmp = cescape(buf);
e736cf35 1081 log_error("invalid key/value pair in file %s on line %u,"
7c902b14 1082 "starting at character %tu ('%s')\n",
1291bc89
ZJS
1083 filename, lineno, linepos - line + 1, tmp);
1084 if (linepos[1] == '#')
75716dd8 1085 log_error("hint: comments can only start at beginning of line");
1291bc89 1086 }
912541b0 1087 break;
e736cf35 1088 }
912541b0 1089
33502ffe 1090 if (streq(key, "ACTION")) {
912541b0 1091 if (op > OP_MATCH_MAX) {
9f6445e3 1092 log_error("invalid ACTION operation");
912541b0
KS
1093 goto invalid;
1094 }
1095 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1096 continue;
1097 }
1098
33502ffe 1099 if (streq(key, "DEVPATH")) {
912541b0 1100 if (op > OP_MATCH_MAX) {
9f6445e3 1101 log_error("invalid DEVPATH operation");
912541b0
KS
1102 goto invalid;
1103 }
1104 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1105 continue;
1106 }
1107
33502ffe 1108 if (streq(key, "KERNEL")) {
912541b0 1109 if (op > OP_MATCH_MAX) {
9f6445e3 1110 log_error("invalid KERNEL operation");
912541b0
KS
1111 goto invalid;
1112 }
1113 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1114 continue;
1115 }
1116
33502ffe 1117 if (streq(key, "SUBSYSTEM")) {
912541b0 1118 if (op > OP_MATCH_MAX) {
9f6445e3 1119 log_error("invalid SUBSYSTEM operation");
912541b0
KS
1120 goto invalid;
1121 }
1122 /* bus, class, subsystem events should all be the same */
33502ffe
KS
1123 if (streq(value, "subsystem") ||
1124 streq(value, "bus") ||
1125 streq(value, "class")) {
1126 if (streq(value, "bus") || streq(value, "class"))
9f6445e3 1127 log_error("'%s' must be specified as 'subsystem' "
912541b0
KS
1128 "please fix it in %s:%u", value, filename, lineno);
1129 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1130 } else
1131 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1132 continue;
1133 }
1134
33502ffe 1135 if (streq(key, "DRIVER")) {
912541b0 1136 if (op > OP_MATCH_MAX) {
9f6445e3 1137 log_error("invalid DRIVER operation");
912541b0
KS
1138 goto invalid;
1139 }
1140 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1141 continue;
1142 }
1143
33502ffe 1144 if (startswith(key, "ATTR{")) {
f8294e41 1145 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
912541b0 1146 if (attr == NULL) {
9f6445e3 1147 log_error("error parsing ATTR attribute");
912541b0
KS
1148 goto invalid;
1149 }
1150 if (op < OP_MATCH_MAX) {
1151 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1152 } else {
1153 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1154 }
1155 continue;
1156 }
1157
c26547d6 1158 if (startswith(key, "SECLABEL{")) {
f8294e41 1159 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
c26547d6 1160 if (!attr) {
9f6445e3 1161 log_error("error parsing SECLABEL attribute");
c26547d6
KS
1162 goto invalid;
1163 }
1164
1165 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1166 continue;
1167 }
1168
33502ffe 1169 if (streq(key, "KERNELS")) {
912541b0 1170 if (op > OP_MATCH_MAX) {
9f6445e3 1171 log_error("invalid KERNELS operation");
912541b0
KS
1172 goto invalid;
1173 }
1174 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1175 continue;
1176 }
1177
33502ffe 1178 if (streq(key, "SUBSYSTEMS")) {
912541b0 1179 if (op > OP_MATCH_MAX) {
9f6445e3 1180 log_error("invalid SUBSYSTEMS operation");
912541b0
KS
1181 goto invalid;
1182 }
1183 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1184 continue;
1185 }
1186
33502ffe 1187 if (streq(key, "DRIVERS")) {
912541b0 1188 if (op > OP_MATCH_MAX) {
9f6445e3 1189 log_error("invalid DRIVERS operation");
912541b0
KS
1190 goto invalid;
1191 }
1192 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1193 continue;
1194 }
1195
33502ffe 1196 if (startswith(key, "ATTRS{")) {
912541b0 1197 if (op > OP_MATCH_MAX) {
9f6445e3 1198 log_error("invalid ATTRS operation");
912541b0
KS
1199 goto invalid;
1200 }
f8294e41 1201 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
912541b0 1202 if (attr == NULL) {
9f6445e3 1203 log_error("error parsing ATTRS attribute");
912541b0
KS
1204 goto invalid;
1205 }
33502ffe 1206 if (startswith(attr, "device/"))
baa30fbc 1207 log_error("the 'device' link may not be available in a future kernel, "
912541b0
KS
1208 "please fix it in %s:%u", filename, lineno);
1209 else if (strstr(attr, "../") != NULL)
baa30fbc 1210 log_error("do not reference parent sysfs directories directly, "
912541b0
KS
1211 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1212 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1213 continue;
1214 }
1215
33502ffe 1216 if (streq(key, "TAGS")) {
912541b0 1217 if (op > OP_MATCH_MAX) {
9f6445e3 1218 log_error("invalid TAGS operation");
912541b0
KS
1219 goto invalid;
1220 }
1221 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1222 continue;
1223 }
1224
33502ffe 1225 if (startswith(key, "ENV{")) {
f8294e41 1226 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
912541b0 1227 if (attr == NULL) {
9f6445e3 1228 log_error("error parsing ENV attribute");
912541b0
KS
1229 goto invalid;
1230 }
1231 if (op < OP_MATCH_MAX) {
1232 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1233 goto invalid;
1234 } else {
1235 static const char *blacklist[] = {
1236 "ACTION",
1237 "SUBSYSTEM",
1238 "DEVTYPE",
1239 "MAJOR",
1240 "MINOR",
1241 "DRIVER",
1242 "IFINDEX",
1243 "DEVNAME",
1244 "DEVLINKS",
1245 "DEVPATH",
1246 "TAGS",
1247 };
1248 unsigned int i;
1249
395ea978
KS
1250 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1251 if (!streq(attr, blacklist[i]))
912541b0 1252 continue;
9f6445e3 1253 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
395ea978
KS
1254 goto invalid;
1255 }
912541b0
KS
1256 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1257 goto invalid;
1258 }
1259 continue;
1260 }
1261
33502ffe 1262 if (streq(key, "TAG")) {
912541b0
KS
1263 if (op < OP_MATCH_MAX)
1264 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1265 else
1266 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1267 continue;
1268 }
1269
33502ffe 1270 if (streq(key, "PROGRAM")) {
912541b0
KS
1271 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1272 continue;
1273 }
1274
33502ffe 1275 if (streq(key, "RESULT")) {
912541b0 1276 if (op > OP_MATCH_MAX) {
9f6445e3 1277 log_error("invalid RESULT operation");
912541b0
KS
1278 goto invalid;
1279 }
1280 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1281 continue;
1282 }
1283
33502ffe 1284 if (startswith(key, "IMPORT")) {
f8294e41 1285 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
912541b0 1286 if (attr == NULL) {
9f6445e3 1287 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1288 continue;
1289 }
33502ffe 1290 if (streq(attr, "program")) {
912541b0
KS
1291 /* find known built-in command */
1292 if (value[0] != '/') {
1293 enum udev_builtin_cmd cmd;
1294
1295 cmd = udev_builtin_lookup(value);
1296 if (cmd < UDEV_BUILTIN_MAX) {
9f6445e3 1297 log_debug("IMPORT found builtin '%s', replacing %s:%u",
baa30fbc 1298 value, filename, lineno);
912541b0
KS
1299 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1300 continue;
1301 }
1302 }
912541b0 1303 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
33502ffe 1304 } else if (streq(attr, "builtin")) {
912541b0
KS
1305 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1306
912541b0
KS
1307 if (cmd < UDEV_BUILTIN_MAX)
1308 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1309 else
9f6445e3 1310 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1311 } else if (streq(attr, "file")) {
912541b0 1312 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
33502ffe 1313 } else if (streq(attr, "db")) {
912541b0 1314 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
33502ffe 1315 } else if (streq(attr, "cmdline")) {
912541b0 1316 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
33502ffe 1317 } else if (streq(attr, "parent")) {
912541b0 1318 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
83cd6b75 1319 } else
9f6445e3 1320 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1321 continue;
1322 }
1323
33502ffe 1324 if (startswith(key, "TEST")) {
912541b0
KS
1325 mode_t mode = 0;
1326
1327 if (op > OP_MATCH_MAX) {
9f6445e3 1328 log_error("invalid TEST operation");
912541b0
KS
1329 goto invalid;
1330 }
f8294e41 1331 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
912541b0
KS
1332 if (attr != NULL) {
1333 mode = strtol(attr, NULL, 8);
1334 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1335 } else {
1336 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1337 }
1338 continue;
1339 }
1340
33502ffe 1341 if (startswith(key, "RUN")) {
f8294e41 1342 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
83cd6b75
KS
1343 if (attr == NULL)
1344 attr = "program";
1345
33502ffe 1346 if (streq(attr, "builtin")) {
83cd6b75
KS
1347 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1348
1349 if (cmd < UDEV_BUILTIN_MAX)
1350 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1351 else
9f6445e3 1352 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1353 } else if (streq(attr, "program")) {
83cd6b75
KS
1354 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1355
83cd6b75
KS
1356 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1357 } else {
9f6445e3 1358 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
83cd6b75
KS
1359 }
1360
912541b0
KS
1361 continue;
1362 }
1363
33502ffe 1364 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
912541b0
KS
1365 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1366 continue;
1367 }
1368
33502ffe 1369 if (streq(key, "LABEL")) {
915bf0f6 1370 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
912541b0
KS
1371 continue;
1372 }
1373
33502ffe 1374 if (streq(key, "GOTO")) {
912541b0
KS
1375 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1376 continue;
1377 }
1378
33502ffe 1379 if (startswith(key, "NAME")) {
912541b0
KS
1380 if (op < OP_MATCH_MAX) {
1381 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1382 } else {
33502ffe 1383 if (streq(value, "%k")) {
baa30fbc 1384 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
912541b0
KS
1385 "please remove it from %s:%u\n", filename, lineno);
1386 continue;
1387 }
1388 if (value[0] == '\0') {
baa30fbc
KS
1389 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1390 "please remove it from %s:%u\n", filename, lineno);
912541b0
KS
1391 continue;
1392 }
1393 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1394 }
1395 rule_tmp.rule.rule.can_set_name = true;
1396 continue;
1397 }
1398
8a173387
KS
1399 if (streq(key, "SYMLINK")) {
1400 if (op < OP_MATCH_MAX)
912541b0 1401 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
8a173387
KS
1402 else
1403 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
912541b0
KS
1404 rule_tmp.rule.rule.can_set_name = true;
1405 continue;
1406 }
1407
33502ffe 1408 if (streq(key, "OWNER")) {
912541b0
KS
1409 uid_t uid;
1410 char *endptr;
1411
1412 uid = strtoul(value, &endptr, 10);
1413 if (endptr[0] == '\0') {
1414 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1415 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1416 uid = add_uid(rules, value);
1417 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1418 } else if (rules->resolve_names >= 0) {
1419 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1420 }
1421 rule_tmp.rule.rule.can_set_name = true;
1422 continue;
1423 }
1424
33502ffe 1425 if (streq(key, "GROUP")) {
912541b0
KS
1426 gid_t gid;
1427 char *endptr;
1428
1429 gid = strtoul(value, &endptr, 10);
1430 if (endptr[0] == '\0') {
1431 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1432 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1433 gid = add_gid(rules, value);
1434 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1435 } else if (rules->resolve_names >= 0) {
1436 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1437 }
1438 rule_tmp.rule.rule.can_set_name = true;
1439 continue;
1440 }
1441
33502ffe 1442 if (streq(key, "MODE")) {
912541b0
KS
1443 mode_t mode;
1444 char *endptr;
1445
1446 mode = strtol(value, &endptr, 8);
1447 if (endptr[0] == '\0')
1448 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1449 else
1450 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1451 rule_tmp.rule.rule.can_set_name = true;
1452 continue;
1453 }
1454
33502ffe 1455 if (streq(key, "OPTIONS")) {
912541b0
KS
1456 const char *pos;
1457
1458 pos = strstr(value, "link_priority=");
1459 if (pos != NULL) {
1460 int prio = atoi(&pos[strlen("link_priority=")]);
1461
1462 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
912541b0
KS
1463 }
1464
1465 pos = strstr(value, "event_timeout=");
1466 if (pos != NULL) {
1467 int tout = atoi(&pos[strlen("event_timeout=")]);
1468
1469 rule_add_key(&rule_tmp, TK_M_EVENT_TIMEOUT, op, NULL, &tout);
912541b0
KS
1470 }
1471
1472 pos = strstr(value, "string_escape=");
1473 if (pos != NULL) {
1474 pos = &pos[strlen("string_escape=")];
33502ffe 1475 if (startswith(pos, "none"))
912541b0 1476 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
33502ffe 1477 else if (startswith(pos, "replace"))
912541b0
KS
1478 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1479 }
1480
1481 pos = strstr(value, "db_persist");
1482 if (pos != NULL)
1483 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1484
1485 pos = strstr(value, "nowatch");
1486 if (pos != NULL) {
1487 const int off = 0;
1488
1489 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
912541b0
KS
1490 } else {
1491 pos = strstr(value, "watch");
1492 if (pos != NULL) {
1493 const int on = 1;
1494
1495 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
912541b0
KS
1496 }
1497 }
1498
1499 pos = strstr(value, "static_node=");
1500 if (pos != NULL) {
1501 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1502 rule_tmp.rule.rule.has_static_node = true;
1503 }
1504
1505 continue;
1506 }
1507
9f6445e3 1508 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
912541b0
KS
1509 goto invalid;
1510 }
1511
1512 /* add rule token */
1513 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1514 if (add_token(rules, &rule_tmp.rule) != 0)
1515 goto invalid;
1516
1517 /* add tokens to list, sorted by type */
1518 if (sort_token(rules, &rule_tmp) != 0)
1519 goto invalid;
1520
1521 return 0;
c7521974 1522invalid:
9f6445e3 1523 log_error("invalid rule '%s:%u'", filename, lineno);
912541b0 1524 return -1;
c7521974
KS
1525}
1526
775f8b3c 1527static int parse_file(struct udev_rules *rules, const char *filename)
c7521974 1528{
912541b0
KS
1529 FILE *f;
1530 unsigned int first_token;
775f8b3c 1531 unsigned int filename_off;
912541b0
KS
1532 char line[UTIL_LINE_SIZE];
1533 int line_nr = 0;
1534 unsigned int i;
1535
ed88bcfb
ZJS
1536 f = fopen(filename, "re");
1537 if (!f) {
1538 if (errno == ENOENT)
1539 return 0;
1540 else
1541 return -errno;
775f8b3c 1542 }
912541b0 1543
ed88bcfb
ZJS
1544 if (null_or_empty_fd(fileno(f))) {
1545 log_debug("Skipping empty file: %s", filename);
1546 return 0;
1547 } else
1548 log_debug("Reading rules file: %s", filename);
912541b0
KS
1549
1550 first_token = rules->token_cur;
915bf0f6 1551 filename_off = rules_add_string(rules, filename);
912541b0
KS
1552
1553 while (fgets(line, sizeof(line), f) != NULL) {
1554 char *key;
1555 size_t len;
1556
1557 /* skip whitespace */
1558 line_nr++;
1559 key = line;
1560 while (isspace(key[0]))
1561 key++;
1562
1563 /* comment */
1564 if (key[0] == '#')
1565 continue;
1566
1567 len = strlen(line);
1568 if (len < 3)
1569 continue;
1570
1571 /* continue reading if backslash+newline is found */
1572 while (line[len-2] == '\\') {
1573 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1574 break;
1575 if (strlen(&line[len-2]) < 2)
1576 break;
1577 line_nr++;
1578 len = strlen(line);
1579 }
1580
1581 if (len+1 >= sizeof(line)) {
9f6445e3 1582 log_error("line too long '%s':%u, ignored", filename, line_nr);
912541b0
KS
1583 continue;
1584 }
1585 add_rule(rules, key, filename, filename_off, line_nr);
1586 }
1587 fclose(f);
1588
1589 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1590 for (i = first_token+1; i < rules->token_cur; i++) {
1591 if (rules->tokens[i].type == TK_A_GOTO) {
915bf0f6 1592 char *label = rules_str(rules, rules->tokens[i].key.value_off);
912541b0
KS
1593 unsigned int j;
1594
1595 for (j = i+1; j < rules->token_cur; j++) {
1596 if (rules->tokens[j].type != TK_RULE)
1597 continue;
1598 if (rules->tokens[j].rule.label_off == 0)
1599 continue;
915bf0f6 1600 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
912541b0
KS
1601 continue;
1602 rules->tokens[i].key.rule_goto = j;
1603 break;
1604 }
1605 if (rules->tokens[i].key.rule_goto == 0)
9f6445e3 1606 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
912541b0
KS
1607 }
1608 }
1609 return 0;
c7521974
KS
1610}
1611
d7ddce18 1612struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
c7521974 1613{
912541b0
KS
1614 struct udev_rules *rules;
1615 struct udev_list file_list;
912541b0 1616 struct token end_token;
775f8b3c
KS
1617 char **files, **f;
1618 int r;
912541b0 1619
955d98c9 1620 rules = new0(struct udev_rules, 1);
912541b0
KS
1621 if (rules == NULL)
1622 return NULL;
1623 rules->udev = udev;
1624 rules->resolve_names = resolve_names;
1625 udev_list_init(udev, &file_list, true);
1626
1627 /* init token array and string buffer */
1628 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
0820a4f0
VP
1629 if (rules->tokens == NULL)
1630 return udev_rules_unref(rules);
912541b0
KS
1631 rules->token_max = PREALLOC_TOKEN;
1632
915bf0f6
KS
1633 rules->strbuf = strbuf_new();
1634 if (!rules->strbuf)
0820a4f0
VP
1635 return udev_rules_unref(rules);
1636
3b8c1cb0
KS
1637 udev_rules_check_timestamp(rules);
1638
2ad8416d 1639 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
775f8b3c 1640 if (r < 0) {
9f6445e3 1641 log_error("failed to enumerate rules files: %s", strerror(-r));
0820a4f0 1642 return udev_rules_unref(rules);
912541b0
KS
1643 }
1644
775f8b3c
KS
1645 /*
1646 * The offset value in the rules strct is limited; add all
1647 * rules file names to the beginning of the string buffer.
1648 */
1649 STRV_FOREACH(f, files)
915bf0f6 1650 rules_add_string(rules, *f);
912541b0 1651
775f8b3c
KS
1652 STRV_FOREACH(f, files)
1653 parse_file(rules, *f);
1654
1655 strv_free(files);
912541b0 1656
29804cc1 1657 memzero(&end_token, sizeof(struct token));
912541b0
KS
1658 end_token.type = TK_END;
1659 add_token(rules, &end_token);
9f6445e3 1660 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
915bf0f6 1661 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
912541b0 1662
915bf0f6 1663 /* cleanup temporary strbuf data */
9f6445e3 1664 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
915bf0f6
KS
1665 rules->strbuf->in_count, rules->strbuf->in_len,
1666 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1667 strbuf_complete(rules->strbuf);
912541b0
KS
1668
1669 /* cleanup uid/gid cache */
1670 free(rules->uids);
1671 rules->uids = NULL;
1672 rules->uids_cur = 0;
1673 rules->uids_max = 0;
1674 free(rules->gids);
1675 rules->gids = NULL;
1676 rules->gids_cur = 0;
1677 rules->gids_max = 0;
1678
1679 dump_rules(rules);
1680 return rules;
c7521974
KS
1681}
1682
7c85d636 1683struct udev_rules *udev_rules_unref(struct udev_rules *rules)
c7521974 1684{
912541b0
KS
1685 if (rules == NULL)
1686 return NULL;
1687 free(rules->tokens);
915bf0f6 1688 strbuf_cleanup(rules->strbuf);
912541b0
KS
1689 free(rules->uids);
1690 free(rules->gids);
1691 free(rules);
1692 return NULL;
c7521974 1693}
6880b25d 1694
6ada823a
KS
1695bool udev_rules_check_timestamp(struct udev_rules *rules)
1696{
5c11fbe3
KS
1697 if (!rules)
1698 return false;
1699
2ad8416d 1700 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
6ada823a
KS
1701}
1702
6880b25d
KS
1703static int match_key(struct udev_rules *rules, struct token *token, const char *val)
1704{
915bf0f6 1705 char *key_value = rules_str(rules, token->key.value_off);
912541b0
KS
1706 char *pos;
1707 bool match = false;
1708
1709 if (val == NULL)
1710 val = "";
1711
1712 switch (token->key.glob) {
1713 case GL_PLAIN:
33502ffe 1714 match = (streq(key_value, val));
912541b0
KS
1715 break;
1716 case GL_GLOB:
1717 match = (fnmatch(key_value, val, 0) == 0);
1718 break;
1719 case GL_SPLIT:
1720 {
33502ffe 1721 const char *s;
912541b0
KS
1722 size_t len;
1723
915bf0f6 1724 s = rules_str(rules, token->key.value_off);
912541b0
KS
1725 len = strlen(val);
1726 for (;;) {
1727 const char *next;
1728
33502ffe 1729 next = strchr(s, '|');
912541b0 1730 if (next != NULL) {
33502ffe 1731 size_t matchlen = (size_t)(next - s);
912541b0 1732
641906e9 1733 match = (matchlen == len && strneq(s, val, matchlen));
912541b0
KS
1734 if (match)
1735 break;
1736 } else {
33502ffe 1737 match = (streq(s, val));
912541b0
KS
1738 break;
1739 }
33502ffe 1740 s = &next[1];
912541b0
KS
1741 }
1742 break;
1743 }
1744 case GL_SPLIT_GLOB:
1745 {
1746 char value[UTIL_PATH_SIZE];
1747
d5a89d7d 1748 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
912541b0
KS
1749 key_value = value;
1750 while (key_value != NULL) {
1751 pos = strchr(key_value, '|');
1752 if (pos != NULL) {
1753 pos[0] = '\0';
1754 pos = &pos[1];
1755 }
912541b0
KS
1756 match = (fnmatch(key_value, val, 0) == 0);
1757 if (match)
1758 break;
1759 key_value = pos;
1760 }
1761 break;
1762 }
1763 case GL_SOMETHING:
1764 match = (val[0] != '\0');
1765 break;
1766 case GL_UNSET:
1767 return -1;
1768 }
1769
baa30fbc 1770 if (match && (token->key.op == OP_MATCH))
912541b0 1771 return 0;
baa30fbc 1772 if (!match && (token->key.op == OP_NOMATCH))
912541b0 1773 return 0;
912541b0 1774 return -1;
6880b25d
KS
1775}
1776
1777static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur)
1778{
912541b0
KS
1779 const char *name;
1780 char nbuf[UTIL_NAME_SIZE];
1781 const char *value;
1782 char vbuf[UTIL_NAME_SIZE];
1783 size_t len;
1784
915bf0f6 1785 name = rules_str(rules, cur->key.attr_off);
912541b0
KS
1786 switch (cur->key.attrsubst) {
1787 case SB_FORMAT:
1788 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1789 name = nbuf;
1790 /* fall through */
1791 case SB_NONE:
1792 value = udev_device_get_sysattr_value(dev, name);
1793 if (value == NULL)
1794 return -1;
1795 break;
1796 case SB_SUBSYS:
1797 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1798 return -1;
1799 value = vbuf;
1800 break;
1801 default:
1802 return -1;
1803 }
1804
1805 /* remove trailing whitespace, if not asked to match for it */
1806 len = strlen(value);
1807 if (len > 0 && isspace(value[len-1])) {
1808 const char *key_value;
1809 size_t klen;
1810
915bf0f6 1811 key_value = rules_str(rules, cur->key.value_off);
912541b0
KS
1812 klen = strlen(key_value);
1813 if (klen > 0 && !isspace(key_value[klen-1])) {
1814 if (value != vbuf) {
d5a89d7d 1815 strscpy(vbuf, sizeof(vbuf), value);
912541b0
KS
1816 value = vbuf;
1817 }
1818 while (len > 0 && isspace(vbuf[--len]))
1819 vbuf[len] = '\0';
912541b0
KS
1820 }
1821 }
1822
1823 return match_key(rules, cur, value);
6880b25d
KS
1824}
1825
1826enum escape_type {
912541b0
KS
1827 ESCAPE_UNSET,
1828 ESCAPE_NONE,
1829 ESCAPE_REPLACE,
6880b25d
KS
1830};
1831
2181d30a 1832int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, const sigset_t *sigmask)
6880b25d 1833{
912541b0
KS
1834 struct token *cur;
1835 struct token *rule;
1836 enum escape_type esc = ESCAPE_UNSET;
1837 bool can_set_name;
1838
1839 if (rules->tokens == NULL)
1840 return -1;
1841
33502ffe 1842 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
912541b0
KS
1843 (major(udev_device_get_devnum(event->dev)) > 0 ||
1844 udev_device_get_ifindex(event->dev) > 0));
1845
1846 /* loop through token list, match, run actions or forward to next rule */
1847 cur = &rules->tokens[0];
1848 rule = cur;
1849 for (;;) {
1850 dump_token(rules, cur);
1851 switch (cur->type) {
1852 case TK_RULE:
1853 /* current rule */
1854 rule = cur;
1855 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1856 if (!can_set_name && rule->rule.can_set_name)
1857 goto nomatch;
1858 esc = ESCAPE_UNSET;
1859 break;
1860 case TK_M_ACTION:
1861 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1862 goto nomatch;
1863 break;
1864 case TK_M_DEVPATH:
1865 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1866 goto nomatch;
1867 break;
1868 case TK_M_KERNEL:
1869 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1870 goto nomatch;
1871 break;
1872 case TK_M_DEVLINK: {
912541b0
KS
1873 struct udev_list_entry *list_entry;
1874 bool match = false;
1875
1876 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1877 const char *devlink;
1878
4cb72937 1879 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
912541b0
KS
1880 if (match_key(rules, cur, devlink) == 0) {
1881 match = true;
1882 break;
1883 }
1884 }
1885 if (!match)
1886 goto nomatch;
1887 break;
1888 }
1889 case TK_M_NAME:
1890 if (match_key(rules, cur, event->name) != 0)
1891 goto nomatch;
1892 break;
1893 case TK_M_ENV: {
915bf0f6 1894 const char *key_name = rules_str(rules, cur->key.attr_off);
912541b0
KS
1895 const char *value;
1896
1897 value = udev_device_get_property_value(event->dev, key_name);
baa30fbc 1898 if (value == NULL)
912541b0 1899 value = "";
912541b0
KS
1900 if (match_key(rules, cur, value))
1901 goto nomatch;
1902 break;
1903 }
1904 case TK_M_TAG: {
1905 struct udev_list_entry *list_entry;
1906 bool match = false;
1907
1908 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
915bf0f6 1909 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
912541b0
KS
1910 match = true;
1911 break;
1912 }
1913 }
1914 if (!match && (cur->key.op != OP_NOMATCH))
1915 goto nomatch;
1916 break;
1917 }
1918 case TK_M_SUBSYSTEM:
1919 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1920 goto nomatch;
1921 break;
1922 case TK_M_DRIVER:
1923 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1924 goto nomatch;
1925 break;
1926 case TK_M_WAITFOR: {
1927 char filename[UTIL_PATH_SIZE];
1928 int found;
1929
915bf0f6 1930 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
912541b0
KS
1931 found = (wait_for_file(event->dev, filename, 10) == 0);
1932 if (!found && (cur->key.op != OP_NOMATCH))
1933 goto nomatch;
1934 break;
1935 }
1936 case TK_M_ATTR:
1937 if (match_attr(rules, event->dev, event, cur) != 0)
1938 goto nomatch;
1939 break;
1940 case TK_M_KERNELS:
1941 case TK_M_SUBSYSTEMS:
1942 case TK_M_DRIVERS:
1943 case TK_M_ATTRS:
1944 case TK_M_TAGS: {
1945 struct token *next;
1946
1947 /* get whole sequence of parent matches */
1948 next = cur;
1949 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1950 next++;
1951
1952 /* loop over parents */
1953 event->dev_parent = event->dev;
1954 for (;;) {
1955 struct token *key;
1956
912541b0
KS
1957 /* loop over sequence of parent match keys */
1958 for (key = cur; key < next; key++ ) {
1959 dump_token(rules, key);
1960 switch(key->type) {
1961 case TK_M_KERNELS:
1962 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1963 goto try_parent;
1964 break;
1965 case TK_M_SUBSYSTEMS:
1966 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1967 goto try_parent;
1968 break;
1969 case TK_M_DRIVERS:
1970 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1971 goto try_parent;
1972 break;
1973 case TK_M_ATTRS:
1974 if (match_attr(rules, event->dev_parent, event, key) != 0)
1975 goto try_parent;
1976 break;
1977 case TK_M_TAGS: {
915bf0f6 1978 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
912541b0
KS
1979
1980 if (match && key->key.op == OP_NOMATCH)
1981 goto try_parent;
1982 if (!match && key->key.op == OP_MATCH)
1983 goto try_parent;
1984 break;
1985 }
1986 default:
1987 goto nomatch;
1988 }
912541b0 1989 }
912541b0
KS
1990 break;
1991
1992 try_parent:
1993 event->dev_parent = udev_device_get_parent(event->dev_parent);
1994 if (event->dev_parent == NULL)
1995 goto nomatch;
1996 }
1997 /* move behind our sequence of parent match keys */
1998 cur = next;
1999 continue;
2000 }
2001 case TK_M_TEST: {
2002 char filename[UTIL_PATH_SIZE];
2003 struct stat statbuf;
2004 int match;
2005
915bf0f6 2006 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
912541b0
KS
2007 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2008 if (filename[0] != '/') {
2009 char tmp[UTIL_PATH_SIZE];
2010
d5a89d7d
KS
2011 strscpy(tmp, sizeof(tmp), filename);
2012 strscpyl(filename, sizeof(filename),
912541b0
KS
2013 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2014 }
2015 }
2016 attr_subst_subdir(filename, sizeof(filename));
2017
2018 match = (stat(filename, &statbuf) == 0);
baa30fbc 2019 if (match && cur->key.mode > 0)
912541b0 2020 match = ((statbuf.st_mode & cur->key.mode) > 0);
912541b0
KS
2021 if (match && cur->key.op == OP_NOMATCH)
2022 goto nomatch;
2023 if (!match && cur->key.op == OP_MATCH)
2024 goto nomatch;
2025 break;
2026 }
2027 case TK_M_EVENT_TIMEOUT:
9f6445e3 2028 log_debug("OPTIONS event_timeout=%u", cur->key.event_timeout);
912541b0
KS
2029 event->timeout_usec = cur->key.event_timeout * 1000 * 1000;
2030 break;
2031 case TK_M_PROGRAM: {
2032 char program[UTIL_PATH_SIZE];
2033 char **envp;
209b031e 2034 char result[UTIL_LINE_SIZE];
912541b0
KS
2035
2036 free(event->program_result);
2037 event->program_result = NULL;
915bf0f6 2038 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
912541b0 2039 envp = udev_device_get_properties_envp(event->dev);
9f6445e3 2040 log_debug("PROGRAM '%s' %s:%u",
baa30fbc 2041 program,
915bf0f6 2042 rules_str(rules, rule->rule.filename_off),
baa30fbc 2043 rule->rule.filename_line);
912541b0
KS
2044
2045 if (udev_event_spawn(event, program, envp, sigmask, result, sizeof(result)) < 0) {
2046 if (cur->key.op != OP_NOMATCH)
2047 goto nomatch;
2048 } else {
2049 int count;
2050
2051 util_remove_trailing_chars(result, '\n');
2052 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2053 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2054 if (count > 0)
9f6445e3 2055 log_debug("%i character(s) replaced" , count);
912541b0
KS
2056 }
2057 event->program_result = strdup(result);
912541b0
KS
2058 if (cur->key.op == OP_NOMATCH)
2059 goto nomatch;
2060 }
2061 break;
2062 }
2063 case TK_M_IMPORT_FILE: {
2064 char import[UTIL_PATH_SIZE];
2065
915bf0f6 2066 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
912541b0
KS
2067 if (import_file_into_properties(event->dev, import) != 0)
2068 if (cur->key.op != OP_NOMATCH)
2069 goto nomatch;
2070 break;
2071 }
2072 case TK_M_IMPORT_PROG: {
2073 char import[UTIL_PATH_SIZE];
2074
915bf0f6 2075 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
9f6445e3 2076 log_debug("IMPORT '%s' %s:%u",
baa30fbc 2077 import,
915bf0f6 2078 rules_str(rules, rule->rule.filename_off),
baa30fbc 2079 rule->rule.filename_line);
912541b0
KS
2080
2081 if (import_program_into_properties(event, import, sigmask) != 0)
2082 if (cur->key.op != OP_NOMATCH)
2083 goto nomatch;
2084 break;
2085 }
2086 case TK_M_IMPORT_BUILTIN: {
2087 char command[UTIL_PATH_SIZE];
2088
2089 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2090 /* check if we ran already */
2091 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
9f6445e3 2092 log_debug("IMPORT builtin skip '%s' %s:%u",
baa30fbc 2093 udev_builtin_name(cur->key.builtin_cmd),
915bf0f6 2094 rules_str(rules, rule->rule.filename_off),
baa30fbc 2095 rule->rule.filename_line);
912541b0
KS
2096 /* return the result from earlier run */
2097 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2098 if (cur->key.op != OP_NOMATCH)
2099 goto nomatch;
2100 break;
2101 }
2102 /* mark as ran */
2103 event->builtin_run |= (1 << cur->key.builtin_cmd);
2104 }
2105
915bf0f6 2106 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
9f6445e3 2107 log_debug("IMPORT builtin '%s' %s:%u",
baa30fbc 2108 udev_builtin_name(cur->key.builtin_cmd),
915bf0f6 2109 rules_str(rules, rule->rule.filename_off),
baa30fbc 2110 rule->rule.filename_line);
912541b0
KS
2111
2112 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2113 /* remember failure */
9f6445e3 2114 log_debug("IMPORT builtin '%s' returned non-zero",
baa30fbc 2115 udev_builtin_name(cur->key.builtin_cmd));
912541b0
KS
2116 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2117 if (cur->key.op != OP_NOMATCH)
2118 goto nomatch;
2119 }
2120 break;
2121 }
2122 case TK_M_IMPORT_DB: {
915bf0f6 2123 const char *key = rules_str(rules, cur->key.value_off);
912541b0
KS
2124 const char *value;
2125
2126 value = udev_device_get_property_value(event->dev_db, key);
2127 if (value != NULL) {
2128 struct udev_list_entry *entry;
2129
2130 entry = udev_device_add_property(event->dev, key, value);
2131 udev_list_entry_set_num(entry, true);
2132 } else {
2133 if (cur->key.op != OP_NOMATCH)
2134 goto nomatch;
2135 }
2136 break;
2137 }
2138 case TK_M_IMPORT_CMDLINE: {
2139 FILE *f;
2140 bool imported = false;
2141
47ef94ac 2142 f = fopen("/proc/cmdline", "re");
912541b0
KS
2143 if (f != NULL) {
2144 char cmdline[4096];
2145
2146 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
915bf0f6 2147 const char *key = rules_str(rules, cur->key.value_off);
912541b0
KS
2148 char *pos;
2149
2150 pos = strstr(cmdline, key);
2151 if (pos != NULL) {
2152 struct udev_list_entry *entry;
2153
2154 pos += strlen(key);
2155 if (pos[0] == '\0' || isspace(pos[0])) {
2156 /* we import simple flags as 'FLAG=1' */
2157 entry = udev_device_add_property(event->dev, key, "1");
2158 udev_list_entry_set_num(entry, true);
2159 imported = true;
2160 } else if (pos[0] == '=') {
2161 const char *value;
2162
2163 pos++;
2164 value = pos;
2165 while (pos[0] != '\0' && !isspace(pos[0]))
2166 pos++;
2167 pos[0] = '\0';
2168 entry = udev_device_add_property(event->dev, key, value);
2169 udev_list_entry_set_num(entry, true);
2170 imported = true;
2171 }
2172 }
2173 }
2174 fclose(f);
2175 }
2176 if (!imported && cur->key.op != OP_NOMATCH)
2177 goto nomatch;
2178 break;
2179 }
2180 case TK_M_IMPORT_PARENT: {
2181 char import[UTIL_PATH_SIZE];
2182
915bf0f6 2183 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
912541b0
KS
2184 if (import_parent_into_properties(event->dev, import) != 0)
2185 if (cur->key.op != OP_NOMATCH)
2186 goto nomatch;
2187 break;
2188 }
2189 case TK_M_RESULT:
2190 if (match_key(rules, cur, event->program_result) != 0)
2191 goto nomatch;
2192 break;
2193 case TK_A_STRING_ESCAPE_NONE:
2194 esc = ESCAPE_NONE;
2195 break;
2196 case TK_A_STRING_ESCAPE_REPLACE:
2197 esc = ESCAPE_REPLACE;
2198 break;
2199 case TK_A_DB_PERSIST:
2200 udev_device_set_db_persist(event->dev);
2201 break;
2202 case TK_A_INOTIFY_WATCH:
2203 if (event->inotify_watch_final)
2204 break;
2205 if (cur->key.op == OP_ASSIGN_FINAL)
2206 event->inotify_watch_final = true;
2207 event->inotify_watch = cur->key.watch;
2208 break;
2209 case TK_A_DEVLINK_PRIO:
2210 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2211 break;
2212 case TK_A_OWNER: {
2213 char owner[UTIL_NAME_SIZE];
2214
2215 if (event->owner_final)
2216 break;
2217 if (cur->key.op == OP_ASSIGN_FINAL)
2218 event->owner_final = true;
915bf0f6 2219 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
1edefa4f 2220 event->owner_set = true;
912541b0 2221 event->uid = util_lookup_user(event->udev, owner);
9f6445e3 2222 log_debug("OWNER %u %s:%u",
baa30fbc 2223 event->uid,
915bf0f6 2224 rules_str(rules, rule->rule.filename_off),
baa30fbc 2225 rule->rule.filename_line);
912541b0
KS
2226 break;
2227 }
2228 case TK_A_GROUP: {
2229 char group[UTIL_NAME_SIZE];
2230
2231 if (event->group_final)
2232 break;
2233 if (cur->key.op == OP_ASSIGN_FINAL)
2234 event->group_final = true;
915bf0f6 2235 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
1edefa4f 2236 event->group_set = true;
912541b0 2237 event->gid = util_lookup_group(event->udev, group);
9f6445e3 2238 log_debug("GROUP %u %s:%u",
baa30fbc 2239 event->gid,
915bf0f6 2240 rules_str(rules, rule->rule.filename_off),
baa30fbc 2241 rule->rule.filename_line);
912541b0
KS
2242 break;
2243 }
2244 case TK_A_MODE: {
2245 char mode_str[UTIL_NAME_SIZE];
2246 mode_t mode;
2247 char *endptr;
2248
2249 if (event->mode_final)
2250 break;
915bf0f6 2251 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
912541b0
KS
2252 mode = strtol(mode_str, &endptr, 8);
2253 if (endptr[0] != '\0') {
9f6445e3 2254 log_error("ignoring invalid mode '%s'", mode_str);
912541b0
KS
2255 break;
2256 }
2257 if (cur->key.op == OP_ASSIGN_FINAL)
2258 event->mode_final = true;
2259 event->mode_set = true;
2260 event->mode = mode;
9f6445e3 2261 log_debug("MODE %#o %s:%u",
baa30fbc 2262 event->mode,
915bf0f6 2263 rules_str(rules, rule->rule.filename_off),
baa30fbc 2264 rule->rule.filename_line);
912541b0
KS
2265 break;
2266 }
2267 case TK_A_OWNER_ID:
2268 if (event->owner_final)
2269 break;
2270 if (cur->key.op == OP_ASSIGN_FINAL)
2271 event->owner_final = true;
1edefa4f 2272 event->owner_set = true;
912541b0 2273 event->uid = cur->key.uid;
9f6445e3 2274 log_debug("OWNER %u %s:%u",
baa30fbc 2275 event->uid,
915bf0f6 2276 rules_str(rules, rule->rule.filename_off),
baa30fbc 2277 rule->rule.filename_line);
912541b0
KS
2278 break;
2279 case TK_A_GROUP_ID:
2280 if (event->group_final)
2281 break;
2282 if (cur->key.op == OP_ASSIGN_FINAL)
2283 event->group_final = true;
1edefa4f 2284 event->group_set = true;
912541b0 2285 event->gid = cur->key.gid;
9f6445e3 2286 log_debug("GROUP %u %s:%u",
baa30fbc 2287 event->gid,
915bf0f6 2288 rules_str(rules, rule->rule.filename_off),
baa30fbc 2289 rule->rule.filename_line);
912541b0
KS
2290 break;
2291 case TK_A_MODE_ID:
2292 if (event->mode_final)
2293 break;
2294 if (cur->key.op == OP_ASSIGN_FINAL)
2295 event->mode_final = true;
2296 event->mode_set = true;
2297 event->mode = cur->key.mode;
9f6445e3 2298 log_debug("MODE %#o %s:%u",
baa30fbc 2299 event->mode,
915bf0f6 2300 rules_str(rules, rule->rule.filename_off),
baa30fbc 2301 rule->rule.filename_line);
912541b0 2302 break;
c26547d6
KS
2303 case TK_A_SECLABEL: {
2304 const char *name, *label;
2305
2306 name = rules_str(rules, cur->key.attr_off);
2307 label = rules_str(rules, cur->key.value_off);
2308 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2309 udev_list_cleanup(&event->seclabel_list);
2310 udev_list_entry_add(&event->seclabel_list, name, label);
9f6445e3 2311 log_debug("SECLABEL{%s}='%s' %s:%u",
c26547d6
KS
2312 name, label,
2313 rules_str(rules, rule->rule.filename_off),
2314 rule->rule.filename_line);
2315 break;
2316 }
912541b0 2317 case TK_A_ENV: {
915bf0f6
KS
2318 const char *name = rules_str(rules, cur->key.attr_off);
2319 char *value = rules_str(rules, cur->key.value_off);
07845c14
KS
2320 char value_new[UTIL_NAME_SIZE];
2321 const char *value_old = NULL;
2322 struct udev_list_entry *entry;
912541b0 2323
07845c14
KS
2324 if (value[0] == '\0') {
2325 if (cur->key.op == OP_ADD)
2326 break;
912541b0 2327 udev_device_add_property(event->dev, name, NULL);
07845c14 2328 break;
912541b0 2329 }
07845c14
KS
2330
2331 if (cur->key.op == OP_ADD)
2332 value_old = udev_device_get_property_value(event->dev, name);
2333 if (value_old) {
2334 char temp[UTIL_NAME_SIZE];
2335
2336 /* append value separated by space */
2337 udev_event_apply_format(event, value, temp, sizeof(temp));
d5a89d7d 2338 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
07845c14
KS
2339 } else
2340 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2341
2342 entry = udev_device_add_property(event->dev, name, value_new);
2343 /* store in db, skip private keys */
2344 if (name[0] != '.')
2345 udev_list_entry_set_num(entry, true);
912541b0
KS
2346 break;
2347 }
2348 case TK_A_TAG: {
2349 char tag[UTIL_PATH_SIZE];
2350 const char *p;
2351
915bf0f6 2352 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
912541b0
KS
2353 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2354 udev_device_cleanup_tags_list(event->dev);
2355 for (p = tag; *p != '\0'; p++) {
2356 if ((*p >= 'a' && *p <= 'z') ||
2357 (*p >= 'A' && *p <= 'Z') ||
2358 (*p >= '0' && *p <= '9') ||
2359 *p == '-' || *p == '_')
2360 continue;
9f6445e3 2361 log_error("ignoring invalid tag name '%s'", tag);
912541b0
KS
2362 break;
2363 }
2364 udev_device_add_tag(event->dev, tag);
2365 break;
2366 }
2367 case TK_A_NAME: {
915bf0f6 2368 const char *name = rules_str(rules, cur->key.value_off);
0ecfcbd4 2369
912541b0
KS
2370 char name_str[UTIL_PATH_SIZE];
2371 int count;
2372
2373 if (event->name_final)
2374 break;
2375 if (cur->key.op == OP_ASSIGN_FINAL)
2376 event->name_final = true;
2377 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2378 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2379 count = util_replace_chars(name_str, "/");
2380 if (count > 0)
9f6445e3 2381 log_debug("%i character(s) replaced", count);
912541b0 2382 }
6ada823a 2383 if (major(udev_device_get_devnum(event->dev)) &&
4cb72937 2384 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
6ada823a
KS
2385 log_error("NAME=\"%s\" ignored, kernel device nodes "
2386 "can not be renamed; please fix it in %s:%u\n", name,
915bf0f6 2387 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
6ada823a 2388 break;
0ecfcbd4 2389 }
912541b0
KS
2390 free(event->name);
2391 event->name = strdup(name_str);
9f6445e3 2392 log_debug("NAME '%s' %s:%u",
baa30fbc 2393 event->name,
915bf0f6 2394 rules_str(rules, rule->rule.filename_off),
baa30fbc 2395 rule->rule.filename_line);
912541b0
KS
2396 break;
2397 }
2398 case TK_A_DEVLINK: {
2399 char temp[UTIL_PATH_SIZE];
2400 char filename[UTIL_PATH_SIZE];
2401 char *pos, *next;
2402 int count = 0;
2403
2404 if (event->devlink_final)
2405 break;
2406 if (major(udev_device_get_devnum(event->dev)) == 0)
2407 break;
2408 if (cur->key.op == OP_ASSIGN_FINAL)
2409 event->devlink_final = true;
2410 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2411 udev_device_cleanup_devlinks_list(event->dev);
2412
2413 /* allow multiple symlinks separated by spaces */
915bf0f6 2414 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
912541b0
KS
2415 if (esc == ESCAPE_UNSET)
2416 count = util_replace_chars(temp, "/ ");
2417 else if (esc == ESCAPE_REPLACE)
2418 count = util_replace_chars(temp, "/");
2419 if (count > 0)
9f6445e3 2420 log_debug("%i character(s) replaced" , count);
912541b0
KS
2421 pos = temp;
2422 while (isspace(pos[0]))
2423 pos++;
2424 next = strchr(pos, ' ');
2425 while (next != NULL) {
2426 next[0] = '\0';
9f6445e3 2427 log_debug("LINK '%s' %s:%u", pos,
915bf0f6 2428 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
d5a89d7d 2429 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
8a173387 2430 udev_device_add_devlink(event->dev, filename);
912541b0
KS
2431 while (isspace(next[1]))
2432 next++;
2433 pos = &next[1];
2434 next = strchr(pos, ' ');
2435 }
2436 if (pos[0] != '\0') {
9f6445e3 2437 log_debug("LINK '%s' %s:%u", pos,
915bf0f6 2438 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
d5a89d7d 2439 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
8a173387 2440 udev_device_add_devlink(event->dev, filename);
912541b0
KS
2441 }
2442 break;
2443 }
2444 case TK_A_ATTR: {
915bf0f6 2445 const char *key_name = rules_str(rules, cur->key.attr_off);
912541b0
KS
2446 char attr[UTIL_PATH_SIZE];
2447 char value[UTIL_NAME_SIZE];
2448 FILE *f;
2449
2450 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
d5a89d7d 2451 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
912541b0
KS
2452 attr_subst_subdir(attr, sizeof(attr));
2453
915bf0f6 2454 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
9f6445e3 2455 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
915bf0f6 2456 rules_str(rules, rule->rule.filename_off),
baa30fbc 2457 rule->rule.filename_line);
47ef94ac 2458 f = fopen(attr, "we");
912541b0
KS
2459 if (f != NULL) {
2460 if (fprintf(f, "%s", value) <= 0)
9f6445e3 2461 log_error("error writing ATTR{%s}: %m", attr);
912541b0
KS
2462 fclose(f);
2463 } else {
9f6445e3 2464 log_error("error opening ATTR{%s} for writing: %m", attr);
912541b0
KS
2465 }
2466 break;
2467 }
83cd6b75
KS
2468 case TK_A_RUN_BUILTIN:
2469 case TK_A_RUN_PROGRAM: {
2470 struct udev_list_entry *entry;
2471
912541b0
KS
2472 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2473 udev_list_cleanup(&event->run_list);
9f6445e3 2474 log_debug("RUN '%s' %s:%u",
915bf0f6
KS
2475 rules_str(rules, cur->key.value_off),
2476 rules_str(rules, rule->rule.filename_off),
baa30fbc 2477 rule->rule.filename_line);
915bf0f6 2478 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
83cd6b75 2479 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
912541b0
KS
2480 break;
2481 }
2482 case TK_A_GOTO:
2483 if (cur->key.rule_goto == 0)
2484 break;
2485 cur = &rules->tokens[cur->key.rule_goto];
2486 continue;
2487 case TK_END:
2488 return 0;
2489
2490 case TK_M_PARENTS_MIN:
2491 case TK_M_PARENTS_MAX:
2492 case TK_M_MAX:
2493 case TK_UNSET:
9f6445e3 2494 log_error("wrong type %u", cur->type);
912541b0
KS
2495 goto nomatch;
2496 }
2497
2498 cur++;
2499 continue;
2500 nomatch:
2501 /* fast-forward to next rule */
2502 cur = rule + rule->rule.token_count;
912541b0 2503 }
6880b25d 2504}
761dfddc 2505
84b6ad70 2506int udev_rules_apply_static_dev_perms(struct udev_rules *rules)
761dfddc 2507{
912541b0
KS
2508 struct token *cur;
2509 struct token *rule;
2510 uid_t uid = 0;
2511 gid_t gid = 0;
2512 mode_t mode = 0;
84b6ad70
TG
2513 _cleanup_strv_free_ char **tags = NULL;
2514 char **t;
2515 FILE *f = NULL;
2516 _cleanup_free_ char *path = NULL;
2517 int r = 0;
912541b0
KS
2518
2519 if (rules->tokens == NULL)
84b6ad70 2520 return 0;
912541b0
KS
2521
2522 cur = &rules->tokens[0];
2523 rule = cur;
2524 for (;;) {
2525 switch (cur->type) {
2526 case TK_RULE:
2527 /* current rule */
2528 rule = cur;
2529
2530 /* skip rules without a static_node tag */
2531 if (!rule->rule.has_static_node)
2532 goto next;
2533
2534 uid = 0;
2535 gid = 0;
2536 mode = 0;
84b6ad70
TG
2537 strv_free(tags);
2538 tags = NULL;
912541b0
KS
2539 break;
2540 case TK_A_OWNER_ID:
2541 uid = cur->key.uid;
2542 break;
2543 case TK_A_GROUP_ID:
2544 gid = cur->key.gid;
2545 break;
2546 case TK_A_MODE_ID:
2547 mode = cur->key.mode;
84b6ad70
TG
2548 break;
2549 case TK_A_TAG:
2550 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2551 if (r < 0)
2552 goto finish;
2553
912541b0
KS
2554 break;
2555 case TK_A_STATIC_NODE: {
84b6ad70
TG
2556 char device_node[UTIL_PATH_SIZE];
2557 char tags_dir[UTIL_PATH_SIZE];
2558 char tag_symlink[UTIL_PATH_SIZE];
912541b0
KS
2559 struct stat stats;
2560
2561 /* we assure, that the permissions tokens are sorted before the static token */
ca2bb160 2562
84b6ad70 2563 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
912541b0 2564 goto next;
d6f116a7 2565
84b6ad70 2566 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
ca2bb160
KS
2567 if (stat(device_node, &stats) != 0)
2568 break;
2569 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2570 break;
84b6ad70 2571
d6f116a7 2572 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
84b6ad70 2573 if (tags) {
84b6ad70
TG
2574 STRV_FOREACH(t, tags) {
2575 _cleanup_free_ char *unescaped_filename = NULL;
2576
2577 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2578 r = mkdir_p(tags_dir, 0755);
2579 if (r < 0) {
9f6445e3 2580 log_error("failed to create %s: %s", tags_dir, strerror(-r));
84b6ad70
TG
2581 return r;
2582 }
2583
2584 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2585
2586 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2587 r = symlink(device_node, tag_symlink);
2588 if (r < 0 && errno != EEXIST) {
9f6445e3 2589 log_error("failed to create symlink %s -> %s: %m", tag_symlink, device_node);
84b6ad70
TG
2590 return -errno;
2591 } else
2592 r = 0;
2593 }
2594 }
2595
15a72200
TG
2596 /* don't touch the permissions if only the tags were set */
2597 if (mode == 0 && uid == 0 && gid == 0)
d6f116a7
KS
2598 break;
2599
912541b0
KS
2600 if (mode == 0) {
2601 if (gid > 0)
2602 mode = 0660;
2603 else
2604 mode = 0600;
2605 }
2606 if (mode != (stats.st_mode & 01777)) {
490f0087
TG
2607 r = chmod(device_node, mode);
2608 if (r < 0) {
9f6445e3 2609 log_error("failed to chmod '%s' %#o", device_node, mode);
490f0087
TG
2610 return -errno;
2611 } else
9f6445e3 2612 log_debug("chmod '%s' %#o", device_node, mode);
912541b0
KS
2613 }
2614
2615 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
490f0087
TG
2616 r = chown(device_node, uid, gid);
2617 if (r < 0) {
9f6445e3 2618 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
490f0087
TG
2619 return -errno;
2620 } else
9f6445e3 2621 log_debug("chown '%s' %u %u", device_node, uid, gid);
912541b0
KS
2622 }
2623
84b6ad70 2624 utimensat(AT_FDCWD, device_node, NULL, 0);
912541b0
KS
2625 break;
2626 }
2627 case TK_END:
84b6ad70 2628 goto finish;
912541b0
KS
2629 }
2630
2631 cur++;
2632 continue;
761dfddc 2633next:
912541b0
KS
2634 /* fast-forward to next rule */
2635 cur = rule + rule->rule.token_count;
2636 continue;
2637 }
84b6ad70
TG
2638
2639finish:
2640 if (f) {
2641 fflush(f);
2642 fchmod(fileno(f), 0644);
2643 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2644 r = -errno;
2645 unlink("/run/udev/static_node-tags");
2646 unlink(path);
2647 }
2648 fclose(f);
2649 }
2650
2651 return r;
761dfddc 2652}