| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <ctype.h> |
| 4 |
#include <fnmatch.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-json.h" |
| 8 |
#include "sd-messages.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "architecture.h" |
| 12 |
#include "chase.h" /* IWYU pragma: keep */ |
| 13 |
#include "conf-files.h" |
| 14 |
#include "conf-parser.h" |
| 15 |
#include "confidential-virt.h" |
| 16 |
#include "constants.h" |
| 17 |
#include "device-private.h" |
| 18 |
#include "device-util.h" |
| 19 |
#include "dirent-util.h" |
| 20 |
#include "errno-util.h" |
| 21 |
#include "escape.h" |
| 22 |
#include "extract-word.h" |
| 23 |
#include "fd-util.h" |
| 24 |
#include "fileio.h" |
| 25 |
#include "format-util.h" |
| 26 |
#include "fs-util.h" |
| 27 |
#include "glob-util.h" |
| 28 |
#include "hashmap.h" |
| 29 |
#include "list.h" |
| 30 |
#include "memstream-util.h" |
| 31 |
#include "netif-naming-scheme.h" |
| 32 |
#include "nulstr-util.h" |
| 33 |
#include "parse-util.h" |
| 34 |
#include "path-util.h" |
| 35 |
#include "proc-cmdline.h" |
| 36 |
#include "socket-util.h" |
| 37 |
#include "stat-util.h" |
| 38 |
#include "string-table.h" |
| 39 |
#include "string-util.h" |
| 40 |
#include "strv.h" |
| 41 |
#include "strxcpyx.h" |
| 42 |
#include "sysctl-util.h" |
| 43 |
#include "syslog-util.h" |
| 44 |
#include "udev-builtin.h" |
| 45 |
#include "udev-dump.h" |
| 46 |
#include "udev-event.h" |
| 47 |
#include "udev-format.h" |
| 48 |
#include "udev-node.h" |
| 49 |
#include "udev-rules.h" |
| 50 |
#include "udev-spawn.h" |
| 51 |
#include "udev-trace.h" |
| 52 |
#include "udev-util.h" |
| 53 |
#include "udev-worker.h" |
| 54 |
#include "uid-classification.h" |
| 55 |
#include "user-record.h" |
| 56 |
#include "user-util.h" |
| 57 |
#include "userdb.h" |
| 58 |
#include "virt.h" |
| 59 |
|
| 60 |
#define RULES_DIRS ((const char* const*) CONF_PATHS_STRV("udev/rules.d")) |
| 61 |
|
| 62 |
typedef enum { |
| 63 |
OP_MATCH, /* == */ |
| 64 |
OP_NOMATCH, /* != */ |
| 65 |
OP_ADD, /* += */ |
| 66 |
OP_REMOVE, /* -= */ |
| 67 |
OP_ASSIGN, /* = */ |
| 68 |
OP_ASSIGN_FINAL, /* := */ |
| 69 |
_OP_TYPE_MAX, |
| 70 |
_OP_TYPE_INVALID = -EINVAL, |
| 71 |
} UdevRuleOperatorType; |
| 72 |
|
| 73 |
typedef enum { |
| 74 |
MATCH_TYPE_EMPTY, /* empty string */ |
| 75 |
MATCH_TYPE_PLAIN, /* no special characters */ |
| 76 |
MATCH_TYPE_PLAIN_WITH_EMPTY, /* no special characters with empty string, e.g., "|foo" */ |
| 77 |
MATCH_TYPE_GLOB, /* shell globs ?,*,[] */ |
| 78 |
MATCH_TYPE_GLOB_WITH_EMPTY, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */ |
| 79 |
MATCH_TYPE_SUBSYSTEM, /* "subsystem", "bus", or "class" */ |
| 80 |
_MATCH_TYPE_MAX, |
| 81 |
|
| 82 |
_MATCH_TYPE_MASK = (1 << 5) - 1, |
| 83 |
MATCH_REMOVE_TRAILING_WHITESPACE = 1 << 5, /* Remove trailing whitespaces in attribute */ |
| 84 |
MATCH_CASE_INSENSITIVE = 1 << 6, /* string or pattern is matched case-insensitively */ |
| 85 |
_MATCH_TYPE_INVALID = -EINVAL, |
| 86 |
} UdevRuleMatchType; |
| 87 |
|
| 88 |
assert_cc(_MATCH_TYPE_MAX <= _MATCH_TYPE_MASK); |
| 89 |
|
| 90 |
typedef enum { |
| 91 |
SUBST_TYPE_PLAIN, /* no substitution */ |
| 92 |
SUBST_TYPE_FORMAT, /* % or $ */ |
| 93 |
SUBST_TYPE_SUBSYS, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */ |
| 94 |
_SUBST_TYPE_MAX, |
| 95 |
_SUBST_TYPE_INVALID = -EINVAL, |
| 96 |
} UdevRuleSubstituteType; |
| 97 |
|
| 98 |
typedef enum { |
| 99 |
/* lvalues which take match or nomatch operator */ |
| 100 |
TK_M_ACTION, /* string, device_get_action() */ |
| 101 |
TK_M_DEVPATH, /* path, sd_device_get_devpath() */ |
| 102 |
TK_M_KERNEL, /* string, sd_device_get_sysname() */ |
| 103 |
TK_M_DEVLINK, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */ |
| 104 |
TK_M_NAME, /* string, name of network interface */ |
| 105 |
TK_M_ENV, /* string, device property, takes key through attribute */ |
| 106 |
TK_M_CONST, /* string, system-specific hard-coded constant */ |
| 107 |
TK_M_TAG, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */ |
| 108 |
TK_M_SUBSYSTEM, /* string, sd_device_get_subsystem() */ |
| 109 |
TK_M_DRIVER, /* string, sd_device_get_driver() */ |
| 110 |
TK_M_ATTR, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */ |
| 111 |
TK_M_SYSCTL, /* string, takes kernel parameter through attribute */ |
| 112 |
|
| 113 |
/* matches parent parameters */ |
| 114 |
TK_M_PARENTS_KERNEL, /* string */ |
| 115 |
TK_M_PARENTS_SUBSYSTEM, /* string */ |
| 116 |
TK_M_PARENTS_DRIVER, /* string */ |
| 117 |
TK_M_PARENTS_ATTR, /* string */ |
| 118 |
TK_M_PARENTS_TAG, /* strv */ |
| 119 |
|
| 120 |
TK_M_TEST, /* path, optionally mode_t can be specified by attribute, test the existence of a file */ |
| 121 |
TK_M_PROGRAM, /* string, execute a program */ |
| 122 |
TK_M_IMPORT_FILE, /* path */ |
| 123 |
TK_M_IMPORT_PROGRAM, /* string, import properties from the result of program */ |
| 124 |
TK_M_IMPORT_BUILTIN, /* string, import properties from the result of built-in command */ |
| 125 |
TK_M_IMPORT_DB, /* string, import properties from database */ |
| 126 |
TK_M_IMPORT_CMDLINE, /* string, kernel command line */ |
| 127 |
TK_M_IMPORT_PARENT, /* string, parent property */ |
| 128 |
TK_M_RESULT, /* string, result of TK_M_PROGRAM */ |
| 129 |
|
| 130 |
#define _TK_M_MAX (TK_M_RESULT + 1) |
| 131 |
#define _TK_A_MIN _TK_M_MAX |
| 132 |
|
| 133 |
/* lvalues which take one of assign operators */ |
| 134 |
TK_A_OPTIONS_DUMP, /* no argument */ |
| 135 |
TK_A_OPTIONS_STRING_ESCAPE_NONE, /* no argument */ |
| 136 |
TK_A_OPTIONS_STRING_ESCAPE_REPLACE, /* no argument */ |
| 137 |
TK_A_OPTIONS_DB_PERSIST, /* no argument */ |
| 138 |
TK_A_OPTIONS_INOTIFY_WATCH, /* boolean */ |
| 139 |
TK_A_OPTIONS_DEVLINK_PRIORITY, /* int */ |
| 140 |
TK_A_OPTIONS_LOG_LEVEL, /* string of log level or "reset" */ |
| 141 |
TK_A_OWNER, /* user name */ |
| 142 |
TK_A_GROUP, /* group name */ |
| 143 |
TK_A_MODE, /* mode string */ |
| 144 |
TK_A_OWNER_ID, /* uid_t */ |
| 145 |
TK_A_GROUP_ID, /* gid_t */ |
| 146 |
TK_A_MODE_ID, /* mode_t */ |
| 147 |
TK_A_TAG, /* string */ |
| 148 |
TK_A_OPTIONS_STATIC_NODE, /* device path, /dev/... */ |
| 149 |
TK_A_SECLABEL, /* string with attribute */ |
| 150 |
TK_A_ENV, /* string with attribute */ |
| 151 |
TK_A_NAME, /* ifname */ |
| 152 |
TK_A_DEVLINK, /* string */ |
| 153 |
TK_A_ATTR, /* string with attribute */ |
| 154 |
TK_A_SYSCTL, /* string with attribute */ |
| 155 |
TK_A_RUN_BUILTIN, /* string */ |
| 156 |
TK_A_RUN_PROGRAM, /* string */ |
| 157 |
|
| 158 |
_TK_TYPE_MAX, |
| 159 |
_TK_TYPE_INVALID = -EINVAL, |
| 160 |
} UdevRuleTokenType; |
| 161 |
|
| 162 |
typedef enum { |
| 163 |
LINE_HAS_NAME = 1 << 0, /* has NAME= */ |
| 164 |
LINE_HAS_DEVLINK = 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */ |
| 165 |
LINE_HAS_STATIC_NODE = 1 << 2, /* has OPTIONS=static_node */ |
| 166 |
LINE_HAS_GOTO = 1 << 3, /* has GOTO= */ |
| 167 |
LINE_HAS_LABEL = 1 << 4, /* has LABEL= */ |
| 168 |
LINE_UPDATE_SOMETHING = 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */ |
| 169 |
LINE_IS_REFERENCED = 1 << 6, /* is referenced by GOTO */ |
| 170 |
} UdevRuleLineType; |
| 171 |
|
| 172 |
typedef struct UdevRuleFile UdevRuleFile; |
| 173 |
typedef struct UdevRuleLine UdevRuleLine; |
| 174 |
typedef struct UdevRuleToken UdevRuleToken; |
| 175 |
|
| 176 |
struct UdevRuleToken { |
| 177 |
UdevRuleTokenType type:8; |
| 178 |
UdevRuleOperatorType op:8; |
| 179 |
UdevRuleMatchType match_type:8; |
| 180 |
UdevRuleSubstituteType attr_subst_type:8; |
| 181 |
const char *value; |
| 182 |
void *data; |
| 183 |
|
| 184 |
const char *token_str; /* original token string for logging */ |
| 185 |
|
| 186 |
UdevRuleLine *rule_line; |
| 187 |
LIST_FIELDS(UdevRuleToken, tokens); |
| 188 |
}; |
| 189 |
|
| 190 |
struct UdevRuleLine { |
| 191 |
char *line; |
| 192 |
char *line_for_logging; |
| 193 |
unsigned line_number; |
| 194 |
UdevRuleLineType type; |
| 195 |
|
| 196 |
const char *label; |
| 197 |
const char *goto_label; |
| 198 |
UdevRuleLine *goto_line; |
| 199 |
|
| 200 |
UdevRuleFile *rule_file; |
| 201 |
LIST_HEAD(UdevRuleToken, tokens); |
| 202 |
LIST_FIELDS(UdevRuleLine, rule_lines); |
| 203 |
}; |
| 204 |
|
| 205 |
struct UdevRuleFile { |
| 206 |
char *filename; |
| 207 |
unsigned issues; /* used by "udevadm verify" */ |
| 208 |
|
| 209 |
UdevRules *rules; |
| 210 |
LIST_HEAD(UdevRuleLine, rule_lines); |
| 211 |
LIST_FIELDS(UdevRuleFile, rule_files); |
| 212 |
}; |
| 213 |
|
| 214 |
struct UdevRules { |
| 215 |
ResolveNameTiming resolve_name_timing; |
| 216 |
Hashmap *known_users; |
| 217 |
Hashmap *known_groups; |
| 218 |
Hashmap *stats_by_path; |
| 219 |
LIST_HEAD(UdevRuleFile, rule_files); |
| 220 |
}; |
| 221 |
|
| 222 |
#define LINE_GET_RULES(line) \ |
| 223 |
ASSERT_PTR(ASSERT_PTR(ASSERT_PTR(line)->rule_file)->rules) |
| 224 |
|
| 225 |
static bool token_is_for_parents(UdevRuleToken *token) { |
| 226 |
return token->type >= TK_M_PARENTS_KERNEL && token->type <= TK_M_PARENTS_TAG; |
| 227 |
} |
| 228 |
|
| 229 |
/*** Logging helpers ***/ |
| 230 |
|
| 231 |
#define log_udev_event_syntax(event, token, level, message_id, fmt, ...) \ |
| 232 |
({ \ |
| 233 |
UdevEvent *_event = (event); \ |
| 234 |
UdevRuleToken *_token = ASSERT_PTR(token); \ |
| 235 |
int _level = (level); \ |
| 236 |
sd_device *_d = token_is_for_parents(_token) ? _event->dev_parent : _event->dev; \ |
| 237 |
const char *_sysname = NULL; \ |
| 238 |
\ |
| 239 |
if (_d && log_get_max_level() >= LOG_PRI(_level)) \ |
| 240 |
(void) sd_device_get_sysname(_d, &_sysname); \ |
| 241 |
log_struct(_level, \ |
| 242 |
LOG_MESSAGE("%s:%u %s:" fmt, \ |
| 243 |
strna(_token->rule_line->rule_file ? _token->rule_line->rule_file->filename : NULL), \ |
| 244 |
_token->rule_line->line_number, \ |
| 245 |
_token->token_str, \ |
| 246 |
__VA_ARGS__), \ |
| 247 |
LOG_MESSAGE_ID(message_id), \ |
| 248 |
LOG_ITEM("DEVICE=%s", strempty(_sysname))); \ |
| 249 |
}) |
| 250 |
|
| 251 |
#define _log_udev_rule_file_full(device, device_u, file, file_u, line_nr, level, level_u, error, fmt, ...) \ |
| 252 |
({ \ |
| 253 |
int level_u = (level); \ |
| 254 |
sd_device *device_u = (device); \ |
| 255 |
UdevRuleFile *file_u = (file); \ |
| 256 |
\ |
| 257 |
if (!device_u && file_u) \ |
| 258 |
file_u->issues |= (1U << level_u); \ |
| 259 |
\ |
| 260 |
log_device_full_errno_zerook( \ |
| 261 |
device_u, level_u, error, "%s:%u " fmt, \ |
| 262 |
strna(file_u ? file_u->filename : NULL), \ |
| 263 |
line_nr, ##__VA_ARGS__); \ |
| 264 |
}) |
| 265 |
|
| 266 |
#define log_udev_rule_file_full(device, file, line_nr, level, error, fmt, ...) \ |
| 267 |
_log_udev_rule_file_full(device, UNIQ_T(d, UNIQ), file, UNIQ_T(f, UNIQ), line_nr, level, UNIQ_T(l, UNIQ), error, fmt, ##__VA_ARGS__) |
| 268 |
|
| 269 |
#define _log_udev_rule_line_full(device, line, line_u, ...) \ |
| 270 |
({ \ |
| 271 |
UdevRuleLine *line_u = ASSERT_PTR(line); \ |
| 272 |
\ |
| 273 |
log_udev_rule_file_full( \ |
| 274 |
device, \ |
| 275 |
line_u->rule_file, line_u->line_number, \ |
| 276 |
__VA_ARGS__); \ |
| 277 |
}) |
| 278 |
|
| 279 |
#define log_udev_rule_line_full(device, line, ...) \ |
| 280 |
_log_udev_rule_line_full(device, line, UNIQ_T(l, UNIQ), __VA_ARGS__) |
| 281 |
|
| 282 |
/* Mainly used when applying tokens to the event device. */ |
| 283 |
#define _log_event_full_errno_zerook(event, event_u, token, token_u, level, error, fmt, ...) \ |
| 284 |
({ \ |
| 285 |
UdevEvent *event_u = ASSERT_PTR(event); \ |
| 286 |
UdevRuleToken *token_u = ASSERT_PTR(token); \ |
| 287 |
\ |
| 288 |
log_udev_rule_line_full( \ |
| 289 |
token_is_for_parents(token_u) ? event_u->dev_parent : event_u->dev, \ |
| 290 |
token_u->rule_line, \ |
| 291 |
level, error, "%s: " fmt, \ |
| 292 |
token_u->token_str, ##__VA_ARGS__); \ |
| 293 |
}) |
| 294 |
|
| 295 |
#define log_event_full_errno_zerook(event, token, ...) \ |
| 296 |
_log_event_full_errno_zerook(event, UNIQ_T(e, UNIQ), token, UNIQ_T(t, UNIQ), __VA_ARGS__) |
| 297 |
|
| 298 |
#define _log_event_full_errno(event, token, level, error, error_u, ...) \ |
| 299 |
({ \ |
| 300 |
int error_u = (error); \ |
| 301 |
ASSERT_NON_ZERO(error_u); \ |
| 302 |
log_event_full_errno_zerook( \ |
| 303 |
event, token, level, error_u, \ |
| 304 |
__VA_ARGS__); \ |
| 305 |
}) |
| 306 |
|
| 307 |
#define log_event_full_errno(event, token, level, error, ...) \ |
| 308 |
_log_event_full_errno(event, token, level, error, UNIQ_T(e, UNIQ), __VA_ARGS__) |
| 309 |
|
| 310 |
#define log_event_full(event, token, level, ...) (void) log_event_full_errno_zerook(event, token, level, 0, __VA_ARGS__) |
| 311 |
|
| 312 |
#define log_event_debug(event, token, ...) log_event_full(event, token, LOG_DEBUG, __VA_ARGS__) |
| 313 |
#define log_event_info(event, token, ...) log_event_full(event, token, LOG_INFO, __VA_ARGS__) |
| 314 |
#define log_event_notice(event, token, ...) log_event_full(event, token, LOG_NOTICE, __VA_ARGS__) |
| 315 |
#define log_event_warning(event, token, ...) log_event_full(event, token, LOG_WARNING, __VA_ARGS__) |
| 316 |
#define log_event_error(event, token, ...) log_event_full(event, token, LOG_ERR, __VA_ARGS__) |
| 317 |
|
| 318 |
#define log_event_debug_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_DEBUG, error, __VA_ARGS__) |
| 319 |
#define log_event_info_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_INFO, error, __VA_ARGS__) |
| 320 |
#define log_event_notice_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_NOTICE, error, __VA_ARGS__) |
| 321 |
#define log_event_warning_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_WARNING, error, __VA_ARGS__) |
| 322 |
#define log_event_error_errno(event, token, error, ...) log_event_full_errno(event, token, LOG_ERR, error, __VA_ARGS__) |
| 323 |
|
| 324 |
#define _log_event_trace_errno(event, event_u, ...) \ |
| 325 |
({ \ |
| 326 |
UdevEvent *event_u = ASSERT_PTR(event); \ |
| 327 |
\ |
| 328 |
event_u->trace ? \ |
| 329 |
log_event_debug_errno(event_u, __VA_ARGS__) : \ |
| 330 |
(void) 0; \ |
| 331 |
}) |
| 332 |
|
| 333 |
#define log_event_trace_errno(event, ...) \ |
| 334 |
_log_event_trace_errno(event, UNIQ_T(e, UNIQ), __VA_ARGS__) |
| 335 |
|
| 336 |
#define _log_event_trace(event, event_u, ...) \ |
| 337 |
({ \ |
| 338 |
UdevEvent *event_u = ASSERT_PTR(event); \ |
| 339 |
\ |
| 340 |
event_u->trace ? \ |
| 341 |
log_event_debug(event_u, __VA_ARGS__) : \ |
| 342 |
(void) 0; \ |
| 343 |
}) |
| 344 |
|
| 345 |
#define log_event_trace(event, ...) \ |
| 346 |
_log_event_trace(event, UNIQ_T(e, UNIQ), __VA_ARGS__) |
| 347 |
|
| 348 |
#define _log_event_result(event, token, result, result_u) \ |
| 349 |
({ \ |
| 350 |
bool result_u = (result); \ |
| 351 |
\ |
| 352 |
log_event_trace(event, token, "%s", \ |
| 353 |
result_u ? "PASS" : "FAIL"); \ |
| 354 |
result_u; \ |
| 355 |
}) |
| 356 |
|
| 357 |
#define log_event_result(event, token, result) \ |
| 358 |
_log_event_result(event, token, result, UNIQ_T(r, UNIQ)) |
| 359 |
|
| 360 |
#define log_event_done(event, token) \ |
| 361 |
({ \ |
| 362 |
log_event_trace(event, token, "DONE"); \ |
| 363 |
true; \ |
| 364 |
}) |
| 365 |
|
| 366 |
#define _log_event_final_set(event, token, token_u) \ |
| 367 |
({ \ |
| 368 |
UdevRuleToken *token_u = ASSERT_PTR(token); \ |
| 369 |
\ |
| 370 |
log_event_trace(event, token_u, \ |
| 371 |
"Already assigned final value, ignoring further %s.", \ |
| 372 |
token_u->op == OP_REMOVE ? "modification" : "assignment"); \ |
| 373 |
true; \ |
| 374 |
}) |
| 375 |
|
| 376 |
#define log_event_final_set(event, token) \ |
| 377 |
_log_event_final_set(event, token, UNIQ_T(t, UNIQ)) |
| 378 |
|
| 379 |
#define _log_event_truncated(event, token, token_u, what, format) \ |
| 380 |
({ \ |
| 381 |
UdevRuleToken *token_u = ASSERT_PTR(token); \ |
| 382 |
\ |
| 383 |
token_u->type < _TK_M_MAX ? \ |
| 384 |
log_event_debug(event, token_u, \ |
| 385 |
"The %s is truncated while substituting into \"%s\", assuming the token fails.", \ |
| 386 |
what, (const char*) format) : \ |
| 387 |
log_event_warning( \ |
| 388 |
event, token_u, \ |
| 389 |
"The %s is truncated while substituting into \"%s\", refusing to apply the token.", \ |
| 390 |
what, (const char*) format); \ |
| 391 |
}) |
| 392 |
|
| 393 |
#define log_event_truncated(event, token, what, format) \ |
| 394 |
_log_event_truncated(event, token, UNIQ_T(t, UNIQ), what, format) |
| 395 |
|
| 396 |
#define _log_event_line(event, event_u, line, ...) \ |
| 397 |
({ \ |
| 398 |
UdevEvent *event_u = ASSERT_PTR(event); \ |
| 399 |
\ |
| 400 |
event_u->trace ? \ |
| 401 |
log_udev_rule_line_full( \ |
| 402 |
event_u->dev, line, \ |
| 403 |
LOG_DEBUG, 0, __VA_ARGS__) : \ |
| 404 |
0; \ |
| 405 |
}) |
| 406 |
|
| 407 |
#define log_event_line(event, line, ...) \ |
| 408 |
_log_event_line(event, UNIQ_T(e, UNIQ), line, __VA_ARGS__) |
| 409 |
|
| 410 |
/* Mainly used when parsing .rules files. */ |
| 411 |
#define log_file_full_errno_zerook(...) \ |
| 412 |
log_udev_rule_file_full(NULL, __VA_ARGS__) |
| 413 |
|
| 414 |
#define log_file_error(file, line_nr, ...) \ |
| 415 |
log_file_full_errno_zerook(file, line_nr, LOG_ERR, 0, __VA_ARGS__) |
| 416 |
|
| 417 |
#define log_line_full_errno_zerook(...) \ |
| 418 |
log_udev_rule_line_full(NULL, __VA_ARGS__) |
| 419 |
|
| 420 |
#define log_line_full_errno(line, level, error, ...) \ |
| 421 |
log_udev_rule_line_full(NULL, line, level, error, __VA_ARGS__) |
| 422 |
|
| 423 |
#define log_line_full(line, level, ...) (void) log_line_full_errno_zerook(line, level, 0, __VA_ARGS__) |
| 424 |
|
| 425 |
#define log_line_debug(line, ...) log_line_full(line, LOG_DEBUG, __VA_ARGS__) |
| 426 |
#define log_line_info(line, ...) log_line_full(line, LOG_INFO, __VA_ARGS__) |
| 427 |
#define log_line_notice(line, ...) log_line_full(line, LOG_NOTICE, __VA_ARGS__) |
| 428 |
#define log_line_warning(line, ...) log_line_full(line, LOG_WARNING, __VA_ARGS__) |
| 429 |
#define log_line_error(line, ...) log_line_full(line, LOG_ERR, __VA_ARGS__) |
| 430 |
|
| 431 |
#define log_line_debug_errno(line, error, ...) log_line_full_errno(line, LOG_DEBUG, error, __VA_ARGS__) |
| 432 |
#define log_line_info_errno(line, error, ...) log_line_full_errno(line, LOG_INFO, error, __VA_ARGS__) |
| 433 |
#define log_line_notice_errno(line, error, ...) log_line_full_errno(line, LOG_NOTICE, error, __VA_ARGS__) |
| 434 |
#define log_line_warning_errno(line, error, ...) log_line_full_errno(line, LOG_WARNING, error, __VA_ARGS__) |
| 435 |
#define log_line_error_errno(line, error, ...) log_line_full_errno(line, LOG_ERR, error, __VA_ARGS__) |
| 436 |
|
| 437 |
#define _log_line_invalid_token(line, key, type) \ |
| 438 |
log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \ |
| 439 |
"Invalid %s for %s.", type, key) |
| 440 |
|
| 441 |
#define log_line_invalid_op(line, key) _log_line_invalid_token(line, key, "operator") |
| 442 |
#define log_line_invalid_attr(line, key) _log_line_invalid_token(line, key, "attribute") |
| 443 |
#define log_line_invalid_prefix(line, key) _log_line_invalid_token(line, key, "prefix 'i'") |
| 444 |
|
| 445 |
#define log_line_invalid_attr_format(line, key, attr, offset, hint) \ |
| 446 |
log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \ |
| 447 |
"Invalid attribute \"%s\" for %s (char %zu: %s), ignoring.", \ |
| 448 |
attr, key, offset, hint) |
| 449 |
#define log_line_invalid_value(line, key, value, offset, hint) \ |
| 450 |
log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \ |
| 451 |
"Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \ |
| 452 |
value, key, offset, hint) |
| 453 |
|
| 454 |
/*** Other functions ***/ |
| 455 |
|
| 456 |
static UdevRuleToken* udev_rule_token_free(UdevRuleToken *token) { |
| 457 |
if (!token) |
| 458 |
return NULL; |
| 459 |
|
| 460 |
if (token->rule_line) |
| 461 |
LIST_REMOVE(tokens, token->rule_line->tokens, token); |
| 462 |
|
| 463 |
return mfree(token); |
| 464 |
} |
| 465 |
|
| 466 |
DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleToken*, udev_rule_token_free); |
| 467 |
|
| 468 |
static void udev_rule_line_clear_tokens(UdevRuleLine *rule_line) { |
| 469 |
assert(rule_line); |
| 470 |
|
| 471 |
LIST_FOREACH(tokens, i, rule_line->tokens) |
| 472 |
udev_rule_token_free(i); |
| 473 |
} |
| 474 |
|
| 475 |
static UdevRuleLine* udev_rule_line_free(UdevRuleLine *rule_line) { |
| 476 |
if (!rule_line) |
| 477 |
return NULL; |
| 478 |
|
| 479 |
udev_rule_line_clear_tokens(rule_line); |
| 480 |
|
| 481 |
if (rule_line->rule_file) |
| 482 |
LIST_REMOVE(rule_lines, rule_line->rule_file->rule_lines, rule_line); |
| 483 |
|
| 484 |
free(rule_line->line); |
| 485 |
free(rule_line->line_for_logging); |
| 486 |
return mfree(rule_line); |
| 487 |
} |
| 488 |
|
| 489 |
DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine*, udev_rule_line_free); |
| 490 |
|
| 491 |
static UdevRuleFile* udev_rule_file_free(UdevRuleFile *rule_file) { |
| 492 |
if (!rule_file) |
| 493 |
return NULL; |
| 494 |
|
| 495 |
LIST_FOREACH(rule_lines, i, rule_file->rule_lines) |
| 496 |
udev_rule_line_free(i); |
| 497 |
|
| 498 |
if (rule_file->rules) |
| 499 |
LIST_REMOVE(rule_files, rule_file->rules->rule_files, rule_file); |
| 500 |
|
| 501 |
free(rule_file->filename); |
| 502 |
return mfree(rule_file); |
| 503 |
} |
| 504 |
|
| 505 |
DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleFile*, udev_rule_file_free); |
| 506 |
|
| 507 |
UdevRules* udev_rules_free(UdevRules *rules) { |
| 508 |
if (!rules) |
| 509 |
return NULL; |
| 510 |
|
| 511 |
LIST_FOREACH(rule_files, i, rules->rule_files) |
| 512 |
udev_rule_file_free(i); |
| 513 |
|
| 514 |
hashmap_free(rules->known_users); |
| 515 |
hashmap_free(rules->known_groups); |
| 516 |
hashmap_free(rules->stats_by_path); |
| 517 |
return mfree(rules); |
| 518 |
} |
| 519 |
|
| 520 |
static int rule_resolve_user(UdevRuleLine *rule_line, const char *name, uid_t *ret) { |
| 521 |
Hashmap **known_users = &LINE_GET_RULES(rule_line)->known_users; |
| 522 |
int r; |
| 523 |
|
| 524 |
assert(name); |
| 525 |
assert(ret); |
| 526 |
|
| 527 |
void *val = hashmap_get(*known_users, name); |
| 528 |
if (val) { |
| 529 |
*ret = PTR_TO_UID(val); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 534 |
r = userdb_by_name(name, /* match= */ NULL, |
| 535 |
USERDB_SUPPRESS_SHADOW | USERDB_PARSE_NUMERIC | USERDB_SYNTHESIZE_NUMERIC, |
| 536 |
&ur); |
| 537 |
if (r < 0) |
| 538 |
return log_line_error_errno(rule_line, r, |
| 539 |
"Failed to resolve user '%s', ignoring: %s", |
| 540 |
name, STRERROR_USER(r)); |
| 541 |
if (!uid_is_system(ur->uid)) |
| 542 |
log_struct(LOG_WARNING, |
| 543 |
LOG_MESSAGE("%s:%u User %s configured to own a device node is not a system user. " |
| 544 |
"Support for device node ownership by non-system accounts is deprecated and will be removed in the future.", |
| 545 |
rule_line->rule_file->filename, rule_line->line_number, name), |
| 546 |
LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_ACCOUNT_REQUIRED_STR)); |
| 547 |
|
| 548 |
_cleanup_free_ char *n = strdup(name); |
| 549 |
if (!n) |
| 550 |
return log_oom(); |
| 551 |
|
| 552 |
r = hashmap_ensure_put(known_users, &string_hash_ops_free, n, UID_TO_PTR(ur->uid)); |
| 553 |
if (r < 0) |
| 554 |
return log_oom(); |
| 555 |
|
| 556 |
TAKE_PTR(n); |
| 557 |
*ret = ur->uid; |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
static int rule_resolve_group(UdevRuleLine *rule_line, const char *name, gid_t *ret) { |
| 562 |
Hashmap **known_groups = &LINE_GET_RULES(rule_line)->known_groups; |
| 563 |
int r; |
| 564 |
|
| 565 |
assert(name); |
| 566 |
assert(ret); |
| 567 |
|
| 568 |
void *val = hashmap_get(*known_groups, name); |
| 569 |
if (val) { |
| 570 |
*ret = PTR_TO_GID(val); |
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL; |
| 575 |
r = groupdb_by_name(name, /* match= */ NULL, |
| 576 |
USERDB_SUPPRESS_SHADOW | USERDB_PARSE_NUMERIC | USERDB_SYNTHESIZE_NUMERIC, |
| 577 |
&gr); |
| 578 |
if (r < 0) |
| 579 |
return log_line_error_errno(rule_line, r, |
| 580 |
"Failed to resolve group '%s', ignoring: %s", |
| 581 |
name, STRERROR_GROUP(r)); |
| 582 |
if (!gid_is_system(gr->gid)) |
| 583 |
log_struct(LOG_WARNING, |
| 584 |
LOG_MESSAGE("%s:%u Group %s configured to own a device node is not a system group. " |
| 585 |
"Support for device node ownership by non-system accounts is deprecated and will be removed in the future.", |
| 586 |
rule_line->rule_file->filename, rule_line->line_number, name), |
| 587 |
LOG_MESSAGE_ID(SD_MESSAGE_SYSTEM_ACCOUNT_REQUIRED_STR)); |
| 588 |
|
| 589 |
_cleanup_free_ char *n = strdup(name); |
| 590 |
if (!n) |
| 591 |
return log_oom(); |
| 592 |
|
| 593 |
r = hashmap_ensure_put(known_groups, &string_hash_ops_free, n, GID_TO_PTR(gr->gid)); |
| 594 |
if (r < 0) |
| 595 |
return log_oom(); |
| 596 |
|
| 597 |
TAKE_PTR(n); |
| 598 |
*ret = gr->gid; |
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
static UdevRuleSubstituteType rule_get_substitution_type(const char *str) { |
| 603 |
assert(str); |
| 604 |
|
| 605 |
if (str[0] == '[') |
| 606 |
return SUBST_TYPE_SUBSYS; |
| 607 |
if (strchr(str, '%') || strchr(str, '$')) |
| 608 |
return SUBST_TYPE_FORMAT; |
| 609 |
return SUBST_TYPE_PLAIN; |
| 610 |
} |
| 611 |
|
| 612 |
static bool type_has_nulstr_value(UdevRuleTokenType type) { |
| 613 |
return type < TK_M_TEST || type == TK_M_RESULT; |
| 614 |
} |
| 615 |
|
| 616 |
static int rule_line_add_token( |
| 617 |
UdevRuleLine *rule_line, |
| 618 |
UdevRuleTokenType type, |
| 619 |
UdevRuleOperatorType op, |
| 620 |
char *value, |
| 621 |
void *data, |
| 622 |
bool is_case_insensitive, |
| 623 |
const char *token_str) { |
| 624 |
|
| 625 |
_cleanup_(udev_rule_token_freep) UdevRuleToken *token = NULL; |
| 626 |
UdevRuleMatchType match_type = _MATCH_TYPE_INVALID; |
| 627 |
UdevRuleSubstituteType subst_type = _SUBST_TYPE_INVALID; |
| 628 |
|
| 629 |
assert(rule_line); |
| 630 |
assert(type >= 0 && type < _TK_TYPE_MAX); |
| 631 |
assert(op >= 0 && op < _OP_TYPE_MAX); |
| 632 |
|
| 633 |
if (type < _TK_M_MAX) { |
| 634 |
assert(value); |
| 635 |
assert(IN_SET(op, OP_MATCH, OP_NOMATCH)); |
| 636 |
|
| 637 |
if (type == TK_M_SUBSYSTEM && STR_IN_SET(value, "subsystem", "bus", "class")) |
| 638 |
match_type = MATCH_TYPE_SUBSYSTEM; |
| 639 |
else if (isempty(value)) |
| 640 |
match_type = MATCH_TYPE_EMPTY; |
| 641 |
else if (streq(value, "?*")) { |
| 642 |
/* Convert KEY=="?*" -> KEY!="" */ |
| 643 |
match_type = MATCH_TYPE_EMPTY; |
| 644 |
op = op == OP_MATCH ? OP_NOMATCH : OP_MATCH; |
| 645 |
} else if (string_is_glob(value)) |
| 646 |
match_type = MATCH_TYPE_GLOB; |
| 647 |
else |
| 648 |
match_type = MATCH_TYPE_PLAIN; |
| 649 |
|
| 650 |
if (type_has_nulstr_value(type)) { |
| 651 |
/* Convert value string to nulstr. */ |
| 652 |
bool bar = true, empty = false; |
| 653 |
char *a, *b; |
| 654 |
|
| 655 |
for (a = b = value; *a != '\0'; a++) { |
| 656 |
if (*a != '|') { |
| 657 |
*b++ = *a; |
| 658 |
bar = false; |
| 659 |
} else { |
| 660 |
if (bar) |
| 661 |
empty = true; |
| 662 |
else |
| 663 |
*b++ = '\0'; |
| 664 |
bar = true; |
| 665 |
} |
| 666 |
} |
| 667 |
*b = '\0'; |
| 668 |
|
| 669 |
/* Make sure the value is end, so NULSTR_FOREACH can read correct match */ |
| 670 |
if (b < a) |
| 671 |
b[1] = '\0'; |
| 672 |
|
| 673 |
if (bar) |
| 674 |
empty = true; |
| 675 |
|
| 676 |
if (empty) { |
| 677 |
if (match_type == MATCH_TYPE_GLOB) |
| 678 |
match_type = MATCH_TYPE_GLOB_WITH_EMPTY; |
| 679 |
if (match_type == MATCH_TYPE_PLAIN) |
| 680 |
match_type = MATCH_TYPE_PLAIN_WITH_EMPTY; |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
if (IN_SET(type, TK_M_ATTR, TK_M_PARENTS_ATTR)) { |
| 686 |
size_t len; |
| 687 |
|
| 688 |
assert(value); |
| 689 |
assert(data); |
| 690 |
assert(match_type >= 0 && match_type < _MATCH_TYPE_MAX); |
| 691 |
|
| 692 |
len = strlen(value); |
| 693 |
if (len > 0 && !isspace(value[len - 1])) |
| 694 |
match_type |= MATCH_REMOVE_TRAILING_WHITESPACE; |
| 695 |
|
| 696 |
subst_type = rule_get_substitution_type(data); |
| 697 |
} |
| 698 |
|
| 699 |
SET_FLAG(match_type, MATCH_CASE_INSENSITIVE, is_case_insensitive); |
| 700 |
|
| 701 |
token = new(UdevRuleToken, 1); |
| 702 |
if (!token) |
| 703 |
return -ENOMEM; |
| 704 |
|
| 705 |
*token = (UdevRuleToken) { |
| 706 |
.type = type, |
| 707 |
.op = op, |
| 708 |
.value = value, |
| 709 |
.data = data, |
| 710 |
.match_type = match_type, |
| 711 |
.attr_subst_type = subst_type, |
| 712 |
.token_str = token_str, |
| 713 |
.rule_line = rule_line, |
| 714 |
}; |
| 715 |
|
| 716 |
LIST_APPEND(tokens, rule_line->tokens, token); |
| 717 |
|
| 718 |
if (token->type == TK_A_NAME) |
| 719 |
SET_FLAG(rule_line->type, LINE_HAS_NAME, true); |
| 720 |
|
| 721 |
else if (IN_SET(token->type, TK_A_DEVLINK, |
| 722 |
TK_A_OWNER, TK_A_GROUP, TK_A_MODE, |
| 723 |
TK_A_OWNER_ID, TK_A_GROUP_ID, TK_A_MODE_ID)) |
| 724 |
SET_FLAG(rule_line->type, LINE_HAS_DEVLINK, true); |
| 725 |
|
| 726 |
else if (token->type == TK_A_OPTIONS_STATIC_NODE) |
| 727 |
SET_FLAG(rule_line->type, LINE_HAS_STATIC_NODE, true); |
| 728 |
|
| 729 |
else if (token->type >= _TK_A_MIN || |
| 730 |
IN_SET(token->type, TK_M_PROGRAM, |
| 731 |
TK_M_IMPORT_FILE, TK_M_IMPORT_PROGRAM, TK_M_IMPORT_BUILTIN, |
| 732 |
TK_M_IMPORT_DB, TK_M_IMPORT_CMDLINE, TK_M_IMPORT_PARENT)) |
| 733 |
SET_FLAG(rule_line->type, LINE_UPDATE_SOMETHING, true); |
| 734 |
|
| 735 |
TAKE_PTR(token); |
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
static void check_value_format_and_warn(UdevRuleLine *line, const char *key, const char *value, bool nonempty) { |
| 740 |
size_t offset; |
| 741 |
const char *hint; |
| 742 |
|
| 743 |
if (nonempty && isempty(value)) |
| 744 |
log_line_invalid_value(line, key, value, (size_t) 0, "empty value"); |
| 745 |
else if (udev_check_format(value, &offset, &hint) < 0) |
| 746 |
log_line_invalid_value(line, key, value, offset + 1, hint); |
| 747 |
} |
| 748 |
|
| 749 |
static int check_attr_format_and_warn(UdevRuleLine *line, const char *key, const char *value) { |
| 750 |
size_t offset; |
| 751 |
const char *hint; |
| 752 |
|
| 753 |
if (isempty(value)) |
| 754 |
return log_line_invalid_attr(line, key); |
| 755 |
if (udev_check_format(value, &offset, &hint) < 0) |
| 756 |
log_line_invalid_attr_format(line, key, value, offset + 1, hint); |
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
static int parse_token( |
| 761 |
UdevRuleLine *rule_line, |
| 762 |
const char *key, |
| 763 |
char *attr, |
| 764 |
UdevRuleOperatorType op, |
| 765 |
char *value, |
| 766 |
bool is_case_insensitive, |
| 767 |
const char *token_str) { |
| 768 |
|
| 769 |
ResolveNameTiming resolve_name_timing = LINE_GET_RULES(rule_line)->resolve_name_timing; |
| 770 |
bool is_match = IN_SET(op, OP_MATCH, OP_NOMATCH); |
| 771 |
int r; |
| 772 |
|
| 773 |
assert(key); |
| 774 |
assert(value); |
| 775 |
|
| 776 |
if (!is_match && is_case_insensitive) |
| 777 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), |
| 778 |
"Invalid prefix 'i' for '%s'. The 'i' prefix can be specified only for '==' or '!=' operator.", key); |
| 779 |
|
| 780 |
if (streq(key, "ACTION")) { |
| 781 |
if (attr) |
| 782 |
return log_line_invalid_attr(rule_line, key); |
| 783 |
if (!is_match) |
| 784 |
return log_line_invalid_op(rule_line, key); |
| 785 |
|
| 786 |
r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL, is_case_insensitive, token_str); |
| 787 |
} else if (streq(key, "DEVPATH")) { |
| 788 |
if (attr) |
| 789 |
return log_line_invalid_attr(rule_line, key); |
| 790 |
if (!is_match) |
| 791 |
return log_line_invalid_op(rule_line, key); |
| 792 |
|
| 793 |
r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL, is_case_insensitive, token_str); |
| 794 |
} else if (streq(key, "KERNEL")) { |
| 795 |
if (attr) |
| 796 |
return log_line_invalid_attr(rule_line, key); |
| 797 |
if (!is_match) |
| 798 |
return log_line_invalid_op(rule_line, key); |
| 799 |
|
| 800 |
r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL, is_case_insensitive, token_str); |
| 801 |
} else if (streq(key, "SYMLINK")) { |
| 802 |
if (attr) |
| 803 |
return log_line_invalid_attr(rule_line, key); |
| 804 |
if (!is_match) { |
| 805 |
check_value_format_and_warn(rule_line, key, value, false); |
| 806 |
r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 807 |
} else |
| 808 |
r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL, is_case_insensitive, token_str); |
| 809 |
} else if (streq(key, "NAME")) { |
| 810 |
if (attr) |
| 811 |
return log_line_invalid_attr(rule_line, key); |
| 812 |
if (op == OP_REMOVE) |
| 813 |
return log_line_invalid_op(rule_line, key); |
| 814 |
if (op == OP_ADD) { |
| 815 |
log_line_warning(rule_line, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key); |
| 816 |
op = OP_ASSIGN; |
| 817 |
} |
| 818 |
|
| 819 |
if (!is_match) { |
| 820 |
if (streq(value, "%k")) |
| 821 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), |
| 822 |
"Ignoring NAME=\"%%k\", as it will take no effect."); |
| 823 |
if (isempty(value)) |
| 824 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), |
| 825 |
"Ignoring NAME=\"\", as udev will not delete any network interfaces."); |
| 826 |
check_value_format_and_warn(rule_line, key, value, false); |
| 827 |
|
| 828 |
r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 829 |
} else |
| 830 |
r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL, is_case_insensitive, token_str); |
| 831 |
} else if (streq(key, "ENV")) { |
| 832 |
if (isempty(attr)) |
| 833 |
return log_line_invalid_attr(rule_line, key); |
| 834 |
if (op == OP_REMOVE) |
| 835 |
return log_line_invalid_op(rule_line, key); |
| 836 |
if (op == OP_ASSIGN_FINAL) { |
| 837 |
log_line_warning(rule_line, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key); |
| 838 |
op = OP_ASSIGN; |
| 839 |
} |
| 840 |
|
| 841 |
if (!is_match) { |
| 842 |
if (!device_property_can_set(attr)) |
| 843 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), |
| 844 |
"Invalid ENV attribute. '%s' cannot be set.", attr); |
| 845 |
|
| 846 |
check_value_format_and_warn(rule_line, key, value, false); |
| 847 |
|
| 848 |
r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr, /* is_case_insensitive= */ false, token_str); |
| 849 |
} else |
| 850 |
r = rule_line_add_token(rule_line, TK_M_ENV, op, value, attr, is_case_insensitive, token_str); |
| 851 |
} else if (streq(key, "CONST")) { |
| 852 |
if (isempty(attr) || !STR_IN_SET(attr, "arch", "virt")) |
| 853 |
return log_line_invalid_attr(rule_line, key); |
| 854 |
if (!is_match) |
| 855 |
return log_line_invalid_op(rule_line, key); |
| 856 |
r = rule_line_add_token(rule_line, TK_M_CONST, op, value, attr, is_case_insensitive, token_str); |
| 857 |
} else if (streq(key, "TAG")) { |
| 858 |
if (attr) |
| 859 |
return log_line_invalid_attr(rule_line, key); |
| 860 |
if (op == OP_ASSIGN_FINAL) { |
| 861 |
log_line_warning(rule_line, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key); |
| 862 |
op = OP_ASSIGN; |
| 863 |
} |
| 864 |
|
| 865 |
if (!is_match) { |
| 866 |
check_value_format_and_warn(rule_line, key, value, true); |
| 867 |
|
| 868 |
r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 869 |
} else |
| 870 |
r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL, is_case_insensitive, token_str); |
| 871 |
} else if (streq(key, "SUBSYSTEM")) { |
| 872 |
if (attr) |
| 873 |
return log_line_invalid_attr(rule_line, key); |
| 874 |
if (!is_match) |
| 875 |
return log_line_invalid_op(rule_line, key); |
| 876 |
|
| 877 |
if (STR_IN_SET(value, "bus", "class")) |
| 878 |
log_line_warning(rule_line, "\"%s\" must be specified as \"subsystem\".", value); |
| 879 |
|
| 880 |
r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL, is_case_insensitive, token_str); |
| 881 |
} else if (streq(key, "DRIVER")) { |
| 882 |
if (attr) |
| 883 |
return log_line_invalid_attr(rule_line, key); |
| 884 |
if (!is_match) |
| 885 |
return log_line_invalid_op(rule_line, key); |
| 886 |
|
| 887 |
r = rule_line_add_token(rule_line, TK_M_DRIVER, op, value, NULL, is_case_insensitive, token_str); |
| 888 |
} else if (streq(key, "ATTR")) { |
| 889 |
r = check_attr_format_and_warn(rule_line, key, attr); |
| 890 |
if (r < 0) |
| 891 |
return r; |
| 892 |
if (op == OP_REMOVE) |
| 893 |
return log_line_invalid_op(rule_line, key); |
| 894 |
if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) { |
| 895 |
log_line_warning(rule_line, "%s key takes '==', '!=', or '=' operator, assuming '='.", key); |
| 896 |
op = OP_ASSIGN; |
| 897 |
} |
| 898 |
|
| 899 |
if (!is_match) { |
| 900 |
check_value_format_and_warn(rule_line, key, value, false); |
| 901 |
r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr, /* is_case_insensitive= */ false, token_str); |
| 902 |
} else |
| 903 |
r = rule_line_add_token(rule_line, TK_M_ATTR, op, value, attr, is_case_insensitive, token_str); |
| 904 |
} else if (streq(key, "SYSCTL")) { |
| 905 |
r = check_attr_format_and_warn(rule_line, key, attr); |
| 906 |
if (r < 0) |
| 907 |
return r; |
| 908 |
if (op == OP_REMOVE) |
| 909 |
return log_line_invalid_op(rule_line, key); |
| 910 |
if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) { |
| 911 |
log_line_warning(rule_line, "%s key takes '==', '!=', or '=' operator, assuming '='.", key); |
| 912 |
op = OP_ASSIGN; |
| 913 |
} |
| 914 |
|
| 915 |
if (!is_match) { |
| 916 |
check_value_format_and_warn(rule_line, key, value, false); |
| 917 |
r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr, /* is_case_insensitive= */ false, token_str); |
| 918 |
} else |
| 919 |
r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr, is_case_insensitive, token_str); |
| 920 |
} else if (streq(key, "KERNELS")) { |
| 921 |
if (attr) |
| 922 |
return log_line_invalid_attr(rule_line, key); |
| 923 |
if (!is_match) |
| 924 |
return log_line_invalid_op(rule_line, key); |
| 925 |
|
| 926 |
r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL, is_case_insensitive, token_str); |
| 927 |
} else if (streq(key, "SUBSYSTEMS")) { |
| 928 |
if (attr) |
| 929 |
return log_line_invalid_attr(rule_line, key); |
| 930 |
if (!is_match) |
| 931 |
return log_line_invalid_op(rule_line, key); |
| 932 |
|
| 933 |
r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL, is_case_insensitive, token_str); |
| 934 |
} else if (streq(key, "DRIVERS")) { |
| 935 |
if (attr) |
| 936 |
return log_line_invalid_attr(rule_line, key); |
| 937 |
if (!is_match) |
| 938 |
return log_line_invalid_op(rule_line, key); |
| 939 |
|
| 940 |
r = rule_line_add_token(rule_line, TK_M_PARENTS_DRIVER, op, value, NULL, is_case_insensitive, token_str); |
| 941 |
} else if (streq(key, "ATTRS")) { |
| 942 |
r = check_attr_format_and_warn(rule_line, key, attr); |
| 943 |
if (r < 0) |
| 944 |
return r; |
| 945 |
if (!is_match) |
| 946 |
return log_line_invalid_op(rule_line, key); |
| 947 |
|
| 948 |
if (startswith(attr, "device/")) |
| 949 |
log_line_warning(rule_line, "'device' link may not be available in future kernels."); |
| 950 |
if (strstr(attr, "../")) |
| 951 |
log_line_warning(rule_line, "Direct reference to parent sysfs directory, may break in future kernels."); |
| 952 |
|
| 953 |
r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr, is_case_insensitive, token_str); |
| 954 |
} else if (streq(key, "TAGS")) { |
| 955 |
if (attr) |
| 956 |
return log_line_invalid_attr(rule_line, key); |
| 957 |
if (!is_match) |
| 958 |
return log_line_invalid_op(rule_line, key); |
| 959 |
|
| 960 |
r = rule_line_add_token(rule_line, TK_M_PARENTS_TAG, op, value, NULL, is_case_insensitive, token_str); |
| 961 |
} else if (streq(key, "TEST")) { |
| 962 |
mode_t mode = MODE_INVALID; |
| 963 |
|
| 964 |
if (!isempty(attr)) { |
| 965 |
r = parse_mode(attr, &mode); |
| 966 |
if (r < 0) |
| 967 |
return log_line_error_errno(rule_line, r, "Failed to parse mode '%s': %m", attr); |
| 968 |
} |
| 969 |
check_value_format_and_warn(rule_line, key, value, true); |
| 970 |
if (!is_match) |
| 971 |
return log_line_invalid_op(rule_line, key); |
| 972 |
if (is_case_insensitive) |
| 973 |
return log_line_invalid_prefix(rule_line, key); |
| 974 |
|
| 975 |
r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode), is_case_insensitive, token_str); |
| 976 |
} else if (streq(key, "PROGRAM")) { |
| 977 |
if (attr) |
| 978 |
return log_line_invalid_attr(rule_line, key); |
| 979 |
check_value_format_and_warn(rule_line, key, value, true); |
| 980 |
if (op == OP_REMOVE) |
| 981 |
return log_line_invalid_op(rule_line, key); |
| 982 |
if (!is_match) |
| 983 |
op = OP_MATCH; |
| 984 |
if (is_case_insensitive) |
| 985 |
return log_line_invalid_prefix(rule_line, key); |
| 986 |
|
| 987 |
r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 988 |
} else if (streq(key, "IMPORT")) { |
| 989 |
if (isempty(attr)) |
| 990 |
return log_line_invalid_attr(rule_line, key); |
| 991 |
check_value_format_and_warn(rule_line, key, value, true); |
| 992 |
if (op == OP_REMOVE) |
| 993 |
return log_line_invalid_op(rule_line, key); |
| 994 |
if (!is_match) |
| 995 |
op = OP_MATCH; |
| 996 |
if (is_case_insensitive) |
| 997 |
return log_line_invalid_prefix(rule_line, key); |
| 998 |
|
| 999 |
if (streq(attr, "file")) |
| 1000 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_FILE, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1001 |
else if (streq(attr, "program")) { |
| 1002 |
UdevBuiltinCommand cmd; |
| 1003 |
|
| 1004 |
cmd = udev_builtin_lookup(value); |
| 1005 |
if (cmd >= 0) { |
| 1006 |
log_line_debug(rule_line, "Found builtin command '%s' for %s, replacing attribute.", value, key); |
| 1007 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive= */ false, token_str); |
| 1008 |
} else |
| 1009 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1010 |
} else if (streq(attr, "builtin")) { |
| 1011 |
UdevBuiltinCommand cmd; |
| 1012 |
|
| 1013 |
cmd = udev_builtin_lookup(value); |
| 1014 |
if (cmd < 0) |
| 1015 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), |
| 1016 |
"Unknown builtin command: %s", value); |
| 1017 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive= */ false, token_str); |
| 1018 |
} else if (streq(attr, "db")) |
| 1019 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_DB, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1020 |
else if (streq(attr, "cmdline")) |
| 1021 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_CMDLINE, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1022 |
else if (streq(attr, "parent")) |
| 1023 |
r = rule_line_add_token(rule_line, TK_M_IMPORT_PARENT, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1024 |
else |
| 1025 |
return log_line_invalid_attr(rule_line, key); |
| 1026 |
} else if (streq(key, "RESULT")) { |
| 1027 |
if (attr) |
| 1028 |
return log_line_invalid_attr(rule_line, key); |
| 1029 |
if (!is_match) |
| 1030 |
return log_line_invalid_op(rule_line, key); |
| 1031 |
|
| 1032 |
r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL, is_case_insensitive, token_str); |
| 1033 |
} else if (streq(key, "OPTIONS")) { |
| 1034 |
char *tmp; |
| 1035 |
|
| 1036 |
if (attr) |
| 1037 |
return log_line_invalid_attr(rule_line, key); |
| 1038 |
if (is_match || op == OP_REMOVE) |
| 1039 |
return log_line_invalid_op(rule_line, key); |
| 1040 |
if (op == OP_ADD) |
| 1041 |
op = OP_ASSIGN; |
| 1042 |
|
| 1043 |
if (streq(value, "dump")) |
| 1044 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_DUMP, op, NULL, UINT_TO_PTR(SD_JSON_FORMAT_OFF), /* is_case_insensitive= */ false, token_str); |
| 1045 |
else if (streq(value, "dump-json")) |
| 1046 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_DUMP, op, NULL, UINT_TO_PTR(SD_JSON_FORMAT_NEWLINE), /* is_case_insensitive= */ false, token_str); |
| 1047 |
else if (streq(value, "string_escape=none")) |
| 1048 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_NONE, op, NULL, NULL, /* is_case_insensitive= */ false, token_str); |
| 1049 |
else if (streq(value, "string_escape=replace")) |
| 1050 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_REPLACE, op, NULL, NULL, /* is_case_insensitive= */ false, token_str); |
| 1051 |
else if (streq(value, "db_persist")) |
| 1052 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_DB_PERSIST, op, NULL, NULL, /* is_case_insensitive= */ false, token_str); |
| 1053 |
else if (streq(value, "watch")) |
| 1054 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(1), /* is_case_insensitive= */ false, token_str); |
| 1055 |
else if (streq(value, "nowatch")) |
| 1056 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(0), /* is_case_insensitive= */ false, token_str); |
| 1057 |
else if ((tmp = startswith(value, "static_node="))) |
| 1058 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_STATIC_NODE, op, tmp, NULL, /* is_case_insensitive= */ false, token_str); |
| 1059 |
else if ((tmp = startswith(value, "link_priority="))) { |
| 1060 |
int prio; |
| 1061 |
|
| 1062 |
r = safe_atoi(tmp, &prio); |
| 1063 |
if (r < 0) |
| 1064 |
return log_line_error_errno(rule_line, r, "Failed to parse link priority '%s': %m", tmp); |
| 1065 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_DEVLINK_PRIORITY, op, NULL, INT_TO_PTR(prio), /* is_case_insensitive= */ false, token_str); |
| 1066 |
} else if ((tmp = startswith(value, "log_level="))) { |
| 1067 |
int level; |
| 1068 |
|
| 1069 |
if (streq(tmp, "reset")) |
| 1070 |
level = -1; |
| 1071 |
else { |
| 1072 |
level = log_level_from_string(tmp); |
| 1073 |
if (level < 0) |
| 1074 |
return log_line_error_errno(rule_line, level, "Failed to parse log level '%s': %m", tmp); |
| 1075 |
} |
| 1076 |
r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level), /* is_case_insensitive= */ false, token_str); |
| 1077 |
} else { |
| 1078 |
log_line_warning(rule_line, "Invalid value for OPTIONS key, ignoring: '%s'", value); |
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
} else if (streq(key, "OWNER")) { |
| 1082 |
if (attr) |
| 1083 |
return log_line_invalid_attr(rule_line, key); |
| 1084 |
if (is_match || op == OP_REMOVE) |
| 1085 |
return log_line_invalid_op(rule_line, key); |
| 1086 |
if (op == OP_ADD) { |
| 1087 |
log_line_warning(rule_line, "%s key takes '=' or ':=' operator, assuming '='.", key); |
| 1088 |
op = OP_ASSIGN; |
| 1089 |
} |
| 1090 |
|
| 1091 |
if (in_charset(value, DIGITS) || |
| 1092 |
(resolve_name_timing == RESOLVE_NAME_EARLY && |
| 1093 |
rule_get_substitution_type(value) == SUBST_TYPE_PLAIN)) { |
| 1094 |
uid_t uid = UID_INVALID; /* avoid false maybe-uninitialized warning */ |
| 1095 |
|
| 1096 |
r = rule_resolve_user(rule_line, value, &uid); |
| 1097 |
if (r < 0) |
| 1098 |
return r; |
| 1099 |
assert(uid_is_valid(uid)); |
| 1100 |
|
| 1101 |
r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid), /* is_case_insensitive= */ false, token_str); |
| 1102 |
} else if (resolve_name_timing != RESOLVE_NAME_NEVER) { |
| 1103 |
check_value_format_and_warn(rule_line, key, value, true); |
| 1104 |
r = rule_line_add_token(rule_line, TK_A_OWNER, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1105 |
} else { |
| 1106 |
log_line_debug(rule_line, "User name resolution is disabled, ignoring %s=\"%s\".", key, value); |
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
} else if (streq(key, "GROUP")) { |
| 1110 |
if (attr) |
| 1111 |
return log_line_invalid_attr(rule_line, key); |
| 1112 |
if (is_match || op == OP_REMOVE) |
| 1113 |
return log_line_invalid_op(rule_line, key); |
| 1114 |
if (op == OP_ADD) { |
| 1115 |
log_line_warning(rule_line, "%s key takes '=' or ':=' operator, assuming '='.", key); |
| 1116 |
op = OP_ASSIGN; |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (in_charset(value, DIGITS) || |
| 1120 |
(resolve_name_timing == RESOLVE_NAME_EARLY && |
| 1121 |
rule_get_substitution_type(value) == SUBST_TYPE_PLAIN)) { |
| 1122 |
gid_t gid = GID_INVALID; /* avoid false maybe-uninitialized warning */ |
| 1123 |
|
| 1124 |
r = rule_resolve_group(rule_line, value, &gid); |
| 1125 |
if (r < 0) |
| 1126 |
return r; |
| 1127 |
assert(gid_is_valid(gid)); |
| 1128 |
|
| 1129 |
r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid), /* is_case_insensitive= */ false, token_str); |
| 1130 |
} else if (resolve_name_timing != RESOLVE_NAME_NEVER) { |
| 1131 |
check_value_format_and_warn(rule_line, key, value, true); |
| 1132 |
r = rule_line_add_token(rule_line, TK_A_GROUP, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1133 |
} else { |
| 1134 |
log_line_debug(rule_line, "Resolving group name is disabled, ignoring GROUP=\"%s\".", value); |
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
} else if (streq(key, "MODE")) { |
| 1138 |
mode_t mode; |
| 1139 |
|
| 1140 |
if (attr) |
| 1141 |
return log_line_invalid_attr(rule_line, key); |
| 1142 |
if (is_match || op == OP_REMOVE) |
| 1143 |
return log_line_invalid_op(rule_line, key); |
| 1144 |
if (op == OP_ADD) { |
| 1145 |
log_line_warning(rule_line, "%s key takes '=' or ':=' operator, assuming '='.", key); |
| 1146 |
op = OP_ASSIGN; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (parse_mode(value, &mode) >= 0) |
| 1150 |
r = rule_line_add_token(rule_line, TK_A_MODE_ID, op, NULL, MODE_TO_PTR(mode), /* is_case_insensitive= */ false, token_str); |
| 1151 |
else { |
| 1152 |
check_value_format_and_warn(rule_line, key, value, true); |
| 1153 |
r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1154 |
} |
| 1155 |
} else if (streq(key, "SECLABEL")) { |
| 1156 |
if (isempty(attr)) |
| 1157 |
return log_line_invalid_attr(rule_line, key); |
| 1158 |
check_value_format_and_warn(rule_line, key, value, true); |
| 1159 |
if (is_match || op == OP_REMOVE) |
| 1160 |
return log_line_invalid_op(rule_line, key); |
| 1161 |
if (op == OP_ASSIGN_FINAL) { |
| 1162 |
log_line_warning(rule_line, "%s key takes '=' or '+=' operator, assuming '='.", key); |
| 1163 |
op = OP_ASSIGN; |
| 1164 |
} |
| 1165 |
|
| 1166 |
r = rule_line_add_token(rule_line, TK_A_SECLABEL, op, value, attr, /* is_case_insensitive= */ false, token_str); |
| 1167 |
} else if (streq(key, "RUN")) { |
| 1168 |
if (is_match || op == OP_REMOVE) |
| 1169 |
return log_line_invalid_op(rule_line, key); |
| 1170 |
check_value_format_and_warn(rule_line, key, value, true); |
| 1171 |
if (!attr || streq(attr, "program")) |
| 1172 |
r = rule_line_add_token(rule_line, TK_A_RUN_PROGRAM, op, value, NULL, /* is_case_insensitive= */ false, token_str); |
| 1173 |
else if (streq(attr, "builtin")) { |
| 1174 |
UdevBuiltinCommand cmd; |
| 1175 |
|
| 1176 |
cmd = udev_builtin_lookup(value); |
| 1177 |
if (cmd < 0) |
| 1178 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), |
| 1179 |
"Unknown builtin command '%s', ignoring.", value); |
| 1180 |
r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd), /* is_case_insensitive= */ false, token_str); |
| 1181 |
} else |
| 1182 |
return log_line_invalid_attr(rule_line, key); |
| 1183 |
} else if (streq(key, "GOTO")) { |
| 1184 |
if (attr) |
| 1185 |
return log_line_invalid_attr(rule_line, key); |
| 1186 |
if (op != OP_ASSIGN) |
| 1187 |
return log_line_invalid_op(rule_line, key); |
| 1188 |
if (FLAGS_SET(rule_line->type, LINE_HAS_GOTO)) { |
| 1189 |
log_line_warning(rule_line, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value); |
| 1190 |
return 0; |
| 1191 |
} |
| 1192 |
|
| 1193 |
rule_line->goto_label = value; |
| 1194 |
SET_FLAG(rule_line->type, LINE_HAS_GOTO, true); |
| 1195 |
return 1; |
| 1196 |
} else if (streq(key, "LABEL")) { |
| 1197 |
if (attr) |
| 1198 |
return log_line_invalid_attr(rule_line, key); |
| 1199 |
if (op != OP_ASSIGN) |
| 1200 |
return log_line_invalid_op(rule_line, key); |
| 1201 |
if (FLAGS_SET(rule_line->type, LINE_HAS_LABEL)) |
| 1202 |
log_line_warning(rule_line, "Contains multiple LABEL keys, ignoring LABEL=\"%s\".", |
| 1203 |
rule_line->label); |
| 1204 |
|
| 1205 |
rule_line->label = value; |
| 1206 |
SET_FLAG(rule_line->type, LINE_HAS_LABEL, true); |
| 1207 |
return 1; |
| 1208 |
} else |
| 1209 |
return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), "Invalid key '%s'.", key); |
| 1210 |
if (r < 0) |
| 1211 |
return log_oom(); |
| 1212 |
|
| 1213 |
return 1; |
| 1214 |
} |
| 1215 |
|
| 1216 |
static UdevRuleOperatorType parse_operator(const char *op) { |
| 1217 |
assert(op); |
| 1218 |
|
| 1219 |
if (startswith(op, "==")) |
| 1220 |
return OP_MATCH; |
| 1221 |
if (startswith(op, "!=")) |
| 1222 |
return OP_NOMATCH; |
| 1223 |
if (startswith(op, "+=")) |
| 1224 |
return OP_ADD; |
| 1225 |
if (startswith(op, "-=")) |
| 1226 |
return OP_REMOVE; |
| 1227 |
if (startswith(op, "=")) |
| 1228 |
return OP_ASSIGN; |
| 1229 |
if (startswith(op, ":=")) |
| 1230 |
return OP_ASSIGN_FINAL; |
| 1231 |
|
| 1232 |
return _OP_TYPE_INVALID; |
| 1233 |
} |
| 1234 |
|
| 1235 |
static void check_token_delimiters(UdevRuleLine *rule_line, const char *line) { |
| 1236 |
assert(rule_line); |
| 1237 |
|
| 1238 |
size_t n_comma = 0; |
| 1239 |
bool ws_before_comma = false, ws_after_comma = false; |
| 1240 |
const char *p; |
| 1241 |
|
| 1242 |
for (p = line; !isempty(p); ++p) { |
| 1243 |
if (*p == ',') |
| 1244 |
++n_comma; |
| 1245 |
else if (strchr(WHITESPACE, *p)) { |
| 1246 |
if (n_comma > 0) |
| 1247 |
ws_after_comma = true; |
| 1248 |
else |
| 1249 |
ws_before_comma = true; |
| 1250 |
} else |
| 1251 |
break; |
| 1252 |
} |
| 1253 |
|
| 1254 |
if (line == rule_line->line) { |
| 1255 |
/* this is the first token of the rule */ |
| 1256 |
if (n_comma > 0) |
| 1257 |
log_line_notice(rule_line, "style: stray leading comma."); |
| 1258 |
} else if (isempty(p)) { |
| 1259 |
/* there are no more tokens in the rule */ |
| 1260 |
if (n_comma > 0) |
| 1261 |
log_line_notice(rule_line, "style: stray trailing comma."); |
| 1262 |
} else { |
| 1263 |
/* single comma is expected */ |
| 1264 |
if (n_comma == 0) |
| 1265 |
log_line_notice(rule_line, "style: a comma between tokens is expected."); |
| 1266 |
else if (n_comma > 1) |
| 1267 |
log_line_notice(rule_line, "style: more than one comma between tokens."); |
| 1268 |
|
| 1269 |
/* whitespace after comma is expected */ |
| 1270 |
if (n_comma > 0) { |
| 1271 |
if (ws_before_comma) |
| 1272 |
log_line_notice(rule_line, "style: stray whitespace before comma."); |
| 1273 |
if (!ws_after_comma) |
| 1274 |
log_line_notice(rule_line, "style: whitespace after comma is expected."); |
| 1275 |
} else if (!ws_before_comma && !ws_after_comma) |
| 1276 |
log_line_notice(rule_line, "style: whitespace between tokens is expected."); |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos, bool *ret_is_case_insensitive) { |
| 1281 |
char *i, *j; |
| 1282 |
bool is_escaped = false, is_case_insensitive = false; |
| 1283 |
|
| 1284 |
assert(str); |
| 1285 |
assert(ret_value); |
| 1286 |
assert(ret_endpos); |
| 1287 |
assert(ret_is_case_insensitive); |
| 1288 |
|
| 1289 |
/* check if string is prefixed with: |
| 1290 |
* - "e" for escaped |
| 1291 |
* - "i" for case insensitive match |
| 1292 |
* |
| 1293 |
* Note both e and i can be set but do not allow duplicates ("eei", "eii"). */ |
| 1294 |
for (const char *k = str; *k != '"' && k < str + 2; k++) |
| 1295 |
if (*k == 'e' && !is_escaped) |
| 1296 |
is_escaped = true; |
| 1297 |
else if (*k == 'i' && !is_case_insensitive) |
| 1298 |
is_case_insensitive = true; |
| 1299 |
else |
| 1300 |
return -EINVAL; |
| 1301 |
|
| 1302 |
/* value must be double quotated */ |
| 1303 |
str += is_escaped + is_case_insensitive; |
| 1304 |
if (str[0] != '"') |
| 1305 |
return -EINVAL; |
| 1306 |
|
| 1307 |
if (!is_escaped) { |
| 1308 |
/* unescape double quotation '\"'->'"' */ |
| 1309 |
for (j = str, i = str + 1; *i != '"'; i++, j++) { |
| 1310 |
if (*i == '\0') |
| 1311 |
return -EINVAL; |
| 1312 |
if (i[0] == '\\' && i[1] == '"') |
| 1313 |
i++; |
| 1314 |
*j = *i; |
| 1315 |
} |
| 1316 |
j[0] = '\0'; |
| 1317 |
/* |
| 1318 |
* The return value must be terminated by two subsequent NULs |
| 1319 |
* so it could be safely interpreted as nulstr. |
| 1320 |
*/ |
| 1321 |
j[1] = '\0'; |
| 1322 |
} else { |
| 1323 |
_cleanup_free_ char *unescaped = NULL; |
| 1324 |
ssize_t l; |
| 1325 |
|
| 1326 |
/* find the end position of value */ |
| 1327 |
for (i = str + 1; *i != '"'; i++) { |
| 1328 |
if (i[0] == '\\') |
| 1329 |
i++; |
| 1330 |
if (*i == '\0') |
| 1331 |
return -EINVAL; |
| 1332 |
} |
| 1333 |
i[0] = '\0'; |
| 1334 |
|
| 1335 |
l = cunescape_length(str + 1, i - (str + 1), 0, &unescaped); |
| 1336 |
if (l < 0) |
| 1337 |
return l; |
| 1338 |
|
| 1339 |
assert(l <= i - (str + 1)); |
| 1340 |
memcpy(str, unescaped, l + 1); |
| 1341 |
/* |
| 1342 |
* The return value must be terminated by two subsequent NULs |
| 1343 |
* so it could be safely interpreted as nulstr. |
| 1344 |
*/ |
| 1345 |
str[l + 1] = '\0'; |
| 1346 |
} |
| 1347 |
|
| 1348 |
*ret_value = str; |
| 1349 |
*ret_endpos = i + 1; |
| 1350 |
*ret_is_case_insensitive = is_case_insensitive; |
| 1351 |
return 0; |
| 1352 |
} |
| 1353 |
|
| 1354 |
static int parse_line(char **line, char **ret_key, char **ret_attr, UdevRuleOperatorType *ret_op, char **ret_value, bool *ret_is_case_insensitive) { |
| 1355 |
char *key_begin, *key_end, *attr, *tmp; |
| 1356 |
UdevRuleOperatorType op; |
| 1357 |
int r; |
| 1358 |
|
| 1359 |
assert(line); |
| 1360 |
assert(*line); |
| 1361 |
assert(ret_key); |
| 1362 |
assert(ret_attr); |
| 1363 |
assert(ret_op); |
| 1364 |
assert(ret_value); |
| 1365 |
assert(ret_is_case_insensitive); |
| 1366 |
|
| 1367 |
key_begin = skip_leading_chars(*line, WHITESPACE ","); |
| 1368 |
|
| 1369 |
if (isempty(key_begin)) |
| 1370 |
return 0; |
| 1371 |
|
| 1372 |
for (key_end = key_begin; ; key_end++) { |
| 1373 |
if (key_end[0] == '\0') |
| 1374 |
return -EINVAL; |
| 1375 |
if (strchr(WHITESPACE "={", key_end[0])) |
| 1376 |
break; |
| 1377 |
if (strchr("+-!:", key_end[0]) && key_end[1] == '=') |
| 1378 |
break; |
| 1379 |
} |
| 1380 |
if (key_end[0] == '{') { |
| 1381 |
attr = key_end + 1; |
| 1382 |
tmp = strchr(attr, '}'); |
| 1383 |
if (!tmp) |
| 1384 |
return -EINVAL; |
| 1385 |
*tmp++ = '\0'; |
| 1386 |
} else { |
| 1387 |
attr = NULL; |
| 1388 |
tmp = key_end; |
| 1389 |
} |
| 1390 |
|
| 1391 |
tmp = skip_leading_chars(tmp, NULL); |
| 1392 |
op = parse_operator(tmp); |
| 1393 |
if (op < 0) |
| 1394 |
return -EINVAL; |
| 1395 |
|
| 1396 |
key_end[0] = '\0'; |
| 1397 |
|
| 1398 |
tmp += op == OP_ASSIGN ? 1 : 2; |
| 1399 |
tmp = skip_leading_chars(tmp, NULL); |
| 1400 |
r = udev_rule_parse_value(tmp, ret_value, line, ret_is_case_insensitive); |
| 1401 |
if (r < 0) |
| 1402 |
return r; |
| 1403 |
|
| 1404 |
*ret_key = key_begin; |
| 1405 |
*ret_attr = attr; |
| 1406 |
*ret_op = op; |
| 1407 |
return 1; |
| 1408 |
} |
| 1409 |
|
| 1410 |
static void check_tokens_order(UdevRuleLine *rule_line) { |
| 1411 |
bool has_result = false; |
| 1412 |
|
| 1413 |
assert(rule_line); |
| 1414 |
|
| 1415 |
LIST_FOREACH(tokens, t, rule_line->tokens) |
| 1416 |
if (t->type == TK_M_RESULT) |
| 1417 |
has_result = true; |
| 1418 |
else if (has_result && t->type == TK_M_PROGRAM) { |
| 1419 |
log_line_warning(rule_line, "Reordering RESULT check after PROGRAM assignment."); |
| 1420 |
break; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
static void sort_tokens(UdevRuleLine *rule_line) { |
| 1425 |
assert(rule_line); |
| 1426 |
|
| 1427 |
UdevRuleToken *old_tokens = TAKE_PTR(rule_line->tokens); |
| 1428 |
|
| 1429 |
while (old_tokens) { |
| 1430 |
UdevRuleToken *min_token = NULL; |
| 1431 |
|
| 1432 |
LIST_FOREACH(tokens, t, old_tokens) |
| 1433 |
if (!min_token || min_token->type > t->type) |
| 1434 |
min_token = t; |
| 1435 |
|
| 1436 |
LIST_REMOVE(tokens, old_tokens, min_token); |
| 1437 |
LIST_APPEND(tokens, rule_line->tokens, min_token); |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
static int rule_add_line(UdevRuleFile *rule_file, const char *line, unsigned line_nr, bool extra_checks) { |
| 1442 |
_cleanup_(udev_rule_line_freep) UdevRuleLine *rule_line = NULL; |
| 1443 |
char *p; |
| 1444 |
int r; |
| 1445 |
|
| 1446 |
assert(rule_file); |
| 1447 |
assert(line); |
| 1448 |
|
| 1449 |
if (isempty(line)) |
| 1450 |
return 0; |
| 1451 |
|
| 1452 |
rule_line = new(UdevRuleLine, 1); |
| 1453 |
if (!rule_line) |
| 1454 |
return log_oom(); |
| 1455 |
|
| 1456 |
*rule_line = (UdevRuleLine) { |
| 1457 |
.line = strdup(line), |
| 1458 |
.line_for_logging = strdup(line), |
| 1459 |
.line_number = line_nr, |
| 1460 |
}; |
| 1461 |
if (!rule_line->line || !rule_line->line_for_logging) |
| 1462 |
return log_oom(); |
| 1463 |
|
| 1464 |
rule_line->rule_file = rule_file; |
| 1465 |
LIST_APPEND(rule_lines, rule_file->rule_lines, rule_line); |
| 1466 |
|
| 1467 |
for (p = rule_line->line; !isempty(p); ) { |
| 1468 |
char *key, *attr, *value; |
| 1469 |
UdevRuleOperatorType op; |
| 1470 |
bool is_case_insensitive; |
| 1471 |
|
| 1472 |
if (extra_checks) |
| 1473 |
check_token_delimiters(rule_line, p); |
| 1474 |
|
| 1475 |
r = parse_line(&p, &key, &attr, &op, &value, &is_case_insensitive); |
| 1476 |
if (r < 0) |
| 1477 |
return log_line_error_errno(rule_line, r, "Invalid key/value pair, ignoring."); |
| 1478 |
if (r == 0) |
| 1479 |
break; |
| 1480 |
|
| 1481 |
char *token_str = rule_line->line_for_logging + (key - rule_line->line); |
| 1482 |
token_str[p - key] = '\0'; |
| 1483 |
r = parse_token(rule_line, key, attr, op, value, is_case_insensitive, token_str); |
| 1484 |
if (r < 0) |
| 1485 |
return r; |
| 1486 |
} |
| 1487 |
|
| 1488 |
if (rule_line->type == 0) { |
| 1489 |
log_line_warning(rule_line, "The line has no effect, ignoring."); |
| 1490 |
return 0; |
| 1491 |
} |
| 1492 |
|
| 1493 |
if (extra_checks) |
| 1494 |
check_tokens_order(rule_line); |
| 1495 |
|
| 1496 |
sort_tokens(rule_line); |
| 1497 |
TAKE_PTR(rule_line); |
| 1498 |
return 0; |
| 1499 |
} |
| 1500 |
|
| 1501 |
static void rule_resolve_goto(UdevRuleFile *rule_file) { |
| 1502 |
assert(rule_file); |
| 1503 |
|
| 1504 |
/* link GOTOs to LABEL rules in this file to be able to fast-forward */ |
| 1505 |
LIST_FOREACH(rule_lines, line, rule_file->rule_lines) { |
| 1506 |
if (!FLAGS_SET(line->type, LINE_HAS_GOTO)) |
| 1507 |
continue; |
| 1508 |
|
| 1509 |
LIST_FOREACH(rule_lines, i, line->rule_lines_next) |
| 1510 |
if (streq_ptr(i->label, line->goto_label)) { |
| 1511 |
line->goto_line = i; |
| 1512 |
SET_FLAG(i->type, LINE_IS_REFERENCED, true); |
| 1513 |
break; |
| 1514 |
} |
| 1515 |
|
| 1516 |
if (!line->goto_line) { |
| 1517 |
log_line_error(line, "GOTO=\"%s\" has no matching label, ignoring.", |
| 1518 |
line->goto_label); |
| 1519 |
|
| 1520 |
SET_FLAG(line->type, LINE_HAS_GOTO, false); |
| 1521 |
line->goto_label = NULL; |
| 1522 |
|
| 1523 |
if ((line->type & ~(LINE_HAS_LABEL|LINE_IS_REFERENCED)) == 0) { |
| 1524 |
log_line_warning(line, "The line has no effect any more, dropping."); |
| 1525 |
/* LINE_IS_REFERENCED implies LINE_HAS_LABEL */ |
| 1526 |
if (line->type & LINE_HAS_LABEL) |
| 1527 |
udev_rule_line_clear_tokens(line); |
| 1528 |
else |
| 1529 |
udev_rule_line_free(line); |
| 1530 |
} |
| 1531 |
} |
| 1532 |
} |
| 1533 |
} |
| 1534 |
|
| 1535 |
static bool token_data_is_string(UdevRuleTokenType type) { |
| 1536 |
return IN_SET(type, TK_M_ENV, |
| 1537 |
TK_M_CONST, |
| 1538 |
TK_M_ATTR, |
| 1539 |
TK_M_SYSCTL, |
| 1540 |
TK_M_PARENTS_ATTR, |
| 1541 |
TK_A_SECLABEL, |
| 1542 |
TK_A_ENV, |
| 1543 |
TK_A_ATTR, |
| 1544 |
TK_A_SYSCTL); |
| 1545 |
} |
| 1546 |
|
| 1547 |
static bool token_type_and_data_eq(const UdevRuleToken *a, const UdevRuleToken *b) { |
| 1548 |
assert(a); |
| 1549 |
assert(b); |
| 1550 |
|
| 1551 |
return a->type == b->type && |
| 1552 |
(token_data_is_string(a->type) ? streq_ptr(a->data, b->data) : (a->data == b->data)); |
| 1553 |
} |
| 1554 |
|
| 1555 |
static bool nulstr_eq(const char *a, const char *b) { |
| 1556 |
NULSTR_FOREACH(i, a) |
| 1557 |
if (!nulstr_contains(b, i)) |
| 1558 |
return false; |
| 1559 |
|
| 1560 |
NULSTR_FOREACH(i, b) |
| 1561 |
if (!nulstr_contains(a, i)) |
| 1562 |
return false; |
| 1563 |
|
| 1564 |
return true; |
| 1565 |
} |
| 1566 |
|
| 1567 |
static bool token_type_and_value_eq(const UdevRuleToken *a, const UdevRuleToken *b) { |
| 1568 |
assert(a); |
| 1569 |
assert(b); |
| 1570 |
|
| 1571 |
if (a->type != b->type || |
| 1572 |
a->match_type != b->match_type) |
| 1573 |
return false; |
| 1574 |
|
| 1575 |
/* token value is ignored for certain match types */ |
| 1576 |
if (IN_SET(a->match_type, MATCH_TYPE_EMPTY, MATCH_TYPE_SUBSYSTEM)) |
| 1577 |
return true; |
| 1578 |
|
| 1579 |
return type_has_nulstr_value(a->type) ? nulstr_eq(a->value, b->value) : |
| 1580 |
streq_ptr(a->value, b->value); |
| 1581 |
} |
| 1582 |
|
| 1583 |
static bool conflicting_op(UdevRuleOperatorType a, UdevRuleOperatorType b) { |
| 1584 |
return (a == OP_MATCH && b == OP_NOMATCH) || |
| 1585 |
(a == OP_NOMATCH && b == OP_MATCH); |
| 1586 |
} |
| 1587 |
|
| 1588 |
/* test whether all fields besides UdevRuleOperatorType of two tokens match */ |
| 1589 |
static bool tokens_eq(const UdevRuleToken *a, const UdevRuleToken *b) { |
| 1590 |
assert(a); |
| 1591 |
assert(b); |
| 1592 |
|
| 1593 |
return a->attr_subst_type == b->attr_subst_type && |
| 1594 |
token_type_and_value_eq(a, b) && |
| 1595 |
token_type_and_data_eq(a, b); |
| 1596 |
} |
| 1597 |
|
| 1598 |
static bool nulstr_tokens_conflict(const UdevRuleToken *a, const UdevRuleToken *b) { |
| 1599 |
assert(a); |
| 1600 |
assert(b); |
| 1601 |
|
| 1602 |
if (!(a->type == b->type && |
| 1603 |
type_has_nulstr_value(a->type) && |
| 1604 |
a->op == b->op && |
| 1605 |
a->op == OP_MATCH && |
| 1606 |
a->match_type == b->match_type && |
| 1607 |
a->attr_subst_type == b->attr_subst_type && |
| 1608 |
token_type_and_data_eq(a, b))) |
| 1609 |
return false; |
| 1610 |
|
| 1611 |
if (a->match_type == MATCH_TYPE_PLAIN) { |
| 1612 |
NULSTR_FOREACH(i, a->value) |
| 1613 |
if (nulstr_contains(b->value, i)) |
| 1614 |
return false; |
| 1615 |
return true; |
| 1616 |
} |
| 1617 |
|
| 1618 |
if (a->match_type == MATCH_TYPE_GLOB) { |
| 1619 |
NULSTR_FOREACH(i, a->value) { |
| 1620 |
size_t i_n = strcspn(i, GLOB_CHARS); |
| 1621 |
if (i_n == 0) |
| 1622 |
return false; |
| 1623 |
NULSTR_FOREACH(j, b->value) { |
| 1624 |
size_t j_n = strcspn(j, GLOB_CHARS); |
| 1625 |
if (j_n == 0 || strneq(i, j, MIN(i_n, j_n))) |
| 1626 |
return false; |
| 1627 |
} |
| 1628 |
|
| 1629 |
} |
| 1630 |
return true; |
| 1631 |
} |
| 1632 |
|
| 1633 |
return false; |
| 1634 |
} |
| 1635 |
|
| 1636 |
static void udev_check_unused_labels(UdevRuleLine *line) { |
| 1637 |
assert(line); |
| 1638 |
|
| 1639 |
if (FLAGS_SET(line->type, LINE_HAS_LABEL) && |
| 1640 |
!FLAGS_SET(line->type, LINE_IS_REFERENCED)) |
| 1641 |
log_line_notice(line, "style: LABEL=\"%s\" is unused.", line->label); |
| 1642 |
} |
| 1643 |
|
| 1644 |
static void udev_check_conflicts_duplicates(UdevRuleLine *line) { |
| 1645 |
assert(line); |
| 1646 |
|
| 1647 |
bool conflicts = false, duplicates = false; |
| 1648 |
|
| 1649 |
LIST_FOREACH(tokens, token, line->tokens) |
| 1650 |
LIST_FOREACH(tokens, i, token->tokens_next) { |
| 1651 |
bool new_conflicts = false, new_duplicates = false; |
| 1652 |
|
| 1653 |
if (tokens_eq(token, i)) { |
| 1654 |
if (!duplicates && token->op == i->op) |
| 1655 |
new_duplicates = true; |
| 1656 |
if (!conflicts && conflicting_op(token->op, i->op)) |
| 1657 |
new_conflicts = true; |
| 1658 |
} else if (!conflicts && nulstr_tokens_conflict(token, i)) |
| 1659 |
new_conflicts = true; |
| 1660 |
else |
| 1661 |
continue; |
| 1662 |
|
| 1663 |
if (new_duplicates) { |
| 1664 |
duplicates = new_duplicates; |
| 1665 |
log_line_warning(line, "duplicate expressions."); |
| 1666 |
} |
| 1667 |
if (new_conflicts) { |
| 1668 |
conflicts = new_conflicts; |
| 1669 |
log_line_error(line, "conflicting match expressions, the line has no effect."); |
| 1670 |
} |
| 1671 |
if (conflicts && duplicates) |
| 1672 |
return; |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
static void udev_check_rule_line(UdevRuleLine *line) { |
| 1677 |
udev_check_unused_labels(line); |
| 1678 |
udev_check_conflicts_duplicates(line); |
| 1679 |
} |
| 1680 |
|
| 1681 |
int udev_rules_parse_file(UdevRules *rules, const ConfFile *c, bool extra_checks, UdevRuleFile **ret) { |
| 1682 |
_cleanup_(udev_rule_file_freep) UdevRuleFile *rule_file = NULL; |
| 1683 |
_cleanup_free_ char *name = NULL; |
| 1684 |
_cleanup_fclose_ FILE *f = NULL; |
| 1685 |
int r; |
| 1686 |
|
| 1687 |
assert(rules); |
| 1688 |
assert(c); |
| 1689 |
assert(c->fd >= 0); |
| 1690 |
assert(c->original_path); |
| 1691 |
|
| 1692 |
f = fopen(FORMAT_PROC_FD_PATH(c->fd), "re"); |
| 1693 |
if (!f) { |
| 1694 |
if (extra_checks) |
| 1695 |
return -errno; |
| 1696 |
|
| 1697 |
return log_warning_errno(errno, "Failed to open %s, ignoring: %m", c->original_path); |
| 1698 |
} |
| 1699 |
|
| 1700 |
r = hashmap_put_stats_by_path(&rules->stats_by_path, c->original_path, &c->st); |
| 1701 |
if (r < 0) |
| 1702 |
return log_warning_errno(r, "Failed to save stat for %s, ignoring: %m", c->original_path); |
| 1703 |
|
| 1704 |
(void) stat_warn_permissions(c->original_path, &c->st); |
| 1705 |
|
| 1706 |
log_debug("Reading rules file: %s", c->original_path); |
| 1707 |
|
| 1708 |
name = strdup(c->original_path); |
| 1709 |
if (!name) |
| 1710 |
return log_oom(); |
| 1711 |
|
| 1712 |
rule_file = new(UdevRuleFile, 1); |
| 1713 |
if (!rule_file) |
| 1714 |
return log_oom(); |
| 1715 |
|
| 1716 |
*rule_file = (UdevRuleFile) { |
| 1717 |
.filename = TAKE_PTR(name), |
| 1718 |
.rules = rules, |
| 1719 |
}; |
| 1720 |
|
| 1721 |
LIST_APPEND(rule_files, rules->rule_files, rule_file); |
| 1722 |
|
| 1723 |
_cleanup_free_ char *continuation = NULL; |
| 1724 |
unsigned line_nr = 0, current_line_nr = 0; |
| 1725 |
bool ignore_line = false; |
| 1726 |
for (;;) { |
| 1727 |
_cleanup_free_ char *buf = NULL; |
| 1728 |
size_t len; |
| 1729 |
char *line; |
| 1730 |
|
| 1731 |
r = read_line(f, UDEV_LINE_SIZE, &buf); |
| 1732 |
if (r < 0) |
| 1733 |
return r; |
| 1734 |
if (r == 0) |
| 1735 |
break; |
| 1736 |
|
| 1737 |
current_line_nr++; |
| 1738 |
if (!continuation) |
| 1739 |
line_nr = current_line_nr; |
| 1740 |
|
| 1741 |
line = skip_leading_chars(buf, NULL); |
| 1742 |
|
| 1743 |
/* Lines beginning with '#' are ignored regardless of line continuation. */ |
| 1744 |
if (line[0] == '#') |
| 1745 |
continue; |
| 1746 |
|
| 1747 |
len = strlen(line); |
| 1748 |
|
| 1749 |
if (continuation && !ignore_line) { |
| 1750 |
if (strlen(continuation) + len >= UDEV_LINE_SIZE) |
| 1751 |
ignore_line = true; |
| 1752 |
|
| 1753 |
if (!strextend(&continuation, line)) |
| 1754 |
return log_oom(); |
| 1755 |
|
| 1756 |
if (!ignore_line) { |
| 1757 |
line = continuation; |
| 1758 |
len = strlen(line); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
if (len > 0 && line[len - 1] == '\\') { |
| 1763 |
if (ignore_line) |
| 1764 |
continue; |
| 1765 |
|
| 1766 |
line[len - 1] = '\0'; |
| 1767 |
if (!continuation) { |
| 1768 |
continuation = strdup(line); |
| 1769 |
if (!continuation) |
| 1770 |
return log_oom(); |
| 1771 |
} |
| 1772 |
|
| 1773 |
continue; |
| 1774 |
} |
| 1775 |
|
| 1776 |
if (ignore_line) |
| 1777 |
log_file_error(rule_file, line_nr, "Line is too long, ignored."); |
| 1778 |
else if (len > 0) |
| 1779 |
(void) rule_add_line(rule_file, line, line_nr, extra_checks); |
| 1780 |
|
| 1781 |
continuation = mfree(continuation); |
| 1782 |
ignore_line = false; |
| 1783 |
} |
| 1784 |
|
| 1785 |
if (continuation) |
| 1786 |
log_file_error(rule_file, line_nr, |
| 1787 |
"Unexpected EOF after line continuation, line ignored."); |
| 1788 |
|
| 1789 |
rule_resolve_goto(rule_file); |
| 1790 |
|
| 1791 |
if (extra_checks) |
| 1792 |
LIST_FOREACH(rule_lines, line, rule_file->rule_lines) |
| 1793 |
udev_check_rule_line(line); |
| 1794 |
|
| 1795 |
if (ret) |
| 1796 |
*ret = rule_file; |
| 1797 |
|
| 1798 |
TAKE_PTR(rule_file); |
| 1799 |
return 0; |
| 1800 |
} |
| 1801 |
|
| 1802 |
unsigned udev_rule_file_get_issues(UdevRuleFile *rule_file) { |
| 1803 |
assert(rule_file); |
| 1804 |
|
| 1805 |
return rule_file->issues; |
| 1806 |
} |
| 1807 |
|
| 1808 |
UdevRules* udev_rules_new(ResolveNameTiming resolve_name_timing) { |
| 1809 |
assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX); |
| 1810 |
|
| 1811 |
UdevRules *rules = new(UdevRules, 1); |
| 1812 |
if (!rules) |
| 1813 |
return NULL; |
| 1814 |
|
| 1815 |
*rules = (UdevRules) { |
| 1816 |
.resolve_name_timing = resolve_name_timing, |
| 1817 |
}; |
| 1818 |
|
| 1819 |
return rules; |
| 1820 |
} |
| 1821 |
|
| 1822 |
int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing, char * const *extra) { |
| 1823 |
_cleanup_(udev_rules_freep) UdevRules *rules = NULL; |
| 1824 |
_cleanup_strv_free_ char **directories = NULL; |
| 1825 |
int r; |
| 1826 |
|
| 1827 |
rules = udev_rules_new(resolve_name_timing); |
| 1828 |
if (!rules) |
| 1829 |
return -ENOMEM; |
| 1830 |
|
| 1831 |
if (!strv_isempty(extra)) { |
| 1832 |
directories = strv_copy(extra); |
| 1833 |
if (!directories) |
| 1834 |
return -ENOMEM; |
| 1835 |
} |
| 1836 |
|
| 1837 |
r = strv_extend_strv(&directories, CONF_PATHS_STRV("udev/rules.d"), /* filter_duplicates= */ false); |
| 1838 |
if (r < 0) |
| 1839 |
return r; |
| 1840 |
|
| 1841 |
ConfFile **files = NULL; |
| 1842 |
size_t n_files = 0; |
| 1843 |
|
| 1844 |
CLEANUP_ARRAY(files, n_files, conf_file_free_array); |
| 1845 |
|
| 1846 |
r = conf_files_list_strv_full(".rules", /* root= */ NULL, CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED, |
| 1847 |
(const char* const*) directories, &files, &n_files); |
| 1848 |
if (r < 0) |
| 1849 |
return log_debug_errno(r, "Failed to enumerate rules files: %m"); |
| 1850 |
|
| 1851 |
FOREACH_ARRAY(i, files, n_files) { |
| 1852 |
ConfFile *c = *i; |
| 1853 |
|
| 1854 |
r = udev_rules_parse_file(rules, c, /* extra_checks= */ false, /* ret= */ NULL); |
| 1855 |
if (r < 0) |
| 1856 |
log_debug_errno(r, "Failed to read rules file '%s', ignoring: %m", c->original_path); |
| 1857 |
} |
| 1858 |
|
| 1859 |
*ret_rules = TAKE_PTR(rules); |
| 1860 |
return 0; |
| 1861 |
} |
| 1862 |
|
| 1863 |
bool udev_rules_should_reload(UdevRules *rules) { |
| 1864 |
_cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; |
| 1865 |
int r; |
| 1866 |
|
| 1867 |
if (!rules) |
| 1868 |
return true; |
| 1869 |
|
| 1870 |
r = config_get_stats_by_path(".rules", NULL, 0, RULES_DIRS, /* check_dropins= */ false, &stats_by_path); |
| 1871 |
if (r < 0) { |
| 1872 |
log_warning_errno(r, "Failed to get stats of udev rules, ignoring: %m"); |
| 1873 |
return true; |
| 1874 |
} |
| 1875 |
|
| 1876 |
if (!stats_by_path_equal(rules->stats_by_path, stats_by_path)) { |
| 1877 |
log_debug("Udev rules need reloading"); |
| 1878 |
return true; |
| 1879 |
} |
| 1880 |
|
| 1881 |
return false; |
| 1882 |
} |
| 1883 |
|
| 1884 |
static bool apply_format_full( |
| 1885 |
UdevEvent *event, |
| 1886 |
UdevRuleToken *token, |
| 1887 |
const char *format, |
| 1888 |
char *result, |
| 1889 |
size_t result_size, |
| 1890 |
bool replace_whitespace, |
| 1891 |
const char *what) { |
| 1892 |
|
| 1893 |
assert(event); |
| 1894 |
assert(token); |
| 1895 |
assert(format); |
| 1896 |
assert(result); |
| 1897 |
assert(what); |
| 1898 |
|
| 1899 |
bool truncated = false; |
| 1900 |
(void) udev_event_apply_format(event, format, result, result_size, replace_whitespace, &truncated); |
| 1901 |
if (truncated) { |
| 1902 |
log_event_truncated(event, token, what, format); |
| 1903 |
return false; |
| 1904 |
} |
| 1905 |
|
| 1906 |
if (event->trace && !streq(format, result)) |
| 1907 |
log_event_trace(event, token, "Format substitution: \"%s\" -> \"%s\"", format, result); |
| 1908 |
|
| 1909 |
return true; |
| 1910 |
} |
| 1911 |
|
| 1912 |
static bool apply_format_value( |
| 1913 |
UdevEvent *event, |
| 1914 |
UdevRuleToken *token, |
| 1915 |
char *result, |
| 1916 |
size_t result_size, |
| 1917 |
const char *what) { |
| 1918 |
|
| 1919 |
return apply_format_full(event, token, token->value, result, result_size, /* replace_whitespace= */ false, what); |
| 1920 |
} |
| 1921 |
|
| 1922 |
static bool apply_format_attr( |
| 1923 |
UdevEvent *event, |
| 1924 |
UdevRuleToken *token, |
| 1925 |
char *result, |
| 1926 |
size_t result_size, |
| 1927 |
const char *what) { |
| 1928 |
|
| 1929 |
return apply_format_full(event, token, token->data, result, result_size, /* replace_whitespace= */ false, what); |
| 1930 |
} |
| 1931 |
|
| 1932 |
static bool token_match_string(UdevEvent *event, UdevRuleToken *token, const char *str, bool log_result) { |
| 1933 |
const char *value; |
| 1934 |
bool match = false, case_insensitive; |
| 1935 |
|
| 1936 |
assert(event); |
| 1937 |
assert(token); |
| 1938 |
assert(token->value); |
| 1939 |
assert(token->type < _TK_M_MAX); |
| 1940 |
|
| 1941 |
str = strempty(str); |
| 1942 |
value = token->value; |
| 1943 |
case_insensitive = FLAGS_SET(token->match_type, MATCH_CASE_INSENSITIVE); |
| 1944 |
|
| 1945 |
switch (token->match_type & _MATCH_TYPE_MASK) { |
| 1946 |
case MATCH_TYPE_EMPTY: |
| 1947 |
match = isempty(str); |
| 1948 |
break; |
| 1949 |
case MATCH_TYPE_SUBSYSTEM: |
| 1950 |
if (case_insensitive) |
| 1951 |
match = STRCASE_IN_SET(str, "subsystem", "class", "bus"); |
| 1952 |
else |
| 1953 |
match = STR_IN_SET(str, "subsystem", "class", "bus"); |
| 1954 |
break; |
| 1955 |
case MATCH_TYPE_PLAIN_WITH_EMPTY: |
| 1956 |
if (isempty(str)) { |
| 1957 |
match = true; |
| 1958 |
break; |
| 1959 |
} |
| 1960 |
_fallthrough_; |
| 1961 |
case MATCH_TYPE_PLAIN: |
| 1962 |
NULSTR_FOREACH(i, value) |
| 1963 |
if (case_insensitive ? strcaseeq(i, str) : streq(i, str)) { |
| 1964 |
match = true; |
| 1965 |
break; |
| 1966 |
} |
| 1967 |
break; |
| 1968 |
case MATCH_TYPE_GLOB_WITH_EMPTY: |
| 1969 |
if (isempty(str)) { |
| 1970 |
match = true; |
| 1971 |
break; |
| 1972 |
} |
| 1973 |
_fallthrough_; |
| 1974 |
case MATCH_TYPE_GLOB: |
| 1975 |
NULSTR_FOREACH(i, value) |
| 1976 |
if ((fnmatch(i, str, case_insensitive ? FNM_CASEFOLD : 0) == 0)) { |
| 1977 |
match = true; |
| 1978 |
break; |
| 1979 |
} |
| 1980 |
break; |
| 1981 |
default: |
| 1982 |
assert_not_reached(); |
| 1983 |
} |
| 1984 |
|
| 1985 |
bool result = token->op == (match ? OP_MATCH : OP_NOMATCH); |
| 1986 |
|
| 1987 |
if (event->trace) |
| 1988 |
switch (token->match_type & _MATCH_TYPE_MASK) { |
| 1989 |
case MATCH_TYPE_EMPTY: |
| 1990 |
log_event_trace(event, token, "String \"%s\" is%s empty%s", |
| 1991 |
strempty(str), match ? "" : " not", |
| 1992 |
log_result ? result ? ": PASS" : ": FAIL" : "."); |
| 1993 |
break; |
| 1994 |
case MATCH_TYPE_SUBSYSTEM: |
| 1995 |
log_event_trace(event, token, |
| 1996 |
"String \"%s\" matches %s of \"subsystem\", \"class\", or \"bus\"%s", |
| 1997 |
strempty(str), match ? "one" : "neither", |
| 1998 |
log_result ? result ? ": PASS" : ": FAIL" : "."); |
| 1999 |
break; |
| 2000 |
case MATCH_TYPE_PLAIN_WITH_EMPTY: |
| 2001 |
case MATCH_TYPE_PLAIN: |
| 2002 |
case MATCH_TYPE_GLOB_WITH_EMPTY: |
| 2003 |
case MATCH_TYPE_GLOB: { |
| 2004 |
_cleanup_free_ char *joined = NULL; |
| 2005 |
unsigned c = 0; |
| 2006 |
|
| 2007 |
if (IN_SET(token->match_type & _MATCH_TYPE_MASK, MATCH_TYPE_PLAIN_WITH_EMPTY, MATCH_TYPE_GLOB_WITH_EMPTY)) { |
| 2008 |
(void) strextend_with_separator(&joined, ", ", "\"\""); |
| 2009 |
c++; |
| 2010 |
} |
| 2011 |
|
| 2012 |
NULSTR_FOREACH(i, value) { |
| 2013 |
(void) strextendf_with_separator(&joined, ", ", "\"%s\"", i); |
| 2014 |
c++; |
| 2015 |
} |
| 2016 |
|
| 2017 |
assert(c > 0); |
| 2018 |
log_event_trace(event, token, "String \"%s\" %s %s%s", |
| 2019 |
strempty(str), |
| 2020 |
match ? (c > 1 ? "matches one of" : "matches") : (c > 1 ? "matches neither of" : "does not match"), |
| 2021 |
strempty(joined), |
| 2022 |
log_result ? result ? ": PASS" : ": FAIL" : "."); |
| 2023 |
break; |
| 2024 |
} |
| 2025 |
default: |
| 2026 |
assert_not_reached(); |
| 2027 |
} |
| 2028 |
|
| 2029 |
return result; |
| 2030 |
} |
| 2031 |
|
| 2032 |
static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *event) { |
| 2033 |
char nbuf[UDEV_NAME_SIZE], vbuf[UDEV_NAME_SIZE]; |
| 2034 |
const char *name, *value; |
| 2035 |
int r; |
| 2036 |
|
| 2037 |
assert(token); |
| 2038 |
assert(IN_SET(token->type, TK_M_ATTR, TK_M_PARENTS_ATTR)); |
| 2039 |
assert(dev); |
| 2040 |
assert(event); |
| 2041 |
|
| 2042 |
name = token->data; |
| 2043 |
|
| 2044 |
switch (token->attr_subst_type) { |
| 2045 |
case SUBST_TYPE_FORMAT: |
| 2046 |
if (!apply_format_attr(event, token, nbuf, sizeof(nbuf), "sysfs attribute name")) |
| 2047 |
return false; |
| 2048 |
|
| 2049 |
name = nbuf; |
| 2050 |
_fallthrough_; |
| 2051 |
case SUBST_TYPE_PLAIN: |
| 2052 |
r = sd_device_get_sysattr_value(dev, name, &value); |
| 2053 |
if (r < 0) { |
| 2054 |
log_event_trace_errno(event, token, r, "Cannot read sysfs attribute%s%s%s: %m", |
| 2055 |
name != token->data ? " \"" : "", |
| 2056 |
name != token->data ? name : "", |
| 2057 |
name != token->data ? "\"" : ""); |
| 2058 |
return false; |
| 2059 |
} |
| 2060 |
|
| 2061 |
/* remove trailing whitespace, if not asked to match for it */ |
| 2062 |
if (FLAGS_SET(token->match_type, MATCH_REMOVE_TRAILING_WHITESPACE)) { |
| 2063 |
strscpy(vbuf, sizeof(vbuf), value); |
| 2064 |
value = delete_trailing_chars(vbuf, NULL); |
| 2065 |
} |
| 2066 |
|
| 2067 |
return token_match_string(event, token, value, /* log_result= */ true); |
| 2068 |
|
| 2069 |
case SUBST_TYPE_SUBSYS: |
| 2070 |
r = udev_resolve_subsys_kernel(name, vbuf, sizeof(vbuf), true); |
| 2071 |
if (r < 0) { |
| 2072 |
log_event_trace_errno(event, token, r, "Cannot read sysfs attribute: %m"); |
| 2073 |
return false; |
| 2074 |
} |
| 2075 |
|
| 2076 |
/* remove trailing whitespace, if not asked to match for it */ |
| 2077 |
if (FLAGS_SET(token->match_type, MATCH_REMOVE_TRAILING_WHITESPACE)) |
| 2078 |
delete_trailing_chars(vbuf, NULL); |
| 2079 |
|
| 2080 |
return token_match_string(event, token, vbuf, /* log_result= */ true); |
| 2081 |
|
| 2082 |
default: |
| 2083 |
assert_not_reached(); |
| 2084 |
} |
| 2085 |
} |
| 2086 |
|
| 2087 |
static int get_property_from_string(char *line, char **ret_key, char **ret_value) { |
| 2088 |
char *key, *val; |
| 2089 |
size_t len; |
| 2090 |
|
| 2091 |
assert(line); |
| 2092 |
assert(ret_key); |
| 2093 |
assert(ret_value); |
| 2094 |
|
| 2095 |
/* find key */ |
| 2096 |
key = skip_leading_chars(line, NULL); |
| 2097 |
|
| 2098 |
/* comment or empty line */ |
| 2099 |
if (IN_SET(key[0], '#', '\0')) { |
| 2100 |
*ret_key = *ret_value = NULL; |
| 2101 |
return 0; |
| 2102 |
} |
| 2103 |
|
| 2104 |
/* split key/value */ |
| 2105 |
val = strchr(key, '='); |
| 2106 |
if (!val) |
| 2107 |
return -EINVAL; |
| 2108 |
*val++ = '\0'; |
| 2109 |
|
| 2110 |
key = strstrip(key); |
| 2111 |
if (isempty(key)) |
| 2112 |
return -EINVAL; |
| 2113 |
|
| 2114 |
val = strstrip(val); |
| 2115 |
if (isempty(val)) |
| 2116 |
return -EINVAL; |
| 2117 |
|
| 2118 |
/* unquote */ |
| 2119 |
if (IN_SET(val[0], '"', '\'')) { |
| 2120 |
len = strlen(val); |
| 2121 |
if (len == 1 || val[len-1] != val[0]) |
| 2122 |
return -EINVAL; |
| 2123 |
val[len-1] = '\0'; |
| 2124 |
val++; |
| 2125 |
} |
| 2126 |
|
| 2127 |
*ret_key = key; |
| 2128 |
*ret_value = val; |
| 2129 |
return 1; |
| 2130 |
} |
| 2131 |
|
| 2132 |
static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) { |
| 2133 |
_cleanup_closedir_ DIR *dir = NULL; |
| 2134 |
char buf[UDEV_PATH_SIZE], *p; |
| 2135 |
const char *tail; |
| 2136 |
size_t len, size; |
| 2137 |
bool truncated; |
| 2138 |
|
| 2139 |
assert(attr); |
| 2140 |
|
| 2141 |
tail = strstr(attr, "/*/"); |
| 2142 |
if (!tail) |
| 2143 |
return 0; |
| 2144 |
|
| 2145 |
len = tail - attr + 1; /* include slash at the end */ |
| 2146 |
tail += 2; /* include slash at the beginning */ |
| 2147 |
|
| 2148 |
p = buf; |
| 2149 |
size = sizeof(buf); |
| 2150 |
size -= strnpcpy_full(&p, size, attr, len, &truncated); |
| 2151 |
if (truncated) |
| 2152 |
return -ENOENT; |
| 2153 |
|
| 2154 |
dir = opendir(buf); |
| 2155 |
if (!dir) |
| 2156 |
return -errno; |
| 2157 |
|
| 2158 |
FOREACH_DIRENT_ALL(de, dir, break) { |
| 2159 |
if (de->d_name[0] == '.') |
| 2160 |
continue; |
| 2161 |
|
| 2162 |
strscpyl_full(p, size, &truncated, de->d_name, tail, NULL); |
| 2163 |
if (truncated) |
| 2164 |
continue; |
| 2165 |
|
| 2166 |
if (faccessat(dirfd(dir), p, F_OK, 0) < 0) |
| 2167 |
continue; |
| 2168 |
|
| 2169 |
strcpy(attr, buf); |
| 2170 |
return 0; |
| 2171 |
} |
| 2172 |
|
| 2173 |
return -ENOENT; |
| 2174 |
} |
| 2175 |
|
| 2176 |
static size_t udev_replace_ifname_strict(char *str) { |
| 2177 |
size_t replaced = 0; |
| 2178 |
|
| 2179 |
assert(str); |
| 2180 |
|
| 2181 |
/* See ifname_valid_full(). */ |
| 2182 |
|
| 2183 |
for (char *p = str; *p != '\0'; p++) |
| 2184 |
if (!ifname_valid_char(*p)) { |
| 2185 |
*p = '_'; |
| 2186 |
replaced++; |
| 2187 |
} |
| 2188 |
|
| 2189 |
return replaced; |
| 2190 |
} |
| 2191 |
|
| 2192 |
static void udev_replace_ifname(UdevEvent *event, UdevRuleToken *token, char *buf) { |
| 2193 |
assert(event); |
| 2194 |
assert(token); |
| 2195 |
assert(buf); |
| 2196 |
|
| 2197 |
size_t count; |
| 2198 |
if (naming_scheme_has(NAMING_REPLACE_STRICTLY)) |
| 2199 |
count = udev_replace_ifname_strict(buf); |
| 2200 |
else |
| 2201 |
count = udev_replace_chars(buf, "/"); |
| 2202 |
if (count > 0) |
| 2203 |
log_event_trace(event, token, |
| 2204 |
"Replaced %zu character(s) from network interface name, results to \"%s\"", |
| 2205 |
count, buf); |
| 2206 |
} |
| 2207 |
|
| 2208 |
static void udev_replace_chars_and_log(UdevEvent *event, UdevRuleToken *token, char *buf, const char *allow, const char *what) { |
| 2209 |
assert(event); |
| 2210 |
assert(token); |
| 2211 |
assert(buf); |
| 2212 |
assert(what); |
| 2213 |
|
| 2214 |
size_t count = udev_replace_chars(buf, allow); |
| 2215 |
if (count > 0) |
| 2216 |
log_event_trace(event, token, |
| 2217 |
"Replaced %zu character(s) from %s, results to \"%s\"", |
| 2218 |
count, what, buf); |
| 2219 |
} |
| 2220 |
|
| 2221 |
static int udev_rule_apply_token_to_event( |
| 2222 |
UdevRuleToken *token, |
| 2223 |
sd_device *dev, |
| 2224 |
UdevEvent *event) { |
| 2225 |
|
| 2226 |
int r; |
| 2227 |
|
| 2228 |
assert(token); |
| 2229 |
assert(dev); |
| 2230 |
assert(event); |
| 2231 |
|
| 2232 |
/* This returns the following values: |
| 2233 |
* 0 on the current token does not match the event, |
| 2234 |
* 1 on the current token matches the event, and |
| 2235 |
* negative errno on some critical errors. */ |
| 2236 |
|
| 2237 |
switch (token->type) { |
| 2238 |
case TK_M_ACTION: { |
| 2239 |
sd_device_action_t a; |
| 2240 |
|
| 2241 |
r = sd_device_get_action(dev, &a); |
| 2242 |
if (r < 0) |
| 2243 |
return log_event_error_errno(event, token, r, "Failed to get uevent action type: %m"); |
| 2244 |
|
| 2245 |
return token_match_string(event, token, device_action_to_string(a), /* log_result= */ true); |
| 2246 |
} |
| 2247 |
case TK_M_DEVPATH: { |
| 2248 |
const char *val; |
| 2249 |
|
| 2250 |
r = sd_device_get_devpath(dev, &val); |
| 2251 |
if (r < 0) |
| 2252 |
return log_event_error_errno(event, token, r, "Failed to get devpath: %m"); |
| 2253 |
|
| 2254 |
return token_match_string(event, token, val, /* log_result= */ true); |
| 2255 |
} |
| 2256 |
case TK_M_KERNEL: |
| 2257 |
case TK_M_PARENTS_KERNEL: { |
| 2258 |
const char *val; |
| 2259 |
|
| 2260 |
r = sd_device_get_sysname(dev, &val); |
| 2261 |
if (r < 0) |
| 2262 |
return log_event_error_errno(event, token, r, "Failed to get sysname: %m"); |
| 2263 |
|
| 2264 |
return token_match_string(event, token, val, /* log_result= */ true); |
| 2265 |
} |
| 2266 |
case TK_M_DEVLINK: |
| 2267 |
FOREACH_DEVICE_DEVLINK(dev, val) |
| 2268 |
if (token_match_string(event, token, strempty(startswith(val, "/dev/")), /* log_result= */ false) == (token->op == OP_MATCH)) |
| 2269 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2270 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2271 |
|
| 2272 |
case TK_M_NAME: |
| 2273 |
return token_match_string(event, token, event->name, /* log_result= */ true); |
| 2274 |
|
| 2275 |
case TK_M_ENV: { |
| 2276 |
const char *val = NULL; |
| 2277 |
|
| 2278 |
(void) device_get_property_value_with_fallback(dev, token->data, event->worker ? event->worker->properties : NULL, &val); |
| 2279 |
|
| 2280 |
return token_match_string(event, token, val, /* log_result= */ true); |
| 2281 |
} |
| 2282 |
case TK_M_CONST: { |
| 2283 |
const char *val, *k = token->data; |
| 2284 |
|
| 2285 |
if (streq(k, "arch")) |
| 2286 |
val = architecture_to_string(uname_architecture()); |
| 2287 |
else if (streq(k, "virt")) |
| 2288 |
val = virtualization_to_string(detect_virtualization()); |
| 2289 |
else if (streq(k, "cvm")) |
| 2290 |
val = confidential_virtualization_to_string(detect_confidential_virtualization()); |
| 2291 |
else |
| 2292 |
assert_not_reached(); |
| 2293 |
return token_match_string(event, token, val, /* log_result= */ true); |
| 2294 |
} |
| 2295 |
case TK_M_TAG: |
| 2296 |
case TK_M_PARENTS_TAG: |
| 2297 |
FOREACH_DEVICE_CURRENT_TAG(dev, val) |
| 2298 |
if (token_match_string(event, token, val, /* log_result= */ false) == (token->op == OP_MATCH)) |
| 2299 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2300 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2301 |
|
| 2302 |
case TK_M_SUBSYSTEM: |
| 2303 |
case TK_M_PARENTS_SUBSYSTEM: { |
| 2304 |
const char *val; |
| 2305 |
|
| 2306 |
r = sd_device_get_subsystem(dev, &val); |
| 2307 |
if (r == -ENOENT) |
| 2308 |
val = NULL; |
| 2309 |
else if (r < 0) |
| 2310 |
return log_event_error_errno(event, token, r, "Failed to get subsystem: %m"); |
| 2311 |
|
| 2312 |
return token_match_string(event, token, val, /* log_result= */ true); |
| 2313 |
} |
| 2314 |
case TK_M_DRIVER: |
| 2315 |
case TK_M_PARENTS_DRIVER: { |
| 2316 |
const char *val; |
| 2317 |
|
| 2318 |
r = sd_device_get_driver(dev, &val); |
| 2319 |
if (r == -ENOENT) |
| 2320 |
val = NULL; |
| 2321 |
else if (r < 0) |
| 2322 |
return log_event_error_errno(event, token, r, "Failed to get driver: %m"); |
| 2323 |
|
| 2324 |
return token_match_string(event, token, val, /* log_result= */ true); |
| 2325 |
} |
| 2326 |
case TK_M_ATTR: |
| 2327 |
case TK_M_PARENTS_ATTR: |
| 2328 |
return token_match_attr(token, dev, event); |
| 2329 |
|
| 2330 |
case TK_M_SYSCTL: { |
| 2331 |
_cleanup_free_ char *value = NULL; |
| 2332 |
char buf[UDEV_PATH_SIZE]; |
| 2333 |
|
| 2334 |
if (!apply_format_attr(event, token, buf, sizeof(buf), "sysctl entry name")) |
| 2335 |
return false; |
| 2336 |
|
| 2337 |
r = sysctl_read(sysctl_normalize(buf), &value); |
| 2338 |
if (r < 0 && r != -ENOENT) |
| 2339 |
return log_event_error_errno(event, token, r, "Failed to read sysctl \"%s\": %m", buf); |
| 2340 |
|
| 2341 |
return token_match_string(event, token, strstrip(value), /* log_result= */ true); |
| 2342 |
} |
| 2343 |
case TK_M_TEST: { |
| 2344 |
mode_t mode = PTR_TO_MODE(token->data); |
| 2345 |
char buf[UDEV_PATH_SIZE]; |
| 2346 |
struct stat statbuf; |
| 2347 |
bool match; |
| 2348 |
|
| 2349 |
if (!apply_format_value(event, token, buf, sizeof(buf), "file name")) |
| 2350 |
return false; |
| 2351 |
|
| 2352 |
if (!path_is_absolute(buf) && |
| 2353 |
udev_resolve_subsys_kernel(buf, buf, sizeof(buf), false) < 0) { |
| 2354 |
char tmp[UDEV_PATH_SIZE]; |
| 2355 |
const char *val; |
| 2356 |
|
| 2357 |
r = sd_device_get_syspath(dev, &val); |
| 2358 |
if (r < 0) |
| 2359 |
return log_event_error_errno(event, token, r, "Failed to get syspath: %m"); |
| 2360 |
|
| 2361 |
bool truncated; |
| 2362 |
strscpy_full(tmp, sizeof(tmp), buf, &truncated); |
| 2363 |
assert(!truncated); |
| 2364 |
strscpyl_full(buf, sizeof(buf), &truncated, val, "/", tmp, NULL); |
| 2365 |
if (truncated) |
| 2366 |
return log_event_result(event, token, false); |
| 2367 |
} |
| 2368 |
|
| 2369 |
r = attr_subst_subdir(buf); |
| 2370 |
if (r == -ENOENT) |
| 2371 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2372 |
if (r < 0) |
| 2373 |
return log_event_error_errno(event, token, r, "Failed to test for the existence of \"%s\": %m", buf); |
| 2374 |
|
| 2375 |
if (stat(buf, &statbuf) < 0) { |
| 2376 |
if (errno != ENOENT) |
| 2377 |
log_event_warning_errno(event, token, errno, "Failed to stat \"%s\", ignoring: %m", buf); |
| 2378 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2379 |
} |
| 2380 |
|
| 2381 |
if (mode == MODE_INVALID) |
| 2382 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2383 |
|
| 2384 |
match = (statbuf.st_mode & mode) > 0; |
| 2385 |
return log_event_result(event, token, token->op == (match ? OP_MATCH : OP_NOMATCH)); |
| 2386 |
} |
| 2387 |
case TK_M_PROGRAM: { |
| 2388 |
char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE]; |
| 2389 |
|
| 2390 |
event->program_result = mfree(event->program_result); |
| 2391 |
|
| 2392 |
if (!apply_format_value(event, token, buf, sizeof(buf), "command")) |
| 2393 |
return false; |
| 2394 |
|
| 2395 |
log_event_debug(event, token, "Running command \"%s\"", buf); |
| 2396 |
|
| 2397 |
r = udev_event_spawn(event, /* accept_failure= */ true, buf, result, sizeof(result), NULL); |
| 2398 |
if (r != 0) { |
| 2399 |
if (r < 0) |
| 2400 |
log_event_warning_errno(event, token, r, "Failed to execute \"%s\": %m", buf); |
| 2401 |
else /* returned value is positive when program fails */ |
| 2402 |
log_event_debug(event, token, "Command \"%s\" returned %d (error)", buf, r); |
| 2403 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2404 |
} |
| 2405 |
|
| 2406 |
delete_trailing_chars(result, "\n"); |
| 2407 |
udev_replace_chars_and_log(event, token, result, UDEV_ALLOWED_CHARS_INPUT, "command output"); |
| 2408 |
|
| 2409 |
event->program_result = strdup(result); |
| 2410 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2411 |
} |
| 2412 |
case TK_M_IMPORT_FILE: { |
| 2413 |
_cleanup_fclose_ FILE *f = NULL; |
| 2414 |
char buf[UDEV_PATH_SIZE]; |
| 2415 |
|
| 2416 |
if (!apply_format_value(event, token, buf, sizeof(buf), "file name to be imported")) |
| 2417 |
return false; |
| 2418 |
|
| 2419 |
log_event_debug(event, token, "Importing properties from \"%s\"", buf); |
| 2420 |
|
| 2421 |
f = fopen(buf, "re"); |
| 2422 |
if (!f) { |
| 2423 |
if (errno != ENOENT) |
| 2424 |
return log_event_error_errno(event, token, errno, "Failed to open \"%s\": %m", buf); |
| 2425 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2426 |
} |
| 2427 |
|
| 2428 |
for (;;) { |
| 2429 |
_cleanup_free_ char *line = NULL; |
| 2430 |
char *key, *value; |
| 2431 |
|
| 2432 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 2433 |
if (r < 0) { |
| 2434 |
log_event_debug_errno(event, token, r, "Failed to read \"%s\", ignoring: %m", buf); |
| 2435 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2436 |
} |
| 2437 |
if (r == 0) |
| 2438 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2439 |
|
| 2440 |
r = get_property_from_string(line, &key, &value); |
| 2441 |
if (r < 0) { |
| 2442 |
log_event_debug_errno(event, token, r, |
| 2443 |
"Failed to parse key and value from \"%s\", ignoring: %m", |
| 2444 |
line); |
| 2445 |
continue; |
| 2446 |
} |
| 2447 |
if (r == 0) |
| 2448 |
continue; |
| 2449 |
|
| 2450 |
r = device_add_property(dev, key, value); |
| 2451 |
if (r == -ENOMEM) |
| 2452 |
return log_oom(); |
| 2453 |
if (r < 0) |
| 2454 |
log_event_warning_errno(event, token, r, "Failed to import property \"%s=%s\", ignoring: %m", key, value); |
| 2455 |
else |
| 2456 |
log_event_trace(event, token, "Imported property \"%s=%s\".", key, value); |
| 2457 |
} |
| 2458 |
|
| 2459 |
assert_not_reached(); |
| 2460 |
} |
| 2461 |
case TK_M_IMPORT_PROGRAM: { |
| 2462 |
_cleanup_strv_free_ char **lines = NULL; |
| 2463 |
char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE]; |
| 2464 |
bool truncated; |
| 2465 |
|
| 2466 |
if (!apply_format_value(event, token, buf, sizeof(buf), "command")) |
| 2467 |
return false; |
| 2468 |
|
| 2469 |
log_event_debug(event, token, "Importing properties from results of \"%s\"", buf); |
| 2470 |
|
| 2471 |
r = udev_event_spawn(event, /* accept_failure= */ true, buf, result, sizeof result, &truncated); |
| 2472 |
if (r != 0) { |
| 2473 |
if (r < 0) |
| 2474 |
log_event_warning_errno(event, token, r, "Failed to execute \"%s\", ignoring: %m", buf); |
| 2475 |
else /* returned value is positive when program fails */ |
| 2476 |
log_event_debug(event, token, "Command \"%s\" returned %d (error), ignoring", buf, r); |
| 2477 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2478 |
} |
| 2479 |
|
| 2480 |
if (truncated) { |
| 2481 |
log_event_debug(event, token, "Result of \"%s\" is too long and truncated, ignoring the last line of the result.", buf); |
| 2482 |
|
| 2483 |
/* Drop the last line. */ |
| 2484 |
bool found = false; |
| 2485 |
for (char *p = PTR_SUB1(buf + strlen(buf), buf); p; p = PTR_SUB1(p, buf)) |
| 2486 |
if (strchr(NEWLINE, *p)) { |
| 2487 |
*p = '\0'; |
| 2488 |
found = true; |
| 2489 |
break; |
| 2490 |
} |
| 2491 |
if (!found) |
| 2492 |
buf[0] = '\0'; |
| 2493 |
} |
| 2494 |
|
| 2495 |
r = strv_split_newlines_full(&lines, result, EXTRACT_RETAIN_ESCAPE); |
| 2496 |
if (r == -ENOMEM) |
| 2497 |
return log_oom(); |
| 2498 |
if (r < 0) { |
| 2499 |
log_event_warning_errno(event, token, r, |
| 2500 |
"Failed to extract lines from result of command \"%s\", ignoring: %m", buf); |
| 2501 |
return log_event_result(event, token, false); |
| 2502 |
} |
| 2503 |
|
| 2504 |
STRV_FOREACH(line, lines) { |
| 2505 |
char *key, *value; |
| 2506 |
|
| 2507 |
r = get_property_from_string(*line, &key, &value); |
| 2508 |
if (r < 0) { |
| 2509 |
log_event_debug_errno(event, token, r, |
| 2510 |
"Failed to parse key and value from \"%s\", ignoring: %m", |
| 2511 |
*line); |
| 2512 |
continue; |
| 2513 |
} |
| 2514 |
if (r == 0) |
| 2515 |
continue; |
| 2516 |
|
| 2517 |
r = device_add_property(dev, key, value); |
| 2518 |
if (r == -ENOMEM) |
| 2519 |
return log_oom(); |
| 2520 |
if (r < 0) |
| 2521 |
log_event_warning_errno(event, token, r, "Failed to import property \"%s=%s\", ignoring: %m", key, value); |
| 2522 |
else |
| 2523 |
log_event_trace(event, token, "Imported property \"%s=%s\".", key, value); |
| 2524 |
} |
| 2525 |
|
| 2526 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2527 |
} |
| 2528 |
case TK_M_IMPORT_BUILTIN: { |
| 2529 |
UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data); |
| 2530 |
assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); |
| 2531 |
unsigned mask = 1U << (int) cmd; |
| 2532 |
char buf[UDEV_LINE_SIZE]; |
| 2533 |
|
| 2534 |
if (udev_builtin_run_once(cmd)) { |
| 2535 |
/* check if we ran already */ |
| 2536 |
if (event->builtin_run & mask) { |
| 2537 |
log_event_debug(event, token, "Builtin command \"%s\" has already run, skipping.", |
| 2538 |
udev_builtin_name(cmd)); |
| 2539 |
/* return the result from earlier run */ |
| 2540 |
return log_event_result(event, token, token->op == (event->builtin_ret & mask ? OP_NOMATCH : OP_MATCH)); |
| 2541 |
} |
| 2542 |
/* mark as ran */ |
| 2543 |
event->builtin_run |= mask; |
| 2544 |
} |
| 2545 |
|
| 2546 |
if (!apply_format_value(event, token, buf, sizeof(buf), "builtin command")) |
| 2547 |
return false; |
| 2548 |
|
| 2549 |
log_event_debug(event, token, "Importing properties from results of builtin command \"%s\".", buf); |
| 2550 |
|
| 2551 |
r = udev_builtin_run(event, cmd, buf); |
| 2552 |
if (r < 0) { |
| 2553 |
/* remember failure */ |
| 2554 |
log_event_debug_errno(event, token, r, "Failed to run builtin \"%s\": %m", buf); |
| 2555 |
event->builtin_ret |= mask; |
| 2556 |
} |
| 2557 |
return log_event_result(event, token, token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH)); |
| 2558 |
} |
| 2559 |
case TK_M_IMPORT_DB: { |
| 2560 |
const char *val; |
| 2561 |
|
| 2562 |
if (!event->dev_db_clone) |
| 2563 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2564 |
r = sd_device_get_property_value(event->dev_db_clone, token->value, &val); |
| 2565 |
if (r == -ENOENT) |
| 2566 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2567 |
if (r < 0) |
| 2568 |
return log_event_error_errno(event, token, r, |
| 2569 |
"Failed to get property \"%s\" from database: %m", |
| 2570 |
token->value); |
| 2571 |
|
| 2572 |
r = device_add_property(dev, token->value, val); |
| 2573 |
if (r == -ENOMEM) |
| 2574 |
return log_oom(); |
| 2575 |
if (r < 0) |
| 2576 |
log_event_warning_errno(event, token, r, "Failed to import property \"%s=%s\", ignoring: %m", token->value, val); |
| 2577 |
else |
| 2578 |
log_event_trace(event, token, "Imported property \"%s=%s\".", token->value, val); |
| 2579 |
|
| 2580 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2581 |
} |
| 2582 |
case TK_M_IMPORT_CMDLINE: { |
| 2583 |
_cleanup_free_ char *value = NULL; |
| 2584 |
|
| 2585 |
r = proc_cmdline_get_key(token->value, PROC_CMDLINE_VALUE_OPTIONAL, &value); |
| 2586 |
if (r < 0) |
| 2587 |
return log_event_error_errno(event, token, r, |
| 2588 |
"Failed to read \"%s\" option from /proc/cmdline: %m", |
| 2589 |
token->value); |
| 2590 |
if (r == 0) |
| 2591 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2592 |
|
| 2593 |
const char *val = value ?: "1"; |
| 2594 |
r = device_add_property(dev, token->value, val); |
| 2595 |
if (r == -ENOMEM) |
| 2596 |
return log_oom(); |
| 2597 |
if (r < 0) |
| 2598 |
log_event_warning_errno(event, token, r, "Failed to import property \"%s=%s\", ignoring: %m", token->value, val); |
| 2599 |
else |
| 2600 |
log_event_trace(event, token, "Imported property \"%s=%s\".", token->value, val); |
| 2601 |
|
| 2602 |
return log_event_result(event, token, token->op == OP_MATCH); |
| 2603 |
} |
| 2604 |
case TK_M_IMPORT_PARENT: { |
| 2605 |
char buf[UDEV_PATH_SIZE]; |
| 2606 |
|
| 2607 |
if (!apply_format_value(event, token, buf, sizeof(buf), "property name")) |
| 2608 |
return false; |
| 2609 |
|
| 2610 |
sd_device *parent; |
| 2611 |
r = sd_device_get_parent(dev, &parent); |
| 2612 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 2613 |
return log_event_result(event, token, token->op == OP_NOMATCH); |
| 2614 |
if (r < 0) |
| 2615 |
return log_event_error_errno(event, token, r, "Failed to get parent device: %m"); |
| 2616 |
|
| 2617 |
bool have = false; |
| 2618 |
FOREACH_DEVICE_PROPERTY(parent, key, val) { |
| 2619 |
if (fnmatch(buf, key, 0) != 0) |
| 2620 |
continue; |
| 2621 |
|
| 2622 |
r = device_add_property(dev, key, val); |
| 2623 |
if (r == -ENOMEM) |
| 2624 |
return log_oom(); |
| 2625 |
if (r < 0) |
| 2626 |
log_event_warning_errno(event, token, r, "Failed to import property \"%s=%s\", ignoring: %m", key, val); |
| 2627 |
else { |
| 2628 |
log_event_trace(event, token, "Imported property \"%s=%s\".", key, val); |
| 2629 |
have = true; |
| 2630 |
} |
| 2631 |
} |
| 2632 |
|
| 2633 |
return log_event_result(event, token, token->op == (have ? OP_MATCH : OP_NOMATCH)); |
| 2634 |
} |
| 2635 |
case TK_M_RESULT: |
| 2636 |
return token_match_string(event, token, event->program_result, /* log_result= */ true); |
| 2637 |
|
| 2638 |
case TK_A_OPTIONS_DUMP: { |
| 2639 |
sd_json_format_flags_t flags = PTR_TO_UINT(token->data); |
| 2640 |
|
| 2641 |
log_event_info(event, token, "Dumping current state:"); |
| 2642 |
|
| 2643 |
_cleanup_(memstream_done) MemStream m = {}; |
| 2644 |
FILE *f = memstream_init(&m); |
| 2645 |
if (!f) |
| 2646 |
return log_oom(); |
| 2647 |
|
| 2648 |
(void) dump_event(event, flags, f); |
| 2649 |
|
| 2650 |
_cleanup_free_ char *buf = NULL; |
| 2651 |
r = memstream_finalize(&m, &buf, NULL); |
| 2652 |
if (r < 0) |
| 2653 |
log_event_warning_errno(event, token, r, "Failed to finalize memory stream, ignoring: %m"); |
| 2654 |
else |
| 2655 |
log_device_info(dev, "%s", buf); |
| 2656 |
|
| 2657 |
log_event_info(event, token, "DONE"); |
| 2658 |
return true; |
| 2659 |
} |
| 2660 |
case TK_A_OPTIONS_STRING_ESCAPE_NONE: |
| 2661 |
event->esc = ESCAPE_NONE; |
| 2662 |
return log_event_done(event, token); |
| 2663 |
|
| 2664 |
case TK_A_OPTIONS_STRING_ESCAPE_REPLACE: |
| 2665 |
event->esc = ESCAPE_REPLACE; |
| 2666 |
return log_event_done(event, token); |
| 2667 |
|
| 2668 |
case TK_A_OPTIONS_DB_PERSIST: |
| 2669 |
device_set_db_persist(dev); |
| 2670 |
return log_event_done(event, token); |
| 2671 |
|
| 2672 |
case TK_A_OPTIONS_INOTIFY_WATCH: |
| 2673 |
if (event->inotify_watch_final) |
| 2674 |
return log_event_final_set(event, token); |
| 2675 |
|
| 2676 |
if (token->op == OP_ASSIGN_FINAL) |
| 2677 |
event->inotify_watch_final = true; |
| 2678 |
|
| 2679 |
event->inotify_watch = token->data; |
| 2680 |
return log_event_done(event, token); |
| 2681 |
|
| 2682 |
case TK_A_OPTIONS_DEVLINK_PRIORITY: |
| 2683 |
device_set_devlink_priority(dev, PTR_TO_INT(token->data)); |
| 2684 |
return log_event_done(event, token); |
| 2685 |
|
| 2686 |
case TK_A_OPTIONS_LOG_LEVEL: { |
| 2687 |
int level = PTR_TO_INT(token->data); |
| 2688 |
|
| 2689 |
if (level < 0) |
| 2690 |
level = event->default_log_level; |
| 2691 |
|
| 2692 |
if (event->event_mode == EVENT_UDEV_WORKER) |
| 2693 |
log_set_max_level(level); |
| 2694 |
else { |
| 2695 |
_cleanup_free_ char *level_str = NULL; |
| 2696 |
(void) log_level_to_string_alloc(level, &level_str); |
| 2697 |
log_event_debug(event, token, "Running in test mode, skipping changing maximum log level to %s.", strna(level_str)); |
| 2698 |
} |
| 2699 |
|
| 2700 |
if (level == LOG_DEBUG && !event->log_level_was_debug) { |
| 2701 |
/* The log level becomes LOG_DEBUG at first time. Let's log basic information. */ |
| 2702 |
if (event->event_mode == EVENT_UDEV_WORKER) |
| 2703 |
log_device_uevent(dev, "The log level is changed to 'debug' while processing device"); |
| 2704 |
event->log_level_was_debug = true; |
| 2705 |
} |
| 2706 |
|
| 2707 |
return log_event_done(event, token); |
| 2708 |
} |
| 2709 |
case TK_A_OWNER: { |
| 2710 |
char owner[UDEV_NAME_SIZE]; |
| 2711 |
|
| 2712 |
if (event->owner_final) |
| 2713 |
return log_event_final_set(event, token); |
| 2714 |
|
| 2715 |
if (token->op == OP_ASSIGN_FINAL) |
| 2716 |
event->owner_final = true; |
| 2717 |
|
| 2718 |
if (!apply_format_value(event, token, owner, sizeof(owner), "user name")) |
| 2719 |
return true; |
| 2720 |
|
| 2721 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 2722 |
r = userdb_by_name(owner, /* match= */ NULL, |
| 2723 |
USERDB_SUPPRESS_SHADOW | USERDB_PARSE_NUMERIC | USERDB_SYNTHESIZE_NUMERIC, |
| 2724 |
&ur); |
| 2725 |
if (r < 0) |
| 2726 |
log_event_error_errno(event, token, r, |
| 2727 |
"Failed to resolve user \"%s\", ignoring: %s", |
| 2728 |
owner, STRERROR_USER(r)); |
| 2729 |
else { |
| 2730 |
if (!uid_is_system(ur->uid)) |
| 2731 |
log_udev_event_syntax(event, token, LOG_WARNING, |
| 2732 |
SD_MESSAGE_SYSTEM_ACCOUNT_REQUIRED_STR, |
| 2733 |
"User %s configured to own a device node is not a system user. " |
| 2734 |
"Support for device node ownership by non-system accounts is deprecated and will be removed in the future.", |
| 2735 |
owner); |
| 2736 |
|
| 2737 |
event->uid = ur->uid; |
| 2738 |
log_event_debug(event, token, "Set owner: %s("UID_FMT")", owner, event->uid); |
| 2739 |
} |
| 2740 |
return true; |
| 2741 |
} |
| 2742 |
case TK_A_GROUP: { |
| 2743 |
char group[UDEV_NAME_SIZE]; |
| 2744 |
|
| 2745 |
if (event->group_final) |
| 2746 |
return log_event_final_set(event, token); |
| 2747 |
|
| 2748 |
if (token->op == OP_ASSIGN_FINAL) |
| 2749 |
event->group_final = true; |
| 2750 |
|
| 2751 |
if (!apply_format_value(event, token, group, sizeof(group), "group name")) |
| 2752 |
return true; |
| 2753 |
|
| 2754 |
_cleanup_(group_record_unrefp) GroupRecord *gr = NULL; |
| 2755 |
r = groupdb_by_name(group, /* match= */ NULL, |
| 2756 |
USERDB_SUPPRESS_SHADOW | USERDB_PARSE_NUMERIC | USERDB_SYNTHESIZE_NUMERIC, |
| 2757 |
&gr); |
| 2758 |
if (r < 0) |
| 2759 |
log_event_error_errno(event, token, r, |
| 2760 |
"Failed to resolve group \"%s\", ignoring: %s", |
| 2761 |
group, STRERROR_GROUP(r)); |
| 2762 |
else { |
| 2763 |
if (!gid_is_system(gr->gid)) |
| 2764 |
log_udev_event_syntax(event, token, LOG_WARNING, |
| 2765 |
SD_MESSAGE_SYSTEM_ACCOUNT_REQUIRED_STR, |
| 2766 |
"Group %s configured to own a device node is not a system group. " |
| 2767 |
"Support for device node ownership by non-system accounts is deprecated and will be removed in the future.", |
| 2768 |
group); |
| 2769 |
|
| 2770 |
event->gid = gr->gid; |
| 2771 |
log_event_debug(event, token, "Set group: %s("GID_FMT")", group, event->gid); |
| 2772 |
} |
| 2773 |
return true; |
| 2774 |
} |
| 2775 |
case TK_A_MODE: { |
| 2776 |
char mode_str[UDEV_NAME_SIZE]; |
| 2777 |
|
| 2778 |
if (event->mode_final) |
| 2779 |
return log_event_final_set(event, token); |
| 2780 |
|
| 2781 |
if (token->op == OP_ASSIGN_FINAL) |
| 2782 |
event->mode_final = true; |
| 2783 |
|
| 2784 |
if (!apply_format_value(event, token, mode_str, sizeof(mode_str), "mode")) |
| 2785 |
return true; |
| 2786 |
|
| 2787 |
r = parse_mode(mode_str, &event->mode); |
| 2788 |
if (r < 0) |
| 2789 |
log_event_error_errno(event, token, r, "Failed to parse mode \"%s\", ignoring: %m", mode_str); |
| 2790 |
else |
| 2791 |
log_event_debug(event, token, "Set mode: %#o", event->mode); |
| 2792 |
return true; |
| 2793 |
} |
| 2794 |
case TK_A_OWNER_ID: |
| 2795 |
if (event->owner_final) |
| 2796 |
return log_event_final_set(event, token); |
| 2797 |
|
| 2798 |
if (token->op == OP_ASSIGN_FINAL) |
| 2799 |
event->owner_final = true; |
| 2800 |
|
| 2801 |
event->uid = PTR_TO_UID(ASSERT_PTR(token->data)); |
| 2802 |
log_event_debug(event, token, "Set owner ID: %u", event->uid); |
| 2803 |
return true; |
| 2804 |
|
| 2805 |
case TK_A_GROUP_ID: |
| 2806 |
if (event->group_final) |
| 2807 |
return log_event_final_set(event, token); |
| 2808 |
|
| 2809 |
if (token->op == OP_ASSIGN_FINAL) |
| 2810 |
event->group_final = true; |
| 2811 |
|
| 2812 |
event->gid = PTR_TO_GID(ASSERT_PTR(token->data)); |
| 2813 |
log_event_debug(event, token, "Set group ID: %u", event->gid); |
| 2814 |
return true; |
| 2815 |
|
| 2816 |
case TK_A_MODE_ID: |
| 2817 |
if (event->mode_final) |
| 2818 |
return log_event_final_set(event, token); |
| 2819 |
|
| 2820 |
if (token->op == OP_ASSIGN_FINAL) |
| 2821 |
event->mode_final = true; |
| 2822 |
|
| 2823 |
event->mode = PTR_TO_MODE(ASSERT_PTR(token->data)); |
| 2824 |
log_event_debug(event, token, "Set mode: %#o", event->mode); |
| 2825 |
return true; |
| 2826 |
|
| 2827 |
case TK_A_SECLABEL: { |
| 2828 |
_cleanup_free_ char *name = NULL, *label = NULL; |
| 2829 |
char label_str[UDEV_LINE_SIZE] = {}; |
| 2830 |
|
| 2831 |
name = strdup(token->data); |
| 2832 |
if (!name) |
| 2833 |
return log_oom(); |
| 2834 |
|
| 2835 |
if (!apply_format_value(event, token, label_str, sizeof(label_str), "security label")) |
| 2836 |
return true; |
| 2837 |
|
| 2838 |
if (!isempty(label_str)) |
| 2839 |
label = strdup(label_str); |
| 2840 |
else |
| 2841 |
label = strdup(token->value); |
| 2842 |
if (!label) |
| 2843 |
return log_oom(); |
| 2844 |
|
| 2845 |
if (token->op == OP_ASSIGN) |
| 2846 |
ordered_hashmap_clear(event->seclabel_list); |
| 2847 |
|
| 2848 |
r = ordered_hashmap_ensure_put(&event->seclabel_list, &trivial_hash_ops_free_free, name, label); |
| 2849 |
if (r == -ENOMEM) |
| 2850 |
return log_oom(); |
| 2851 |
if (r < 0) |
| 2852 |
return log_event_error_errno(event, token, r, "Failed to store security label \"%s=%s\": %m", name, label); |
| 2853 |
|
| 2854 |
log_event_debug(event, token, "Set security label: %s=%s", name, label); |
| 2855 |
|
| 2856 |
TAKE_PTR(name); |
| 2857 |
TAKE_PTR(label); |
| 2858 |
return true; |
| 2859 |
} |
| 2860 |
case TK_A_ENV: { |
| 2861 |
const char *val, *name = token->data; |
| 2862 |
char value_new[UDEV_NAME_SIZE], *p = value_new; |
| 2863 |
size_t l = sizeof(value_new); |
| 2864 |
|
| 2865 |
if (isempty(token->value)) { |
| 2866 |
if (token->op == OP_ADD) |
| 2867 |
return log_event_done(event, token); |
| 2868 |
|
| 2869 |
r = device_add_property(dev, name, NULL); |
| 2870 |
if (r < 0) |
| 2871 |
return log_event_error_errno(event, token, r, "Failed to remove property \"%s\": %m", name); |
| 2872 |
log_event_trace(event, token, "Removed property \"%s\".", name); |
| 2873 |
return true; |
| 2874 |
} |
| 2875 |
|
| 2876 |
if (token->op == OP_ADD && |
| 2877 |
device_get_property_value_with_fallback(dev, name, event->worker ? event->worker->properties : NULL, &val) >= 0) { |
| 2878 |
bool truncated; |
| 2879 |
l = strpcpyl_full(&p, l, &truncated, val, " ", NULL); |
| 2880 |
if (truncated) { |
| 2881 |
log_event_warning(event, token, |
| 2882 |
"The buffer for the property is full, refusing to append new property \"%s=%s\".", |
| 2883 |
name, token->value); |
| 2884 |
return true; |
| 2885 |
} |
| 2886 |
} |
| 2887 |
|
| 2888 |
if (!apply_format_value(event, token, p, l, "property value")) |
| 2889 |
return true; |
| 2890 |
|
| 2891 |
if (event->esc == ESCAPE_REPLACE) |
| 2892 |
udev_replace_chars_and_log(event, token, p, /* allow= */ NULL, "property value"); |
| 2893 |
|
| 2894 |
r = device_add_property(dev, name, value_new); |
| 2895 |
if (r == -ENOMEM) |
| 2896 |
return log_oom(); |
| 2897 |
if (r < 0) |
| 2898 |
log_event_warning_errno(event, token, r, "Failed to set property \"%s=%s\", ignoring: %m", name, value_new); |
| 2899 |
else |
| 2900 |
log_event_trace(event, token, "Set property \"%s=%s\".", name, value_new); |
| 2901 |
return true; |
| 2902 |
} |
| 2903 |
case TK_A_TAG: { |
| 2904 |
char buf[UDEV_PATH_SIZE]; |
| 2905 |
|
| 2906 |
if (!apply_format_value(event, token, buf, sizeof(buf), "tag name")) |
| 2907 |
return true; |
| 2908 |
|
| 2909 |
if (token->op == OP_REMOVE) { |
| 2910 |
device_remove_tag(dev, buf); |
| 2911 |
log_event_trace(event, token, "Removed tag \"%s\".", buf); |
| 2912 |
return true; |
| 2913 |
} |
| 2914 |
|
| 2915 |
assert(IN_SET(token->op, OP_ASSIGN, OP_ADD)); |
| 2916 |
|
| 2917 |
if (token->op == OP_ASSIGN) { |
| 2918 |
r = device_cleanup_tags(dev, event->dev_db_clone); |
| 2919 |
if (r < 0) |
| 2920 |
log_event_warning_errno(event, token, r, "Failed to clear previously assigned tags, ignoring: %m"); |
| 2921 |
} |
| 2922 |
|
| 2923 |
r = device_add_tag(dev, buf, /* both= */ true); |
| 2924 |
if (r == -ENOMEM) |
| 2925 |
return log_oom(); |
| 2926 |
if (r < 0) |
| 2927 |
log_event_warning_errno(event, token, r, "Failed to %s tag \"%s\", ignoring: %m", |
| 2928 |
token->op == OP_ASSIGN ? "set" : "add", buf); |
| 2929 |
else |
| 2930 |
log_event_trace(event, token, "%s tag \"%s\".", token->op == OP_ASSIGN ? "Set" : "Added", buf); |
| 2931 |
return true; |
| 2932 |
} |
| 2933 |
case TK_A_NAME: { |
| 2934 |
char buf[UDEV_PATH_SIZE]; |
| 2935 |
|
| 2936 |
if (event->name_final) |
| 2937 |
return log_event_final_set(event, token); |
| 2938 |
|
| 2939 |
if (token->op == OP_ASSIGN_FINAL) |
| 2940 |
event->name_final = true; |
| 2941 |
|
| 2942 |
if (sd_device_get_ifindex(dev, NULL) < 0) { |
| 2943 |
log_event_warning(event, token, "Only network interfaces can be renamed, ignoring."); |
| 2944 |
return true; |
| 2945 |
} |
| 2946 |
|
| 2947 |
if (!apply_format_value(event, token, buf, sizeof(buf), "network interface name")) |
| 2948 |
return true; |
| 2949 |
|
| 2950 |
if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) |
| 2951 |
udev_replace_ifname(event, token, buf); |
| 2952 |
|
| 2953 |
r = free_and_strdup_warn(&event->name, buf); |
| 2954 |
if (r < 0) |
| 2955 |
return r; |
| 2956 |
|
| 2957 |
log_event_debug(event, token, "Set network interface name: %s", event->name); |
| 2958 |
return true; |
| 2959 |
} |
| 2960 |
case TK_A_DEVLINK: { |
| 2961 |
char buf[UDEV_PATH_SIZE]; |
| 2962 |
|
| 2963 |
if (event->devlink_final) |
| 2964 |
return log_event_final_set(event, token); |
| 2965 |
|
| 2966 |
if (sd_device_get_devnum(dev, NULL) < 0) { |
| 2967 |
log_event_debug(event, token, "Device does not have device node, ignoring to manage device node symlink."); |
| 2968 |
return true; |
| 2969 |
} |
| 2970 |
|
| 2971 |
if (token->op == OP_ASSIGN_FINAL) |
| 2972 |
event->devlink_final = true; |
| 2973 |
|
| 2974 |
if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL)) |
| 2975 |
device_cleanup_devlinks(dev); |
| 2976 |
|
| 2977 |
if (!apply_format_full(event, token, token->value, buf, sizeof(buf), |
| 2978 |
/* replace_whitespace= */ event->esc != ESCAPE_NONE, |
| 2979 |
"symbolic link path")) |
| 2980 |
return true; |
| 2981 |
|
| 2982 |
/* By default or string_escape=none, allow multiple symlinks separated by spaces. */ |
| 2983 |
if (event->esc == ESCAPE_UNSET) |
| 2984 |
udev_replace_chars_and_log(event, token, buf, /* allow= */ "/ ", "device node symlink"); |
| 2985 |
else if (event->esc == ESCAPE_REPLACE) |
| 2986 |
udev_replace_chars_and_log(event, token, buf, /* allow= */ "/", "device node symlink"); |
| 2987 |
|
| 2988 |
for (const char *p = buf;;) { |
| 2989 |
_cleanup_free_ char *path = NULL; |
| 2990 |
|
| 2991 |
r = extract_first_word(&p, &path, NULL, EXTRACT_RETAIN_ESCAPE); |
| 2992 |
if (r == -ENOMEM) |
| 2993 |
return log_oom(); |
| 2994 |
if (r < 0) { |
| 2995 |
log_warning_errno(r, "Failed to extract first path in device node symlinks, ignoring: %m"); |
| 2996 |
return true; |
| 2997 |
} |
| 2998 |
if (r == 0) |
| 2999 |
return true; |
| 3000 |
|
| 3001 |
if (token->op == OP_REMOVE) { |
| 3002 |
r = device_remove_devlink(dev, path); |
| 3003 |
if (r == -ENOMEM) |
| 3004 |
return log_oom(); |
| 3005 |
if (r < 0) |
| 3006 |
log_event_warning_errno(event, token, r, "Failed to remove device node symlink \"%s\", ignoring: %m", path); |
| 3007 |
else if (r > 0) |
| 3008 |
log_event_debug(event, token, "Removed device node symlink \"%s\"", path); |
| 3009 |
} else { |
| 3010 |
r = device_add_devlink(dev, path); |
| 3011 |
if (r == -ENOMEM) |
| 3012 |
return log_oom(); |
| 3013 |
if (r < 0) |
| 3014 |
log_event_warning_errno(event, token, r, "Failed to add device node symlink \"%s\", ignoring: %m", path); |
| 3015 |
else if (r > 0) |
| 3016 |
log_event_debug(event, token, "Added device node symlink \"%s\".", path); |
| 3017 |
} |
| 3018 |
} |
| 3019 |
assert_not_reached(); |
| 3020 |
} |
| 3021 |
case TK_A_ATTR: { |
| 3022 |
char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE]; |
| 3023 |
const char *key = token->data; |
| 3024 |
|
| 3025 |
/* First, try to resolve "[<SUBSYSTEM>/<KERNEL>]<attribute>" format. */ |
| 3026 |
r = udev_resolve_subsys_kernel(key, buf, sizeof(buf), false); |
| 3027 |
if (r == -ENOMEM) |
| 3028 |
return log_oom(); |
| 3029 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 3030 |
log_event_warning_errno(event, token, r, "Failed to resolve sysfs attribute \"%s\", ignoring: %m", key); |
| 3031 |
return true; |
| 3032 |
} |
| 3033 |
if (r < 0) { |
| 3034 |
/* If not, make the path to sysfs attribute absolute, to make '*' resolvable by attr_subst_subdir(). */ |
| 3035 |
const char *syspath; |
| 3036 |
r = sd_device_get_syspath(dev, &syspath); |
| 3037 |
if (r < 0) |
| 3038 |
return log_event_error_errno(event, token, r, "Failed to get syspath: %m"); |
| 3039 |
|
| 3040 |
bool truncated; |
| 3041 |
strscpyl_full(buf, sizeof(buf), &truncated, syspath, "/", key, NULL); |
| 3042 |
if (truncated) { |
| 3043 |
log_event_warning(event, token, |
| 3044 |
"The path to the attribute \"%s/%s\" is too long, refusing to set the attribute.", |
| 3045 |
syspath, key); |
| 3046 |
return true; |
| 3047 |
} |
| 3048 |
|
| 3049 |
/* Resolve '*' in the path. */ |
| 3050 |
r = attr_subst_subdir(buf); |
| 3051 |
if (r < 0) { |
| 3052 |
log_event_error_errno(event, token, r, "Could not find file matches \"%s\", ignoring: %m", buf); |
| 3053 |
return true; |
| 3054 |
} |
| 3055 |
} |
| 3056 |
|
| 3057 |
/* Then, make the path relative again. This also checks if the path being inside of the sysfs. */ |
| 3058 |
_cleanup_free_ char *resolved = NULL; |
| 3059 |
_cleanup_close_ int fd = -EBADF; |
| 3060 |
r = device_chase(dev, buf, /* flags= */ 0, &resolved, &fd); |
| 3061 |
if (r < 0) { |
| 3062 |
log_event_error_errno(event, token, r, "Could not chase sysfs attribute \"%s\", ignoring: %m", buf); |
| 3063 |
return true; |
| 3064 |
} |
| 3065 |
|
| 3066 |
/* Apply formatting to the value. */ |
| 3067 |
if (!apply_format_value(event, token, value, sizeof(value), "attribute value")) |
| 3068 |
return true; |
| 3069 |
|
| 3070 |
if (EVENT_MODE_DESTRUCTIVE(event)) { |
| 3071 |
log_event_debug(event, token, "Writing \"%s\" to sysfs attribute \"%s\".", value, resolved); |
| 3072 |
r = sd_device_set_sysattr_value(dev, resolved, value); |
| 3073 |
if (r < 0) |
| 3074 |
log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysfs attribute \"%s\", ignoring: %m", value, resolved); |
| 3075 |
else { |
| 3076 |
event_cache_written_sysattr(event, resolved, value); |
| 3077 |
log_event_done(event, token); |
| 3078 |
} |
| 3079 |
} else { |
| 3080 |
log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysfs attribute \"%s\".", value, resolved); |
| 3081 |
|
| 3082 |
/* We assume the attribute is writable if the path points to a regular file, and cache |
| 3083 |
* the value to make it shown by OPTIONS="dump" or obtained by later ATTR match token. */ |
| 3084 |
r = fd_verify_regular(fd); |
| 3085 |
if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r)) |
| 3086 |
log_event_error_errno(event, token, r, "Failed to verify sysfs attribute \"%s\" is a regular file: %m", resolved); |
| 3087 |
else { |
| 3088 |
event_cache_written_sysattr(event, resolved, value); |
| 3089 |
|
| 3090 |
_cleanup_free_ char *copied = strdup(value); |
| 3091 |
if (!copied) |
| 3092 |
return log_oom(); |
| 3093 |
|
| 3094 |
r = device_cache_sysattr_value(dev, resolved, copied, /* error= */ 0); |
| 3095 |
if (r < 0) |
| 3096 |
log_event_warning_errno(event, token, r, "Failed to cache sysfs attribute \"%s\", ignoring: %m", resolved); |
| 3097 |
else if (r > 0) { |
| 3098 |
TAKE_PTR(resolved); |
| 3099 |
TAKE_PTR(copied); |
| 3100 |
} |
| 3101 |
} |
| 3102 |
} |
| 3103 |
return true; |
| 3104 |
} |
| 3105 |
case TK_A_SYSCTL: { |
| 3106 |
char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE]; |
| 3107 |
|
| 3108 |
if (!apply_format_attr(event, token, buf, sizeof(buf), "sysctl entry name")) |
| 3109 |
return true; |
| 3110 |
|
| 3111 |
if (!apply_format_value(event, token, value, sizeof(value), "sysctl value")) |
| 3112 |
return true; |
| 3113 |
|
| 3114 |
sysctl_normalize(buf); |
| 3115 |
|
| 3116 |
if (EVENT_MODE_DESTRUCTIVE(event)) { |
| 3117 |
log_event_debug(event, token, "Writing \"%s\" to sysctl entry \"%s\".", value, buf); |
| 3118 |
r = sysctl_write(buf, value); |
| 3119 |
if (r < 0) |
| 3120 |
log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysctl entry \"%s\", ignoring: %m", value, buf); |
| 3121 |
else { |
| 3122 |
event_cache_written_sysctl(event, buf, value); |
| 3123 |
log_event_done(event, token); |
| 3124 |
} |
| 3125 |
} else { |
| 3126 |
log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysctl entry \"%s\".", value, buf); |
| 3127 |
|
| 3128 |
_cleanup_free_ char *path = path_join("/proc/sys/", buf); |
| 3129 |
if (!path) |
| 3130 |
return log_oom(); |
| 3131 |
|
| 3132 |
r = verify_regular_at(AT_FDCWD, path, /* follow= */ true); |
| 3133 |
if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r)) |
| 3134 |
log_event_error_errno(event, token, r, "Failed to verify sysctl entry \"%s\" is a regular file: %m", buf); |
| 3135 |
else |
| 3136 |
event_cache_written_sysctl(event, buf, value); |
| 3137 |
} |
| 3138 |
return true; |
| 3139 |
} |
| 3140 |
case TK_A_RUN_BUILTIN: |
| 3141 |
case TK_A_RUN_PROGRAM: { |
| 3142 |
_cleanup_free_ char *cmd = NULL; |
| 3143 |
char buf[UDEV_LINE_SIZE]; |
| 3144 |
|
| 3145 |
if (event->run_final) |
| 3146 |
return log_event_final_set(event, token); |
| 3147 |
|
| 3148 |
if (token->op == OP_ASSIGN_FINAL) |
| 3149 |
event->run_final = true; |
| 3150 |
|
| 3151 |
if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL)) |
| 3152 |
ordered_hashmap_clear(event->run_list); |
| 3153 |
|
| 3154 |
if (!apply_format_value(event, token, buf, sizeof(buf), "command")) |
| 3155 |
return true; |
| 3156 |
|
| 3157 |
cmd = strdup(buf); |
| 3158 |
if (!cmd) |
| 3159 |
return log_oom(); |
| 3160 |
|
| 3161 |
r = ordered_hashmap_ensure_put(&event->run_list, &trivial_hash_ops_free, cmd, token->data); |
| 3162 |
if (r < 0) |
| 3163 |
return log_event_error_errno(event, token, r, "Failed to store command \"%s\": %m", cmd); |
| 3164 |
|
| 3165 |
log_event_debug(event, token, "Set command: %s", cmd); |
| 3166 |
TAKE_PTR(cmd); |
| 3167 |
return true; |
| 3168 |
} |
| 3169 |
case TK_A_OPTIONS_STATIC_NODE: |
| 3170 |
/* do nothing for events. */ |
| 3171 |
return true; |
| 3172 |
|
| 3173 |
default: |
| 3174 |
assert_not_reached(); |
| 3175 |
} |
| 3176 |
|
| 3177 |
assert_not_reached(); |
| 3178 |
} |
| 3179 |
|
| 3180 |
static int udev_rule_apply_parent_token_to_event(UdevRuleToken *head_token, UdevEvent *event) { |
| 3181 |
int r; |
| 3182 |
|
| 3183 |
assert(head_token); |
| 3184 |
assert(token_is_for_parents(head_token)); |
| 3185 |
assert(event); |
| 3186 |
|
| 3187 |
UdevRuleLine *line = head_token->rule_line; |
| 3188 |
if (event->trace) { |
| 3189 |
_cleanup_free_ char *joined = NULL; |
| 3190 |
|
| 3191 |
LIST_FOREACH(tokens, token, head_token) |
| 3192 |
if (token_is_for_parents(token)) |
| 3193 |
(void) strextend_with_separator(&joined, ", ", token->token_str); |
| 3194 |
else |
| 3195 |
break; |
| 3196 |
|
| 3197 |
log_event_line(event, line, "Checking conditions for parent devices (including self): %s", strna(joined)); |
| 3198 |
} |
| 3199 |
|
| 3200 |
event->dev_parent = ASSERT_PTR(event->dev); |
| 3201 |
|
| 3202 |
for (;;) { |
| 3203 |
LIST_FOREACH(tokens, token, head_token) { |
| 3204 |
if (!token_is_for_parents(token)) { |
| 3205 |
r = 1; /* avoid false maybe-uninitialized warning */ |
| 3206 |
break; /* All parent tokens match. */ |
| 3207 |
} |
| 3208 |
|
| 3209 |
r = udev_rule_apply_token_to_event(token, event->dev_parent, event); |
| 3210 |
if (r < 0) |
| 3211 |
return r; |
| 3212 |
if (r == 0) |
| 3213 |
break; |
| 3214 |
} |
| 3215 |
if (r > 0) { |
| 3216 |
if (event->trace) { |
| 3217 |
const char *s = NULL; |
| 3218 |
(void) sd_device_get_syspath(event->dev_parent, &s); |
| 3219 |
log_event_line(event, line, "Parent device \"%s\" passed all parent conditions.", strna(s)); |
| 3220 |
} |
| 3221 |
return true; |
| 3222 |
} |
| 3223 |
|
| 3224 |
if (sd_device_get_parent(event->dev_parent, &event->dev_parent) < 0) { |
| 3225 |
event->dev_parent = NULL; |
| 3226 |
log_event_line(event, line, "No parent device passed parent conditions."); |
| 3227 |
return false; |
| 3228 |
} |
| 3229 |
} |
| 3230 |
} |
| 3231 |
|
| 3232 |
static int udev_rule_apply_line_to_event( |
| 3233 |
UdevRuleLine *line, |
| 3234 |
UdevEvent *event, |
| 3235 |
UdevRuleLine **next_line) { |
| 3236 |
|
| 3237 |
UdevRuleLineType mask = LINE_HAS_GOTO | LINE_UPDATE_SOMETHING; |
| 3238 |
bool parents_done = false; |
| 3239 |
sd_device_action_t action; |
| 3240 |
int r; |
| 3241 |
|
| 3242 |
assert(line); |
| 3243 |
assert(event); |
| 3244 |
assert(next_line); |
| 3245 |
|
| 3246 |
r = sd_device_get_action(event->dev, &action); |
| 3247 |
if (r < 0) |
| 3248 |
return r; |
| 3249 |
|
| 3250 |
if (action != SD_DEVICE_REMOVE) { |
| 3251 |
if (sd_device_get_devnum(event->dev, NULL) >= 0) |
| 3252 |
mask |= LINE_HAS_DEVLINK; |
| 3253 |
|
| 3254 |
if (sd_device_get_ifindex(event->dev, NULL) >= 0) |
| 3255 |
mask |= LINE_HAS_NAME; |
| 3256 |
} |
| 3257 |
|
| 3258 |
if ((line->type & mask) == 0) |
| 3259 |
return 0; |
| 3260 |
|
| 3261 |
event->esc = ESCAPE_UNSET; |
| 3262 |
|
| 3263 |
DEVICE_TRACE_POINT(rules_apply_line, event->dev, line->rule_file->filename, line->line_number); |
| 3264 |
|
| 3265 |
LIST_FOREACH(tokens, token, line->tokens) { |
| 3266 |
if (token_is_for_parents(token)) { |
| 3267 |
if (parents_done) |
| 3268 |
continue; |
| 3269 |
|
| 3270 |
r = udev_rule_apply_parent_token_to_event(token, event); |
| 3271 |
if (r <= 0) |
| 3272 |
return r; |
| 3273 |
|
| 3274 |
parents_done = true; |
| 3275 |
continue; |
| 3276 |
} |
| 3277 |
|
| 3278 |
r = udev_rule_apply_token_to_event(token, event->dev, event); |
| 3279 |
if (r <= 0) |
| 3280 |
return r; |
| 3281 |
} |
| 3282 |
|
| 3283 |
if (line->goto_line) { |
| 3284 |
log_event_line(event, line, "GOTO=%s", strna(line->goto_label)); |
| 3285 |
*next_line = line->goto_line; /* update next_line only when the line has GOTO token. */ |
| 3286 |
} |
| 3287 |
|
| 3288 |
return 0; |
| 3289 |
} |
| 3290 |
|
| 3291 |
int udev_rules_apply_to_event(UdevRules *rules, UdevEvent *event) { |
| 3292 |
int r; |
| 3293 |
|
| 3294 |
assert(rules); |
| 3295 |
assert(event); |
| 3296 |
|
| 3297 |
LIST_FOREACH(rule_files, file, rules->rule_files) |
| 3298 |
LIST_FOREACH_WITH_NEXT(rule_lines, line, next_line, file->rule_lines) { |
| 3299 |
r = udev_rule_apply_line_to_event(line, event, &next_line); |
| 3300 |
if (r < 0) |
| 3301 |
return r; |
| 3302 |
} |
| 3303 |
|
| 3304 |
return 0; |
| 3305 |
} |
| 3306 |
|
| 3307 |
static int udev_rule_line_apply_static_dev_perms(UdevRuleLine *rule_line) { |
| 3308 |
_cleanup_strv_free_ char **tags = NULL; |
| 3309 |
uid_t uid = UID_INVALID; |
| 3310 |
gid_t gid = GID_INVALID; |
| 3311 |
mode_t mode = MODE_INVALID; |
| 3312 |
int r; |
| 3313 |
|
| 3314 |
assert(rule_line); |
| 3315 |
|
| 3316 |
if (!FLAGS_SET(rule_line->type, LINE_HAS_STATIC_NODE)) |
| 3317 |
return 0; |
| 3318 |
|
| 3319 |
LIST_FOREACH(tokens, token, rule_line->tokens) |
| 3320 |
if (token->type == TK_A_OWNER_ID) |
| 3321 |
uid = PTR_TO_UID(token->data); |
| 3322 |
else if (token->type == TK_A_GROUP_ID) |
| 3323 |
gid = PTR_TO_GID(token->data); |
| 3324 |
else if (token->type == TK_A_MODE_ID) |
| 3325 |
mode = PTR_TO_MODE(token->data); |
| 3326 |
else if (token->type == TK_A_TAG) { |
| 3327 |
r = strv_extend(&tags, token->value); |
| 3328 |
if (r < 0) |
| 3329 |
return log_oom(); |
| 3330 |
} else if (token->type == TK_A_OPTIONS_STATIC_NODE) { |
| 3331 |
r = static_node_apply_permissions(token->value, mode, uid, gid, tags); |
| 3332 |
if (r < 0) |
| 3333 |
return r; |
| 3334 |
} |
| 3335 |
|
| 3336 |
return 0; |
| 3337 |
} |
| 3338 |
|
| 3339 |
int udev_rules_apply_static_dev_perms(UdevRules *rules) { |
| 3340 |
int r; |
| 3341 |
|
| 3342 |
assert(rules); |
| 3343 |
|
| 3344 |
LIST_FOREACH(rule_files, file, rules->rule_files) |
| 3345 |
LIST_FOREACH(rule_lines, line, file->rule_lines) { |
| 3346 |
r = udev_rule_line_apply_static_dev_perms(line); |
| 3347 |
if (r < 0) |
| 3348 |
return r; |
| 3349 |
} |
| 3350 |
|
| 3351 |
return 0; |
| 3352 |
} |
| 3353 |
|
| 3354 |
static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = { |
| 3355 |
[RESOLVE_NAME_NEVER] = "never", |
| 3356 |
[RESOLVE_NAME_LATE] = "late", |
| 3357 |
[RESOLVE_NAME_EARLY] = "early", |
| 3358 |
}; |
| 3359 |
|
| 3360 |
DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming); |