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
;
166 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
167 if ((q
= startswith(p
, map
[i
].name
)))
169 } else if (*p
== '%') {
176 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
177 if (*p
== map
[i
].fmt
) {
184 /* When 'strict' flag is set, then '$' and '%' must be escaped. */
185 return strict
? -EINVAL
: 0;
188 const char *start
, *end
;
192 end
= strchr(start
, '}');
197 if (len
== 0 || len
>= UTIL_PATH_SIZE
)
200 strnscpy(ret_attr
, UTIL_PATH_SIZE
, start
, len
);
206 *ret_type
= map
[i
].type
;
210 static int safe_atou_optional_plus(const char *s
, unsigned *ret
) {
217 /* Returns 1 if plus, 0 if no plus, negative on error */
219 p
= endswith(s
, "+");
221 s
= strndupa(s
, p
- s
);
223 r
= safe_atou(s
, ret
);
230 static ssize_t
udev_event_subst_format(
232 FormatSubstitutionType type
,
236 sd_device
*parent
, *dev
= event
->dev
;
237 const char *val
= NULL
;
242 case FORMAT_SUBST_DEVPATH
:
243 r
= sd_device_get_devpath(dev
, &val
);
246 l
= strpcpy(&s
, l
, val
);
248 case FORMAT_SUBST_KERNEL
:
249 r
= sd_device_get_sysname(dev
, &val
);
252 l
= strpcpy(&s
, l
, val
);
254 case FORMAT_SUBST_KERNEL_NUMBER
:
255 r
= sd_device_get_sysnum(dev
, &val
);
260 l
= strpcpy(&s
, l
, val
);
262 case FORMAT_SUBST_ID
:
263 if (!event
->dev_parent
)
265 r
= sd_device_get_sysname(event
->dev_parent
, &val
);
268 l
= strpcpy(&s
, l
, val
);
270 case FORMAT_SUBST_DRIVER
:
271 if (!event
->dev_parent
)
273 r
= sd_device_get_driver(event
->dev_parent
, &val
);
278 l
= strpcpy(&s
, l
, val
);
280 case FORMAT_SUBST_MAJOR
:
281 case FORMAT_SUBST_MINOR
: {
284 r
= sd_device_get_devnum(dev
, &devnum
);
285 if (r
< 0 && r
!= -ENOENT
)
287 l
= strpcpyf(&s
, l
, "%u", r
< 0 ? 0 : type
== FORMAT_SUBST_MAJOR
? major(devnum
) : minor(devnum
));
290 case FORMAT_SUBST_RESULT
: {
291 unsigned index
= 0; /* 0 means whole string */
294 if (!event
->program_result
)
297 if (!isempty(attr
)) {
298 r
= safe_atou_optional_plus(attr
, &index
);
306 l
= strpcpy(&s
, l
, event
->program_result
);
308 const char *start
, *p
;
311 p
= skip_leading_chars(event
->program_result
, NULL
);
313 for (i
= 1; i
< index
; i
++) {
314 while (*p
&& !strchr(WHITESPACE
, *p
))
316 p
= skip_leading_chars(p
, NULL
);
321 log_device_debug(dev
, "requested part of result string not found");
326 /* %c{2+} copies the whole string from the second part on */
328 l
= strpcpy(&s
, l
, start
);
330 while (*p
&& !strchr(WHITESPACE
, *p
))
332 l
= strnpcpy(&s
, l
, start
, p
- start
);
337 case FORMAT_SUBST_ATTR
: {
338 char vbuf
[UTIL_NAME_SIZE
];
344 /* try to read the value specified by "[dmi/id]product_name" */
345 if (util_resolve_subsys_kernel(attr
, vbuf
, sizeof(vbuf
), true) == 0)
348 /* try to read the attribute the device */
350 (void) sd_device_get_sysattr_value(dev
, attr
, &val
);
352 /* try to read the attribute of the parent device, other matches have selected */
353 if (!val
&& event
->dev_parent
&& event
->dev_parent
!= dev
)
354 (void) sd_device_get_sysattr_value(event
->dev_parent
, attr
, &val
);
359 /* strip trailing whitespace, and replace unwanted characters */
361 strscpy(vbuf
, sizeof(vbuf
), val
);
362 delete_trailing_chars(vbuf
, NULL
);
363 count
= util_replace_chars(vbuf
, UDEV_ALLOWED_CHARS_INPUT
);
365 log_device_debug(dev
, "%i character(s) replaced", count
);
366 l
= strpcpy(&s
, l
, vbuf
);
369 case FORMAT_SUBST_PARENT
:
370 r
= sd_device_get_parent(dev
, &parent
);
375 r
= sd_device_get_devname(parent
, &val
);
380 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
382 case FORMAT_SUBST_DEVNODE
:
383 r
= sd_device_get_devname(dev
, &val
);
388 l
= strpcpy(&s
, l
, val
);
390 case FORMAT_SUBST_NAME
:
392 l
= strpcpy(&s
, l
, event
->name
);
393 else if (sd_device_get_devname(dev
, &val
) >= 0)
394 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
396 r
= sd_device_get_sysname(dev
, &val
);
399 l
= strpcpy(&s
, l
, val
);
402 case FORMAT_SUBST_LINKS
:
403 FOREACH_DEVICE_DEVLINK(dev
, val
)
405 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
407 l
= strpcpyl(&s
, l
, " ", val
+ STRLEN("/dev/"), NULL
);
411 case FORMAT_SUBST_ROOT
:
412 l
= strpcpy(&s
, l
, "/dev");
414 case FORMAT_SUBST_SYS
:
415 l
= strpcpy(&s
, l
, "/sys");
417 case FORMAT_SUBST_ENV
:
420 r
= sd_device_get_property_value(dev
, attr
, &val
);
425 l
= strpcpy(&s
, l
, val
);
428 assert_not_reached("Unknown format substitution type");
438 ssize_t
udev_event_apply_format(UdevEvent
*event
,
439 const char *src
, char *dest
, size_t size
,
440 bool replace_whitespace
) {
451 FormatSubstitutionType type
;
452 char attr
[UTIL_PATH_SIZE
];
455 r
= get_subst_type(&s
, false, &type
, attr
);
457 return log_device_warning_errno(event
->dev
, r
, "Invalid format string, ignoring: %s", src
);
459 if (size
< 2) /* need space for this char and the terminating NUL */
466 subst_len
= udev_event_subst_format(event
, type
, attr
, dest
, size
);
468 return log_device_warning_errno(event
->dev
, subst_len
,
469 "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
470 format_type_to_string(type
), format_type_to_char(type
));
472 /* FORMAT_SUBST_RESULT handles spaces itself */
473 if (replace_whitespace
&& type
!= FORMAT_SUBST_RESULT
)
474 /* util_replace_whitespace can replace in-place,
475 * and does nothing if subst_len == 0 */
476 subst_len
= util_replace_whitespace(dest
, dest
, subst_len
);
487 int udev_check_format(const char *value
, size_t *offset
, const char **hint
) {
488 FormatSubstitutionType type
;
489 const char *s
= value
;
490 char attr
[UTIL_PATH_SIZE
];
494 r
= get_subst_type(&s
, true, &type
, attr
);
499 *hint
= "invalid substitution type";
506 if (IN_SET(type
, FORMAT_SUBST_ATTR
, FORMAT_SUBST_ENV
) && isempty(attr
)) {
510 *hint
= "attribute value missing";
514 if (type
== FORMAT_SUBST_RESULT
&& !isempty(attr
)) {
517 r
= safe_atou_optional_plus(attr
, &i
);
522 *hint
= "attribute value not a valid number";
531 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
532 Spawn
*spawn
= userdata
;
538 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
539 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
541 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
542 p
= spawn
->result
+ spawn
->result_len
;
543 size
= spawn
->result_size
- spawn
->result_len
;
549 l
= read(fd
, p
, size
- 1);
552 log_error_errno(errno
, "Failed to read stdout of '%s': %m", spawn
->cmd
);
558 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
559 spawn
->result_len
+= l
;
561 /* Log output only if we watch stderr. */
562 if (l
> 0 && spawn
->fd_stderr
>= 0) {
563 _cleanup_strv_free_
char **v
= NULL
;
566 v
= strv_split_newlines(p
);
571 log_debug("'%s'(%s) '%s'", spawn
->cmd
,
572 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
578 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
579 Spawn
*spawn
= userdata
;
580 char timeout
[FORMAT_TIMESPAN_MAX
];
584 kill_and_sigcont(spawn
->pid
, SIGKILL
);
586 log_error("Spawned process '%s' ["PID_FMT
"] timed out after %s, killing", spawn
->cmd
, spawn
->pid
,
587 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
592 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
593 Spawn
*spawn
= userdata
;
594 char timeout
[FORMAT_TIMESPAN_MAX
];
598 log_warning("Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete", spawn
->cmd
, spawn
->pid
,
599 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
604 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
605 Spawn
*spawn
= userdata
;
610 switch (si
->si_code
) {
612 if (si
->si_status
== 0)
613 log_debug("Process '%s' succeeded.", spawn
->cmd
);
615 log_full(spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
,
616 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
621 log_error("Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
624 log_error("Process '%s' failed due to unknown reason.", spawn
->cmd
);
627 sd_event_exit(sd_event_source_get_event(s
), ret
);
631 static int spawn_wait(Spawn
*spawn
) {
632 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
637 r
= sd_event_new(&e
);
641 if (spawn
->timeout_usec
> 0) {
642 usec_t usec
, age_usec
;
644 usec
= now(CLOCK_MONOTONIC
);
645 age_usec
= usec
- spawn
->event_birth_usec
;
646 if (age_usec
< spawn
->timeout_usec
) {
647 if (spawn
->timeout_warn_usec
> 0 &&
648 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
649 spawn
->timeout_warn_usec
> age_usec
) {
650 spawn
->timeout_warn_usec
-= age_usec
;
652 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
653 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
654 on_spawn_timeout_warning
, spawn
);
659 spawn
->timeout_usec
-= age_usec
;
661 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
662 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
668 if (spawn
->fd_stdout
>= 0) {
669 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
674 if (spawn
->fd_stderr
>= 0) {
675 r
= sd_event_add_io(e
, NULL
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
680 r
= sd_event_add_child(e
, NULL
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
684 return sd_event_loop(e
);
687 int udev_event_spawn(UdevEvent
*event
,
691 char *result
, size_t ressize
) {
692 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
693 _cleanup_strv_free_
char **argv
= NULL
;
701 assert(result
|| ressize
== 0);
703 /* pipes from child to parent */
704 if (result
|| log_get_max_level() >= LOG_INFO
)
705 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
706 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
708 if (log_get_max_level() >= LOG_INFO
)
709 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
710 return log_error_errno(errno
, "Failed to create pipe for command '%s': %m", cmd
);
712 argv
= strv_split_full(cmd
, NULL
, SPLIT_QUOTES
|SPLIT_RELAX
);
716 if (isempty(argv
[0]))
717 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
718 "Invalid command '%s'", cmd
);
720 /* allow programs in /usr/lib/udev/ to be called without the path */
721 if (!path_is_absolute(argv
[0])) {
724 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
728 free_and_replace(argv
[0], program
);
731 r
= device_get_properties_strv(event
->dev
, &envp
);
733 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
735 log_debug("Starting '%s'", cmd
);
737 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
739 return log_error_errno(r
, "Failed to fork() to execute command '%s': %m", cmd
);
741 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
744 (void) close_all_fds(NULL
, 0);
745 (void) rlimit_nofile_safe();
747 execve(argv
[0], argv
, envp
);
751 /* parent closed child's ends of pipes */
752 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
753 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
758 .accept_failure
= accept_failure
,
759 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
760 .timeout_usec
= timeout_usec
,
761 .event_birth_usec
= event
->birth_usec
,
762 .fd_stdout
= outpipe
[READ_END
],
763 .fd_stderr
= errpipe
[READ_END
],
765 .result_size
= ressize
,
767 r
= spawn_wait(&spawn
);
769 return log_error_errno(r
, "Failed to wait for spawned command '%s': %m", cmd
);
772 result
[spawn
.result_len
] = '\0';
774 return r
; /* 0 for success, and positive if the program failed */
777 static int rename_netif(UdevEvent
*event
) {
778 sd_device
*dev
= event
->dev
;
783 return 0; /* No new name is requested. */
785 r
= sd_device_get_sysname(dev
, &oldname
);
787 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
789 if (streq(event
->name
, oldname
))
790 return 0; /* The interface name is already requested name. */
792 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
793 return 0; /* Rename the interface only when it is added. */
795 r
= sd_device_get_ifindex(dev
, &ifindex
);
797 return 0; /* Device is not a network interface. */
799 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
801 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
803 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
804 ifindex
, oldname
, event
->name
);
806 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
807 r
= device_add_property(dev
, "ID_RENAMING", "1");
809 return log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
811 r
= device_rename(dev
, event
->name
);
813 return log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
815 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
820 static int update_devnode(UdevEvent
*event
) {
821 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
);
840 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
843 if (!gid_is_valid(event
->gid
)) {
844 r
= device_get_devnode_gid(dev
, &event
->gid
);
848 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
851 if (event
->mode
== MODE_INVALID
) {
852 r
= device_get_devnode_mode(dev
, &event
->mode
);
853 if (r
< 0 && r
!= -ENOENT
)
854 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
857 /* default 0660 if a group is assigned */
865 apply
= device_for_action(dev
, DEVICE_ACTION_ADD
) ||
866 uid_is_valid(event
->uid
) ||
867 gid_is_valid(event
->gid
) ||
868 event
->mode
!= MODE_INVALID
;
869 return udev_node_add(dev
, apply
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
872 static void event_execute_rules_on_remove(
875 Hashmap
*properties_list
,
878 sd_device
*dev
= event
->dev
;
881 r
= device_read_db_internal(dev
, true);
883 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
885 r
= device_tag_index(dev
, NULL
, false);
887 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
889 r
= device_delete_db(dev
);
891 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
893 if (sd_device_get_devnum(dev
, NULL
) >= 0)
894 (void) udev_watch_end(dev
);
896 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
898 if (sd_device_get_devnum(dev
, NULL
) >= 0)
899 (void) udev_node_remove(dev
);
902 static int udev_event_on_move(UdevEvent
*event
) {
903 sd_device
*dev
= event
->dev
;
906 if (event
->dev_db_clone
&&
907 sd_device_get_devnum(dev
, NULL
) < 0) {
908 r
= device_copy_properties(dev
, event
->dev_db_clone
);
910 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
913 /* Drop previously added property */
914 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
916 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property: %m");
921 int udev_event_execute_rules(UdevEvent
*event
,
923 Hashmap
*properties_list
,
925 const char *subsystem
;
935 r
= sd_device_get_subsystem(dev
, &subsystem
);
937 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
939 r
= device_get_action(dev
, &action
);
941 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
943 if (action
== DEVICE_ACTION_REMOVE
) {
944 event_execute_rules_on_remove(event
, timeout_usec
, properties_list
, rules
);
948 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
950 return log_device_debug_errno(dev
, r
, "Failed to clone sd_device object: %m");
952 if (event
->dev_db_clone
&& sd_device_get_devnum(dev
, NULL
) >= 0)
953 /* Disable watch during event processing. */
954 (void) udev_watch_end(event
->dev_db_clone
);
956 if (action
== DEVICE_ACTION_MOVE
) {
957 r
= udev_event_on_move(event
);
962 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, properties_list
);
964 return log_device_debug_errno(dev
, r
, "Failed to apply udev rules: %m");
966 r
= rename_netif(event
);
970 r
= update_devnode(event
);
974 /* preserve old, or get new initialization timestamp */
975 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
977 return log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp: %m");
979 /* (re)write database file */
980 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
982 return log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/: %m");
984 r
= device_update_db(dev
);
986 return log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
988 device_set_is_initialized(dev
);
990 event
->dev_db_clone
= sd_device_unref(event
->dev_db_clone
);
995 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
) {
1001 ORDERED_HASHMAP_FOREACH_KEY(val
, command
, event
->run_list
, i
) {
1002 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
1004 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
1005 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
1006 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
1008 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
1010 if (event
->exec_delay_usec
> 0) {
1011 char buf
[FORMAT_TIMESPAN_MAX
];
1013 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
1014 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
1015 (void) usleep(event
->exec_delay_usec
);
1018 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
1019 r
= udev_event_spawn(event
, timeout_usec
, false, command
, NULL
, 0);
1020 if (r
> 0) /* returned value is positive when program fails */
1021 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);