From: Davin McCall Date: Sat, 18 Jun 2016 19:25:17 +0000 (+0100) Subject: Separate the commencement of logging and the opening of the control socket X-Git-Tag: v0.03~7 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=ab2922171431d164c6bf9a7638b79eec96e59c91;p=oweals%2Fdinit.git Separate the commencement of logging and the opening of the control socket into two distinct functions. --- diff --git a/src/dinit.cc b/src/dinit.cc index 5fd99d5..1b8fc65 100644 --- a/src/dinit.cc +++ b/src/dinit.cc @@ -68,11 +68,14 @@ EventLoop_t eventLoop = EventLoop_t(); static void sigint_reboot_cb(EventLoop_t *eloop) noexcept; static void sigquit_cb(EventLoop_t *eloop) noexcept; static void sigterm_cb(EventLoop_t *eloop) noexcept; -static void open_control_socket(EventLoop_t *loop) noexcept; -static void close_control_socket(EventLoop_t *loop) noexcept; +static void close_control_socket() noexcept; static void control_socket_cb(EventLoop_t *loop, int fd); +void open_control_socket() noexcept; +void setup_external_log() noexcept; + + class ControlSocketWatcher : public EventLoop_t::FdWatcher { Rearm fdEvent(EventLoop_t &loop, int fd, int flags) override @@ -324,7 +327,7 @@ int main(int argc, char **argv) sigterm_watcher.addWatch(eventLoop, SIGTERM); // Try to open control socket (may fail due to readonly filesystem) - open_control_socket(&eventLoop); + open_control_socket(); #ifdef __linux__ if (am_system_init) { @@ -387,7 +390,7 @@ int main(int argc, char **argv) eventLoop.run(); } - close_control_socket(&eventLoop); + close_control_socket(); if (am_system_init) { if (shutdown_type == ShutdownType::CONTINUE) { @@ -451,7 +454,7 @@ static void control_socket_cb(EventLoop_t *loop, int sockfd) } } -static void open_control_socket(EventLoop_t *loop) noexcept +void open_control_socket() noexcept { if (! control_socket_open) { const char * saddrname = control_socket_path; @@ -504,16 +507,23 @@ static void open_control_socket(EventLoop_t *loop) noexcept return; } - control_socket_open = true; - control_socket_io.addWatch(eventLoop, sockfd, IN_EVENTS); + try { + control_socket_io.addWatch(eventLoop, sockfd, IN_EVENTS); + control_socket_open = true; + } + catch (std::exception &e) + { + log(LogLevel::ERROR, "Could not setup I/O on control socket: ", e.what()); + close(sockfd); + } } } -static void close_control_socket(EventLoop_t *loop) noexcept +static void close_control_socket() noexcept { if (control_socket_open) { int fd = control_socket_io.fd; - control_socket_io.deregister(*loop); + control_socket_io.deregister(eventLoop); close(fd); // Unlink the socket: @@ -521,7 +531,7 @@ static void close_control_socket(EventLoop_t *loop) noexcept } } -static void setup_external_log() noexcept +void setup_external_log() noexcept { if (! external_log_open) { @@ -566,14 +576,6 @@ static void setup_external_log() noexcept } } -// Called from service when the system is "rw ready" (filesystem mounted r/w etc). -// May be called more than once. -void system_rw_ready() noexcept -{ - open_control_socket(&eventLoop); - setup_external_log(); -} - /* handle SIGINT signal (generated by kernel when ctrl+alt+del pressed) */ static void sigint_reboot_cb(EventLoop_t *eloop) noexcept { @@ -586,7 +588,7 @@ static void sigquit_cb(EventLoop_t *eloop) noexcept // This allows remounting the filesystem read-only if the dinit binary has been // unlinked. In that case the kernel holds the binary open, so that it can't be // properly removed. - close_control_socket(eloop); + close_control_socket(); execl("/sbin/shutdown", "/sbin/shutdown", (char *) 0); log(LogLevel::ERROR, "Error executing /sbin/shutdown: ", strerror(errno)); } diff --git a/src/service.cc b/src/service.cc index b81c323..c81b0cb 100644 --- a/src/service.cc +++ b/src/service.cc @@ -23,7 +23,8 @@ */ // from dinit.cc: -void system_rw_ready() noexcept; +void open_control_socket() noexcept; +void setup_external_log() noexcept; extern EventLoop_t eventLoop; // Find the requested service by name @@ -68,6 +69,7 @@ void ServiceRecord::stopped() noexcept { if (service_type != ServiceType::SCRIPTED && service_type != ServiceType::BGPROCESS && onstart_flags.runs_on_console) { tcsetpgrp(0, getpgrp()); + discard_console_log_buffer(); releaseConsole(); } @@ -569,7 +571,10 @@ void ServiceRecord::started() noexcept notifyListeners(ServiceEvent::STARTED); if (onstart_flags.rw_ready) { - system_rw_ready(); + open_control_socket(); + } + if (onstart_flags.log_ready) { + setup_external_log(); } if (force_stop || desired_state == ServiceState::STOPPED) {