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
;
540 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
541 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
543 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
544 p
= spawn
->result
+ spawn
->result_len
;
545 size
= spawn
->result_size
- spawn
->result_len
;
551 l
= read(fd
, p
, size
- 1);
556 log_device_error_errno(spawn
->device
, errno
,
557 "Failed to read stdout of '%s': %m", spawn
->cmd
);
563 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
564 spawn
->result_len
+= l
;
566 /* Log output only if we watch stderr. */
567 if (l
> 0 && spawn
->fd_stderr
>= 0) {
568 _cleanup_strv_free_
char **v
= NULL
;
571 v
= strv_split_newlines(p
);
576 log_device_debug(spawn
->device
, "'%s'(%s) '%s'", spawn
->cmd
,
577 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
584 /* Re-enable the event source if we did not encounter EOF */
586 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
588 log_device_error_errno(spawn
->device
, r
,
589 "Failed to reactivate IO source of '%s'", spawn
->cmd
);
593 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
594 Spawn
*spawn
= userdata
;
595 char timeout
[FORMAT_TIMESPAN_MAX
];
599 kill_and_sigcont(spawn
->pid
, SIGKILL
);
601 log_device_error(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] timed out after %s, killing",
602 spawn
->cmd
, spawn
->pid
,
603 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
608 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
609 Spawn
*spawn
= userdata
;
610 char timeout
[FORMAT_TIMESPAN_MAX
];
614 log_device_warning(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete",
615 spawn
->cmd
, spawn
->pid
,
616 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
621 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
622 Spawn
*spawn
= userdata
;
627 switch (si
->si_code
) {
629 if (si
->si_status
== 0)
630 log_device_debug(spawn
->device
, "Process '%s' succeeded.", spawn
->cmd
);
632 log_device_full(spawn
->device
, spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
, 0,
633 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
638 log_device_error(spawn
->device
, "Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
641 log_device_error(spawn
->device
, "Process '%s' failed due to unknown reason.", spawn
->cmd
);
644 sd_event_exit(sd_event_source_get_event(s
), ret
);
648 static int spawn_wait(Spawn
*spawn
) {
649 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
650 _cleanup_(sd_event_source_unrefp
) sd_event_source
*sigchld_source
= NULL
;
651 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stdout_source
= NULL
;
652 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stderr_source
= NULL
;
657 r
= sd_event_new(&e
);
661 if (spawn
->timeout_usec
> 0) {
662 usec_t usec
, age_usec
;
664 usec
= now(CLOCK_MONOTONIC
);
665 age_usec
= usec
- spawn
->event_birth_usec
;
666 if (age_usec
< spawn
->timeout_usec
) {
667 if (spawn
->timeout_warn_usec
> 0 &&
668 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
669 spawn
->timeout_warn_usec
> age_usec
) {
670 spawn
->timeout_warn_usec
-= age_usec
;
672 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
673 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
674 on_spawn_timeout_warning
, spawn
);
679 spawn
->timeout_usec
-= age_usec
;
681 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
682 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
688 if (spawn
->fd_stdout
>= 0) {
689 r
= sd_event_add_io(e
, &stdout_source
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
692 r
= sd_event_source_set_enabled(stdout_source
, SD_EVENT_ONESHOT
);
697 if (spawn
->fd_stderr
>= 0) {
698 r
= sd_event_add_io(e
, &stderr_source
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
701 r
= sd_event_source_set_enabled(stderr_source
, SD_EVENT_ONESHOT
);
706 r
= sd_event_add_child(e
, &sigchld_source
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
709 /* SIGCHLD should be processed after IO is complete */
710 r
= sd_event_source_set_priority(sigchld_source
, SD_EVENT_PRIORITY_NORMAL
+ 1);
715 return sd_event_loop(e
);
718 int udev_event_spawn(UdevEvent
*event
,
722 char *result
, size_t ressize
) {
723 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
724 _cleanup_strv_free_
char **argv
= NULL
;
732 assert(result
|| ressize
== 0);
734 /* pipes from child to parent */
735 if (result
|| log_get_max_level() >= LOG_INFO
)
736 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
737 return log_device_error_errno(event
->dev
, errno
,
738 "Failed to create pipe for command '%s': %m", cmd
);
740 if (log_get_max_level() >= LOG_INFO
)
741 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
742 return log_device_error_errno(event
->dev
, errno
,
743 "Failed to create pipe for command '%s': %m", cmd
);
745 argv
= strv_split_full(cmd
, NULL
, SPLIT_QUOTES
|SPLIT_RELAX
);
749 if (isempty(argv
[0]))
750 return log_device_error_errno(event
->dev
, SYNTHETIC_ERRNO(EINVAL
),
751 "Invalid command '%s'", cmd
);
753 /* allow programs in /usr/lib/udev/ to be called without the path */
754 if (!path_is_absolute(argv
[0])) {
757 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
761 free_and_replace(argv
[0], program
);
764 r
= device_get_properties_strv(event
->dev
, &envp
);
766 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
768 log_device_debug(event
->dev
, "Starting '%s'", cmd
);
770 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
772 return log_device_error_errno(event
->dev
, r
,
773 "Failed to fork() to execute command '%s': %m", cmd
);
775 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
778 (void) close_all_fds(NULL
, 0);
779 (void) rlimit_nofile_safe();
781 execve(argv
[0], argv
, envp
);
785 /* parent closed child's ends of pipes */
786 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
787 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
790 .device
= event
->dev
,
793 .accept_failure
= accept_failure
,
794 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
795 .timeout_usec
= timeout_usec
,
796 .event_birth_usec
= event
->birth_usec
,
797 .fd_stdout
= outpipe
[READ_END
],
798 .fd_stderr
= errpipe
[READ_END
],
800 .result_size
= ressize
,
802 r
= spawn_wait(&spawn
);
804 return log_device_error_errno(event
->dev
, r
,
805 "Failed to wait for spawned command '%s': %m", cmd
);
808 result
[spawn
.result_len
] = '\0';
810 return r
; /* 0 for success, and positive if the program failed */
813 static int rename_netif(UdevEvent
*event
) {
814 sd_device
*dev
= event
->dev
;
819 return 0; /* No new name is requested. */
821 r
= sd_device_get_sysname(dev
, &oldname
);
823 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
825 if (streq(event
->name
, oldname
))
826 return 0; /* The interface name is already requested name. */
828 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
829 return 0; /* Rename the interface only when it is added. */
831 r
= sd_device_get_ifindex(dev
, &ifindex
);
833 return 0; /* Device is not a network interface. */
835 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
837 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
839 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
840 ifindex
, oldname
, event
->name
);
842 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
843 r
= device_add_property(dev
, "ID_RENAMING", "1");
845 return log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
847 r
= device_rename(dev
, event
->name
);
849 return log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
851 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
856 static int update_devnode(UdevEvent
*event
) {
857 sd_device
*dev
= event
->dev
;
860 r
= sd_device_get_devnum(dev
, NULL
);
864 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
866 /* remove/update possible left-over symlinks from old database entry */
867 if (event
->dev_db_clone
)
868 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
870 if (!uid_is_valid(event
->uid
)) {
871 r
= device_get_devnode_uid(dev
, &event
->uid
);
872 if (r
< 0 && r
!= -ENOENT
)
873 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
876 if (!gid_is_valid(event
->gid
)) {
877 r
= device_get_devnode_gid(dev
, &event
->gid
);
878 if (r
< 0 && r
!= -ENOENT
)
879 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
882 if (event
->mode
== MODE_INVALID
) {
883 r
= device_get_devnode_mode(dev
, &event
->mode
);
884 if (r
< 0 && r
!= -ENOENT
)
885 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
887 if (event
->mode
== MODE_INVALID
&& gid_is_valid(event
->gid
) && event
->gid
> 0)
888 /* If group is set, but mode is not set, "upgrade" mode for the group. */
891 bool apply_mac
= device_for_action(dev
, DEVICE_ACTION_ADD
);
893 return udev_node_add(dev
, apply_mac
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
896 static void event_execute_rules_on_remove(
899 Hashmap
*properties_list
,
902 sd_device
*dev
= event
->dev
;
905 r
= device_read_db_internal(dev
, true);
907 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
909 r
= device_tag_index(dev
, NULL
, false);
911 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
913 r
= device_delete_db(dev
);
915 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
917 if (sd_device_get_devnum(dev
, NULL
) >= 0)
918 (void) udev_watch_end(dev
);
920 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
922 if (sd_device_get_devnum(dev
, NULL
) >= 0)
923 (void) udev_node_remove(dev
);
926 static int udev_event_on_move(UdevEvent
*event
) {
927 sd_device
*dev
= event
->dev
;
930 if (event
->dev_db_clone
&&
931 sd_device_get_devnum(dev
, NULL
) < 0) {
932 r
= device_copy_properties(dev
, event
->dev_db_clone
);
934 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
937 /* Drop previously added property */
938 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
940 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property: %m");
945 int udev_event_execute_rules(UdevEvent
*event
,
947 Hashmap
*properties_list
,
949 const char *subsystem
;
959 r
= sd_device_get_subsystem(dev
, &subsystem
);
961 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
963 r
= device_get_action(dev
, &action
);
965 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
967 if (action
== DEVICE_ACTION_REMOVE
) {
968 event_execute_rules_on_remove(event
, timeout_usec
, properties_list
, rules
);
972 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
974 return log_device_debug_errno(dev
, r
, "Failed to clone sd_device object: %m");
976 if (event
->dev_db_clone
&& sd_device_get_devnum(dev
, NULL
) >= 0)
977 /* Disable watch during event processing. */
978 (void) udev_watch_end(event
->dev_db_clone
);
980 if (action
== DEVICE_ACTION_MOVE
) {
981 r
= udev_event_on_move(event
);
986 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
988 return log_device_debug_errno(dev
, r
, "Failed to apply udev rules: %m");
990 r
= rename_netif(event
);
994 r
= update_devnode(event
);
998 /* preserve old, or get new initialization timestamp */
999 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
1001 return log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp: %m");
1003 /* (re)write database file */
1004 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
1006 return log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/: %m");
1008 r
= device_update_db(dev
);
1010 return log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
1012 device_set_is_initialized(dev
);
1014 event
->dev_db_clone
= sd_device_unref(event
->dev_db_clone
);
1019 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
) {
1020 const char *command
;
1025 ORDERED_HASHMAP_FOREACH_KEY(val
, command
, event
->run_list
, i
) {
1026 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
1028 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
1029 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
1030 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
1032 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
1034 if (event
->exec_delay_usec
> 0) {
1035 char buf
[FORMAT_TIMESPAN_MAX
];
1037 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
1038 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
1039 (void) usleep(event
->exec_delay_usec
);
1042 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
1043 r
= udev_event_spawn(event
, timeout_usec
, false, command
, NULL
, 0);
1045 log_device_warning_errno(event
->dev
, r
, "Failed to execute '%s', ignoring: %m", command
);
1046 else if (r
> 0) /* returned value is positive when program fails */
1047 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);