mirror of
https://github.com/systemd/systemd.git
synced 2025-03-08 08:58:27 +03:00
udev: move listen_fds() to udev-manager.c
Also - drop redundant error message when manager_init() failed, - close unexpected fds. No functional change, just refactoring.
This commit is contained in:
parent
7ab25935f3
commit
a2840b9599
@ -1227,15 +1227,69 @@ void manager_adjust_arguments(Manager *manager) {
|
||||
}
|
||||
}
|
||||
|
||||
int manager_init(Manager *manager, int fd_ctrl, int fd_uevent) {
|
||||
static int listen_fds(int *ret_ctrl, int *ret_netlink) {
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
_cleanup_close_ int ctrl_fd = -EBADF, netlink_fd = -EBADF;
|
||||
|
||||
assert(ret_ctrl);
|
||||
assert(ret_netlink);
|
||||
|
||||
int n = sd_listen_fds_with_names(/* unset_environment = */ true, &names);
|
||||
if (n < 0)
|
||||
return n;
|
||||
|
||||
if (strv_length(names) != (size_t) n)
|
||||
return -EIO;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = SD_LISTEN_FDS_START + i;
|
||||
|
||||
if (sd_is_socket(fd, AF_UNIX, SOCK_SEQPACKET, -1) > 0) {
|
||||
if (ctrl_fd >= 0) {
|
||||
log_debug("Received multiple seqpacket socket (%s), ignoring.", names[i]);
|
||||
goto unused;
|
||||
}
|
||||
|
||||
ctrl_fd = fd;
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
log_debug("Received unexpected fd (%s), ignoring.", names[i]);
|
||||
|
||||
unused:
|
||||
close_and_notify_warn(fd, names[i]);
|
||||
}
|
||||
|
||||
*ret_ctrl = TAKE_FD(ctrl_fd);
|
||||
*ret_netlink = TAKE_FD(netlink_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int manager_init(Manager *manager) {
|
||||
_cleanup_close_ int fd_ctrl = -EBADF, fd_uevent = -EBADF;
|
||||
_cleanup_free_ char *cgroup = NULL;
|
||||
int r;
|
||||
|
||||
assert(manager);
|
||||
|
||||
r = listen_fds(&fd_ctrl, &fd_uevent);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to listen on fds: %m");
|
||||
|
||||
r = udev_ctrl_new_from_fd(&manager->ctrl, fd_ctrl);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize udev control socket: %m");
|
||||
TAKE_FD(fd_ctrl);
|
||||
|
||||
r = udev_ctrl_enable_receiving(manager->ctrl);
|
||||
if (r < 0)
|
||||
@ -1244,6 +1298,7 @@ int manager_init(Manager *manager, int fd_ctrl, int fd_uevent) {
|
||||
r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize device monitor: %m");
|
||||
TAKE_FD(fd_uevent);
|
||||
|
||||
(void) sd_device_monitor_set_description(manager->monitor, "manager");
|
||||
|
||||
|
@ -53,7 +53,7 @@ Manager* manager_free(Manager *manager);
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
|
||||
|
||||
void manager_adjust_arguments(Manager *manager);
|
||||
int manager_init(Manager *manager, int fd_ctrl, int fd_uevent);
|
||||
int manager_init(Manager *manager);
|
||||
int manager_main(Manager *manager);
|
||||
|
||||
bool devpath_conflict(const char *a, const char *b);
|
||||
|
@ -8,8 +8,6 @@
|
||||
#include <getopt.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sd-daemon.h"
|
||||
|
||||
#include "conf-parser.h"
|
||||
#include "env-file.h"
|
||||
#include "errno-util.h"
|
||||
@ -31,40 +29,6 @@
|
||||
static bool arg_debug = false;
|
||||
static int arg_daemonize = false;
|
||||
|
||||
static int listen_fds(int *ret_ctrl, int *ret_netlink) {
|
||||
int ctrl_fd = -EBADF, netlink_fd = -EBADF;
|
||||
|
||||
assert(ret_ctrl);
|
||||
assert(ret_netlink);
|
||||
|
||||
int n = sd_listen_fds(true);
|
||||
if (n < 0)
|
||||
return n;
|
||||
|
||||
for (int fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
|
||||
if (sd_is_socket(fd, AF_UNIX, SOCK_SEQPACKET, -1) > 0) {
|
||||
if (ctrl_fd >= 0)
|
||||
return -EINVAL;
|
||||
ctrl_fd = fd;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
|
||||
if (netlink_fd >= 0)
|
||||
return -EINVAL;
|
||||
netlink_fd = fd;
|
||||
continue;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
*ret_ctrl = ctrl_fd;
|
||||
*ret_netlink = netlink_fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static DEFINE_CONFIG_PARSE_ENUM(config_parse_resolve_name_timing, resolve_name_timing, ResolveNameTiming);
|
||||
|
||||
static int manager_parse_udev_config(Manager *manager) {
|
||||
@ -286,7 +250,6 @@ static int parse_argv(int argc, char *argv[], Manager *manager) {
|
||||
|
||||
int run_udevd(int argc, char *argv[]) {
|
||||
_cleanup_(manager_freep) Manager *manager = NULL;
|
||||
int fd_ctrl = -EBADF, fd_uevent = -EBADF;
|
||||
int r;
|
||||
|
||||
log_setup();
|
||||
@ -330,13 +293,9 @@ int run_udevd(int argc, char *argv[]) {
|
||||
if (r < 0 && r != -EEXIST)
|
||||
return log_error_errno(r, "Failed to create /run/udev: %m");
|
||||
|
||||
r = listen_fds(&fd_ctrl, &fd_uevent);
|
||||
r = manager_init(manager);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to listen on fds: %m");
|
||||
|
||||
r = manager_init(manager, fd_ctrl, fd_uevent);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to create manager: %m");
|
||||
return r;
|
||||
|
||||
if (arg_daemonize) {
|
||||
pid_t pid;
|
||||
|
Loading…
x
Reference in New Issue
Block a user