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 "parse-util.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "rlimit-util.h"
27 #include "signal-util.h"
28 #include "stdio-util.h"
29 #include "string-util.h"
32 #include "udev-builtin.h"
33 #include "udev-event.h"
34 #include "udev-node.h"
35 #include "udev-util.h"
36 #include "udev-watch.h"
37 #include "user-util.h"
39 typedef struct Spawn
{
42 usec_t timeout_warn_usec
;
44 usec_t event_birth_usec
;
53 UdevEvent
*udev_event_new(sd_device
*dev
, usec_t exec_delay_usec
, sd_netlink
*rtnl
) {
58 event
= new(UdevEvent
, 1);
62 *event
= (UdevEvent
) {
63 .dev
= sd_device_ref(dev
),
64 .birth_usec
= now(CLOCK_MONOTONIC
),
65 .exec_delay_usec
= exec_delay_usec
,
66 .rtnl
= sd_netlink_ref(rtnl
),
75 UdevEvent
*udev_event_free(UdevEvent
*event
) {
79 sd_device_unref(event
->dev
);
80 sd_device_unref(event
->dev_db_clone
);
81 sd_netlink_unref(event
->rtnl
);
82 ordered_hashmap_free_free_key(event
->run_list
);
83 ordered_hashmap_free_free_free(event
->seclabel_list
);
84 free(event
->program_result
);
95 FORMAT_SUBST_KERNEL_NUMBER
,
107 _FORMAT_SUBST_TYPE_MAX
,
108 _FORMAT_SUBST_TYPE_INVALID
= -1
109 } FormatSubstitutionType
;
111 struct subst_map_entry
{
114 FormatSubstitutionType type
;
117 static const struct subst_map_entry map
[] = {
118 { .name
= "devnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
},
119 { .name
= "tempnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
}, /* deprecated */
120 { .name
= "attr", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
},
121 { .name
= "sysfs", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
}, /* deprecated */
122 { .name
= "env", .fmt
= 'E', .type
= FORMAT_SUBST_ENV
},
123 { .name
= "kernel", .fmt
= 'k', .type
= FORMAT_SUBST_KERNEL
},
124 { .name
= "number", .fmt
= 'n', .type
= FORMAT_SUBST_KERNEL_NUMBER
},
125 { .name
= "driver", .fmt
= 'd', .type
= FORMAT_SUBST_DRIVER
},
126 { .name
= "devpath", .fmt
= 'p', .type
= FORMAT_SUBST_DEVPATH
},
127 { .name
= "id", .fmt
= 'b', .type
= FORMAT_SUBST_ID
},
128 { .name
= "major", .fmt
= 'M', .type
= FORMAT_SUBST_MAJOR
},
129 { .name
= "minor", .fmt
= 'm', .type
= FORMAT_SUBST_MINOR
},
130 { .name
= "result", .fmt
= 'c', .type
= FORMAT_SUBST_RESULT
},
131 { .name
= "parent", .fmt
= 'P', .type
= FORMAT_SUBST_PARENT
},
132 { .name
= "name", .fmt
= 'D', .type
= FORMAT_SUBST_NAME
},
133 { .name
= "links", .fmt
= 'L', .type
= FORMAT_SUBST_LINKS
},
134 { .name
= "root", .fmt
= 'r', .type
= FORMAT_SUBST_ROOT
},
135 { .name
= "sys", .fmt
= 'S', .type
= FORMAT_SUBST_SYS
},
138 static const char *format_type_to_string(FormatSubstitutionType t
) {
139 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
140 if (map
[i
].type
== t
)
145 static char format_type_to_char(FormatSubstitutionType t
) {
146 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
147 if (map
[i
].type
== t
)
152 static int get_subst_type(const char **str
, bool strict
, FormatSubstitutionType
*ret_type
, char ret_attr
[static UTIL_PATH_SIZE
]) {
153 const char *p
= *str
, *q
= NULL
;
167 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
168 if ((q
= startswith(p
, map
[i
].name
)))
170 } else if (*p
== '%') {
177 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
178 if (*p
== map
[i
].fmt
) {
185 /* When 'strict' flag is set, then '$' and '%' must be escaped. */
186 return strict
? -EINVAL
: 0;
189 const char *start
, *end
;
193 end
= strchr(start
, '}');
198 if (len
== 0 || len
>= UTIL_PATH_SIZE
)
201 strnscpy(ret_attr
, UTIL_PATH_SIZE
, start
, len
);
207 *ret_type
= map
[i
].type
;
211 static int safe_atou_optional_plus(const char *s
, unsigned *ret
) {
218 /* Returns 1 if plus, 0 if no plus, negative on error */
220 p
= endswith(s
, "+");
222 s
= strndupa(s
, p
- s
);
224 r
= safe_atou(s
, ret
);
231 static ssize_t
udev_event_subst_format(
233 FormatSubstitutionType type
,
237 sd_device
*parent
, *dev
= event
->dev
;
238 const char *val
= NULL
;
243 case FORMAT_SUBST_DEVPATH
:
244 r
= sd_device_get_devpath(dev
, &val
);
247 l
= strpcpy(&s
, l
, val
);
249 case FORMAT_SUBST_KERNEL
:
250 r
= sd_device_get_sysname(dev
, &val
);
253 l
= strpcpy(&s
, l
, val
);
255 case FORMAT_SUBST_KERNEL_NUMBER
:
256 r
= sd_device_get_sysnum(dev
, &val
);
261 l
= strpcpy(&s
, l
, val
);
263 case FORMAT_SUBST_ID
:
264 if (!event
->dev_parent
)
266 r
= sd_device_get_sysname(event
->dev_parent
, &val
);
269 l
= strpcpy(&s
, l
, val
);
271 case FORMAT_SUBST_DRIVER
:
272 if (!event
->dev_parent
)
274 r
= sd_device_get_driver(event
->dev_parent
, &val
);
279 l
= strpcpy(&s
, l
, val
);
281 case FORMAT_SUBST_MAJOR
:
282 case FORMAT_SUBST_MINOR
: {
285 r
= sd_device_get_devnum(dev
, &devnum
);
286 if (r
< 0 && r
!= -ENOENT
)
288 l
= strpcpyf(&s
, l
, "%u", r
< 0 ? 0 : type
== FORMAT_SUBST_MAJOR
? major(devnum
) : minor(devnum
));
291 case FORMAT_SUBST_RESULT
: {
292 unsigned index
= 0; /* 0 means whole string */
295 if (!event
->program_result
)
298 if (!isempty(attr
)) {
299 r
= safe_atou_optional_plus(attr
, &index
);
307 l
= strpcpy(&s
, l
, event
->program_result
);
309 const char *start
, *p
;
312 p
= skip_leading_chars(event
->program_result
, NULL
);
314 for (i
= 1; i
< index
; i
++) {
315 while (*p
&& !strchr(WHITESPACE
, *p
))
317 p
= skip_leading_chars(p
, NULL
);
322 log_device_debug(dev
, "requested part of result string not found");
327 /* %c{2+} copies the whole string from the second part on */
329 l
= strpcpy(&s
, l
, start
);
331 while (*p
&& !strchr(WHITESPACE
, *p
))
333 l
= strnpcpy(&s
, l
, start
, p
- start
);
338 case FORMAT_SUBST_ATTR
: {
339 char vbuf
[UTIL_NAME_SIZE
];
345 /* try to read the value specified by "[dmi/id]product_name" */
346 if (util_resolve_subsys_kernel(attr
, vbuf
, sizeof(vbuf
), true) == 0)
349 /* try to read the attribute the device */
351 (void) sd_device_get_sysattr_value(dev
, attr
, &val
);
353 /* try to read the attribute of the parent device, other matches have selected */
354 if (!val
&& event
->dev_parent
&& event
->dev_parent
!= dev
)
355 (void) sd_device_get_sysattr_value(event
->dev_parent
, attr
, &val
);
360 /* strip trailing whitespace, and replace unwanted characters */
362 strscpy(vbuf
, sizeof(vbuf
), val
);
363 delete_trailing_chars(vbuf
, NULL
);
364 count
= util_replace_chars(vbuf
, UDEV_ALLOWED_CHARS_INPUT
);
366 log_device_debug(dev
, "%i character(s) replaced", count
);
367 l
= strpcpy(&s
, l
, vbuf
);
370 case FORMAT_SUBST_PARENT
:
371 r
= sd_device_get_parent(dev
, &parent
);
376 r
= sd_device_get_devname(parent
, &val
);
381 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
383 case FORMAT_SUBST_DEVNODE
:
384 r
= sd_device_get_devname(dev
, &val
);
389 l
= strpcpy(&s
, l
, val
);
391 case FORMAT_SUBST_NAME
:
393 l
= strpcpy(&s
, l
, event
->name
);
394 else if (sd_device_get_devname(dev
, &val
) >= 0)
395 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
397 r
= sd_device_get_sysname(dev
, &val
);
400 l
= strpcpy(&s
, l
, val
);
403 case FORMAT_SUBST_LINKS
:
404 FOREACH_DEVICE_DEVLINK(dev
, val
)
406 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
408 l
= strpcpyl(&s
, l
, " ", val
+ STRLEN("/dev/"), NULL
);
412 case FORMAT_SUBST_ROOT
:
413 l
= strpcpy(&s
, l
, "/dev");
415 case FORMAT_SUBST_SYS
:
416 l
= strpcpy(&s
, l
, "/sys");
418 case FORMAT_SUBST_ENV
:
421 r
= sd_device_get_property_value(dev
, attr
, &val
);
426 l
= strpcpy(&s
, l
, val
);
429 assert_not_reached("Unknown format substitution type");
439 ssize_t
udev_event_apply_format(UdevEvent
*event
,
440 const char *src
, char *dest
, size_t size
,
441 bool replace_whitespace
) {
452 FormatSubstitutionType type
;
453 char attr
[UTIL_PATH_SIZE
];
456 r
= get_subst_type(&s
, false, &type
, attr
);
458 return log_device_warning_errno(event
->dev
, r
, "Invalid format string, ignoring: %s", src
);
460 if (size
< 2) /* need space for this char and the terminating NUL */
467 subst_len
= udev_event_subst_format(event
, type
, attr
, dest
, size
);
469 return log_device_warning_errno(event
->dev
, subst_len
,
470 "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
471 format_type_to_string(type
), format_type_to_char(type
));
473 /* FORMAT_SUBST_RESULT handles spaces itself */
474 if (replace_whitespace
&& type
!= FORMAT_SUBST_RESULT
)
475 /* util_replace_whitespace can replace in-place,
476 * and does nothing if subst_len == 0 */
477 subst_len
= util_replace_whitespace(dest
, dest
, subst_len
);
488 int udev_check_format(const char *value
, size_t *offset
, const char **hint
) {
489 FormatSubstitutionType type
;
490 const char *s
= value
;
491 char attr
[UTIL_PATH_SIZE
];
495 r
= get_subst_type(&s
, true, &type
, attr
);
500 *hint
= "invalid substitution type";
507 if (IN_SET(type
, FORMAT_SUBST_ATTR
, FORMAT_SUBST_ENV
) && isempty(attr
)) {
511 *hint
= "attribute value missing";
515 if (type
== FORMAT_SUBST_RESULT
&& !isempty(attr
)) {
518 r
= safe_atou_optional_plus(attr
, &i
);
523 *hint
= "attribute value not a valid number";
532 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
533 Spawn
*spawn
= userdata
;
539 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
540 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
542 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
543 p
= spawn
->result
+ spawn
->result_len
;
544 size
= spawn
->result_size
- spawn
->result_len
;
550 l
= read(fd
, p
, size
- 1);
553 log_error_errno(errno
, "Failed to read stdout of '%s': %m", spawn
->cmd
);
559 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
560 spawn
->result_len
+= l
;
562 /* Log output only if we watch stderr. */
563 if (l
> 0 && spawn
->fd_stderr
>= 0) {
564 _cleanup_strv_free_
char **v
= NULL
;
567 v
= strv_split_newlines(p
);
572 log_debug("'%s'(%s) '%s'", spawn
->cmd
,
573 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
579 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
580 Spawn
*spawn
= userdata
;
581 char timeout
[FORMAT_TIMESPAN_MAX
];
585 kill_and_sigcont(spawn
->pid
, SIGKILL
);
587 log_error("Spawned process '%s' ["PID_FMT
"] timed out after %s, killing", spawn
->cmd
, spawn
->pid
,
588 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
593 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
594 Spawn
*spawn
= userdata
;
595 char timeout
[FORMAT_TIMESPAN_MAX
];
599 log_warning("Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete", spawn
->cmd
, spawn
->pid
,
600 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
605 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
606 Spawn
*spawn
= userdata
;
611 switch (si
->si_code
) {
613 if (si
->si_status
== 0)
614 log_debug("Process '%s' succeeded.", spawn
->cmd
);
616 log_full(spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
,
617 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
622 log_error("Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
625 log_error("Process '%s' failed due to unknown reason.", spawn
->cmd
);
628 sd_event_exit(sd_event_source_get_event(s
), ret
);
632 static int spawn_wait(Spawn
*spawn
) {
633 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
638 r
= sd_event_new(&e
);
642 if (spawn
->timeout_usec
> 0) {
643 usec_t usec
, age_usec
;
645 usec
= now(CLOCK_MONOTONIC
);
646 age_usec
= usec
- spawn
->event_birth_usec
;
647 if (age_usec
< spawn
->timeout_usec
) {
648 if (spawn
->timeout_warn_usec
> 0 &&
649 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
650 spawn
->timeout_warn_usec
> age_usec
) {
651 spawn
->timeout_warn_usec
-= age_usec
;
653 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
654 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
655 on_spawn_timeout_warning
, spawn
);
660 spawn
->timeout_usec
-= age_usec
;
662 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
663 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
669 if (spawn
->fd_stdout
>= 0) {
670 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
675 if (spawn
->fd_stderr
>= 0) {
676 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
681 r
= sd_event_add_child(e
, NULL
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
685 return sd_event_loop(e
);
688 int udev_event_spawn(UdevEvent
*event
,
692 char *result
, size_t ressize
) {
693 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
694 _cleanup_strv_free_
char **argv
= NULL
;
702 assert(result
|| ressize
== 0);
704 /* pipes from child to parent */
705 if (result
|| log_get_max_level() >= LOG_INFO
)
706 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
707 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
709 if (log_get_max_level() >= LOG_INFO
)
710 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
711 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
713 argv
= strv_split_full(cmd
, NULL
, SPLIT_QUOTES
|SPLIT_RELAX
);
717 if (isempty(argv
[0]))
718 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
719 "Invalid command '%s'", cmd
);
721 /* allow programs in /usr/lib/udev/ to be called without the path */
722 if (!path_is_absolute(argv
[0])) {
725 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
729 free_and_replace(argv
[0], program
);
732 r
= device_get_properties_strv(event
->dev
, &envp
);
734 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
736 log_debug("Starting '%s'", cmd
);
738 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
740 return log_error_errno(r
, "Failed to fork() to execute command '%s': %m", cmd
);
742 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
745 (void) close_all_fds(NULL
, 0);
746 (void) rlimit_nofile_safe();
748 execve(argv
[0], argv
, envp
);
752 /* parent closed child's ends of pipes */
753 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
754 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
759 .accept_failure
= accept_failure
,
760 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
761 .timeout_usec
= timeout_usec
,
762 .event_birth_usec
= event
->birth_usec
,
763 .fd_stdout
= outpipe
[READ_END
],
764 .fd_stderr
= errpipe
[READ_END
],
766 .result_size
= ressize
,
768 r
= spawn_wait(&spawn
);
770 return log_error_errno(r
, "Failed to wait for spawned command '%s': %m", cmd
);
773 result
[spawn
.result_len
] = '\0';
775 return r
; /* 0 for success, and positive if the program failed */
778 static int rename_netif(UdevEvent
*event
) {
779 sd_device
*dev
= event
->dev
;
784 return 0; /* No new name is requested. */
786 r
= sd_device_get_sysname(dev
, &oldname
);
788 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
790 if (streq(event
->name
, oldname
))
791 return 0; /* The interface name is already requested name. */
793 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
794 return 0; /* Rename the interface only when it is added. */
796 r
= sd_device_get_ifindex(dev
, &ifindex
);
798 return 0; /* Device is not a network interface. */
800 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
802 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
804 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
805 ifindex
, oldname
, event
->name
);
807 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
808 r
= device_add_property(dev
, "ID_RENAMING", "1");
810 return log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
812 r
= device_rename(dev
, event
->name
);
814 return log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
816 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
821 static int update_devnode(UdevEvent
*event
) {
822 sd_device
*dev
= event
->dev
;
825 r
= sd_device_get_devnum(dev
, NULL
);
829 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
831 /* remove/update possible left-over symlinks from old database entry */
832 if (event
->dev_db_clone
)
833 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
835 if (!uid_is_valid(event
->uid
)) {
836 r
= device_get_devnode_uid(dev
, &event
->uid
);
837 if (r
< 0 && r
!= -ENOENT
)
838 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
841 if (!gid_is_valid(event
->gid
)) {
842 r
= device_get_devnode_gid(dev
, &event
->gid
);
843 if (r
< 0 && r
!= -ENOENT
)
844 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
847 if (event
->mode
== MODE_INVALID
) {
848 r
= device_get_devnode_mode(dev
, &event
->mode
);
849 if (r
< 0 && r
!= -ENOENT
)
850 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
852 if (event
->mode
== MODE_INVALID
&& gid_is_valid(event
->gid
) && event
->gid
> 0)
853 /* If group is set, but mode is not set, "upgrade" mode for the group. */
856 bool apply_mac
= device_for_action(dev
, DEVICE_ACTION_ADD
);
858 return udev_node_add(dev
, apply_mac
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
861 static void event_execute_rules_on_remove(
864 Hashmap
*properties_list
,
867 sd_device
*dev
= event
->dev
;
870 r
= device_read_db_internal(dev
, true);
872 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
874 r
= device_tag_index(dev
, NULL
, false);
876 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
878 r
= device_delete_db(dev
);
880 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
882 if (sd_device_get_devnum(dev
, NULL
) >= 0)
883 (void) udev_watch_end(dev
);
885 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
887 if (sd_device_get_devnum(dev
, NULL
) >= 0)
888 (void) udev_node_remove(dev
);
891 static int udev_event_on_move(UdevEvent
*event
) {
892 sd_device
*dev
= event
->dev
;
895 if (event
->dev_db_clone
&&
896 sd_device_get_devnum(dev
, NULL
) < 0) {
897 r
= device_copy_properties(dev
, event
->dev_db_clone
);
899 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
902 /* Drop previously added property */
903 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
905 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property: %m");
910 int udev_event_execute_rules(UdevEvent
*event
,
912 Hashmap
*properties_list
,
914 const char *subsystem
;
924 r
= sd_device_get_subsystem(dev
, &subsystem
);
926 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
928 r
= device_get_action(dev
, &action
);
930 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
932 if (action
== DEVICE_ACTION_REMOVE
) {
933 event_execute_rules_on_remove(event
, timeout_usec
, properties_list
, rules
);
937 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
939 return log_device_debug_errno(dev
, r
, "Failed to clone sd_device object: %m");
941 if (event
->dev_db_clone
&& sd_device_get_devnum(dev
, NULL
) >= 0)
942 /* Disable watch during event processing. */
943 (void) udev_watch_end(event
->dev_db_clone
);
945 if (action
== DEVICE_ACTION_MOVE
) {
946 r
= udev_event_on_move(event
);
951 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
953 return log_device_debug_errno(dev
, r
, "Failed to apply udev rules: %m");
955 r
= rename_netif(event
);
959 r
= update_devnode(event
);
963 /* preserve old, or get new initialization timestamp */
964 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
966 return log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp: %m");
968 /* (re)write database file */
969 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
971 return log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/: %m");
973 r
= device_update_db(dev
);
975 return log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
977 device_set_is_initialized(dev
);
979 event
->dev_db_clone
= sd_device_unref(event
->dev_db_clone
);
984 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
) {
990 ORDERED_HASHMAP_FOREACH_KEY(val
, command
, event
->run_list
, i
) {
991 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
993 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
994 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
995 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
997 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
999 if (event
->exec_delay_usec
> 0) {
1000 char buf
[FORMAT_TIMESPAN_MAX
];
1002 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
1003 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
1004 (void) usleep(event
->exec_delay_usec
);
1007 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
1008 r
= udev_event_spawn(event
, timeout_usec
, false, command
, NULL
, 0);
1009 if (r
> 0) /* returned value is positive when program fails */
1010 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);