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
}, /* deprecated */
119 { .name
= "attr", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
},
120 { .name
= "sysfs", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
}, /* deprecated */
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
udev_event_subst_format(
139 FormatSubstitutionType type
,
143 sd_device
*parent
, *dev
= event
->dev
;
144 const char *val
= NULL
;
150 case FORMAT_SUBST_DEVPATH
:
151 r
= sd_device_get_devpath(dev
, &val
);
154 l
= strpcpy(&s
, l
, val
);
156 case FORMAT_SUBST_KERNEL
:
157 r
= sd_device_get_sysname(dev
, &val
);
160 l
= strpcpy(&s
, l
, val
);
162 case FORMAT_SUBST_KERNEL_NUMBER
:
163 r
= sd_device_get_sysnum(dev
, &val
);
168 l
= strpcpy(&s
, l
, val
);
170 case FORMAT_SUBST_ID
:
171 if (!event
->dev_parent
)
173 r
= sd_device_get_sysname(event
->dev_parent
, &val
);
176 l
= strpcpy(&s
, l
, val
);
178 case FORMAT_SUBST_DRIVER
:
179 if (!event
->dev_parent
)
181 r
= sd_device_get_driver(event
->dev_parent
, &val
);
186 l
= strpcpy(&s
, l
, val
);
188 case FORMAT_SUBST_MAJOR
:
189 case FORMAT_SUBST_MINOR
: {
190 char buf
[DECIMAL_STR_MAX(unsigned)];
192 r
= sd_device_get_devnum(dev
, &devnum
);
193 if (r
< 0 && r
!= -ENOENT
)
195 xsprintf(buf
, "%u", r
< 0 ? 0 : type
== FORMAT_SUBST_MAJOR
? major(devnum
) : minor(devnum
));
196 l
= strpcpy(&s
, l
, buf
);
199 case FORMAT_SUBST_RESULT
: {
203 if (!event
->program_result
)
206 /* get part of the result string */
209 i
= strtoul(attr
, &rest
, 10);
211 char result
[UTIL_PATH_SIZE
], tmp
[UTIL_PATH_SIZE
], *cpos
;
213 strscpy(result
, sizeof(result
), event
->program_result
);
216 while (cpos
[0] != '\0' && !isspace(cpos
[0]))
218 while (isspace(cpos
[0]))
224 log_error("requested part of result string not found");
227 strscpy(tmp
, sizeof(tmp
), cpos
);
228 /* %{2+}c copies the whole string from the second part on */
229 if (rest
[0] != '+') {
230 cpos
= strchr(tmp
, ' ');
234 l
= strpcpy(&s
, l
, tmp
);
236 l
= strpcpy(&s
, l
, event
->program_result
);
239 case FORMAT_SUBST_ATTR
: {
240 char vbuf
[UTIL_NAME_SIZE
];
247 /* try to read the value specified by "[dmi/id]product_name" */
248 if (util_resolve_subsys_kernel(attr
, vbuf
, sizeof(vbuf
), true) == 0)
251 /* try to read the attribute the device */
253 (void) sd_device_get_sysattr_value(dev
, attr
, &val
);
255 /* try to read the attribute of the parent device, other matches have selected */
256 if (!val
&& event
->dev_parent
&& event
->dev_parent
!= dev
)
257 (void) sd_device_get_sysattr_value(event
->dev_parent
, attr
, &val
);
262 /* strip trailing whitespace, and replace unwanted characters */
264 strscpy(vbuf
, sizeof(vbuf
), val
);
266 while (len
> 0 && isspace(vbuf
[--len
]))
268 count
= util_replace_chars(vbuf
, UDEV_ALLOWED_CHARS_INPUT
);
270 log_device_debug(dev
, "%i character(s) replaced", count
);
271 l
= strpcpy(&s
, l
, vbuf
);
274 case FORMAT_SUBST_PARENT
:
275 r
= sd_device_get_parent(dev
, &parent
);
280 r
= sd_device_get_devname(parent
, &val
);
285 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
287 case FORMAT_SUBST_DEVNODE
:
288 r
= sd_device_get_devname(dev
, &val
);
293 l
= strpcpy(&s
, l
, val
);
295 case FORMAT_SUBST_NAME
:
297 l
= strpcpy(&s
, l
, event
->name
);
298 else if (sd_device_get_devname(dev
, &val
) >= 0)
299 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
301 r
= sd_device_get_sysname(dev
, &val
);
304 l
= strpcpy(&s
, l
, val
);
307 case FORMAT_SUBST_LINKS
:
308 FOREACH_DEVICE_DEVLINK(dev
, val
)
310 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
312 l
= strpcpyl(&s
, l
, " ", val
+ STRLEN("/dev/"), NULL
);
316 case FORMAT_SUBST_ROOT
:
317 l
= strpcpy(&s
, l
, "/dev");
319 case FORMAT_SUBST_SYS
:
320 l
= strpcpy(&s
, l
, "/sys");
322 case FORMAT_SUBST_ENV
:
325 r
= sd_device_get_property_value(dev
, attr
, &val
);
330 l
= strpcpy(&s
, l
, val
);
333 assert_not_reached("Unknown format substitution type");
343 ssize_t
udev_event_apply_format(UdevEvent
*event
,
344 const char *src
, char *dest
, size_t size
,
345 bool replace_whitespace
) {
361 const struct subst_map_entry
*entry
= NULL
;
362 char attrbuf
[UTIL_PATH_SIZE
], *attr
;
363 bool format_dollar
= false;
366 while (from
[0] != '\0') {
367 if (from
[0] == '$') {
368 /* substitute named variable */
371 if (from
[1] == '$') {
376 for (i
= 0; i
< ELEMENTSOF(map
); i
++) {
377 if (startswith(&from
[1], map
[i
].name
)) {
379 from
+= strlen(map
[i
].name
)+1;
380 format_dollar
= true;
384 } else if (from
[0] == '%') {
385 /* substitute format char */
388 if (from
[1] == '%') {
393 for (i
= 0; i
< ELEMENTSOF(map
); i
++) {
394 if (from
[1] == map
[i
].fmt
) {
403 if (l
< 2) /* need space for this char and the terminating NUL */
413 /* extract possible $format{attr} */
414 if (from
[0] == '{') {
418 for (i
= 0; from
[i
] != '}'; i
++)
419 if (from
[i
] == '\0') {
420 log_error("missing closing brace for format '%s'", src
);
424 if (i
>= sizeof(attrbuf
))
426 memcpy(attrbuf
, from
, i
);
433 subst_len
= udev_event_subst_format(event
, entry
->type
, attr
, s
, l
);
436 log_device_warning_errno(event
->dev
, subst_len
, "Failed to substitute variable '$%s', ignoring: %m", entry
->name
);
438 log_device_warning_errno(event
->dev
, subst_len
, "Failed to apply format '%%%c', ignoring: %m", entry
->fmt
);
443 /* FORMAT_SUBST_RESULT handles spaces itself */
444 if (replace_whitespace
&& entry
->type
!= FORMAT_SUBST_RESULT
)
445 /* util_replace_whitespace can replace in-place,
446 * and does nothing if subst_len == 0
448 subst_len
= util_replace_whitespace(s
, s
, subst_len
);
460 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
461 Spawn
*spawn
= userdata
;
467 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
468 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
470 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
471 p
= spawn
->result
+ spawn
->result_len
;
472 size
= spawn
->result_size
- spawn
->result_len
;
478 l
= read(fd
, p
, size
- 1);
481 log_error_errno(errno
, "Failed to read stdout of '%s': %m", spawn
->cmd
);
487 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
488 spawn
->result_len
+= l
;
490 /* Log output only if we watch stderr. */
491 if (l
> 0 && spawn
->fd_stderr
>= 0) {
492 _cleanup_strv_free_
char **v
= NULL
;
495 v
= strv_split_newlines(p
);
500 log_debug("'%s'(%s) '%s'", spawn
->cmd
,
501 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
507 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
508 Spawn
*spawn
= userdata
;
509 char timeout
[FORMAT_TIMESPAN_MAX
];
513 kill_and_sigcont(spawn
->pid
, SIGKILL
);
515 log_error("Spawned process '%s' ["PID_FMT
"] timed out after %s, killing", spawn
->cmd
, spawn
->pid
,
516 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
521 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
522 Spawn
*spawn
= userdata
;
523 char timeout
[FORMAT_TIMESPAN_MAX
];
527 log_warning("Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete", spawn
->cmd
, spawn
->pid
,
528 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
533 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
534 Spawn
*spawn
= userdata
;
539 switch (si
->si_code
) {
541 if (si
->si_status
== 0)
542 log_debug("Process '%s' succeeded.", spawn
->cmd
);
544 log_full(spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
,
545 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
550 log_error("Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
553 log_error("Process '%s' failed due to unknown reason.", spawn
->cmd
);
556 sd_event_exit(sd_event_source_get_event(s
), ret
);
560 static int spawn_wait(Spawn
*spawn
) {
561 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
566 r
= sd_event_new(&e
);
570 if (spawn
->timeout_usec
> 0) {
571 usec_t usec
, age_usec
;
573 usec
= now(CLOCK_MONOTONIC
);
574 age_usec
= usec
- spawn
->event_birth_usec
;
575 if (age_usec
< spawn
->timeout_usec
) {
576 if (spawn
->timeout_warn_usec
> 0 &&
577 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
578 spawn
->timeout_warn_usec
> age_usec
) {
579 spawn
->timeout_warn_usec
-= age_usec
;
581 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
582 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
583 on_spawn_timeout_warning
, spawn
);
588 spawn
->timeout_usec
-= age_usec
;
590 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
591 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
597 if (spawn
->fd_stdout
>= 0) {
598 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
603 if (spawn
->fd_stderr
>= 0) {
604 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
609 r
= sd_event_add_child(e
, NULL
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
613 return sd_event_loop(e
);
616 int udev_event_spawn(UdevEvent
*event
,
620 char *result
, size_t ressize
) {
621 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
622 _cleanup_strv_free_
char **argv
= NULL
;
630 assert(result
|| ressize
== 0);
632 /* pipes from child to parent */
633 if (result
|| log_get_max_level() >= LOG_INFO
)
634 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
635 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
637 if (log_get_max_level() >= LOG_INFO
)
638 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
639 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
641 argv
= strv_split_full(cmd
, NULL
, SPLIT_QUOTES
|SPLIT_RELAX
);
645 if (isempty(argv
[0]))
646 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
647 "Invalid command '%s'", cmd
);
649 /* allow programs in /usr/lib/udev/ to be called without the path */
650 if (!path_is_absolute(argv
[0])) {
653 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
657 free_and_replace(argv
[0], program
);
660 r
= device_get_properties_strv(event
->dev
, &envp
);
662 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
664 log_debug("Starting '%s'", cmd
);
666 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
668 return log_error_errno(r
, "Failed to fork() to execute command '%s': %m", cmd
);
670 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
673 (void) close_all_fds(NULL
, 0);
674 (void) rlimit_nofile_safe();
676 execve(argv
[0], argv
, envp
);
680 /* parent closed child's ends of pipes */
681 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
682 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
687 .accept_failure
= accept_failure
,
688 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
689 .timeout_usec
= timeout_usec
,
690 .event_birth_usec
= event
->birth_usec
,
691 .fd_stdout
= outpipe
[READ_END
],
692 .fd_stderr
= errpipe
[READ_END
],
694 .result_size
= ressize
,
696 r
= spawn_wait(&spawn
);
698 return log_error_errno(r
, "Failed to wait for spawned command '%s': %m", cmd
);
701 result
[spawn
.result_len
] = '\0';
703 return r
; /* 0 for success, and positive if the program failed */
706 static int rename_netif(UdevEvent
*event
) {
707 sd_device
*dev
= event
->dev
;
712 return 0; /* No new name is requested. */
714 r
= sd_device_get_sysname(dev
, &oldname
);
716 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
718 if (streq(event
->name
, oldname
))
719 return 0; /* The interface name is already requested name. */
721 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
722 return 0; /* Rename the interface only when it is added. */
724 r
= sd_device_get_ifindex(dev
, &ifindex
);
726 return 0; /* Device is not a network interface. */
728 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
730 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
732 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
733 ifindex
, oldname
, event
->name
);
735 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
736 r
= device_add_property(dev
, "ID_RENAMING", "1");
738 log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
740 r
= device_rename(dev
, event
->name
);
742 log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
744 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
749 static int update_devnode(UdevEvent
*event
) {
750 sd_device
*dev
= event
->dev
;
754 r
= sd_device_get_devnum(dev
, NULL
);
758 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
760 /* remove/update possible left-over symlinks from old database entry */
761 if (event
->dev_db_clone
)
762 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
764 if (!uid_is_valid(event
->uid
)) {
765 r
= device_get_devnode_uid(dev
, &event
->uid
);
769 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
772 if (!gid_is_valid(event
->gid
)) {
773 r
= device_get_devnode_gid(dev
, &event
->gid
);
777 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
780 if (event
->mode
== MODE_INVALID
) {
781 r
= device_get_devnode_mode(dev
, &event
->mode
);
782 if (r
< 0 && r
!= -ENOENT
)
783 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
786 /* default 0660 if a group is assigned */
794 apply
= device_for_action(dev
, DEVICE_ACTION_ADD
) ||
795 uid_is_valid(event
->uid
) ||
796 gid_is_valid(event
->gid
) ||
797 event
->mode
!= MODE_INVALID
;
798 return udev_node_add(dev
, apply
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
801 static void event_execute_rules_on_remove(
804 Hashmap
*properties_list
,
807 sd_device
*dev
= event
->dev
;
810 r
= device_read_db_internal(dev
, true);
812 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
814 r
= device_tag_index(dev
, NULL
, false);
816 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
818 r
= device_delete_db(dev
);
820 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
822 if (sd_device_get_devnum(dev
, NULL
) >= 0)
823 (void) udev_watch_end(dev
);
825 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
827 if (sd_device_get_devnum(dev
, NULL
) >= 0)
828 (void) udev_node_remove(dev
);
831 static int udev_event_on_move(UdevEvent
*event
) {
832 sd_device
*dev
= event
->dev
;
835 if (event
->dev_db_clone
&&
836 sd_device_get_devnum(dev
, NULL
) < 0) {
837 r
= device_copy_properties(dev
, event
->dev_db_clone
);
839 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
842 /* Drop previously added property */
843 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
845 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property, ignoring: %m");
850 int udev_event_execute_rules(UdevEvent
*event
,
852 Hashmap
*properties_list
,
854 const char *subsystem
;
864 r
= sd_device_get_subsystem(dev
, &subsystem
);
866 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
868 r
= device_get_action(dev
, &action
);
870 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
872 if (action
== DEVICE_ACTION_REMOVE
) {
873 event_execute_rules_on_remove(event
, timeout_usec
, properties_list
, rules
);
877 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
879 log_device_debug_errno(dev
, r
, "Failed to clone sd_device object, ignoring: %m");
881 if (event
->dev_db_clone
&& sd_device_get_devnum(dev
, NULL
) >= 0)
882 /* Disable watch during event processing. */
883 (void) udev_watch_end(event
->dev_db_clone
);
885 if (action
== DEVICE_ACTION_MOVE
)
886 (void) udev_event_on_move(event
);
888 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
890 (void) rename_netif(event
);
891 (void) update_devnode(event
);
893 /* preserve old, or get new initialization timestamp */
894 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
896 log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp, ignoring: %m");
898 /* (re)write database file */
899 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
901 log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/, ignoring: %m");
903 r
= device_update_db(dev
);
905 log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/, ignoring: %m");
907 device_set_is_initialized(dev
);
909 event
->dev_db_clone
= sd_device_unref(event
->dev_db_clone
);
914 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
) {
920 ORDERED_HASHMAP_FOREACH_KEY(val
, cmd
, event
->run_list
, i
) {
921 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
922 char command
[UTIL_PATH_SIZE
];
924 (void) udev_event_apply_format(event
, cmd
, command
, sizeof(command
), false);
926 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
927 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
928 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
930 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
932 if (event
->exec_delay_usec
> 0) {
933 char buf
[FORMAT_TIMESPAN_MAX
];
935 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
936 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
937 (void) usleep(event
->exec_delay_usec
);
940 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
941 r
= udev_event_spawn(event
, timeout_usec
, false, command
, NULL
, 0);
942 if (r
> 0) /* returned value is positive when program fails */
943 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);