1 /* SPDX-License-Identifier: GPL-2.0+ */
15 #include "alloc-util.h"
16 #include "device-private.h"
17 #include "device-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"
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"
38 typedef struct Spawn
{
41 usec_t timeout_warn_usec
;
43 usec_t event_birth_usec
;
52 UdevEvent
*udev_event_new(sd_device
*dev
, usec_t exec_delay_usec
, sd_netlink
*rtnl
) {
57 event
= new(UdevEvent
, 1);
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
),
74 UdevEvent
*udev_event_free(UdevEvent
*event
) {
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
);
94 FORMAT_SUBST_KERNEL_NUMBER
,
106 _FORMAT_SUBST_TYPE_MAX
,
107 _FORMAT_SUBST_TYPE_INVALID
= -1
108 } FormatSubstitutionType
;
110 struct subst_map_entry
{
113 FormatSubstitutionType type
;
116 static const struct subst_map_entry map
[] = {
117 { .name
= "devnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
},
118 { .name
= "tempnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
},
119 { .name
= "attr", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
},
120 { .name
= "sysfs", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
},
121 { .name
= "env", .fmt
= 'E', .type
= FORMAT_SUBST_ENV
},
122 { .name
= "kernel", .fmt
= 'k', .type
= FORMAT_SUBST_KERNEL
},
123 { .name
= "number", .fmt
= 'n', .type
= FORMAT_SUBST_KERNEL_NUMBER
},
124 { .name
= "driver", .fmt
= 'd', .type
= FORMAT_SUBST_DRIVER
},
125 { .name
= "devpath", .fmt
= 'p', .type
= FORMAT_SUBST_DEVPATH
},
126 { .name
= "id", .fmt
= 'b', .type
= FORMAT_SUBST_ID
},
127 { .name
= "major", .fmt
= 'M', .type
= FORMAT_SUBST_MAJOR
},
128 { .name
= "minor", .fmt
= 'm', .type
= FORMAT_SUBST_MINOR
},
129 { .name
= "result", .fmt
= 'c', .type
= FORMAT_SUBST_RESULT
},
130 { .name
= "parent", .fmt
= 'P', .type
= FORMAT_SUBST_PARENT
},
131 { .name
= "name", .fmt
= 'D', .type
= FORMAT_SUBST_NAME
},
132 { .name
= "links", .fmt
= 'L', .type
= FORMAT_SUBST_LINKS
},
133 { .name
= "root", .fmt
= 'r', .type
= FORMAT_SUBST_ROOT
},
134 { .name
= "sys", .fmt
= 'S', .type
= FORMAT_SUBST_SYS
},
137 static ssize_t
subst_format_var(UdevEvent
*event
,
138 const struct subst_map_entry
*entry
, char *attr
,
139 char *dest
, size_t l
) {
140 sd_device
*parent
, *dev
= event
->dev
;
141 const char *val
= NULL
;
148 switch (entry
->type
) {
149 case FORMAT_SUBST_DEVPATH
:
150 r
= sd_device_get_devpath(dev
, &val
);
153 l
= strpcpy(&s
, l
, val
);
155 case FORMAT_SUBST_KERNEL
:
156 r
= sd_device_get_sysname(dev
, &val
);
159 l
= strpcpy(&s
, l
, val
);
161 case FORMAT_SUBST_KERNEL_NUMBER
:
162 r
= sd_device_get_sysnum(dev
, &val
);
167 l
= strpcpy(&s
, l
, val
);
169 case FORMAT_SUBST_ID
:
170 if (!event
->dev_parent
)
172 r
= sd_device_get_sysname(event
->dev_parent
, &val
);
175 l
= strpcpy(&s
, l
, val
);
177 case FORMAT_SUBST_DRIVER
:
178 if (!event
->dev_parent
)
180 r
= sd_device_get_driver(event
->dev_parent
, &val
);
185 l
= strpcpy(&s
, l
, val
);
187 case FORMAT_SUBST_MAJOR
:
188 case FORMAT_SUBST_MINOR
: {
189 char buf
[DECIMAL_STR_MAX(unsigned)];
191 r
= sd_device_get_devnum(dev
, &devnum
);
192 if (r
< 0 && r
!= -ENOENT
)
194 xsprintf(buf
, "%u", r
< 0 ? 0 : entry
->type
== FORMAT_SUBST_MAJOR
? major(devnum
) : minor(devnum
));
195 l
= strpcpy(&s
, l
, buf
);
198 case FORMAT_SUBST_RESULT
: {
202 if (!event
->program_result
)
205 /* get part of the result string */
208 i
= strtoul(attr
, &rest
, 10);
210 char result
[UTIL_PATH_SIZE
], tmp
[UTIL_PATH_SIZE
], *cpos
;
212 strscpy(result
, sizeof(result
), event
->program_result
);
215 while (cpos
[0] != '\0' && !isspace(cpos
[0]))
217 while (isspace(cpos
[0]))
223 log_error("requested part of result string not found");
226 strscpy(tmp
, sizeof(tmp
), cpos
);
227 /* %{2+}c copies the whole string from the second part on */
228 if (rest
[0] != '+') {
229 cpos
= strchr(tmp
, ' ');
233 l
= strpcpy(&s
, l
, tmp
);
235 l
= strpcpy(&s
, l
, event
->program_result
);
238 case FORMAT_SUBST_ATTR
: {
239 char vbuf
[UTIL_NAME_SIZE
];
246 /* try to read the value specified by "[dmi/id]product_name" */
247 if (util_resolve_subsys_kernel(attr
, vbuf
, sizeof(vbuf
), true) == 0)
250 /* try to read the attribute the device */
252 (void) sd_device_get_sysattr_value(dev
, attr
, &val
);
254 /* try to read the attribute of the parent device, other matches have selected */
255 if (!val
&& event
->dev_parent
&& event
->dev_parent
!= dev
)
256 (void) sd_device_get_sysattr_value(event
->dev_parent
, attr
, &val
);
261 /* strip trailing whitespace, and replace unwanted characters */
263 strscpy(vbuf
, sizeof(vbuf
), val
);
265 while (len
> 0 && isspace(vbuf
[--len
]))
267 count
= util_replace_chars(vbuf
, UDEV_ALLOWED_CHARS_INPUT
);
269 log_device_debug(dev
, "%i character(s) replaced", count
);
270 l
= strpcpy(&s
, l
, vbuf
);
273 case FORMAT_SUBST_PARENT
:
274 r
= sd_device_get_parent(dev
, &parent
);
279 r
= sd_device_get_devname(parent
, &val
);
284 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
286 case FORMAT_SUBST_DEVNODE
:
287 r
= sd_device_get_devname(dev
, &val
);
292 l
= strpcpy(&s
, l
, val
);
294 case FORMAT_SUBST_NAME
:
296 l
= strpcpy(&s
, l
, event
->name
);
297 else if (sd_device_get_devname(dev
, &val
) >= 0)
298 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
300 r
= sd_device_get_sysname(dev
, &val
);
303 l
= strpcpy(&s
, l
, val
);
306 case FORMAT_SUBST_LINKS
:
307 FOREACH_DEVICE_DEVLINK(dev
, val
)
309 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
311 l
= strpcpyl(&s
, l
, " ", val
+ STRLEN("/dev/"), NULL
);
315 case FORMAT_SUBST_ROOT
:
316 l
= strpcpy(&s
, l
, "/dev");
318 case FORMAT_SUBST_SYS
:
319 l
= strpcpy(&s
, l
, "/sys");
321 case FORMAT_SUBST_ENV
:
324 r
= sd_device_get_property_value(dev
, attr
, &val
);
329 l
= strpcpy(&s
, l
, val
);
332 assert_not_reached("Unknown format substitution type");
342 ssize_t
udev_event_apply_format(UdevEvent
*event
,
343 const char *src
, char *dest
, size_t size
,
344 bool replace_whitespace
) {
360 const struct subst_map_entry
*entry
= NULL
;
361 char attrbuf
[UTIL_PATH_SIZE
], *attr
;
362 bool format_dollar
= false;
365 while (from
[0] != '\0') {
366 if (from
[0] == '$') {
367 /* substitute named variable */
370 if (from
[1] == '$') {
375 for (i
= 0; i
< ELEMENTSOF(map
); i
++) {
376 if (startswith(&from
[1], map
[i
].name
)) {
378 from
+= strlen(map
[i
].name
)+1;
379 format_dollar
= true;
383 } else if (from
[0] == '%') {
384 /* substitute format char */
387 if (from
[1] == '%') {
392 for (i
= 0; i
< ELEMENTSOF(map
); i
++) {
393 if (from
[1] == map
[i
].fmt
) {
402 if (l
< 2) /* need space for this char and the terminating NUL */
412 /* extract possible $format{attr} */
413 if (from
[0] == '{') {
417 for (i
= 0; from
[i
] != '}'; i
++)
418 if (from
[i
] == '\0') {
419 log_error("missing closing brace for format '%s'", src
);
423 if (i
>= sizeof(attrbuf
))
425 memcpy(attrbuf
, from
, i
);
432 subst_len
= subst_format_var(event
, entry
, attr
, s
, l
);
435 log_device_warning_errno(event
->dev
, subst_len
, "Failed to substitute variable '$%s', ignoring: %m", entry
->name
);
437 log_device_warning_errno(event
->dev
, subst_len
, "Failed to apply format '%%%c', ignoring: %m", entry
->fmt
);
442 /* FORMAT_SUBST_RESULT handles spaces itself */
443 if (replace_whitespace
&& entry
->type
!= FORMAT_SUBST_RESULT
)
444 /* util_replace_whitespace can replace in-place,
445 * and does nothing if subst_len == 0
447 subst_len
= util_replace_whitespace(s
, s
, subst_len
);
459 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
460 Spawn
*spawn
= userdata
;
466 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
467 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
469 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
470 p
= spawn
->result
+ spawn
->result_len
;
471 size
= spawn
->result_size
- spawn
->result_len
;
477 l
= read(fd
, p
, size
- 1);
480 log_error_errno(errno
, "Failed to read stdout of '%s': %m", spawn
->cmd
);
486 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
487 spawn
->result_len
+= l
;
489 /* Log output only if we watch stderr. */
490 if (l
> 0 && spawn
->fd_stderr
>= 0) {
491 _cleanup_strv_free_
char **v
= NULL
;
494 v
= strv_split_newlines(p
);
499 log_debug("'%s'(%s) '%s'", spawn
->cmd
,
500 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
506 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
507 Spawn
*spawn
= userdata
;
508 char timeout
[FORMAT_TIMESPAN_MAX
];
512 kill_and_sigcont(spawn
->pid
, SIGKILL
);
514 log_error("Spawned process '%s' ["PID_FMT
"] timed out after %s, killing", spawn
->cmd
, spawn
->pid
,
515 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
520 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
521 Spawn
*spawn
= userdata
;
522 char timeout
[FORMAT_TIMESPAN_MAX
];
526 log_warning("Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete", spawn
->cmd
, spawn
->pid
,
527 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
532 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
533 Spawn
*spawn
= userdata
;
538 switch (si
->si_code
) {
540 if (si
->si_status
== 0)
541 log_debug("Process '%s' succeeded.", spawn
->cmd
);
543 log_full(spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
,
544 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
549 log_error("Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
552 log_error("Process '%s' failed due to unknown reason.", spawn
->cmd
);
555 sd_event_exit(sd_event_source_get_event(s
), ret
);
559 static int spawn_wait(Spawn
*spawn
) {
560 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
565 r
= sd_event_new(&e
);
569 if (spawn
->timeout_usec
> 0) {
570 usec_t usec
, age_usec
;
572 usec
= now(CLOCK_MONOTONIC
);
573 age_usec
= usec
- spawn
->event_birth_usec
;
574 if (age_usec
< spawn
->timeout_usec
) {
575 if (spawn
->timeout_warn_usec
> 0 &&
576 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
577 spawn
->timeout_warn_usec
> age_usec
) {
578 spawn
->timeout_warn_usec
-= age_usec
;
580 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
581 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
582 on_spawn_timeout_warning
, spawn
);
587 spawn
->timeout_usec
-= age_usec
;
589 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
590 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
596 if (spawn
->fd_stdout
>= 0) {
597 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
602 if (spawn
->fd_stderr
>= 0) {
603 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
608 r
= sd_event_add_child(e
, NULL
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
612 return sd_event_loop(e
);
615 int udev_event_spawn(UdevEvent
*event
,
619 char *result
, size_t ressize
) {
620 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
621 _cleanup_strv_free_
char **argv
= NULL
;
629 assert(result
|| ressize
== 0);
631 /* pipes from child to parent */
632 if (result
|| log_get_max_level() >= LOG_INFO
)
633 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
634 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
636 if (log_get_max_level() >= LOG_INFO
)
637 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
638 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
640 argv
= strv_split_full(cmd
, NULL
, SPLIT_QUOTES
|SPLIT_RELAX
);
644 if (isempty(argv
[0]))
645 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
646 "Invalid command '%s'", cmd
);
648 /* allow programs in /usr/lib/udev/ to be called without the path */
649 if (!path_is_absolute(argv
[0])) {
652 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
656 free_and_replace(argv
[0], program
);
659 r
= device_get_properties_strv(event
->dev
, &envp
);
661 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
663 log_debug("Starting '%s'", cmd
);
665 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
667 return log_error_errno(r
, "Failed to fork() to execute command '%s': %m", cmd
);
669 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
672 (void) close_all_fds(NULL
, 0);
673 (void) rlimit_nofile_safe();
675 execve(argv
[0], argv
, envp
);
679 /* parent closed child's ends of pipes */
680 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
681 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
686 .accept_failure
= accept_failure
,
687 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
688 .timeout_usec
= timeout_usec
,
689 .event_birth_usec
= event
->birth_usec
,
690 .fd_stdout
= outpipe
[READ_END
],
691 .fd_stderr
= errpipe
[READ_END
],
693 .result_size
= ressize
,
695 r
= spawn_wait(&spawn
);
697 return log_error_errno(r
, "Failed to wait for spawned command '%s': %m", cmd
);
700 result
[spawn
.result_len
] = '\0';
702 return r
; /* 0 for success, and positive if the program failed */
705 static int rename_netif(UdevEvent
*event
) {
706 sd_device
*dev
= event
->dev
;
711 return 0; /* No new name is requested. */
713 r
= sd_device_get_sysname(dev
, &oldname
);
715 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
717 if (streq(event
->name
, oldname
))
718 return 0; /* The interface name is already requested name. */
720 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
721 return 0; /* Rename the interface only when it is added. */
723 r
= sd_device_get_ifindex(dev
, &ifindex
);
725 return 0; /* Device is not a network interface. */
727 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
729 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
731 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
732 ifindex
, oldname
, event
->name
);
734 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
735 r
= device_add_property(dev
, "ID_RENAMING", "1");
737 log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
739 r
= device_rename(dev
, event
->name
);
741 log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
743 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
748 static int update_devnode(UdevEvent
*event
) {
749 sd_device
*dev
= event
->dev
;
753 r
= sd_device_get_devnum(dev
, NULL
);
757 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
759 /* remove/update possible left-over symlinks from old database entry */
760 if (event
->dev_db_clone
)
761 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
763 if (!uid_is_valid(event
->uid
)) {
764 r
= device_get_devnode_uid(dev
, &event
->uid
);
768 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
771 if (!gid_is_valid(event
->gid
)) {
772 r
= device_get_devnode_gid(dev
, &event
->gid
);
776 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
779 if (event
->mode
== MODE_INVALID
) {
780 r
= device_get_devnode_mode(dev
, &event
->mode
);
781 if (r
< 0 && r
!= -ENOENT
)
782 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
785 /* default 0660 if a group is assigned */
793 apply
= device_for_action(dev
, DEVICE_ACTION_ADD
) ||
794 uid_is_valid(event
->uid
) ||
795 gid_is_valid(event
->gid
) ||
796 event
->mode
!= MODE_INVALID
;
797 return udev_node_add(dev
, apply
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
800 static void event_execute_rules_on_remove(
803 Hashmap
*properties_list
,
806 sd_device
*dev
= event
->dev
;
809 r
= device_read_db_internal(dev
, true);
811 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
813 r
= device_tag_index(dev
, NULL
, false);
815 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
817 r
= device_delete_db(dev
);
819 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
821 if (sd_device_get_devnum(dev
, NULL
) >= 0)
822 (void) udev_watch_end(dev
);
824 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
826 if (sd_device_get_devnum(dev
, NULL
) >= 0)
827 (void) udev_node_remove(dev
);
830 static int udev_event_on_move(UdevEvent
*event
) {
831 sd_device
*dev
= event
->dev
;
834 if (event
->dev_db_clone
&&
835 sd_device_get_devnum(dev
, NULL
) < 0) {
836 r
= device_copy_properties(dev
, event
->dev_db_clone
);
838 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
841 /* Drop previously added property */
842 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
844 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property, ignoring: %m");
849 int udev_event_execute_rules(UdevEvent
*event
,
851 Hashmap
*properties_list
,
853 const char *subsystem
;
863 r
= sd_device_get_subsystem(dev
, &subsystem
);
865 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
867 r
= device_get_action(dev
, &action
);
869 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
871 if (action
== DEVICE_ACTION_REMOVE
) {
872 event_execute_rules_on_remove(event
, timeout_usec
, properties_list
, rules
);
876 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
878 log_device_debug_errno(dev
, r
, "Failed to clone sd_device object, ignoring: %m");
880 if (event
->dev_db_clone
&& sd_device_get_devnum(dev
, NULL
) >= 0)
881 /* Disable watch during event processing. */
882 (void) udev_watch_end(event
->dev_db_clone
);
884 if (action
== DEVICE_ACTION_MOVE
)
885 (void) udev_event_on_move(event
);
887 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
889 (void) rename_netif(event
);
890 (void) update_devnode(event
);
892 /* preserve old, or get new initialization timestamp */
893 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
895 log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp, ignoring: %m");
897 /* (re)write database file */
898 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
900 log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/, ignoring: %m");
902 r
= device_update_db(dev
);
904 log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/, ignoring: %m");
906 device_set_is_initialized(dev
);
908 event
->dev_db_clone
= sd_device_unref(event
->dev_db_clone
);
913 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
) {
919 ORDERED_HASHMAP_FOREACH_KEY(val
, cmd
, event
->run_list
, i
) {
920 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
921 char command
[UTIL_PATH_SIZE
];
923 (void) udev_event_apply_format(event
, cmd
, command
, sizeof(command
), false);
925 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
926 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
927 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
929 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
931 if (event
->exec_delay_usec
> 0) {
932 char buf
[FORMAT_TIMESPAN_MAX
];
934 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
935 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
936 (void) usleep(event
->exec_delay_usec
);
939 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
940 r
= udev_event_spawn(event
, timeout_usec
, false, command
, NULL
, 0);
941 if (r
> 0) /* returned value is positive when program fails */
942 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);