]>
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 YW |
237 | dev_t devnum; |
238 | int r; | |
0d53705b | 239 | |
ef315ada | 240 | switch (type) { |
7e9c23dd | 241 | case FORMAT_SUBST_DEVPATH: |
4cade7a1 YW |
242 | r = sd_device_get_devpath(dev, &val); |
243 | if (r < 0) | |
244 | return r; | |
245 | l = strpcpy(&s, l, val); | |
0d53705b | 246 | break; |
7e9c23dd | 247 | case FORMAT_SUBST_KERNEL: |
4cade7a1 YW |
248 | r = sd_device_get_sysname(dev, &val); |
249 | if (r < 0) | |
250 | return r; | |
251 | l = strpcpy(&s, l, val); | |
0d53705b | 252 | break; |
7e9c23dd | 253 | case FORMAT_SUBST_KERNEL_NUMBER: |
4cade7a1 | 254 | r = sd_device_get_sysnum(dev, &val); |
380d1901 YW |
255 | if (r == -ENOENT) |
256 | goto null_terminate; | |
4cade7a1 | 257 | if (r < 0) |
380d1901 | 258 | return r; |
4cade7a1 | 259 | l = strpcpy(&s, l, val); |
0d53705b | 260 | break; |
7e9c23dd | 261 | case FORMAT_SUBST_ID: |
4cade7a1 | 262 | if (!event->dev_parent) |
380d1901 | 263 | goto null_terminate; |
f3d241fe | 264 | r = sd_device_get_sysname(event->dev_parent, &val); |
4cade7a1 YW |
265 | if (r < 0) |
266 | return r; | |
267 | l = strpcpy(&s, l, val); | |
0d53705b | 268 | break; |
7e9c23dd | 269 | case FORMAT_SUBST_DRIVER: |
4cade7a1 | 270 | if (!event->dev_parent) |
380d1901 | 271 | goto null_terminate; |
f3d241fe | 272 | r = sd_device_get_driver(event->dev_parent, &val); |
380d1901 YW |
273 | if (r == -ENOENT) |
274 | goto null_terminate; | |
4cade7a1 | 275 | if (r < 0) |
380d1901 | 276 | return r; |
4cade7a1 | 277 | l = strpcpy(&s, l, val); |
0d53705b | 278 | break; |
7e9c23dd YW |
279 | case FORMAT_SUBST_MAJOR: |
280 | case FORMAT_SUBST_MINOR: { | |
4cade7a1 | 281 | char buf[DECIMAL_STR_MAX(unsigned)]; |
0d53705b | 282 | |
4cade7a1 YW |
283 | r = sd_device_get_devnum(dev, &devnum); |
284 | if (r < 0 && r != -ENOENT) | |
285 | return r; | |
ef315ada | 286 | xsprintf(buf, "%u", r < 0 ? 0 : type == FORMAT_SUBST_MAJOR ? major(devnum) : minor(devnum)); |
4cade7a1 | 287 | l = strpcpy(&s, l, buf); |
0d53705b DS |
288 | break; |
289 | } | |
7e9c23dd | 290 | case FORMAT_SUBST_RESULT: { |
ce4f94b8 YW |
291 | unsigned index = 0; /* 0 means whole string */ |
292 | bool has_plus; | |
0d53705b | 293 | |
4cade7a1 | 294 | if (!event->program_result) |
380d1901 | 295 | goto null_terminate; |
4cade7a1 | 296 | |
ce4f94b8 YW |
297 | if (!isempty(attr)) { |
298 | r = safe_atou_optional_plus(attr, &index); | |
299 | if (r < 0) | |
300 | return r; | |
301 | ||
302 | has_plus = r; | |
303 | } | |
304 | ||
305 | if (index == 0) | |
306 | l = strpcpy(&s, l, event->program_result); | |
307 | else { | |
308 | const char *start, *p; | |
309 | unsigned i; | |
310 | ||
311 | p = skip_leading_chars(event->program_result, NULL); | |
312 | ||
313 | for (i = 1; i < index; i++) { | |
314 | while (p[0] != '\0' && !strchr(WHITESPACE, p[0])) | |
315 | p++; | |
316 | p = skip_leading_chars(p, NULL); | |
317 | if (*p == '\0') | |
0d53705b DS |
318 | break; |
319 | } | |
ce4f94b8 YW |
320 | if (i != index) { |
321 | log_device_debug(dev, "requested part of result string not found"); | |
322 | goto null_terminate; | |
0d53705b | 323 | } |
ce4f94b8 YW |
324 | |
325 | start = p; | |
326 | /* %c{2+} copies the whole string from the second part on */ | |
327 | if (has_plus) | |
328 | l = strpcpy(&s, l, start); | |
329 | else { | |
330 | while (p[0] != '\0' && !strchr(WHITESPACE, p[0])) | |
331 | p++; | |
332 | l = strnpcpy(&s, l, start, p - start); | |
0d53705b | 333 | } |
ce4f94b8 | 334 | } |
0d53705b DS |
335 | break; |
336 | } | |
7e9c23dd | 337 | case FORMAT_SUBST_ATTR: { |
0d53705b DS |
338 | char vbuf[UTIL_NAME_SIZE]; |
339 | size_t len; | |
340 | int count; | |
341 | ||
13cd553f | 342 | if (isempty(attr)) |
4cade7a1 | 343 | return -EINVAL; |
0d53705b DS |
344 | |
345 | /* try to read the value specified by "[dmi/id]product_name" */ | |
3839535a | 346 | if (util_resolve_subsys_kernel(attr, vbuf, sizeof(vbuf), true) == 0) |
4cade7a1 | 347 | val = vbuf; |
0d53705b DS |
348 | |
349 | /* try to read the attribute the device */ | |
4cade7a1 YW |
350 | if (!val) |
351 | (void) sd_device_get_sysattr_value(dev, attr, &val); | |
0d53705b DS |
352 | |
353 | /* try to read the attribute of the parent device, other matches have selected */ | |
f3d241fe YW |
354 | if (!val && event->dev_parent && event->dev_parent != dev) |
355 | (void) sd_device_get_sysattr_value(event->dev_parent, attr, &val); | |
0d53705b | 356 | |
4cade7a1 | 357 | if (!val) |
380d1901 | 358 | goto null_terminate; |
0d53705b DS |
359 | |
360 | /* strip trailing whitespace, and replace unwanted characters */ | |
4cade7a1 YW |
361 | if (val != vbuf) |
362 | strscpy(vbuf, sizeof(vbuf), val); | |
0d53705b DS |
363 | len = strlen(vbuf); |
364 | while (len > 0 && isspace(vbuf[--len])) | |
365 | vbuf[len] = '\0'; | |
366 | count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT); | |
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); |
380d1901 YW |
374 | if (r == -ENODEV) |
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 | ||
2e088715 | 441 | ssize_t udev_event_apply_format(UdevEvent *event, |
4cade7a1 YW |
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 | |
13cd553f YW |
453 | while (s[0] != '\0') { |
454 | FormatSubstitutionType type; | |
455 | char attr[UTIL_PATH_SIZE]; | |
4cade7a1 | 456 | ssize_t subst_len; |
912541b0 | 457 | |
13cd553f YW |
458 | r = get_subst_type(&s, &type, attr); |
459 | if (r < 0) | |
460 | return log_device_warning_errno(event->dev, r, "Invalid format string, ignoring: %s", src); | |
461 | if (r == 0) { | |
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 YW |
469 | subst_len = udev_event_subst_format(event, type, attr, dest, size); |
470 | if (subst_len < 0) | |
471 | return 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 | ||
7e9c23dd | 475 | /* FORMAT_SUBST_RESULT handles spaces itself */ |
13cd553f | 476 | if (replace_whitespace && type != FORMAT_SUBST_RESULT) |
be452683 | 477 | /* util_replace_whitespace can replace in-place, |
13cd553f YW |
478 | * and does nothing if subst_len == 0 */ |
479 | subst_len = util_replace_whitespace(dest, dest, subst_len); | |
e20a9171 | 480 | |
13cd553f YW |
481 | dest += subst_len; |
482 | size -= subst_len; | |
912541b0 | 483 | } |
065db052 | 484 | |
13cd553f YW |
485 | assert(size >= 1); |
486 | dest[0] = '\0'; | |
487 | return size; | |
f1128767 KS |
488 | } |
489 | ||
e81c3a4c YW |
490 | static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
491 | Spawn *spawn = userdata; | |
492 | char buf[4096], *p; | |
493 | size_t size; | |
494 | ssize_t l; | |
912541b0 | 495 | |
e81c3a4c YW |
496 | assert(spawn); |
497 | assert(fd == spawn->fd_stdout || fd == spawn->fd_stderr); | |
498 | assert(!spawn->result || spawn->result_len < spawn->result_size); | |
499 | ||
500 | if (fd == spawn->fd_stdout && spawn->result) { | |
501 | p = spawn->result + spawn->result_len; | |
502 | size = spawn->result_size - spawn->result_len; | |
503 | } else { | |
504 | p = buf; | |
505 | size = sizeof(buf); | |
912541b0 KS |
506 | } |
507 | ||
e81c3a4c YW |
508 | l = read(fd, p, size - 1); |
509 | if (l < 0) { | |
510 | if (errno != EAGAIN) | |
511 | log_error_errno(errno, "Failed to read stdout of '%s': %m", spawn->cmd); | |
912541b0 | 512 | |
e81c3a4c | 513 | return 0; |
912541b0 KS |
514 | } |
515 | ||
e81c3a4c YW |
516 | p[l] = '\0'; |
517 | if (fd == spawn->fd_stdout && spawn->result) | |
518 | spawn->result_len += l; | |
912541b0 | 519 | |
e81c3a4c YW |
520 | /* Log output only if we watch stderr. */ |
521 | if (l > 0 && spawn->fd_stderr >= 0) { | |
522 | _cleanup_strv_free_ char **v = NULL; | |
523 | char **q; | |
912541b0 | 524 | |
e81c3a4c YW |
525 | v = strv_split_newlines(p); |
526 | if (!v) | |
527 | return 0; | |
912541b0 | 528 | |
e81c3a4c YW |
529 | STRV_FOREACH(q, v) |
530 | log_debug("'%s'(%s) '%s'", spawn->cmd, | |
531 | fd == spawn->fd_stdout ? "out" : "err", *q); | |
912541b0 KS |
532 | } |
533 | ||
e81c3a4c | 534 | return 0; |
2181d30a KS |
535 | } |
536 | ||
8128f229 TG |
537 | static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
538 | Spawn *spawn = userdata; | |
4375dab5 | 539 | char timeout[FORMAT_TIMESPAN_MAX]; |
912541b0 | 540 | |
8128f229 | 541 | assert(spawn); |
912541b0 | 542 | |
8128f229 | 543 | kill_and_sigcont(spawn->pid, SIGKILL); |
912541b0 | 544 | |
5ca3dbc9 | 545 | log_error("Spawned process '%s' ["PID_FMT"] timed out after %s, killing", spawn->cmd, spawn->pid, |
4375dab5 | 546 | format_timespan(timeout, sizeof(timeout), spawn->timeout_usec, USEC_PER_SEC)); |
912541b0 | 547 | |
8128f229 TG |
548 | return 1; |
549 | } | |
67117413 | 550 | |
8128f229 TG |
551 | static int on_spawn_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { |
552 | Spawn *spawn = userdata; | |
4375dab5 | 553 | char timeout[FORMAT_TIMESPAN_MAX]; |
912541b0 | 554 | |
8128f229 | 555 | assert(spawn); |
67117413 | 556 | |
5ca3dbc9 | 557 | log_warning("Spawned process '%s' ["PID_FMT"] is taking longer than %s to complete", spawn->cmd, spawn->pid, |
4375dab5 | 558 | format_timespan(timeout, sizeof(timeout), spawn->timeout_warn_usec, USEC_PER_SEC)); |
8128f229 TG |
559 | |
560 | return 1; | |
561 | } | |
562 | ||
563 | static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) { | |
564 | Spawn *spawn = userdata; | |
a7521142 | 565 | int ret = -EIO; |
8128f229 TG |
566 | |
567 | assert(spawn); | |
568 | ||
569 | switch (si->si_code) { | |
570 | case CLD_EXITED: | |
a7521142 | 571 | if (si->si_status == 0) |
53318514 | 572 | log_debug("Process '%s' succeeded.", spawn->cmd); |
a7521142 ZJS |
573 | else |
574 | log_full(spawn->accept_failure ? LOG_DEBUG : LOG_WARNING, | |
575 | "Process '%s' failed with exit code %i.", spawn->cmd, si->si_status); | |
576 | ret = si->si_status; | |
8128f229 TG |
577 | break; |
578 | case CLD_KILLED: | |
579 | case CLD_DUMPED: | |
a7521142 | 580 | log_error("Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status)); |
8128f229 TG |
581 | break; |
582 | default: | |
53318514 | 583 | log_error("Process '%s' failed due to unknown reason.", spawn->cmd); |
8128f229 | 584 | } |
912541b0 | 585 | |
a7521142 | 586 | sd_event_exit(sd_event_source_get_event(s), ret); |
8128f229 TG |
587 | return 1; |
588 | } | |
589 | ||
e81c3a4c | 590 | static int spawn_wait(Spawn *spawn) { |
4afd3348 | 591 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
a7521142 | 592 | int r; |
8128f229 | 593 | |
e81c3a4c YW |
594 | assert(spawn); |
595 | ||
8128f229 TG |
596 | r = sd_event_new(&e); |
597 | if (r < 0) | |
598 | return r; | |
599 | ||
e81c3a4c | 600 | if (spawn->timeout_usec > 0) { |
8128f229 TG |
601 | usec_t usec, age_usec; |
602 | ||
3285baa8 | 603 | usec = now(CLOCK_MONOTONIC); |
e81c3a4c YW |
604 | age_usec = usec - spawn->event_birth_usec; |
605 | if (age_usec < spawn->timeout_usec) { | |
606 | if (spawn->timeout_warn_usec > 0 && | |
607 | spawn->timeout_warn_usec < spawn->timeout_usec && | |
608 | spawn->timeout_warn_usec > age_usec) { | |
609 | spawn->timeout_warn_usec -= age_usec; | |
8128f229 | 610 | |
3285baa8 | 611 | r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
e81c3a4c YW |
612 | usec + spawn->timeout_warn_usec, USEC_PER_SEC, |
613 | on_spawn_timeout_warning, spawn); | |
8128f229 TG |
614 | if (r < 0) |
615 | return r; | |
912541b0 | 616 | } |
8128f229 | 617 | |
e81c3a4c | 618 | spawn->timeout_usec -= age_usec; |
8128f229 | 619 | |
3285baa8 | 620 | r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
e81c3a4c | 621 | usec + spawn->timeout_usec, USEC_PER_SEC, on_spawn_timeout, spawn); |
8128f229 TG |
622 | if (r < 0) |
623 | return r; | |
912541b0 KS |
624 | } |
625 | } | |
8128f229 | 626 | |
adeb26c1 YW |
627 | if (spawn->fd_stdout >= 0) { |
628 | r = sd_event_add_io(e, NULL, spawn->fd_stdout, EPOLLIN, on_spawn_io, spawn); | |
629 | if (r < 0) | |
630 | return r; | |
631 | } | |
e81c3a4c | 632 | |
adeb26c1 YW |
633 | if (spawn->fd_stderr >= 0) { |
634 | r = sd_event_add_io(e, NULL, spawn->fd_stderr, EPOLLIN, on_spawn_io, spawn); | |
635 | if (r < 0) | |
636 | return r; | |
637 | } | |
e81c3a4c YW |
638 | |
639 | r = sd_event_add_child(e, NULL, spawn->pid, WEXITED, on_spawn_sigchld, spawn); | |
8128f229 TG |
640 | if (r < 0) |
641 | return r; | |
642 | ||
a7521142 | 643 | return sd_event_loop(e); |
2181d30a KS |
644 | } |
645 | ||
2e088715 | 646 | int udev_event_spawn(UdevEvent *event, |
dd5eddd2 | 647 | usec_t timeout_usec, |
53318514 | 648 | bool accept_failure, |
bbf35206 | 649 | const char *cmd, |
dd5eddd2 | 650 | char *result, size_t ressize) { |
feaa6db7 YW |
651 | _cleanup_close_pair_ int outpipe[2] = {-1, -1}, errpipe[2] = {-1, -1}; |
652 | _cleanup_strv_free_ char **argv = NULL; | |
947ce772 | 653 | char **envp = NULL; |
e81c3a4c | 654 | Spawn spawn; |
912541b0 | 655 | pid_t pid; |
feaa6db7 | 656 | int r; |
912541b0 | 657 | |
dc8aec36 YW |
658 | assert(event); |
659 | assert(event->dev); | |
e81c3a4c YW |
660 | assert(result || ressize == 0); |
661 | ||
912541b0 | 662 | /* pipes from child to parent */ |
2e48548f | 663 | if (result || log_get_max_level() >= LOG_INFO) |
f71e8ec1 | 664 | if (pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) != 0) |
feaa6db7 | 665 | return log_error_errno(errno, "Failed to create pipe for command '%s': %m", cmd); |
912541b0 | 666 | |
feaa6db7 | 667 | if (log_get_max_level() >= LOG_INFO) |
f71e8ec1 | 668 | if (pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) != 0) |
feaa6db7 YW |
669 | return log_error_errno(errno, "Failed to create pipe for command '%s': %m", cmd); |
670 | ||
671 | argv = strv_split_full(cmd, NULL, SPLIT_QUOTES|SPLIT_RELAX); | |
672 | if (!argv) | |
673 | return log_oom(); | |
674 | ||
baaa35ad ZJS |
675 | if (isempty(argv[0])) |
676 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
677 | "Invalid command '%s'", cmd); | |
dc8aec36 | 678 | |
feaa6db7 YW |
679 | /* allow programs in /usr/lib/udev/ to be called without the path */ |
680 | if (!path_is_absolute(argv[0])) { | |
681 | char *program; | |
682 | ||
62a85ee0 | 683 | program = path_join(UDEVLIBEXECDIR, argv[0]); |
feaa6db7 YW |
684 | if (!program) |
685 | return log_oom(); | |
bbf35206 | 686 | |
feaa6db7 YW |
687 | free_and_replace(argv[0], program); |
688 | } | |
689 | ||
cf28ad46 | 690 | r = device_get_properties_strv(event->dev, &envp); |
947ce772 | 691 | if (r < 0) |
cf28ad46 | 692 | return log_device_error_errno(event->dev, r, "Failed to get device properties"); |
947ce772 | 693 | |
3ad4d482 YW |
694 | log_debug("Starting '%s'", cmd); |
695 | ||
6ce075a2 | 696 | r = safe_fork("(spawn)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
feaa6db7 YW |
697 | if (r < 0) |
698 | return log_error_errno(r, "Failed to fork() to execute command '%s': %m", cmd); | |
699 | if (r == 0) { | |
84b1ccb9 YW |
700 | if (rearrange_stdio(-1, outpipe[WRITE_END], errpipe[WRITE_END]) < 0) |
701 | _exit(EXIT_FAILURE); | |
912541b0 | 702 | |
84b1ccb9 | 703 | (void) close_all_fds(NULL, 0); |
595225af | 704 | (void) rlimit_nofile_safe(); |
912541b0 | 705 | |
947ce772 | 706 | execve(argv[0], argv, envp); |
84b1ccb9 | 707 | _exit(EXIT_FAILURE); |
bbf35206 | 708 | } |
912541b0 | 709 | |
4c253ed1 LP |
710 | /* parent closed child's ends of pipes */ |
711 | outpipe[WRITE_END] = safe_close(outpipe[WRITE_END]); | |
712 | errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]); | |
912541b0 | 713 | |
e81c3a4c YW |
714 | spawn = (Spawn) { |
715 | .cmd = cmd, | |
716 | .pid = pid, | |
717 | .accept_failure = accept_failure, | |
66f737b4 | 718 | .timeout_warn_usec = udev_warn_timeout(timeout_usec), |
e81c3a4c YW |
719 | .timeout_usec = timeout_usec, |
720 | .event_birth_usec = event->birth_usec, | |
721 | .fd_stdout = outpipe[READ_END], | |
722 | .fd_stderr = errpipe[READ_END], | |
723 | .result = result, | |
724 | .result_size = ressize, | |
725 | }; | |
726 | r = spawn_wait(&spawn); | |
feaa6db7 | 727 | if (r < 0) |
a7521142 | 728 | return log_error_errno(r, "Failed to wait for spawned command '%s': %m", cmd); |
2181d30a | 729 | |
e81c3a4c YW |
730 | if (result) |
731 | result[spawn.result_len] = '\0'; | |
732 | ||
a7521142 | 733 | return r; /* 0 for success, and positive if the program failed */ |
2181d30a KS |
734 | } |
735 | ||
2e088715 | 736 | static int rename_netif(UdevEvent *event) { |
cf28ad46 | 737 | sd_device *dev = event->dev; |
d4d690fa | 738 | const char *oldname; |
2740750d YW |
739 | int ifindex, r; |
740 | ||
741 | if (!event->name) | |
742 | return 0; /* No new name is requested. */ | |
743 | ||
744 | r = sd_device_get_sysname(dev, &oldname); | |
745 | if (r < 0) | |
746 | return log_device_error_errno(dev, r, "Failed to get sysname: %m"); | |
747 | ||
748 | if (streq(event->name, oldname)) | |
749 | return 0; /* The interface name is already requested name. */ | |
750 | ||
d4d690fa | 751 | if (!device_for_action(dev, DEVICE_ACTION_ADD)) |
2740750d | 752 | return 0; /* Rename the interface only when it is added. */ |
16d26d55 | 753 | |
2740750d YW |
754 | r = sd_device_get_ifindex(dev, &ifindex); |
755 | if (r == -ENOENT) | |
756 | return 0; /* Device is not a network interface. */ | |
757 | if (r < 0) | |
758 | return log_device_error_errno(dev, r, "Failed to get ifindex: %m"); | |
912541b0 | 759 | |
589384be | 760 | r = rtnl_set_link_name(&event->rtnl, ifindex, event->name); |
2740750d | 761 | if (r < 0) |
589384be YW |
762 | return log_device_error_errno(dev, r, "Failed to rename network interface %i from '%s' to '%s': %m", |
763 | ifindex, oldname, event->name); | |
912541b0 | 764 | |
a4055a60 YW |
765 | /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */ |
766 | r = device_add_property(dev, "ID_RENAMING", "1"); | |
767 | if (r < 0) | |
768 | log_device_warning_errno(dev, r, "Failed to add 'ID_RENAMING' property: %m"); | |
912541b0 | 769 | |
2740750d | 770 | r = device_rename(dev, event->name); |
f647962d | 771 | if (r < 0) |
a4055a60 | 772 | log_device_warning_errno(dev, r, "Failed to update properties with new name '%s': %m", event->name); |
16d26d55 | 773 | |
589384be | 774 | log_device_debug(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, event->name); |
16d26d55 | 775 | |
2740750d | 776 | return 1; |
d46f37fd KS |
777 | } |
778 | ||
2e088715 | 779 | static int update_devnode(UdevEvent *event) { |
cf28ad46 | 780 | sd_device *dev = event->dev; |
e52eaf56 YW |
781 | bool apply; |
782 | int r; | |
783 | ||
7af1c780 | 784 | r = sd_device_get_devnum(dev, NULL); |
e52eaf56 YW |
785 | if (r == -ENOENT) |
786 | return 0; | |
787 | if (r < 0) | |
788 | return log_device_error_errno(dev, r, "Failed to get devnum: %m"); | |
789 | ||
790 | /* remove/update possible left-over symlinks from old database entry */ | |
480ecb7d YW |
791 | if (event->dev_db_clone) |
792 | (void) udev_node_update_old_links(dev, event->dev_db_clone); | |
e52eaf56 | 793 | |
25de7aa7 | 794 | if (!uid_is_valid(event->uid)) { |
e52eaf56 | 795 | r = device_get_devnode_uid(dev, &event->uid); |
25de7aa7 YW |
796 | if (r == -ENOENT) |
797 | event->uid = 0; | |
798 | else if (r < 0) | |
e52eaf56 YW |
799 | return log_device_error_errno(dev, r, "Failed to get devnode UID: %m"); |
800 | } | |
801 | ||
25de7aa7 | 802 | if (!gid_is_valid(event->gid)) { |
e52eaf56 | 803 | r = device_get_devnode_gid(dev, &event->gid); |
25de7aa7 YW |
804 | if (r == -ENOENT) |
805 | event->gid = 0; | |
806 | else if (r < 0) | |
e52eaf56 YW |
807 | return log_device_error_errno(dev, r, "Failed to get devnode GID: %m"); |
808 | } | |
809 | ||
25de7aa7 | 810 | if (event->mode == MODE_INVALID) { |
e52eaf56 YW |
811 | r = device_get_devnode_mode(dev, &event->mode); |
812 | if (r < 0 && r != -ENOENT) | |
813 | return log_device_error_errno(dev, r, "Failed to get devnode mode: %m"); | |
814 | if (r == -ENOENT) { | |
815 | if (event->gid > 0) | |
816 | /* default 0660 if a group is assigned */ | |
817 | event->mode = 0660; | |
818 | else | |
819 | /* default 0600 */ | |
820 | event->mode = 0600; | |
821 | } | |
822 | } | |
823 | ||
25de7aa7 YW |
824 | apply = device_for_action(dev, DEVICE_ACTION_ADD) || |
825 | uid_is_valid(event->uid) || | |
826 | gid_is_valid(event->gid) || | |
827 | event->mode != MODE_INVALID; | |
e52eaf56 YW |
828 | return udev_node_add(dev, apply, event->mode, event->uid, event->gid, event->seclabel_list); |
829 | } | |
830 | ||
eb1f9e30 | 831 | static void event_execute_rules_on_remove( |
2e088715 | 832 | UdevEvent *event, |
66f737b4 | 833 | usec_t timeout_usec, |
eb1f9e30 | 834 | Hashmap *properties_list, |
9a07157d | 835 | UdevRules *rules) { |
912541b0 | 836 | |
cf28ad46 | 837 | sd_device *dev = event->dev; |
eb1f9e30 | 838 | int r; |
912541b0 | 839 | |
ebcc52fa | 840 | r = device_read_db_internal(dev, true); |
eb1f9e30 YW |
841 | if (r < 0) |
842 | log_device_debug_errno(dev, r, "Failed to read database under /run/udev/data/: %m"); | |
107f2e25 | 843 | |
eb1f9e30 YW |
844 | r = device_tag_index(dev, NULL, false); |
845 | if (r < 0) | |
846 | log_device_debug_errno(dev, r, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m"); | |
912541b0 | 847 | |
eb1f9e30 YW |
848 | r = device_delete_db(dev); |
849 | if (r < 0) | |
850 | log_device_debug_errno(dev, r, "Failed to delete database under /run/udev/data/, ignoring: %m"); | |
912541b0 | 851 | |
d4a95a95 | 852 | if (sd_device_get_devnum(dev, NULL) >= 0) |
eb1f9e30 YW |
853 | (void) udev_watch_end(dev); |
854 | ||
66f737b4 | 855 | (void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list); |
eb1f9e30 | 856 | |
d4a95a95 | 857 | if (sd_device_get_devnum(dev, NULL) >= 0) |
eb1f9e30 YW |
858 | (void) udev_node_remove(dev); |
859 | } | |
860 | ||
a4055a60 YW |
861 | static int udev_event_on_move(UdevEvent *event) { |
862 | sd_device *dev = event->dev; | |
863 | int r; | |
864 | ||
865 | if (event->dev_db_clone && | |
866 | sd_device_get_devnum(dev, NULL) < 0) { | |
867 | r = device_copy_properties(dev, event->dev_db_clone); | |
868 | if (r < 0) | |
869 | log_device_debug_errno(dev, r, "Failed to copy properties from cloned sd_device object, ignoring: %m"); | |
870 | } | |
871 | ||
872 | /* Drop previously added property */ | |
873 | r = device_add_property(dev, "ID_RENAMING", NULL); | |
874 | if (r < 0) | |
875 | return log_device_debug_errno(dev, r, "Failed to remove 'ID_RENAMING' property, ignoring: %m"); | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
2e088715 | 880 | int udev_event_execute_rules(UdevEvent *event, |
66f737b4 | 881 | usec_t timeout_usec, |
eb1f9e30 | 882 | Hashmap *properties_list, |
9a07157d | 883 | UdevRules *rules) { |
d4d690fa YW |
884 | const char *subsystem; |
885 | DeviceAction action; | |
6d0fdf45 | 886 | sd_device *dev; |
eb1f9e30 YW |
887 | int r; |
888 | ||
889 | assert(event); | |
890 | assert(rules); | |
891 | ||
6d0fdf45 YW |
892 | dev = event->dev; |
893 | ||
eb1f9e30 YW |
894 | r = sd_device_get_subsystem(dev, &subsystem); |
895 | if (r < 0) | |
896 | return log_device_error_errno(dev, r, "Failed to get subsystem: %m"); | |
897 | ||
d4d690fa | 898 | r = device_get_action(dev, &action); |
eb1f9e30 | 899 | if (r < 0) |
d4d690fa | 900 | return log_device_error_errno(dev, r, "Failed to get ACTION: %m"); |
b081b27e | 901 | |
d4d690fa | 902 | if (action == DEVICE_ACTION_REMOVE) { |
66f737b4 | 903 | event_execute_rules_on_remove(event, timeout_usec, properties_list, rules); |
eb1f9e30 YW |
904 | return 0; |
905 | } | |
912541b0 | 906 | |
480ecb7d | 907 | r = device_clone_with_db(dev, &event->dev_db_clone); |
eb1f9e30 YW |
908 | if (r < 0) |
909 | log_device_debug_errno(dev, r, "Failed to clone sd_device object, ignoring: %m"); | |
912541b0 | 910 | |
a4055a60 YW |
911 | if (event->dev_db_clone && sd_device_get_devnum(dev, NULL) >= 0) |
912 | /* Disable watch during event processing. */ | |
913 | (void) udev_watch_end(event->dev_db_clone); | |
912541b0 | 914 | |
d4d690fa | 915 | if (action == DEVICE_ACTION_MOVE) |
a4055a60 | 916 | (void) udev_event_on_move(event); |
eb1f9e30 | 917 | |
66f737b4 | 918 | (void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list); |
eb1f9e30 YW |
919 | |
920 | (void) rename_netif(event); | |
921 | (void) update_devnode(event); | |
922 | ||
923 | /* preserve old, or get new initialization timestamp */ | |
480ecb7d | 924 | r = device_ensure_usec_initialized(dev, event->dev_db_clone); |
eb1f9e30 YW |
925 | if (r < 0) |
926 | log_device_debug_errno(dev, r, "Failed to set initialization timestamp, ignoring: %m"); | |
927 | ||
928 | /* (re)write database file */ | |
480ecb7d | 929 | r = device_tag_index(dev, event->dev_db_clone, true); |
eb1f9e30 YW |
930 | if (r < 0) |
931 | log_device_debug_errno(dev, r, "Failed to update tags under /run/udev/tag/, ignoring: %m"); | |
932 | ||
933 | r = device_update_db(dev); | |
934 | if (r < 0) | |
935 | log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/, ignoring: %m"); | |
936 | ||
937 | device_set_is_initialized(dev); | |
938 | ||
480ecb7d | 939 | event->dev_db_clone = sd_device_unref(event->dev_db_clone); |
eb1f9e30 YW |
940 | |
941 | return 0; | |
d46f37fd | 942 | } |
2d73813e | 943 | |
2e088715 | 944 | void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec) { |
29448498 YW |
945 | const char *cmd; |
946 | void *val; | |
947 | Iterator i; | |
25de7aa7 | 948 | int r; |
912541b0 | 949 | |
39a15c8a | 950 | ORDERED_HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) { |
25de7aa7 | 951 | UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val); |
bbf35206 | 952 | char command[UTIL_PATH_SIZE]; |
83cd6b75 | 953 | |
25de7aa7 | 954 | (void) udev_event_apply_format(event, cmd, command, sizeof(command), false); |
912541b0 | 955 | |
25de7aa7 YW |
956 | if (builtin_cmd != _UDEV_BUILTIN_INVALID) { |
957 | log_device_debug(event->dev, "Running built-in command \"%s\"", command); | |
958 | r = udev_builtin_run(event->dev, builtin_cmd, command, false); | |
959 | if (r < 0) | |
960 | log_device_debug_errno(event->dev, r, "Failed to run built-in command \"%s\", ignoring: %m", command); | |
961 | } else { | |
6b92f429 | 962 | if (event->exec_delay_usec > 0) { |
25de7aa7 YW |
963 | char buf[FORMAT_TIMESPAN_MAX]; |
964 | ||
965 | log_device_debug(event->dev, "Delaying execution of \"%s\" for %s.", | |
966 | command, format_timespan(buf, sizeof(buf), event->exec_delay_usec, USEC_PER_SEC)); | |
6b92f429 | 967 | (void) usleep(event->exec_delay_usec); |
912541b0 KS |
968 | } |
969 | ||
25de7aa7 YW |
970 | log_device_debug(event->dev, "Running command \"%s\"", command); |
971 | r = udev_event_spawn(event, timeout_usec, false, command, NULL, 0); | |
972 | if (r > 0) /* returned value is positive when program fails */ | |
973 | log_device_debug(event->dev, "Command \"%s\" returned %d (error), ignoring.", command, r); | |
912541b0 KS |
974 | } |
975 | } | |
2d73813e | 976 | } |