]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-event.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / udev / udev-event.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2
3 #include <ctype.h>
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <net/if.h>
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "sd-event.h"
13
14 #include "alloc-util.h"
15 #include "device-private.h"
16 #include "device-util.h"
17 #include "fd-util.h"
18 #include "format-util.h"
19 #include "libudev-private.h"
20 #include "netlink-util.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "signal-util.h"
24 #include "stdio-util.h"
25 #include "string-util.h"
26 #include "udev-builtin.h"
27 #include "udev-node.h"
28 #include "udev-watch.h"
29 #include "udev.h"
30
31 typedef struct Spawn {
32 const char *cmd;
33 pid_t pid;
34 usec_t timeout_warn_usec;
35 usec_t timeout_usec;
36 usec_t event_birth_usec;
37 bool accept_failure;
38 int fd_stdout;
39 int fd_stderr;
40 char *result;
41 size_t result_size;
42 size_t result_len;
43 } Spawn;
44
45 struct udev_event *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rtnl) {
46 struct udev_event *event;
47
48 assert(dev);
49
50 event = new(struct udev_event, 1);
51 if (!event)
52 return NULL;
53
54 *event = (struct udev_event) {
55 .dev = sd_device_ref(dev),
56 .birth_usec = now(CLOCK_MONOTONIC),
57 .exec_delay_usec = exec_delay_usec,
58 .rtnl = sd_netlink_ref(rtnl),
59 };
60
61 return event;
62 }
63
64 struct udev_event *udev_event_free(struct udev_event *event) {
65 void *p;
66
67 if (!event)
68 return NULL;
69
70 sd_device_unref(event->dev);
71 sd_device_unref(event->dev_db_clone);
72 sd_netlink_unref(event->rtnl);
73 while ((p = hashmap_steal_first_key(event->run_list)))
74 free(p);
75 hashmap_free(event->run_list);
76 hashmap_free_free_free(event->seclabel_list);
77 free(event->program_result);
78 free(event->name);
79
80 return mfree(event);
81 }
82
83 enum subst_type {
84 SUBST_DEVNODE,
85 SUBST_ATTR,
86 SUBST_ENV,
87 SUBST_KERNEL,
88 SUBST_KERNEL_NUMBER,
89 SUBST_DRIVER,
90 SUBST_DEVPATH,
91 SUBST_ID,
92 SUBST_MAJOR,
93 SUBST_MINOR,
94 SUBST_RESULT,
95 SUBST_PARENT,
96 SUBST_NAME,
97 SUBST_LINKS,
98 SUBST_ROOT,
99 SUBST_SYS,
100 };
101
102 struct subst_map_entry {
103 const char *name;
104 const char fmt;
105 enum subst_type type;
106 };
107
108 static const struct subst_map_entry map[] = {
109 { .name = "devnode", .fmt = 'N', .type = SUBST_DEVNODE },
110 { .name = "tempnode", .fmt = 'N', .type = SUBST_DEVNODE },
111 { .name = "attr", .fmt = 's', .type = SUBST_ATTR },
112 { .name = "sysfs", .fmt = 's', .type = SUBST_ATTR },
113 { .name = "env", .fmt = 'E', .type = SUBST_ENV },
114 { .name = "kernel", .fmt = 'k', .type = SUBST_KERNEL },
115 { .name = "number", .fmt = 'n', .type = SUBST_KERNEL_NUMBER },
116 { .name = "driver", .fmt = 'd', .type = SUBST_DRIVER },
117 { .name = "devpath", .fmt = 'p', .type = SUBST_DEVPATH },
118 { .name = "id", .fmt = 'b', .type = SUBST_ID },
119 { .name = "major", .fmt = 'M', .type = SUBST_MAJOR },
120 { .name = "minor", .fmt = 'm', .type = SUBST_MINOR },
121 { .name = "result", .fmt = 'c', .type = SUBST_RESULT },
122 { .name = "parent", .fmt = 'P', .type = SUBST_PARENT },
123 { .name = "name", .fmt = 'D', .type = SUBST_NAME },
124 { .name = "links", .fmt = 'L', .type = SUBST_LINKS },
125 { .name = "root", .fmt = 'r', .type = SUBST_ROOT },
126 { .name = "sys", .fmt = 'S', .type = SUBST_SYS },
127 };
128
129 static ssize_t subst_format_var(struct udev_event *event,
130 const struct subst_map_entry *entry, char *attr,
131 char *dest, size_t l) {
132 sd_device *parent, *dev = event->dev;
133 const char *val = NULL;
134 char *s = dest;
135 dev_t devnum;
136 int r;
137
138 assert(entry);
139
140 switch (entry->type) {
141 case SUBST_DEVPATH:
142 r = sd_device_get_devpath(dev, &val);
143 if (r < 0)
144 return r;
145 l = strpcpy(&s, l, val);
146 break;
147 case SUBST_KERNEL:
148 r = sd_device_get_sysname(dev, &val);
149 if (r < 0)
150 return r;
151 l = strpcpy(&s, l, val);
152 break;
153 case SUBST_KERNEL_NUMBER:
154 r = sd_device_get_sysnum(dev, &val);
155 if (r < 0)
156 return r == -ENOENT ? 0 : r;
157 l = strpcpy(&s, l, val);
158 break;
159 case SUBST_ID:
160 if (!event->dev_parent)
161 return 0;
162 r = sd_device_get_sysname(event->dev_parent, &val);
163 if (r < 0)
164 return r;
165 l = strpcpy(&s, l, val);
166 break;
167 case SUBST_DRIVER:
168 if (!event->dev_parent)
169 return 0;
170 r = sd_device_get_driver(event->dev_parent, &val);
171 if (r < 0)
172 return r == -ENOENT ? 0 : r;
173 l = strpcpy(&s, l, val);
174 break;
175 case SUBST_MAJOR:
176 case SUBST_MINOR: {
177 char buf[DECIMAL_STR_MAX(unsigned)];
178
179 r = sd_device_get_devnum(dev, &devnum);
180 if (r < 0 && r != -ENOENT)
181 return r;
182 xsprintf(buf, "%u", r < 0 ? 0 : entry->type == SUBST_MAJOR ? major(devnum) : minor(devnum));
183 l = strpcpy(&s, l, buf);
184 break;
185 }
186 case SUBST_RESULT: {
187 char *rest;
188 int i;
189
190 if (!event->program_result)
191 return 0;
192
193 /* get part of the result string */
194 i = 0;
195 if (attr)
196 i = strtoul(attr, &rest, 10);
197 if (i > 0) {
198 char result[UTIL_PATH_SIZE], tmp[UTIL_PATH_SIZE], *cpos;
199
200 strscpy(result, sizeof(result), event->program_result);
201 cpos = result;
202 while (--i) {
203 while (cpos[0] != '\0' && !isspace(cpos[0]))
204 cpos++;
205 while (isspace(cpos[0]))
206 cpos++;
207 if (cpos[0] == '\0')
208 break;
209 }
210 if (i > 0) {
211 log_error("requested part of result string not found");
212 break;
213 }
214 strscpy(tmp, sizeof(tmp), cpos);
215 /* %{2+}c copies the whole string from the second part on */
216 if (rest[0] != '+') {
217 cpos = strchr(tmp, ' ');
218 if (cpos)
219 cpos[0] = '\0';
220 }
221 l = strpcpy(&s, l, tmp);
222 } else
223 l = strpcpy(&s, l, event->program_result);
224 break;
225 }
226 case SUBST_ATTR: {
227 char vbuf[UTIL_NAME_SIZE];
228 size_t len;
229 int count;
230
231 if (!attr)
232 return -EINVAL;
233
234 /* try to read the value specified by "[dmi/id]product_name" */
235 if (util_resolve_subsys_kernel(attr, vbuf, sizeof(vbuf), 1) == 0)
236 val = vbuf;
237
238 /* try to read the attribute the device */
239 if (!val)
240 (void) sd_device_get_sysattr_value(dev, attr, &val);
241
242 /* try to read the attribute of the parent device, other matches have selected */
243 if (!val && event->dev_parent && event->dev_parent != dev)
244 (void) sd_device_get_sysattr_value(event->dev_parent, attr, &val);
245
246 if (!val)
247 return 0;
248
249 /* strip trailing whitespace, and replace unwanted characters */
250 if (val != vbuf)
251 strscpy(vbuf, sizeof(vbuf), val);
252 len = strlen(vbuf);
253 while (len > 0 && isspace(vbuf[--len]))
254 vbuf[len] = '\0';
255 count = util_replace_chars(vbuf, UDEV_ALLOWED_CHARS_INPUT);
256 if (count > 0)
257 log_device_debug(dev, "%i character(s) replaced", count);
258 l = strpcpy(&s, l, vbuf);
259 break;
260 }
261 case SUBST_PARENT:
262 r = sd_device_get_parent(dev, &parent);
263 if (r < 0)
264 return r == -ENODEV ? 0 : r;
265 r = sd_device_get_devname(parent, &val);
266 if (r < 0)
267 return r == -ENOENT ? 0 : r;
268 l = strpcpy(&s, l, val + STRLEN("/dev/"));
269 break;
270 case SUBST_DEVNODE:
271 r = sd_device_get_devname(dev, &val);
272 if (r < 0)
273 return r == -ENOENT ? 0 : r;
274 l = strpcpy(&s, l, val);
275 break;
276 case SUBST_NAME:
277 if (event->name)
278 l = strpcpy(&s, l, event->name);
279 else if (sd_device_get_devname(dev, &val) >= 0)
280 l = strpcpy(&s, l, val + STRLEN("/dev/"));
281 else {
282 r = sd_device_get_sysname(dev, &val);
283 if (r < 0)
284 return r;
285 l = strpcpy(&s, l, val);
286 }
287 break;
288 case SUBST_LINKS:
289 FOREACH_DEVICE_DEVLINK(dev, val)
290 if (s == dest)
291 l = strpcpy(&s, l, val + STRLEN("/dev/"));
292 else
293 l = strpcpyl(&s, l, " ", val + STRLEN("/dev/"), NULL);
294 break;
295 case SUBST_ROOT:
296 l = strpcpy(&s, l, "/dev");
297 break;
298 case SUBST_SYS:
299 l = strpcpy(&s, l, "/sys");
300 break;
301 case SUBST_ENV:
302 if (!attr)
303 return 0;
304 r = sd_device_get_property_value(dev, attr, &val);
305 if (r < 0)
306 return r == -ENOENT ? 0 : r;
307 l = strpcpy(&s, l, val);
308 break;
309 default:
310 assert_not_reached("Unknown format substitution type");
311 }
312
313 return s - dest;
314 }
315
316 ssize_t udev_event_apply_format(struct udev_event *event,
317 const char *src, char *dest, size_t size,
318 bool replace_whitespace) {
319 const char *from;
320 char *s;
321 size_t l;
322
323 assert(event);
324 assert(event->dev);
325 assert(src);
326 assert(dest);
327 assert(size > 0);
328
329 from = src;
330 s = dest;
331 l = size;
332
333 for (;;) {
334 const struct subst_map_entry *entry = NULL;
335 char attrbuf[UTIL_PATH_SIZE], *attr;
336 bool format_dollar = false;
337 ssize_t subst_len;
338
339 while (from[0] != '\0') {
340 if (from[0] == '$') {
341 /* substitute named variable */
342 unsigned i;
343
344 if (from[1] == '$') {
345 from++;
346 goto copy;
347 }
348
349 for (i = 0; i < ELEMENTSOF(map); i++) {
350 if (startswith(&from[1], map[i].name)) {
351 entry = &map[i];
352 from += strlen(map[i].name)+1;
353 format_dollar = true;
354 goto subst;
355 }
356 }
357 } else if (from[0] == '%') {
358 /* substitute format char */
359 unsigned i;
360
361 if (from[1] == '%') {
362 from++;
363 goto copy;
364 }
365
366 for (i = 0; i < ELEMENTSOF(map); i++) {
367 if (from[1] == map[i].fmt) {
368 entry = &map[i];
369 from += 2;
370 goto subst;
371 }
372 }
373 }
374 copy:
375 /* copy char */
376 if (l < 2) /* need space for this char and the terminating NUL */
377 goto out;
378 s[0] = from[0];
379 from++;
380 s++;
381 l--;
382 }
383
384 goto out;
385 subst:
386 /* extract possible $format{attr} */
387 if (from[0] == '{') {
388 unsigned i;
389
390 from++;
391 for (i = 0; from[i] != '}'; i++)
392 if (from[i] == '\0') {
393 log_error("missing closing brace for format '%s'", src);
394 goto out;
395 }
396
397 if (i >= sizeof(attrbuf))
398 goto out;
399 memcpy(attrbuf, from, i);
400 attrbuf[i] = '\0';
401 from += i+1;
402 attr = attrbuf;
403 } else
404 attr = NULL;
405
406 subst_len = subst_format_var(event, entry, attr, s, l);
407 if (subst_len < 0) {
408 if (format_dollar)
409 log_device_warning_errno(event->dev, subst_len, "Failed to substitute variable '$%s', ignoring: %m", entry->name);
410 else
411 log_device_warning_errno(event->dev, subst_len, "Failed to apply format '%%%c', ignoring: %m", entry->fmt);
412
413 continue;
414 }
415
416 /* SUBST_RESULT handles spaces itself */
417 if (replace_whitespace && entry->type != SUBST_RESULT)
418 /* util_replace_whitespace can replace in-place,
419 * and does nothing if subst_len == 0
420 */
421 subst_len = util_replace_whitespace(s, s, subst_len);
422
423 s += subst_len;
424 l -= subst_len;
425 }
426
427 out:
428 assert(l >= 1);
429 s[0] = '\0';
430 return l;
431 }
432
433 static int on_spawn_io(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
434 Spawn *spawn = userdata;
435 char buf[4096], *p;
436 size_t size;
437 ssize_t l;
438
439 assert(spawn);
440 assert(fd == spawn->fd_stdout || fd == spawn->fd_stderr);
441 assert(!spawn->result || spawn->result_len < spawn->result_size);
442
443 if (fd == spawn->fd_stdout && spawn->result) {
444 p = spawn->result + spawn->result_len;
445 size = spawn->result_size - spawn->result_len;
446 } else {
447 p = buf;
448 size = sizeof(buf);
449 }
450
451 l = read(fd, p, size - 1);
452 if (l < 0) {
453 if (errno != EAGAIN)
454 log_error_errno(errno, "Failed to read stdout of '%s': %m", spawn->cmd);
455
456 return 0;
457 }
458
459 p[l] = '\0';
460 if (fd == spawn->fd_stdout && spawn->result)
461 spawn->result_len += l;
462
463 /* Log output only if we watch stderr. */
464 if (l > 0 && spawn->fd_stderr >= 0) {
465 _cleanup_strv_free_ char **v = NULL;
466 char **q;
467
468 v = strv_split_newlines(p);
469 if (!v)
470 return 0;
471
472 STRV_FOREACH(q, v)
473 log_debug("'%s'(%s) '%s'", spawn->cmd,
474 fd == spawn->fd_stdout ? "out" : "err", *q);
475 }
476
477 return 0;
478 }
479
480 static int on_spawn_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
481 Spawn *spawn = userdata;
482 char timeout[FORMAT_TIMESPAN_MAX];
483
484 assert(spawn);
485
486 kill_and_sigcont(spawn->pid, SIGKILL);
487
488 log_error("Spawned process '%s' ["PID_FMT"] timed out after %s, killing", spawn->cmd, spawn->pid,
489 format_timespan(timeout, sizeof(timeout), spawn->timeout_usec, USEC_PER_SEC));
490
491 return 1;
492 }
493
494 static int on_spawn_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
495 Spawn *spawn = userdata;
496 char timeout[FORMAT_TIMESPAN_MAX];
497
498 assert(spawn);
499
500 log_warning("Spawned process '%s' ["PID_FMT"] is taking longer than %s to complete", spawn->cmd, spawn->pid,
501 format_timespan(timeout, sizeof(timeout), spawn->timeout_warn_usec, USEC_PER_SEC));
502
503 return 1;
504 }
505
506 static int on_spawn_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) {
507 Spawn *spawn = userdata;
508
509 assert(spawn);
510
511 switch (si->si_code) {
512 case CLD_EXITED:
513 if (si->si_status == 0) {
514 log_debug("Process '%s' succeeded.", spawn->cmd);
515 sd_event_exit(sd_event_source_get_event(s), 0);
516
517 return 1;
518 }
519
520 log_full(spawn->accept_failure ? LOG_DEBUG : LOG_WARNING,
521 "Process '%s' failed with exit code %i.", spawn->cmd, si->si_status);
522 break;
523 case CLD_KILLED:
524 case CLD_DUMPED:
525 log_warning("Process '%s' terminated by signal %s.", spawn->cmd, signal_to_string(si->si_status));
526
527 break;
528 default:
529 log_error("Process '%s' failed due to unknown reason.", spawn->cmd);
530 }
531
532 sd_event_exit(sd_event_source_get_event(s), -EIO);
533
534 return 1;
535 }
536
537 static int spawn_wait(Spawn *spawn) {
538 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
539 int r, ret;
540
541 assert(spawn);
542
543 r = sd_event_new(&e);
544 if (r < 0)
545 return r;
546
547 if (spawn->timeout_usec > 0) {
548 usec_t usec, age_usec;
549
550 usec = now(CLOCK_MONOTONIC);
551 age_usec = usec - spawn->event_birth_usec;
552 if (age_usec < spawn->timeout_usec) {
553 if (spawn->timeout_warn_usec > 0 &&
554 spawn->timeout_warn_usec < spawn->timeout_usec &&
555 spawn->timeout_warn_usec > age_usec) {
556 spawn->timeout_warn_usec -= age_usec;
557
558 r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC,
559 usec + spawn->timeout_warn_usec, USEC_PER_SEC,
560 on_spawn_timeout_warning, spawn);
561 if (r < 0)
562 return r;
563 }
564
565 spawn->timeout_usec -= age_usec;
566
567 r = sd_event_add_time(e, NULL, CLOCK_MONOTONIC,
568 usec + spawn->timeout_usec, USEC_PER_SEC, on_spawn_timeout, spawn);
569 if (r < 0)
570 return r;
571 }
572 }
573
574 r = sd_event_add_io(e, NULL, spawn->fd_stdout, EPOLLIN, on_spawn_io, spawn);
575 if (r < 0)
576 return r;
577
578 r = sd_event_add_io(e, NULL, spawn->fd_stderr, EPOLLIN, on_spawn_io, spawn);
579 if (r < 0)
580 return r;
581
582 r = sd_event_add_child(e, NULL, spawn->pid, WEXITED, on_spawn_sigchld, spawn);
583 if (r < 0)
584 return r;
585
586 r = sd_event_loop(e);
587 if (r < 0)
588 return r;
589
590 r = sd_event_get_exit_code(e, &ret);
591 if (r < 0)
592 return r;
593
594 return ret;
595 }
596
597 int udev_event_spawn(struct udev_event *event,
598 usec_t timeout_usec,
599 bool accept_failure,
600 const char *cmd,
601 char *result, size_t ressize) {
602 _cleanup_close_pair_ int outpipe[2] = {-1, -1}, errpipe[2] = {-1, -1};
603 _cleanup_strv_free_ char **argv = NULL;
604 char **envp = NULL;
605 Spawn spawn;
606 pid_t pid;
607 int r;
608
609 assert(event);
610 assert(event->dev);
611 assert(result || ressize == 0);
612
613 /* pipes from child to parent */
614 if (result || log_get_max_level() >= LOG_INFO)
615 if (pipe2(outpipe, O_NONBLOCK|O_CLOEXEC) != 0)
616 return log_error_errno(errno, "Failed to create pipe for command '%s': %m", cmd);
617
618 if (log_get_max_level() >= LOG_INFO)
619 if (pipe2(errpipe, O_NONBLOCK|O_CLOEXEC) != 0)
620 return log_error_errno(errno, "Failed to create pipe for command '%s': %m", cmd);
621
622 argv = strv_split_full(cmd, NULL, SPLIT_QUOTES|SPLIT_RELAX);
623 if (!argv)
624 return log_oom();
625
626 if (isempty(argv[0])) {
627 log_error("Invalid command '%s'", cmd);
628 return -EINVAL;
629 }
630
631 /* allow programs in /usr/lib/udev/ to be called without the path */
632 if (!path_is_absolute(argv[0])) {
633 char *program;
634
635 program = path_join(NULL, UDEVLIBEXECDIR, argv[0]);
636 if (!program)
637 return log_oom();
638
639 free_and_replace(argv[0], program);
640 }
641
642 r = device_get_properties_strv(event->dev, &envp);
643 if (r < 0)
644 return log_device_error_errno(event->dev, r, "Failed to get device properties");
645
646 log_debug("Starting '%s'", cmd);
647
648 r = safe_fork("(spawn)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
649 if (r < 0)
650 return log_error_errno(r, "Failed to fork() to execute command '%s': %m", cmd);
651 if (r == 0) {
652 if (rearrange_stdio(-1, outpipe[WRITE_END], errpipe[WRITE_END]) < 0)
653 _exit(EXIT_FAILURE);
654
655 (void) close_all_fds(NULL, 0);
656
657 execve(argv[0], argv, envp);
658 _exit(EXIT_FAILURE);
659 }
660
661 /* parent closed child's ends of pipes */
662 outpipe[WRITE_END] = safe_close(outpipe[WRITE_END]);
663 errpipe[WRITE_END] = safe_close(errpipe[WRITE_END]);
664
665 spawn = (Spawn) {
666 .cmd = cmd,
667 .pid = pid,
668 .accept_failure = accept_failure,
669 .timeout_warn_usec = udev_warn_timeout(timeout_usec),
670 .timeout_usec = timeout_usec,
671 .event_birth_usec = event->birth_usec,
672 .fd_stdout = outpipe[READ_END],
673 .fd_stderr = errpipe[READ_END],
674 .result = result,
675 .result_size = ressize,
676 };
677 r = spawn_wait(&spawn);
678 if (r < 0)
679 return log_error_errno(r, "Failed to wait spawned command '%s': %m", cmd);
680
681 if (result)
682 result[spawn.result_len] = '\0';
683
684 return r;
685 }
686
687 static int rename_netif(struct udev_event *event) {
688 sd_device *dev = event->dev;
689 const char *action, *oldname;
690 char name[IFNAMSIZ];
691 int ifindex, r;
692
693 if (!event->name)
694 return 0; /* No new name is requested. */
695
696 r = sd_device_get_sysname(dev, &oldname);
697 if (r < 0)
698 return log_device_error_errno(dev, r, "Failed to get sysname: %m");
699
700 if (streq(event->name, oldname))
701 return 0; /* The interface name is already requested name. */
702
703 r = sd_device_get_property_value(dev, "ACTION", &action);
704 if (r < 0)
705 return log_device_error_errno(dev, r, "Failed to get property 'ACTION': %m");
706
707 if (!streq(action, "add"))
708 return 0; /* Rename the interface only when it is added. */
709
710 r = sd_device_get_ifindex(dev, &ifindex);
711 if (r == -ENOENT)
712 return 0; /* Device is not a network interface. */
713 if (r < 0)
714 return log_device_error_errno(dev, r, "Failed to get ifindex: %m");
715
716 strscpy(name, IFNAMSIZ, event->name);
717 r = rtnl_set_link_name(&event->rtnl, ifindex, name);
718 if (r < 0)
719 return log_device_error_errno(dev, r, "Failed to rename network interface %i from '%s' to '%s': %m", ifindex, oldname, name);
720
721 r = device_rename(dev, event->name);
722 if (r < 0)
723 return log_warning_errno(r, "Network interface %i is renamed from '%s' to '%s', but could not update sd_device object: %m", ifindex, oldname, name);
724
725 log_device_debug(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, oldname, name);
726
727 return 1;
728 }
729
730 static int update_devnode(struct udev_event *event) {
731 sd_device *dev = event->dev;
732 const char *action;
733 bool apply;
734 int r;
735
736 r = sd_device_get_devnum(dev, NULL);
737 if (r == -ENOENT)
738 return 0;
739 if (r < 0)
740 return log_device_error_errno(dev, r, "Failed to get devnum: %m");
741
742 /* remove/update possible left-over symlinks from old database entry */
743 if (event->dev_db_clone)
744 (void) udev_node_update_old_links(dev, event->dev_db_clone);
745
746 if (!event->owner_set) {
747 r = device_get_devnode_uid(dev, &event->uid);
748 if (r < 0 && r != -ENOENT)
749 return log_device_error_errno(dev, r, "Failed to get devnode UID: %m");
750 }
751
752 if (!event->group_set) {
753 r = device_get_devnode_gid(dev, &event->gid);
754 if (r < 0 && r != -ENOENT)
755 return log_device_error_errno(dev, r, "Failed to get devnode GID: %m");
756 }
757
758 if (!event->mode_set) {
759 r = device_get_devnode_mode(dev, &event->mode);
760 if (r < 0 && r != -ENOENT)
761 return log_device_error_errno(dev, r, "Failed to get devnode mode: %m");
762 if (r == -ENOENT) {
763 if (event->gid > 0)
764 /* default 0660 if a group is assigned */
765 event->mode = 0660;
766 else
767 /* default 0600 */
768 event->mode = 0600;
769 }
770 }
771
772 r = sd_device_get_property_value(dev, "ACTION", &action);
773 if (r < 0)
774 return log_device_error_errno(dev, r, "Failed to get property 'ACTION': %m");
775
776 apply = streq(action, "add") || event->owner_set || event->group_set || event->mode_set;
777 return udev_node_add(dev, apply, event->mode, event->uid, event->gid, event->seclabel_list);
778 }
779
780 static void event_execute_rules_on_remove(
781 struct udev_event *event,
782 usec_t timeout_usec,
783 Hashmap *properties_list,
784 struct udev_rules *rules) {
785
786 sd_device *dev = event->dev;
787 int r;
788
789 r = device_read_db_force(dev);
790 if (r < 0)
791 log_device_debug_errno(dev, r, "Failed to read database under /run/udev/data/: %m");
792
793 r = device_tag_index(dev, NULL, false);
794 if (r < 0)
795 log_device_debug_errno(dev, r, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
796
797 r = device_delete_db(dev);
798 if (r < 0)
799 log_device_debug_errno(dev, r, "Failed to delete database under /run/udev/data/, ignoring: %m");
800
801 if (sd_device_get_devnum(dev, NULL) >= 0)
802 (void) udev_watch_end(dev);
803
804 (void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list);
805
806 if (sd_device_get_devnum(dev, NULL) >= 0)
807 (void) udev_node_remove(dev);
808 }
809
810 int udev_event_execute_rules(struct udev_event *event,
811 usec_t timeout_usec,
812 Hashmap *properties_list,
813 struct udev_rules *rules) {
814 sd_device *dev = event->dev;
815 const char *subsystem, *action;
816 int r;
817
818 assert(event);
819 assert(rules);
820
821 r = sd_device_get_subsystem(dev, &subsystem);
822 if (r < 0)
823 return log_device_error_errno(dev, r, "Failed to get subsystem: %m");
824
825 r = sd_device_get_property_value(dev, "ACTION", &action);
826 if (r < 0)
827 return log_device_error_errno(dev, r, "Failed to get property 'ACTION': %m");
828
829 if (streq(action, "remove")) {
830 event_execute_rules_on_remove(event, timeout_usec, properties_list, rules);
831 return 0;
832 }
833
834 r = device_clone_with_db(dev, &event->dev_db_clone);
835 if (r < 0)
836 log_device_debug_errno(dev, r, "Failed to clone sd_device object, ignoring: %m");
837
838 if (event->dev_db_clone) {
839 r = sd_device_get_devnum(dev, NULL);
840 if (r < 0) {
841 if (r != -ENOENT)
842 log_device_debug_errno(dev, r, "Failed to get devnum, ignoring: %m");
843
844 if (streq(action, "move")) {
845 r = device_copy_properties(dev, event->dev_db_clone);
846 if (r < 0)
847 log_device_debug_errno(dev, r, "Failed to copy properties from cloned device, ignoring: %m");
848 }
849 } else
850 /* Disable watch during event processing. */
851 (void) udev_watch_end(event->dev_db_clone);
852 }
853
854 (void) udev_rules_apply_to_event(rules, event, timeout_usec, properties_list);
855
856 (void) rename_netif(event);
857 (void) update_devnode(event);
858
859 /* preserve old, or get new initialization timestamp */
860 r = device_ensure_usec_initialized(dev, event->dev_db_clone);
861 if (r < 0)
862 log_device_debug_errno(dev, r, "Failed to set initialization timestamp, ignoring: %m");
863
864 /* (re)write database file */
865 r = device_tag_index(dev, event->dev_db_clone, true);
866 if (r < 0)
867 log_device_debug_errno(dev, r, "Failed to update tags under /run/udev/tag/, ignoring: %m");
868
869 r = device_update_db(dev);
870 if (r < 0)
871 log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/, ignoring: %m");
872
873 device_set_is_initialized(dev);
874
875 event->dev_db_clone = sd_device_unref(event->dev_db_clone);
876
877 return 0;
878 }
879
880 void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec) {
881 const char *cmd;
882 void *val;
883 Iterator i;
884
885 HASHMAP_FOREACH_KEY(val, cmd, event->run_list, i) {
886 enum udev_builtin_cmd builtin_cmd = PTR_TO_INT(val);
887 char command[UTIL_PATH_SIZE];
888
889 udev_event_apply_format(event, cmd, command, sizeof(command), false);
890
891 if (builtin_cmd >= 0 && builtin_cmd < _UDEV_BUILTIN_MAX)
892 udev_builtin_run(event->dev, builtin_cmd, command, false);
893 else {
894 if (event->exec_delay_usec > 0) {
895 log_debug("delay execution of '%s'", command);
896 (void) usleep(event->exec_delay_usec);
897 }
898
899 udev_event_spawn(event, timeout_usec, false, command, NULL, 0);
900 }
901 }
902 }