1 /* SPDX-License-Identifier: GPL-2.0-or-later */
14 #include "alloc-util.h"
15 #include "device-private.h"
16 #include "device-util.h"
19 #include "format-util.h"
20 #include "netif-naming-scheme.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
, int log_level
) {
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
),
71 .log_level_was_debug
= log_level
== LOG_DEBUG
,
72 .default_log_level
= log_level
,
78 UdevEvent
*udev_event_free(UdevEvent
*event
) {
82 sd_device_unref(event
->dev
);
83 sd_device_unref(event
->dev_db_clone
);
84 sd_netlink_unref(event
->rtnl
);
85 ordered_hashmap_free_free_key(event
->run_list
);
86 ordered_hashmap_free_free_free(event
->seclabel_list
);
87 free(event
->program_result
);
98 FORMAT_SUBST_KERNEL_NUMBER
,
100 FORMAT_SUBST_DEVPATH
,
110 _FORMAT_SUBST_TYPE_MAX
,
111 _FORMAT_SUBST_TYPE_INVALID
= -EINVAL
,
112 } FormatSubstitutionType
;
114 struct subst_map_entry
{
117 FormatSubstitutionType type
;
120 static const struct subst_map_entry map
[] = {
121 { .name
= "devnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
},
122 { .name
= "tempnode", .fmt
= 'N', .type
= FORMAT_SUBST_DEVNODE
}, /* deprecated */
123 { .name
= "attr", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
},
124 { .name
= "sysfs", .fmt
= 's', .type
= FORMAT_SUBST_ATTR
}, /* deprecated */
125 { .name
= "env", .fmt
= 'E', .type
= FORMAT_SUBST_ENV
},
126 { .name
= "kernel", .fmt
= 'k', .type
= FORMAT_SUBST_KERNEL
},
127 { .name
= "number", .fmt
= 'n', .type
= FORMAT_SUBST_KERNEL_NUMBER
},
128 { .name
= "driver", .fmt
= 'd', .type
= FORMAT_SUBST_DRIVER
},
129 { .name
= "devpath", .fmt
= 'p', .type
= FORMAT_SUBST_DEVPATH
},
130 { .name
= "id", .fmt
= 'b', .type
= FORMAT_SUBST_ID
},
131 { .name
= "major", .fmt
= 'M', .type
= FORMAT_SUBST_MAJOR
},
132 { .name
= "minor", .fmt
= 'm', .type
= FORMAT_SUBST_MINOR
},
133 { .name
= "result", .fmt
= 'c', .type
= FORMAT_SUBST_RESULT
},
134 { .name
= "parent", .fmt
= 'P', .type
= FORMAT_SUBST_PARENT
},
135 { .name
= "name", .fmt
= 'D', .type
= FORMAT_SUBST_NAME
},
136 { .name
= "links", .fmt
= 'L', .type
= FORMAT_SUBST_LINKS
},
137 { .name
= "root", .fmt
= 'r', .type
= FORMAT_SUBST_ROOT
},
138 { .name
= "sys", .fmt
= 'S', .type
= FORMAT_SUBST_SYS
},
141 static const char *format_type_to_string(FormatSubstitutionType t
) {
142 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
143 if (map
[i
].type
== t
)
148 static char format_type_to_char(FormatSubstitutionType t
) {
149 for (size_t i
= 0; i
< ELEMENTSOF(map
); i
++)
150 if (map
[i
].type
== t
)
155 static int get_subst_type(const char **str
, bool strict
, FormatSubstitutionType
*ret_type
, char ret_attr
[static UDEV_PATH_SIZE
]) {
156 const char *p
= *str
, *q
= NULL
;
170 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
171 if ((q
= startswith(p
, map
[i
].name
)))
173 } else if (*p
== '%') {
180 for (i
= 0; i
< ELEMENTSOF(map
); i
++)
181 if (*p
== map
[i
].fmt
) {
188 /* When 'strict' flag is set, then '$' and '%' must be escaped. */
189 return strict
? -EINVAL
: 0;
192 const char *start
, *end
;
196 end
= strchr(start
, '}');
201 if (len
== 0 || len
>= UDEV_PATH_SIZE
)
204 strnscpy(ret_attr
, UDEV_PATH_SIZE
, start
, len
);
210 *ret_type
= map
[i
].type
;
214 static int safe_atou_optional_plus(const char *s
, unsigned *ret
) {
221 /* Returns 1 if plus, 0 if no plus, negative on error */
223 p
= endswith(s
, "+");
225 s
= strndupa(s
, p
- s
);
227 r
= safe_atou(s
, ret
);
234 static ssize_t
udev_event_subst_format(
236 FormatSubstitutionType type
,
240 sd_device
*parent
, *dev
= event
->dev
;
241 const char *val
= NULL
;
246 case FORMAT_SUBST_DEVPATH
:
247 r
= sd_device_get_devpath(dev
, &val
);
250 l
= strpcpy(&s
, l
, val
);
252 case FORMAT_SUBST_KERNEL
:
253 r
= sd_device_get_sysname(dev
, &val
);
256 l
= strpcpy(&s
, l
, val
);
258 case FORMAT_SUBST_KERNEL_NUMBER
:
259 r
= sd_device_get_sysnum(dev
, &val
);
264 l
= strpcpy(&s
, l
, val
);
266 case FORMAT_SUBST_ID
:
267 if (!event
->dev_parent
)
269 r
= sd_device_get_sysname(event
->dev_parent
, &val
);
272 l
= strpcpy(&s
, l
, val
);
274 case FORMAT_SUBST_DRIVER
:
275 if (!event
->dev_parent
)
277 r
= sd_device_get_driver(event
->dev_parent
, &val
);
282 l
= strpcpy(&s
, l
, val
);
284 case FORMAT_SUBST_MAJOR
:
285 case FORMAT_SUBST_MINOR
: {
288 r
= sd_device_get_devnum(dev
, &devnum
);
289 if (r
< 0 && r
!= -ENOENT
)
291 l
= strpcpyf(&s
, l
, "%u", r
< 0 ? 0 : type
== FORMAT_SUBST_MAJOR
? major(devnum
) : minor(devnum
));
294 case FORMAT_SUBST_RESULT
: {
295 unsigned index
= 0; /* 0 means whole string */
298 if (!event
->program_result
)
301 if (!isempty(attr
)) {
302 r
= safe_atou_optional_plus(attr
, &index
);
310 l
= strpcpy(&s
, l
, event
->program_result
);
312 const char *start
, *p
;
315 p
= skip_leading_chars(event
->program_result
, NULL
);
317 for (i
= 1; i
< index
; i
++) {
318 while (*p
&& !strchr(WHITESPACE
, *p
))
320 p
= skip_leading_chars(p
, NULL
);
325 log_device_debug(dev
, "requested part of result string not found");
330 /* %c{2+} copies the whole string from the second part on */
332 l
= strpcpy(&s
, l
, start
);
334 while (*p
&& !strchr(WHITESPACE
, *p
))
336 l
= strnpcpy(&s
, l
, start
, p
- start
);
341 case FORMAT_SUBST_ATTR
: {
342 char vbuf
[UDEV_NAME_SIZE
];
348 /* try to read the value specified by "[dmi/id]product_name" */
349 if (udev_resolve_subsys_kernel(attr
, vbuf
, sizeof(vbuf
), true) == 0)
352 /* try to read the attribute the device */
354 (void) sd_device_get_sysattr_value(dev
, attr
, &val
);
356 /* try to read the attribute of the parent device, other matches have selected */
357 if (!val
&& event
->dev_parent
&& event
->dev_parent
!= dev
)
358 (void) sd_device_get_sysattr_value(event
->dev_parent
, attr
, &val
);
363 /* strip trailing whitespace, and replace unwanted characters */
365 strscpy(vbuf
, sizeof(vbuf
), val
);
366 delete_trailing_chars(vbuf
, NULL
);
367 count
= udev_replace_chars(vbuf
, UDEV_ALLOWED_CHARS_INPUT
);
369 log_device_debug(dev
, "%i character(s) replaced", count
);
370 l
= strpcpy(&s
, l
, vbuf
);
373 case FORMAT_SUBST_PARENT
:
374 r
= sd_device_get_parent(dev
, &parent
);
379 r
= sd_device_get_devname(parent
, &val
);
384 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
386 case FORMAT_SUBST_DEVNODE
:
387 r
= sd_device_get_devname(dev
, &val
);
392 l
= strpcpy(&s
, l
, val
);
394 case FORMAT_SUBST_NAME
:
396 l
= strpcpy(&s
, l
, event
->name
);
397 else if (sd_device_get_devname(dev
, &val
) >= 0)
398 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
400 r
= sd_device_get_sysname(dev
, &val
);
403 l
= strpcpy(&s
, l
, val
);
406 case FORMAT_SUBST_LINKS
:
407 FOREACH_DEVICE_DEVLINK(dev
, val
)
409 l
= strpcpy(&s
, l
, val
+ STRLEN("/dev/"));
411 l
= strpcpyl(&s
, l
, " ", val
+ STRLEN("/dev/"), NULL
);
415 case FORMAT_SUBST_ROOT
:
416 l
= strpcpy(&s
, l
, "/dev");
418 case FORMAT_SUBST_SYS
:
419 l
= strpcpy(&s
, l
, "/sys");
421 case FORMAT_SUBST_ENV
:
424 r
= sd_device_get_property_value(dev
, attr
, &val
);
429 l
= strpcpy(&s
, l
, val
);
432 assert_not_reached("Unknown format substitution type");
442 size_t udev_event_apply_format(UdevEvent
*event
,
443 const char *src
, char *dest
, size_t size
,
444 bool replace_whitespace
) {
455 FormatSubstitutionType type
;
456 char attr
[UDEV_PATH_SIZE
];
459 r
= get_subst_type(&s
, false, &type
, attr
);
461 log_device_warning_errno(event
->dev
, r
, "Invalid format string, ignoring: %s", src
);
464 if (size
< 2) /* need space for this char and the terminating NUL */
471 subst_len
= udev_event_subst_format(event
, type
, attr
, dest
, size
);
473 log_device_warning_errno(event
->dev
, subst_len
,
474 "Failed to substitute variable '$%s' or apply format '%%%c', ignoring: %m",
475 format_type_to_string(type
), format_type_to_char(type
));
479 /* FORMAT_SUBST_RESULT handles spaces itself */
480 if (replace_whitespace
&& type
!= FORMAT_SUBST_RESULT
)
481 /* udev_replace_whitespace can replace in-place,
482 * and does nothing if subst_len == 0 */
483 subst_len
= udev_replace_whitespace(dest
, dest
, subst_len
);
494 int udev_check_format(const char *value
, size_t *offset
, const char **hint
) {
495 FormatSubstitutionType type
;
496 const char *s
= value
;
497 char attr
[UDEV_PATH_SIZE
];
501 r
= get_subst_type(&s
, true, &type
, attr
);
506 *hint
= "invalid substitution type";
513 if (IN_SET(type
, FORMAT_SUBST_ATTR
, FORMAT_SUBST_ENV
) && isempty(attr
)) {
517 *hint
= "attribute value missing";
521 if (type
== FORMAT_SUBST_RESULT
&& !isempty(attr
)) {
524 r
= safe_atou_optional_plus(attr
, &i
);
529 *hint
= "attribute value not a valid number";
538 static int on_spawn_io(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
539 Spawn
*spawn
= userdata
;
546 assert(fd
== spawn
->fd_stdout
|| fd
== spawn
->fd_stderr
);
547 assert(!spawn
->result
|| spawn
->result_len
< spawn
->result_size
);
549 if (fd
== spawn
->fd_stdout
&& spawn
->result
) {
550 p
= spawn
->result
+ spawn
->result_len
;
551 size
= spawn
->result_size
- spawn
->result_len
;
557 l
= read(fd
, p
, size
- 1);
562 log_device_error_errno(spawn
->device
, errno
,
563 "Failed to read stdout of '%s': %m", spawn
->cmd
);
569 if (fd
== spawn
->fd_stdout
&& spawn
->result
)
570 spawn
->result_len
+= l
;
572 /* Log output only if we watch stderr. */
573 if (l
> 0 && spawn
->fd_stderr
>= 0) {
574 _cleanup_strv_free_
char **v
= NULL
;
577 r
= strv_split_newlines_full(&v
, p
, EXTRACT_RETAIN_ESCAPE
);
579 log_device_debug(spawn
->device
,
580 "Failed to split output from '%s'(%s), ignoring: %m",
581 spawn
->cmd
, fd
== spawn
->fd_stdout
? "out" : "err");
584 log_device_debug(spawn
->device
, "'%s'(%s) '%s'", spawn
->cmd
,
585 fd
== spawn
->fd_stdout
? "out" : "err", *q
);
592 /* Re-enable the event source if we did not encounter EOF */
594 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
596 log_device_error_errno(spawn
->device
, r
,
597 "Failed to reactivate IO source of '%s'", spawn
->cmd
);
601 static int on_spawn_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
602 Spawn
*spawn
= userdata
;
603 char timeout
[FORMAT_TIMESPAN_MAX
];
607 DEVICE_TRACE_POINT(spawn_timeout
, spawn
->device
, spawn
->cmd
);
609 kill_and_sigcont(spawn
->pid
, spawn
->timeout_signal
);
611 log_device_error(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] timed out after %s, killing",
612 spawn
->cmd
, spawn
->pid
,
613 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_usec
, USEC_PER_SEC
));
618 static int on_spawn_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
619 Spawn
*spawn
= userdata
;
620 char timeout
[FORMAT_TIMESPAN_MAX
];
624 log_device_warning(spawn
->device
, "Spawned process '%s' ["PID_FMT
"] is taking longer than %s to complete",
625 spawn
->cmd
, spawn
->pid
,
626 format_timespan(timeout
, sizeof(timeout
), spawn
->timeout_warn_usec
, USEC_PER_SEC
));
631 static int on_spawn_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
632 Spawn
*spawn
= userdata
;
637 switch (si
->si_code
) {
639 if (si
->si_status
== 0)
640 log_device_debug(spawn
->device
, "Process '%s' succeeded.", spawn
->cmd
);
642 log_device_full(spawn
->device
, spawn
->accept_failure
? LOG_DEBUG
: LOG_WARNING
,
643 "Process '%s' failed with exit code %i.", spawn
->cmd
, si
->si_status
);
648 log_device_error(spawn
->device
, "Process '%s' terminated by signal %s.", spawn
->cmd
, signal_to_string(si
->si_status
));
651 log_device_error(spawn
->device
, "Process '%s' failed due to unknown reason.", spawn
->cmd
);
654 DEVICE_TRACE_POINT(spawn_exit
, spawn
->device
, spawn
->cmd
);
656 sd_event_exit(sd_event_source_get_event(s
), ret
);
660 static int spawn_wait(Spawn
*spawn
) {
661 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
662 _cleanup_(sd_event_source_unrefp
) sd_event_source
*sigchld_source
= NULL
;
663 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stdout_source
= NULL
;
664 _cleanup_(sd_event_source_unrefp
) sd_event_source
*stderr_source
= NULL
;
669 r
= sd_event_new(&e
);
671 return log_device_debug_errno(spawn
->device
, r
, "Failed to allocate sd-event object: %m");
673 if (spawn
->timeout_usec
> 0) {
674 usec_t usec
, age_usec
;
676 usec
= now(CLOCK_MONOTONIC
);
677 age_usec
= usec
- spawn
->event_birth_usec
;
678 if (age_usec
< spawn
->timeout_usec
) {
679 if (spawn
->timeout_warn_usec
> 0 &&
680 spawn
->timeout_warn_usec
< spawn
->timeout_usec
&&
681 spawn
->timeout_warn_usec
> age_usec
) {
682 spawn
->timeout_warn_usec
-= age_usec
;
684 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
685 usec
+ spawn
->timeout_warn_usec
, USEC_PER_SEC
,
686 on_spawn_timeout_warning
, spawn
);
688 return log_device_debug_errno(spawn
->device
, r
, "Failed to create timeout warning event source: %m");
691 spawn
->timeout_usec
-= age_usec
;
693 r
= sd_event_add_time(e
, NULL
, CLOCK_MONOTONIC
,
694 usec
+ spawn
->timeout_usec
, USEC_PER_SEC
, on_spawn_timeout
, spawn
);
696 return log_device_debug_errno(spawn
->device
, r
, "Failed to create timeout event source: %m");
700 if (spawn
->fd_stdout
>= 0) {
701 r
= sd_event_add_io(e
, &stdout_source
, spawn
->fd_stdout
, EPOLLIN
, on_spawn_io
, spawn
);
703 return log_device_debug_errno(spawn
->device
, r
, "Failed to create stdio event source: %m");
704 r
= sd_event_source_set_enabled(stdout_source
, SD_EVENT_ONESHOT
);
706 return log_device_debug_errno(spawn
->device
, r
, "Failed to enable stdio event source: %m");
709 if (spawn
->fd_stderr
>= 0) {
710 r
= sd_event_add_io(e
, &stderr_source
, spawn
->fd_stderr
, EPOLLIN
, on_spawn_io
, spawn
);
712 return log_device_debug_errno(spawn
->device
, r
, "Failed to create stderr event source: %m");
713 r
= sd_event_source_set_enabled(stderr_source
, SD_EVENT_ONESHOT
);
715 return log_device_debug_errno(spawn
->device
, r
, "Failed to enable stderr event source: %m");
718 r
= sd_event_add_child(e
, &sigchld_source
, spawn
->pid
, WEXITED
, on_spawn_sigchld
, spawn
);
720 return log_device_debug_errno(spawn
->device
, r
, "Failed to create sigchild event source: %m");
721 /* SIGCHLD should be processed after IO is complete */
722 r
= sd_event_source_set_priority(sigchld_source
, SD_EVENT_PRIORITY_NORMAL
+ 1);
724 return log_device_debug_errno(spawn
->device
, r
, "Failed to set priority to sigchild event source: %m");
726 return sd_event_loop(e
);
729 int udev_event_spawn(UdevEvent
*event
,
734 char *result
, size_t ressize
) {
735 _cleanup_close_pair_
int outpipe
[2] = {-1, -1}, errpipe
[2] = {-1, -1};
736 _cleanup_strv_free_
char **argv
= NULL
;
744 assert(result
|| ressize
== 0);
746 /* pipes from child to parent */
747 if (result
|| log_get_max_level() >= LOG_INFO
)
748 if (pipe2(outpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
749 return log_device_error_errno(event
->dev
, errno
,
750 "Failed to create pipe for command '%s': %m", cmd
);
752 if (log_get_max_level() >= LOG_INFO
)
753 if (pipe2(errpipe
, O_NONBLOCK
|O_CLOEXEC
) != 0)
754 return log_device_error_errno(event
->dev
, errno
,
755 "Failed to create pipe for command '%s': %m", cmd
);
757 r
= strv_split_full(&argv
, cmd
, NULL
, EXTRACT_UNQUOTE
| EXTRACT_RELAX
| EXTRACT_RETAIN_ESCAPE
);
759 return log_device_error_errno(event
->dev
, r
, "Failed to split command: %m");
761 if (isempty(argv
[0]))
762 return log_device_error_errno(event
->dev
, SYNTHETIC_ERRNO(EINVAL
),
763 "Invalid command '%s'", cmd
);
765 /* allow programs in /usr/lib/udev/ to be called without the path */
766 if (!path_is_absolute(argv
[0])) {
769 program
= path_join(UDEVLIBEXECDIR
, argv
[0]);
773 free_and_replace(argv
[0], program
);
776 r
= device_get_properties_strv(event
->dev
, &envp
);
778 return log_device_error_errno(event
->dev
, r
, "Failed to get device properties");
780 log_device_debug(event
->dev
, "Starting '%s'", cmd
);
782 r
= safe_fork("(spawn)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
784 return log_device_error_errno(event
->dev
, r
,
785 "Failed to fork() to execute command '%s': %m", cmd
);
787 if (rearrange_stdio(-1, outpipe
[WRITE_END
], errpipe
[WRITE_END
]) < 0)
790 (void) close_all_fds(NULL
, 0);
791 (void) rlimit_nofile_safe();
793 DEVICE_TRACE_POINT(spawn_exec
, event
->dev
, cmd
);
795 execve(argv
[0], argv
, envp
);
799 /* parent closed child's ends of pipes */
800 outpipe
[WRITE_END
] = safe_close(outpipe
[WRITE_END
]);
801 errpipe
[WRITE_END
] = safe_close(errpipe
[WRITE_END
]);
804 .device
= event
->dev
,
807 .accept_failure
= accept_failure
,
808 .timeout_warn_usec
= udev_warn_timeout(timeout_usec
),
809 .timeout_usec
= timeout_usec
,
810 .timeout_signal
= timeout_signal
,
811 .event_birth_usec
= event
->birth_usec
,
812 .fd_stdout
= outpipe
[READ_END
],
813 .fd_stderr
= errpipe
[READ_END
],
815 .result_size
= ressize
,
817 r
= spawn_wait(&spawn
);
819 return log_device_error_errno(event
->dev
, r
,
820 "Failed to wait for spawned command '%s': %m", cmd
);
823 result
[spawn
.result_len
] = '\0';
825 return r
; /* 0 for success, and positive if the program failed */
828 static int rename_netif(UdevEvent
*event
) {
829 sd_device
*dev
= event
->dev
;
834 return 0; /* No new name is requested. */
836 r
= sd_device_get_sysname(dev
, &oldname
);
838 return log_device_error_errno(dev
, r
, "Failed to get sysname: %m");
840 if (streq(event
->name
, oldname
))
841 return 0; /* The interface name is already requested name. */
843 if (!device_for_action(dev
, SD_DEVICE_ADD
))
844 return 0; /* Rename the interface only when it is added. */
846 r
= sd_device_get_ifindex(dev
, &ifindex
);
848 return 0; /* Device is not a network interface. */
850 return log_device_error_errno(dev
, r
, "Failed to get ifindex: %m");
852 if (naming_scheme_has(NAMING_REPLACE_STRICTLY
) &&
853 !ifname_valid(event
->name
)) {
854 log_device_warning(dev
, "Invalid network interface name, ignoring: %s", event
->name
);
858 /* Set ID_RENAMING boolean property here, and drop it in the corresponding move uevent later. */
859 r
= device_add_property(dev
, "ID_RENAMING", "1");
861 return log_device_warning_errno(dev
, r
, "Failed to add 'ID_RENAMING' property: %m");
863 r
= device_rename(dev
, event
->name
);
865 return log_device_warning_errno(dev
, r
, "Failed to update properties with new name '%s': %m", event
->name
);
867 /* Also set ID_RENAMING boolean property to cloned sd_device object and save it to database
868 * before calling rtnl_set_link_name(). Otherwise, clients (e.g., systemd-networkd) may receive
869 * RTM_NEWLINK netlink message before the database is updated. */
870 r
= device_add_property(event
->dev_db_clone
, "ID_RENAMING", "1");
872 return log_device_warning_errno(event
->dev_db_clone
, r
, "Failed to add 'ID_RENAMING' property: %m");
874 r
= device_update_db(event
->dev_db_clone
);
876 return log_device_debug_errno(event
->dev_db_clone
, r
, "Failed to update database under /run/udev/data/: %m");
878 r
= rtnl_set_link_name(&event
->rtnl
, ifindex
, event
->name
);
880 return log_device_error_errno(dev
, r
, "Failed to rename network interface %i from '%s' to '%s': %m",
881 ifindex
, oldname
, event
->name
);
883 log_device_debug(dev
, "Network interface %i is renamed from '%s' to '%s'", ifindex
, oldname
, event
->name
);
888 static int update_devnode(UdevEvent
*event
) {
889 sd_device
*dev
= event
->dev
;
892 r
= sd_device_get_devnum(dev
, NULL
);
896 return log_device_error_errno(dev
, r
, "Failed to get devnum: %m");
898 /* remove/update possible left-over symlinks from old database entry */
899 (void) udev_node_update_old_links(dev
, event
->dev_db_clone
);
901 if (!uid_is_valid(event
->uid
)) {
902 r
= device_get_devnode_uid(dev
, &event
->uid
);
903 if (r
< 0 && r
!= -ENOENT
)
904 return log_device_error_errno(dev
, r
, "Failed to get devnode UID: %m");
907 if (!gid_is_valid(event
->gid
)) {
908 r
= device_get_devnode_gid(dev
, &event
->gid
);
909 if (r
< 0 && r
!= -ENOENT
)
910 return log_device_error_errno(dev
, r
, "Failed to get devnode GID: %m");
913 if (event
->mode
== MODE_INVALID
) {
914 r
= device_get_devnode_mode(dev
, &event
->mode
);
915 if (r
< 0 && r
!= -ENOENT
)
916 return log_device_error_errno(dev
, r
, "Failed to get devnode mode: %m");
918 if (event
->mode
== MODE_INVALID
&& gid_is_valid(event
->gid
) && event
->gid
> 0)
919 /* If group is set, but mode is not set, "upgrade" mode for the group. */
922 bool apply_mac
= device_for_action(dev
, SD_DEVICE_ADD
);
924 return udev_node_add(dev
, apply_mac
, event
->mode
, event
->uid
, event
->gid
, event
->seclabel_list
);
927 static int event_execute_rules_on_remove(
932 Hashmap
*properties_list
,
935 sd_device
*dev
= event
->dev
;
938 r
= device_read_db_internal(dev
, true);
940 log_device_debug_errno(dev
, r
, "Failed to read database under /run/udev/data/: %m");
942 r
= device_tag_index(dev
, NULL
, false);
944 log_device_debug_errno(dev
, r
, "Failed to remove corresponding tag files under /run/udev/tag/, ignoring: %m");
946 r
= device_delete_db(dev
);
948 log_device_debug_errno(dev
, r
, "Failed to delete database under /run/udev/data/, ignoring: %m");
950 (void) udev_watch_end(inotify_fd
, dev
);
952 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
);
954 if (sd_device_get_devnum(dev
, NULL
) >= 0)
955 (void) udev_node_remove(dev
);
960 static int udev_event_on_move(sd_device
*dev
) {
963 /* Drop previously added property */
964 r
= device_add_property(dev
, "ID_RENAMING", NULL
);
966 return log_device_debug_errno(dev
, r
, "Failed to remove 'ID_RENAMING' property: %m");
971 static int copy_all_tags(sd_device
*d
, sd_device
*s
) {
980 FOREACH_DEVICE_TAG(s
, tag
) {
981 r
= device_add_tag(d
, tag
, false);
989 int udev_event_execute_rules(
991 int inotify_fd
, /* This may be negative */
994 Hashmap
*properties_list
,
997 sd_device_action_t action
;
1006 r
= sd_device_get_action(dev
, &action
);
1008 return log_device_error_errno(dev
, r
, "Failed to get ACTION: %m");
1010 if (action
== SD_DEVICE_REMOVE
)
1011 return event_execute_rules_on_remove(event
, inotify_fd
, timeout_usec
, timeout_signal
, properties_list
, rules
);
1013 /* Disable watch during event processing. */
1014 (void) udev_watch_end(inotify_fd
, event
->dev
);
1016 r
= device_clone_with_db(dev
, &event
->dev_db_clone
);
1018 return log_device_debug_errno(dev
, r
, "Failed to clone sd_device object: %m");
1020 r
= copy_all_tags(dev
, event
->dev_db_clone
);
1022 log_device_warning_errno(dev
, r
, "Failed to copy all tags from old database entry, ignoring: %m");
1024 if (action
== SD_DEVICE_MOVE
) {
1025 r
= udev_event_on_move(event
->dev
);
1030 DEVICE_TRACE_POINT(rules_start
, dev
);
1032 r
= udev_rules_apply_to_event(rules
, event
, timeout_usec
, timeout_signal
, properties_list
);
1034 return log_device_debug_errno(dev
, r
, "Failed to apply udev rules: %m");
1036 DEVICE_TRACE_POINT(rules_finished
, dev
);
1038 r
= rename_netif(event
);
1042 r
= update_devnode(event
);
1046 /* preserve old, or get new initialization timestamp */
1047 r
= device_ensure_usec_initialized(dev
, event
->dev_db_clone
);
1049 return log_device_debug_errno(dev
, r
, "Failed to set initialization timestamp: %m");
1051 /* (re)write database file */
1052 r
= device_tag_index(dev
, event
->dev_db_clone
, true);
1054 return log_device_debug_errno(dev
, r
, "Failed to update tags under /run/udev/tag/: %m");
1056 r
= device_update_db(dev
);
1058 return log_device_debug_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
1060 device_set_is_initialized(dev
);
1062 /* Yes, we run update_devnode() twice, because in the first invocation, that is before update of udev database,
1063 * it could happen that two contenders are replacing each other's symlink. Hence we run it again to make sure
1064 * symlinks point to devices that claim them with the highest priority. */
1065 return update_devnode(event
);
1068 void udev_event_execute_run(UdevEvent
*event
, usec_t timeout_usec
, int timeout_signal
) {
1069 const char *command
;
1073 ORDERED_HASHMAP_FOREACH_KEY(val
, command
, event
->run_list
) {
1074 UdevBuiltinCommand builtin_cmd
= PTR_TO_UDEV_BUILTIN_CMD(val
);
1076 if (builtin_cmd
!= _UDEV_BUILTIN_INVALID
) {
1077 log_device_debug(event
->dev
, "Running built-in command \"%s\"", command
);
1078 r
= udev_builtin_run(event
->dev
, builtin_cmd
, command
, false);
1080 log_device_debug_errno(event
->dev
, r
, "Failed to run built-in command \"%s\", ignoring: %m", command
);
1082 if (event
->exec_delay_usec
> 0) {
1083 char buf
[FORMAT_TIMESPAN_MAX
];
1085 log_device_debug(event
->dev
, "Delaying execution of \"%s\" for %s.",
1086 command
, format_timespan(buf
, sizeof(buf
), event
->exec_delay_usec
, USEC_PER_SEC
));
1087 (void) usleep(event
->exec_delay_usec
);
1090 log_device_debug(event
->dev
, "Running command \"%s\"", command
);
1092 r
= udev_event_spawn(event
, timeout_usec
, timeout_signal
, false, command
, NULL
, 0);
1094 log_device_warning_errno(event
->dev
, r
, "Failed to execute '%s', ignoring: %m", command
);
1095 else if (r
> 0) /* returned value is positive when program fails */
1096 log_device_debug(event
->dev
, "Command \"%s\" returned %d (error), ignoring.", command
, r
);
1101 int udev_event_process_inotify_watch(UdevEvent
*event
, int inotify_fd
) {
1105 assert(inotify_fd
>= 0);
1111 if (device_for_action(dev
, SD_DEVICE_REMOVE
))
1114 if (event
->inotify_watch
)
1115 (void) udev_watch_begin(inotify_fd
, dev
);
1117 (void) udev_watch_end(inotify_fd
, dev
);