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