]>
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> |
0a6f50c0 | 7 | #include <poll.h> |
07630cea LP |
8 | #include <stddef.h> |
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
2181d30a | 12 | #include <sys/epoll.h> |
07630cea | 13 | #include <sys/prctl.h> |
2181d30a | 14 | #include <sys/signalfd.h> |
07630cea LP |
15 | #include <sys/wait.h> |
16 | #include <unistd.h> | |
d46f37fd | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
f97b34a6 | 20 | #include "format-util.h" |
07630cea | 21 | #include "netlink-util.h" |
8128f229 | 22 | #include "process-util.h" |
24882e06 | 23 | #include "signal-util.h" |
07630cea | 24 | #include "string-util.h" |
24882e06 | 25 | #include "udev.h" |
8128f229 TG |
26 | |
27 | typedef struct Spawn { | |
28 | const char *cmd; | |
29 | pid_t pid; | |
30 | usec_t timeout_warn; | |
31 | usec_t timeout; | |
53318514 | 32 | bool accept_failure; |
8128f229 | 33 | } Spawn; |
d46f37fd | 34 | |
9ec6e95b | 35 | struct udev_event *udev_event_new(struct udev_device *dev) { |
912541b0 KS |
36 | struct udev_event *event; |
37 | ||
955d98c9 | 38 | event = new0(struct udev_event, 1); |
912541b0 KS |
39 | if (event == NULL) |
40 | return NULL; | |
41 | event->dev = dev; | |
2024ed61 YW |
42 | udev_list_init(NULL, &event->run_list, false); |
43 | udev_list_init(NULL, &event->seclabel_list, false); | |
3285baa8 | 44 | event->birth_usec = now(CLOCK_MONOTONIC); |
912541b0 | 45 | return event; |
aa8734ff KS |
46 | } |
47 | ||
9ec6e95b | 48 | void udev_event_unref(struct udev_event *event) { |
912541b0 KS |
49 | if (event == NULL) |
50 | return; | |
1c4baffc | 51 | sd_netlink_unref(event->rtnl); |
912541b0 | 52 | udev_list_cleanup(&event->run_list); |
c26547d6 | 53 | udev_list_cleanup(&event->seclabel_list); |
912541b0 KS |
54 | free(event->program_result); |
55 | free(event->name); | |
912541b0 | 56 | free(event); |
aa8734ff KS |
57 | } |
58 | ||
0d53705b DS |
59 | enum subst_type { |
60 | SUBST_UNKNOWN, | |
61 | SUBST_DEVNODE, | |
62 | SUBST_ATTR, | |
63 | SUBST_ENV, | |
64 | SUBST_KERNEL, | |
65 | SUBST_KERNEL_NUMBER, | |
66 | SUBST_DRIVER, | |
67 | SUBST_DEVPATH, | |
68 | SUBST_ID, | |
69 | SUBST_MAJOR, | |
70 | SUBST_MINOR, | |
71 | SUBST_RESULT, | |
72 | SUBST_PARENT, | |
73 | SUBST_NAME, | |
74 | SUBST_LINKS, | |
75 | SUBST_ROOT, | |
76 | SUBST_SYS, | |
77 | }; | |
78 | ||
79 | static size_t subst_format_var(struct udev_event *event, struct udev_device *dev, | |
80 | enum subst_type type, char *attr, | |
be452683 DS |
81 | char *dest, size_t l) { |
82 | char *s = dest; | |
0d53705b DS |
83 | |
84 | switch (type) { | |
85 | case SUBST_DEVPATH: | |
86 | l = strpcpy(&s, l, udev_device_get_devpath(dev)); | |
87 | break; | |
88 | case SUBST_KERNEL: | |
89 | l = strpcpy(&s, l, udev_device_get_sysname(dev)); | |
90 | break; | |
91 | case SUBST_KERNEL_NUMBER: | |
92 | if (udev_device_get_sysnum(dev) == NULL) | |
93 | break; | |
94 | l = strpcpy(&s, l, udev_device_get_sysnum(dev)); | |
95 | break; | |
96 | case SUBST_ID: | |
97 | if (event->dev_parent == NULL) | |
98 | break; | |
99 | l = strpcpy(&s, l, udev_device_get_sysname(event->dev_parent)); | |
100 | break; | |
101 | case SUBST_DRIVER: { | |
102 | const char *driver; | |
103 | ||
104 | if (event->dev_parent == NULL) | |
105 | break; | |
106 | ||
107 | driver = udev_device_get_driver(event->dev_parent); | |
108 | if (driver == NULL) | |
109 | break; | |
110 | l = strpcpy(&s, l, driver); | |
111 | break; | |
112 | } | |
113 | case SUBST_MAJOR: { | |
114 | char num[UTIL_PATH_SIZE]; | |
115 | ||
116 | sprintf(num, "%u", major(udev_device_get_devnum(dev))); | |
117 | l = strpcpy(&s, l, num); | |
118 | break; | |
119 | } | |
120 | case SUBST_MINOR: { | |
121 | char num[UTIL_PATH_SIZE]; | |
122 | ||
123 | sprintf(num, "%u", minor(udev_device_get_devnum(dev))); | |
124 | l = strpcpy(&s, l, num); | |
125 | break; | |
126 | } | |
127 | case SUBST_RESULT: { | |
128 | char *rest; | |
129 | int i; | |
130 | ||
131 | if (event->program_result == NULL) | |
132 | break; | |
133 | /* get part of the result string */ | |
134 | i = 0; | |
135 | if (attr != NULL) | |
136 | i = strtoul(attr, &rest, 10); | |
137 | if (i > 0) { | |
138 | char result[UTIL_PATH_SIZE]; | |
139 | char tmp[UTIL_PATH_SIZE]; | |
140 | char *cpos; | |
141 | ||
142 | strscpy(result, sizeof(result), event->program_result); | |
143 | cpos = result; | |
144 | while (--i) { | |
145 | while (cpos[0] != '\0' && !isspace(cpos[0])) | |
146 | cpos++; | |
147 | while (isspace(cpos[0])) | |
148 | cpos++; | |
149 | if (cpos[0] == '\0') | |
150 | break; | |
151 | } | |
152 | if (i > 0) { | |
153 | log_error("requested part of result string not found"); | |
154 | break; | |
155 | } | |
156 | strscpy(tmp, sizeof(tmp), cpos); | |
157 | /* %{2+}c copies the whole string from the second part on */ | |
158 | if (rest[0] != '+') { | |
159 | cpos = strchr(tmp, ' '); | |
160 | if (cpos) | |
161 | cpos[0] = '\0'; | |
162 | } | |
163 | l = strpcpy(&s, l, tmp); | |
164 | } else { | |
165 | l = strpcpy(&s, l, event->program_result); | |
166 | } | |
167 | break; | |
168 | } | |
169 | case SUBST_ATTR: { | |
170 | const char *value = NULL; | |
171 | char vbuf[UTIL_NAME_SIZE]; | |
172 | size_t len; | |
173 | int count; | |
174 | ||
175 | if (attr == NULL) { | |
176 | log_error("missing file parameter for attr"); | |
177 | break; | |
178 | } | |
179 | ||
180 | /* try to read the value specified by "[dmi/id]product_name" */ | |
755c3fe9 | 181 | if (util_resolve_subsys_kernel(attr, vbuf, sizeof(vbuf), 1) == 0) |
0d53705b DS |
182 | value = vbuf; |
183 | ||
184 | /* try to read the attribute the device */ | |
185 | if (value == NULL) | |
186 | value = udev_device_get_sysattr_value(event->dev, attr); | |
187 | ||
188 | /* try to read the attribute of the parent device, other matches have selected */ | |
189 | if (value == NULL && event->dev_parent != NULL && event->dev_parent != event->dev) | |
190 | value = udev_device_get_sysattr_value(event->dev_parent, attr); | |
191 | ||
192 | if (value == NULL) | |
193 | break; | |
194 | ||
195 | /* strip trailing whitespace, and replace unwanted characters */ | |
196 | if (value != vbuf) | |
197 | strscpy(vbuf, sizeof(vbuf), value); | |
198 | len = strlen(vbuf); | |
199 | while (len > 0 && isspace(vbuf[--len])) | |
200 | vbuf[len] = '\0'; | |
201 | count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT); | |
202 | if (count > 0) | |
203 | log_debug("%i character(s) replaced" , count); | |
204 | l = strpcpy(&s, l, vbuf); | |
205 | break; | |
206 | } | |
207 | case SUBST_PARENT: { | |
208 | struct udev_device *dev_parent; | |
209 | const char *devnode; | |
210 | ||
211 | dev_parent = udev_device_get_parent(event->dev); | |
212 | if (dev_parent == NULL) | |
213 | break; | |
214 | devnode = udev_device_get_devnode(dev_parent); | |
215 | if (devnode != NULL) | |
fbd0b64f | 216 | l = strpcpy(&s, l, devnode + STRLEN("/dev/")); |
0d53705b DS |
217 | break; |
218 | } | |
219 | case SUBST_DEVNODE: | |
220 | if (udev_device_get_devnode(dev) != NULL) | |
221 | l = strpcpy(&s, l, udev_device_get_devnode(dev)); | |
222 | break; | |
223 | case SUBST_NAME: | |
224 | if (event->name != NULL) | |
225 | l = strpcpy(&s, l, event->name); | |
226 | else if (udev_device_get_devnode(dev) != NULL) | |
fbd0b64f LP |
227 | l = strpcpy(&s, l, |
228 | udev_device_get_devnode(dev) + STRLEN("/dev/")); | |
0d53705b DS |
229 | else |
230 | l = strpcpy(&s, l, udev_device_get_sysname(dev)); | |
231 | break; | |
232 | case SUBST_LINKS: { | |
233 | struct udev_list_entry *list_entry; | |
234 | ||
235 | list_entry = udev_device_get_devlinks_list_entry(dev); | |
236 | if (list_entry == NULL) | |
237 | break; | |
fbd0b64f LP |
238 | l = strpcpy(&s, l, |
239 | udev_list_entry_get_name(list_entry) + STRLEN("/dev/")); | |
0d53705b | 240 | udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) |
fbd0b64f LP |
241 | l = strpcpyl(&s, l, " ", |
242 | udev_list_entry_get_name(list_entry) + STRLEN("/dev/"), | |
243 | NULL); | |
0d53705b DS |
244 | break; |
245 | } | |
246 | case SUBST_ROOT: | |
247 | l = strpcpy(&s, l, "/dev"); | |
248 | break; | |
249 | case SUBST_SYS: | |
250 | l = strpcpy(&s, l, "/sys"); | |
251 | break; | |
252 | case SUBST_ENV: | |
253 | if (attr == NULL) { | |
254 | break; | |
255 | } else { | |
256 | const char *value; | |
257 | ||
258 | value = udev_device_get_property_value(event->dev, attr); | |
259 | if (value == NULL) | |
260 | break; | |
261 | l = strpcpy(&s, l, value); | |
262 | break; | |
263 | } | |
264 | default: | |
265 | log_error("unknown substitution type=%i", type); | |
266 | break; | |
267 | } | |
268 | ||
be452683 | 269 | return s - dest; |
0d53705b DS |
270 | } |
271 | ||
e20a9171 DS |
272 | size_t udev_event_apply_format(struct udev_event *event, |
273 | const char *src, char *dest, size_t size, | |
274 | bool replace_whitespace) { | |
912541b0 | 275 | struct udev_device *dev = event->dev; |
912541b0 | 276 | static const struct subst_map { |
04a9d3a0 KS |
277 | const char *name; |
278 | const char fmt; | |
912541b0 KS |
279 | enum subst_type type; |
280 | } map[] = { | |
3fd0c4c6 KS |
281 | { .name = "devnode", .fmt = 'N', .type = SUBST_DEVNODE }, |
282 | { .name = "tempnode", .fmt = 'N', .type = SUBST_DEVNODE }, | |
283 | { .name = "attr", .fmt = 's', .type = SUBST_ATTR }, | |
284 | { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR }, | |
285 | { .name = "env", .fmt = 'E', .type = SUBST_ENV }, | |
286 | { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL }, | |
287 | { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER }, | |
288 | { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER }, | |
289 | { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH }, | |
290 | { .name = "id", .fmt = 'b', .type = SUBST_ID }, | |
291 | { .name = "major", .fmt = 'M', .type = SUBST_MAJOR }, | |
292 | { .name = "minor", .fmt = 'm', .type = SUBST_MINOR }, | |
293 | { .name = "result", .fmt = 'c', .type = SUBST_RESULT }, | |
294 | { .name = "parent", .fmt = 'P', .type = SUBST_PARENT }, | |
295 | { .name = "name", .fmt = 'D', .type = SUBST_NAME }, | |
296 | { .name = "links", .fmt = 'L', .type = SUBST_LINKS }, | |
297 | { .name = "root", .fmt = 'r', .type = SUBST_ROOT }, | |
298 | { .name = "sys", .fmt = 'S', .type = SUBST_SYS }, | |
912541b0 KS |
299 | }; |
300 | const char *from; | |
301 | char *s; | |
302 | size_t l; | |
303 | ||
3b64e4d4 TG |
304 | assert(dev); |
305 | ||
912541b0 KS |
306 | from = src; |
307 | s = dest; | |
308 | l = size; | |
309 | ||
310 | for (;;) { | |
311 | enum subst_type type = SUBST_UNKNOWN; | |
be452683 DS |
312 | char attrbuf[UTIL_PATH_SIZE]; |
313 | char *attr = NULL; | |
314 | size_t subst_len; | |
912541b0 KS |
315 | |
316 | while (from[0] != '\0') { | |
317 | if (from[0] == '$') { | |
318 | /* substitute named variable */ | |
319 | unsigned int i; | |
320 | ||
321 | if (from[1] == '$') { | |
322 | from++; | |
323 | goto copy; | |
324 | } | |
325 | ||
8fef0ff2 | 326 | for (i = 0; i < ELEMENTSOF(map); i++) { |
33502ffe | 327 | if (startswith(&from[1], map[i].name)) { |
912541b0 KS |
328 | type = map[i].type; |
329 | from += strlen(map[i].name)+1; | |
912541b0 KS |
330 | goto subst; |
331 | } | |
332 | } | |
333 | } else if (from[0] == '%') { | |
334 | /* substitute format char */ | |
335 | unsigned int i; | |
336 | ||
337 | if (from[1] == '%') { | |
338 | from++; | |
339 | goto copy; | |
340 | } | |
341 | ||
8fef0ff2 | 342 | for (i = 0; i < ELEMENTSOF(map); i++) { |
912541b0 KS |
343 | if (from[1] == map[i].fmt) { |
344 | type = map[i].type; | |
345 | from += 2; | |
912541b0 KS |
346 | goto subst; |
347 | } | |
348 | } | |
349 | } | |
065db052 | 350 | copy: |
912541b0 | 351 | /* copy char */ |
79a695f2 | 352 | if (l < 2) /* need space for this char and the terminating NUL */ |
912541b0 KS |
353 | goto out; |
354 | s[0] = from[0]; | |
355 | from++; | |
356 | s++; | |
357 | l--; | |
358 | } | |
359 | ||
360 | goto out; | |
065db052 | 361 | subst: |
912541b0 KS |
362 | /* extract possible $format{attr} */ |
363 | if (from[0] == '{') { | |
364 | unsigned int i; | |
365 | ||
366 | from++; | |
79a695f2 | 367 | for (i = 0; from[i] != '}'; i++) |
912541b0 | 368 | if (from[i] == '\0') { |
9f6445e3 | 369 | log_error("missing closing brace for format '%s'", src); |
912541b0 KS |
370 | goto out; |
371 | } | |
79a695f2 | 372 | |
912541b0 KS |
373 | if (i >= sizeof(attrbuf)) |
374 | goto out; | |
375 | memcpy(attrbuf, from, i); | |
376 | attrbuf[i] = '\0'; | |
377 | from += i+1; | |
378 | attr = attrbuf; | |
379 | } else { | |
380 | attr = NULL; | |
381 | } | |
382 | ||
be452683 | 383 | subst_len = subst_format_var(event, dev, type, attr, s, l); |
e20a9171 | 384 | |
be452683 DS |
385 | /* SUBST_RESULT handles spaces itself */ |
386 | if (replace_whitespace && type != SUBST_RESULT) | |
387 | /* util_replace_whitespace can replace in-place, | |
388 | * and does nothing if subst_len == 0 | |
389 | */ | |
390 | subst_len = util_replace_whitespace(s, s, subst_len); | |
e20a9171 | 391 | |
be452683 DS |
392 | s += subst_len; |
393 | l -= subst_len; | |
912541b0 | 394 | } |
065db052 KS |
395 | |
396 | out: | |
79a695f2 | 397 | assert(l >= 1); |
912541b0 | 398 | s[0] = '\0'; |
912541b0 | 399 | return l; |
f1128767 KS |
400 | } |
401 | ||
2181d30a | 402 | static int spawn_exec(struct udev_event *event, |
8314de1d | 403 | const char *cmd, char *const argv[], char **envp, |
9ec6e95b | 404 | int fd_stdout, int fd_stderr) { |
19c784c4 | 405 | _cleanup_close_ int fd = -1; |
f6e0a353 | 406 | int r; |
912541b0 KS |
407 | |
408 | /* discard child output or connect to pipe */ | |
409 | fd = open("/dev/null", O_RDWR); | |
410 | if (fd >= 0) { | |
f6e0a353 TG |
411 | r = dup2(fd, STDIN_FILENO); |
412 | if (r < 0) | |
413 | log_warning_errno(errno, "redirecting stdin failed: %m"); | |
414 | ||
415 | if (fd_stdout < 0) { | |
416 | r = dup2(fd, STDOUT_FILENO); | |
417 | if (r < 0) | |
418 | log_warning_errno(errno, "redirecting stdout failed: %m"); | |
419 | } | |
420 | ||
421 | if (fd_stderr < 0) { | |
422 | r = dup2(fd, STDERR_FILENO); | |
423 | if (r < 0) | |
424 | log_warning_errno(errno, "redirecting stderr failed: %m"); | |
425 | } | |
19c784c4 | 426 | } else |
f6e0a353 | 427 | log_warning_errno(errno, "open /dev/null failed: %m"); |
912541b0 KS |
428 | |
429 | /* connect pipes to std{out,err} */ | |
430 | if (fd_stdout >= 0) { | |
f6e0a353 TG |
431 | r = dup2(fd_stdout, STDOUT_FILENO); |
432 | if (r < 0) | |
433 | log_warning_errno(errno, "redirecting stdout failed: %m"); | |
434 | ||
435 | fd_stdout = safe_close(fd_stdout); | |
912541b0 | 436 | } |
f6e0a353 | 437 | |
912541b0 | 438 | if (fd_stderr >= 0) { |
f6e0a353 TG |
439 | r = dup2(fd_stderr, STDERR_FILENO); |
440 | if (r < 0) | |
441 | log_warning_errno(errno, "redirecting stdout failed: %m"); | |
442 | ||
443 | fd_stderr = safe_close(fd_stderr); | |
912541b0 KS |
444 | } |
445 | ||
446 | /* terminate child in case parent goes away */ | |
447 | prctl(PR_SET_PDEATHSIG, SIGTERM); | |
448 | ||
8314de1d TG |
449 | /* restore sigmask before exec */ |
450 | (void) reset_signal_mask(); | |
912541b0 KS |
451 | |
452 | execve(argv[0], argv, envp); | |
453 | ||
454 | /* exec failed */ | |
e1427b13 | 455 | return log_error_errno(errno, "failed to execute '%s' '%s': %m", argv[0], cmd); |
2181d30a KS |
456 | } |
457 | ||
f18f225c | 458 | static void spawn_read(struct udev_event *event, |
dd5eddd2 KS |
459 | usec_t timeout_usec, |
460 | const char *cmd, | |
461 | int fd_stdout, int fd_stderr, | |
9ec6e95b | 462 | char *result, size_t ressize) { |
4cd5d5ad TG |
463 | _cleanup_close_ int fd_ep = -1; |
464 | struct epoll_event ep_outpipe = { | |
465 | .events = EPOLLIN, | |
466 | .data.ptr = &fd_stdout, | |
467 | }; | |
468 | struct epoll_event ep_errpipe = { | |
469 | .events = EPOLLIN, | |
470 | .data.ptr = &fd_stderr, | |
471 | }; | |
912541b0 | 472 | size_t respos = 0; |
4cd5d5ad | 473 | int r; |
912541b0 KS |
474 | |
475 | /* read from child if requested */ | |
476 | if (fd_stdout < 0 && fd_stderr < 0) | |
477 | return; | |
478 | ||
479 | fd_ep = epoll_create1(EPOLL_CLOEXEC); | |
480 | if (fd_ep < 0) { | |
56f64d95 | 481 | log_error_errno(errno, "error creating epoll fd: %m"); |
4cd5d5ad | 482 | return; |
912541b0 KS |
483 | } |
484 | ||
485 | if (fd_stdout >= 0) { | |
4cd5d5ad TG |
486 | r = epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_stdout, &ep_outpipe); |
487 | if (r < 0) { | |
56f64d95 | 488 | log_error_errno(errno, "fail to add stdout fd to epoll: %m"); |
4cd5d5ad | 489 | return; |
912541b0 KS |
490 | } |
491 | } | |
492 | ||
493 | if (fd_stderr >= 0) { | |
4cd5d5ad TG |
494 | r = epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_stderr, &ep_errpipe); |
495 | if (r < 0) { | |
56f64d95 | 496 | log_error_errno(errno, "fail to add stderr fd to epoll: %m"); |
4cd5d5ad | 497 | return; |
912541b0 KS |
498 | } |
499 | } | |
500 | ||
501 | /* read child output */ | |
502 | while (fd_stdout >= 0 || fd_stderr >= 0) { | |
503 | int timeout; | |
504 | int fdcount; | |
505 | struct epoll_event ev[4]; | |
506 | int i; | |
507 | ||
dd5eddd2 | 508 | if (timeout_usec > 0) { |
40fe8b11 | 509 | usec_t age_usec; |
912541b0 | 510 | |
3285baa8 | 511 | age_usec = now(CLOCK_MONOTONIC) - event->birth_usec; |
dd5eddd2 | 512 | if (age_usec >= timeout_usec) { |
9f6445e3 | 513 | log_error("timeout '%s'", cmd); |
4cd5d5ad | 514 | return; |
912541b0 | 515 | } |
dd5eddd2 | 516 | timeout = ((timeout_usec - age_usec) / USEC_PER_MSEC) + MSEC_PER_SEC; |
912541b0 KS |
517 | } else { |
518 | timeout = -1; | |
519 | } | |
520 | ||
8fef0ff2 | 521 | fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout); |
912541b0 KS |
522 | if (fdcount < 0) { |
523 | if (errno == EINTR) | |
524 | continue; | |
56f64d95 | 525 | log_error_errno(errno, "failed to poll: %m"); |
4cd5d5ad TG |
526 | return; |
527 | } else if (fdcount == 0) { | |
9f6445e3 | 528 | log_error("timeout '%s'", cmd); |
4cd5d5ad | 529 | return; |
912541b0 KS |
530 | } |
531 | ||
532 | for (i = 0; i < fdcount; i++) { | |
533 | int *fd = (int *)ev[i].data.ptr; | |
534 | ||
3f796750 TG |
535 | if (*fd < 0) |
536 | continue; | |
537 | ||
912541b0 KS |
538 | if (ev[i].events & EPOLLIN) { |
539 | ssize_t count; | |
540 | char buf[4096]; | |
541 | ||
542 | count = read(*fd, buf, sizeof(buf)-1); | |
543 | if (count <= 0) | |
544 | continue; | |
545 | buf[count] = '\0'; | |
546 | ||
547 | /* store stdout result */ | |
548 | if (result != NULL && *fd == fd_stdout) { | |
549 | if (respos + count < ressize) { | |
550 | memcpy(&result[respos], buf, count); | |
551 | respos += count; | |
552 | } else { | |
1fa2f38f | 553 | log_error("'%s' ressize %zu too short", cmd, ressize); |
912541b0 KS |
554 | } |
555 | } | |
556 | ||
557 | /* log debug output only if we watch stderr */ | |
558 | if (fd_stderr >= 0) { | |
559 | char *pos; | |
560 | char *line; | |
561 | ||
562 | pos = buf; | |
563 | while ((line = strsep(&pos, "\n"))) { | |
564 | if (pos != NULL || line[0] != '\0') | |
9f6445e3 | 565 | log_debug("'%s'(%s) '%s'", cmd, *fd == fd_stdout ? "out" : "err" , line); |
912541b0 KS |
566 | } |
567 | } | |
568 | } else if (ev[i].events & EPOLLHUP) { | |
4cd5d5ad TG |
569 | r = epoll_ctl(fd_ep, EPOLL_CTL_DEL, *fd, NULL); |
570 | if (r < 0) { | |
56f64d95 | 571 | log_error_errno(errno, "failed to remove fd from epoll: %m"); |
4cd5d5ad | 572 | return; |
912541b0 KS |
573 | } |
574 | *fd = -1; | |
575 | } | |
576 | } | |
577 | } | |
578 | ||
579 | /* return the child's stdout string */ | |
baa30fbc | 580 | if (result != NULL) |
912541b0 | 581 | result[respos] = '\0'; |
2181d30a KS |
582 | } |
583 | ||
8128f229 TG |
584 | static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
585 | Spawn *spawn = userdata; | |
586 | char timeout[FORMAT_TIMESTAMP_RELATIVE_MAX]; | |
912541b0 | 587 | |
8128f229 | 588 | assert(spawn); |
912541b0 | 589 | |
8128f229 | 590 | kill_and_sigcont(spawn->pid, SIGKILL); |
912541b0 | 591 | |
8128f229 TG |
592 | log_error("spawned process '%s' ["PID_FMT"] timed out after %s, killing", spawn->cmd, spawn->pid, |
593 | format_timestamp_relative(timeout, sizeof(timeout), spawn->timeout)); | |
912541b0 | 594 | |
8128f229 TG |
595 | return 1; |
596 | } | |
67117413 | 597 | |
8128f229 TG |
598 | static int on_spawn_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { |
599 | Spawn *spawn = userdata; | |
600 | char timeout[FORMAT_TIMESTAMP_RELATIVE_MAX]; | |
912541b0 | 601 | |
8128f229 | 602 | assert(spawn); |
67117413 | 603 | |
8128f229 TG |
604 | log_warning("spawned process '%s' ["PID_FMT"] is taking longer than %s to complete", spawn->cmd, spawn->pid, |
605 | format_timestamp_relative(timeout, sizeof(timeout), spawn->timeout)); | |
606 | ||
607 | return 1; | |
608 | } | |
609 | ||
610 | static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) { | |
611 | Spawn *spawn = userdata; | |
612 | ||
613 | assert(spawn); | |
614 | ||
615 | switch (si->si_code) { | |
616 | case CLD_EXITED: | |
53318514 TG |
617 | if (si->si_status == 0) { |
618 | log_debug("Process '%s' succeeded.", spawn->cmd); | |
8128f229 TG |
619 | sd_event_exit(sd_event_source_get_event(s), 0); |
620 | ||
621 | return 1; | |
53318514 TG |
622 | } else if (spawn->accept_failure) |
623 | log_debug("Process '%s' failed with exit code %i.", spawn->cmd, si->si_status); | |
624 | else | |
625 | log_warning("Process '%s' failed with exit code %i.", spawn->cmd, si->si_status); | |
912541b0 | 626 | |
8128f229 TG |
627 | break; |
628 | case CLD_KILLED: | |
629 | case CLD_DUMPED: | |
53318514 | 630 | log_warning("Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status)); |
912541b0 | 631 | |
8128f229 TG |
632 | break; |
633 | default: | |
53318514 | 634 | log_error("Process '%s' failed due to unknown reason.", spawn->cmd); |
8128f229 | 635 | } |
912541b0 | 636 | |
8128f229 TG |
637 | sd_event_exit(sd_event_source_get_event(s), -EIO); |
638 | ||
639 | return 1; | |
640 | } | |
641 | ||
642 | static int spawn_wait(struct udev_event *event, | |
643 | usec_t timeout_usec, | |
644 | usec_t timeout_warn_usec, | |
53318514 TG |
645 | const char *cmd, pid_t pid, |
646 | bool accept_failure) { | |
8128f229 TG |
647 | Spawn spawn = { |
648 | .cmd = cmd, | |
649 | .pid = pid, | |
53318514 | 650 | .accept_failure = accept_failure, |
8128f229 | 651 | }; |
4afd3348 | 652 | _cleanup_(sd_event_unrefp) sd_event *e = NULL; |
8128f229 TG |
653 | int r, ret; |
654 | ||
655 | r = sd_event_new(&e); | |
656 | if (r < 0) | |
657 | return r; | |
658 | ||
659 | if (timeout_usec > 0) { | |
660 | usec_t usec, age_usec; | |
661 | ||
3285baa8 | 662 | usec = now(CLOCK_MONOTONIC); |
8128f229 TG |
663 | age_usec = usec - event->birth_usec; |
664 | if (age_usec < timeout_usec) { | |
665 | if (timeout_warn_usec > 0 && timeout_warn_usec < timeout_usec && age_usec < timeout_warn_usec) { | |
666 | spawn.timeout_warn = timeout_warn_usec - age_usec; | |
667 | ||
3285baa8 | 668 | r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
920b52e4 TA |
669 | usec + spawn.timeout_warn, USEC_PER_SEC, |
670 | on_spawn_timeout_warning, &spawn); | |
8128f229 TG |
671 | if (r < 0) |
672 | return r; | |
912541b0 | 673 | } |
8128f229 TG |
674 | |
675 | spawn.timeout = timeout_usec - age_usec; | |
676 | ||
3285baa8 | 677 | r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC, |
8128f229 TG |
678 | usec + spawn.timeout, USEC_PER_SEC, on_spawn_timeout, &spawn); |
679 | if (r < 0) | |
680 | return r; | |
912541b0 KS |
681 | } |
682 | } | |
8128f229 TG |
683 | |
684 | r = sd_event_add_child(e, NULL, pid, WEXITED, on_spawn_sigchld, &spawn); | |
685 | if (r < 0) | |
686 | return r; | |
687 | ||
688 | r = sd_event_loop(e); | |
689 | if (r < 0) | |
690 | return r; | |
691 | ||
692 | r = sd_event_get_exit_code(e, &ret); | |
693 | if (r < 0) | |
694 | return r; | |
695 | ||
696 | return ret; | |
2181d30a KS |
697 | } |
698 | ||
2024ed61 | 699 | int udev_build_argv(char *cmd, int *argc, char *argv[]) { |
912541b0 KS |
700 | int i = 0; |
701 | char *pos; | |
702 | ||
703 | if (strchr(cmd, ' ') == NULL) { | |
704 | argv[i++] = cmd; | |
705 | goto out; | |
706 | } | |
707 | ||
708 | pos = cmd; | |
709 | while (pos != NULL && pos[0] != '\0') { | |
7e760b79 FB |
710 | if (IN_SET(pos[0], '\'', '"')) { |
711 | /* do not separate quotes or double quotes */ | |
712 | char delim[2] = { pos[0], '\0' }; | |
713 | ||
912541b0 | 714 | pos++; |
7e760b79 | 715 | argv[i] = strsep(&pos, delim); |
912541b0 KS |
716 | if (pos != NULL) |
717 | while (pos[0] == ' ') | |
718 | pos++; | |
719 | } else { | |
720 | argv[i] = strsep(&pos, " "); | |
721 | if (pos != NULL) | |
722 | while (pos[0] == ' ') | |
723 | pos++; | |
724 | } | |
912541b0 KS |
725 | i++; |
726 | } | |
e216e514 | 727 | out: |
912541b0 KS |
728 | argv[i] = NULL; |
729 | if (argc) | |
730 | *argc = i; | |
731 | return 0; | |
e216e514 KS |
732 | } |
733 | ||
2181d30a | 734 | int udev_event_spawn(struct udev_event *event, |
dd5eddd2 | 735 | usec_t timeout_usec, |
67117413 | 736 | usec_t timeout_warn_usec, |
53318514 | 737 | bool accept_failure, |
bbf35206 | 738 | const char *cmd, |
dd5eddd2 | 739 | char *result, size_t ressize) { |
912541b0 KS |
740 | int outpipe[2] = {-1, -1}; |
741 | int errpipe[2] = {-1, -1}; | |
742 | pid_t pid; | |
912541b0 KS |
743 | int err = 0; |
744 | ||
912541b0 | 745 | /* pipes from child to parent */ |
25e773ee | 746 | if (result != NULL || log_get_max_level() >= LOG_INFO) { |
912541b0 | 747 | if (pipe2(outpipe, O_NONBLOCK) != 0) { |
76ef789d | 748 | err = log_error_errno(errno, "pipe failed: %m"); |
912541b0 KS |
749 | goto out; |
750 | } | |
751 | } | |
25e773ee | 752 | if (log_get_max_level() >= LOG_INFO) { |
912541b0 | 753 | if (pipe2(errpipe, O_NONBLOCK) != 0) { |
76ef789d | 754 | err = log_error_errno(errno, "pipe failed: %m"); |
912541b0 KS |
755 | goto out; |
756 | } | |
757 | } | |
758 | ||
b6e1fff1 LP |
759 | err = safe_fork("(spawn)", FORK_RESET_SIGNALS|FORK_LOG, &pid); |
760 | if (err < 0) | |
4c253ed1 | 761 | goto out; |
4c253ed1 | 762 | if (err == 0) { |
bbf35206 TG |
763 | char arg[UTIL_PATH_SIZE]; |
764 | char *argv[128]; | |
765 | char program[UTIL_PATH_SIZE]; | |
766 | ||
912541b0 | 767 | /* child closes parent's ends of pipes */ |
7f6e12b0 LP |
768 | outpipe[READ_END] = safe_close(outpipe[READ_END]); |
769 | errpipe[READ_END] = safe_close(errpipe[READ_END]); | |
912541b0 | 770 | |
bbf35206 | 771 | strscpy(arg, sizeof(arg), cmd); |
2024ed61 | 772 | udev_build_argv(arg, NULL, argv); |
bbf35206 TG |
773 | |
774 | /* allow programs in /usr/lib/udev/ to be called without the path */ | |
775 | if (argv[0][0] != '/') { | |
776 | strscpyl(program, sizeof(program), UDEVLIBEXECDIR "/", argv[0], NULL); | |
777 | argv[0] = program; | |
778 | } | |
779 | ||
9f6445e3 | 780 | log_debug("starting '%s'", cmd); |
912541b0 | 781 | |
bbf35206 | 782 | spawn_exec(event, cmd, argv, udev_device_get_properties_envp(event->dev), |
b49d9b50 | 783 | outpipe[WRITE_END], errpipe[WRITE_END]); |
912541b0 | 784 | |
bbf35206 TG |
785 | _exit(2); |
786 | } | |
912541b0 | 787 | |
4c253ed1 LP |
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]); | |
912541b0 | 791 | |
4c253ed1 LP |
792 | spawn_read(event, |
793 | timeout_usec, | |
794 | cmd, | |
795 | outpipe[READ_END], errpipe[READ_END], | |
796 | result, ressize); | |
797 | ||
798 | err = spawn_wait(event, timeout_usec, timeout_warn_usec, cmd, pid, accept_failure); | |
2181d30a KS |
799 | |
800 | out: | |
912541b0 KS |
801 | if (outpipe[READ_END] >= 0) |
802 | close(outpipe[READ_END]); | |
803 | if (outpipe[WRITE_END] >= 0) | |
804 | close(outpipe[WRITE_END]); | |
805 | if (errpipe[READ_END] >= 0) | |
806 | close(errpipe[READ_END]); | |
807 | if (errpipe[WRITE_END] >= 0) | |
808 | close(errpipe[WRITE_END]); | |
809 | return err; | |
2181d30a KS |
810 | } |
811 | ||
9ec6e95b | 812 | static int rename_netif(struct udev_event *event) { |
912541b0 | 813 | struct udev_device *dev = event->dev; |
16d26d55 TG |
814 | char name[IFNAMSIZ]; |
815 | const char *oldname; | |
816 | int r; | |
817 | ||
818 | oldname = udev_device_get_sysname(dev); | |
912541b0 | 819 | |
16d26d55 | 820 | strscpy(name, IFNAMSIZ, event->name); |
912541b0 | 821 | |
4c83d994 | 822 | r = rtnl_set_link_name(&event->rtnl, udev_device_get_ifindex(dev), name); |
f647962d MS |
823 | if (r < 0) |
824 | return log_error_errno(r, "Error changing net interface name '%s' to '%s': %m", oldname, name); | |
16d26d55 | 825 | |
ff49bc32 | 826 | log_debug("renamed network interface '%s' to '%s'", oldname, name); |
16d26d55 | 827 | |
4c83d994 | 828 | return 0; |
d46f37fd KS |
829 | } |
830 | ||
dd5eddd2 | 831 | void udev_event_execute_rules(struct udev_event *event, |
adeba500 KS |
832 | usec_t timeout_usec, usec_t timeout_warn_usec, |
833 | struct udev_list *properties_list, | |
8314de1d | 834 | struct udev_rules *rules) { |
912541b0 | 835 | struct udev_device *dev = event->dev; |
912541b0 KS |
836 | |
837 | if (udev_device_get_subsystem(dev) == NULL) | |
1ea97217 | 838 | return; |
912541b0 | 839 | |
090be865 | 840 | if (streq(udev_device_get_action(dev), "remove")) { |
107f2e25 TG |
841 | udev_device_read_db(dev); |
842 | udev_device_tag_index(dev, NULL, false); | |
843 | udev_device_delete_db(dev); | |
844 | ||
912541b0 | 845 | if (major(udev_device_get_devnum(dev)) != 0) |
2024ed61 | 846 | udev_watch_end(dev); |
912541b0 | 847 | |
adeba500 KS |
848 | udev_rules_apply_to_event(rules, event, |
849 | timeout_usec, timeout_warn_usec, | |
8314de1d | 850 | properties_list); |
912541b0 KS |
851 | |
852 | if (major(udev_device_get_devnum(dev)) != 0) | |
e7f32890 | 853 | udev_node_remove(dev); |
912541b0 | 854 | } else { |
8f0f13f0 | 855 | event->dev_db = udev_device_clone_with_db(dev); |
912541b0 | 856 | if (event->dev_db != NULL) { |
912541b0 KS |
857 | /* disable watch during event processing */ |
858 | if (major(udev_device_get_devnum(dev)) != 0) | |
2024ed61 | 859 | udev_watch_end(event->dev_db); |
912541b0 | 860 | |
9eba69df ZJS |
861 | if (major(udev_device_get_devnum(dev)) == 0 && |
862 | streq(udev_device_get_action(dev), "move")) | |
863 | udev_device_copy_properties(dev, event->dev_db); | |
864 | } | |
b081b27e | 865 | |
adeba500 KS |
866 | udev_rules_apply_to_event(rules, event, |
867 | timeout_usec, timeout_warn_usec, | |
8314de1d | 868 | properties_list); |
912541b0 KS |
869 | |
870 | /* rename a new network interface, if needed */ | |
090be865 TA |
871 | if (udev_device_get_ifindex(dev) > 0 && streq(udev_device_get_action(dev), "add") && |
872 | event->name != NULL && !streq(event->name, udev_device_get_sysname(dev))) { | |
1ea97217 | 873 | int r; |
912541b0 | 874 | |
1ea97217 | 875 | r = rename_netif(event); |
243d1825 TG |
876 | if (r < 0) |
877 | log_warning_errno(r, "could not rename interface '%d' from '%s' to '%s': %m", udev_device_get_ifindex(dev), | |
878 | udev_device_get_sysname(dev), event->name); | |
879 | else { | |
243d1825 TG |
880 | r = udev_device_rename(dev, event->name); |
881 | if (r < 0) | |
882 | log_warning_errno(r, "renamed interface '%d' from '%s' to '%s', but could not update udev_device: %m", | |
883 | udev_device_get_ifindex(dev), udev_device_get_sysname(dev), event->name); | |
3738cc85 | 884 | else |
9f6445e3 | 885 | log_debug("changed devpath to '%s'", udev_device_get_devpath(dev)); |
912541b0 KS |
886 | } |
887 | } | |
888 | ||
b0a00806 | 889 | if (major(udev_device_get_devnum(dev)) > 0) { |
9a8ae49d KS |
890 | bool apply; |
891 | ||
912541b0 KS |
892 | /* remove/update possible left-over symlinks from old database entry */ |
893 | if (event->dev_db != NULL) | |
894 | udev_node_update_old_links(dev, event->dev_db); | |
895 | ||
1edefa4f KS |
896 | if (!event->owner_set) |
897 | event->uid = udev_device_get_devnode_uid(dev); | |
898 | ||
899 | if (!event->group_set) | |
900 | event->gid = udev_device_get_devnode_gid(dev); | |
901 | ||
912541b0 KS |
902 | if (!event->mode_set) { |
903 | if (udev_device_get_devnode_mode(dev) > 0) { | |
904 | /* kernel supplied value */ | |
905 | event->mode = udev_device_get_devnode_mode(dev); | |
906 | } else if (event->gid > 0) { | |
907 | /* default 0660 if a group is assigned */ | |
908 | event->mode = 0660; | |
909 | } else { | |
910 | /* default 0600 */ | |
911 | event->mode = 0600; | |
912 | } | |
913 | } | |
914 | ||
9a8ae49d | 915 | apply = streq(udev_device_get_action(dev), "add") || event->owner_set || event->group_set || event->mode_set; |
c26547d6 | 916 | udev_node_add(dev, apply, event->mode, event->uid, event->gid, &event->seclabel_list); |
912541b0 KS |
917 | } |
918 | ||
919 | /* preserve old, or get new initialization timestamp */ | |
1b14c3cf | 920 | udev_device_ensure_usec_initialized(event->dev, event->dev_db); |
912541b0 KS |
921 | |
922 | /* (re)write database file */ | |
912541b0 | 923 | udev_device_tag_index(dev, event->dev_db, true); |
353f6058 | 924 | udev_device_update_db(dev); |
912541b0 KS |
925 | udev_device_set_is_initialized(dev); |
926 | ||
353f6058 | 927 | event->dev_db = udev_device_unref(event->dev_db); |
912541b0 | 928 | } |
d46f37fd | 929 | } |
2d73813e | 930 | |
8314de1d | 931 | void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec, usec_t timeout_warn_usec) { |
912541b0 | 932 | struct udev_list_entry *list_entry; |
912541b0 | 933 | |
912541b0 | 934 | udev_list_entry_foreach(list_entry, udev_list_get_entry(&event->run_list)) { |
bbf35206 | 935 | char command[UTIL_PATH_SIZE]; |
912541b0 | 936 | const char *cmd = udev_list_entry_get_name(list_entry); |
83cd6b75 KS |
937 | enum udev_builtin_cmd builtin_cmd = udev_list_entry_get_num(list_entry); |
938 | ||
e20a9171 | 939 | udev_event_apply_format(event, cmd, command, sizeof(command), false); |
912541b0 | 940 | |
bbf35206 | 941 | if (builtin_cmd < UDEV_BUILTIN_MAX) |
83cd6b75 | 942 | udev_builtin_run(event->dev, builtin_cmd, command, false); |
bbf35206 | 943 | else { |
912541b0 | 944 | if (event->exec_delay > 0) { |
bbf35206 | 945 | log_debug("delay execution of '%s'", command); |
912541b0 KS |
946 | sleep(event->exec_delay); |
947 | } | |
948 | ||
bbf35206 | 949 | udev_event_spawn(event, timeout_usec, timeout_warn_usec, false, command, NULL, 0); |
912541b0 KS |
950 | } |
951 | } | |
2d73813e | 952 | } |