X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fdinitctl.cc;h=f260bf0a9d0aa5d1a4f4708357b010e7845b8f31;hb=ff68ea5768509a1d84f1b14f9232610388cfce06;hp=e8c5daa38385fb35b8a361b03f2a2f3e9dbfd6e7;hpb=a65509aebe197004804e26e03db0cd732690aeff;p=oweals%2Fdinit.git diff --git a/src/dinitctl.cc b/src/dinitctl.cc index e8c5daa..f260bf0 100644 --- a/src/dinitctl.cc +++ b/src/dinitctl.cc @@ -3,10 +3,14 @@ #include #include #include +#include #include #include +#include #include +#include +#include #include #include #include @@ -16,53 +20,33 @@ #include "control-cmds.h" #include "service-constants.h" #include "cpbuffer.h" +#include "dinit-client.h" +#include "load-service.h" +#include "dinit-util.h" +#include "mconfig.h" // dinitctl: utility to control the Dinit daemon, including starting and stopping of services. -// This utility communicates with the dinit daemon via a unix stream socket (/dev/initctl, or $HOME/.dinitctl). +// This utility communicates with the dinit daemon via a unix stream socket (as specified in +// SYSCONTROLSOCKET, or $HOME/.dinitctl). -using handle_t = uint32_t; +static constexpr uint16_t min_cp_version = 1; +static constexpr uint16_t max_cp_version = 1; +enum class command_t; -class ReadCPException -{ - public: - int errcode; - ReadCPException(int err) : errcode(err) { } -}; - -enum class Command; - -static int issueLoadService(int socknum, const char *service_name); -static int checkLoadReply(int socknum, cpbuffer<1024> &rbuffer, handle_t *handle_p, service_state_t *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); - - -// Fill a circular buffer from a file descriptor, reading at least _rlength_ bytes. -// Throws ReadException if the requested number of bytes cannot be read, with: -// 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) -{ - do { - int r = buf->fill_to(fd, rlength); - if (r == -1) { - if (errno != EINTR) { - throw ReadCPException(errno); - } - } - else if (r == 0) { - throw ReadCPException(0); - } - else { - return; - } - } - while (true); -} +static int issue_load_service(int socknum, const char *service_name, bool find_only = false); +static int check_load_reply(int socknum, cpbuffer_t &, handle_t *handle_p, service_state_t *state_p); +static int start_stop_service(int socknum, cpbuffer_t &, const char *service_name, command_t command, + bool do_pin, bool do_force, bool wait_for_service, bool verbose); +static int unpin_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose); +static int unload_service(int socknum, cpbuffer_t &, const char *service_name); +static int list_services(int socknum, cpbuffer_t &); +static int shutdown_dinit(int soclknum, cpbuffer_t &); +static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add, const char *service_from, + const char *service_to, dependency_type dep_type); +static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to, + bool enable); static const char * describeState(bool stopped) { @@ -74,61 +58,34 @@ static const char * describeVerb(bool stop) return stop ? "stop" : "start"; } -// 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) -{ - fillBufferTo(&rbuffer, fd, 1); - - while (rbuffer[0] >= 100) { - // Information packet; discard. - fillBufferTo(&rbuffer, fd, 1); - int pktlen = (unsigned char) rbuffer[1]; - - rbuffer.consume(1); // Consume one byte so we'll read one byte of the next packet - fillBufferTo(&rbuffer, fd, pktlen); - rbuffer.consume(pktlen - 1); - } -} - - -// Write *all* the requested buffer and re-try if necessary until -// the buffer is written or an unrecoverable error occurs. -static int write_all(int fd, const void *buf, size_t count) -{ - const char *cbuf = static_cast(buf); - int w = 0; - while (count > 0) { - int r = write(fd, cbuf, count); - if (r == -1) { - if (errno == EINTR) continue; - return r; - } - w += r; - cbuf += r; - count -= r; - } - return w; -} - - -enum class Command { +enum class command_t { NONE, START_SERVICE, WAKE_SERVICE, STOP_SERVICE, + RESTART_SERVICE, RELEASE_SERVICE, UNPIN_SERVICE, - LIST_SERVICES + UNLOAD_SERVICE, + LIST_SERVICES, + SHUTDOWN, + ADD_DEPENDENCY, + RM_DEPENDENCY, + ENABLE_SERVICE, + DISABLE_SERVICE }; + // Entry point. int main(int argc, char **argv) { using namespace std; bool show_help = argc < 2; - char *service_name = nullptr; + const char *service_name = nullptr; + const char *to_service_name = nullptr; + dependency_type dep_type; + bool dep_type_set = false; std::string control_socket_str; const char * control_socket_path = nullptr; @@ -137,8 +94,9 @@ int main(int argc, char **argv) bool sys_dinit = false; // communicate with system daemon bool wait_for_service = true; bool do_pin = false; + bool do_force = false; - Command command = Command::NONE; + command_t command = command_t::NONE; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { @@ -158,99 +116,202 @@ int main(int argc, char **argv) else if (strcmp(argv[i], "--pin") == 0) { do_pin = true; } + else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) { + ++i; + if (i == argc) { + cerr << "dinitctl: --socket-path/-p should be followed by socket path" << std::endl; + return 1; + } + control_socket_str = argv[i]; + } + else if ((command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) + && strcmp(argv[i], "--from") == 0) { + ++i; + if (i == argc) { + cerr << "dinitctl: --from should be followed by a service name" << std::endl; + return 1; + } + service_name = argv[i]; + } + else if ((command == command_t::STOP_SERVICE || command == command_t::RESTART_SERVICE) + && (strcmp(argv[i], "--force") == 0 || strcmp(argv[i], "-f") == 0)) { + do_force = true; + } else { + cerr << "dinitctl: unrecognized/invalid option: " << argv[i] << " (use --help for help)\n"; return 1; } } - else if (command == Command::NONE) { + else if (command == command_t::NONE) { if (strcmp(argv[i], "start") == 0) { - command = Command::START_SERVICE; + command = command_t::START_SERVICE; } else if (strcmp(argv[i], "wake") == 0) { - command = Command::WAKE_SERVICE; + command = command_t::WAKE_SERVICE; } else if (strcmp(argv[i], "stop") == 0) { - command = Command::STOP_SERVICE; + command = command_t::STOP_SERVICE; + } + else if (strcmp(argv[i], "restart") == 0) { + command = command_t::RESTART_SERVICE; } else if (strcmp(argv[i], "release") == 0) { - command = Command::RELEASE_SERVICE; + command = command_t::RELEASE_SERVICE; } else if (strcmp(argv[i], "unpin") == 0) { - command = Command::UNPIN_SERVICE; + command = command_t::UNPIN_SERVICE; + } + else if (strcmp(argv[i], "unload") == 0) { + command = command_t::UNLOAD_SERVICE; } else if (strcmp(argv[i], "list") == 0) { - command = Command::LIST_SERVICES; + command = command_t::LIST_SERVICES; + } + else if (strcmp(argv[i], "shutdown") == 0) { + command = command_t::SHUTDOWN; + } + else if (strcmp(argv[i], "add-dep") == 0) { + command = command_t::ADD_DEPENDENCY; + } + else if (strcmp(argv[i], "rm-dep") == 0) { + command = command_t::RM_DEPENDENCY; + } + else if (strcmp(argv[i], "enable") == 0) { + command = command_t::ENABLE_SERVICE; + } + else if (strcmp(argv[i], "disable") == 0) { + command = command_t::DISABLE_SERVICE; } else { - show_help = true; - break; + cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n"; + return 1; } } else { - // service name - if (service_name != nullptr) { - show_help = true; - break; + // service name / other non-option + if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) { + if (! dep_type_set) { + if (strcmp(argv[i], "regular") == 0) { + dep_type = dependency_type::REGULAR; + } + else if (strcmp(argv[i], "milestone") == 0) { + dep_type = dependency_type::MILESTONE; + } + else if (strcmp(argv[i], "waits-for") == 0) { + dep_type = dependency_type::WAITS_FOR; + } + else { + show_help = true; + break; + } + dep_type_set = true; + } + else if (service_name == nullptr) { + service_name = argv[i]; + } + else if (to_service_name == nullptr) { + to_service_name = argv[i]; + } + else { + show_help = true; + break; + } + } + else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) { + if (to_service_name != nullptr) { + show_help = true; + break; + } + to_service_name = argv[i]; + } + else { + if (service_name != nullptr) { + show_help = true; + break; + } + service_name = argv[i]; + // TODO support multiple services } - service_name = argv[i]; - // TODO support multiple services } } - if (service_name != nullptr && command == Command::LIST_SERVICES) { + bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN); + + if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) { + show_help |= (to_service_name == nullptr); + } + else if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) { show_help = true; } - - if ((service_name == nullptr && command != Command::LIST_SERVICES) || command == Command::NONE) { + + if (service_name != nullptr && no_service_cmd) { + show_help = true; + } + + if ((command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) + && (! dep_type_set || service_name == nullptr || to_service_name == nullptr)) { show_help = true; } if (show_help) { - cout << "dinitctl: control Dinit services" << endl; - - cout << "\nUsage:" << endl; - cout << " dinitctl [options] start [options] : start and activate service" << endl; - cout << " dinitctl [options] stop [options] : stop service and cancel explicit activation" << endl; - cout << " dinitctl [options] wake [options] : start but do not mark activated" << endl; - cout << " dinitctl [options] release [options] : release activation, stop if no dependents" << endl; - cout << " dinitctl [options] unpin : un-pin the service (after a previous pin)" << endl; - cout << " dinitctl list : list loaded services" << endl; - - cout << "\nNote: An activated service continues running when its dependents stop." << endl; - - cout << "\nGeneral options:" << endl; - cout << " -s, --system : control system daemon instead of user daemon" << endl; - cout << " --quiet : suppress output (except errors)" << endl; - - cout << "\nCommand options:" << endl; - cout << " --help : show this help" << endl; - cout << " --no-wait : don't wait for service startup/shutdown to complete" << endl; - cout << " --pin : pin the service in the requested (started/stopped) state" << endl; + cout << "dinitctl: control Dinit services\n" + "\n" + "Usage:\n" + " dinitctl [options] start [options] \n" + " dinitctl [options] stop [options] \n" + " dinitctl [options] wake [options] \n" + " dinitctl [options] release [options] \n" + " dinitctl [options] unpin \n" + " dinitctl [options] unload \n" + " dinitctl [options] list\n" + " dinitctl [options] shutdown\n" + " dinitctl [options] add-dep \n" + " dinitctl [options] rm-dep \n" + " dinitctl [options] enable [--from ] \n" + " dinitctl [options] disable [--from ] \n" + "\n" + "Note: An activated service continues running when its dependents stop.\n" + "\n" + "General options:\n" + " --help : show this help\n" + " -s, --system : control system daemon instead of user daemon\n" + " --quiet : suppress output (except errors)\n" + " --socket-path , -p \n" + " : specify socket for communication with daemon\n" + "\n" + "Command options:\n" + " --no-wait : don't wait for service startup/shutdown to complete\n" + " --pin : pin the service in the requested state\n" + " --force : force stop even if dependents will be affected\n"; return 1; } signal(SIGPIPE, SIG_IGN); - control_socket_path = "/dev/dinitctl"; - // Locate control socket - if (! sys_dinit) { - char * userhome = getenv("HOME"); - if (userhome == nullptr) { - struct passwd * pwuid_p = getpwuid(getuid()); - if (pwuid_p != nullptr) { - userhome = pwuid_p->pw_dir; + if (! control_socket_str.empty()) { + control_socket_path = control_socket_str.c_str(); + } + else { + control_socket_path = SYSCONTROLSOCKET; // default to system + if (! sys_dinit) { + char * userhome = getenv("HOME"); + if (userhome == nullptr) { + struct passwd * pwuid_p = getpwuid(getuid()); + if (pwuid_p != nullptr) { + userhome = pwuid_p->pw_dir; + } + } + + if (userhome != nullptr) { + control_socket_str = userhome; + control_socket_str += "/.dinitctl"; + control_socket_path = control_socket_str.c_str(); + } + else { + cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl; + return 1; } - } - - if (userhome != nullptr) { - control_socket_str = userhome; - control_socket_str += "/.dinitctl"; - control_socket_path = control_socket_str.c_str(); - } - else { - cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl; - return 1; } } @@ -277,217 +338,349 @@ int main(int argc, char **argv) return 1; } - // TODO should start by querying protocol version - - if (command == Command::UNPIN_SERVICE) { - return unpinService(socknum, service_name, verbose); + try { + // Start by querying protocol version: + cpbuffer_t rbuffer; + check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum); + + if (command == command_t::UNPIN_SERVICE) { + return unpin_service(socknum, rbuffer, service_name, verbose); + } + else if (command == command_t::UNLOAD_SERVICE) { + return unload_service(socknum, rbuffer, service_name); + } + else if (command == command_t::LIST_SERVICES) { + return list_services(socknum, rbuffer); + } + else if (command == command_t::SHUTDOWN) { + return shutdown_dinit(socknum, rbuffer); + } + else if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) { + return add_remove_dependency(socknum, rbuffer, command == command_t::ADD_DEPENDENCY, + service_name, to_service_name, dep_type); + } + else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) { + // If only one service specified, assume that we enable for 'boot' service: + if (service_name == nullptr) { + service_name = "boot"; + } + return enable_disable_service(socknum, rbuffer, service_name, to_service_name, + command == command_t::ENABLE_SERVICE); + } + else { + return start_stop_service(socknum, rbuffer, service_name, command, do_pin, do_force, + wait_for_service, verbose); + } + } + catch (cp_old_client_exception &e) { + std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl; + return 1; } - else if (command == Command::LIST_SERVICES) { - return listServices(socknum); + catch (cp_old_server_exception &e) { + std::cerr << "dinitctl: server too old or protocol error" << std::endl; + return 1; + } + catch (cp_read_exception &e) { + cerr << "dinitctl: control socket read failure or protocol error" << endl; + return 1; + } + catch (cp_write_exception &e) { + cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl; + return 1; + } +} + +// Extract/read a string of specified length from the buffer/socket. The string is consumed +// from the buffer. +static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length) +{ + int rb_len = rbuffer.get_length(); + if (uint32_t(rb_len) >= length) { + std::string r = rbuffer.extract_string(0, length); + rbuffer.consume(length); + return r; } - return startStopService(socknum, service_name, command, do_pin, wait_for_service, verbose); + std::string r = rbuffer.extract_string(0, rb_len); + uint32_t rlen = length - rb_len; + uint32_t clen; + do { + rbuffer.reset(); + rbuffer.fill(socknum); + char *bptr = rbuffer.get_ptr(0); + clen = rbuffer.get_length(); + clen = std::min(clen, rlen); + r.append(bptr, clen); + rlen -= clen; + } while (rlen > 0); + + rbuffer.consume(clen); + + return r; +} + +// Load a service: issue load command, wait for reply. Return true on success, display error message +// and return false on failure. +// socknum - the socket fd to communicate via +// rbuffer - the buffer for communication +// name - the name of the service to load +// handle - where to store the handle of the loaded service +// state - where to store the state of the loaded service (may be null). +static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle, + service_state_t *state) +{ + // Load 'to' service: + if (issue_load_service(socknum, name)) { + return false; + } + + wait_for_reply(rbuffer, socknum); + + if (check_load_reply(socknum, rbuffer, handle, state) != 0) { + return false; + } + + return true; +} + +// Get the service name for a given handle, by querying the daemon. +static std::string get_service_name(int socknum, cpbuffer_t &rbuffer, handle_t handle) +{ + auto m = membuf() + .append((char) DINIT_CP_QUERYSERVICENAME) + .append((char) 0) + .append(handle); + write_all_x(socknum, m); + + wait_for_reply(rbuffer, socknum); + + if (rbuffer[0] != DINIT_RP_SERVICENAME) { + throw cp_read_exception{0}; + } + + // 1 byte reserved + // uint16_t size + fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint16_t)); + uint16_t namesize; + rbuffer.extract(&namesize, 2, sizeof(uint16_t)); + rbuffer.consume(2 + sizeof(uint16_t)); + + std::string name; + + do { + if (rbuffer.get_length() == 0) { + rbuffer.fill(socknum); + } + + size_t to_extract = std::min(size_t(rbuffer.get_length()), namesize - name.length()); + size_t contiguous_len = rbuffer.get_contiguous_length(rbuffer.get_ptr(0)); + if (contiguous_len <= to_extract) { + name.append(rbuffer.get_ptr(0), contiguous_len); + rbuffer.consume(contiguous_len); + name.append(rbuffer.get_ptr(0), to_extract - contiguous_len); + rbuffer.consume(to_extract - contiguous_len); + } + else { + name.append(rbuffer.get_ptr(0), to_extract); + rbuffer.consume(to_extract); + break; + } + + } while (name.length() < namesize); + + return name; } // Start/stop a service -static int startStopService(int socknum, const char *service_name, Command command, bool do_pin, bool wait_for_service, bool verbose) +static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, + command_t command, bool do_pin, bool do_force, bool wait_for_service, bool verbose) { using namespace std; - bool do_stop = (command == Command::STOP_SERVICE || command == Command::RELEASE_SERVICE); + bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE); + + service_state_t state; + handle_t handle; - if (issueLoadService(socknum, service_name)) { + if (! load_service(socknum, rbuffer, service_name, &handle, &state)) { return 1; } - // Now we expect a reply: - - try { - cpbuffer<1024> rbuffer; - wait_for_reply(rbuffer, socknum); - - service_state_t state; - //service_state_t target_state; - handle_t handle; - - if (checkLoadReply(socknum, rbuffer, &handle, &state) != 0) { - return 0; - } - - service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED; - int pcommand = 0; - switch (command) { - case Command::STOP_SERVICE: + service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED; + int pcommand = 0; + switch (command) { + case command_t::STOP_SERVICE: + case command_t::RESTART_SERVICE: // stop, and then start pcommand = DINIT_CP_STOPSERVICE; break; - case Command::RELEASE_SERVICE: + case command_t::RELEASE_SERVICE: pcommand = DINIT_CP_RELEASESERVICE; break; - case Command::START_SERVICE: + case command_t::START_SERVICE: pcommand = DINIT_CP_STARTSERVICE; break; - case Command::WAKE_SERVICE: + case command_t::WAKE_SERVICE: pcommand = DINIT_CP_WAKESERVICE; break; default: ; + } + + // Need to issue STOPSERVICE/STARTSERVICE + // We'll do this regardless of the current service state / target state, since issuing + // start/stop also sets or clears the "explicitly started" flag on the service. + { + char flags = (do_pin ? 1 : 0) | ((pcommand == DINIT_CP_STOPSERVICE && !do_force) ? 2 : 0); + if (command == command_t::RESTART_SERVICE) { + flags |= 4; } - - // Need to issue STOPSERVICE/STARTSERVICE - // We'll do this regardless of the current service state / target state, since issuing - // start/stop also sets or clears the "explicitly started" flag on the service. - { - int r; - - { - auto buf = new char[2 + sizeof(handle)]; - unique_ptr ubuf(buf); - - buf[0] = pcommand; - buf[1] = do_pin ? 1 : 0; - memcpy(buf + 2, &handle, sizeof(handle)); - r = write_all(socknum, buf, 2 + sizeof(handle)); - } - - if (r == -1) { - perror("dinitctl: write"); - return 1; - } - - wait_for_reply(rbuffer, socknum); - if (rbuffer[0] == DINIT_RP_ALREADYSS) { - bool already = (state == wanted_state); - if (verbose) { - cout << "Service " << (already ? "(already) " : "") << describeState(do_stop) << "." << endl; - } - return 0; // success! - } - if (rbuffer[0] != DINIT_RP_ACK) { - cerr << "dinitctl: Protocol error." << endl; - return 1; + + auto m = membuf() + .append((char) pcommand) + .append(flags) + .append(handle); + write_all_x(socknum, m); + + wait_for_reply(rbuffer, socknum); + auto reply_pkt_h = rbuffer[0]; + rbuffer.consume(1); // consume header + if (reply_pkt_h == DINIT_RP_ALREADYSS) { + bool already = (state == wanted_state); + if (verbose) { + cout << "Service " << (already ? "(already) " : "") + << describeState(do_stop) << "." << endl; } - rbuffer.consume(1); + return 0; // success! } - - if (! wait_for_service) { - if (verbose) { - cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl; + if (reply_pkt_h == DINIT_RP_DEPENDENTS && pcommand == DINIT_CP_STOPSERVICE) { + cerr << "dinitctl: Cannot stop service due to the following dependents:\n" + "(Only direct dependents are listed. Exercise caution before using '--force' !!)\n"; + // size_t number, N * handle_t handles + size_t number; + rbuffer.fill_to(socknum, sizeof(number)); + rbuffer.extract(&number, 0, sizeof(number)); + rbuffer.consume(sizeof(number)); + std::vector handles; + handles.reserve(number); + for (size_t i = 0; i < number; i++) { + handle_t handle; + rbuffer.fill_to(socknum, sizeof(handle_t)); + rbuffer.extract(&handle, 0, sizeof(handle)); + handles.push_back(handle); + rbuffer.consume(sizeof(handle)); + } + // Print the directly affected dependents: + cerr << " "; + for (handle_t handle : handles) { + cerr << " " << get_service_name(socknum, rbuffer, handle); } - return 0; + cerr << "\n"; + return 1; } - - service_event completionEvent; - service_event cancelledEvent; - - if (do_stop) { - completionEvent = service_event::STOPPED; - cancelledEvent = service_event::STOPCANCELLED; + if (reply_pkt_h != DINIT_RP_ACK && reply_pkt_h != DINIT_RP_ALREADYSS) { + cerr << "dinitctl: protocol error." << endl; + return 1; } - else { - completionEvent = service_event::STARTED; - cancelledEvent = service_event::STARTCANCELLED; + } + + if (! wait_for_service) { + if (verbose) { + cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl; } - - // Wait until service started: - int r = rbuffer.fill_to(socknum, 2); - while (r > 0) { - if (rbuffer[0] >= 100) { - int pktlen = (unsigned char) rbuffer[1]; - fillBufferTo(&rbuffer, socknum, pktlen); - - if (rbuffer[0] == DINIT_IP_SERVICEEVENT) { - handle_t ev_handle; - rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle)); - service_event event = static_cast(rbuffer[2 + sizeof(ev_handle)]); - if (ev_handle == handle) { - if (event == completionEvent) { - if (verbose) { - cout << "Service " << describeState(do_stop) << "." << endl; - } - return 0; + return 0; + } + + service_event_t completionEvent; + service_event_t cancelledEvent; + + if (do_stop) { + completionEvent = service_event_t::STOPPED; + cancelledEvent = service_event_t::STOPCANCELLED; + } + else { + completionEvent = service_event_t::STARTED; + cancelledEvent = service_event_t::STARTCANCELLED; + } + + // Wait until service started: + int r = rbuffer.fill_to(socknum, 2); + while (r > 0) { + if (rbuffer[0] >= 100) { + int pktlen = (unsigned char) rbuffer[1]; + fill_buffer_to(rbuffer, socknum, pktlen); + + if (rbuffer[0] == DINIT_IP_SERVICEEVENT) { + handle_t ev_handle; + rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle)); + service_event_t event = static_cast(rbuffer[2 + sizeof(ev_handle)]); + if (ev_handle == handle) { + if (event == completionEvent) { + if (verbose) { + cout << "Service " << describeState(do_stop) << "." << endl; } - else if (event == cancelledEvent) { - if (verbose) { - cout << "Service " << describeVerb(do_stop) << " cancelled." << endl; - } - return 1; + return 0; + } + else if (event == cancelledEvent) { + if (verbose) { + cout << "Service " << describeVerb(do_stop) << " cancelled." << endl; } - else if (! do_stop && event == service_event::FAILEDSTART) { - if (verbose) { - cout << "Service failed to start." << endl; - } - return 1; + return 1; + } + else if (! do_stop && event == service_event_t::FAILEDSTART) { + if (verbose) { + cout << "Service failed to start." << endl; } + return 1; } } - - rbuffer.consume(pktlen); - r = rbuffer.fill_to(socknum, 2); - } - else { - // Not an information packet? - cerr << "dinitctl: protocol error" << endl; - return 1; } - } - - if (r == -1) { - perror("dinitctl: read"); + + rbuffer.consume(pktlen); + r = rbuffer.fill_to(socknum, 2); } else { - cerr << "protocol error (connection closed by server)" << endl; + // Not an information packet? + cerr << "dinitctl: protocol error" << endl; + return 1; } - return 1; } - catch (ReadCPException &exc) { - cerr << "dinitctl: control socket read failure or protocol error" << endl; - return 1; + + if (r == -1) { + perror("dinitctl: read"); } - catch (std::bad_alloc &exc) { - cerr << "dinitctl: out of memory" << endl; - return 1; + else { + cerr << "protocol error (connection closed by server)" << endl; } - - return 0; + return 1; } // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for // a response. Returns 1 on failure (with error logged), 0 on success. -static int issueLoadService(int socknum, const char *service_name) +static int issue_load_service(int socknum, const char *service_name, bool find_only) { - using namespace std; - // Build buffer; uint16_t sname_len = strlen(service_name); int bufsize = 3 + sname_len; - int r; - { - // TODO: new: catch exception - unique_ptr ubuf(new char[bufsize]); - auto buf = ubuf.get(); - - buf[0] = DINIT_CP_LOADSERVICE; - memcpy(buf + 1, &sname_len, 2); - memcpy(buf + 3, service_name, sname_len); - - r = write_all(socknum, buf, bufsize); - } - - if (r == -1) { - perror("dinitctl: write"); - return 1; - } + std::unique_ptr ubuf(new char[bufsize]); + auto buf = ubuf.get(); + + buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE; + memcpy(buf + 1, &sname_len, 2); + memcpy(buf + 3, service_name, sname_len); + + write_all_x(socknum, buf, bufsize); return 0; } // 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, service_state_t *state_p) +// state_p may be null. +static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p) { using namespace std; if (rbuffer[0] == DINIT_RP_SERVICERECORD) { - fillBufferTo(&rbuffer, socknum, 2 + sizeof(*handle_p)); + fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p)); rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p)); if (state_p) *state_p = static_cast(rbuffer[1]); //target_state = static_cast(rbuffer[2 + sizeof(handle)]); @@ -495,144 +688,458 @@ static int checkLoadReply(int socknum, cpbuffer<1024> &rbuffer, handle_t *handle return 0; } else if (rbuffer[0] == DINIT_RP_NOSERVICE) { - cerr << "dinitctl: Failed to find/load service." << endl; + cerr << "dinitctl: failed to find/load service." << endl; return 1; } else { - cerr << "dinitctl: Protocol error." << endl; + cerr << "dinitctl: protocol error." << endl; return 1; } } -static int unpinService(int socknum, const char *service_name, bool verbose) +static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose) { using namespace std; + + handle_t handle; // Build buffer; - if (issueLoadService(socknum, service_name) == 1) { + if (! load_service(socknum, rbuffer, service_name, &handle, nullptr)) { return 1; } - - // Now we expect a reply: - try { - cpbuffer<1024> rbuffer; + // Issue UNPIN command. + { + auto m = membuf() + .append(DINIT_CP_UNPINSERVICE) + .append(handle); + write_all_x(socknum, m); + wait_for_reply(rbuffer, socknum); + if (rbuffer[0] != DINIT_RP_ACK) { + cerr << "dinitctl: protocol error." << endl; + return 1; + } + rbuffer.consume(1); + } + + if (verbose) { + cout << "Service unpinned." << endl; + } + return 0; +} + +static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name) +{ + using namespace std; + + if (issue_load_service(socknum, service_name, true) == 1) { + return 1; + } + + wait_for_reply(rbuffer, socknum); + + handle_t handle; + + if (rbuffer[0] == DINIT_RP_NOSERVICE) { + cerr << "dinitctl: service not loaded." << endl; + return 1; + } + + if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) { + return 1; + } + + // Issue UNLOAD command. + { + auto m = membuf() + .append(DINIT_CP_UNLOADSERVICE) + .append(handle); + write_all_x(socknum, m); + + wait_for_reply(rbuffer, socknum); + if (rbuffer[0] == DINIT_RP_NAK) { + cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of " + "other service." << endl; + return 1; + } + if (rbuffer[0] != DINIT_RP_ACK) { + cerr << "dinitctl: Protocol error." << endl; + return 1; + } + rbuffer.consume(1); + } + + cout << "Service unloaded." << endl; + return 0; +} + +static int list_services(int socknum, cpbuffer_t &rbuffer) +{ + using namespace std; + + char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES }; + write_all_x(socknum, cmdbuf, 1); + + wait_for_reply(rbuffer, socknum); + while (rbuffer[0] == DINIT_RP_SVCINFO) { + int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t)); + fill_buffer_to(rbuffer, socknum, hdrsize); + int nameLen = rbuffer[1]; + service_state_t current = static_cast(rbuffer[2]); + service_state_t target = static_cast(rbuffer[3]); + + int console_flags = rbuffer[4]; + bool has_console = (console_flags & 2) != 0; + bool waiting_console = (console_flags & 1) != 0; + bool was_skipped = (console_flags & 4) != 0; + + stopped_reason_t stop_reason = static_cast(rbuffer[5]); + + pid_t service_pid; + int exit_status; + if (current != service_state_t::STOPPED) { + rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid)); + } + else { + rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status)); + } + + fill_buffer_to(rbuffer, socknum, nameLen + hdrsize); + + char *name_ptr = rbuffer.get_ptr(hdrsize); + int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen); + + string name = string(name_ptr, clength); + name.append(rbuffer.get_buf_base(), nameLen - clength); + + cout << "["; + + cout << (target == service_state_t::STARTED ? "{" : " "); + if (current == service_state_t::STARTED) { + cout << (was_skipped ? "s" : "+"); + } + else { + cout << " "; + } + cout << (target == service_state_t::STARTED ? "}" : " "); - handle_t handle; + if (current == service_state_t::STARTING) { + cout << "<<"; + } + else if (current == service_state_t::STOPPING) { + cout << ">>"; + } + else { + cout << " "; + } - if (checkLoadReply(socknum, rbuffer, &handle, nullptr) != 0) { - return 1; + cout << (target == service_state_t::STOPPED ? "{" : " "); + if (current == service_state_t::STOPPED) { + bool did_fail = false; + if (stop_reason == stopped_reason_t::TERMINATED) { + if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) { + did_fail = true; + } + } + else did_fail = (stop_reason != stopped_reason_t::NORMAL); + + cout << (did_fail ? "X" : "-"); + } + else { + cout << " "; + } + cout << (target == service_state_t::STOPPED ? "}" : " "); + + cout << "] " << name; + + if (current != service_state_t::STOPPED && service_pid != -1) { + cout << " (pid: " << service_pid << ")"; } - // Issue UNPIN command. - { - int r; - - { - char *buf = new char[1 + sizeof(handle)]; - unique_ptr ubuf(buf); - buf[0] = DINIT_CP_UNPINSERVICE; - memcpy(buf + 1, &handle, sizeof(handle)); - r = write_all(socknum, buf, 2 + sizeof(handle)); - } - - if (r == -1) { - perror("dinitctl: write"); - return 1; + if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) { + if (WIFEXITED(exit_status)) { + cout << " (exit status: " << WEXITSTATUS(exit_status) << ")"; } - - wait_for_reply(rbuffer, socknum); - if (rbuffer[0] != DINIT_RP_ACK) { - cerr << "dinitctl: Protocol error." << endl; - return 1; + else if (WIFSIGNALED(exit_status)) { + cout << " (signal: " << WTERMSIG(exit_status) << ")"; } - rbuffer.consume(1); } + + if (has_console) { + cout << " (has console)"; + } + else if (waiting_console) { + cout << " (waiting for console)"; + } + + cout << endl; + + rbuffer.consume(hdrsize + nameLen); + wait_for_reply(rbuffer, socknum); } - catch (ReadCPException &exc) { - cerr << "dinitctl: Control socket read failure or protocol error" << endl; + + if (rbuffer[0] != DINIT_RP_LISTDONE) { + cerr << "dinitctl: Control socket protocol error" << endl; return 1; } - catch (std::bad_alloc &exc) { - cerr << "dinitctl: Out of memory" << endl; + + return 0; +} + +static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add, + const char *service_from, const char *service_to, dependency_type dep_type) +{ + using namespace std; + + handle_t from_handle; + handle_t to_handle; + + if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr) + || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) { return 1; } - - if (verbose) { - cout << "Service unpinned." << endl; + + auto m = membuf() + .append(add ? (char)DINIT_CP_ADD_DEP : (char)DINIT_CP_REM_DEP) + .append(dep_type) + .append(from_handle) + .append(to_handle); + write_all_x(socknum, m); + + wait_for_reply(rbuffer, socknum); + + // check reply + if (rbuffer[0] == DINIT_RP_NAK) { + cerr << "dinitctl: Could not add dependency: circular dependency or wrong state" << endl; + return 1; } + if (rbuffer[0] != DINIT_RP_ACK) { + cerr << "dinitctl: Control socket protocol error" << endl; + return 1; + } + return 0; } -static int listServices(int socknum) +static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer) { + // TODO support no-wait option. using namespace std; - + + auto m = membuf() + .append(DINIT_CP_SHUTDOWN) + .append(static_cast(shutdown_type_t::HALT)); + write_all_x(socknum, m); + + wait_for_reply(rbuffer, socknum); + + if (rbuffer[0] != DINIT_RP_ACK) { + cerr << "dinitctl: Control socket protocol error" << endl; + return 1; + } + + // Now wait for rollback complete, by waiting for the connection to close: try { - char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES }; - int r = write_all(socknum, cmdbuf, 1); - - if (r == -1) { - perror("dinitctl: write"); - return 1; + while (true) { + wait_for_info(rbuffer, socknum); + rbuffer.consume(rbuffer[1]); } - - cpbuffer<1024> rbuffer; - wait_for_reply(rbuffer, socknum); - while (rbuffer[0] == DINIT_RP_SVCINFO) { - fillBufferTo(&rbuffer, socknum, 8); - int nameLen = rbuffer[1]; - service_state_t current = static_cast(rbuffer[2]); - service_state_t target = static_cast(rbuffer[3]); - - fillBufferTo(&rbuffer, socknum, nameLen + 8); - - char *name_ptr = rbuffer.get_ptr(8); - int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen); - - string name = string(name_ptr, clength); - name.append(rbuffer.get_buf_base(), nameLen - clength); - - cout << "["; - - cout << (target == service_state_t::STARTED ? "{" : " "); - cout << (current == service_state_t::STARTED ? "+" : " "); - cout << (target == service_state_t::STARTED ? "}" : " "); - - if (current == service_state_t::STARTING) { - cout << "<<"; - } - else if (current == service_state_t::STOPPING) { - cout << ">>"; + } + catch (cp_read_exception &exc) { + // Assume that the connection closed. + } + + return 0; +} + +// exception for cancelling a service operation +class service_op_cancel { }; + +static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to, + bool enable) +{ + using namespace std; + + service_state_t from_state = service_state_t::STARTED; + handle_t from_handle; + + handle_t to_handle; + + if (! load_service(socknum, rbuffer, from, &from_handle, &from_state) + || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) { + return 1; + } + + // Get service load path + char buf[1] = { DINIT_CP_QUERY_LOAD_MECH }; + write_all_x(socknum, buf, 1); + + wait_for_reply(rbuffer, socknum); + + if (rbuffer[0] != DINIT_RP_LOADER_MECH) { + cerr << "dinitctl: Control socket protocol error" << endl; + return 1; + } + + // Packet type, load mechanism type, packet size: + fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t)); + + if (rbuffer[1] != SSET_TYPE_DIRLOAD) { + cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl; + return 1; + } + + vector paths; + + uint32_t pktsize; + rbuffer.extract(&pktsize, 2, sizeof(uint32_t)); + + fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length + + uint32_t path_entries; // number of service directories + rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t)); + + uint32_t cwd_len; + rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t)); + rbuffer.consume(2 + sizeof(uint32_t) * 3); + pktsize -= 2 + sizeof(uint32_t) * 3; + + // Read current working directory of daemon: + std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len); + + // dinit daemon base directory against which service paths are resolved is in dinit_cwd + + for (int i = 0; i < (int)path_entries; i++) { + uint32_t plen; + fill_buffer_to(rbuffer, socknum, sizeof(uint32_t)); + rbuffer.extract(&plen, 0, sizeof(uint32_t)); + rbuffer.consume(sizeof(uint32_t)); + paths.push_back(read_string(socknum, rbuffer, plen)); + } + + // all service directories are now in the 'paths' vector + // Load/read service description for 'from' service: + + ifstream service_file; + string service_file_path; + + for (std::string path : paths) { + string test_path = combine_paths(combine_paths(dinit_cwd, path.c_str()), from); + + service_file.open(test_path.c_str(), ios::in); + if (service_file) { + service_file_path = test_path; + break; + } + } + + if (! service_file) { + cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl; + return 1; + } + + // We now need to read the service file, identify the waits-for.d directory (bail out if more than one), + // make sure the service is not listed as a dependency individually. + + string waits_for_d; + + try { + process_service_file(from, service_file, [&](string &line, string &setting, + dinit_load::string_iterator i, dinit_load::string_iterator end) -> void { + if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") { + string dname = dinit_load::read_setting_value(i, end); + if (dname == to) { + // There is already a dependency + cerr << "dinitctl: there is a fixed dependency to service '" << to + << "' in the service description of '" << from << "'." << endl; + throw service_op_cancel(); + } } - else { - cout << " "; - } - - cout << (target == service_state_t::STOPPED ? "{" : " "); - cout << (current == service_state_t::STOPPED ? "-" : " "); - cout << (target == service_state_t::STOPPED ? "}" : " "); - - cout << "] " << name << endl; - - rbuffer.consume(8 + nameLen); - wait_for_reply(rbuffer, socknum); + else if (setting == "waits-for.d") { + string dname = dinit_load::read_setting_value(i, end); + if (! waits_for_d.empty()) { + cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories " + << "specified in service description" << endl; + throw service_op_cancel(); + } + waits_for_d = std::move(dname); + } + }); + } + catch (const service_op_cancel &cexc) { + return 1; + } + + // If the from service has no waits-for.d specified, we can't continue + if (waits_for_d.empty()) { + cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl; + return 1; + } + + // The waits-for.d path is relative to the service file path, combine: + string waits_for_d_full = combine_paths(parent_path(service_file_path), waits_for_d.c_str()); + + // check if dependency already exists + string dep_link_path = combine_paths(waits_for_d_full, to); + struct stat stat_buf; + if (lstat(dep_link_path.c_str(), &stat_buf) == -1) { + if (errno != ENOENT) { + cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": " + << strerror(errno) << endl; + return 1; } - - if (rbuffer[0] != DINIT_RP_LISTDONE) { - cerr << "dinitctl: Control socket protocol error" << endl; + } + else { + // dependency already exists + if (enable) { + cerr << "dinitctl: service already enabled." << endl; return 1; } } - catch (ReadCPException &exc) { - cerr << "dinitctl: Control socket read failure or protocol error" << endl; + + // warn if 'from' service is not started + if (enable && from_state != service_state_t::STARTED) { + cerr << "dinitctl: warning: enabling dependency for non-started service" << endl; + } + + // add/remove dependency + constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2; + char cmdbuf[enable_pktsize] = { char(enable ? DINIT_CP_ENABLESERVICE : DINIT_CP_REM_DEP), + char(dependency_type::WAITS_FOR)}; + memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle)); + memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle)); + write_all_x(socknum, cmdbuf, enable_pktsize); + + wait_for_reply(rbuffer, socknum); + + // check reply + if (enable && rbuffer[0] == DINIT_RP_NAK) { + cerr << "dinitctl: Could not enable service: possible circular dependency" << endl; return 1; } - catch (std::bad_alloc &exc) { - cerr << "dinitctl: Out of memory" << endl; + if (rbuffer[0] != DINIT_RP_ACK) { + cerr << "dinitctl: Control socket protocol error" << endl; return 1; } - + + // create link + if (enable) { + if (symlink((string("../") + to).c_str(), dep_link_path.c_str()) == -1) { + cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno) + << "\n" "dinitctl: Note: service was activated, but will not be enabled on restart." + << endl; + return 1; + } + } + else { + if (unlink(dep_link_path.c_str()) == -1) { + cerr << "dinitctl: Could not unlink dependency entry " << dep_link_path << ": " + << strerror(errno) << "\n" + "dinitctl: Note: service was disabled, but will be re-enabled on restart." << endl; + return 1; + } + } + return 0; }