7 #include <system_error>
11 #include <sys/types.h>
13 #include <sys/socket.h>
19 #include "control-cmds.h"
20 #include "service-constants.h"
22 #include "dinit-client.h"
23 #include "load-service.h"
25 // dinitctl: utility to control the Dinit daemon, including starting and stopping of services.
27 // This utility communicates with the dinit daemon via a unix stream socket (/dev/initctl,
28 // or $HOME/.dinitctl).
30 static constexpr uint16_t min_cp_version = 1;
31 static constexpr uint16_t max_cp_version = 1;
35 static int issue_load_service(int socknum, const char *service_name, bool find_only = false);
36 static int check_load_reply(int socknum, cpbuffer_t &, handle_t *handle_p, service_state_t *state_p);
37 static int start_stop_service(int socknum, cpbuffer_t &, const char *service_name, command_t command,
38 bool do_pin, bool wait_for_service, bool verbose);
39 static int unpin_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose);
40 static int unload_service(int socknum, cpbuffer_t &, const char *service_name);
41 static int list_services(int socknum, cpbuffer_t &);
42 static int shutdown_dinit(int soclknum, cpbuffer_t &);
43 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add, const char *service_from,
44 const char *service_to, dependency_type dep_type);
45 static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to,
48 static const char * describeState(bool stopped)
50 return stopped ? "stopped" : "started";
53 static const char * describeVerb(bool stop)
55 return stop ? "stop" : "start";
58 enum class command_t {
76 int main(int argc, char **argv)
80 bool show_help = argc < 2;
81 const char *service_name = nullptr;
82 const char *to_service_name = nullptr;
83 dependency_type dep_type;
84 bool dep_type_set = false;
86 std::string control_socket_str;
87 const char * control_socket_path = nullptr;
90 bool sys_dinit = false; // communicate with system daemon
91 bool wait_for_service = true;
94 command_t command = command_t::NONE;
96 for (int i = 1; i < argc; i++) {
97 if (argv[i][0] == '-') {
98 if (strcmp(argv[i], "--help") == 0) {
102 else if (strcmp(argv[i], "--no-wait") == 0) {
103 wait_for_service = false;
105 else if (strcmp(argv[i], "--quiet") == 0) {
108 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
111 else if (strcmp(argv[i], "--pin") == 0) {
114 else if ((command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE)
115 && strcmp(argv[i], "--from") == 0) {
118 cerr << "dinitctl: --from should be followed by a service name" << std::endl;
121 service_name = argv[i];
124 cerr << "dinitctl: unrecognized option: " << argv[i] << " (use --help for help)\n";
128 else if (command == command_t::NONE) {
129 if (strcmp(argv[i], "start") == 0) {
130 command = command_t::START_SERVICE;
132 else if (strcmp(argv[i], "wake") == 0) {
133 command = command_t::WAKE_SERVICE;
135 else if (strcmp(argv[i], "stop") == 0) {
136 command = command_t::STOP_SERVICE;
138 else if (strcmp(argv[i], "release") == 0) {
139 command = command_t::RELEASE_SERVICE;
141 else if (strcmp(argv[i], "unpin") == 0) {
142 command = command_t::UNPIN_SERVICE;
144 else if (strcmp(argv[i], "unload") == 0) {
145 command = command_t::UNLOAD_SERVICE;
147 else if (strcmp(argv[i], "list") == 0) {
148 command = command_t::LIST_SERVICES;
150 else if (strcmp(argv[i], "shutdown") == 0) {
151 command = command_t::SHUTDOWN;
153 else if (strcmp(argv[i], "add-dep") == 0) {
154 command = command_t::ADD_DEPENDENCY;
156 else if (strcmp(argv[i], "rm-dep") == 0) {
157 command = command_t::RM_DEPENDENCY;
159 else if (strcmp(argv[i], "enable") == 0) {
160 command = command_t::ENABLE_SERVICE;
162 else if (strcmp(argv[i], "disable") == 0) {
163 command = command_t::DISABLE_SERVICE;
166 cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n";
171 // service name / other non-option
172 if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
173 if (! dep_type_set) {
174 if (strcmp(argv[i], "regular") == 0) {
175 dep_type = dependency_type::REGULAR;
177 else if (strcmp(argv[i], "milestone") == 0) {
178 dep_type = dependency_type::MILESTONE;
180 else if (strcmp(argv[i], "waits-for") == 0) {
181 dep_type = dependency_type::WAITS_FOR;
189 else if (service_name == nullptr) {
190 service_name = argv[i];
192 else if (to_service_name == nullptr) {
193 to_service_name = argv[i];
200 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
201 if (to_service_name != nullptr) {
205 to_service_name = argv[i];
208 if (service_name != nullptr) {
212 service_name = argv[i];
213 // TODO support multiple services
218 bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN);
220 if (service_name != nullptr && no_service_cmd) {
224 if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) {
228 if ((command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY)
229 && (! dep_type_set || service_name == nullptr || to_service_name == nullptr)) {
234 cout << "dinitctl: control Dinit services\n"
237 " dinitctl [options] start [options] <service-name>\n"
238 " dinitctl [options] stop [options] <service-name>\n"
239 " dinitctl [options] wake [options] <service-name>\n"
240 " dinitctl [options] release [options] <service-name>\n"
241 " dinitctl [options] unpin <service-name>\n"
242 " dinitctl unload <service-name>\n"
244 " dinitctl shutdown\n"
245 " dinitctl add-dep <type> <from-service> <to-service>\n"
246 " dinitctl rm-dep <type> <from-service> <to-service>\n"
247 " dinitctl enable [--from <from-service>] <to-service>\n"
248 " dinitctl disable [--from <from-service>] <to-service>\n"
250 "Note: An activated service continues running when its dependents stop.\n"
253 " -s, --system : control system daemon instead of user daemon\n"
254 " --quiet : suppress output (except errors)\n"
257 " --help : show this help\n"
258 " --no-wait : don't wait for service startup/shutdown to complete\n"
259 " --pin : pin the service in the requested state\n";
263 signal(SIGPIPE, SIG_IGN);
265 control_socket_path = "/dev/dinitctl";
267 // Locate control socket
269 char * userhome = getenv("HOME");
270 if (userhome == nullptr) {
271 struct passwd * pwuid_p = getpwuid(getuid());
272 if (pwuid_p != nullptr) {
273 userhome = pwuid_p->pw_dir;
277 if (userhome != nullptr) {
278 control_socket_str = userhome;
279 control_socket_str += "/.dinitctl";
280 control_socket_path = control_socket_str.c_str();
283 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
288 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
290 perror("dinitctl: socket");
294 struct sockaddr_un * name;
295 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
296 name = (struct sockaddr_un *) malloc(sockaddr_size);
297 if (name == nullptr) {
298 cerr << "dinitctl: Out of memory" << endl;
302 name->sun_family = AF_UNIX;
303 strcpy(name->sun_path, control_socket_path);
305 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
307 perror("dinitctl: connect");
312 // Start by querying protocol version:
314 check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum);
316 if (command == command_t::UNPIN_SERVICE) {
317 return unpin_service(socknum, rbuffer, service_name, verbose);
319 else if (command == command_t::UNLOAD_SERVICE) {
320 return unload_service(socknum, rbuffer, service_name);
322 else if (command == command_t::LIST_SERVICES) {
323 return list_services(socknum, rbuffer);
325 else if (command == command_t::SHUTDOWN) {
326 return shutdown_dinit(socknum, rbuffer);
328 else if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
329 return add_remove_dependency(socknum, rbuffer, command == command_t::ADD_DEPENDENCY,
330 service_name, to_service_name, dep_type);
332 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
333 // If only one service specified, assume that we enable for 'boot' service:
334 if (service_name == nullptr) {
335 service_name = "boot";
337 return enable_disable_service(socknum, rbuffer, service_name, to_service_name,
338 command == command_t::ENABLE_SERVICE);
341 return start_stop_service(socknum, rbuffer, service_name, command, do_pin,
342 wait_for_service, verbose);
345 catch (cp_old_client_exception &e) {
346 std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl;
349 catch (cp_old_server_exception &e) {
350 std::cerr << "dinitctl: server too old or protocol error" << std::endl;
353 catch (cp_read_exception &e) {
354 cerr << "dinitctl: control socket read failure or protocol error" << endl;
357 catch (cp_write_exception &e) {
358 cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl;
363 // Extract/read a string of specified length from the buffer/socket. The string is consumed
365 static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length)
367 int rb_len = rbuffer.get_length();
368 if (rb_len >= length) {
369 std::string r = rbuffer.extract_string(0, length);
370 rbuffer.consume(length);
374 std::string r = rbuffer.extract_string(0, rb_len);
375 uint32_t rlen = length - rb_len;
379 rbuffer.fill(socknum);
380 char *bptr = rbuffer.get_ptr(0);
381 clen = rbuffer.get_length();
382 clen = std::min(clen, rlen);
383 r.append(bptr, clen);
387 rbuffer.consume(clen);
392 // Load a service: issue load command, wait for reply. Return true on success, display error message
393 // and return false on failure.
394 // socknum - the socket fd to communicate via
395 // rbuffer - the buffer for communication
396 // name - the name of the service to load
397 // handle - where to store the handle of the loaded service
398 // state - where to store the state of the loaded service (may be null).
399 static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle,
400 service_state_t *state)
402 // Load 'to' service:
403 if (issue_load_service(socknum, name)) {
407 wait_for_reply(rbuffer, socknum);
409 if (check_load_reply(socknum, rbuffer, handle, state) != 0) {
416 // Start/stop a service
417 static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
418 command_t command, bool do_pin, bool wait_for_service, bool verbose)
422 bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE);
424 service_state_t state;
427 if (! load_service(socknum, rbuffer, service_name, &handle, &state)) {
431 service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
434 case command_t::STOP_SERVICE:
435 pcommand = DINIT_CP_STOPSERVICE;
437 case command_t::RELEASE_SERVICE:
438 pcommand = DINIT_CP_RELEASESERVICE;
440 case command_t::START_SERVICE:
441 pcommand = DINIT_CP_STARTSERVICE;
443 case command_t::WAKE_SERVICE:
444 pcommand = DINIT_CP_WAKESERVICE;
449 // Need to issue STOPSERVICE/STARTSERVICE
450 // We'll do this regardless of the current service state / target state, since issuing
451 // start/stop also sets or clears the "explicitly started" flag on the service.
453 char buf[2 + sizeof(handle)];
455 buf[1] = do_pin ? 1 : 0;
456 memcpy(buf + 2, &handle, sizeof(handle));
457 write_all_x(socknum, buf, 2 + sizeof(handle));
459 wait_for_reply(rbuffer, socknum);
460 if (rbuffer[0] == DINIT_RP_ALREADYSS) {
461 bool already = (state == wanted_state);
463 cout << "Service " << (already ? "(already) " : "")
464 << describeState(do_stop) << "." << endl;
466 return 0; // success!
468 if (rbuffer[0] != DINIT_RP_ACK) {
469 cerr << "dinitctl: Protocol error." << endl;
475 if (! wait_for_service) {
477 cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl;
482 service_event_t completionEvent;
483 service_event_t cancelledEvent;
486 completionEvent = service_event_t::STOPPED;
487 cancelledEvent = service_event_t::STOPCANCELLED;
490 completionEvent = service_event_t::STARTED;
491 cancelledEvent = service_event_t::STARTCANCELLED;
494 // Wait until service started:
495 int r = rbuffer.fill_to(socknum, 2);
497 if (rbuffer[0] >= 100) {
498 int pktlen = (unsigned char) rbuffer[1];
499 fill_buffer_to(rbuffer, socknum, pktlen);
501 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
503 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
504 service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
505 if (ev_handle == handle) {
506 if (event == completionEvent) {
508 cout << "Service " << describeState(do_stop) << "." << endl;
512 else if (event == cancelledEvent) {
514 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
518 else if (! do_stop && event == service_event_t::FAILEDSTART) {
520 cout << "Service failed to start." << endl;
527 rbuffer.consume(pktlen);
528 r = rbuffer.fill_to(socknum, 2);
531 // Not an information packet?
532 cerr << "dinitctl: protocol error" << endl;
538 perror("dinitctl: read");
541 cerr << "protocol error (connection closed by server)" << endl;
546 // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for
547 // a response. Returns 1 on failure (with error logged), 0 on success.
548 static int issue_load_service(int socknum, const char *service_name, bool find_only)
551 uint16_t sname_len = strlen(service_name);
552 int bufsize = 3 + sname_len;
554 std::unique_ptr<char[]> ubuf(new char[bufsize]);
555 auto buf = ubuf.get();
557 buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE;
558 memcpy(buf + 1, &sname_len, 2);
559 memcpy(buf + 3, service_name, sname_len);
561 write_all_x(socknum, buf, bufsize);
566 // Check that a "load service" reply was received, and that the requested service was found.
567 // state_p may be null.
568 static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p)
572 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
573 fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
574 rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
575 if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
576 //target_state = static_cast<service_state_t>(rbuffer[2 + sizeof(handle)]);
577 rbuffer.consume(3 + sizeof(*handle_p));
580 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
581 cerr << "dinitctl: failed to find/load service." << endl;
585 cerr << "dinitctl: protocol error." << endl;
590 static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose)
597 if (! load_service(socknum, rbuffer, service_name, &handle, nullptr)) {
601 // Issue UNPIN command.
603 char buf[1 + sizeof(handle)];
604 buf[0] = DINIT_CP_UNPINSERVICE;
605 memcpy(buf + 1, &handle, sizeof(handle));
606 write_all_x(socknum, buf, 2 + sizeof(handle));
608 wait_for_reply(rbuffer, socknum);
609 if (rbuffer[0] != DINIT_RP_ACK) {
610 cerr << "dinitctl: protocol error." << endl;
617 cout << "Service unpinned." << endl;
622 static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name)
626 if (issue_load_service(socknum, service_name, true) == 1) {
630 wait_for_reply(rbuffer, socknum);
634 if (rbuffer[0] == DINIT_RP_NOSERVICE) {
635 cerr << "dinitctl: service not loaded." << endl;
639 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
643 // Issue UNLOAD command.
645 char buf[1 + sizeof(handle)];
646 buf[0] = DINIT_CP_UNLOADSERVICE;
647 memcpy(buf + 1, &handle, sizeof(handle));
648 write_all_x(socknum, buf, 2 + sizeof(handle));
650 wait_for_reply(rbuffer, socknum);
651 if (rbuffer[0] == DINIT_RP_NAK) {
652 cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of "
653 "other service." << endl;
656 if (rbuffer[0] != DINIT_RP_ACK) {
657 cerr << "dinitctl: Protocol error." << endl;
663 cout << "Service unloaded." << endl;
667 static int list_services(int socknum, cpbuffer_t &rbuffer)
671 char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES };
672 write_all_x(socknum, cmdbuf, 1);
674 wait_for_reply(rbuffer, socknum);
675 while (rbuffer[0] == DINIT_RP_SVCINFO) {
676 int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
677 fill_buffer_to(rbuffer, socknum, hdrsize);
678 int nameLen = rbuffer[1];
679 service_state_t current = static_cast<service_state_t>(rbuffer[2]);
680 service_state_t target = static_cast<service_state_t>(rbuffer[3]);
682 int console_flags = rbuffer[4];
683 bool has_console = (console_flags & 2) != 0;
684 bool waiting_console = (console_flags & 1) != 0;
685 bool was_skipped = (console_flags & 4) != 0;
687 stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[5]);
691 if (current != service_state_t::STOPPED) {
692 rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid));
695 rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
698 fill_buffer_to(rbuffer, socknum, nameLen + hdrsize);
700 char *name_ptr = rbuffer.get_ptr(hdrsize);
701 int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
703 string name = string(name_ptr, clength);
704 name.append(rbuffer.get_buf_base(), nameLen - clength);
708 cout << (target == service_state_t::STARTED ? "{" : " ");
709 if (current == service_state_t::STARTED) {
710 cout << (was_skipped ? "s" : "+");
715 cout << (target == service_state_t::STARTED ? "}" : " ");
717 if (current == service_state_t::STARTING) {
720 else if (current == service_state_t::STOPPING) {
727 cout << (target == service_state_t::STOPPED ? "{" : " ");
728 if (current == service_state_t::STOPPED) {
729 bool did_fail = false;
730 if (stop_reason == stopped_reason_t::TERMINATED) {
731 if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) {
735 else did_fail = (stop_reason != stopped_reason_t::NORMAL);
737 cout << (did_fail ? "X" : "-");
742 cout << (target == service_state_t::STOPPED ? "}" : " ");
744 cout << "] " << name;
746 if (current != service_state_t::STOPPED && service_pid != -1) {
747 cout << " (pid: " << service_pid << ")";
750 if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) {
751 if (WIFEXITED(exit_status)) {
752 cout << " (exit status: " << WEXITSTATUS(exit_status) << ")";
754 else if (WIFSIGNALED(exit_status)) {
755 cout << " (signal: " << WSTOPSIG(exit_status) << ")";
760 cout << " (has console)";
762 else if (waiting_console) {
763 cout << " (waiting for console)";
768 rbuffer.consume(hdrsize + nameLen);
769 wait_for_reply(rbuffer, socknum);
772 if (rbuffer[0] != DINIT_RP_LISTDONE) {
773 cerr << "dinitctl: Control socket protocol error" << endl;
780 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
781 const char *service_from, const char *service_to, dependency_type dep_type)
786 handle_t from_handle;
789 if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr)
790 || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) {
794 constexpr int pktsize = 2 + sizeof(handle_t) * 2;
795 char cmdbuf[pktsize] = { add ? (char)DINIT_CP_ADD_DEP : (char)DINIT_CP_REM_DEP, (char)dep_type};
796 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
797 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
798 write_all_x(socknum, cmdbuf, pktsize);
800 wait_for_reply(rbuffer, socknum);
803 if (rbuffer[0] == DINIT_RP_NAK) {
804 cerr << "dinitctl: Could not add dependency: circular dependency or wrong state" << endl;
807 if (rbuffer[0] != DINIT_RP_ACK) {
808 cerr << "dinitctl: Control socket protocol error" << endl;
815 static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer)
817 // TODO support no-wait option.
821 constexpr int bufsize = 2;
824 buf[0] = DINIT_CP_SHUTDOWN;
825 buf[1] = static_cast<char>(shutdown_type_t::HALT);
827 write_all_x(socknum, buf, bufsize);
829 wait_for_reply(rbuffer, socknum);
831 if (rbuffer[0] != DINIT_RP_ACK) {
832 cerr << "dinitctl: Control socket protocol error" << endl;
836 // Now wait for rollback complete:
839 wait_for_info(rbuffer, socknum);
840 if (rbuffer[0] == DINIT_ROLLBACK_COMPLETED) {
845 catch (cp_read_exception &exc) {
846 // Dinit can terminate before replying: let's assume that happened.
847 // TODO: better check, possibly ensure that dinit actually sends rollback complete before
854 // Join two paths; the 2nd must be relative
855 static std::string join_paths(std::string p1, std::string p2)
858 if (*(r.rbegin()) != '/') {
864 // exception for cancelling a service operation
865 class service_op_cancel { };
867 static int enable_disable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to, bool enable)
871 service_state_t from_state = service_state_t::STARTED;
872 handle_t from_handle;
876 if (! load_service(socknum, rbuffer, from, &from_handle, &from_state)
877 || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) {
881 // Get service load path
882 char buf[1] = { DINIT_CP_QUERY_LOAD_MECH };
883 write_all_x(socknum, buf, 1);
885 wait_for_reply(rbuffer, socknum);
887 if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
888 cerr << "dinitctl: Control socket protocol error" << endl;
892 // Packet type, load mechanism type, packet size:
893 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t));
895 if (rbuffer[1] != SSET_TYPE_DIRLOAD) {
896 cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl;
900 vector<string> paths;
903 rbuffer.extract(&pktsize, 2, sizeof(uint32_t));
905 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length
907 uint32_t path_entries; // number of service directories
908 rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t));
911 rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t));
912 rbuffer.consume(2 + sizeof(uint32_t) * 3);
913 pktsize -= 2 + sizeof(uint32_t) * 3;
915 // Read current working directory of daemon:
916 std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len);
918 // dinit daemon base directory against which service paths are resolved is in dinit_cwd
920 for (int i = 0; i < (int)path_entries; i++) {
922 fill_buffer_to(rbuffer, socknum, sizeof(uint32_t));
923 rbuffer.extract(&plen, 0, sizeof(uint32_t));
924 rbuffer.consume(sizeof(uint32_t));
925 paths.push_back(read_string(socknum, rbuffer, plen));
928 // all service directories are now in the 'paths' vector
929 // Load/read service description for 'from' service:
931 ifstream service_file;
932 string service_file_path;
934 for (std::string path : paths) {
935 string test_path = join_paths(dinit_cwd + '/' + path, from);
937 service_file.open(test_path.c_str(), ios::in);
939 service_file_path = test_path;
944 if (! service_file) {
945 cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl;
949 // We now need to read the service file, identify the waits-for.d directory (bail out if more than one),
950 // make sure the service is not listed as a dependency individually.
955 process_service_file(from, service_file, [&](string &line, string &setting,
956 dinit_load::string_iterator i, dinit_load::string_iterator end) -> void {
957 if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") {
958 string dname = dinit_load::read_setting_value(i, end);
960 // There is already a dependency
961 cerr << "dinitctl: there is a fixed dependency to service '" << to
962 << "' in the service description of '" << from << "'." << endl;
963 throw service_op_cancel();
966 else if (setting == "waits-for.d") {
967 string dname = dinit_load::read_setting_value(i, end);
968 if (! waits_for_d.empty()) {
969 cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories "
970 << "specified in service description" << endl;
971 throw service_op_cancel();
973 waits_for_d = std::move(dname);
977 catch (const service_op_cancel &cexc) {
981 // If the from service has no waits-for.d specified, we can't continue
982 if (waits_for_d.empty()) {
983 cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl;
987 // check if dependency already exists
988 string dep_link_path = join_paths(waits_for_d, to);
989 struct stat stat_buf;
990 if (lstat(dep_link_path.c_str(), &stat_buf) == -1) {
991 if (errno != ENOENT) {
992 cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": "
993 << strerror(errno) << endl;
998 // dependency already exists
1000 cerr << "dinitctl: service already enabled." << endl;
1005 // warn if 'from' service is not started
1006 if (enable && from_state != service_state_t::STARTED) {
1007 cerr << "dinitctl: warning: enabling dependency for non-started service" << endl;
1010 // add/remove dependency
1011 constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2;
1012 char cmdbuf[enable_pktsize] = { char(enable ? DINIT_CP_ENABLESERVICE : DINIT_CP_REM_DEP),
1013 char(dependency_type::WAITS_FOR)};
1014 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
1015 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
1016 write_all_x(socknum, cmdbuf, enable_pktsize);
1018 wait_for_reply(rbuffer, socknum);
1021 if (enable && rbuffer[0] == DINIT_RP_NAK) {
1022 cerr << "dinitctl: Could not enable service: possible circular dependency" << endl;
1025 if (rbuffer[0] != DINIT_RP_ACK) {
1026 cerr << "dinitctl: Control socket protocol error" << endl;
1032 if (symlink(dep_link_path.c_str(), (string("../") + to).c_str()) == -1) {
1033 cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno) << "\n"
1034 "dinitctl: Note: service was activated, but will not be enabled on restart." << endl;
1039 if (unlink(dep_link_path.c_str()) == -1) {
1040 cerr << "dinitctl: Could not unlink dependency entry " << dep_link_path << ": " << strerror(errno) << "\n"
1041 "dinitctl: Note: service was disabled, but will be re-enabled on restart." << endl;