From fff223cfc2cefe4fb7b387ee1045894e8cb8f536 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Thu, 15 Jun 2017 12:22:17 +0100 Subject: [PATCH] Rename various classes/functions for consistency. ServiceSet to service_set, etc. --- src/control.cc | 46 ++++++++-------- src/control.h | 54 +++++++++---------- src/cpbuffer.h | 2 +- src/dinit-log.cc | 18 +++---- src/dinit-log.h | 4 +- src/dinit.cc | 96 +++++++++++++++------------------ src/dinitctl.cc | 30 +++++------ src/load_service.cc | 70 ++++++++++++------------ src/service-constants.h | 6 +-- src/service-listener.h | 4 +- src/service.cc | 102 +++++++++++++++++------------------ src/service.h | 116 ++++++++++++++++++++-------------------- src/shutdown.cc | 24 ++++----- 13 files changed, 281 insertions(+), 291 deletions(-) diff --git a/src/control.cc b/src/control.cc index 31ec0d3..c91bc4f 100644 --- a/src/control.cc +++ b/src/control.cc @@ -1,7 +1,7 @@ #include "control.h" #include "service.h" -bool ControlConn::processPacket() +bool control_conn_t::processPacket() { using std::string; @@ -35,9 +35,9 @@ bool ControlConn::processPacket() return true; } - auto sd_type = static_cast(rbuf[1]); + auto sd_type = static_cast(rbuf[1]); - service_set->stop_all_services(sd_type); + services->stop_all_services(sd_type); char ackBuf[] = { DINIT_RP_ACK }; if (! queuePacket(ackBuf, 1)) return false; @@ -59,7 +59,7 @@ bool ControlConn::processPacket() return true; } -bool ControlConn::processFindLoad(int pktType) +bool control_conn_t::processFindLoad(int pktType) { using std::string; @@ -94,15 +94,15 @@ bool ControlConn::processFindLoad(int pktType) if (pktType == DINIT_CP_LOADSERVICE) { // LOADSERVICE try { - record = service_set->loadService(serviceName); + record = services->loadService(serviceName); } - catch (ServiceLoadExc &slexc) { + catch (service_load_exc &slexc) { log(LogLevel::ERROR, "Could not load service ", slexc.serviceName, ": ", slexc.excDescription); } } else { // FINDSERVICE - record = service_set->find_service(serviceName.c_str()); + record = services->find_service(serviceName.c_str()); } if (record != nullptr) { @@ -129,7 +129,7 @@ bool ControlConn::processFindLoad(int pktType) return true; } -bool ControlConn::processStartStop(int pktType) +bool control_conn_t::processStartStop(int pktType) { using std::string; @@ -165,7 +165,7 @@ bool ControlConn::processStartStop(int pktType) // start service, mark as required if (do_pin) service->pinStart(); service->start(); - service_set->processQueues(true); + services->processQueues(true); already_there = service->getState() == ServiceState::STARTED; break; case DINIT_CP_STOPSERVICE: @@ -173,21 +173,21 @@ bool ControlConn::processStartStop(int pktType) if (do_pin) service->pinStop(); service->stop(true); service->forceStop(); - service_set->processQueues(false); + services->processQueues(false); already_there = service->getState() == ServiceState::STOPPED; break; case DINIT_CP_WAKESERVICE: // re-start a stopped service (do not mark as required) if (do_pin) service->pinStart(); service->start(false); - service_set->processQueues(true); + services->processQueues(true); already_there = service->getState() == ServiceState::STARTED; break; case DINIT_CP_RELEASESERVICE: // remove required mark, stop if not required by dependents if (do_pin) service->pinStop(); service->stop(false); - service_set->processQueues(false); + services->processQueues(false); already_there = service->getState() == ServiceState::STOPPED; break; } @@ -203,7 +203,7 @@ bool ControlConn::processStartStop(int pktType) return true; } -bool ControlConn::processUnpinService() +bool control_conn_t::processUnpinService() { using std::string; @@ -231,7 +231,7 @@ bool ControlConn::processUnpinService() } else { service->unpin(); - service_set->processQueues(true); + services->processQueues(true); char ack_buf[] = { (char) DINIT_RP_ACK }; if (! queuePacket(ack_buf, 1)) return false; } @@ -242,13 +242,13 @@ bool ControlConn::processUnpinService() return true; } -bool ControlConn::listServices() +bool control_conn_t::listServices() { rbuf.consume(1); // clear request packet chklen = 0; try { - auto slist = service_set->listServices(); + auto slist = services->listServices(); for (auto sptr : slist) { std::vector pkt_buf; @@ -285,7 +285,7 @@ bool ControlConn::listServices() } } -ControlConn::handle_t ControlConn::allocateServiceHandle(service_record *record) +control_conn_t::handle_t control_conn_t::allocateServiceHandle(service_record *record) { bool is_unique = true; handle_t largest_seen = 0; @@ -310,7 +310,7 @@ ControlConn::handle_t ControlConn::allocateServiceHandle(service_record *record) } -bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept +bool control_conn_t::queuePacket(const char *pkt, unsigned size) noexcept { int in_flag = bad_conn_close ? 0 : IN_EVENTS; bool was_empty = outbuf.empty(); @@ -365,7 +365,7 @@ bool ControlConn::queuePacket(const char *pkt, unsigned size) noexcept // This queuePacket method is frustratingly similar to the one above, but the subtle differences // make them extraordinary difficult to combine into a single method. -bool ControlConn::queuePacket(std::vector &&pkt) noexcept +bool control_conn_t::queuePacket(std::vector &&pkt) noexcept { int in_flag = bad_conn_close ? 0 : IN_EVENTS; bool was_empty = outbuf.empty(); @@ -416,13 +416,13 @@ bool ControlConn::queuePacket(std::vector &&pkt) noexcept } } -bool ControlConn::rollbackComplete() noexcept +bool control_conn_t::rollbackComplete() noexcept { char ackBuf[2] = { DINIT_ROLLBACK_COMPLETED, 2 }; return queuePacket(ackBuf, 2); } -bool ControlConn::dataReady() noexcept +bool control_conn_t::dataReady() noexcept { int fd = iob.get_watched_fd(); @@ -465,7 +465,7 @@ bool ControlConn::dataReady() noexcept return false; } -bool ControlConn::sendData() noexcept +bool control_conn_t::sendData() noexcept { if (outbuf.empty() && bad_conn_close) { if (oom_close) { @@ -512,7 +512,7 @@ bool ControlConn::sendData() noexcept return false; } -ControlConn::~ControlConn() noexcept +control_conn_t::~control_conn_t() noexcept { close(iob.get_watched_fd()); iob.deregister(*loop); diff --git a/src/control.h b/src/control.h index 8550a3d..c2cf0e3 100644 --- a/src/control.h +++ b/src/control.h @@ -19,16 +19,16 @@ // Control connection for dinit using namespace dasynq; -using EventLoop_t = event_loop; +using eventloop_t = event_loop; -class ControlConn; -class ControlConnWatcher; +class control_conn_t; +class control_conn_watcher; // forward-declaration of callback: -static rearm control_conn_cb(EventLoop_t *loop, ControlConnWatcher *watcher, int revents); +static rearm control_conn_cb(eventloop_t *loop, control_conn_watcher *watcher, int revents); // Pointer to the control connection that is listening for rollback completion -extern ControlConn * rollback_handler_conn; +extern control_conn_t * rollback_handler_conn; extern int active_control_conns; @@ -44,51 +44,51 @@ extern int active_control_conns; // (1 byte) packet length (including all fields) // N bytes: packet data (N = (length - 2)) -class ServiceSet; +class service_set; class service_record; -class ControlConnWatcher : public EventLoop_t::bidi_fd_watcher_impl +class control_conn_watcher : public eventloop_t::bidi_fd_watcher_impl { - inline rearm receiveEvent(EventLoop_t &loop, int fd, int flags) noexcept; + inline rearm receiveEvent(eventloop_t &loop, int fd, int flags) noexcept; public: - rearm read_ready(EventLoop_t &loop, int fd) noexcept + rearm read_ready(eventloop_t &loop, int fd) noexcept { return receiveEvent(loop, fd, IN_EVENTS); } - rearm write_ready(EventLoop_t &loop, int fd) noexcept + rearm write_ready(eventloop_t &loop, int fd) noexcept { return receiveEvent(loop, fd, OUT_EVENTS); } - EventLoop_t * eventLoop; + eventloop_t * eventLoop; void set_watches(int flags) { - EventLoop_t::bidi_fd_watcher::set_watches(*eventLoop, flags); + eventloop_t::bidi_fd_watcher::set_watches(*eventLoop, flags); } - void registerWith(EventLoop_t &loop, int fd, int flags) + void registerWith(eventloop_t &loop, int fd, int flags) { this->eventLoop = &loop; - bidi_fd_watcher::add_watch(loop, fd, flags); + bidi_fd_watcher::add_watch(loop, fd, flags); } }; -inline rearm ControlConnWatcher::receiveEvent(EventLoop_t &loop, int fd, int flags) noexcept +inline rearm control_conn_watcher::receiveEvent(eventloop_t &loop, int fd, int flags) noexcept { return control_conn_cb(&loop, this, flags); } -class ControlConn : private ServiceListener +class control_conn_t : private service_listener { - friend rearm control_conn_cb(EventLoop_t *loop, ControlConnWatcher *watcher, int revents); + friend rearm control_conn_cb(eventloop_t *loop, control_conn_watcher *watcher, int revents); - ControlConnWatcher iob; - EventLoop_t *loop; - ServiceSet *service_set; + control_conn_watcher iob; + eventloop_t *loop; + service_set *services; bool bad_conn_close = false; // close when finished output? bool oom_close = false; // send final 'out of memory' indicator @@ -98,7 +98,7 @@ class ControlConn : private ServiceListener int chklen; // Receive buffer - CPBuffer<1024> rbuf; + cpbuffer<1024> rbuf; template using list = std::list; template using vector = std::vector; @@ -175,7 +175,7 @@ class ControlConn : private ServiceListener // Process service event broadcast. // Note that this can potentially be called during packet processing (upon issuing // service start/stop orders etc). - void serviceEvent(service_record * service, ServiceEvent event) noexcept final override + void serviceEvent(service_record * service, service_event event) noexcept final override { // For each service handle corresponding to the event, send an information packet. auto range = serviceKeyMap.equal_range(service); @@ -204,7 +204,7 @@ class ControlConn : private ServiceListener } public: - ControlConn(EventLoop_t * loop, ServiceSet * service_set, int fd) : loop(loop), service_set(service_set), chklen(0) + control_conn_t(eventloop_t * loop, service_set * services_p, int fd) : loop(loop), services(services_p), chklen(0) { iob.registerWith(*loop, fd, IN_EVENTS); active_control_conns++; @@ -212,14 +212,14 @@ class ControlConn : private ServiceListener bool rollbackComplete() noexcept; - virtual ~ControlConn() noexcept; + virtual ~control_conn_t() noexcept; }; -static rearm control_conn_cb(EventLoop_t * loop, ControlConnWatcher * watcher, int revents) +static rearm control_conn_cb(eventloop_t * loop, control_conn_watcher * watcher, int revents) { - char * cc_addr = (reinterpret_cast(watcher)) - offsetof(ControlConn, iob); - ControlConn *conn = reinterpret_cast(cc_addr); + char * cc_addr = (reinterpret_cast(watcher)) - offsetof(control_conn_t, iob); + control_conn_t *conn = reinterpret_cast(cc_addr); if (revents & IN_EVENTS) { if (conn->dataReady()) { delete conn; diff --git a/src/cpbuffer.h b/src/cpbuffer.h index 1478b74..9019f4a 100644 --- a/src/cpbuffer.h +++ b/src/cpbuffer.h @@ -4,7 +4,7 @@ #include // control protocol buffer, a circular buffer with 1024-byte capacity. -template class CPBuffer +template class cpbuffer { char buf[SIZE]; int cur_idx = 0; diff --git a/src/dinit-log.cc b/src/dinit-log.cc index 68bbced..717e601 100644 --- a/src/dinit-log.cc +++ b/src/dinit-log.cc @@ -12,15 +12,15 @@ #include "dinit-log.h" #include "cpbuffer.h" -extern EventLoop_t eventLoop; +extern eventloop_t eventLoop; static bool log_current_line[2]; // Whether the current line is being logged (for console, main log) LogLevel log_level[2] = { LogLevel::WARN, LogLevel::WARN }; -static ServiceSet *service_set = nullptr; // Reference to service set +static service_set *services = nullptr; // Reference to service set namespace { -class BufferedLogStream : public EventLoop_t::fd_watcher_impl +class BufferedLogStream : public eventloop_t::fd_watcher_impl { private: @@ -35,7 +35,7 @@ class BufferedLogStream : public EventLoop_t::fd_watcher_impl const char *special_buf; // buffer containing special message int msg_index; // index into special message - CPBuffer<4096> log_buffer; + cpbuffer<4096> log_buffer; public: @@ -50,7 +50,7 @@ class BufferedLogStream : public EventLoop_t::fd_watcher_impl release = false; } - rearm fd_event(EventLoop_t &loop, int fd, int flags) noexcept; + rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept; // Check whether the console can be released. void flushForRelease(); @@ -109,7 +109,7 @@ void BufferedLogStream::release_console() if (release) { int flags = fcntl(1, F_GETFL, 0); fcntl(1, F_SETFL, flags & ~O_NONBLOCK); - service_set->pull_console_queue(); + services->pull_console_queue(); } } @@ -127,7 +127,7 @@ void BufferedLogStream::flushForRelease() // release when it's finished. } -rearm BufferedLogStream::fd_event(EventLoop_t &loop, int fd, int flags) noexcept +rearm BufferedLogStream::fd_event(eventloop_t &loop, int fd, int flags) noexcept { if ((! partway) && (! special) && discarded) { special_buf = "dinit: *** message discarded due to full buffer ****\n"; @@ -234,9 +234,9 @@ rearm BufferedLogStream::fd_event(EventLoop_t &loop, int fd, int flags) noexcept // Initialise the logging subsystem // Potentially throws std::bad_alloc or std::system_error -void init_log(ServiceSet *sset) +void init_log(service_set *sset) { - service_set = sset; + services = sset; log_stream[DLOG_CONS].add_watch(eventLoop, STDOUT_FILENO, OUT_EVENTS, false); enable_console_log(true); } diff --git a/src/dinit-log.h b/src/dinit-log.h index 88fdc54..0b2153c 100644 --- a/src/dinit-log.h +++ b/src/dinit-log.h @@ -7,7 +7,7 @@ #include #include -class ServiceSet; +class service_set; enum class LogLevel { DEBUG, @@ -19,7 +19,7 @@ enum class LogLevel { extern LogLevel log_level[2]; void enable_console_log(bool do_enable) noexcept; -void init_log(ServiceSet *sset); +void init_log(service_set *sset); void setup_main_log(int fd); bool is_log_flushed() noexcept; void discard_console_log_buffer() noexcept; diff --git a/src/dinit.cc b/src/dinit.cc index 5a20889..bb9f680 100644 --- a/src/dinit.cc +++ b/src/dinit.cc @@ -39,38 +39,25 @@ using namespace dasynq; -using EventLoop_t = event_loop; +using eventloop_t = event_loop; -EventLoop_t eventLoop = EventLoop_t(); +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 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 close_control_socket() noexcept; static void wait_for_user_input() noexcept; -static void control_socket_cb(EventLoop_t *loop, int fd); +static void control_socket_cb(eventloop_t *loop, int fd); void open_control_socket(bool report_ro_failure = true) noexcept; void setup_external_log() noexcept; -class ControlSocketWatcher : public EventLoop_t::fd_watcher_impl -{ - public: - rearm fd_event(EventLoop_t &loop, int fd, int flags) noexcept - { - control_socket_cb(&loop, fd); - return rearm::REARM; - } -}; - -ControlSocketWatcher control_socket_io; - - // Variables -static ServiceSet *service_set; +static service_set *services; static bool am_system_init = false; // true if we are the system init process @@ -106,38 +93,41 @@ const char * get_user_home() namespace { - class CallbackSignalHandler : public EventLoop_t::signal_watcher_impl + class callback_signal_handler : public eventloop_t::signal_watcher_impl { public: - typedef void (*cb_func_t)(EventLoop_t &); + typedef void (*cb_func_t)(eventloop_t &); private: cb_func_t cb_func; public: - CallbackSignalHandler() : cb_func(nullptr) { } - CallbackSignalHandler(cb_func_t pcb_func) : cb_func(pcb_func) { } + callback_signal_handler() : cb_func(nullptr) { } + callback_signal_handler(cb_func_t pcb_func) : cb_func(pcb_func) { } void setCbFunc(cb_func_t cb_func) { this->cb_func = cb_func; } - rearm received(EventLoop_t &eloop, int signo, siginfo_p siginfo) + rearm received(eventloop_t &eloop, int signo, siginfo_p siginfo) { cb_func(eloop); return rearm::REARM; } }; - class ControlSocketWatcher : public EventLoop_t::fd_watcher_impl + class control_socket_watcher : public eventloop_t::fd_watcher_impl { - rearm fd_event(EventLoop_t &loop, int fd, int flags) + public: + rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept { control_socket_cb(&loop, fd); return rearm::REARM; } }; + + control_socket_watcher control_socket_io; } static int dinit_main(int argc, char **argv) @@ -277,9 +267,9 @@ static int dinit_main(int argc, char **argv) } // Set up signal handlers - CallbackSignalHandler sigterm_watcher {sigterm_cb}; - CallbackSignalHandler sigint_watcher; - CallbackSignalHandler sigquit_watcher; + callback_signal_handler sigterm_watcher {sigterm_cb}; + callback_signal_handler sigint_watcher; + callback_signal_handler sigquit_watcher; if (am_system_init) { sigint_watcher.setCbFunc(sigint_reboot_cb); @@ -311,22 +301,22 @@ static int dinit_main(int argc, char **argv) #endif /* start requested services */ - service_set = new ServiceSet(service_dir); + services = new service_set(service_dir); - init_log(service_set); + init_log(services); for (auto svc : services_to_start) { try { - service_set->startService(svc); + services->startService(svc); // Note in general if we fail to start a service we don't need any special error handling, // since we either leave other services running or, if it was the only service, then no // services will be running and we will process normally (reboot if system process, // exit if user process). } - catch (ServiceNotFound &snf) { + catch (service_not_found &snf) { log(LogLevel::ERROR, snf.serviceName, ": Could not find service description."); } - catch (ServiceLoadExc &sle) { + catch (service_load_exc &sle) { log(LogLevel::ERROR, sle.serviceName, ": ", sle.excDescription); } catch (std::bad_alloc &badalloce) { @@ -338,22 +328,22 @@ static int dinit_main(int argc, char **argv) event_loop: // Process events until all services have terminated. - while (service_set->count_active_services() != 0) { + while (services->count_active_services() != 0) { eventLoop.run(); } - ShutdownType shutdown_type = service_set->getShutdownType(); + shutdown_type_t shutdown_type = services->getShutdownType(); if (am_system_init) { logMsgBegin(LogLevel::INFO, "No more active services."); - if (shutdown_type == ShutdownType::REBOOT) { + if (shutdown_type == shutdown_type_t::REBOOT) { logMsgEnd(" Will reboot."); } - else if (shutdown_type == ShutdownType::HALT) { + else if (shutdown_type == shutdown_type_t::HALT) { logMsgEnd(" Will halt."); } - else if (shutdown_type == ShutdownType::POWEROFF) { + else if (shutdown_type == shutdown_type_t::POWEROFF) { logMsgEnd(" Will power down."); } else { @@ -368,27 +358,27 @@ static int dinit_main(int argc, char **argv) close_control_socket(); if (am_system_init) { - if (shutdown_type == ShutdownType::CONTINUE) { + if (shutdown_type == shutdown_type_t::CONTINUE) { // It could be that we started in single user mode, and the // user has now exited the shell. We'll try and re-start the // boot process... try { - service_set->startService("boot"); + services->startService("boot"); goto event_loop; // yes, the "evil" goto } catch (...) { // Now what do we do? try to reboot, but wait for user ack to avoid boot loop. log(LogLevel::ERROR, "Could not start 'boot' service. Will attempt reboot."); wait_for_user_input(); - shutdown_type = ShutdownType::REBOOT; + shutdown_type = shutdown_type_t::REBOOT; } } const char * cmd_arg; - if (shutdown_type == ShutdownType::HALT) { + if (shutdown_type == shutdown_type_t::HALT) { cmd_arg = "-h"; } - else if (shutdown_type == ShutdownType::REBOOT) { + else if (shutdown_type == shutdown_type_t::REBOOT) { cmd_arg = "-r"; } else { @@ -405,7 +395,7 @@ static int dinit_main(int argc, char **argv) eventLoop.run(); } } - else if (shutdown_type == ShutdownType::REBOOT) { + else if (shutdown_type == shutdown_type_t::REBOOT) { // Non-system-process. If we got SIGINT, let's die due to it: sigset_t sigwait_set; sigemptyset(&sigwait_set); @@ -446,7 +436,7 @@ static void wait_for_user_input() noexcept } // Callback for control socket -static void control_socket_cb(EventLoop_t *loop, int sockfd) +static void control_socket_cb(eventloop_t *loop, int sockfd) { // TODO limit the number of active connections. Keep a tally, and disable the // control connection listening socket watcher if it gets high, and re-enable @@ -457,7 +447,7 @@ static void control_socket_cb(EventLoop_t *loop, int sockfd) if (newfd != -1) { try { - new ControlConn(loop, service_set, newfd); // will delete itself when it's finished + new control_conn_t(loop, services, newfd); // will delete itself when it's finished } catch (std::exception &exc) { log(LogLevel::ERROR, "Accepting control connection: ", exc.what()); @@ -590,13 +580,13 @@ void setup_external_log() noexcept } /* handle SIGINT signal (generated by Linux kernel when ctrl+alt+del pressed) */ -static void sigint_reboot_cb(EventLoop_t &eloop) noexcept +static void sigint_reboot_cb(eventloop_t &eloop) noexcept { - service_set->stop_all_services(ShutdownType::REBOOT); + services->stop_all_services(shutdown_type_t::REBOOT); } /* handle SIGQUIT (if we are system init) */ -static void sigquit_cb(EventLoop_t &eloop) noexcept +static void sigquit_cb(eventloop_t &eloop) noexcept { // This performs an immediate shutdown, without service rollback. close_control_socket(); @@ -606,7 +596,7 @@ static void sigquit_cb(EventLoop_t &eloop) noexcept } /* handle SIGTERM/SIGQUIT(non-system-daemon) - stop all services and shut down */ -static void sigterm_cb(EventLoop_t &eloop) noexcept +static void sigterm_cb(eventloop_t &eloop) noexcept { - service_set->stop_all_services(); + services->stop_all_services(); } diff --git a/src/dinitctl.cc b/src/dinitctl.cc index 648cfc0..1bce29b 100644 --- a/src/dinitctl.cc +++ b/src/dinitctl.cc @@ -34,7 +34,7 @@ class ReadCPException enum class Command; static int issueLoadService(int socknum, const char *service_name); -static int checkLoadReply(int socknum, CPBuffer<1024> &rbuffer, handle_t *handle_p, ServiceState *state_p); +static int checkLoadReply(int socknum, cpbuffer<1024> &rbuffer, handle_t *handle_p, ServiceState *state_p); static int startStopService(int socknum, const char *service_name, Command command, bool do_pin, bool wait_for_service, bool verbose); static int unpinService(int socknum, const char *service_name, bool verbose); static int listServices(int socknum); @@ -45,7 +45,7 @@ static int listServices(int socknum); // errcode = 0 if end of stream (remote end closed) // errcode = errno if another error occurred // Note that EINTR is ignored (i.e. the read will be re-tried). -static void fillBufferTo(CPBuffer<1024> *buf, int fd, int rlength) +static void fillBufferTo(cpbuffer<1024> *buf, int fd, int rlength) { do { int r = buf->fill_to(fd, rlength); @@ -76,7 +76,7 @@ static const char * describeVerb(bool stop) // Wait for a reply packet, skipping over any information packets // that are received in the meantime. -static void wait_for_reply(CPBuffer<1024> &rbuffer, int fd) +static void wait_for_reply(cpbuffer<1024> &rbuffer, int fd) { fillBufferTo(&rbuffer, fd, 1); @@ -303,7 +303,7 @@ static int startStopService(int socknum, const char *service_name, Command comma // Now we expect a reply: try { - CPBuffer<1024> rbuffer; + cpbuffer<1024> rbuffer; wait_for_reply(rbuffer, socknum); ServiceState state; @@ -375,16 +375,16 @@ static int startStopService(int socknum, const char *service_name, Command comma return 0; } - ServiceEvent completionEvent; - ServiceEvent cancelledEvent; + service_event completionEvent; + service_event cancelledEvent; if (do_stop) { - completionEvent = ServiceEvent::STOPPED; - cancelledEvent = ServiceEvent::STOPCANCELLED; + completionEvent = service_event::STOPPED; + cancelledEvent = service_event::STOPCANCELLED; } else { - completionEvent = ServiceEvent::STARTED; - cancelledEvent = ServiceEvent::STARTCANCELLED; + completionEvent = service_event::STARTED; + cancelledEvent = service_event::STARTCANCELLED; } // Wait until service started: @@ -397,7 +397,7 @@ static int startStopService(int socknum, const char *service_name, Command comma if (rbuffer[0] == DINIT_IP_SERVICEEVENT) { handle_t ev_handle; rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle)); - ServiceEvent event = static_cast(rbuffer[2 + sizeof(ev_handle)]); + service_event event = static_cast(rbuffer[2 + sizeof(ev_handle)]); if (ev_handle == handle) { if (event == completionEvent) { if (verbose) { @@ -411,7 +411,7 @@ static int startStopService(int socknum, const char *service_name, Command comma } return 1; } - else if (! do_stop && event == ServiceEvent::FAILEDSTART) { + else if (! do_stop && event == service_event::FAILEDSTART) { if (verbose) { cout << "Service failed to start." << endl; } @@ -482,7 +482,7 @@ static int issueLoadService(int socknum, const char *service_name) } // Check that a "load service" reply was received, and that the requested service was found. -static int checkLoadReply(int socknum, CPBuffer<1024> &rbuffer, handle_t *handle_p, ServiceState *state_p) +static int checkLoadReply(int socknum, cpbuffer<1024> &rbuffer, handle_t *handle_p, ServiceState *state_p) { using namespace std; @@ -516,7 +516,7 @@ static int unpinService(int socknum, const char *service_name, bool verbose) // Now we expect a reply: try { - CPBuffer<1024> rbuffer; + cpbuffer<1024> rbuffer; wait_for_reply(rbuffer, socknum); handle_t handle; @@ -578,7 +578,7 @@ static int listServices(int socknum) return 1; } - CPBuffer<1024> rbuffer; + cpbuffer<1024> rbuffer; wait_for_reply(rbuffer, socknum); while (rbuffer[0] == DINIT_RP_SVCINFO) { fillBufferTo(&rbuffer, socknum, 8); diff --git a/src/load_service.cc b/src/load_service.cc index d6fe465..6d8ce3e 100644 --- a/src/load_service.cc +++ b/src/load_service.cc @@ -214,12 +214,12 @@ static uid_t parse_uid_param(const std::string ¶m, const std::string &servic static_assert((uintmax_t)std::numeric_limits::max() <= (uintmax_t)std::numeric_limits::max(), "uid_t is too large"); unsigned long long v = std::stoull(param, &ind, 0); if (v > static_cast(std::numeric_limits::max()) || ind != param.length()) { - throw ServiceDescriptionExc(service_name, uid_err_msg); + throw service_description_exc(service_name, uid_err_msg); } return v; } catch (std::out_of_range &exc) { - throw ServiceDescriptionExc(service_name, uid_err_msg); + throw service_description_exc(service_name, uid_err_msg); } catch (std::invalid_argument &exc) { // Ok, so it doesn't look like a number: proceed... @@ -230,10 +230,10 @@ static uid_t parse_uid_param(const std::string ¶m, const std::string &servic if (pwent == nullptr) { // Maybe an error, maybe just no entry. if (errno == 0) { - throw ServiceDescriptionExc(service_name, "Specified user \"" + param + "\" does not exist in system database."); + throw service_description_exc(service_name, "Specified user \"" + param + "\" does not exist in system database."); } else { - throw ServiceDescriptionExc(service_name, std::string("Error accessing user database: ") + strerror(errno)); + throw service_description_exc(service_name, std::string("Error accessing user database: ") + strerror(errno)); } } @@ -254,15 +254,15 @@ static unsigned long long parse_unum_param(const std::string ¶m, const std:: try { unsigned long long v = std::stoull(param, &ind, 0); if (v > max || ind != param.length()) { - throw ServiceDescriptionExc(service_name, num_err_msg); + throw service_description_exc(service_name, num_err_msg); } return v; } catch (std::out_of_range &exc) { - throw ServiceDescriptionExc(service_name, num_err_msg); + throw service_description_exc(service_name, num_err_msg); } catch (std::invalid_argument &exc) { - throw ServiceDescriptionExc(service_name, num_err_msg); + throw service_description_exc(service_name, num_err_msg); } } @@ -283,12 +283,12 @@ static gid_t parse_gid_param(const std::string ¶m, const std::string &servic // functions accept a leading minus sign... unsigned long long v = std::stoull(param, &ind, 0); if (v > static_cast(std::numeric_limits::max()) || ind != param.length()) { - throw ServiceDescriptionExc(service_name, gid_err_msg); + throw service_description_exc(service_name, gid_err_msg); } return v; } catch (std::out_of_range &exc) { - throw ServiceDescriptionExc(service_name, gid_err_msg); + throw service_description_exc(service_name, gid_err_msg); } catch (std::invalid_argument &exc) { // Ok, so it doesn't look like a number: proceed... @@ -299,10 +299,10 @@ static gid_t parse_gid_param(const std::string ¶m, const std::string &servic if (grent == nullptr) { // Maybe an error, maybe just no entry. if (errno == 0) { - throw ServiceDescriptionExc(service_name, "Specified group \"" + param + "\" does not exist in system database."); + throw service_description_exc(service_name, "Specified group \"" + param + "\" does not exist in system database."); } else { - throw ServiceDescriptionExc(service_name, std::string("Error accessing group database: ") + strerror(errno)); + throw service_description_exc(service_name, std::string("Error accessing group database: ") + strerror(errno)); } } @@ -324,11 +324,11 @@ static void parse_timespec(const std::string ¶mval, const std::string &servi break; } if (ch < '0' || ch > '9') { - throw ServiceDescriptionExc(servicename, std::string("Bad value for ") + paramname); + throw service_description_exc(servicename, std::string("Bad value for ") + paramname); } // check for overflow if (isec >= max_secs) { - throw ServiceDescriptionExc(servicename, std::string("Too-large value for ") + paramname); + throw service_description_exc(servicename, std::string("Too-large value for ") + paramname); } isec *= 10; isec += ch - '0'; @@ -337,7 +337,7 @@ static void parse_timespec(const std::string ¶mval, const std::string &servi for ( ; i < len; i++) { char ch = paramval[i]; if (ch < '0' || ch > '9') { - throw ServiceDescriptionExc(servicename, std::string("Bad value for ") + paramname); + throw service_description_exc(servicename, std::string("Bad value for ") + paramname); } insec += (ch - '0') * insec_m; insec_m /= 10; @@ -352,7 +352,7 @@ static void parse_timespec(const std::string ¶mval, const std::string &servi // Might throw a ServiceLoadExc exception if a dependency cycle is found or if another // problem occurs (I/O error, service description not found etc). Throws std::bad_alloc // if a memory allocation failure occurs. -service_record * ServiceSet::loadServiceRecord(const char * name) +service_record * service_set::loadServiceRecord(const char * name) { using std::string; using std::ifstream; @@ -368,7 +368,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) service_record * rval = find_service(string(name)); if (rval != 0) { if (rval->isDummy()) { - throw ServiceCyclicDependency(name); + throw service_cyclic_dependency(name); } return rval; } @@ -386,11 +386,11 @@ service_record * ServiceSet::loadServiceRecord(const char * name) list> stop_command_offsets; string pid_file; - ServiceType service_type = ServiceType::PROCESS; + service_type service_type = service_type::PROCESS; std::list depends_on; std::list depends_soft; string logfile; - OnstartFlags onstart_flags; + onstart_flags_t onstart_flags; int term_signal = -1; // additional termination signal bool auto_restart = false; bool smooth_recovery = false; @@ -413,7 +413,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) service_file.open(service_filename.c_str(), ios::in); } catch (std::ios_base::failure &exc) { - throw ServiceNotFound(name); + throw service_not_found(name); } // Add a dummy service record now to prevent infinite recursion in case of cyclic dependency @@ -437,7 +437,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) string setting = read_setting_name(i, end); i = skipws(i, end); if (i == end || (*i != '=' && *i != ':')) { - throw ServiceDescriptionExc(name, "Badly formed line."); + throw service_description_exc(name, "Badly formed line."); } i = skipws(++i, end); @@ -457,7 +457,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) } } catch (std::logic_error &exc) { - throw ServiceDescriptionExc(name, "socket-permissions: Badly-formed or out-of-range numeric value"); + throw service_description_exc(name, "socket-permissions: Badly-formed or out-of-range numeric value"); } } else if (setting == "socket-uid") { @@ -496,19 +496,19 @@ service_record * ServiceSet::loadServiceRecord(const char * name) else if (setting == "type") { string type_str = read_setting_value(i, end); if (type_str == "scripted") { - service_type = ServiceType::SCRIPTED; + service_type = service_type::SCRIPTED; } else if (type_str == "process") { - service_type = ServiceType::PROCESS; + service_type = service_type::PROCESS; } else if (type_str == "bgprocess") { - service_type = ServiceType::BGPROCESS; + service_type = service_type::BGPROCESS; } else if (type_str == "internal") { - service_type = ServiceType::INTERNAL; + service_type = service_type::INTERNAL; } else { - throw ServiceDescriptionExc(name, "Service type must be one of: \"scripted\"," + throw service_description_exc(name, "Service type must be one of: \"scripted\"," " \"process\", \"bgprocess\" or \"internal\""); } } @@ -538,7 +538,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) onstart_flags.pass_cs_fd = true; } else { - throw ServiceDescriptionExc(name, "Unknown option: " + option_txt); + throw service_description_exc(name, "Unknown option: " + option_txt); } } } @@ -546,7 +546,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) string signame = read_setting_value(i, end, nullptr); int signo = signalNameToNumber(signame); if (signo == -1) { - throw ServiceDescriptionExc(name, "Unknown/unsupported termination signal: " + signame); + throw service_description_exc(name, "Unknown/unsupported termination signal: " + signame); } else { term_signal = signo; @@ -565,16 +565,16 @@ service_record * ServiceSet::loadServiceRecord(const char * name) max_restarts = parse_unum_param(limit_str, name, std::numeric_limits::max()); } else { - throw ServiceDescriptionExc(name, "Unknown setting: " + setting); + throw service_description_exc(name, "Unknown setting: " + setting); } } } service_file.close(); - if (service_type == ServiceType::PROCESS || service_type == ServiceType::BGPROCESS || service_type == ServiceType::SCRIPTED) { + if (service_type == service_type::PROCESS || service_type == service_type::BGPROCESS || service_type == service_type::SCRIPTED) { if (command.length() == 0) { - throw ServiceDescriptionExc(name, "Service command not specified"); + throw service_description_exc(name, "Service command not specified"); } } @@ -583,14 +583,14 @@ service_record * ServiceSet::loadServiceRecord(const char * name) if (*iter == rval) { // We've found the dummy record delete rval; - if (service_type == ServiceType::PROCESS) { + if (service_type == service_type::PROCESS) { auto rvalps = new process_service(this, string(name), std::move(command), command_offsets, &depends_on, &depends_soft); rvalps->set_restart_interval(restart_interval, max_restarts); rvalps->set_restart_delay(restart_delay); rval = rvalps; } - else if (service_type == ServiceType::BGPROCESS) { + else if (service_type == service_type::BGPROCESS) { auto rvalps = new bgproc_service(this, string(name), std::move(command), command_offsets, &depends_on, &depends_soft); rvalps->set_pid_file(std::move(pid_file)); @@ -598,7 +598,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) rvalps->set_restart_delay(restart_delay); rval = rvalps; } - else if (service_type == ServiceType::SCRIPTED) { + else if (service_type == service_type::SCRIPTED) { rval = new scripted_service(this, string(name), std::move(command), command_offsets, &depends_on, &depends_soft); rval->setStopCommand(stop_command, stop_command_offsets); @@ -625,7 +625,7 @@ service_record * ServiceSet::loadServiceRecord(const char * name) // Must remove the dummy service record. std::remove(records.begin(), records.end(), rval); delete rval; - throw ServiceDescriptionExc(name, std::move(setting_exc.getInfo())); + throw service_description_exc(name, std::move(setting_exc.getInfo())); } catch (...) { // Must remove the dummy service record. diff --git a/src/service-constants.h b/src/service-constants.h index 8084066..509ddb6 100644 --- a/src/service-constants.h +++ b/src/service-constants.h @@ -10,7 +10,7 @@ enum class ServiceState { }; /* Service types */ -enum class ServiceType { +enum class service_type { DUMMY, // Dummy service, used to detect cyclice dependencies PROCESS, // Service runs as a process, and can be stopped by // sending the process a signal (usually SIGTERM) @@ -22,7 +22,7 @@ enum class ServiceType { }; /* Service events */ -enum class ServiceEvent { +enum class service_event { STARTED, // Service was started (reached STARTED state) STOPPED, // Service was stopped (reached STOPPED state) FAILEDSTART, // Service failed to start (possibly due to dependency failing) @@ -31,7 +31,7 @@ enum class ServiceEvent { }; /* Shutdown types */ -enum class ShutdownType { +enum class shutdown_type_t { CONTINUE, // Continue normal boot sequence (used after single-user shell) HALT, // Halt system without powering down POWEROFF, // Power off system diff --git a/src/service-listener.h b/src/service-listener.h index fa6307a..a5c1269 100644 --- a/src/service-listener.h +++ b/src/service-listener.h @@ -6,13 +6,13 @@ class service_record; // Interface for listening to services -class ServiceListener +class service_listener { public: // An event occurred on the service being observed. // Listeners must not be added or removed during event notification. - virtual void serviceEvent(service_record * service, ServiceEvent event) noexcept = 0; + virtual void serviceEvent(service_record * service, service_event event) noexcept = 0; }; #endif diff --git a/src/service.cc b/src/service.cc index 70e880a..b8e7600 100644 --- a/src/service.cc +++ b/src/service.cc @@ -26,7 +26,7 @@ // from dinit.cc: void open_control_socket(bool report_ro_failure = true) noexcept; void setup_external_log() noexcept; -extern EventLoop_t eventLoop; +extern eventloop_t eventLoop; // Find the requested service by name static service_record * find_service(const std::list & records, @@ -42,12 +42,12 @@ static service_record * find_service(const std::list & records return (service_record *)0; } -service_record * ServiceSet::find_service(const std::string &name) noexcept +service_record * service_set::find_service(const std::string &name) noexcept { return ::find_service(records, name.c_str()); } -void ServiceSet::startService(const char *name) +void service_set::startService(const char *name) { using namespace std; service_record *record = loadServiceRecord(name); @@ -56,7 +56,7 @@ void ServiceSet::startService(const char *name) processQueues(true); } -void ServiceSet::stopService(const std::string & name) noexcept +void service_set::stopService(const std::string & name) noexcept { service_record *record = find_service(name); if (record != nullptr) { @@ -86,7 +86,7 @@ void service_record::stopped() noexcept } bool will_restart = (desired_state == ServiceState::STARTED) - && service_set->get_auto_restart(); + && services->get_auto_restart(); for (auto dependency : depends_on) { // we signal dependencies in case they are waiting for us to stop: @@ -111,15 +111,15 @@ void service_record::stopped() noexcept release(); } else if (required_by == 0) { - service_set->service_inactive(this); + services->service_inactive(this); } } logServiceStopped(service_name); - notifyListeners(ServiceEvent::STOPPED); + notifyListeners(service_event::STOPPED); } -dasynq::rearm ServiceChildWatcher::status_change(EventLoop_t &loop, pid_t child, int status) noexcept +dasynq::rearm service_child_watcher::status_change(eventloop_t &loop, pid_t child, int status) noexcept { base_process_service *sr = service; @@ -147,7 +147,7 @@ dasynq::rearm ServiceChildWatcher::status_change(EventLoop_t &loop, pid_t child, bool service_record::do_auto_restart() noexcept { if (auto_restart) { - return service_set->get_auto_restart(); + return services->get_auto_restart(); } return false; } @@ -196,14 +196,14 @@ void process_service::handle_exit_status(int exit_status) noexcept // service process. if (! restart_ps_process()) { emergency_stop(); - service_set->processQueues(false); + services->processQueues(false); } return; } else { emergency_stop(); } - service_set->processQueues(false); + services->processQueues(false); } void bgproc_service::handle_exit_status(int exit_status) noexcept @@ -239,7 +239,7 @@ void bgproc_service::handle_exit_status(int exit_status) noexcept if (need_stop) { // Failed startup: no auto-restart. emergency_stop(); - service_set->processQueues(false); + services->processQueues(false); } return; @@ -272,7 +272,7 @@ void bgproc_service::handle_exit_status(int exit_status) noexcept doing_recovery = true; if (! restart_ps_process()) { emergency_stop(); - service_set->processQueues(); + services->processQueues(); } return; } @@ -286,7 +286,7 @@ void bgproc_service::handle_exit_status(int exit_status) noexcept stopDependents(); stopped(); } - service_set->processQueues(false); + services->processQueues(false); } void scripted_service::handle_exit_status(int exit_status) noexcept @@ -310,7 +310,7 @@ void scripted_service::handle_exit_status(int exit_status) noexcept // can be stopped: stopped(); } - service_set->processQueues(false); + services->processQueues(false); } else { // STARTING if (exit_status == 0) { @@ -326,11 +326,11 @@ void scripted_service::handle_exit_status(int exit_status) noexcept } failed_to_start(); } - service_set->processQueues(true); + services->processQueues(true); } } -rearm ServiceIoWatcher::fd_event(EventLoop_t &loop, int fd, int flags) noexcept +rearm ServiceIoWatcher::fd_event(eventloop_t &loop, int fd, int flags) noexcept { base_process_service *sr = service; sr->waiting_for_execstat = false; @@ -358,7 +358,7 @@ rearm ServiceIoWatcher::fd_event(EventLoop_t &loop, int fd, int flags) noexcept } else { // exec() succeeded. - if (sr->service_type == ServiceType::PROCESS) { + if (sr->record_type == service_type::PROCESS) { // This could be a smooth recovery (state already STARTED). Even more, the process // might be stopped (and killed via a signal) during smooth recovery. We don't to // process startup again in either case, so we check for state STARTING: @@ -373,7 +373,7 @@ rearm ServiceIoWatcher::fd_event(EventLoop_t &loop, int fd, int flags) noexcept } } - sr->service_set->processQueues(true); + sr->services->processQueues(true); return rearm::REMOVED; } @@ -383,7 +383,7 @@ void service_record::require() noexcept if (required_by++ == 0) { prop_require = !prop_release; prop_release = false; - service_set->addToPropQueue(this); + services->addToPropQueue(this); } } @@ -396,10 +396,10 @@ void service_record::release() noexcept // the require was pending though: prop_release = !prop_require; prop_require = false; - service_set->addToPropQueue(this); + services->addToPropQueue(this); if (service_state == ServiceState::STOPPED) { - service_set->service_inactive(this); + services->service_inactive(this); } else { do_stop(); @@ -443,17 +443,17 @@ void service_record::start(bool activate) noexcept // We're STOPPING, and that can be interrupted. Our dependencies might be STOPPING, // but if so they are waiting (for us), so they too can be instantly returned to // STARTING state. - notifyListeners(ServiceEvent::STOPCANCELLED); + notifyListeners(service_event::STOPCANCELLED); } else if (! was_active) { - service_set->service_active(this); + services->service_active(this); } service_state = ServiceState::STARTING; waiting_for_deps = true; if (startCheckDependencies(true)) { - service_set->addToStartQueue(this); + services->addToStartQueue(this); } } @@ -531,7 +531,7 @@ void service_record::do_start() noexcept void service_record::dependencyStarted() noexcept { if (service_state == ServiceState::STARTING && waiting_for_deps) { - service_set->addToStartQueue(this); + services->addToStartQueue(this); } } @@ -544,7 +544,7 @@ bool service_record::startCheckDependencies(bool start_deps) noexcept if (start_deps) { all_deps_started = false; (*i)->prop_start = true; - service_set->addToPropQueue(*i); + services->addToPropQueue(*i); } else { return false; @@ -557,7 +557,7 @@ bool service_record::startCheckDependencies(bool start_deps) noexcept if (start_deps) { if (to->service_state != ServiceState::STARTED) { to->prop_start = true; - service_set->addToPropQueue(to); + services->addToPropQueue(to); i->waiting_on = true; all_deps_started = false; } @@ -722,7 +722,7 @@ void service_record::started() noexcept logServiceStarted(service_name); service_state = ServiceState::STARTED; - notifyListeners(ServiceEvent::STARTED); + notifyListeners(service_event::STARTED); if (onstart_flags.rw_ready) { open_control_socket(); @@ -759,13 +759,13 @@ void service_record::failed_to_start(bool depfailed) noexcept start_explicit = false; release(); } - notifyListeners(ServiceEvent::FAILEDSTART); + notifyListeners(service_event::FAILEDSTART); // Cancel start of dependents: for (sr_iter i = dependents.begin(); i != dependents.end(); i++) { if ((*i)->service_state == ServiceState::STARTING) { (*i)->prop_failure = true; - service_set->addToPropQueue(*i); + services->addToPropQueue(*i); } } for (auto i = soft_dpts.begin(); i != soft_dpts.end(); i++) { @@ -820,7 +820,7 @@ bool base_process_service::start_ps_process(const std::vector &cmd } bool child_status_registered = false; - ControlConn *control_conn = nullptr; + control_conn_t *control_conn = nullptr; int control_socket[2] = {-1, -1}; if (onstart_flags.pass_cs_fd) { @@ -834,7 +834,7 @@ bool base_process_service::start_ps_process(const std::vector &cmd fcntl(control_socket[0], F_SETFD, fdflags | FD_CLOEXEC); try { - control_conn = new ControlConn(&eventLoop, service_set, control_socket[0]); + control_conn = new control_conn_t(&eventLoop, services, control_socket[0]); } catch (std::exception &exc) { log(LogLevel::ERROR, service_name, ": can't launch process; out of memory"); @@ -1016,14 +1016,14 @@ void service_record::forceStop() noexcept { if (service_state != ServiceState::STOPPED) { force_stop = true; - service_set->addToStopQueue(this); + services->addToStopQueue(this); } } void service_record::dependentStopped() noexcept { if (service_state == ServiceState::STOPPING && waiting_for_deps) { - service_set->addToStopQueue(this); + services->addToStopQueue(this); } } @@ -1055,7 +1055,7 @@ void service_record::do_stop() noexcept } // We must have had desired_state == STARTED. - notifyListeners(ServiceEvent::STARTCANCELLED); + notifyListeners(service_event::STARTCANCELLED); interrupt_start(); @@ -1072,7 +1072,7 @@ void service_record::do_stop() noexcept service_state = ServiceState::STOPPING; waiting_for_deps = true; if (stopDependents()) { - service_set->addToStopQueue(this); + services->addToStopQueue(this); } } @@ -1107,7 +1107,7 @@ bool service_record::stopDependents() noexcept } (*i)->prop_stop = true; - service_set->addToPropQueue(*i); + services->addToPropQueue(*i); } return all_deps_stopped; @@ -1135,7 +1135,7 @@ void base_process_service::all_deps_stopped() noexcept // In most cases, the rest is done in handle_exit_status. // If we are a BGPROCESS and the process is not our immediate child, however, that // won't work - check for this now: - if (service_type == ServiceType::BGPROCESS) { + if (record_type == service_type::BGPROCESS) { int status; pid_t r = waitpid(pid, &status, WNOHANG); if (r == -1 && errno == ECHILD) { @@ -1173,47 +1173,47 @@ void service_record::unpin() noexcept pinned_started = false; if (desired_state == ServiceState::STOPPED) { do_stop(); - service_set->processQueues(false); + services->processQueues(false); } } if (pinned_stopped) { pinned_stopped = false; if (desired_state == ServiceState::STARTED) { do_start(); - service_set->processQueues(true); + services->processQueues(true); } } } void service_record::queue_for_console() noexcept { - service_set->append_console_queue(this); + services->append_console_queue(this); } void service_record::release_console() noexcept { - service_set->pull_console_queue(); + services->pull_console_queue(); } void service_record::interrupt_start() noexcept { - service_set->unqueue_console(this); + services->unqueue_console(this); } -void ServiceSet::service_active(service_record *sr) noexcept +void service_set::service_active(service_record *sr) noexcept { active_services++; } -void ServiceSet::service_inactive(service_record *sr) noexcept +void service_set::service_inactive(service_record *sr) noexcept { active_services--; } -base_process_service::base_process_service(ServiceSet *sset, string name, ServiceType service_type, string &&command, +base_process_service::base_process_service(service_set *sset, string name, service_type service_type_p, string &&command, std::list> &command_offsets, sr_list * pdepends_on, sr_list * pdepends_soft) - : service_record(sset, name, service_type, std::move(command), command_offsets, + : service_record(sset, name, service_type_p, std::move(command), command_offsets, pdepends_on, pdepends_soft), child_listener(this), child_status_listener(this) { restart_interval_count = 0; @@ -1246,13 +1246,13 @@ void base_process_service::do_restart() noexcept desired_state = ServiceState::STOPPED; forceStop(); } - service_set->processQueues(); + services->processQueues(); } } bool base_process_service::restart_ps_process() noexcept { - using time_val = EventLoop_t::time_val; + using time_val = eventloop_t::time_val; time_val current_time; eventLoop.get_time(current_time, clock_type::MONOTONIC); @@ -1296,7 +1296,7 @@ void base_process_service::interrupt_start() noexcept service_record::interrupt_start(); } -dasynq::rearm process_restart_timer::timer_expiry(EventLoop_t &, int expiry_count) +dasynq::rearm process_restart_timer::timer_expiry(eventloop_t &, int expiry_count) { service->do_restart(); return dasynq::rearm::DISARM; diff --git a/src/service.h b/src/service.h index a99ea26..1eb47c3 100644 --- a/src/service.h +++ b/src/service.h @@ -15,8 +15,8 @@ #include "dinit-ll.h" /* - * This header defines ServiceRecord, a data record maintaining information about a service, - * and ServiceSet, a set of interdependent service records. It also defines some associated + * This header defines service_record, a data record maintaining information about a service, + * and service_set, a set of interdependent service records. It also defines some associated * types and exceptions. * * Service states @@ -101,7 +101,7 @@ * transition stage, at the latest. */ -struct OnstartFlags { +struct onstart_flags_t { bool rw_ready : 1; bool log_ready : 1; @@ -111,59 +111,59 @@ struct OnstartFlags { bool starts_on_console : 1; // starts in the foreground bool pass_cs_fd : 1; // pass this service a control socket connection via fd - OnstartFlags() noexcept : rw_ready(false), log_ready(false), + onstart_flags_t() noexcept : rw_ready(false), log_ready(false), no_sigterm(false), runs_on_console(false), starts_on_console(false), pass_cs_fd(false) { } }; // Exception while loading a service -class ServiceLoadExc +class service_load_exc { public: std::string serviceName; std::string excDescription; protected: - ServiceLoadExc(std::string serviceName, std::string &&desc) noexcept + service_load_exc(std::string serviceName, std::string &&desc) noexcept : serviceName(serviceName), excDescription(std::move(desc)) { } }; -class ServiceNotFound : public ServiceLoadExc +class service_not_found : public service_load_exc { public: - ServiceNotFound(std::string serviceName) noexcept - : ServiceLoadExc(serviceName, "Service description not found.") + service_not_found(std::string serviceName) noexcept + : service_load_exc(serviceName, "Service description not found.") { } }; -class ServiceCyclicDependency : public ServiceLoadExc +class service_cyclic_dependency : public service_load_exc { public: - ServiceCyclicDependency(std::string serviceName) noexcept - : ServiceLoadExc(serviceName, "Has cyclic dependency.") + service_cyclic_dependency(std::string serviceName) noexcept + : service_load_exc(serviceName, "Has cyclic dependency.") { } }; -class ServiceDescriptionExc : public ServiceLoadExc +class service_description_exc : public service_load_exc { public: - ServiceDescriptionExc(std::string serviceName, std::string &&extraInfo) noexcept - : ServiceLoadExc(serviceName, std::move(extraInfo)) + service_description_exc(std::string serviceName, std::string &&extraInfo) noexcept + : service_load_exc(serviceName, std::move(extraInfo)) { } }; class service_record; -class ServiceSet; +class service_set; class base_process_service; /* Service dependency record */ -class ServiceDep +class service_dep { service_record * from; service_record * to; @@ -174,7 +174,7 @@ class ServiceDep /* Whether the 'from' service is holding an acquire on the 'to' service */ bool holding_acq; - ServiceDep(service_record * from, service_record * to) noexcept : from(from), to(to), waiting_on(false), holding_acq(false) + service_dep(service_record * from, service_record * to) noexcept : from(from), to(to), waiting_on(false), holding_acq(false) { } service_record * getFrom() noexcept @@ -212,20 +212,20 @@ static std::vector separate_args(std::string &s, std::list +class service_child_watcher : public eventloop_t::child_proc_watcher_impl { public: base_process_service * service; - rearm status_change(EventLoop_t &eloop, pid_t child, int status) noexcept; + rearm status_change(eventloop_t &eloop, pid_t child, int status) noexcept; - ServiceChildWatcher(base_process_service * sr) noexcept : service(sr) { } + service_child_watcher(base_process_service * sr) noexcept : service(sr) { } }; -class ServiceIoWatcher : public EventLoop_t::fd_watcher_impl +class ServiceIoWatcher : public eventloop_t::fd_watcher_impl { public: base_process_service * service; - rearm fd_event(EventLoop_t &eloop, int fd, int flags) noexcept; + rearm fd_event(eventloop_t &eloop, int fd, int flags) noexcept; ServiceIoWatcher(base_process_service * sr) noexcept : service(sr) { } }; @@ -236,7 +236,7 @@ class service_record typedef std::string string; string service_name; - ServiceType service_type; /* ServiceType::DUMMY, PROCESS, SCRIPTED, INTERNAL */ + service_type record_type; /* ServiceType::DUMMY, PROCESS, SCRIPTED, INTERNAL */ ServiceState service_state = ServiceState::STOPPED; /* ServiceState::STOPPED, STARTING, STARTED, STOPPING */ ServiceState desired_state = ServiceState::STOPPED; /* ServiceState::STOPPED / STARTED */ @@ -248,7 +248,7 @@ class service_record string pid_file; - OnstartFlags onstart_flags; + onstart_flags_t onstart_flags; string logfile; // log file name, empty string specifies /dev/null @@ -274,10 +274,10 @@ class service_record typedef sr_list::iterator sr_iter; // list of soft dependencies - typedef std::list softdep_list; + typedef std::list softdep_list; // list of soft dependents - typedef std::list softdpt_list; + typedef std::list softdpt_list; sr_list depends_on; // services this one depends on sr_list dependents; // services depending on this one @@ -287,9 +287,9 @@ class service_record // unsigned wait_count; /* if we are waiting for dependents/dependencies to // start/stop, this is how many we're waiting for */ - ServiceSet *service_set; // the set this service belongs to + service_set *services; // the set this service belongs to - std::unordered_set listeners; + std::unordered_set listeners; // Process services: bool force_stop; // true if the service must actually stop. This is the @@ -313,7 +313,7 @@ class service_record // descriptor for the socket. - // Data for use by ServiceSet + // Data for use by service_set public: // Console queue. @@ -394,7 +394,7 @@ class service_record || (service_state == ServiceState::STARTING && waiting_for_deps); } - void notifyListeners(ServiceEvent event) noexcept + void notifyListeners(service_event event) noexcept { for (auto l : listeners) { l->serviceEvent(this, event); @@ -415,7 +415,7 @@ class service_record public: - service_record(ServiceSet *set, string name) + service_record(service_set *set, string name) : service_state(ServiceState::STOPPED), desired_state(ServiceState::STOPPED), auto_restart(false), smooth_recovery(false), pinned_stopped(false), pinned_started(false), waiting_for_deps(false), @@ -423,18 +423,18 @@ class service_record prop_require(false), prop_release(false), prop_failure(false), prop_start(false), prop_stop(false), restarting(false), force_stop(false) { - service_set = set; + services = set; service_name = name; - service_type = ServiceType::DUMMY; + record_type = service_type::DUMMY; } - service_record(ServiceSet *set, string name, ServiceType service_type, string &&command, std::list> &command_offsets, + service_record(service_set *set, string name, service_type record_type_p, string &&command, std::list> &command_offsets, sr_list * pdepends_on, sr_list * pdepends_soft) : service_record(set, name) { - service_set = set; + services = set; service_name = name; - this->service_type = service_type; + this->record_type = record_type_p; this->depends_on = std::move(*pdepends_on); program_name = std::move(command); @@ -508,7 +508,7 @@ class service_record } // Set "on start" flags (commands) - void setOnstartFlags(OnstartFlags flags) noexcept + void setOnstartFlags(onstart_flags_t flags) noexcept { this->onstart_flags = flags; } @@ -559,17 +559,17 @@ class service_record bool isDummy() noexcept { - return service_type == ServiceType::DUMMY; + return record_type == service_type::DUMMY; } // Add a listener. A listener must only be added once. May throw std::bad_alloc. - void addListener(ServiceListener * listener) + void addListener(service_listener * listener) { listeners.insert(listener); } // Remove a listener. - void removeListener(ServiceListener * listener) noexcept + void removeListener(service_listener * listener) noexcept { listeners.erase(listener); } @@ -579,7 +579,7 @@ class base_process_service; // A timer for process restarting. Used to ensure a minimum delay between // process restarts. -class process_restart_timer : public EventLoop_t::timer_impl +class process_restart_timer : public eventloop_t::timer_impl { public: base_process_service * service; @@ -588,12 +588,12 @@ class process_restart_timer : public EventLoop_t::timer_impl> &command_offsets, sr_list * pdepends_on, sr_list * pdepends_soft); @@ -664,10 +664,10 @@ class process_service : public base_process_service virtual void handle_exit_status(int exit_status) noexcept override; public: - process_service(ServiceSet *sset, string name, string &&command, + process_service(service_set *sset, string name, string &&command, std::list> &command_offsets, sr_list * pdepends_on, sr_list * pdepends_soft) - : base_process_service(sset, name, ServiceType::PROCESS, std::move(command), command_offsets, + : base_process_service(sset, name, service_type::PROCESS, std::move(command), command_offsets, pdepends_on, pdepends_soft) { } @@ -688,10 +688,10 @@ class bgproc_service : public base_process_service bool read_pid_file() noexcept; public: - bgproc_service(ServiceSet *sset, string name, string &&command, + bgproc_service(service_set *sset, string name, string &&command, std::list> &command_offsets, sr_list * pdepends_on, sr_list * pdepends_soft) - : base_process_service(sset, name, ServiceType::BGPROCESS, std::move(command), command_offsets, + : base_process_service(sset, name, service_type::BGPROCESS, std::move(command), command_offsets, pdepends_on, pdepends_soft) { doing_recovery = false; @@ -708,10 +708,10 @@ class scripted_service : public base_process_service virtual void handle_exit_status(int exit_status) noexcept override; public: - scripted_service(ServiceSet *sset, string name, string &&command, + scripted_service(service_set *sset, string name, string &&command, std::list> &command_offsets, sr_list * pdepends_on, sr_list * pdepends_soft) - : base_process_service(sset, name, ServiceType::SCRIPTED, std::move(command), command_offsets, + : base_process_service(sset, name, service_type::SCRIPTED, std::move(command), command_offsets, pdepends_on, pdepends_soft) { } @@ -737,7 +737,7 @@ inline auto extract_console_queue(service_record *sr) -> decltype(sr->console_qu } /* - * A ServiceSet, as the name suggests, manages a set of services. + * A service_set, as the name suggests, manages a set of services. * * Other than the ability to find services by name, the service set manages various queues. * One is the queue for processes wishing to acquire the console. There is also a set of @@ -754,14 +754,14 @@ inline auto extract_console_queue(service_record *sr) -> decltype(sr->console_qu * process is finite because starting a service can never cause services to stop, unless they * fail to start, which should cause them to stop semi-permanently. */ -class ServiceSet +class service_set { int active_services; std::list records; const char *service_dir; // directory containing service descriptions bool restart_enabled; // whether automatic restart is enabled (allowed) - ShutdownType shutdown_type = ShutdownType::CONTINUE; // Shutdown type, if stopping + shutdown_type_t shutdown_type = shutdown_type_t::CONTINUE; // Shutdown type, if stopping // Services waiting for exclusive access to the console dlist console_queue; @@ -782,7 +782,7 @@ class ServiceSet // Public public: - ServiceSet(const char *service_dir) + service_set(const char *service_dir) { this->service_dir = service_dir; active_services = 0; @@ -909,7 +909,7 @@ class ServiceSet return active_services; } - void stop_all_services(ShutdownType type = ShutdownType::HALT) noexcept + void stop_all_services(shutdown_type_t type = shutdown_type_t::HALT) noexcept { restart_enabled = false; shutdown_type = type; @@ -930,7 +930,7 @@ class ServiceSet return restart_enabled; } - ShutdownType getShutdownType() noexcept + shutdown_type_t getShutdownType() noexcept { return shutdown_type; } diff --git a/src/shutdown.cc b/src/shutdown.cc index b9ff24c..9f66e1a 100644 --- a/src/shutdown.cc +++ b/src/shutdown.cc @@ -21,10 +21,10 @@ // shutdown: shut down the system // This utility communicates with the dinit daemon via a unix socket (/dev/initctl). -void do_system_shutdown(ShutdownType shutdown_type); +void do_system_shutdown(shutdown_type_t shutdown_type); static void unmount_disks(); static void swap_off(); -static void wait_for_reply(CPBuffer<1024> &rbuffer, int fd); +static void wait_for_reply(cpbuffer<1024> &rbuffer, int fd); class ReadCPException @@ -43,7 +43,7 @@ int main(int argc, char **argv) bool sys_shutdown = false; bool use_passed_cfd = false; - auto shutdown_type = ShutdownType::POWEROFF; + auto shutdown_type = shutdown_type_t::POWEROFF; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { @@ -56,13 +56,13 @@ int main(int argc, char **argv) sys_shutdown = true; } else if (strcmp(argv[i], "-r") == 0) { - shutdown_type = ShutdownType::REBOOT; + shutdown_type = shutdown_type_t::REBOOT; } else if (strcmp(argv[i], "-h") == 0) { - shutdown_type = ShutdownType::HALT; + shutdown_type = shutdown_type_t::HALT; } else if (strcmp(argv[i], "-p") == 0) { - shutdown_type = ShutdownType::POWEROFF; + shutdown_type = shutdown_type_t::POWEROFF; } else if (strcmp(argv[i], "--use-passed-cfd") == 0) { use_passed_cfd = true; @@ -161,7 +161,7 @@ int main(int argc, char **argv) // cout << "Received acknowledgement. System should now shut down." << endl; //} - CPBuffer<1024> rbuffer; + cpbuffer<1024> rbuffer; try { wait_for_reply(rbuffer, socknum); @@ -188,7 +188,7 @@ int main(int argc, char **argv) // errcode = 0 if end of stream (remote end closed) // errcode = errno if another error occurred // Note that EINTR is ignored (i.e. the read will be re-tried). -static void fillBufferTo(CPBuffer<1024> *buf, int fd, int rlength) +static void fillBufferTo(cpbuffer<1024> *buf, int fd, int rlength) { do { int r = buf->fill_to(fd, rlength); @@ -209,7 +209,7 @@ static void fillBufferTo(CPBuffer<1024> *buf, int fd, int rlength) // Wait for a reply packet, skipping over any information packets // that are received in the meantime. -static void wait_for_reply(CPBuffer<1024> &rbuffer, int fd) +static void wait_for_reply(cpbuffer<1024> &rbuffer, int fd) { fillBufferTo(&rbuffer, fd, 1); @@ -225,7 +225,7 @@ static void wait_for_reply(CPBuffer<1024> &rbuffer, int fd) } // Actually shut down the system. -void do_system_shutdown(ShutdownType shutdown_type) +void do_system_shutdown(shutdown_type_t shutdown_type) { using namespace std; @@ -235,8 +235,8 @@ void do_system_shutdown(ShutdownType shutdown_type) sigprocmask(SIG_SETMASK, &allsigs, nullptr); int reboot_type = 0; - if (shutdown_type == ShutdownType::REBOOT) reboot_type = RB_AUTOBOOT; - else if (shutdown_type == ShutdownType::POWEROFF) reboot_type = RB_POWER_OFF; + if (shutdown_type == shutdown_type_t::REBOOT) reboot_type = RB_AUTOBOOT; + else if (shutdown_type == shutdown_type_t::POWEROFF) reboot_type = RB_POWER_OFF; else reboot_type = RB_HALT_SYSTEM; // Write to console rather than any terminal, since we lose the terminal it seems: -- 2.25.1