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 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;
97 command_t command = command_t::NONE;
99 for (int i = 1; i < argc; i++) {
100 if (argv[i][0] == '-') {
101 if (strcmp(argv[i], "--help") == 0) {
105 else if (strcmp(argv[i], "--no-wait") == 0) {
106 wait_for_service = false;
108 else if (strcmp(argv[i], "--quiet") == 0) {
111 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
114 else if (strcmp(argv[i], "--pin") == 0) {
117 else if ((command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE)
118 && strcmp(argv[i], "--from") == 0) {
121 cerr << "dinitctl: --from should be followed by a service name" << std::endl;
124 service_name = argv[i];
127 cerr << "dinitctl: unrecognized option: " << argv[i] << " (use --help for help)\n";
131 else if (command == command_t::NONE) {
132 if (strcmp(argv[i], "start") == 0) {
133 command = command_t::START_SERVICE;
135 else if (strcmp(argv[i], "wake") == 0) {
136 command = command_t::WAKE_SERVICE;
138 else if (strcmp(argv[i], "stop") == 0) {
139 command = command_t::STOP_SERVICE;
141 else if (strcmp(argv[i], "release") == 0) {
142 command = command_t::RELEASE_SERVICE;
144 else if (strcmp(argv[i], "unpin") == 0) {
145 command = command_t::UNPIN_SERVICE;
147 else if (strcmp(argv[i], "unload") == 0) {
148 command = command_t::UNLOAD_SERVICE;
150 else if (strcmp(argv[i], "list") == 0) {
151 command = command_t::LIST_SERVICES;
153 else if (strcmp(argv[i], "shutdown") == 0) {
154 command = command_t::SHUTDOWN;
156 else if (strcmp(argv[i], "add-dep") == 0) {
157 command = command_t::ADD_DEPENDENCY;
159 else if (strcmp(argv[i], "rm-dep") == 0) {
160 command = command_t::RM_DEPENDENCY;
162 else if (strcmp(argv[i], "enable") == 0) {
163 command = command_t::ENABLE_SERVICE;
165 else if (strcmp(argv[i], "disable") == 0) {
166 command = command_t::DISABLE_SERVICE;
169 cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n";
174 // service name / other non-option
175 if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
176 if (! dep_type_set) {
177 if (strcmp(argv[i], "regular") == 0) {
178 dep_type = dependency_type::REGULAR;
180 else if (strcmp(argv[i], "milestone") == 0) {
181 dep_type = dependency_type::MILESTONE;
183 else if (strcmp(argv[i], "waits-for") == 0) {
184 dep_type = dependency_type::WAITS_FOR;
192 else if (service_name == nullptr) {
193 service_name = argv[i];
195 else if (to_service_name == nullptr) {
196 to_service_name = argv[i];
203 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
204 if (to_service_name != nullptr) {
208 to_service_name = argv[i];
211 if (service_name != nullptr) {
215 service_name = argv[i];
216 // TODO support multiple services
221 bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN);
223 if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
224 show_help |= (to_service_name == nullptr);
226 else if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) {
230 if (service_name != nullptr && no_service_cmd) {
234 if ((command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY)
235 && (! dep_type_set || service_name == nullptr || to_service_name == nullptr)) {
240 cout << "dinitctl: control Dinit services\n"
243 " dinitctl [options] start [options] <service-name>\n"
244 " dinitctl [options] stop [options] <service-name>\n"
245 " dinitctl [options] wake [options] <service-name>\n"
246 " dinitctl [options] release [options] <service-name>\n"
247 " dinitctl [options] unpin <service-name>\n"
248 " dinitctl unload <service-name>\n"
250 " dinitctl shutdown\n"
251 " dinitctl add-dep <type> <from-service> <to-service>\n"
252 " dinitctl rm-dep <type> <from-service> <to-service>\n"
253 " dinitctl enable [--from <from-service>] <to-service>\n"
254 " dinitctl disable [--from <from-service>] <to-service>\n"
256 "Note: An activated service continues running when its dependents stop.\n"
259 " -s, --system : control system daemon instead of user daemon\n"
260 " --quiet : suppress output (except errors)\n"
263 " --help : show this help\n"
264 " --no-wait : don't wait for service startup/shutdown to complete\n"
265 " --pin : pin the service in the requested state\n";
269 signal(SIGPIPE, SIG_IGN);
271 control_socket_path = SYSCONTROLSOCKET;
273 // Locate control socket
275 char * userhome = getenv("HOME");
276 if (userhome == nullptr) {
277 struct passwd * pwuid_p = getpwuid(getuid());
278 if (pwuid_p != nullptr) {
279 userhome = pwuid_p->pw_dir;
283 if (userhome != nullptr) {
284 control_socket_str = userhome;
285 control_socket_str += "/.dinitctl";
286 control_socket_path = control_socket_str.c_str();
289 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
294 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
296 perror("dinitctl: socket");
300 struct sockaddr_un * name;
301 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
302 name = (struct sockaddr_un *) malloc(sockaddr_size);
303 if (name == nullptr) {
304 cerr << "dinitctl: Out of memory" << endl;
308 name->sun_family = AF_UNIX;
309 strcpy(name->sun_path, control_socket_path);
311 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
313 perror("dinitctl: connect");
318 // Start by querying protocol version:
320 check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum);
322 if (command == command_t::UNPIN_SERVICE) {
323 return unpin_service(socknum, rbuffer, service_name, verbose);
325 else if (command == command_t::UNLOAD_SERVICE) {
326 return unload_service(socknum, rbuffer, service_name);
328 else if (command == command_t::LIST_SERVICES) {
329 return list_services(socknum, rbuffer);
331 else if (command == command_t::SHUTDOWN) {
332 return shutdown_dinit(socknum, rbuffer);
334 else if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
335 return add_remove_dependency(socknum, rbuffer, command == command_t::ADD_DEPENDENCY,
336 service_name, to_service_name, dep_type);
338 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
339 // If only one service specified, assume that we enable for 'boot' service:
340 if (service_name == nullptr) {
341 service_name = "boot";
343 return enable_disable_service(socknum, rbuffer, service_name, to_service_name,
344 command == command_t::ENABLE_SERVICE);
347 return start_stop_service(socknum, rbuffer, service_name, command, do_pin,
348 wait_for_service, verbose);
351 catch (cp_old_client_exception &e) {
352 std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl;
355 catch (cp_old_server_exception &e) {
356 std::cerr << "dinitctl: server too old or protocol error" << std::endl;
359 catch (cp_read_exception &e) {
360 cerr << "dinitctl: control socket read failure or protocol error" << endl;
363 catch (cp_write_exception &e) {
364 cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl;
369 // Extract/read a string of specified length from the buffer/socket. The string is consumed
371 static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length)
373 int rb_len = rbuffer.get_length();
374 if (uint32_t(rb_len) >= length) {
375 std::string r = rbuffer.extract_string(0, length);
376 rbuffer.consume(length);
380 std::string r = rbuffer.extract_string(0, rb_len);
381 uint32_t rlen = length - rb_len;
385 rbuffer.fill(socknum);
386 char *bptr = rbuffer.get_ptr(0);
387 clen = rbuffer.get_length();
388 clen = std::min(clen, rlen);
389 r.append(bptr, clen);
393 rbuffer.consume(clen);
398 // Load a service: issue load command, wait for reply. Return true on success, display error message
399 // and return false on failure.
400 // socknum - the socket fd to communicate via
401 // rbuffer - the buffer for communication
402 // name - the name of the service to load
403 // handle - where to store the handle of the loaded service
404 // state - where to store the state of the loaded service (may be null).
405 static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle,
406 service_state_t *state)
408 // Load 'to' service:
409 if (issue_load_service(socknum, name)) {
413 wait_for_reply(rbuffer, socknum);
415 if (check_load_reply(socknum, rbuffer, handle, state) != 0) {
422 // Start/stop a service
423 static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
424 command_t command, bool do_pin, bool wait_for_service, bool verbose)
428 bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE);
430 service_state_t state;
433 if (! load_service(socknum, rbuffer, service_name, &handle, &state)) {
437 service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
440 case command_t::STOP_SERVICE:
441 pcommand = DINIT_CP_STOPSERVICE;
443 case command_t::RELEASE_SERVICE:
444 pcommand = DINIT_CP_RELEASESERVICE;
446 case command_t::START_SERVICE:
447 pcommand = DINIT_CP_STARTSERVICE;
449 case command_t::WAKE_SERVICE:
450 pcommand = DINIT_CP_WAKESERVICE;
455 // Need to issue STOPSERVICE/STARTSERVICE
456 // We'll do this regardless of the current service state / target state, since issuing
457 // start/stop also sets or clears the "explicitly started" flag on the service.
459 char buf[2 + sizeof(handle)];
461 buf[1] = do_pin ? 1 : 0;
462 memcpy(buf + 2, &handle, sizeof(handle));
463 write_all_x(socknum, buf, 2 + sizeof(handle));
465 wait_for_reply(rbuffer, socknum);
466 if (rbuffer[0] == DINIT_RP_ALREADYSS) {
467 bool already = (state == wanted_state);
469 cout << "Service " << (already ? "(already) " : "")
470 << describeState(do_stop) << "." << endl;
472 return 0; // success!
474 if (rbuffer[0] != DINIT_RP_ACK) {
475 cerr << "dinitctl: Protocol error." << endl;
481 if (! wait_for_service) {
483 cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl;
488 service_event_t completionEvent;
489 service_event_t cancelledEvent;
492 completionEvent = service_event_t::STOPPED;
493 cancelledEvent = service_event_t::STOPCANCELLED;
496 completionEvent = service_event_t::STARTED;
497 cancelledEvent = service_event_t::STARTCANCELLED;
500 // Wait until service started:
501 int r = rbuffer.fill_to(socknum, 2);
503 if (rbuffer[0] >= 100) {
504 int pktlen = (unsigned char) rbuffer[1];
505 fill_buffer_to(rbuffer, socknum, pktlen);
507 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
509 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
510 service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
511 if (ev_handle == handle) {
512 if (event == completionEvent) {
514 cout << "Service " << describeState(do_stop) << "." << endl;
518 else if (event == cancelledEvent) {
520 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
524 else if (! do_stop && event == service_event_t::FAILEDSTART) {
526 cout << "Service failed to start." << endl;
533 rbuffer.consume(pktlen);
534 r = rbuffer.fill_to(socknum, 2);
537 // Not an information packet?
538 cerr << "dinitctl: protocol error" << endl;
544 perror("dinitctl: read");
547 cerr << "protocol error (connection closed by server)" << endl;
552 // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for
553 // a response. Returns 1 on failure (with error logged), 0 on success.
554 static int issue_load_service(int socknum, const char *service_name, bool find_only)
557 uint16_t sname_len = strlen(service_name);
558 int bufsize = 3 + sname_len;
560 std::unique_ptr<char[]> ubuf(new char[bufsize]);
561 auto buf = ubuf.get();
563 buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE;
564 memcpy(buf + 1, &sname_len, 2);
565 memcpy(buf + 3, service_name, sname_len);
567 write_all_x(socknum, buf, bufsize);
572 // Check that a "load service" reply was received, and that the requested service was found.
573 // state_p may be null.
574 static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p)
578 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
579 fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
580 rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
581 if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
582 //target_state = static_cast<service_state_t>(rbuffer[2 + sizeof(handle)]);
583 rbuffer.consume(3 + sizeof(*handle_p));
586 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
587 cerr << "dinitctl: failed to find/load service." << endl;
591 cerr << "dinitctl: protocol error." << endl;
596 static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose)
603 if (! load_service(socknum, rbuffer, service_name, &handle, nullptr)) {
607 // Issue UNPIN command.
609 char buf[1 + sizeof(handle)];
610 buf[0] = DINIT_CP_UNPINSERVICE;
611 memcpy(buf + 1, &handle, sizeof(handle));
612 write_all_x(socknum, buf, 2 + sizeof(handle));
614 wait_for_reply(rbuffer, socknum);
615 if (rbuffer[0] != DINIT_RP_ACK) {
616 cerr << "dinitctl: protocol error." << endl;
623 cout << "Service unpinned." << endl;
628 static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name)
632 if (issue_load_service(socknum, service_name, true) == 1) {
636 wait_for_reply(rbuffer, socknum);
640 if (rbuffer[0] == DINIT_RP_NOSERVICE) {
641 cerr << "dinitctl: service not loaded." << endl;
645 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
649 // Issue UNLOAD command.
651 char buf[1 + sizeof(handle)];
652 buf[0] = DINIT_CP_UNLOADSERVICE;
653 memcpy(buf + 1, &handle, sizeof(handle));
654 write_all_x(socknum, buf, 2 + sizeof(handle));
656 wait_for_reply(rbuffer, socknum);
657 if (rbuffer[0] == DINIT_RP_NAK) {
658 cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of "
659 "other service." << endl;
662 if (rbuffer[0] != DINIT_RP_ACK) {
663 cerr << "dinitctl: Protocol error." << endl;
669 cout << "Service unloaded." << endl;
673 static int list_services(int socknum, cpbuffer_t &rbuffer)
677 char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES };
678 write_all_x(socknum, cmdbuf, 1);
680 wait_for_reply(rbuffer, socknum);
681 while (rbuffer[0] == DINIT_RP_SVCINFO) {
682 int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
683 fill_buffer_to(rbuffer, socknum, hdrsize);
684 int nameLen = rbuffer[1];
685 service_state_t current = static_cast<service_state_t>(rbuffer[2]);
686 service_state_t target = static_cast<service_state_t>(rbuffer[3]);
688 int console_flags = rbuffer[4];
689 bool has_console = (console_flags & 2) != 0;
690 bool waiting_console = (console_flags & 1) != 0;
691 bool was_skipped = (console_flags & 4) != 0;
693 stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[5]);
697 if (current != service_state_t::STOPPED) {
698 rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid));
701 rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
704 fill_buffer_to(rbuffer, socknum, nameLen + hdrsize);
706 char *name_ptr = rbuffer.get_ptr(hdrsize);
707 int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
709 string name = string(name_ptr, clength);
710 name.append(rbuffer.get_buf_base(), nameLen - clength);
714 cout << (target == service_state_t::STARTED ? "{" : " ");
715 if (current == service_state_t::STARTED) {
716 cout << (was_skipped ? "s" : "+");
721 cout << (target == service_state_t::STARTED ? "}" : " ");
723 if (current == service_state_t::STARTING) {
726 else if (current == service_state_t::STOPPING) {
733 cout << (target == service_state_t::STOPPED ? "{" : " ");
734 if (current == service_state_t::STOPPED) {
735 bool did_fail = false;
736 if (stop_reason == stopped_reason_t::TERMINATED) {
737 if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) {
741 else did_fail = (stop_reason != stopped_reason_t::NORMAL);
743 cout << (did_fail ? "X" : "-");
748 cout << (target == service_state_t::STOPPED ? "}" : " ");
750 cout << "] " << name;
752 if (current != service_state_t::STOPPED && service_pid != -1) {
753 cout << " (pid: " << service_pid << ")";
756 if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) {
757 if (WIFEXITED(exit_status)) {
758 cout << " (exit status: " << WEXITSTATUS(exit_status) << ")";
760 else if (WIFSIGNALED(exit_status)) {
761 cout << " (signal: " << WTERMSIG(exit_status) << ")";
766 cout << " (has console)";
768 else if (waiting_console) {
769 cout << " (waiting for console)";
774 rbuffer.consume(hdrsize + nameLen);
775 wait_for_reply(rbuffer, socknum);
778 if (rbuffer[0] != DINIT_RP_LISTDONE) {
779 cerr << "dinitctl: Control socket protocol error" << endl;
786 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
787 const char *service_from, const char *service_to, dependency_type dep_type)
792 handle_t from_handle;
795 if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr)
796 || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) {
800 constexpr int pktsize = 2 + sizeof(handle_t) * 2;
801 char cmdbuf[pktsize] = { add ? (char)DINIT_CP_ADD_DEP : (char)DINIT_CP_REM_DEP, (char)dep_type};
802 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
803 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
804 write_all_x(socknum, cmdbuf, pktsize);
806 wait_for_reply(rbuffer, socknum);
809 if (rbuffer[0] == DINIT_RP_NAK) {
810 cerr << "dinitctl: Could not add dependency: circular dependency or wrong state" << endl;
813 if (rbuffer[0] != DINIT_RP_ACK) {
814 cerr << "dinitctl: Control socket protocol error" << endl;
821 static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer)
823 // TODO support no-wait option.
827 constexpr int bufsize = 2;
830 buf[0] = DINIT_CP_SHUTDOWN;
831 buf[1] = static_cast<char>(shutdown_type_t::HALT);
833 write_all_x(socknum, buf, bufsize);
835 wait_for_reply(rbuffer, socknum);
837 if (rbuffer[0] != DINIT_RP_ACK) {
838 cerr << "dinitctl: Control socket protocol error" << endl;
842 // Now wait for rollback complete, by waiting for the connection to close:
845 wait_for_info(rbuffer, socknum);
846 rbuffer.consume(rbuffer[1]);
849 catch (cp_read_exception &exc) {
850 // Assume that the connection closed.
856 // exception for cancelling a service operation
857 class service_op_cancel { };
859 static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to,
864 service_state_t from_state = service_state_t::STARTED;
865 handle_t from_handle;
869 if (! load_service(socknum, rbuffer, from, &from_handle, &from_state)
870 || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) {
874 // Get service load path
875 char buf[1] = { DINIT_CP_QUERY_LOAD_MECH };
876 write_all_x(socknum, buf, 1);
878 wait_for_reply(rbuffer, socknum);
880 if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
881 cerr << "dinitctl: Control socket protocol error" << endl;
885 // Packet type, load mechanism type, packet size:
886 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t));
888 if (rbuffer[1] != SSET_TYPE_DIRLOAD) {
889 cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl;
893 vector<string> paths;
896 rbuffer.extract(&pktsize, 2, sizeof(uint32_t));
898 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length
900 uint32_t path_entries; // number of service directories
901 rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t));
904 rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t));
905 rbuffer.consume(2 + sizeof(uint32_t) * 3);
906 pktsize -= 2 + sizeof(uint32_t) * 3;
908 // Read current working directory of daemon:
909 std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len);
911 // dinit daemon base directory against which service paths are resolved is in dinit_cwd
913 for (int i = 0; i < (int)path_entries; i++) {
915 fill_buffer_to(rbuffer, socknum, sizeof(uint32_t));
916 rbuffer.extract(&plen, 0, sizeof(uint32_t));
917 rbuffer.consume(sizeof(uint32_t));
918 paths.push_back(read_string(socknum, rbuffer, plen));
921 // all service directories are now in the 'paths' vector
922 // Load/read service description for 'from' service:
924 ifstream service_file;
925 string service_file_path;
927 for (std::string path : paths) {
928 string test_path = combine_paths(combine_paths(dinit_cwd, path.c_str()), from);
930 service_file.open(test_path.c_str(), ios::in);
932 service_file_path = test_path;
937 if (! service_file) {
938 cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl;
942 // We now need to read the service file, identify the waits-for.d directory (bail out if more than one),
943 // make sure the service is not listed as a dependency individually.
948 process_service_file(from, service_file, [&](string &line, string &setting,
949 dinit_load::string_iterator i, dinit_load::string_iterator end) -> void {
950 if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") {
951 string dname = dinit_load::read_setting_value(i, end);
953 // There is already a dependency
954 cerr << "dinitctl: there is a fixed dependency to service '" << to
955 << "' in the service description of '" << from << "'." << endl;
956 throw service_op_cancel();
959 else if (setting == "waits-for.d") {
960 string dname = dinit_load::read_setting_value(i, end);
961 if (! waits_for_d.empty()) {
962 cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories "
963 << "specified in service description" << endl;
964 throw service_op_cancel();
966 waits_for_d = std::move(dname);
970 catch (const service_op_cancel &cexc) {
974 // If the from service has no waits-for.d specified, we can't continue
975 if (waits_for_d.empty()) {
976 cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl;
980 // The waits-for.d path is relative to the service file path, combine:
981 string waits_for_d_full = combine_paths(parent_path(service_file_path), waits_for_d.c_str());
983 // check if dependency already exists
984 string dep_link_path = combine_paths(waits_for_d_full, to);
985 struct stat stat_buf;
986 if (lstat(dep_link_path.c_str(), &stat_buf) == -1) {
987 if (errno != ENOENT) {
988 cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": "
989 << strerror(errno) << endl;
994 // dependency already exists
996 cerr << "dinitctl: service already enabled." << endl;
1001 // warn if 'from' service is not started
1002 if (enable && from_state != service_state_t::STARTED) {
1003 cerr << "dinitctl: warning: enabling dependency for non-started service" << endl;
1006 // add/remove dependency
1007 constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2;
1008 char cmdbuf[enable_pktsize] = { char(enable ? DINIT_CP_ENABLESERVICE : DINIT_CP_REM_DEP),
1009 char(dependency_type::WAITS_FOR)};
1010 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
1011 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
1012 write_all_x(socknum, cmdbuf, enable_pktsize);
1014 wait_for_reply(rbuffer, socknum);
1017 if (enable && rbuffer[0] == DINIT_RP_NAK) {
1018 cerr << "dinitctl: Could not enable service: possible circular dependency" << endl;
1021 if (rbuffer[0] != DINIT_RP_ACK) {
1022 cerr << "dinitctl: Control socket protocol error" << endl;
1028 if (symlink((string("../") + to).c_str(), dep_link_path.c_str()) == -1) {
1029 cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno)
1030 << "\n" "dinitctl: Note: service was activated, but will not be enabled on restart."
1036 if (unlink(dep_link_path.c_str()) == -1) {
1037 cerr << "dinitctl: Could not unlink dependency entry " << dep_link_path << ": "
1038 << strerror(errno) << "\n"
1039 "dinitctl: Note: service was disabled, but will be re-enabled on restart." << endl;