1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-25 06:03:40 +03:00

Merge pull request #11376 from yuwata/11365-v2

udev: initialize sockets before fork()
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-01-10 09:08:57 +01:00 committed by GitHub
commit a1b939dfc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -465,7 +465,7 @@ static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *
static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) { static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) {
_cleanup_(sd_device_unrefp) sd_device *dev = first_device; _cleanup_(sd_device_unrefp) sd_device *dev = first_device;
_cleanup_(manager_freep) Manager *manager = _manager; _cleanup_(manager_freep) Manager *manager = _manager;
int r, ret; int r;
assert(manager); assert(manager);
assert(monitor); assert(monitor);
@ -508,11 +508,7 @@ static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device
if (r < 0) if (r < 0)
return log_error_errno(r, "Event loop failed: %m"); return log_error_errno(r, "Event loop failed: %m");
r = sd_event_get_exit_code(manager->event, &ret); return 0;
if (r < 0)
return log_error_errno(r, "Failed to get exit code: %m");
return ret;
} }
static int worker_spawn(Manager *manager, struct event *event) { static int worker_spawn(Manager *manager, struct event *event) {
@ -1595,7 +1591,7 @@ static int parse_argv(int argc, char *argv[]) {
static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cgroup) { static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cgroup) {
_cleanup_(manager_freep) Manager *manager = NULL; _cleanup_(manager_freep) Manager *manager = NULL;
int r, fd_worker; int r;
assert(ret); assert(ret);
@ -1609,25 +1605,13 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
.cgroup = cgroup, .cgroup = cgroup,
}; };
udev_builtin_init();
r = udev_rules_new(&manager->rules, arg_resolve_name_timing);
if (!manager->rules)
return log_error_errno(r, "Failed to read udev rules: %m");
manager->ctrl = udev_ctrl_new_from_fd(fd_ctrl); manager->ctrl = udev_ctrl_new_from_fd(fd_ctrl);
if (!manager->ctrl) if (!manager->ctrl)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize udev control socket"); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize udev control socket");
if (fd_ctrl < 0) { r = udev_ctrl_enable_receiving(manager->ctrl);
r = udev_ctrl_enable_receiving(manager->ctrl); if (r < 0)
if (r < 0) return log_error_errno(r, "Failed to bind udev control socket: %m");
return log_error_errno(r, "Failed to bind udev control socket: %m");
}
fd_ctrl = udev_ctrl_get_fd(manager->ctrl);
if (fd_ctrl < 0)
return log_error_errno(fd_ctrl, "Failed to get udev control socket fd: %m");
r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent); r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent);
if (r < 0) if (r < 0)
@ -1635,6 +1619,18 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
(void) sd_device_monitor_set_receive_buffer_size(manager->monitor, 128 * 1024 * 1024); (void) sd_device_monitor_set_receive_buffer_size(manager->monitor, 128 * 1024 * 1024);
r = device_monitor_enable_receiving(manager->monitor);
if (r < 0)
return log_error_errno(r, "Failed to bind netlink socket: %m");
*ret = TAKE_PTR(manager);
return 0;
}
static int main_loop(Manager *manager) {
int fd_worker, fd_ctrl, r;
/* unnamed socket from workers to the main daemon */ /* unnamed socket from workers to the main daemon */
r = socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, manager->worker_watch); r = socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, manager->worker_watch);
if (r < 0) if (r < 0)
@ -1680,6 +1676,10 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create watchdog event source: %m"); return log_error_errno(r, "Failed to create watchdog event source: %m");
fd_ctrl = udev_ctrl_get_fd(manager->ctrl);
if (fd_ctrl < 0)
return log_error_errno(fd_ctrl, "Failed to get udev control socket fd: %m");
r = sd_event_add_io(manager->event, &manager->ctrl_event, fd_ctrl, EPOLLIN, on_ctrl_msg, manager); r = sd_event_add_io(manager->event, &manager->ctrl_event, fd_ctrl, EPOLLIN, on_ctrl_msg, manager);
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create udev control event source: %m"); return log_error_errno(r, "Failed to create udev control event source: %m");
@ -1714,20 +1714,11 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to create post event source: %m"); return log_error_errno(r, "Failed to create post event source: %m");
*ret = TAKE_PTR(manager); udev_builtin_init();
return 0; r = udev_rules_new(&manager->rules, arg_resolve_name_timing);
} if (!manager->rules)
return log_error_errno(r, "Failed to read udev rules: %m");
static int main_loop(int fd_ctrl, int fd_uevent, const char *cgroup) {
_cleanup_(manager_freep) Manager *manager = NULL;
int r;
r = manager_new(&manager, fd_ctrl, fd_uevent, cgroup);
if (r < 0) {
r = log_error_errno(r, "Failed to allocate manager object: %m");
goto exit;
}
r = udev_rules_apply_static_dev_perms(manager->rules); r = udev_rules_apply_static_dev_perms(manager->rules);
if (r < 0) if (r < 0)
@ -1738,24 +1729,18 @@ static int main_loop(int fd_ctrl, int fd_uevent, const char *cgroup) {
"STATUS=Processing with %u children at max", arg_children_max); "STATUS=Processing with %u children at max", arg_children_max);
r = sd_event_loop(manager->event); r = sd_event_loop(manager->event);
if (r < 0) { if (r < 0)
log_error_errno(r, "Event loop failed: %m"); log_error_errno(r, "Event loop failed: %m");
goto exit;
}
sd_event_get_exit_code(manager->event, &r);
exit:
sd_notify(false, sd_notify(false,
"STOPPING=1\n" "STOPPING=1\n"
"STATUS=Shutting down..."); "STATUS=Shutting down...");
if (manager)
udev_ctrl_cleanup(manager->ctrl);
return r; return r;
} }
static int run(int argc, char *argv[]) { static int run(int argc, char *argv[]) {
_cleanup_free_ char *cgroup = NULL; _cleanup_free_ char *cgroup = NULL;
_cleanup_(manager_freep) Manager *manager = NULL;
int fd_ctrl = -1, fd_uevent = -1; int fd_ctrl = -1, fd_uevent = -1;
int r; int r;
@ -1832,10 +1817,14 @@ static int run(int argc, char *argv[]) {
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to listen on fds: %m"); return log_error_errno(r, "Failed to listen on fds: %m");
r = manager_new(&manager, fd_ctrl, fd_uevent, cgroup);
if (r < 0)
return log_error_errno(r, "Failed to create manager: %m");
if (arg_daemonize) { if (arg_daemonize) {
pid_t pid; pid_t pid;
log_info("starting version " GIT_VERSION); log_info("Starting version " GIT_VERSION);
/* connect /dev/null to stdin, stdout, stderr */ /* connect /dev/null to stdin, stdout, stderr */
if (log_get_max_level() < LOG_DEBUG) { if (log_get_max_level() < LOG_DEBUG) {
@ -1859,7 +1848,10 @@ static int run(int argc, char *argv[]) {
log_debug_errno(r, "Failed to adjust OOM score, ignoring: %m"); log_debug_errno(r, "Failed to adjust OOM score, ignoring: %m");
} }
return main_loop(fd_ctrl, fd_uevent, cgroup); r = main_loop(manager);
/* FIXME: move this into manager_free() */
udev_ctrl_cleanup(manager->ctrl);
return r;
} }
DEFINE_MAIN_FUNCTION(run); DEFINE_MAIN_FUNCTION(run);