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