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