1 /* SPDX-License-Identifier: GPL-2.0+ */
14 #include "alloc-util.h"
15 #include "device-private.h"
16 #include "device-util.h"
19 #include "format-util.h"
20 #include "libudev-util.h"
21 #include "netlink-util.h"
22 #include "parse-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
{
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_device_error_errno(spawn
->device
, errno
,
554 "Failed to read stdout of '%s': %m", spawn
->cmd
);
560 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
561 spawn
->result_len
+= l
;
563 /* Log output only if we watch stderr. */
564 if (l
> 0 && spawn
->fd_stderr
>= 0) {
565 _cleanup_strv_free_
char **v
= NULL
;
568 v
= strv_split_newlines(p
);
573 log_device_debug(spawn
->device
, "'%s'(%s) '%s'", spawn
->cmd
,
574 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
580 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
581 Spawn
*spawn
= userdata
;
582 char timeout
[FORMAT_TIMESPAN_MAX
];
586 kill_and_sigcont(spawn
->pid
, SIGKILL
);
588 log_device_error(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] timed out after %s, killing",
589 spawn
->cmd
, spawn
->pid
,
590 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
595 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
596 Spawn
*spawn
= userdata
;
597 char timeout
[FORMAT_TIMESPAN_MAX
];
601 log_device_warning(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete",
602 spawn
->cmd
, spawn
->pid
,
603 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
608 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
609 Spawn
*spawn
= userdata
;
614 switch (si
->si_code
) {
616 if (si
->si_status
== 0)
617 log_device_debug(spawn
->device
, "Process '%s' succeeded.", spawn
->cmd
);
619 log_device_full(spawn
->device
, spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
, 0,
620 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
625 log_device_error(spawn
->device
, "Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
628 log_device_error(spawn
->device
, "Process '%s' failed due to unknown reason.", spawn
->cmd
);
631 sd_event_exit(sd_event_source_get_event(s
), ret
);
635 static int spawn_wait(Spawn
*spawn
) {
636 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
641 r
= sd_event_new(&e
);
645 if (spawn
->timeout_usec
> 0) {
646 usec_t usec
, age_usec
;
648 usec
= now(CLOCK_MONOTONIC
);
649 age_usec
= usec
- spawn
->event_birth_usec
;
650 if (age_usec
< spawn
->timeout_usec
) {
651 if (spawn
->timeout_warn_usec
> 0 &&
652 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
653 spawn
->timeout_warn_usec
> age_usec
) {
654 spawn
->timeout_warn_usec
-= age_usec
;
656 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
657 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
658 on_spawn_timeout_warning
, spawn
);
663 spawn
->timeout_usec
-= age_usec
;
665 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
666 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
672 if (spawn
->fd_stdout
>= 0) {
673 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
678 if (spawn
->fd_stderr
>= 0) {
679 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
684 r
= sd_event_add_child(e
, NULL
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
688 return sd_event_loop(e
);
691 int udev_event_spawn(UdevEvent
*event
,
695 char *result
, size_t ressize
) {
696 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
697 _cleanup_strv_free_
char **argv
= NULL
;
705 assert(result
|| ressize
== 0);
707 /* pipes from child to parent */
708 if (result
|| log_get_max_level() >= LOG_INFO
)
709 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
710 return log_device_error_errno(event
->dev
, errno
,
711 "Failed to create pipe for command '%s': %m", cmd
);
713 if (log_get_max_level() >= LOG_INFO
)
714 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
715 return log_device_error_errno(event
->dev
, errno
,
716 "Failed to create pipe for command '%s': %m", cmd
);
718 argv
= strv_split_full(cmd
, NULL
, SPLIT_QUOTES
|SPLIT_RELAX
);
722 if (isempty(argv
[0]))
723 return log_device_error_errno(event
->dev
, SYNTHETIC_ERRNO(EINVAL
),
724 "Invalid command '%s'", cmd
);
726 /* allow programs in /usr/lib/udev/ to be called without the path */
727 if (!path_is_absolute(argv
[0])) {
730 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
734 free_and_replace(argv
[0], program
);
737 r
= device_get_properties_strv(event
->dev
, &envp
);
739 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
741 log_device_debug(event
->dev
, "Starting '%s'", cmd
);
743 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
745 return log_device_error_errno(event
->dev
, r
,
746 "Failed to fork() to execute command '%s': %m", cmd
);
748 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
751 (void) close_all_fds(NULL
, 0);
752 (void) rlimit_nofile_safe();
754 execve(argv
[0], argv
, envp
);
758 /* parent closed child's ends of pipes */
759 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
760 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
763 .device
= event
->dev
,
766 .accept_failure
= accept_failure
,
767 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
768 .timeout_usec
= timeout_usec
,
769 .event_birth_usec
= event
->birth_usec
,
770 .fd_stdout
= outpipe
[READ_END
],
771 .fd_stderr
= errpipe
[READ_END
],
773 .result_size
= ressize
,
775 r
= spawn_wait(&spawn
);
777 return log_device_error_errno(event
->dev
, r
,
778 "Failed to wait for spawned command '%s': %m", cmd
);
781 result
[spawn
.result_len
] = '\0';
783 return r
; /* 0 for success, and positive if the program failed */
786 static int rename_netif(UdevEvent
*event
) {
787 sd_device
*dev
= event
->dev
;
792 return 0; /* No new name is requested. */
794 r
= sd_device_get_sysname(dev
, &oldname
);
796 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
798 if (streq(event
->name
, oldname
))
799 return 0; /* The interface name is already requested name. */
801 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
802 return 0; /* Rename the interface only when it is added. */
804 r
= sd_device_get_ifindex(dev
, &ifindex
);
806 return 0; /* Device is not a network interface. */
808 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
810 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
812 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
813 ifindex
, oldname
, event
->name
);
815 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
816 r
= device_add_property(dev
, "ID_RENAMING", "1");
818 return log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
820 r
= device_rename(dev
, event
->name
);
822 return log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
824 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
829 static int update_devnode(UdevEvent
*event
) {
830 sd_device
*dev
= event
->dev
;
833 r
= sd_device_get_devnum(dev
, NULL
);
837 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
839 /* remove/update possible left-over symlinks from old database entry */
840 if (event
->dev_db_clone
)
841 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
843 if (!uid_is_valid(event
->uid
)) {
844 r
= device_get_devnode_uid(dev
, &event
->uid
);
845 if (r
< 0 && r
!= -ENOENT
)
846 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
849 if (!gid_is_valid(event
->gid
)) {
850 r
= device_get_devnode_gid(dev
, &event
->gid
);
851 if (r
< 0 && r
!= -ENOENT
)
852 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
855 if (event
->mode
== MODE_INVALID
) {
856 r
= device_get_devnode_mode(dev
, &event
->mode
);
857 if (r
< 0 && r
!= -ENOENT
)
858 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
860 if (event
->mode
== MODE_INVALID
&& gid_is_valid(event
->gid
) && event
->gid
> 0)
861 /* If group is set, but mode is not set, "upgrade" mode for the group. */
864 bool apply_mac
= device_for_action(dev
, DEVICE_ACTION_ADD
);
866 return udev_node_add(dev
, apply_mac
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
869 static void event_execute_rules_on_remove(
872 Hashmap
*properties_list
,
875 sd_device
*dev
= event
->dev
;
878 r
= device_read_db_internal(dev
, true);
880 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
882 r
= device_tag_index(dev
, NULL
, false);
884 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
886 r
= device_delete_db(dev
);
888 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
890 if (sd_device_get_devnum(dev
, NULL
) >= 0)
891 (void) udev_watch_end(dev
);
893 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
895 if (sd_device_get_devnum(dev
, NULL
) >= 0)
896 (void) udev_node_remove(dev
);
899 static int udev_event_on_move(UdevEvent
*event
) {
900 sd_device
*dev
= event
->dev
;
903 if (event
->dev_db_clone
&&
904 sd_device_get_devnum(dev
, NULL
) < 0) {
905 r
= device_copy_properties(dev
, event
->dev_db_clone
);
907 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
910 /* Drop previously added property */
911 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
913 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property: %m");
918 int udev_event_execute_rules(UdevEvent
*event
,
920 Hashmap
*properties_list
,
922 const char *subsystem
;
932 r
= sd_device_get_subsystem(dev
, &subsystem
);
934 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
936 r
= device_get_action(dev
, &action
);
938 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
940 if (action
== DEVICE_ACTION_REMOVE
) {
941 event_execute_rules_on_remove(event
, timeout_usec
, properties_list
, rules
);
945 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
947 return log_device_debug_errno(dev
, r
, "Failed to clone sd_device object: %m");
949 if (event
->dev_db_clone
&& sd_device_get_devnum(dev
, NULL
) >= 0)
950 /* Disable watch during event processing. */
951 (void) udev_watch_end(event
->dev_db_clone
);
953 if (action
== DEVICE_ACTION_MOVE
) {
954 r
= udev_event_on_move(event
);
959 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
961 return log_device_debug_errno(dev
, r
, "Failed to apply udev rules: %m");
963 r
= rename_netif(event
);
967 r
= update_devnode(event
);
971 /* preserve old, or get new initialization timestamp */
972 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
974 return log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp: %m");
976 /* (re)write database file */
977 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
979 return log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/: %m");
981 r
= device_update_db(dev
);
983 return log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
985 device_set_is_initialized(dev
);
987 event
->dev_db_clone
= sd_device_unref(event
->dev_db_clone
);
992 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
) {
998 ORDERED_HASHMAP_FOREACH_KEY(val
, command
, event
->run_list
, i
) {
999 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
1001 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
1002 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
1003 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
1005 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
1007 if (event
->exec_delay_usec
> 0) {
1008 char buf
[FORMAT_TIMESPAN_MAX
];
1010 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
1011 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
1012 (void) usleep(event
->exec_delay_usec
);
1015 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
1016 r
= udev_event_spawn(event
, timeout_usec
, false, command
, NULL
, 0);
1018 log_device_warning_errno(event
->dev
, r
, "Failed to execute '%s', ignoring: %m", command
);
1019 else if (r
> 0) /* returned value is positive when program fails */
1020 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);