mirror of
https://github.com/systemd/systemd.git
synced 2025-03-31 14:50:15 +03:00
udev-manager: rework initialization of device monitor
Let's determine the socket for device monitor based on its name.
This commit is contained in:
parent
4a2c33847f
commit
a75abf1ab9
@ -1073,13 +1073,39 @@ Manager* manager_new(void) {
|
||||
return manager;
|
||||
}
|
||||
|
||||
static int manager_listen_fds(Manager *manager, int *ret_netlink) {
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
_cleanup_close_ int netlink_fd = -EBADF;
|
||||
static int manager_init_device_monitor(Manager *manager, int fd) {
|
||||
int r;
|
||||
|
||||
assert(manager);
|
||||
|
||||
/* This takes passed file descriptor on success. */
|
||||
|
||||
if (fd >= 0) {
|
||||
if (manager->monitor)
|
||||
return log_warning_errno(SYNTHETIC_ERRNO(EALREADY), "Received multiple netlink socket (%i), ignoring.", fd);
|
||||
|
||||
r = sd_is_socket(fd, AF_NETLINK, SOCK_RAW, /* listening = */ -1);
|
||||
if (r < 0)
|
||||
return log_warning_errno(r, "Failed to verify socket type of %i, ignoring: %m", fd);
|
||||
if (r == 0)
|
||||
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Received invalid netlink socket (%i), ignoring.", fd);
|
||||
} else {
|
||||
if (manager->monitor)
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize device monitor: %m");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int manager_listen_fds(Manager *manager) {
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
int r;
|
||||
|
||||
assert(manager);
|
||||
assert(ret_netlink);
|
||||
|
||||
int n = sd_listen_fds_with_names(/* unset_environment = */ true, &names);
|
||||
if (n < 0)
|
||||
@ -1091,69 +1117,32 @@ static int manager_listen_fds(Manager *manager, int *ret_netlink) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = SD_LISTEN_FDS_START + i;
|
||||
|
||||
if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
|
||||
if (netlink_fd >= 0) {
|
||||
log_debug("Received multiple netlink socket (%s), ignoring.", names[i]);
|
||||
goto unused;
|
||||
}
|
||||
|
||||
netlink_fd = fd;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (streq(names[i], "varlink"))
|
||||
r = 0; /* The fd will be handled by sd_varlink_server_listen_auto(). */
|
||||
else if (streq(names[i], "systemd-udevd-control.socket"))
|
||||
r = manager_init_ctrl(manager, fd);
|
||||
else if (streq(names[i], "systemd-udevd-kernel.socket"))
|
||||
r = manager_init_device_monitor(manager, fd);
|
||||
else
|
||||
r = log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Received unexpected fd (%s), ignoring.", names[i]);
|
||||
if (r < 0)
|
||||
goto unused;
|
||||
|
||||
unused:
|
||||
close_and_notify_warn(fd, names[i]);
|
||||
close_and_notify_warn(fd, names[i]);
|
||||
}
|
||||
|
||||
*ret_netlink = TAKE_FD(netlink_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int manager_init_device_monitor(Manager *manager, int fd_uevent) {
|
||||
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
|
||||
_cleanup_close_ int fd = fd_uevent;
|
||||
int r;
|
||||
|
||||
assert(manager);
|
||||
|
||||
/* This consumes passed file descriptor. */
|
||||
|
||||
r = device_monitor_new_full(&monitor, MONITOR_GROUP_KERNEL, fd);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize device monitor: %m");
|
||||
TAKE_FD(fd);
|
||||
|
||||
(void) sd_device_monitor_set_description(monitor, "manager");
|
||||
|
||||
manager->monitor = TAKE_PTR(monitor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int manager_init(Manager *manager) {
|
||||
_cleanup_close_ int fd_uevent = -EBADF;
|
||||
_cleanup_free_ char *cgroup = NULL;
|
||||
int r;
|
||||
|
||||
assert(manager);
|
||||
|
||||
r = manager_listen_fds(manager, &fd_uevent);
|
||||
r = manager_listen_fds(manager);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to listen on fds: %m");
|
||||
|
||||
r = manager_init_device_monitor(manager, TAKE_FD(fd_uevent));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
_cleanup_free_ char *cgroup = NULL;
|
||||
r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to get cgroup, ignoring: %m");
|
||||
@ -1169,7 +1158,12 @@ static int manager_start_device_monitor(Manager *manager) {
|
||||
int r;
|
||||
|
||||
assert(manager);
|
||||
assert(manager->monitor);
|
||||
|
||||
r = manager_init_device_monitor(manager, -EBADF);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
(void) sd_device_monitor_set_description(manager->monitor, "manager");
|
||||
|
||||
r = sd_device_monitor_attach_event(manager->monitor, manager->event);
|
||||
if (r < 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user