]>
Commit | Line | Data |
---|---|---|
f13467ec | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
d46f37fd | 2 | |
d46f37fd | 3 | #include <ctype.h> |
07630cea LP |
4 | #include <errno.h> |
5 | #include <fcntl.h> | |
959e8b5d | 6 | #include <net/if.h> |
07630cea | 7 | #include <stddef.h> |
07630cea | 8 | #include <stdlib.h> |
010f917c | 9 | #include <sys/wait.h> |
07630cea | 10 | #include <unistd.h> |
d46f37fd | 11 | |
e81c3a4c YW |
12 | #include "sd-event.h" |
13 | ||
b5efdb8a | 14 | #include "alloc-util.h" |
947ce772 | 15 | #include "device-private.h" |
e9343893 | 16 | #include "device-util.h" |
3ffd4af2 | 17 | #include "fd-util.h" |
25de7aa7 | 18 | #include "fs-util.h" |
f97b34a6 | 19 | #include "format-util.h" |
07630cea | 20 | #include "netlink-util.h" |
ce4f94b8 | 21 | #include "parse-util.h" |
feaa6db7 | 22 | #include "path-util.h" |
8128f229 | 23 | #include "process-util.h" |
595225af | 24 | #include "rlimit-util.h" |
24882e06 | 25 | #include "signal-util.h" |
4cade7a1 | 26 | #include "stdio-util.h" |
07630cea | 27 | #include "string-util.h" |
5ea78a39 YW |
28 | #include "strv.h" |
29 | #include "strxcpyx.h" | |
07a26e42 | 30 | #include "udev-builtin.h" |
25de7aa7 | 31 | #include "udev-event.h" |
a2554ace | 32 | #include "udev-node.h" |
d4d690fa | 33 | #include "udev-util.h" |
70068602 | 34 | #include "udev-watch.h" |
25de7aa7 | 35 | #include "user-util.h" |
8128f229 TG |
36 | |
37 | typedef struct Spawn { | |
88d566aa | 38 | sd_device *device; |
8128f229 TG |
39 | const char *cmd; |
40 | pid_t pid; | |
e81c3a4c YW |
41 | usec_t timeout_warn_usec; |
42 | usec_t timeout_usec; | |
e2099267 | 43 | int timeout_signal; |
e81c3a4c | 44 | usec_t event_birth_usec; |
53318514 | 45 | bool accept_failure; |
e81c3a4c YW |
46 | int fd_stdout; |
47 | int fd_stderr; | |
48 | char *result; | |
49 | size_t result_size; | |
50 | size_t result_len; | |
8128f229 | 51 | } Spawn; |
d46f37fd | 52 | |
1a0bd015 | 53 | UdevEvent *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rtnl, int log_level) { |
2e088715 | 54 | UdevEvent *event; |
912541b0 | 55 | |
89665d09 YW |
56 | assert(dev); |
57 | ||
2e088715 | 58 | event = new(UdevEvent, 1); |
89665d09 | 59 | if (!event) |
912541b0 | 60 | return NULL; |
89665d09 | 61 | |
2e088715 | 62 | *event = (UdevEvent) { |
cf28ad46 | 63 | .dev = sd_device_ref(dev), |
89665d09 | 64 | .birth_usec = now(CLOCK_MONOTONIC), |
6b92f429 | 65 | .exec_delay_usec = exec_delay_usec, |
e0bb2ff9 | 66 | .rtnl = sd_netlink_ref(rtnl), |
25de7aa7 YW |
67 | .uid = UID_INVALID, |
68 | .gid = GID_INVALID, | |
69 | .mode = MODE_INVALID, | |
1a0bd015 YW |
70 | .log_level_was_debug = log_level == LOG_DEBUG, |
71 | .default_log_level = log_level, | |
89665d09 YW |
72 | }; |
73 | ||
912541b0 | 74 | return event; |
aa8734ff KS |
75 | } |
76 | ||
2e088715 | 77 | UdevEvent *udev_event_free(UdevEvent *event) { |
c1118ceb YW |
78 | if (!event) |
79 | return NULL; | |
80 | ||
cf28ad46 | 81 | sd_device_unref(event->dev); |
480ecb7d | 82 | sd_device_unref(event->dev_db_clone); |
1c4baffc | 83 | sd_netlink_unref(event->rtnl); |
39a15c8a YW |
84 | ordered_hashmap_free_free_key(event->run_list); |
85 | ordered_hashmap_free_free_free(event->seclabel_list); | |
912541b0 KS |
86 | free(event->program_result); |
87 | free(event->name); | |
c1118ceb YW |
88 | |
89 | return mfree(event); | |
aa8734ff KS |
90 | } |
91 | ||
7e9c23dd YW |
92 | typedef enum { |
93 | FORMAT_SUBST_DEVNODE, | |
94 | FORMAT_SUBST_ATTR, | |
95 | FORMAT_SUBST_ENV, | |
96 | FORMAT_SUBST_KERNEL, | |
97 | FORMAT_SUBST_KERNEL_NUMBER, | |
98 | FORMAT_SUBST_DRIVER, | |
99 | FORMAT_SUBST_DEVPATH, | |
100 | FORMAT_SUBST_ID, | |
101 | FORMAT_SUBST_MAJOR, | |
102 | FORMAT_SUBST_MINOR, | |
103 | FORMAT_SUBST_RESULT, | |
104 | FORMAT_SUBST_PARENT, | |
105 | FORMAT_SUBST_NAME, | |
106 | FORMAT_SUBST_LINKS, | |
107 | FORMAT_SUBST_ROOT, | |
108 | FORMAT_SUBST_SYS, | |
109 | _FORMAT_SUBST_TYPE_MAX, | |
2d93c20e | 110 | _FORMAT_SUBST_TYPE_INVALID = -EINVAL, |
7e9c23dd | 111 | } FormatSubstitutionType; |
0d53705b | 112 | |
9204d802 YW |
113 | struct subst_map_entry { |
114 | const char *name; | |
115 | const char fmt; | |
7e9c23dd | 116 | FormatSubstitutionType type; |
9204d802 YW |
117 | }; |
118 | ||
119 | static const struct subst_map_entry map[] = { | |
7e9c23dd | 120 | { .name = "devnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE }, |
3c209d60 | 121 | { .name = "tempnode", .fmt = 'N', .type = FORMAT_SUBST_DEVNODE }, /* deprecated */ |
7e9c23dd | 122 | { .name = "attr", .fmt = 's', .type = FORMAT_SUBST_ATTR }, |
3c209d60 | 123 | { .name = "sysfs", .fmt = 's', .type = FORMAT_SUBST_ATTR }, /* deprecated */ |
7e9c23dd YW |
124 | { .name = "env", .fmt = 'E', .type = FORMAT_SUBST_ENV }, |
125 | { .name = "kernel", .fmt = 'k', .type = FORMAT_SUBST_KERNEL }, | |
126 | { .name = "number", .fmt = 'n', .type = FORMAT_SUBST_KERNEL_NUMBER }, | |
127 | { .name = "driver", .fmt = 'd', .type = FORMAT_SUBST_DRIVER }, | |
128 | { .name = "devpath", .fmt = 'p', .type = FORMAT_SUBST_DEVPATH }, | |
129 | { .name = "id", .fmt = 'b', .type = FORMAT_SUBST_ID }, | |
130 | { .name = "major", .fmt = 'M', .type = FORMAT_SUBST_MAJOR }, | |
131 | { .name = "minor", .fmt = 'm', .type = FORMAT_SUBST_MINOR }, | |
132 | { .name = "result", .fmt = 'c', .type = FORMAT_SUBST_RESULT }, | |
133 | { .name = "parent", .fmt = 'P', .type = FORMAT_SUBST_PARENT }, | |
134 | { .name = "name", .fmt = 'D', .type = FORMAT_SUBST_NAME }, | |
135 | { .name = "links", .fmt = 'L', .type = FORMAT_SUBST_LINKS }, | |
136 | { .name = "root", .fmt = 'r', .type = FORMAT_SUBST_ROOT }, | |
137 | { .name = "sys", .fmt = 'S', .type = FORMAT_SUBST_SYS }, | |
9204d802 YW |
138 | }; |
139 | ||
13cd553f YW |
140 | static const char *format_type_to_string(FormatSubstitutionType t) { |
141 | for (size_t i = 0; i < ELEMENTSOF(map); i++) | |
142 | if (map[i].type == t) | |
143 | return map[i].name; | |
144 | return NULL; | |
145 | } | |
146 | ||
147 | static char format_type_to_char(FormatSubstitutionType t) { | |
148 | for (size_t i = 0; i < ELEMENTSOF(map); i++) | |
149 | if (map[i].type == t) | |
150 | return map[i].fmt; | |
151 | return '\0'; | |
152 | } | |
153 | ||
c8eaaf69 | 154 | static int get_subst_type(const char **str, bool strict, FormatSubstitutionType *ret_type, char ret_attr[static UDEV_PATH_SIZE]) { |
13cd553f YW |
155 | const char *p = *str, *q = NULL; |
156 | size_t i; | |
157 | ||
158 | assert(str); | |
159 | assert(*str); | |
160 | assert(ret_type); | |
2caa38e9 | 161 | assert(ret_attr); |
13cd553f | 162 | |
1824300a | 163 | if (*p == '$') { |
13cd553f | 164 | p++; |
1824300a | 165 | if (*p == '$') { |
13cd553f YW |
166 | *str = p; |
167 | return 0; | |
168 | } | |
169 | for (i = 0; i < ELEMENTSOF(map); i++) | |
170 | if ((q = startswith(p, map[i].name))) | |
171 | break; | |
1824300a | 172 | } else if (*p == '%') { |
13cd553f | 173 | p++; |
1824300a | 174 | if (*p == '%') { |
13cd553f YW |
175 | *str = p; |
176 | return 0; | |
177 | } | |
178 | ||
179 | for (i = 0; i < ELEMENTSOF(map); i++) | |
1824300a | 180 | if (*p == map[i].fmt) { |
13cd553f YW |
181 | q = p + 1; |
182 | break; | |
183 | } | |
d7aee41d | 184 | } else |
13cd553f | 185 | return 0; |
d7aee41d YW |
186 | if (!q) |
187 | /* When 'strict' flag is set, then '$' and '%' must be escaped. */ | |
188 | return strict ? -EINVAL : 0; | |
13cd553f | 189 | |
1824300a | 190 | if (*q == '{') { |
13cd553f YW |
191 | const char *start, *end; |
192 | size_t len; | |
193 | ||
194 | start = q + 1; | |
195 | end = strchr(start, '}'); | |
196 | if (!end) | |
197 | return -EINVAL; | |
198 | ||
199 | len = end - start; | |
c8eaaf69 | 200 | if (len == 0 || len >= UDEV_PATH_SIZE) |
13cd553f YW |
201 | return -EINVAL; |
202 | ||
c8eaaf69 | 203 | strnscpy(ret_attr, UDEV_PATH_SIZE, start, len); |
13cd553f YW |
204 | q = end + 1; |
205 | } else | |
1824300a | 206 | *ret_attr = '\0'; |
13cd553f YW |
207 | |
208 | *str = q; | |
209 | *ret_type = map[i].type; | |
210 | return 1; | |
211 | } | |
212 | ||
ce4f94b8 YW |
213 | static int safe_atou_optional_plus(const char *s, unsigned *ret) { |
214 | const char *p; | |
215 | int r; | |
216 | ||
217 | assert(s); | |
218 | assert(ret); | |
219 | ||
220 | /* Returns 1 if plus, 0 if no plus, negative on error */ | |
221 | ||
222 | p = endswith(s, "+"); | |
223 | if (p) | |
224 | s = strndupa(s, p - s); | |
225 | ||
226 | r = safe_atou(s, ret); | |
227 | if (r < 0) | |
228 | return r; | |
229 | ||
230 | return !!p; | |
231 | } | |
232 | ||
ef315ada YW |
233 | static ssize_t udev_event_subst_format( |
234 | UdevEvent *event, | |
235 | FormatSubstitutionType type, | |
236 | const char *attr, | |
237 | char *dest, | |
238 | size_t l) { | |
cf28ad46 | 239 | sd_device *parent, *dev = event->dev; |
4cade7a1 | 240 | const char *val = NULL; |
be452683 | 241 | char *s = dest; |
4cade7a1 | 242 | int r; |
0d53705b | 243 | |
ef315ada | 244 | switch (type) { |
7e9c23dd | 245 | case FORMAT_SUBST_DEVPATH: |
4cade7a1 YW |
246 | r = sd_device_get_devpath(dev, &val); |
247 | if (r < 0) | |
248 | return r; | |
249 | l = strpcpy(&s, l, val); | |
0d53705b | 250 | break; |
7e9c23dd | 251 | case FORMAT_SUBST_KERNEL: |
4cade7a1 YW |
252 | r = sd_device_get_sysname(dev, &val); |
253 | if (r < 0) | |
254 | return r; | |
255 | l = strpcpy(&s, l, val); | |
0d53705b | 256 | break; |
7e9c23dd | 257 | case FORMAT_SUBST_KERNEL_NUMBER: |
4cade7a1 | 258 | r = sd_device_get_sysnum(dev, &val); |
380d1901 YW |
259 | if (r == -ENOENT) |
260 | goto null_terminate; | |
4cade7a1 | 261 | if (r < 0) |
380d1901 | 262 | return r; |
4cade7a1 | 263 | l = strpcpy(&s, l, val); |
0d53705b | 264 | break; |
7e9c23dd | 265 | case FORMAT_SUBST_ID: |
4cade7a1 | 266 | if (!event->dev_parent) |
380d1901 | 267 | goto null_terminate; |
f3d241fe | 268 | r = sd_device_get_sysname(event->dev_parent, &val); |
4cade7a1 YW |
269 | if (r < 0) |
270 | return r; | |
271 | l = strpcpy(&s, l, val); | |
0d53705b | 272 | break; |
7e9c23dd | 273 | case FORMAT_SUBST_DRIVER: |
4cade7a1 | 274 | if (!event->dev_parent) |
380d1901 | 275 | goto null_terminate; |
f3d241fe | 276 | r = sd_device_get_driver(event->dev_parent, &val); |
380d1901 YW |
277 | if (r == -ENOENT) |
278 | goto null_terminate; | |
4cade7a1 | 279 | if (r < 0) |
380d1901 | 280 | return r; |
4cade7a1 | 281 | l = strpcpy(&s, l, val); |
0d53705b | 282 | break; |
7e9c23dd YW |
283 | case FORMAT_SUBST_MAJOR: |
284 | case FORMAT_SUBST_MINOR: { | |
1b65f1eb | 285 | dev_t devnum; |
0d53705b | 286 | |
4cade7a1 YW |
287 | r = sd_device_get_devnum(dev, &devnum); |
288 | if (r < 0 && r != -ENOENT) | |
289 | return r; | |
1b65f1eb | 290 | l = strpcpyf(&s, l, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum)); |
0d53705b DS |
291 | break; |
292 | } | |
7e9c23dd | 293 | case FORMAT_SUBST_RESULT: { |
ce4f94b8 YW |
294 | unsigned index = 0; /* 0 means whole string */ |
295 | bool has_plus; | |
0d53705b | 296 | |
4cade7a1 | 297 | if (!event->program_result) |
380d1901 | 298 | goto null_terminate; |
4cade7a1 | 299 | |
ce4f94b8 YW |
300 | if (!isempty(attr)) { |
301 | r = safe_atou_optional_plus(attr, &index); | |
302 | if (r < 0) | |
303 | return r; | |
304 | ||
305 | has_plus = r; | |
306 | } | |
307 | ||
308 | if (index == 0) | |
309 | l = strpcpy(&s, l, event->program_result); | |
310 | else { | |
311 | const char *start, *p; | |
312 | unsigned i; | |
313 | ||
314 | p = skip_leading_chars(event->program_result, NULL); | |
315 | ||
316 | for (i = 1; i < index; i++) { | |
1824300a | 317 | while (*p && !strchr(WHITESPACE, *p)) |
ce4f94b8 YW |
318 | p++; |
319 | p = skip_leading_chars(p, NULL); | |
320 | if (*p == '\0') | |
0d53705b DS |
321 | break; |
322 | } | |
ce4f94b8 YW |
323 | if (i != index) { |
324 | log_device_debug(dev, "requested part of result string not found"); | |
325 | goto null_terminate; | |
0d53705b | 326 | } |
ce4f94b8 YW |
327 | |
328 | start = p; | |
329 | /* %c{2+} copies the whole string from the second part on */ | |
330 | if (has_plus) | |
331 | l = strpcpy(&s, l, start); | |
332 | else { | |
1824300a | 333 | while (*p && !strchr(WHITESPACE, *p)) |
ce4f94b8 YW |
334 | p++; |
335 | l = strnpcpy(&s, l, start, p - start); | |
0d53705b | 336 | } |
ce4f94b8 | 337 | } |
0d53705b DS |
338 | break; |
339 | } | |
7e9c23dd | 340 | case FORMAT_SUBST_ATTR: { |
c8eaaf69 | 341 | char vbuf[UDEV_NAME_SIZE]; |
0d53705b DS |
342 | int count; |
343 | ||
13cd553f | 344 | if (isempty(attr)) |
4cade7a1 | 345 | return -EINVAL; |
0d53705b DS |
346 | |
347 | /* try to read the value specified by "[dmi/id]product_name" */ | |
1223227f | 348 | if (udev_resolve_subsys_kernel(attr, vbuf, sizeof(vbuf), true) == 0) |
4cade7a1 | 349 | val = vbuf; |
0d53705b DS |
350 | |
351 | /* try to read the attribute the device */ | |
4cade7a1 YW |
352 | if (!val) |
353 | (void) sd_device_get_sysattr_value(dev, attr, &val); | |
0d53705b DS |
354 | |
355 | /* try to read the attribute of the parent device, other matches have selected */ | |
f3d241fe YW |
356 | if (!val && event->dev_parent && event->dev_parent != dev) |
357 | (void) sd_device_get_sysattr_value(event->dev_parent, attr, &val); | |
0d53705b | 358 | |
4cade7a1 | 359 | if (!val) |
380d1901 | 360 | goto null_terminate; |
0d53705b DS |
361 | |
362 | /* strip trailing whitespace, and replace unwanted characters */ | |
4cade7a1 YW |
363 | if (val != vbuf) |
364 | strscpy(vbuf, sizeof(vbuf), val); | |
72801533 | 365 | delete_trailing_chars(vbuf, NULL); |
393fcaf7 | 366 | count = udev_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT); |
0d53705b | 367 | if (count > 0) |
4cade7a1 | 368 | log_device_debug(dev, "%i character(s) replaced", count); |
0d53705b DS |
369 | l = strpcpy(&s, l, vbuf); |
370 | break; | |
371 | } | |
7e9c23dd | 372 | case FORMAT_SUBST_PARENT: |
4cade7a1 | 373 | r = sd_device_get_parent(dev, &parent); |
bc568a7a | 374 | if (r == -ENOENT) |
380d1901 | 375 | goto null_terminate; |
4cade7a1 | 376 | if (r < 0) |
380d1901 | 377 | return r; |
4cade7a1 | 378 | r = sd_device_get_devname(parent, &val); |
380d1901 YW |
379 | if (r == -ENOENT) |
380 | goto null_terminate; | |
4cade7a1 | 381 | if (r < 0) |
380d1901 | 382 | return r; |
4cade7a1 | 383 | l = strpcpy(&s, l, val + STRLEN("/dev/")); |
0d53705b | 384 | break; |
7e9c23dd | 385 | case FORMAT_SUBST_DEVNODE: |
4cade7a1 | 386 | r = sd_device_get_devname(dev, &val); |
380d1901 YW |
387 | if (r == -ENOENT) |
388 | goto null_terminate; | |
4cade7a1 | 389 | if (r < 0) |
380d1901 | 390 | return r; |
4cade7a1 | 391 | l = strpcpy(&s, l, val); |
0d53705b | 392 | break; |
7e9c23dd | 393 | case FORMAT_SUBST_NAME: |
4cade7a1 | 394 | if (event->name) |
0d53705b | 395 | l = strpcpy(&s, l, event->name); |
4cade7a1 YW |
396 | else if (sd_device_get_devname(dev, &val) >= 0) |
397 | l = strpcpy(&s, l, val + STRLEN("/dev/")); | |
398 | else { | |
399 | r = sd_device_get_sysname(dev, &val); | |
400 | if (r < 0) | |
401 | return r; | |
402 | l = strpcpy(&s, l, val); | |
403 | } | |
0d53705b | 404 | break; |
7e9c23dd | 405 | case FORMAT_SUBST_LINKS: |
4cade7a1 YW |
406 | FOREACH_DEVICE_DEVLINK(dev, val) |
407 | if (s == dest) | |
408 | l = strpcpy(&s, l, val + STRLEN("/dev/")); | |
409 | else | |
410 | l = strpcpyl(&s, l, " ", val + STRLEN("/dev/"), NULL); | |
380d1901 YW |
411 | if (s == dest) |
412 | goto null_terminate; | |
0d53705b | 413 | break; |
7e9c23dd | 414 | case FORMAT_SUBST_ROOT: |
0d53705b DS |
415 | l = strpcpy(&s, l, "/dev"); |
416 | break; | |
7e9c23dd | 417 | case FORMAT_SUBST_SYS: |
0d53705b DS |
418 | l = strpcpy(&s, l, "/sys"); |
419 | break; | |
7e9c23dd | 420 | case FORMAT_SUBST_ENV: |
13cd553f YW |
421 | if (isempty(attr)) |
422 | return -EINVAL; | |
4cade7a1 | 423 | r = sd_device_get_property_value(dev, attr, &val); |
380d1901 YW |
424 | if (r == -ENOENT) |
425 | goto null_terminate; | |
4cade7a1 | 426 | if (r < 0) |
380d1901 | 427 | return r; |
4cade7a1 YW |
428 | l = strpcpy(&s, l, val); |
429 | break; | |
0d53705b | 430 | default: |
9204d802 | 431 | assert_not_reached("Unknown format substitution type"); |
0d53705b DS |
432 | } |
433 | ||
be452683 | 434 | return s - dest; |
380d1901 YW |
435 | |
436 | null_terminate: | |
437 | *s = '\0'; | |
438 | return 0; | |
0d53705b DS |
439 | } |
440 | ||
5eb6ef8b YW |
441 | size_t udev_event_apply_format(UdevEvent *event, |
442 | const char *src, char *dest, size_t size, | |
443 | bool replace_whitespace) { | |
13cd553f YW |
444 | const char *s = src; |
445 | int r; | |
912541b0 | 446 | |
a368732b YW |
447 | assert(event); |
448 | assert(event->dev); | |
449 | assert(src); | |
450 | assert(dest); | |
451 | assert(size > 0); | |
3b64e4d4 | 452 | |
1824300a | 453 | while (*s) { |
13cd553f | 454 | FormatSubstitutionType type; |
c8eaaf69 | 455 | char attr[UDEV_PATH_SIZE]; |
4cade7a1 | 456 | ssize_t subst_len; |
912541b0 | 457 | |
d7aee41d | 458 | r = get_subst_type(&s, false, &type, attr); |
5eb6ef8b YW |
459 | if (r < 0) { |
460 | log_device_warning_errno(event->dev, r, "Invalid format string, ignoring: %s", src); | |
461 | break; | |
462 | } else if (r == 0) { | |
13cd553f YW |
463 | if (size < 2) /* need space for this char and the terminating NUL */ |
464 | break; | |
465 | *dest++ = *s++; | |
466 | size--; | |
4cade7a1 YW |
467 | continue; |
468 | } | |
e20a9171 | 469 | |
13cd553f | 470 | subst_len = udev_event_subst_format(event, type, attr, dest, size); |
5eb6ef8b YW |
471 | if (subst_len < 0) { |
472 | log_device_warning_errno(event->dev, subst_len, | |
473 | "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m", | |
474 | format_type_to_string(type), format_type_to_char(type)); | |
475 | break; | |
476 | } | |
13cd553f | 477 | |
7e9c23dd | 478 | /* FORMAT_SUBST_RESULT handles spaces itself */ |
13cd553f | 479 | if (replace_whitespace && type != FORMAT_SUBST_RESULT) |
5953d8b9 | 480 | /* udev_replace_whitespace can replace in-place, |
13cd553f | 481 | * and does nothing if subst_len == 0 */ |
5953d8b9 | 482 | subst_len = udev_replace_whitespace(dest, dest, subst_len); |
e20a9171 | 483 | |
13cd553f YW |
484 | dest += subst_len; |
485 | size -= subst_len; | |
912541b0 | 486 | } |
065db052 | 487 | |
13cd553f | 488 | assert(size >= 1); |
1824300a | 489 | *dest = '\0'; |
13cd553f | 490 | return size; |
f1128767 KS |
491 | } |
492 | ||
f85cc54c | 493 | int udev_check_format(const char *value, size_t *offset, const char **hint) { |
d7aee41d | 494 | FormatSubstitutionType type; |
f85cc54c | 495 | const char *s = value; |
c8eaaf69 | 496 | char attr[UDEV_PATH_SIZE]; |
d7aee41d YW |
497 | int r; |
498 | ||
1824300a | 499 | while (*s) { |
d7aee41d | 500 | r = get_subst_type(&s, true, &type, attr); |
f85cc54c ZJS |
501 | if (r < 0) { |
502 | if (offset) | |
503 | *offset = s - value; | |
504 | if (hint) | |
505 | *hint = "invalid substitution type"; | |
d7aee41d | 506 | return r; |
f85cc54c | 507 | } else if (r == 0) { |
d7aee41d YW |
508 | s++; |
509 | continue; | |
510 | } | |
511 | ||
f85cc54c ZJS |
512 | if (IN_SET(type, FORMAT_SUBST_ATTR, FORMAT_SUBST_ENV) && isempty(attr)) { |
513 | if (offset) | |
514 | *offset = s - value; | |
515 | if (hint) | |
516 | *hint = "attribute value missing"; | |
d7aee41d | 517 | return -EINVAL; |
f85cc54c | 518 | } |
d7aee41d YW |
519 | |
520 | if (type == FORMAT_SUBST_RESULT && !isempty(attr)) { | |
521 | unsigned i; | |
522 | ||
523 | r = safe_atou_optional_plus(attr, &i); | |
f85cc54c ZJS |
524 | if (r < 0) { |
525 | if (offset) | |
526 | *offset = s - value; | |
527 | if (hint) | |
528 | *hint = "attribute value not a valid number"; | |
d7aee41d | 529 | return r; |
f85cc54c | 530 | } |
d7aee41d YW |
531 | } |
532 | } | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
e81c3a4c YW |
537 | static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
538 | Spawn *spawn = userdata; | |
539 | char buf[4096], *p; | |
540 | size_t size; | |
541 | ssize_t l; | |
9f537ae3 | 542 | int r; |
912541b0 | 543 | |
e81c3a4c YW |
544 | assert(spawn); |
545 | assert(fd == spawn->fd_stdout || fd == spawn->fd_stderr); | |
546 | assert(!spawn->result || spawn->result_len < spawn->result_size); | |
547 | ||
548 | if (fd == spawn->fd_stdout && spawn->result) { | |
549 | p = spawn->result + spawn->result_len; | |
550 | size = spawn->result_size - spawn->result_len; | |
551 | } else { | |
552 | p = buf; | |
553 | size = sizeof(buf); | |
912541b0 KS |
554 | } |
555 | ||
e81c3a4c YW |
556 | l = read(fd, p, size - 1); |
557 | if (l < 0) { | |
9f537ae3 PD |
558 | if (errno == EAGAIN) |
559 | goto reenable; | |
560 | ||
561 | log_device_error_errno(spawn->device, errno, | |
562 | "Failed to read stdout of '%s': %m", spawn->cmd); | |
912541b0 | 563 | |
e81c3a4c | 564 | return 0; |
912541b0 KS |
565 | } |
566 | ||
e81c3a4c YW |
567 | p[l] = '\0'; |
568 | if (fd == spawn->fd_stdout && spawn->result) | |
569 | spawn->result_len += l; | |
912541b0 | 570 | |
e81c3a4c YW |
571 | /* Log output only if we watch stderr. */ |
572 | if (l > 0 && spawn->fd_stderr >= 0) { | |
573 | _cleanup_strv_free_ char **v = NULL; | |
574 | char **q; | |
912541b0 | 575 | |
daceb818 YW |
576 | r = strv_split_newlines_full(&v, p, EXTRACT_RETAIN_ESCAPE); |
577 | if (r < 0) | |
578 | log_device_debug(spawn->device, | |
579 | "Failed to split output from '%s'(%s), ignoring: %m", | |
580 | spawn->cmd, fd == spawn->fd_stdout ? "out" : "err"); | |
912541b0 | 581 | |
e81c3a4c | 582 | STRV_FOREACH(q, v) |
88d566aa YW |
583 | log_device_debug(spawn->device, "'%s'(%s) '%s'", spawn->cmd, |
584 | fd == spawn->fd_stdout ? "out" : "err", *q); | |
912541b0 KS |
585 | } |
586 | ||
9f537ae3 PD |
587 | if (l == 0) |
588 | return 0; | |
589 | ||
9f537ae3 | 590 | reenable: |
2be0218a YW |
591 | /* Re-enable the event source if we did not encounter EOF */ |
592 | ||
9f537ae3 PD |
593 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); |
594 | if (r < 0) | |
595 | log_device_error_errno(spawn->device, r, | |
596 | "Failed to reactivate IO source of '%s'", spawn->cmd); | |
e81c3a4c | 597 | return 0; |
2181d30a KS |
598 | } |
599 | ||
8128f229 TG |
600 | static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
601 | Spawn *spawn = userdata; | |
4375dab5 | 602 | char timeout[FORMAT_TIMESPAN_MAX]; |
912541b0 | 603 | |
8128f229 | 604 | assert(spawn); |
912541b0 | 605 | |
e2099267 | 606 | kill_and_sigcont(spawn->pid, spawn->timeout_signal); |
912541b0 | 607 | |
88d566aa YW |
608 | log_device_error(spawn->device, "Spawned process '%s' ["PID_FMT"] timed out after %s, killing", |
609 | spawn->cmd, spawn->pid, | |
610 | format_timespan(timeout, sizeof(timeout), spawn->timeout_usec, USEC_PER_SEC)); | |
912541b0 | 611 | |
8128f229 TG |
612 | return 1; |
613 | } | |
67117413 | 614 | |
8128f229 TG |
615 | static int on_spawn_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { |
616 | Spawn *spawn = userdata; | |
4375dab5 | 617 | char timeout[FORMAT_TIMESPAN_MAX]; |
912541b0 | 618 | |
8128f229 | 619 | assert(spawn); |
67117413 | 620 | |
88d566aa YW |
621 | log_device_warning(spawn->device, "Spawned process '%s' ["PID_FMT"] is taking longer than %s to complete", |
622 | spawn->cmd, spawn->pid, | |
623 | format_timespan(timeout, sizeof(timeout), spawn->timeout_warn_usec, USEC_PER_SEC)); | |
8128f229 TG |
624 | |
625 | return 1; | |
626 | } | |
627 | ||
628 | static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) { | |
629 | Spawn *spawn = userdata; | |
a7521142 | 630 | int ret = -EIO; |
8128f229 TG |
631 | |
632 | assert(spawn); | |
633 | ||
634 | switch (si->si_code) { | |
635 | case CLD_EXITED: | |
a7521142 | 636 | if (si->si_status == 0) |
88d566aa | 637 | log_device_debug(spawn->device, "Process '%s' succeeded.", spawn->cmd); |
a7521142 | 638 | else |
ab54f12b | 639 | log_device_full(spawn->device, spawn->accept_failure ? LOG_DEBUG : LOG_WARNING, |
88d566aa | 640 | "Process '%s' failed with exit code %i.", spawn->cmd, si->si_status); |
a7521142 | 641 | ret = si->si_status; |
8128f229 TG |
642 | break; |
643 | case CLD_KILLED: | |
644 | case CLD_DUMPED: | |
88d566aa | 645 | log_device_error(spawn->device, "Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status)); |
8128f229 TG |
646 | break; |
647 | default: | |
88d566aa | 648 | log_device_error(spawn->device, "Process '%s' failed due to unknown reason.", spawn->cmd); |
8128f229 | 649 | } |
912541b0 | 650 | |
a7521142 | 651 | sd_event_exit(sd_event_source_get_event(s), ret); |
8128f229 TG |
652 | return 1; |
653 | } | |
654 | ||
e81c3a4c | 655 | static int spawn_wait(Spawn *spawn) { |
4afd3348 | 656 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
9f537ae3 PD |
657 | _cleanup_(sd_event_source_unrefp) sd_event_source *sigchld_source = NULL; |
658 | _cleanup_(sd_event_source_unrefp) sd_event_source *stdout_source = NULL; | |
659 | _cleanup_(sd_event_source_unrefp) sd_event_source *stderr_source = NULL; | |
a7521142 | 660 | int r; |
8128f229 | 661 | |
e81c3a4c YW |
662 | assert(spawn); |
663 | ||
8128f229 TG |
664 | r = sd_event_new(&e); |
665 | if (r < 0) | |
90d260d8 | 666 | return log_device_debug_errno(spawn->device, r, "Failed to allocate sd-event object: %m"); |
8128f229 | 667 | |
e81c3a4c | 668 | if (spawn->timeout_usec > 0) { |
8128f229 TG |
669 | usec_t usec, age_usec; |
670 | ||
3285baa8 | 671 | usec = now(CLOCK_MONOTONIC); |
e81c3a4c YW |
672 | age_usec = usec - spawn->event_birth_usec; |
673 | if (age_usec < spawn->timeout_usec) { | |
674 | if (spawn->timeout_warn_usec > 0 && | |
675 | spawn->timeout_warn_usec < spawn->timeout_usec && | |
676 | spawn->timeout_warn_usec > age_usec) { | |
677 | spawn->timeout_warn_usec -= age_usec; | |
8128f229 | 678 | |
3285baa8 | 679 | r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
e81c3a4c YW |
680 | usec + spawn->timeout_warn_usec, USEC_PER_SEC, |
681 | on_spawn_timeout_warning, spawn); | |
8128f229 | 682 | if (r < 0) |
90d260d8 | 683 | return log_device_debug_errno(spawn->device, r, "Failed to create timeout warning event source: %m"); |
912541b0 | 684 | } |
8128f229 | 685 | |
e81c3a4c | 686 | spawn->timeout_usec -= age_usec; |
8128f229 | 687 | |
3285baa8 | 688 | r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
e81c3a4c | 689 | usec + spawn->timeout_usec, USEC_PER_SEC, on_spawn_timeout, spawn); |
8128f229 | 690 | if (r < 0) |
90d260d8 | 691 | return log_device_debug_errno(spawn->device, r, "Failed to create timeout event source: %m"); |
912541b0 KS |
692 | } |
693 | } | |
8128f229 | 694 | |
adeb26c1 | 695 | if (spawn->fd_stdout >= 0) { |
9f537ae3 PD |
696 | r = sd_event_add_io(e, &stdout_source, spawn->fd_stdout, EPOLLIN, on_spawn_io, spawn); |
697 | if (r < 0) | |
90d260d8 | 698 | return log_device_debug_errno(spawn->device, r, "Failed to create stdio event source: %m"); |
9f537ae3 | 699 | r = sd_event_source_set_enabled(stdout_source, SD_EVENT_ONESHOT); |
adeb26c1 | 700 | if (r < 0) |
90d260d8 | 701 | return log_device_debug_errno(spawn->device, r, "Failed to enable stdio event source: %m"); |
adeb26c1 | 702 | } |
e81c3a4c | 703 | |
adeb26c1 | 704 | if (spawn->fd_stderr >= 0) { |
9f537ae3 PD |
705 | r = sd_event_add_io(e, &stderr_source, spawn->fd_stderr, EPOLLIN, on_spawn_io, spawn); |
706 | if (r < 0) | |
90d260d8 | 707 | return log_device_debug_errno(spawn->device, r, "Failed to create stderr event source: %m"); |
9f537ae3 | 708 | r = sd_event_source_set_enabled(stderr_source, SD_EVENT_ONESHOT); |
adeb26c1 | 709 | if (r < 0) |
90d260d8 | 710 | return log_device_debug_errno(spawn->device, r, "Failed to enable stderr event source: %m"); |
adeb26c1 | 711 | } |
e81c3a4c | 712 | |
9f537ae3 | 713 | r = sd_event_add_child(e, &sigchld_source, spawn->pid, WEXITED, on_spawn_sigchld, spawn); |
8128f229 | 714 | if (r < 0) |
90d260d8 | 715 | return log_device_debug_errno(spawn->device, r, "Failed to create sigchild event source: %m"); |
9f537ae3 PD |
716 | /* SIGCHLD should be processed after IO is complete */ |
717 | r = sd_event_source_set_priority(sigchld_source, SD_EVENT_PRIORITY_NORMAL + 1); | |
718 | if (r < 0) | |
90d260d8 | 719 | return log_device_debug_errno(spawn->device, r, "Failed to set priority to sigchild event source: %m"); |
9f537ae3 | 720 | |
a7521142 | 721 | return sd_event_loop(e); |
2181d30a KS |
722 | } |
723 | ||
2e088715 | 724 | int udev_event_spawn(UdevEvent *event, |
dd5eddd2 | 725 | usec_t timeout_usec, |
e2099267 | 726 | int timeout_signal, |
53318514 | 727 | bool accept_failure, |
bbf35206 | 728 | const char *cmd, |
dd5eddd2 | 729 | char *result, size_t ressize) { |
feaa6db7 YW |
730 | _cleanup_close_pair_ int outpipe[2] = {-1, -1}, errpipe[2] = {-1, -1}; |
731 | _cleanup_strv_free_ char **argv = NULL; | |
947ce772 | 732 | char **envp = NULL; |
e81c3a4c | 733 | Spawn spawn; |
912541b0 | 734 | pid_t pid; |
feaa6db7 | 735 | int r; |
912541b0 | 736 | |
dc8aec36 YW |
737 | assert(event); |
738 | assert(event->dev); | |
e81c3a4c YW |
739 | assert(result || ressize == 0); |
740 | ||
912541b0 | 741 | /* pipes from child to parent */ |
2e48548f | 742 | if (result || log_get_max_level() >= LOG_INFO) |
f71e8ec1 | 743 | if (pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) != 0) |
88d566aa YW |
744 | return log_device_error_errno(event->dev, errno, |
745 | "Failed to create pipe for command '%s': %m", cmd); | |
912541b0 | 746 | |
feaa6db7 | 747 | if (log_get_max_level() >= LOG_INFO) |
f71e8ec1 | 748 | if (pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) != 0) |
88d566aa YW |
749 | return log_device_error_errno(event->dev, errno, |
750 | "Failed to create pipe for command '%s': %m", cmd); | |
feaa6db7 | 751 | |
90e30d76 | 752 | r = strv_split_full(&argv, cmd, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_RETAIN_ESCAPE); |
0645b83a ZJS |
753 | if (r < 0) |
754 | return log_device_error_errno(event->dev, r, "Failed to split command: %m"); | |
feaa6db7 | 755 | |
baaa35ad | 756 | if (isempty(argv[0])) |
88d566aa YW |
757 | return log_device_error_errno(event->dev, SYNTHETIC_ERRNO(EINVAL), |
758 | "Invalid command '%s'", cmd); | |
dc8aec36 | 759 | |
feaa6db7 YW |
760 | /* allow programs in /usr/lib/udev/ to be called without the path */ |
761 | if (!path_is_absolute(argv[0])) { | |
762 | char *program; | |
763 | ||
62a85ee0 | 764 | program = path_join(UDEVLIBEXECDIR, argv[0]); |
feaa6db7 YW |
765 | if (!program) |
766 | return log_oom(); | |
bbf35206 | 767 | |
feaa6db7 YW |
768 | free_and_replace(argv[0], program); |
769 | } | |
770 | ||
cf28ad46 | 771 | r = device_get_properties_strv(event->dev, &envp); |
947ce772 | 772 | if (r < 0) |
cf28ad46 | 773 | return log_device_error_errno(event->dev, r, "Failed to get device properties"); |
947ce772 | 774 | |
88d566aa | 775 | log_device_debug(event->dev, "Starting '%s'", cmd); |
3ad4d482 | 776 | |
6ce075a2 | 777 | r = safe_fork("(spawn)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
feaa6db7 | 778 | if (r < 0) |
88d566aa YW |
779 | return log_device_error_errno(event->dev, r, |
780 | "Failed to fork() to execute command '%s': %m", cmd); | |
feaa6db7 | 781 | if (r == 0) { |
84b1ccb9 YW |
782 | if (rearrange_stdio(-1, outpipe[WRITE_END], errpipe[WRITE_END]) < 0) |
783 | _exit(EXIT_FAILURE); | |
912541b0 | 784 | |
84b1ccb9 | 785 | (void) close_all_fds(NULL, 0); |
595225af | 786 | (void) rlimit_nofile_safe(); |
912541b0 | 787 | |
947ce772 | 788 | execve(argv[0], argv, envp); |
84b1ccb9 | 789 | _exit(EXIT_FAILURE); |
bbf35206 | 790 | } |
912541b0 | 791 | |
4c253ed1 LP |
792 | /* parent closed child's ends of pipes */ |
793 | outpipe[WRITE_END] = safe_close(outpipe[WRITE_END]); | |
794 | errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]); | |
912541b0 | 795 | |
e81c3a4c | 796 | spawn = (Spawn) { |
88d566aa | 797 | .device = event->dev, |
e81c3a4c YW |
798 | .cmd = cmd, |
799 | .pid = pid, | |
800 | .accept_failure = accept_failure, | |
66f737b4 | 801 | .timeout_warn_usec = udev_warn_timeout(timeout_usec), |
e81c3a4c | 802 | .timeout_usec = timeout_usec, |
e2099267 | 803 | .timeout_signal = timeout_signal, |
e81c3a4c YW |
804 | .event_birth_usec = event->birth_usec, |
805 | .fd_stdout = outpipe[READ_END], | |
806 | .fd_stderr = errpipe[READ_END], | |
807 | .result = result, | |
808 | .result_size = ressize, | |
809 | }; | |
810 | r = spawn_wait(&spawn); | |
feaa6db7 | 811 | if (r < 0) |
88d566aa YW |
812 | return log_device_error_errno(event->dev, r, |
813 | "Failed to wait for spawned command '%s': %m", cmd); | |
2181d30a | 814 | |
e81c3a4c YW |
815 | if (result) |
816 | result[spawn.result_len] = '\0'; | |
817 | ||
a7521142 | 818 | return r; /* 0 for success, and positive if the program failed */ |
2181d30a KS |
819 | } |
820 | ||
2e088715 | 821 | static int rename_netif(UdevEvent *event) { |
cf28ad46 | 822 | sd_device *dev = event->dev; |
d4d690fa | 823 | const char *oldname; |
2740750d YW |
824 | int ifindex, r; |
825 | ||
826 | if (!event->name) | |
827 | return 0; /* No new name is requested. */ | |
828 | ||
829 | r = sd_device_get_sysname(dev, &oldname); | |
830 | if (r < 0) | |
831 | return log_device_error_errno(dev, r, "Failed to get sysname: %m"); | |
832 | ||
833 | if (streq(event->name, oldname)) | |
834 | return 0; /* The interface name is already requested name. */ | |
835 | ||
a1130022 | 836 | if (!device_for_action(dev, SD_DEVICE_ADD)) |
2740750d | 837 | return 0; /* Rename the interface only when it is added. */ |
16d26d55 | 838 | |
2740750d YW |
839 | r = sd_device_get_ifindex(dev, &ifindex); |
840 | if (r == -ENOENT) | |
841 | return 0; /* Device is not a network interface. */ | |
842 | if (r < 0) | |
843 | return log_device_error_errno(dev, r, "Failed to get ifindex: %m"); | |
912541b0 | 844 | |
a4055a60 YW |
845 | /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */ |
846 | r = device_add_property(dev, "ID_RENAMING", "1"); | |
847 | if (r < 0) | |
99058cd6 | 848 | return log_device_warning_errno(dev, r, "Failed to add 'ID_RENAMING' property: %m"); |
912541b0 | 849 | |
2740750d | 850 | r = device_rename(dev, event->name); |
f647962d | 851 | if (r < 0) |
99058cd6 | 852 | return log_device_warning_errno(dev, r, "Failed to update properties with new name '%s': %m", event->name); |
16d26d55 | 853 | |
ae353ec2 YW |
854 | /* Also set ID_RENAMING boolean property to cloned sd_device object and save it to database |
855 | * before calling rtnl_set_link_name(). Otherwise, clients (e.g., systemd-networkd) may receive | |
856 | * RTM_NEWLINK netlink message before the database is updated. */ | |
857 | r = device_add_property(event->dev_db_clone, "ID_RENAMING", "1"); | |
858 | if (r < 0) | |
859 | return log_device_warning_errno(event->dev_db_clone, r, "Failed to add 'ID_RENAMING' property: %m"); | |
860 | ||
861 | r = device_update_db(event->dev_db_clone); | |
862 | if (r < 0) | |
863 | return log_device_debug_errno(event->dev_db_clone, r, "Failed to update database under /run/udev/data/: %m"); | |
864 | ||
865 | r = rtnl_set_link_name(&event->rtnl, ifindex, event->name); | |
866 | if (r < 0) | |
867 | return log_device_error_errno(dev, r, "Failed to rename network interface %i from '%s' to '%s': %m", | |
868 | ifindex, oldname, event->name); | |
869 | ||
589384be | 870 | log_device_debug(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, event->name); |
16d26d55 | 871 | |
2740750d | 872 | return 1; |
d46f37fd KS |
873 | } |
874 | ||
2e088715 | 875 | static int update_devnode(UdevEvent *event) { |
cf28ad46 | 876 | sd_device *dev = event->dev; |
e52eaf56 YW |
877 | int r; |
878 | ||
7af1c780 | 879 | r = sd_device_get_devnum(dev, NULL); |
e52eaf56 YW |
880 | if (r == -ENOENT) |
881 | return 0; | |
882 | if (r < 0) | |
883 | return log_device_error_errno(dev, r, "Failed to get devnum: %m"); | |
884 | ||
885 | /* remove/update possible left-over symlinks from old database entry */ | |
28266c44 | 886 | (void) udev_node_update_old_links(dev, event->dev_db_clone); |
e52eaf56 | 887 | |
25de7aa7 | 888 | if (!uid_is_valid(event->uid)) { |
e52eaf56 | 889 | r = device_get_devnode_uid(dev, &event->uid); |
3708c0f4 | 890 | if (r < 0 && r != -ENOENT) |
e52eaf56 YW |
891 | return log_device_error_errno(dev, r, "Failed to get devnode UID: %m"); |
892 | } | |
893 | ||
25de7aa7 | 894 | if (!gid_is_valid(event->gid)) { |
e52eaf56 | 895 | r = device_get_devnode_gid(dev, &event->gid); |
3708c0f4 | 896 | if (r < 0 && r != -ENOENT) |
e52eaf56 YW |
897 | return log_device_error_errno(dev, r, "Failed to get devnode GID: %m"); |
898 | } | |
899 | ||
25de7aa7 | 900 | if (event->mode == MODE_INVALID) { |
e52eaf56 YW |
901 | r = device_get_devnode_mode(dev, &event->mode); |
902 | if (r < 0 && r != -ENOENT) | |
903 | return log_device_error_errno(dev, r, "Failed to get devnode mode: %m"); | |
e52eaf56 | 904 | } |
3708c0f4 ZJS |
905 | if (event->mode == MODE_INVALID && gid_is_valid(event->gid) && event->gid > 0) |
906 | /* If group is set, but mode is not set, "upgrade" mode for the group. */ | |
907 | event->mode = 0660; | |
908 | ||
a1130022 | 909 | bool apply_mac = device_for_action(dev, SD_DEVICE_ADD); |
e52eaf56 | 910 | |
3708c0f4 | 911 | return udev_node_add(dev, apply_mac, event->mode, event->uid, event->gid, event->seclabel_list); |
e52eaf56 YW |
912 | } |
913 | ||
4cac2260 | 914 | static int event_execute_rules_on_remove( |
2e088715 | 915 | UdevEvent *event, |
df7ee959 | 916 | int inotify_fd, |
66f737b4 | 917 | usec_t timeout_usec, |
e2099267 | 918 | int timeout_signal, |
eb1f9e30 | 919 | Hashmap *properties_list, |
9a07157d | 920 | UdevRules *rules) { |
912541b0 | 921 | |
cf28ad46 | 922 | sd_device *dev = event->dev; |
eb1f9e30 | 923 | int r; |
912541b0 | 924 | |
ebcc52fa | 925 | r = device_read_db_internal(dev, true); |
eb1f9e30 YW |
926 | if (r < 0) |
927 | log_device_debug_errno(dev, r, "Failed to read database under /run/udev/data/: %m"); | |
107f2e25 | 928 | |
eb1f9e30 YW |
929 | r = device_tag_index(dev, NULL, false); |
930 | if (r < 0) | |
931 | log_device_debug_errno(dev, r, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m"); | |
912541b0 | 932 | |
eb1f9e30 YW |
933 | r = device_delete_db(dev); |
934 | if (r < 0) | |
935 | log_device_debug_errno(dev, r, "Failed to delete database under /run/udev/data/, ignoring: %m"); | |
912541b0 | 936 | |
5e0d0510 | 937 | (void) udev_watch_end(inotify_fd, dev); |
eb1f9e30 | 938 | |
4cac2260 | 939 | r = udev_rules_apply_to_event(rules, event, timeout_usec, timeout_signal, properties_list); |
eb1f9e30 | 940 | |
d4a95a95 | 941 | if (sd_device_get_devnum(dev, NULL) >= 0) |
eb1f9e30 | 942 | (void) udev_node_remove(dev); |
4cac2260 YW |
943 | |
944 | return r; | |
eb1f9e30 YW |
945 | } |
946 | ||
51d9aec0 | 947 | static int udev_event_on_move(sd_device *dev) { |
a4055a60 YW |
948 | int r; |
949 | ||
a4055a60 YW |
950 | /* Drop previously added property */ |
951 | r = device_add_property(dev, "ID_RENAMING", NULL); | |
952 | if (r < 0) | |
99058cd6 | 953 | return log_device_debug_errno(dev, r, "Failed to remove 'ID_RENAMING' property: %m"); |
a4055a60 YW |
954 | |
955 | return 0; | |
956 | } | |
957 | ||
e77b146f LP |
958 | static int copy_all_tags(sd_device *d, sd_device *s) { |
959 | const char *tag; | |
960 | int r; | |
961 | ||
962 | assert(d); | |
963 | ||
964 | if (!s) | |
965 | return 0; | |
966 | ||
5b90b906 | 967 | FOREACH_DEVICE_TAG(s, tag) { |
e77b146f LP |
968 | r = device_add_tag(d, tag, false); |
969 | if (r < 0) | |
970 | return r; | |
971 | } | |
972 | ||
973 | return 0; | |
974 | } | |
975 | ||
df7ee959 YW |
976 | int udev_event_execute_rules( |
977 | UdevEvent *event, | |
978 | int inotify_fd, /* This may be negative */ | |
979 | usec_t timeout_usec, | |
980 | int timeout_signal, | |
981 | Hashmap *properties_list, | |
982 | UdevRules *rules) { | |
983 | ||
a1130022 | 984 | sd_device_action_t action; |
6d0fdf45 | 985 | sd_device *dev; |
eb1f9e30 YW |
986 | int r; |
987 | ||
988 | assert(event); | |
989 | assert(rules); | |
990 | ||
6d0fdf45 YW |
991 | dev = event->dev; |
992 | ||
a1130022 | 993 | r = sd_device_get_action(dev, &action); |
eb1f9e30 | 994 | if (r < 0) |
d4d690fa | 995 | return log_device_error_errno(dev, r, "Failed to get ACTION: %m"); |
b081b27e | 996 | |
4cac2260 YW |
997 | if (action == SD_DEVICE_REMOVE) |
998 | return event_execute_rules_on_remove(event, inotify_fd, timeout_usec, timeout_signal, properties_list, rules); | |
912541b0 | 999 | |
e7f781e4 YW |
1000 | /* Disable watch during event processing. */ |
1001 | (void) udev_watch_end(inotify_fd, event->dev); | |
1002 | ||
480ecb7d | 1003 | r = device_clone_with_db(dev, &event->dev_db_clone); |
eb1f9e30 | 1004 | if (r < 0) |
99058cd6 | 1005 | return log_device_debug_errno(dev, r, "Failed to clone sd_device object: %m"); |
912541b0 | 1006 | |
e77b146f LP |
1007 | r = copy_all_tags(dev, event->dev_db_clone); |
1008 | if (r < 0) | |
1009 | log_device_warning_errno(dev, r, "Failed to copy all tags from old database entry, ignoring: %m"); | |
1010 | ||
a1130022 | 1011 | if (action == SD_DEVICE_MOVE) { |
51d9aec0 | 1012 | r = udev_event_on_move(event->dev); |
99058cd6 YW |
1013 | if (r < 0) |
1014 | return r; | |
1015 | } | |
eb1f9e30 | 1016 | |
e2099267 | 1017 | r = udev_rules_apply_to_event(rules, event, timeout_usec, timeout_signal, properties_list); |
99058cd6 YW |
1018 | if (r < 0) |
1019 | return log_device_debug_errno(dev, r, "Failed to apply udev rules: %m"); | |
eb1f9e30 | 1020 | |
99058cd6 YW |
1021 | r = rename_netif(event); |
1022 | if (r < 0) | |
1023 | return r; | |
1024 | ||
1025 | r = update_devnode(event); | |
1026 | if (r < 0) | |
1027 | return r; | |
eb1f9e30 YW |
1028 | |
1029 | /* preserve old, or get new initialization timestamp */ | |
480ecb7d | 1030 | r = device_ensure_usec_initialized(dev, event->dev_db_clone); |
eb1f9e30 | 1031 | if (r < 0) |
99058cd6 | 1032 | return log_device_debug_errno(dev, r, "Failed to set initialization timestamp: %m"); |
eb1f9e30 YW |
1033 | |
1034 | /* (re)write database file */ | |
480ecb7d | 1035 | r = device_tag_index(dev, event->dev_db_clone, true); |
eb1f9e30 | 1036 | if (r < 0) |
99058cd6 | 1037 | return log_device_debug_errno(dev, r, "Failed to update tags under /run/udev/tag/: %m"); |
eb1f9e30 YW |
1038 | |
1039 | r = device_update_db(dev); | |
1040 | if (r < 0) | |
99058cd6 | 1041 | return log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/: %m"); |
eb1f9e30 | 1042 | |
996c8390 MS |
1043 | device_set_is_initialized(dev); |
1044 | ||
30f6dce6 MS |
1045 | /* Yes, we run update_devnode() twice, because in the first invocation, that is before update of udev database, |
1046 | * it could happen that two contenders are replacing each other's symlink. Hence we run it again to make sure | |
1047 | * symlinks point to devices that claim them with the highest priority. */ | |
75b49846 | 1048 | return update_devnode(event); |
d46f37fd | 1049 | } |
2d73813e | 1050 | |
e2099267 | 1051 | void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec, int timeout_signal) { |
1448820a | 1052 | const char *command; |
29448498 | 1053 | void *val; |
25de7aa7 | 1054 | int r; |
912541b0 | 1055 | |
90e74a66 | 1056 | ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) { |
25de7aa7 | 1057 | UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val); |
912541b0 | 1058 | |
25de7aa7 YW |
1059 | if (builtin_cmd != _UDEV_BUILTIN_INVALID) { |
1060 | log_device_debug(event->dev, "Running built-in command \"%s\"", command); | |
1061 | r = udev_builtin_run(event->dev, builtin_cmd, command, false); | |
1062 | if (r < 0) | |
1063 | log_device_debug_errno(event->dev, r, "Failed to run built-in command \"%s\", ignoring: %m", command); | |
1064 | } else { | |
6b92f429 | 1065 | if (event->exec_delay_usec > 0) { |
25de7aa7 YW |
1066 | char buf[FORMAT_TIMESPAN_MAX]; |
1067 | ||
1068 | log_device_debug(event->dev, "Delaying execution of \"%s\" for %s.", | |
1069 | command, format_timespan(buf, sizeof(buf), event->exec_delay_usec, USEC_PER_SEC)); | |
6b92f429 | 1070 | (void) usleep(event->exec_delay_usec); |
912541b0 KS |
1071 | } |
1072 | ||
25de7aa7 | 1073 | log_device_debug(event->dev, "Running command \"%s\"", command); |
e2099267 MS |
1074 | |
1075 | r = udev_event_spawn(event, timeout_usec, timeout_signal, false, command, NULL, 0); | |
08de1958 YW |
1076 | if (r < 0) |
1077 | log_device_warning_errno(event->dev, r, "Failed to execute '%s', ignoring: %m", command); | |
1078 | else if (r > 0) /* returned value is positive when program fails */ | |
25de7aa7 | 1079 | log_device_debug(event->dev, "Command \"%s\" returned %d (error), ignoring.", command, r); |
912541b0 KS |
1080 | } |
1081 | } | |
2d73813e | 1082 | } |
0d77bc4a YW |
1083 | |
1084 | int udev_event_process_inotify_watch(UdevEvent *event, int inotify_fd) { | |
1085 | sd_device *dev; | |
0d77bc4a YW |
1086 | |
1087 | assert(event); | |
1088 | assert(inotify_fd >= 0); | |
1089 | ||
1090 | dev = event->dev; | |
1091 | ||
1092 | assert(dev); | |
1093 | ||
1094 | if (device_for_action(dev, SD_DEVICE_REMOVE)) | |
1095 | return 0; | |
1096 | ||
e7f781e4 YW |
1097 | if (event->inotify_watch) |
1098 | (void) udev_watch_begin(inotify_fd, dev); | |
1099 | else | |
0d77bc4a | 1100 | (void) udev_watch_end(inotify_fd, dev); |
0d77bc4a YW |
1101 | |
1102 | return 0; | |
1103 | } |