From 23092c17b46fb53f311b2b66088aad94120e4aee Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Mon, 28 Dec 2015 16:15:24 +0000 Subject: [PATCH] Fix bind of control socket, and unlink control socket path on exit --- dinit.cc | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/dinit.cc b/dinit.cc index 2e2f577..c79643f 100644 --- a/dinit.cc +++ b/dinit.cc @@ -4,6 +4,7 @@ #include #include #include + #include #include #include @@ -61,6 +62,7 @@ static void sigint_reboot_cb(struct ev_loop *loop, ev_signal *w, int revents); static void sigquit_cb(struct ev_loop *loop, ev_signal *w, int revents); static void sigterm_cb(struct ev_loop *loop, ev_signal *w, int revents); void open_control_socket(struct ev_loop *loop) noexcept; +void close_control_socket(struct ev_loop *loop) noexcept; struct ev_io control_socket_io; @@ -252,6 +254,8 @@ int main(int argc, char **argv) } } + close_control_socket(ev_default_loop(EVFLAG_AUTO)); + if (am_system_init) { if (do_reboot) { // Fork and execute /sbin/reboot @@ -351,7 +355,7 @@ void open_control_socket(struct ev_loop *loop) noexcept return; } - if (bind(sockfd, (struct sockaddr *) &name, sockaddr_size) == -1) { + if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) { log(LogLevel::ERROR, "Error binding control socket: ", strerror(errno)); close(sockfd); free(name); @@ -380,6 +384,18 @@ void open_control_socket(struct ev_loop *loop) noexcept } } +void close_control_socket(struct ev_loop *loop) noexcept +{ + if (control_socket_open) { + int fd = control_socket_io.fd; + ev_io_stop(loop, &control_socket_io); + close(fd); + + // Unlink the socket: + unlink(control_socket_path); + } +} + /* handle SIGINT signal (generated by kernel when ctrl+alt+del pressed) */ static void sigint_reboot_cb(struct ev_loop *loop, ev_signal *w, int revents) { -- 2.25.1