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