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
;
45 usec_t event_birth_usec
;
54 UdevEvent
*udev_event_new(sd_device
*dev
, usec_t exec_delay_usec
, sd_netlink
*rtnl
) {
59 event
= new(UdevEvent
, 1);
63 *event
= (UdevEvent
) {
64 .dev
= sd_device_ref(dev
),
65 .birth_usec
= now(CLOCK_MONOTONIC
),
66 .exec_delay_usec
= exec_delay_usec
,
67 .rtnl
= sd_netlink_ref(rtnl
),
76 UdevEvent
*udev_event_free(UdevEvent
*event
) {
80 sd_device_unref(event
->dev
);
81 sd_device_unref(event
->dev_db_clone
);
82 sd_netlink_unref(event
->rtnl
);
83 ordered_hashmap_free_free_key(event
->run_list
);
84 ordered_hashmap_free_free_free(event
->seclabel_list
);
85 free(event
->program_result
);
96 FORMAT_SUBST_KERNEL_NUMBER
,
108 _FORMAT_SUBST_TYPE_MAX
,
109 _FORMAT_SUBST_TYPE_INVALID
= -1
110 } FormatSubstitutionType
;
112 struct subst_map_entry
{
115 FormatSubstitutionType type
;
118 static const struct subst_map_entry map
[] = {
119 { .name
= "devnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
},
120 { .name
= "tempnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
}, /* deprecated */
121 { .name
= "attr", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
},
122 { .name
= "sysfs", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
}, /* deprecated */
123 { .name
= "env", .fmt
= 'E', .type
= FORMAT_SUBST_ENV
},
124 { .name
= "kernel", .fmt
= 'k', .type
= FORMAT_SUBST_KERNEL
},
125 { .name
= "number", .fmt
= 'n', .type
= FORMAT_SUBST_KERNEL_NUMBER
},
126 { .name
= "driver", .fmt
= 'd', .type
= FORMAT_SUBST_DRIVER
},
127 { .name
= "devpath", .fmt
= 'p', .type
= FORMAT_SUBST_DEVPATH
},
128 { .name
= "id", .fmt
= 'b', .type
= FORMAT_SUBST_ID
},
129 { .name
= "major", .fmt
= 'M', .type
= FORMAT_SUBST_MAJOR
},
130 { .name
= "minor", .fmt
= 'm', .type
= FORMAT_SUBST_MINOR
},
131 { .name
= "result", .fmt
= 'c', .type
= FORMAT_SUBST_RESULT
},
132 { .name
= "parent", .fmt
= 'P', .type
= FORMAT_SUBST_PARENT
},
133 { .name
= "name", .fmt
= 'D', .type
= FORMAT_SUBST_NAME
},
134 { .name
= "links", .fmt
= 'L', .type
= FORMAT_SUBST_LINKS
},
135 { .name
= "root", .fmt
= 'r', .type
= FORMAT_SUBST_ROOT
},
136 { .name
= "sys", .fmt
= 'S', .type
= FORMAT_SUBST_SYS
},
139 static const char *format_type_to_string(FormatSubstitutionType t
) {
140 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
141 if (map
[i
].type
== t
)
146 static char format_type_to_char(FormatSubstitutionType t
) {
147 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
148 if (map
[i
].type
== t
)
153 static int get_subst_type(const char **str
, bool strict
, FormatSubstitutionType
*ret_type
, char ret_attr
[static UTIL_PATH_SIZE
]) {
154 const char *p
= *str
, *q
= NULL
;
168 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
169 if ((q
= startswith(p
, map
[i
].name
)))
171 } else if (*p
== '%') {
178 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
179 if (*p
== map
[i
].fmt
) {
186 /* When 'strict' flag is set, then '$' and '%' must be escaped. */
187 return strict
? -EINVAL
: 0;
190 const char *start
, *end
;
194 end
= strchr(start
, '}');
199 if (len
== 0 || len
>= UTIL_PATH_SIZE
)
202 strnscpy(ret_attr
, UTIL_PATH_SIZE
, start
, len
);
208 *ret_type
= map
[i
].type
;
212 static int safe_atou_optional_plus(const char *s
, unsigned *ret
) {
219 /* Returns 1 if plus, 0 if no plus, negative on error */
221 p
= endswith(s
, "+");
223 s
= strndupa(s
, p
- s
);
225 r
= safe_atou(s
, ret
);
232 static ssize_t
udev_event_subst_format(
234 FormatSubstitutionType type
,
238 sd_device
*parent
, *dev
= event
->dev
;
239 const char *val
= NULL
;
244 case FORMAT_SUBST_DEVPATH
:
245 r
= sd_device_get_devpath(dev
, &val
);
248 l
= strpcpy(&s
, l
, val
);
250 case FORMAT_SUBST_KERNEL
:
251 r
= sd_device_get_sysname(dev
, &val
);
254 l
= strpcpy(&s
, l
, val
);
256 case FORMAT_SUBST_KERNEL_NUMBER
:
257 r
= sd_device_get_sysnum(dev
, &val
);
262 l
= strpcpy(&s
, l
, val
);
264 case FORMAT_SUBST_ID
:
265 if (!event
->dev_parent
)
267 r
= sd_device_get_sysname(event
->dev_parent
, &val
);
270 l
= strpcpy(&s
, l
, val
);
272 case FORMAT_SUBST_DRIVER
:
273 if (!event
->dev_parent
)
275 r
= sd_device_get_driver(event
->dev_parent
, &val
);
280 l
= strpcpy(&s
, l
, val
);
282 case FORMAT_SUBST_MAJOR
:
283 case FORMAT_SUBST_MINOR
: {
286 r
= sd_device_get_devnum(dev
, &devnum
);
287 if (r
< 0 && r
!= -ENOENT
)
289 l
= strpcpyf(&s
, l
, "%u", r
< 0 ? 0 : type
== FORMAT_SUBST_MAJOR
? major(devnum
) : minor(devnum
));
292 case FORMAT_SUBST_RESULT
: {
293 unsigned index
= 0; /* 0 means whole string */
296 if (!event
->program_result
)
299 if (!isempty(attr
)) {
300 r
= safe_atou_optional_plus(attr
, &index
);
308 l
= strpcpy(&s
, l
, event
->program_result
);
310 const char *start
, *p
;
313 p
= skip_leading_chars(event
->program_result
, NULL
);
315 for (i
= 1; i
< index
; i
++) {
316 while (*p
&& !strchr(WHITESPACE
, *p
))
318 p
= skip_leading_chars(p
, NULL
);
323 log_device_debug(dev
, "requested part of result string not found");
328 /* %c{2+} copies the whole string from the second part on */
330 l
= strpcpy(&s
, l
, start
);
332 while (*p
&& !strchr(WHITESPACE
, *p
))
334 l
= strnpcpy(&s
, l
, start
, p
- start
);
339 case FORMAT_SUBST_ATTR
: {
340 char vbuf
[UTIL_NAME_SIZE
];
346 /* try to read the value specified by "[dmi/id]product_name" */
347 if (util_resolve_subsys_kernel(attr
, vbuf
, sizeof(vbuf
), true) == 0)
350 /* try to read the attribute the device */
352 (void) sd_device_get_sysattr_value(dev
, attr
, &val
);
354 /* try to read the attribute of the parent device, other matches have selected */
355 if (!val
&& event
->dev_parent
&& event
->dev_parent
!= dev
)
356 (void) sd_device_get_sysattr_value(event
->dev_parent
, attr
, &val
);
361 /* strip trailing whitespace, and replace unwanted characters */
363 strscpy(vbuf
, sizeof(vbuf
), val
);
364 delete_trailing_chars(vbuf
, NULL
);
365 count
= util_replace_chars(vbuf
, UDEV_ALLOWED_CHARS_INPUT
);
367 log_device_debug(dev
, "%i character(s) replaced", count
);
368 l
= strpcpy(&s
, l
, vbuf
);
371 case FORMAT_SUBST_PARENT
:
372 r
= sd_device_get_parent(dev
, &parent
);
377 r
= sd_device_get_devname(parent
, &val
);
382 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
384 case FORMAT_SUBST_DEVNODE
:
385 r
= sd_device_get_devname(dev
, &val
);
390 l
= strpcpy(&s
, l
, val
);
392 case FORMAT_SUBST_NAME
:
394 l
= strpcpy(&s
, l
, event
->name
);
395 else if (sd_device_get_devname(dev
, &val
) >= 0)
396 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
398 r
= sd_device_get_sysname(dev
, &val
);
401 l
= strpcpy(&s
, l
, val
);
404 case FORMAT_SUBST_LINKS
:
405 FOREACH_DEVICE_DEVLINK(dev
, val
)
407 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
409 l
= strpcpyl(&s
, l
, " ", val
+ STRLEN("/dev/"), NULL
);
413 case FORMAT_SUBST_ROOT
:
414 l
= strpcpy(&s
, l
, "/dev");
416 case FORMAT_SUBST_SYS
:
417 l
= strpcpy(&s
, l
, "/sys");
419 case FORMAT_SUBST_ENV
:
422 r
= sd_device_get_property_value(dev
, attr
, &val
);
427 l
= strpcpy(&s
, l
, val
);
430 assert_not_reached("Unknown format substitution type");
440 size_t udev_event_apply_format(UdevEvent
*event
,
441 const char *src
, char *dest
, size_t size
,
442 bool replace_whitespace
) {
453 FormatSubstitutionType type
;
454 char attr
[UTIL_PATH_SIZE
];
457 r
= get_subst_type(&s
, false, &type
, attr
);
459 log_device_warning_errno(event
->dev
, r
, "Invalid format string, ignoring: %s", src
);
462 if (size
< 2) /* need space for this char and the terminating NUL */
469 subst_len
= udev_event_subst_format(event
, type
, attr
, dest
, size
);
471 log_device_warning_errno(event
->dev
, subst_len
,
472 "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
473 format_type_to_string(type
), format_type_to_char(type
));
477 /* FORMAT_SUBST_RESULT handles spaces itself */
478 if (replace_whitespace
&& type
!= FORMAT_SUBST_RESULT
)
479 /* util_replace_whitespace can replace in-place,
480 * and does nothing if subst_len == 0 */
481 subst_len
= util_replace_whitespace(dest
, dest
, subst_len
);
492 int udev_check_format(const char *value
, size_t *offset
, const char **hint
) {
493 FormatSubstitutionType type
;
494 const char *s
= value
;
495 char attr
[UTIL_PATH_SIZE
];
499 r
= get_subst_type(&s
, true, &type
, attr
);
504 *hint
= "invalid substitution type";
511 if (IN_SET(type
, FORMAT_SUBST_ATTR
, FORMAT_SUBST_ENV
) && isempty(attr
)) {
515 *hint
= "attribute value missing";
519 if (type
== FORMAT_SUBST_RESULT
&& !isempty(attr
)) {
522 r
= safe_atou_optional_plus(attr
, &i
);
527 *hint
= "attribute value not a valid number";
536 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
537 Spawn
*spawn
= userdata
;
544 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
545 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
547 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
548 p
= spawn
->result
+ spawn
->result_len
;
549 size
= spawn
->result_size
- spawn
->result_len
;
555 l
= read(fd
, p
, size
- 1);
560 log_device_error_errno(spawn
->device
, errno
,
561 "Failed to read stdout of '%s': %m", spawn
->cmd
);
567 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
568 spawn
->result_len
+= l
;
570 /* Log output only if we watch stderr. */
571 if (l
> 0 && spawn
->fd_stderr
>= 0) {
572 _cleanup_strv_free_
char **v
= NULL
;
575 v
= strv_split_newlines(p
);
580 log_device_debug(spawn
->device
, "'%s'(%s) '%s'", spawn
->cmd
,
581 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
588 /* Re-enable the event source if we did not encounter EOF */
590 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
592 log_device_error_errno(spawn
->device
, r
,
593 "Failed to reactivate IO source of '%s'", spawn
->cmd
);
597 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
598 Spawn
*spawn
= userdata
;
599 char timeout
[FORMAT_TIMESPAN_MAX
];
603 kill_and_sigcont(spawn
->pid
, spawn
->timeout_signal
);
605 log_device_error(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] timed out after %s, killing",
606 spawn
->cmd
, spawn
->pid
,
607 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
612 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
613 Spawn
*spawn
= userdata
;
614 char timeout
[FORMAT_TIMESPAN_MAX
];
618 log_device_warning(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete",
619 spawn
->cmd
, spawn
->pid
,
620 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
625 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
626 Spawn
*spawn
= userdata
;
631 switch (si
->si_code
) {
633 if (si
->si_status
== 0)
634 log_device_debug(spawn
->device
, "Process '%s' succeeded.", spawn
->cmd
);
636 log_device_full(spawn
->device
, spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
,
637 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
642 log_device_error(spawn
->device
, "Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
645 log_device_error(spawn
->device
, "Process '%s' failed due to unknown reason.", spawn
->cmd
);
648 sd_event_exit(sd_event_source_get_event(s
), ret
);
652 static int spawn_wait(Spawn
*spawn
) {
653 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
654 _cleanup_(sd_event_source_unrefp
) sd_event_source
*sigchld_source
= NULL
;
655 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stdout_source
= NULL
;
656 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stderr_source
= NULL
;
661 r
= sd_event_new(&e
);
665 if (spawn
->timeout_usec
> 0) {
666 usec_t usec
, age_usec
;
668 usec
= now(CLOCK_MONOTONIC
);
669 age_usec
= usec
- spawn
->event_birth_usec
;
670 if (age_usec
< spawn
->timeout_usec
) {
671 if (spawn
->timeout_warn_usec
> 0 &&
672 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
673 spawn
->timeout_warn_usec
> age_usec
) {
674 spawn
->timeout_warn_usec
-= age_usec
;
676 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
677 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
678 on_spawn_timeout_warning
, spawn
);
683 spawn
->timeout_usec
-= age_usec
;
685 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
686 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
692 if (spawn
->fd_stdout
>= 0) {
693 r
= sd_event_add_io(e
, &stdout_source
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
696 r
= sd_event_source_set_enabled(stdout_source
, SD_EVENT_ONESHOT
);
701 if (spawn
->fd_stderr
>= 0) {
702 r
= sd_event_add_io(e
, &stderr_source
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
705 r
= sd_event_source_set_enabled(stderr_source
, SD_EVENT_ONESHOT
);
710 r
= sd_event_add_child(e
, &sigchld_source
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
713 /* SIGCHLD should be processed after IO is complete */
714 r
= sd_event_source_set_priority(sigchld_source
, SD_EVENT_PRIORITY_NORMAL
+ 1);
719 return sd_event_loop(e
);
722 int udev_event_spawn(UdevEvent
*event
,
727 char *result
, size_t ressize
) {
728 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
729 _cleanup_strv_free_
char **argv
= NULL
;
737 assert(result
|| ressize
== 0);
739 /* pipes from child to parent */
740 if (result
|| log_get_max_level() >= LOG_INFO
)
741 if (pipe2(outpipe
, 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 if (log_get_max_level() >= LOG_INFO
)
746 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
747 return log_device_error_errno(event
->dev
, errno
,
748 "Failed to create pipe for command '%s': %m", cmd
);
750 r
= strv_split_full(&argv
, cmd
, NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
| EXTRACT_RETAIN_ESCAPE
);
752 return log_device_error_errno(event
->dev
, r
, "Failed to split command: %m");
754 if (isempty(argv
[0]))
755 return log_device_error_errno(event
->dev
, SYNTHETIC_ERRNO(EINVAL
),
756 "Invalid command '%s'", cmd
);
758 /* allow programs in /usr/lib/udev/ to be called without the path */
759 if (!path_is_absolute(argv
[0])) {
762 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
766 free_and_replace(argv
[0], program
);
769 r
= device_get_properties_strv(event
->dev
, &envp
);
771 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
773 log_device_debug(event
->dev
, "Starting '%s'", cmd
);
775 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
777 return log_device_error_errno(event
->dev
, r
,
778 "Failed to fork() to execute command '%s': %m", cmd
);
780 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
783 (void) close_all_fds(NULL
, 0);
784 (void) rlimit_nofile_safe();
786 execve(argv
[0], argv
, envp
);
790 /* parent closed child's ends of pipes */
791 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
792 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
795 .device
= event
->dev
,
798 .accept_failure
= accept_failure
,
799 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
800 .timeout_usec
= timeout_usec
,
801 .timeout_signal
= timeout_signal
,
802 .event_birth_usec
= event
->birth_usec
,
803 .fd_stdout
= outpipe
[READ_END
],
804 .fd_stderr
= errpipe
[READ_END
],
806 .result_size
= ressize
,
808 r
= spawn_wait(&spawn
);
810 return log_device_error_errno(event
->dev
, r
,
811 "Failed to wait for spawned command '%s': %m", cmd
);
814 result
[spawn
.result_len
] = '\0';
816 return r
; /* 0 for success, and positive if the program failed */
819 static int rename_netif(UdevEvent
*event
) {
820 sd_device
*dev
= event
->dev
;
825 return 0; /* No new name is requested. */
827 r
= sd_device_get_sysname(dev
, &oldname
);
829 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
831 if (streq(event
->name
, oldname
))
832 return 0; /* The interface name is already requested name. */
834 if (!device_for_action(dev
, DEVICE_ACTION_ADD
))
835 return 0; /* Rename the interface only when it is added. */
837 r
= sd_device_get_ifindex(dev
, &ifindex
);
839 return 0; /* Device is not a network interface. */
841 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
843 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
844 r
= device_add_property(dev
, "ID_RENAMING", "1");
846 return log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
848 r
= device_rename(dev
, event
->name
);
850 return log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
852 /* Also set ID_RENAMING boolean property to cloned sd_device object and save it to database
853 * before calling rtnl_set_link_name(). Otherwise, clients (e.g., systemd-networkd) may receive
854 * RTM_NEWLINK netlink message before the database is updated. */
855 r
= device_add_property(event
->dev_db_clone
, "ID_RENAMING", "1");
857 return log_device_warning_errno(event
->dev_db_clone
, r
, "Failed to add 'ID_RENAMING' property: %m");
859 r
= device_update_db(event
->dev_db_clone
);
861 return log_device_debug_errno(event
->dev_db_clone
, r
, "Failed to update database under /run/udev/data/: %m");
863 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
865 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
866 ifindex
, oldname
, event
->name
);
868 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
873 static int update_devnode(UdevEvent
*event
) {
874 sd_device
*dev
= event
->dev
;
877 r
= sd_device_get_devnum(dev
, NULL
);
881 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
883 /* remove/update possible left-over symlinks from old database entry */
884 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
886 if (!uid_is_valid(event
->uid
)) {
887 r
= device_get_devnode_uid(dev
, &event
->uid
);
888 if (r
< 0 && r
!= -ENOENT
)
889 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
892 if (!gid_is_valid(event
->gid
)) {
893 r
= device_get_devnode_gid(dev
, &event
->gid
);
894 if (r
< 0 && r
!= -ENOENT
)
895 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
898 if (event
->mode
== MODE_INVALID
) {
899 r
= device_get_devnode_mode(dev
, &event
->mode
);
900 if (r
< 0 && r
!= -ENOENT
)
901 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
903 if (event
->mode
== MODE_INVALID
&& gid_is_valid(event
->gid
) && event
->gid
> 0)
904 /* If group is set, but mode is not set, "upgrade" mode for the group. */
907 bool apply_mac
= device_for_action(dev
, DEVICE_ACTION_ADD
);
909 return udev_node_add(dev
, apply_mac
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
912 static void event_execute_rules_on_remove(
916 Hashmap
*properties_list
,
919 sd_device
*dev
= event
->dev
;
922 r
= device_read_db_internal(dev
, true);
924 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
926 r
= device_tag_index(dev
, NULL
, false);
928 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
930 r
= device_delete_db(dev
);
932 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
934 if (sd_device_get_devnum(dev
, NULL
) >= 0)
935 (void) udev_watch_end(dev
);
937 (void) udev_rules_apply_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
);
939 if (sd_device_get_devnum(dev
, NULL
) >= 0)
940 (void) udev_node_remove(dev
);
943 static int udev_event_on_move(UdevEvent
*event
) {
944 sd_device
*dev
= event
->dev
;
947 if (sd_device_get_devnum(dev
, NULL
) < 0) {
948 r
= device_copy_properties(dev
, event
->dev_db_clone
);
950 log_device_debug_errno(dev
, r
, "Failed to copy properties from cloned sd_device object, ignoring: %m");
953 /* Drop previously added property */
954 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
956 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property: %m");
961 static int copy_all_tags(sd_device
*d
, sd_device
*s
) {
970 for (tag
= sd_device_get_tag_first(s
); tag
; tag
= sd_device_get_tag_next(s
)) {
971 r
= device_add_tag(d
, tag
, false);
979 int udev_event_execute_rules(UdevEvent
*event
,
982 Hashmap
*properties_list
,
984 const char *subsystem
;
994 r
= sd_device_get_subsystem(dev
, &subsystem
);
996 return log_device_error_errno(dev
, r
, "Failed to get subsystem: %m");
998 r
= device_get_action(dev
, &action
);
1000 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
1002 if (action
== DEVICE_ACTION_REMOVE
) {
1003 event_execute_rules_on_remove(event
, timeout_usec
, timeout_signal
, properties_list
, rules
);
1007 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
1009 return log_device_debug_errno(dev
, r
, "Failed to clone sd_device object: %m");
1011 r
= copy_all_tags(dev
, event
->dev_db_clone
);
1013 log_device_warning_errno(dev
, r
, "Failed to copy all tags from old database entry, ignoring: %m");
1015 if (sd_device_get_devnum(dev
, NULL
) >= 0)
1016 /* Disable watch during event processing. */
1017 (void) udev_watch_end(event
->dev_db_clone
);
1019 if (action
== DEVICE_ACTION_MOVE
) {
1020 r
= udev_event_on_move(event
);
1025 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
);
1027 return log_device_debug_errno(dev
, r
, "Failed to apply udev rules: %m");
1029 r
= rename_netif(event
);
1033 r
= update_devnode(event
);
1037 /* preserve old, or get new initialization timestamp */
1038 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
1040 return log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp: %m");
1042 /* (re)write database file */
1043 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
1045 return log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/: %m");
1047 r
= device_update_db(dev
);
1049 return log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
1051 device_set_is_initialized(dev
);
1056 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
, int timeout_signal
) {
1057 const char *command
;
1061 ORDERED_HASHMAP_FOREACH_KEY(val
, command
, event
->run_list
) {
1062 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
1064 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
1065 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
1066 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
1068 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
1070 if (event
->exec_delay_usec
> 0) {
1071 char buf
[FORMAT_TIMESPAN_MAX
];
1073 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
1074 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
1075 (void) usleep(event
->exec_delay_usec
);
1078 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
1080 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, false, command
, NULL
, 0);
1082 log_device_warning_errno(event
->dev
, r
, "Failed to execute '%s', ignoring: %m", command
);
1083 else if (r
> 0) /* returned value is positive when program fails */
1084 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);