]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: move listen_fds() to udev-manager.c
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Dec 2024 20:02:53 +0000 (05:02 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 14 Dec 2024 07:19:57 +0000 (16:19 +0900)
Also
- drop redundant error message when manager_init() failed,
- close unexpected fds.

No functional change, just refactoring.

src/udev/udev-manager.c
src/udev/udev-manager.h
src/udev/udevd.c

index 32bc0db345c0bf5be912327e1bb26f7456ef8c3f..5a8f1613e17ebe03445bffadd84c8b092377ae7a 100644 (file)
@@ -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");
 
index 5921d333d1f7dc56a848734e0cb705ba8b5d0ac5..9e96e3ff288bb161b9aeb8aa20cec5999ecb1216 100644 (file)
@@ -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);
index c46fcaa03826058196eaaebf75cb10ad9d1dce1d..c5b5f652a7c77d3d706b33c9a74f9176da65663f 100644 (file)
@@ -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"
 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);
-        if (r < 0)
-                return log_error_errno(r, "Failed to listen on fds: %m");
-
-        r = manager_init(manager, fd_ctrl, fd_uevent);
+        r = manager_init(manager);
         if (r < 0)
-                return log_error_errno(r, "Failed to create manager: %m");
+                return r;
 
         if (arg_daemonize) {
                 pid_t pid;