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