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