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