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 {
79 int main(int argc, char **argv)
83 bool show_help = argc < 2;
84 const char *service_name = nullptr;
85 const char *to_service_name = nullptr;
86 dependency_type dep_type;
87 bool dep_type_set = false;
89 std::string control_socket_str;
90 const char * control_socket_path = nullptr;
93 bool sys_dinit = false; // communicate with system daemon
94 bool wait_for_service = true;
96 bool do_force = false;
98 command_t command = command_t::NONE;
100 for (int i = 1; i < argc; i++) {
101 if (argv[i][0] == '-') {
102 if (strcmp(argv[i], "--help") == 0) {
106 else if (strcmp(argv[i], "--no-wait") == 0) {
107 wait_for_service = false;
109 else if (strcmp(argv[i], "--quiet") == 0) {
112 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
115 else if (strcmp(argv[i], "--pin") == 0) {
118 else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) {
121 cerr << "dinitctl: --socket-path/-p should be followed by socket path" << std::endl;
124 control_socket_str = argv[i];
126 else if ((command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE)
127 && strcmp(argv[i], "--from") == 0) {
130 cerr << "dinitctl: --from should be followed by a service name" << std::endl;
133 service_name = argv[i];
135 else if ((command == command_t::STOP_SERVICE)
136 && (strcmp(argv[i], "--force") == 0 || strcmp(argv[i], "-f") == 0)) {
140 cerr << "dinitctl: unrecognized/invalid option: " << argv[i] << " (use --help for help)\n";
144 else if (command == command_t::NONE) {
145 if (strcmp(argv[i], "start") == 0) {
146 command = command_t::START_SERVICE;
148 else if (strcmp(argv[i], "wake") == 0) {
149 command = command_t::WAKE_SERVICE;
151 else if (strcmp(argv[i], "stop") == 0) {
152 command = command_t::STOP_SERVICE;
154 else if (strcmp(argv[i], "release") == 0) {
155 command = command_t::RELEASE_SERVICE;
157 else if (strcmp(argv[i], "unpin") == 0) {
158 command = command_t::UNPIN_SERVICE;
160 else if (strcmp(argv[i], "unload") == 0) {
161 command = command_t::UNLOAD_SERVICE;
163 else if (strcmp(argv[i], "list") == 0) {
164 command = command_t::LIST_SERVICES;
166 else if (strcmp(argv[i], "shutdown") == 0) {
167 command = command_t::SHUTDOWN;
169 else if (strcmp(argv[i], "add-dep") == 0) {
170 command = command_t::ADD_DEPENDENCY;
172 else if (strcmp(argv[i], "rm-dep") == 0) {
173 command = command_t::RM_DEPENDENCY;
175 else if (strcmp(argv[i], "enable") == 0) {
176 command = command_t::ENABLE_SERVICE;
178 else if (strcmp(argv[i], "disable") == 0) {
179 command = command_t::DISABLE_SERVICE;
182 cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n";
187 // service name / other non-option
188 if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
189 if (! dep_type_set) {
190 if (strcmp(argv[i], "regular") == 0) {
191 dep_type = dependency_type::REGULAR;
193 else if (strcmp(argv[i], "milestone") == 0) {
194 dep_type = dependency_type::MILESTONE;
196 else if (strcmp(argv[i], "waits-for") == 0) {
197 dep_type = dependency_type::WAITS_FOR;
205 else if (service_name == nullptr) {
206 service_name = argv[i];
208 else if (to_service_name == nullptr) {
209 to_service_name = argv[i];
216 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
217 if (to_service_name != nullptr) {
221 to_service_name = argv[i];
224 if (service_name != nullptr) {
228 service_name = argv[i];
229 // TODO support multiple services
234 bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN);
236 if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
237 show_help |= (to_service_name == nullptr);
239 else if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) {
243 if (service_name != nullptr && no_service_cmd) {
247 if ((command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY)
248 && (! dep_type_set || service_name == nullptr || to_service_name == nullptr)) {
253 cout << "dinitctl: control Dinit services\n"
256 " dinitctl [options] start [options] <service-name>\n"
257 " dinitctl [options] stop [options] <service-name>\n"
258 " dinitctl [options] wake [options] <service-name>\n"
259 " dinitctl [options] release [options] <service-name>\n"
260 " dinitctl [options] unpin <service-name>\n"
261 " dinitctl [options] unload <service-name>\n"
262 " dinitctl [options] list\n"
263 " dinitctl [options] shutdown\n"
264 " dinitctl [options] add-dep <type> <from-service> <to-service>\n"
265 " dinitctl [options] rm-dep <type> <from-service> <to-service>\n"
266 " dinitctl [options] enable [--from <from-service>] <to-service>\n"
267 " dinitctl [options] disable [--from <from-service>] <to-service>\n"
269 "Note: An activated service continues running when its dependents stop.\n"
272 " --help : show this help\n"
273 " -s, --system : control system daemon instead of user daemon\n"
274 " --quiet : suppress output (except errors)\n"
275 " --socket-path <path>, -p <path>\n"
276 " : specify socket for communication with daemon\n"
279 " --no-wait : don't wait for service startup/shutdown to complete\n"
280 " --pin : pin the service in the requested state\n"
281 " --force : force stop even if dependents will be affected\n";
285 signal(SIGPIPE, SIG_IGN);
287 // Locate control socket
288 if (! control_socket_str.empty()) {
289 control_socket_path = control_socket_str.c_str();
292 control_socket_path = SYSCONTROLSOCKET; // default to system
294 char * userhome = getenv("HOME");
295 if (userhome == nullptr) {
296 struct passwd * pwuid_p = getpwuid(getuid());
297 if (pwuid_p != nullptr) {
298 userhome = pwuid_p->pw_dir;
302 if (userhome != nullptr) {
303 control_socket_str = userhome;
304 control_socket_str += "/.dinitctl";
305 control_socket_path = control_socket_str.c_str();
308 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
314 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
316 perror("dinitctl: socket");
320 struct sockaddr_un * name;
321 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
322 name = (struct sockaddr_un *) malloc(sockaddr_size);
323 if (name == nullptr) {
324 cerr << "dinitctl: Out of memory" << endl;
328 name->sun_family = AF_UNIX;
329 strcpy(name->sun_path, control_socket_path);
331 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
333 perror("dinitctl: connect");
338 // Start by querying protocol version:
340 check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum);
342 if (command == command_t::UNPIN_SERVICE) {
343 return unpin_service(socknum, rbuffer, service_name, verbose);
345 else if (command == command_t::UNLOAD_SERVICE) {
346 return unload_service(socknum, rbuffer, service_name);
348 else if (command == command_t::LIST_SERVICES) {
349 return list_services(socknum, rbuffer);
351 else if (command == command_t::SHUTDOWN) {
352 return shutdown_dinit(socknum, rbuffer);
354 else if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
355 return add_remove_dependency(socknum, rbuffer, command == command_t::ADD_DEPENDENCY,
356 service_name, to_service_name, dep_type);
358 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
359 // If only one service specified, assume that we enable for 'boot' service:
360 if (service_name == nullptr) {
361 service_name = "boot";
363 return enable_disable_service(socknum, rbuffer, service_name, to_service_name,
364 command == command_t::ENABLE_SERVICE);
367 return start_stop_service(socknum, rbuffer, service_name, command, do_pin, do_force,
368 wait_for_service, verbose);
371 catch (cp_old_client_exception &e) {
372 std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl;
375 catch (cp_old_server_exception &e) {
376 std::cerr << "dinitctl: server too old or protocol error" << std::endl;
379 catch (cp_read_exception &e) {
380 cerr << "dinitctl: control socket read failure or protocol error" << endl;
383 catch (cp_write_exception &e) {
384 cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl;
389 // Extract/read a string of specified length from the buffer/socket. The string is consumed
391 static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length)
393 int rb_len = rbuffer.get_length();
394 if (uint32_t(rb_len) >= length) {
395 std::string r = rbuffer.extract_string(0, length);
396 rbuffer.consume(length);
400 std::string r = rbuffer.extract_string(0, rb_len);
401 uint32_t rlen = length - rb_len;
405 rbuffer.fill(socknum);
406 char *bptr = rbuffer.get_ptr(0);
407 clen = rbuffer.get_length();
408 clen = std::min(clen, rlen);
409 r.append(bptr, clen);
413 rbuffer.consume(clen);
418 // Load a service: issue load command, wait for reply. Return true on success, display error message
419 // and return false on failure.
420 // socknum - the socket fd to communicate via
421 // rbuffer - the buffer for communication
422 // name - the name of the service to load
423 // handle - where to store the handle of the loaded service
424 // state - where to store the state of the loaded service (may be null).
425 static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle,
426 service_state_t *state)
428 // Load 'to' service:
429 if (issue_load_service(socknum, name)) {
433 wait_for_reply(rbuffer, socknum);
435 if (check_load_reply(socknum, rbuffer, handle, state) != 0) {
442 // Get the service name for a given handle, by querying the daemon.
443 static std::string get_service_name(int socknum, cpbuffer_t &rbuffer, handle_t handle)
445 char buf[2 + sizeof(handle)];
446 buf[0] = DINIT_CP_QUERYSERVICENAME;
448 memcpy(buf + 2, &handle, sizeof(handle));
450 write_all_x(socknum, buf, sizeof(buf));
452 wait_for_reply(rbuffer, socknum);
454 if (rbuffer[0] != DINIT_RP_SERVICENAME) {
455 throw cp_read_exception{0};
460 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint16_t));
462 rbuffer.extract(&namesize, 2, sizeof(uint16_t));
463 rbuffer.consume(2 + sizeof(uint16_t));
468 if (rbuffer.get_length() == 0) {
469 rbuffer.fill(socknum);
472 size_t to_extract = std::min(size_t(rbuffer.get_length()), namesize - name.length());
473 size_t contiguous_len = rbuffer.get_contiguous_length(rbuffer.get_ptr(0));
474 if (contiguous_len <= to_extract) {
475 name.append(rbuffer.get_ptr(0), contiguous_len);
476 rbuffer.consume(contiguous_len);
477 name.append(rbuffer.get_ptr(0), to_extract - contiguous_len);
478 rbuffer.consume(to_extract - contiguous_len);
481 name.append(rbuffer.get_ptr(0), to_extract);
482 rbuffer.consume(to_extract);
486 } while (name.length() < namesize);
491 // Start/stop a service
492 static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
493 command_t command, bool do_pin, bool do_force, bool wait_for_service, bool verbose)
497 bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE);
499 service_state_t state;
502 if (! load_service(socknum, rbuffer, service_name, &handle, &state)) {
506 service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
509 case command_t::STOP_SERVICE:
510 pcommand = DINIT_CP_STOPSERVICE;
512 case command_t::RELEASE_SERVICE:
513 pcommand = DINIT_CP_RELEASESERVICE;
515 case command_t::START_SERVICE:
516 pcommand = DINIT_CP_STARTSERVICE;
518 case command_t::WAKE_SERVICE:
519 pcommand = DINIT_CP_WAKESERVICE;
524 // Need to issue STOPSERVICE/STARTSERVICE
525 // We'll do this regardless of the current service state / target state, since issuing
526 // start/stop also sets or clears the "explicitly started" flag on the service.
528 char buf[2 + sizeof(handle)];
530 buf[1] = (do_pin ? 1 : 0) | ((pcommand == DINIT_CP_STOPSERVICE && !do_force) ? 2 : 0);
531 memcpy(buf + 2, &handle, sizeof(handle));
532 write_all_x(socknum, buf, 2 + sizeof(handle));
534 wait_for_reply(rbuffer, socknum);
535 if (rbuffer[0] == DINIT_RP_ALREADYSS) {
536 bool already = (state == wanted_state);
538 cout << "Service " << (already ? "(already) " : "")
539 << describeState(do_stop) << "." << endl;
541 return 0; // success!
543 if (rbuffer[0] == DINIT_RP_DEPENDENTS && pcommand == DINIT_CP_STOPSERVICE) {
544 cerr << "dinitctl: Cannot stop service due to the following dependents:\n"
545 "(Only direct dependents are listed. Exercise caution before using '--force' !!)\n";
546 // size_t number, N * handle_t handles
547 rbuffer.consume(1); // packet header
549 rbuffer.fill_to(socknum, sizeof(number));
550 rbuffer.extract(&number, 0, sizeof(number));
551 rbuffer.consume(sizeof(number));
552 std::vector<handle_t> handles;
553 handles.reserve(number);
554 for (size_t i = 0; i < number; i++) {
556 rbuffer.fill_to(socknum, sizeof(handle_t));
557 rbuffer.extract(&handle, 0, sizeof(handle));
558 handles.push_back(handle);
559 rbuffer.consume(sizeof(handle));
561 // Print the directly affected dependents:
563 for (handle_t handle : handles) {
564 cerr << " " << get_service_name(socknum, rbuffer, handle);
569 if (rbuffer[0] != DINIT_RP_ACK) {
570 cerr << "dinitctl: Protocol error." << endl;
576 if (! wait_for_service) {
578 cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl;
583 service_event_t completionEvent;
584 service_event_t cancelledEvent;
587 completionEvent = service_event_t::STOPPED;
588 cancelledEvent = service_event_t::STOPCANCELLED;
591 completionEvent = service_event_t::STARTED;
592 cancelledEvent = service_event_t::STARTCANCELLED;
595 // Wait until service started:
596 int r = rbuffer.fill_to(socknum, 2);
598 if (rbuffer[0] >= 100) {
599 int pktlen = (unsigned char) rbuffer[1];
600 fill_buffer_to(rbuffer, socknum, pktlen);
602 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
604 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
605 service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
606 if (ev_handle == handle) {
607 if (event == completionEvent) {
609 cout << "Service " << describeState(do_stop) << "." << endl;
613 else if (event == cancelledEvent) {
615 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
619 else if (! do_stop && event == service_event_t::FAILEDSTART) {
621 cout << "Service failed to start." << endl;
628 rbuffer.consume(pktlen);
629 r = rbuffer.fill_to(socknum, 2);
632 // Not an information packet?
633 cerr << "dinitctl: protocol error" << endl;
639 perror("dinitctl: read");
642 cerr << "protocol error (connection closed by server)" << endl;
647 // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for
648 // a response. Returns 1 on failure (with error logged), 0 on success.
649 static int issue_load_service(int socknum, const char *service_name, bool find_only)
652 uint16_t sname_len = strlen(service_name);
653 int bufsize = 3 + sname_len;
655 std::unique_ptr<char[]> ubuf(new char[bufsize]);
656 auto buf = ubuf.get();
658 buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE;
659 memcpy(buf + 1, &sname_len, 2);
660 memcpy(buf + 3, service_name, sname_len);
662 write_all_x(socknum, buf, bufsize);
667 // Check that a "load service" reply was received, and that the requested service was found.
668 // state_p may be null.
669 static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p)
673 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
674 fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
675 rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
676 if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
677 //target_state = static_cast<service_state_t>(rbuffer[2 + sizeof(handle)]);
678 rbuffer.consume(3 + sizeof(*handle_p));
681 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
682 cerr << "dinitctl: failed to find/load service." << endl;
686 cerr << "dinitctl: protocol error." << endl;
691 static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose)
698 if (! load_service(socknum, rbuffer, service_name, &handle, nullptr)) {
702 // Issue UNPIN command.
704 char buf[1 + sizeof(handle)];
705 buf[0] = DINIT_CP_UNPINSERVICE;
706 memcpy(buf + 1, &handle, sizeof(handle));
707 write_all_x(socknum, buf, sizeof(buf));
709 wait_for_reply(rbuffer, socknum);
710 if (rbuffer[0] != DINIT_RP_ACK) {
711 cerr << "dinitctl: protocol error." << endl;
718 cout << "Service unpinned." << endl;
723 static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name)
727 if (issue_load_service(socknum, service_name, true) == 1) {
731 wait_for_reply(rbuffer, socknum);
735 if (rbuffer[0] == DINIT_RP_NOSERVICE) {
736 cerr << "dinitctl: service not loaded." << endl;
740 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
744 // Issue UNLOAD command.
746 char buf[1 + sizeof(handle)];
747 buf[0] = DINIT_CP_UNLOADSERVICE;
748 memcpy(buf + 1, &handle, sizeof(handle));
749 write_all_x(socknum, buf, 2 + sizeof(handle));
751 wait_for_reply(rbuffer, socknum);
752 if (rbuffer[0] == DINIT_RP_NAK) {
753 cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of "
754 "other service." << endl;
757 if (rbuffer[0] != DINIT_RP_ACK) {
758 cerr << "dinitctl: Protocol error." << endl;
764 cout << "Service unloaded." << endl;
768 static int list_services(int socknum, cpbuffer_t &rbuffer)
772 char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES };
773 write_all_x(socknum, cmdbuf, 1);
775 wait_for_reply(rbuffer, socknum);
776 while (rbuffer[0] == DINIT_RP_SVCINFO) {
777 int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
778 fill_buffer_to(rbuffer, socknum, hdrsize);
779 int nameLen = rbuffer[1];
780 service_state_t current = static_cast<service_state_t>(rbuffer[2]);
781 service_state_t target = static_cast<service_state_t>(rbuffer[3]);
783 int console_flags = rbuffer[4];
784 bool has_console = (console_flags & 2) != 0;
785 bool waiting_console = (console_flags & 1) != 0;
786 bool was_skipped = (console_flags & 4) != 0;
788 stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[5]);
792 if (current != service_state_t::STOPPED) {
793 rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid));
796 rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
799 fill_buffer_to(rbuffer, socknum, nameLen + hdrsize);
801 char *name_ptr = rbuffer.get_ptr(hdrsize);
802 int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
804 string name = string(name_ptr, clength);
805 name.append(rbuffer.get_buf_base(), nameLen - clength);
809 cout << (target == service_state_t::STARTED ? "{" : " ");
810 if (current == service_state_t::STARTED) {
811 cout << (was_skipped ? "s" : "+");
816 cout << (target == service_state_t::STARTED ? "}" : " ");
818 if (current == service_state_t::STARTING) {
821 else if (current == service_state_t::STOPPING) {
828 cout << (target == service_state_t::STOPPED ? "{" : " ");
829 if (current == service_state_t::STOPPED) {
830 bool did_fail = false;
831 if (stop_reason == stopped_reason_t::TERMINATED) {
832 if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) {
836 else did_fail = (stop_reason != stopped_reason_t::NORMAL);
838 cout << (did_fail ? "X" : "-");
843 cout << (target == service_state_t::STOPPED ? "}" : " ");
845 cout << "] " << name;
847 if (current != service_state_t::STOPPED && service_pid != -1) {
848 cout << " (pid: " << service_pid << ")";
851 if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) {
852 if (WIFEXITED(exit_status)) {
853 cout << " (exit status: " << WEXITSTATUS(exit_status) << ")";
855 else if (WIFSIGNALED(exit_status)) {
856 cout << " (signal: " << WTERMSIG(exit_status) << ")";
861 cout << " (has console)";
863 else if (waiting_console) {
864 cout << " (waiting for console)";
869 rbuffer.consume(hdrsize + nameLen);
870 wait_for_reply(rbuffer, socknum);
873 if (rbuffer[0] != DINIT_RP_LISTDONE) {
874 cerr << "dinitctl: Control socket protocol error" << endl;
881 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
882 const char *service_from, const char *service_to, dependency_type dep_type)
887 handle_t from_handle;
890 if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr)
891 || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) {
895 constexpr int pktsize = 2 + sizeof(handle_t) * 2;
896 char cmdbuf[pktsize] = { add ? (char)DINIT_CP_ADD_DEP : (char)DINIT_CP_REM_DEP, (char)dep_type};
897 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
898 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
899 write_all_x(socknum, cmdbuf, pktsize);
901 wait_for_reply(rbuffer, socknum);
904 if (rbuffer[0] == DINIT_RP_NAK) {
905 cerr << "dinitctl: Could not add dependency: circular dependency or wrong state" << endl;
908 if (rbuffer[0] != DINIT_RP_ACK) {
909 cerr << "dinitctl: Control socket protocol error" << endl;
916 static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer)
918 // TODO support no-wait option.
922 constexpr int bufsize = 2;
925 buf[0] = DINIT_CP_SHUTDOWN;
926 buf[1] = static_cast<char>(shutdown_type_t::HALT);
928 write_all_x(socknum, buf, bufsize);
930 wait_for_reply(rbuffer, socknum);
932 if (rbuffer[0] != DINIT_RP_ACK) {
933 cerr << "dinitctl: Control socket protocol error" << endl;
937 // Now wait for rollback complete, by waiting for the connection to close:
940 wait_for_info(rbuffer, socknum);
941 rbuffer.consume(rbuffer[1]);
944 catch (cp_read_exception &exc) {
945 // Assume that the connection closed.
951 // exception for cancelling a service operation
952 class service_op_cancel { };
954 static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to,
959 service_state_t from_state = service_state_t::STARTED;
960 handle_t from_handle;
964 if (! load_service(socknum, rbuffer, from, &from_handle, &from_state)
965 || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) {
969 // Get service load path
970 char buf[1] = { DINIT_CP_QUERY_LOAD_MECH };
971 write_all_x(socknum, buf, 1);
973 wait_for_reply(rbuffer, socknum);
975 if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
976 cerr << "dinitctl: Control socket protocol error" << endl;
980 // Packet type, load mechanism type, packet size:
981 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t));
983 if (rbuffer[1] != SSET_TYPE_DIRLOAD) {
984 cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl;
988 vector<string> paths;
991 rbuffer.extract(&pktsize, 2, sizeof(uint32_t));
993 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length
995 uint32_t path_entries; // number of service directories
996 rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t));
999 rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t));
1000 rbuffer.consume(2 + sizeof(uint32_t) * 3);
1001 pktsize -= 2 + sizeof(uint32_t) * 3;
1003 // Read current working directory of daemon:
1004 std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len);
1006 // dinit daemon base directory against which service paths are resolved is in dinit_cwd
1008 for (int i = 0; i < (int)path_entries; i++) {
1010 fill_buffer_to(rbuffer, socknum, sizeof(uint32_t));
1011 rbuffer.extract(&plen, 0, sizeof(uint32_t));
1012 rbuffer.consume(sizeof(uint32_t));
1013 paths.push_back(read_string(socknum, rbuffer, plen));
1016 // all service directories are now in the 'paths' vector
1017 // Load/read service description for 'from' service:
1019 ifstream service_file;
1020 string service_file_path;
1022 for (std::string path : paths) {
1023 string test_path = combine_paths(combine_paths(dinit_cwd, path.c_str()), from);
1025 service_file.open(test_path.c_str(), ios::in);
1027 service_file_path = test_path;
1032 if (! service_file) {
1033 cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl;
1037 // We now need to read the service file, identify the waits-for.d directory (bail out if more than one),
1038 // make sure the service is not listed as a dependency individually.
1043 process_service_file(from, service_file, [&](string &line, string &setting,
1044 dinit_load::string_iterator i, dinit_load::string_iterator end) -> void {
1045 if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") {
1046 string dname = dinit_load::read_setting_value(i, end);
1048 // There is already a dependency
1049 cerr << "dinitctl: there is a fixed dependency to service '" << to
1050 << "' in the service description of '" << from << "'." << endl;
1051 throw service_op_cancel();
1054 else if (setting == "waits-for.d") {
1055 string dname = dinit_load::read_setting_value(i, end);
1056 if (! waits_for_d.empty()) {
1057 cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories "
1058 << "specified in service description" << endl;
1059 throw service_op_cancel();
1061 waits_for_d = std::move(dname);
1065 catch (const service_op_cancel &cexc) {
1069 // If the from service has no waits-for.d specified, we can't continue
1070 if (waits_for_d.empty()) {
1071 cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl;
1075 // The waits-for.d path is relative to the service file path, combine:
1076 string waits_for_d_full = combine_paths(parent_path(service_file_path), waits_for_d.c_str());
1078 // check if dependency already exists
1079 string dep_link_path = combine_paths(waits_for_d_full, to);
1080 struct stat stat_buf;
1081 if (lstat(dep_link_path.c_str(), &stat_buf) == -1) {
1082 if (errno != ENOENT) {
1083 cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": "
1084 << strerror(errno) << endl;
1089 // dependency already exists
1091 cerr << "dinitctl: service already enabled." << endl;
1096 // warn if 'from' service is not started
1097 if (enable && from_state != service_state_t::STARTED) {
1098 cerr << "dinitctl: warning: enabling dependency for non-started service" << endl;
1101 // add/remove dependency
1102 constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2;
1103 char cmdbuf[enable_pktsize] = { char(enable ? DINIT_CP_ENABLESERVICE : DINIT_CP_REM_DEP),
1104 char(dependency_type::WAITS_FOR)};
1105 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
1106 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
1107 write_all_x(socknum, cmdbuf, enable_pktsize);
1109 wait_for_reply(rbuffer, socknum);
1112 if (enable && rbuffer[0] == DINIT_RP_NAK) {
1113 cerr << "dinitctl: Could not enable service: possible circular dependency" << endl;
1116 if (rbuffer[0] != DINIT_RP_ACK) {
1117 cerr << "dinitctl: Control socket protocol error" << endl;
1123 if (symlink((string("../") + to).c_str(), dep_link_path.c_str()) == -1) {
1124 cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno)
1125 << "\n" "dinitctl: Note: service was activated, but will not be enabled on restart."
1131 if (unlink(dep_link_path.c_str()) == -1) {
1132 cerr << "dinitctl: Could not unlink dependency entry " << dep_link_path << ": "
1133 << strerror(errno) << "\n"
1134 "dinitctl: Note: service was disabled, but will be re-enabled on restart." << endl;