7 #include <system_error>
11 #include <sys/types.h>
14 #include <sys/socket.h>
20 #include "control-cmds.h"
21 #include "service-constants.h"
23 #include "dinit-client.h"
24 #include "load-service.h"
25 #include "dinit-util.h"
28 // dinitctl: utility to control the Dinit daemon, including starting and stopping of services.
30 // This utility communicates with the dinit daemon via a unix stream socket (as specified in
31 // SYSCONTROLSOCKET, or $HOME/.dinitctl).
33 static constexpr uint16_t min_cp_version = 1;
34 static constexpr uint16_t max_cp_version = 1;
38 static int issue_load_service(int socknum, const char *service_name, bool find_only = false);
39 static int check_load_reply(int socknum, cpbuffer_t &, handle_t *handle_p, service_state_t *state_p);
40 static int start_stop_service(int socknum, cpbuffer_t &, const char *service_name, command_t command,
41 bool do_pin, bool do_force, bool wait_for_service, bool verbose);
42 static int unpin_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose);
43 static int unload_service(int socknum, cpbuffer_t &, const char *service_name);
44 static int list_services(int socknum, cpbuffer_t &);
45 static int shutdown_dinit(int soclknum, cpbuffer_t &);
46 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add, const char *service_from,
47 const char *service_to, dependency_type dep_type);
48 static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to,
51 static const char * describeState(bool stopped)
53 return stopped ? "stopped" : "started";
56 static const char * describeVerb(bool stop)
58 return stop ? "stop" : "start";
61 enum class command_t {
80 int main(int argc, char **argv)
84 bool show_help = argc < 2;
85 const char *service_name = nullptr;
86 const char *to_service_name = nullptr;
87 dependency_type dep_type;
88 bool dep_type_set = false;
90 std::string control_socket_str;
91 const char * control_socket_path = nullptr;
94 bool sys_dinit = false; // communicate with system daemon
95 bool wait_for_service = true;
97 bool do_force = false;
99 command_t command = command_t::NONE;
101 for (int i = 1; i < argc; i++) {
102 if (argv[i][0] == '-') {
103 if (strcmp(argv[i], "--help") == 0) {
107 else if (strcmp(argv[i], "--no-wait") == 0) {
108 wait_for_service = false;
110 else if (strcmp(argv[i], "--quiet") == 0) {
113 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
116 else if (strcmp(argv[i], "--pin") == 0) {
119 else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) {
122 cerr << "dinitctl: --socket-path/-p should be followed by socket path" << std::endl;
125 control_socket_str = argv[i];
127 else if ((command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE)
128 && strcmp(argv[i], "--from") == 0) {
131 cerr << "dinitctl: --from should be followed by a service name" << std::endl;
134 service_name = argv[i];
136 else if ((command == command_t::STOP_SERVICE || command == command_t::RESTART_SERVICE)
137 && (strcmp(argv[i], "--force") == 0 || strcmp(argv[i], "-f") == 0)) {
141 cerr << "dinitctl: unrecognized/invalid option: " << argv[i] << " (use --help for help)\n";
145 else if (command == command_t::NONE) {
146 if (strcmp(argv[i], "start") == 0) {
147 command = command_t::START_SERVICE;
149 else if (strcmp(argv[i], "wake") == 0) {
150 command = command_t::WAKE_SERVICE;
152 else if (strcmp(argv[i], "stop") == 0) {
153 command = command_t::STOP_SERVICE;
155 else if (strcmp(argv[i], "restart") == 0) {
156 command = command_t::RESTART_SERVICE;
158 else if (strcmp(argv[i], "release") == 0) {
159 command = command_t::RELEASE_SERVICE;
161 else if (strcmp(argv[i], "unpin") == 0) {
162 command = command_t::UNPIN_SERVICE;
164 else if (strcmp(argv[i], "unload") == 0) {
165 command = command_t::UNLOAD_SERVICE;
167 else if (strcmp(argv[i], "list") == 0) {
168 command = command_t::LIST_SERVICES;
170 else if (strcmp(argv[i], "shutdown") == 0) {
171 command = command_t::SHUTDOWN;
173 else if (strcmp(argv[i], "add-dep") == 0) {
174 command = command_t::ADD_DEPENDENCY;
176 else if (strcmp(argv[i], "rm-dep") == 0) {
177 command = command_t::RM_DEPENDENCY;
179 else if (strcmp(argv[i], "enable") == 0) {
180 command = command_t::ENABLE_SERVICE;
182 else if (strcmp(argv[i], "disable") == 0) {
183 command = command_t::DISABLE_SERVICE;
186 cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n";
191 // service name / other non-option
192 if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
193 if (! dep_type_set) {
194 if (strcmp(argv[i], "regular") == 0) {
195 dep_type = dependency_type::REGULAR;
197 else if (strcmp(argv[i], "milestone") == 0) {
198 dep_type = dependency_type::MILESTONE;
200 else if (strcmp(argv[i], "waits-for") == 0) {
201 dep_type = dependency_type::WAITS_FOR;
209 else if (service_name == nullptr) {
210 service_name = argv[i];
212 else if (to_service_name == nullptr) {
213 to_service_name = argv[i];
220 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
221 if (to_service_name != nullptr) {
225 to_service_name = argv[i];
228 if (service_name != nullptr) {
232 service_name = argv[i];
233 // TODO support multiple services
238 bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN);
240 if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
241 show_help |= (to_service_name == nullptr);
243 else if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) {
247 if (service_name != nullptr && no_service_cmd) {
251 if ((command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY)
252 && (! dep_type_set || service_name == nullptr || to_service_name == nullptr)) {
257 cout << "dinitctl: control Dinit services\n"
260 " dinitctl [options] start [options] <service-name>\n"
261 " dinitctl [options] stop [options] <service-name>\n"
262 " dinitctl [options] wake [options] <service-name>\n"
263 " dinitctl [options] release [options] <service-name>\n"
264 " dinitctl [options] unpin <service-name>\n"
265 " dinitctl [options] unload <service-name>\n"
266 " dinitctl [options] list\n"
267 " dinitctl [options] shutdown\n"
268 " dinitctl [options] add-dep <type> <from-service> <to-service>\n"
269 " dinitctl [options] rm-dep <type> <from-service> <to-service>\n"
270 " dinitctl [options] enable [--from <from-service>] <to-service>\n"
271 " dinitctl [options] disable [--from <from-service>] <to-service>\n"
273 "Note: An activated service continues running when its dependents stop.\n"
276 " --help : show this help\n"
277 " -s, --system : control system daemon instead of user daemon\n"
278 " --quiet : suppress output (except errors)\n"
279 " --socket-path <path>, -p <path>\n"
280 " : specify socket for communication with daemon\n"
283 " --no-wait : don't wait for service startup/shutdown to complete\n"
284 " --pin : pin the service in the requested state\n"
285 " --force : force stop even if dependents will be affected\n";
289 signal(SIGPIPE, SIG_IGN);
291 // Locate control socket
292 if (! control_socket_str.empty()) {
293 control_socket_path = control_socket_str.c_str();
296 control_socket_path = SYSCONTROLSOCKET; // default to system
298 char * userhome = getenv("HOME");
299 if (userhome == nullptr) {
300 struct passwd * pwuid_p = getpwuid(getuid());
301 if (pwuid_p != nullptr) {
302 userhome = pwuid_p->pw_dir;
306 if (userhome != nullptr) {
307 control_socket_str = userhome;
308 control_socket_str += "/.dinitctl";
309 control_socket_path = control_socket_str.c_str();
312 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
318 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
320 perror("dinitctl: socket");
324 struct sockaddr_un * name;
325 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
326 name = (struct sockaddr_un *) malloc(sockaddr_size);
327 if (name == nullptr) {
328 cerr << "dinitctl: Out of memory" << endl;
332 name->sun_family = AF_UNIX;
333 strcpy(name->sun_path, control_socket_path);
335 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
337 perror("dinitctl: connect");
342 // Start by querying protocol version:
344 check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum);
346 if (command == command_t::UNPIN_SERVICE) {
347 return unpin_service(socknum, rbuffer, service_name, verbose);
349 else if (command == command_t::UNLOAD_SERVICE) {
350 return unload_service(socknum, rbuffer, service_name);
352 else if (command == command_t::LIST_SERVICES) {
353 return list_services(socknum, rbuffer);
355 else if (command == command_t::SHUTDOWN) {
356 return shutdown_dinit(socknum, rbuffer);
358 else if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
359 return add_remove_dependency(socknum, rbuffer, command == command_t::ADD_DEPENDENCY,
360 service_name, to_service_name, dep_type);
362 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
363 // If only one service specified, assume that we enable for 'boot' service:
364 if (service_name == nullptr) {
365 service_name = "boot";
367 return enable_disable_service(socknum, rbuffer, service_name, to_service_name,
368 command == command_t::ENABLE_SERVICE);
371 return start_stop_service(socknum, rbuffer, service_name, command, do_pin, do_force,
372 wait_for_service, verbose);
375 catch (cp_old_client_exception &e) {
376 std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl;
379 catch (cp_old_server_exception &e) {
380 std::cerr << "dinitctl: server too old or protocol error" << std::endl;
383 catch (cp_read_exception &e) {
384 cerr << "dinitctl: control socket read failure or protocol error" << endl;
387 catch (cp_write_exception &e) {
388 cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl;
393 // Extract/read a string of specified length from the buffer/socket. The string is consumed
395 static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length)
397 int rb_len = rbuffer.get_length();
398 if (uint32_t(rb_len) >= length) {
399 std::string r = rbuffer.extract_string(0, length);
400 rbuffer.consume(length);
404 std::string r = rbuffer.extract_string(0, rb_len);
405 uint32_t rlen = length - rb_len;
409 rbuffer.fill(socknum);
410 char *bptr = rbuffer.get_ptr(0);
411 clen = rbuffer.get_length();
412 clen = std::min(clen, rlen);
413 r.append(bptr, clen);
417 rbuffer.consume(clen);
422 // Load a service: issue load command, wait for reply. Return true on success, display error message
423 // and return false on failure.
424 // socknum - the socket fd to communicate via
425 // rbuffer - the buffer for communication
426 // name - the name of the service to load
427 // handle - where to store the handle of the loaded service
428 // state - where to store the state of the loaded service (may be null).
429 static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle,
430 service_state_t *state)
432 // Load 'to' service:
433 if (issue_load_service(socknum, name)) {
437 wait_for_reply(rbuffer, socknum);
439 if (check_load_reply(socknum, rbuffer, handle, state) != 0) {
446 // Get the service name for a given handle, by querying the daemon.
447 static std::string get_service_name(int socknum, cpbuffer_t &rbuffer, handle_t handle)
450 .append((char) DINIT_CP_QUERYSERVICENAME)
453 write_all_x(socknum, m);
455 wait_for_reply(rbuffer, socknum);
457 if (rbuffer[0] != DINIT_RP_SERVICENAME) {
458 throw cp_read_exception{0};
463 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint16_t));
465 rbuffer.extract(&namesize, 2, sizeof(uint16_t));
466 rbuffer.consume(2 + sizeof(uint16_t));
471 if (rbuffer.get_length() == 0) {
472 rbuffer.fill(socknum);
475 size_t to_extract = std::min(size_t(rbuffer.get_length()), namesize - name.length());
476 size_t contiguous_len = rbuffer.get_contiguous_length(rbuffer.get_ptr(0));
477 if (contiguous_len <= to_extract) {
478 name.append(rbuffer.get_ptr(0), contiguous_len);
479 rbuffer.consume(contiguous_len);
480 name.append(rbuffer.get_ptr(0), to_extract - contiguous_len);
481 rbuffer.consume(to_extract - contiguous_len);
484 name.append(rbuffer.get_ptr(0), to_extract);
485 rbuffer.consume(to_extract);
489 } while (name.length() < namesize);
494 // Start/stop a service
495 static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
496 command_t command, bool do_pin, bool do_force, bool wait_for_service, bool verbose)
500 bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE);
502 service_state_t state;
505 if (! load_service(socknum, rbuffer, service_name, &handle, &state)) {
509 service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
512 case command_t::STOP_SERVICE:
513 case command_t::RESTART_SERVICE: // stop, and then start
514 pcommand = DINIT_CP_STOPSERVICE;
516 case command_t::RELEASE_SERVICE:
517 pcommand = DINIT_CP_RELEASESERVICE;
519 case command_t::START_SERVICE:
520 pcommand = DINIT_CP_STARTSERVICE;
522 case command_t::WAKE_SERVICE:
523 pcommand = DINIT_CP_WAKESERVICE;
528 // Need to issue STOPSERVICE/STARTSERVICE
529 // We'll do this regardless of the current service state / target state, since issuing
530 // start/stop also sets or clears the "explicitly started" flag on the service.
532 char flags = (do_pin ? 1 : 0) | ((pcommand == DINIT_CP_STOPSERVICE && !do_force) ? 2 : 0);
533 if (command == command_t::RESTART_SERVICE) {
538 .append((char) pcommand)
541 write_all_x(socknum, m);
543 wait_for_reply(rbuffer, socknum);
544 auto reply_pkt_h = rbuffer[0];
545 rbuffer.consume(1); // consume header
546 if (reply_pkt_h == DINIT_RP_ALREADYSS) {
547 bool already = (state == wanted_state);
549 cout << "Service " << (already ? "(already) " : "")
550 << describeState(do_stop) << "." << endl;
552 return 0; // success!
554 if (reply_pkt_h == DINIT_RP_DEPENDENTS && pcommand == DINIT_CP_STOPSERVICE) {
555 cerr << "dinitctl: Cannot stop service due to the following dependents:\n"
556 "(Only direct dependents are listed. Exercise caution before using '--force' !!)\n";
557 // size_t number, N * handle_t handles
559 rbuffer.fill_to(socknum, sizeof(number));
560 rbuffer.extract(&number, 0, sizeof(number));
561 rbuffer.consume(sizeof(number));
562 std::vector<handle_t> handles;
563 handles.reserve(number);
564 for (size_t i = 0; i < number; i++) {
566 rbuffer.fill_to(socknum, sizeof(handle_t));
567 rbuffer.extract(&handle, 0, sizeof(handle));
568 handles.push_back(handle);
569 rbuffer.consume(sizeof(handle));
571 // Print the directly affected dependents:
573 for (handle_t handle : handles) {
574 cerr << " " << get_service_name(socknum, rbuffer, handle);
579 if (reply_pkt_h != DINIT_RP_ACK && reply_pkt_h != DINIT_RP_ALREADYSS) {
580 cerr << "dinitctl: protocol error." << endl;
585 if (! wait_for_service) {
587 cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl;
592 service_event_t completionEvent;
593 service_event_t cancelledEvent;
596 completionEvent = service_event_t::STOPPED;
597 cancelledEvent = service_event_t::STOPCANCELLED;
600 completionEvent = service_event_t::STARTED;
601 cancelledEvent = service_event_t::STARTCANCELLED;
604 // Wait until service started:
605 int r = rbuffer.fill_to(socknum, 2);
607 if (rbuffer[0] >= 100) {
608 int pktlen = (unsigned char) rbuffer[1];
609 fill_buffer_to(rbuffer, socknum, pktlen);
611 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
613 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
614 service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
615 if (ev_handle == handle) {
616 if (event == completionEvent) {
618 cout << "Service " << describeState(do_stop) << "." << endl;
622 else if (event == cancelledEvent) {
624 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
628 else if (! do_stop && event == service_event_t::FAILEDSTART) {
630 cout << "Service failed to start." << endl;
637 rbuffer.consume(pktlen);
638 r = rbuffer.fill_to(socknum, 2);
641 // Not an information packet?
642 cerr << "dinitctl: protocol error" << endl;
648 perror("dinitctl: read");
651 cerr << "protocol error (connection closed by server)" << endl;
656 // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for
657 // a response. Returns 1 on failure (with error logged), 0 on success.
658 static int issue_load_service(int socknum, const char *service_name, bool find_only)
661 uint16_t sname_len = strlen(service_name);
662 int bufsize = 3 + sname_len;
664 std::unique_ptr<char[]> ubuf(new char[bufsize]);
665 auto buf = ubuf.get();
667 buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE;
668 memcpy(buf + 1, &sname_len, 2);
669 memcpy(buf + 3, service_name, sname_len);
671 write_all_x(socknum, buf, bufsize);
676 // Check that a "load service" reply was received, and that the requested service was found.
677 // state_p may be null.
678 static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p)
682 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
683 fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
684 rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
685 if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
686 //target_state = static_cast<service_state_t>(rbuffer[2 + sizeof(handle)]);
687 rbuffer.consume(3 + sizeof(*handle_p));
690 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
691 cerr << "dinitctl: failed to find/load service." << endl;
695 cerr << "dinitctl: protocol error." << endl;
700 static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose)
707 if (! load_service(socknum, rbuffer, service_name, &handle, nullptr)) {
711 // Issue UNPIN command.
714 .append<char>(DINIT_CP_UNPINSERVICE)
716 write_all_x(socknum, m);
718 wait_for_reply(rbuffer, socknum);
719 if (rbuffer[0] != DINIT_RP_ACK) {
720 cerr << "dinitctl: protocol error." << endl;
727 cout << "Service unpinned." << endl;
732 static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name)
736 if (issue_load_service(socknum, service_name, true) == 1) {
740 wait_for_reply(rbuffer, socknum);
744 if (rbuffer[0] == DINIT_RP_NOSERVICE) {
745 cerr << "dinitctl: service not loaded." << endl;
749 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
753 // Issue UNLOAD command.
756 .append<char>(DINIT_CP_UNLOADSERVICE)
758 write_all_x(socknum, m);
760 wait_for_reply(rbuffer, socknum);
761 if (rbuffer[0] == DINIT_RP_NAK) {
762 cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of "
763 "other service." << endl;
766 if (rbuffer[0] != DINIT_RP_ACK) {
767 cerr << "dinitctl: Protocol error." << endl;
773 cout << "Service unloaded." << endl;
777 static int list_services(int socknum, cpbuffer_t &rbuffer)
781 char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES };
782 write_all_x(socknum, cmdbuf, 1);
784 wait_for_reply(rbuffer, socknum);
785 while (rbuffer[0] == DINIT_RP_SVCINFO) {
786 int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
787 fill_buffer_to(rbuffer, socknum, hdrsize);
788 int nameLen = rbuffer[1];
789 service_state_t current = static_cast<service_state_t>(rbuffer[2]);
790 service_state_t target = static_cast<service_state_t>(rbuffer[3]);
792 int console_flags = rbuffer[4];
793 bool has_console = (console_flags & 2) != 0;
794 bool waiting_console = (console_flags & 1) != 0;
795 bool was_skipped = (console_flags & 4) != 0;
797 stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[5]);
801 if (current != service_state_t::STOPPED) {
802 rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid));
805 rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
808 fill_buffer_to(rbuffer, socknum, nameLen + hdrsize);
810 char *name_ptr = rbuffer.get_ptr(hdrsize);
811 int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
813 string name = string(name_ptr, clength);
814 name.append(rbuffer.get_buf_base(), nameLen - clength);
818 cout << (target == service_state_t::STARTED ? "{" : " ");
819 if (current == service_state_t::STARTED) {
820 cout << (was_skipped ? "s" : "+");
825 cout << (target == service_state_t::STARTED ? "}" : " ");
827 if (current == service_state_t::STARTING) {
830 else if (current == service_state_t::STOPPING) {
837 cout << (target == service_state_t::STOPPED ? "{" : " ");
838 if (current == service_state_t::STOPPED) {
839 bool did_fail = false;
840 if (stop_reason == stopped_reason_t::TERMINATED) {
841 if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) {
845 else did_fail = (stop_reason != stopped_reason_t::NORMAL);
847 cout << (did_fail ? "X" : "-");
852 cout << (target == service_state_t::STOPPED ? "}" : " ");
854 cout << "] " << name;
856 if (current != service_state_t::STOPPED && service_pid != -1) {
857 cout << " (pid: " << service_pid << ")";
860 if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) {
861 if (WIFEXITED(exit_status)) {
862 cout << " (exit status: " << WEXITSTATUS(exit_status) << ")";
864 else if (WIFSIGNALED(exit_status)) {
865 cout << " (signal: " << WTERMSIG(exit_status) << ")";
870 cout << " (has console)";
872 else if (waiting_console) {
873 cout << " (waiting for console)";
878 rbuffer.consume(hdrsize + nameLen);
879 wait_for_reply(rbuffer, socknum);
882 if (rbuffer[0] != DINIT_RP_LISTDONE) {
883 cerr << "dinitctl: Control socket protocol error" << endl;
890 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
891 const char *service_from, const char *service_to, dependency_type dep_type)
895 handle_t from_handle;
898 if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr)
899 || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) {
904 .append<char>(add ? (char)DINIT_CP_ADD_DEP : (char)DINIT_CP_REM_DEP)
908 write_all_x(socknum, m);
910 wait_for_reply(rbuffer, socknum);
913 if (rbuffer[0] == DINIT_RP_NAK) {
914 cerr << "dinitctl: Could not add dependency: circular dependency or wrong state" << endl;
917 if (rbuffer[0] != DINIT_RP_ACK) {
918 cerr << "dinitctl: Control socket protocol error" << endl;
925 static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer)
927 // TODO support no-wait option.
931 .append<char>(DINIT_CP_SHUTDOWN)
932 .append(static_cast<char>(shutdown_type_t::HALT));
933 write_all_x(socknum, m);
935 wait_for_reply(rbuffer, socknum);
937 if (rbuffer[0] != DINIT_RP_ACK) {
938 cerr << "dinitctl: Control socket protocol error" << endl;
942 // Now wait for rollback complete, by waiting for the connection to close:
945 wait_for_info(rbuffer, socknum);
946 rbuffer.consume(rbuffer[1]);
949 catch (cp_read_exception &exc) {
950 // Assume that the connection closed.
956 // exception for cancelling a service operation
957 class service_op_cancel { };
959 static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to,
964 service_state_t from_state = service_state_t::STARTED;
965 handle_t from_handle;
969 if (! load_service(socknum, rbuffer, from, &from_handle, &from_state)
970 || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) {
974 // Get service load path
975 char buf[1] = { DINIT_CP_QUERY_LOAD_MECH };
976 write_all_x(socknum, buf, 1);
978 wait_for_reply(rbuffer, socknum);
980 if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
981 cerr << "dinitctl: Control socket protocol error" << endl;
985 // Packet type, load mechanism type, packet size:
986 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t));
988 if (rbuffer[1] != SSET_TYPE_DIRLOAD) {
989 cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl;
993 vector<string> paths;
996 rbuffer.extract(&pktsize, 2, sizeof(uint32_t));
998 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length
1000 uint32_t path_entries; // number of service directories
1001 rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t));
1004 rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t));
1005 rbuffer.consume(2 + sizeof(uint32_t) * 3);
1006 pktsize -= 2 + sizeof(uint32_t) * 3;
1008 // Read current working directory of daemon:
1009 std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len);
1011 // dinit daemon base directory against which service paths are resolved is in dinit_cwd
1013 for (int i = 0; i < (int)path_entries; i++) {
1015 fill_buffer_to(rbuffer, socknum, sizeof(uint32_t));
1016 rbuffer.extract(&plen, 0, sizeof(uint32_t));
1017 rbuffer.consume(sizeof(uint32_t));
1018 paths.push_back(read_string(socknum, rbuffer, plen));
1021 // all service directories are now in the 'paths' vector
1022 // Load/read service description for 'from' service:
1024 ifstream service_file;
1025 string service_file_path;
1027 for (std::string path : paths) {
1028 string test_path = combine_paths(combine_paths(dinit_cwd, path.c_str()), from);
1030 service_file.open(test_path.c_str(), ios::in);
1032 service_file_path = test_path;
1037 if (! service_file) {
1038 cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl;
1042 // We now need to read the service file, identify the waits-for.d directory (bail out if more than one),
1043 // make sure the service is not listed as a dependency individually.
1048 process_service_file(from, service_file, [&](string &line, string &setting,
1049 dinit_load::string_iterator i, dinit_load::string_iterator end) -> void {
1050 if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") {
1051 string dname = dinit_load::read_setting_value(i, end);
1053 // There is already a dependency
1054 cerr << "dinitctl: there is a fixed dependency to service '" << to
1055 << "' in the service description of '" << from << "'." << endl;
1056 throw service_op_cancel();
1059 else if (setting == "waits-for.d") {
1060 string dname = dinit_load::read_setting_value(i, end);
1061 if (! waits_for_d.empty()) {
1062 cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories "
1063 << "specified in service description" << endl;
1064 throw service_op_cancel();
1066 waits_for_d = std::move(dname);
1070 catch (const service_op_cancel &cexc) {
1074 // If the from service has no waits-for.d specified, we can't continue
1075 if (waits_for_d.empty()) {
1076 cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl;
1080 // The waits-for.d path is relative to the service file path, combine:
1081 string waits_for_d_full = combine_paths(parent_path(service_file_path), waits_for_d.c_str());
1083 // check if dependency already exists
1084 string dep_link_path = combine_paths(waits_for_d_full, to);
1085 struct stat stat_buf;
1086 if (lstat(dep_link_path.c_str(), &stat_buf) == -1) {
1087 if (errno != ENOENT) {
1088 cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": "
1089 << strerror(errno) << endl;
1094 // dependency already exists
1096 cerr << "dinitctl: service already enabled." << endl;
1101 // warn if 'from' service is not started
1102 if (enable && from_state != service_state_t::STARTED) {
1103 cerr << "dinitctl: warning: enabling dependency for non-started service" << endl;
1106 // add/remove dependency
1107 constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2;
1108 char cmdbuf[enable_pktsize] = { char(enable ? DINIT_CP_ENABLESERVICE : DINIT_CP_REM_DEP),
1109 char(dependency_type::WAITS_FOR)};
1110 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
1111 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
1112 write_all_x(socknum, cmdbuf, enable_pktsize);
1114 wait_for_reply(rbuffer, socknum);
1117 if (enable && rbuffer[0] == DINIT_RP_NAK) {
1118 cerr << "dinitctl: Could not enable service: possible circular dependency" << endl;
1121 if (rbuffer[0] != DINIT_RP_ACK) {
1122 cerr << "dinitctl: Control socket protocol error" << endl;
1128 if (symlink((string("../") + to).c_str(), dep_link_path.c_str()) == -1) {
1129 cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno)
1130 << "\n" "dinitctl: Note: service was activated, but will not be enabled on restart."
1136 if (unlink(dep_link_path.c_str()) == -1) {
1137 cerr << "dinitctl: Could not unlink dependency entry " << dep_link_path << ": "
1138 << strerror(errno) << "\n"
1139 "dinitctl: Note: service was disabled, but will be re-enabled on restart." << endl;