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