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_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to);
47 static const char * describeState(bool stopped)
49 return stopped ? "stopped" : "started";
52 static const char * describeVerb(bool stop)
54 return stop ? "stop" : "start";
57 enum class command_t {
75 int main(int argc, char **argv)
79 bool show_help = argc < 2;
80 const char *service_name = nullptr;
81 const char *to_service_name = nullptr;
82 dependency_type dep_type;
83 bool dep_type_set = false;
85 std::string control_socket_str;
86 const char * control_socket_path = nullptr;
89 bool sys_dinit = false; // communicate with system daemon
90 bool wait_for_service = true;
93 command_t command = command_t::NONE;
95 for (int i = 1; i < argc; i++) {
96 if (argv[i][0] == '-') {
97 if (strcmp(argv[i], "--help") == 0) {
101 else if (strcmp(argv[i], "--no-wait") == 0) {
102 wait_for_service = false;
104 else if (strcmp(argv[i], "--quiet") == 0) {
107 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
110 else if (strcmp(argv[i], "--pin") == 0) {
113 else if ((command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE)
114 && strcmp(argv[i], "--from") == 0) {
117 cerr << "dinitctl: --from should be followed by a service name" << std::endl;
120 service_name = argv[i];
123 cerr << "dinitctl: unrecognized option: " << argv[i] << " (use --help for help)\n";
127 else if (command == command_t::NONE) {
128 if (strcmp(argv[i], "start") == 0) {
129 command = command_t::START_SERVICE;
131 else if (strcmp(argv[i], "wake") == 0) {
132 command = command_t::WAKE_SERVICE;
134 else if (strcmp(argv[i], "stop") == 0) {
135 command = command_t::STOP_SERVICE;
137 else if (strcmp(argv[i], "release") == 0) {
138 command = command_t::RELEASE_SERVICE;
140 else if (strcmp(argv[i], "unpin") == 0) {
141 command = command_t::UNPIN_SERVICE;
143 else if (strcmp(argv[i], "unload") == 0) {
144 command = command_t::UNLOAD_SERVICE;
146 else if (strcmp(argv[i], "list") == 0) {
147 command = command_t::LIST_SERVICES;
149 else if (strcmp(argv[i], "shutdown") == 0) {
150 command = command_t::SHUTDOWN;
152 else if (strcmp(argv[i], "add-dep") == 0) {
153 command = command_t::ADD_DEPENDENCY;
155 else if (strcmp(argv[i], "rm-dep") == 0) {
156 command = command_t::RM_DEPENDENCY;
158 else if (strcmp(argv[i], "enable") == 0) {
159 command = command_t::ENABLE_SERVICE;
162 cerr << "dinitctl: unrecognized command: " << argv[i] << " (use --help for help)\n";
167 // service name / other non-option
168 if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
169 if (! dep_type_set) {
170 if (strcmp(argv[i], "regular") == 0) {
171 dep_type = dependency_type::REGULAR;
173 else if (strcmp(argv[i], "milestone") == 0) {
174 dep_type = dependency_type::MILESTONE;
176 else if (strcmp(argv[i], "waits-for") == 0) {
177 dep_type = dependency_type::WAITS_FOR;
185 else if (service_name == nullptr) {
186 service_name = argv[i];
188 else if (to_service_name == nullptr) {
189 to_service_name = argv[i];
196 else if (command == command_t::ENABLE_SERVICE || command == command_t::DISABLE_SERVICE) {
197 if (to_service_name != nullptr) {
201 to_service_name = argv[i];
204 if (service_name != nullptr) {
208 service_name = argv[i];
209 // TODO support multiple services
214 bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN);
216 if (service_name != nullptr && no_service_cmd) {
220 if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) {
224 if ((command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY)
225 && (! dep_type_set || service_name == nullptr || to_service_name == nullptr)) {
230 cout << "dinitctl: control Dinit services\n"
233 " dinitctl [options] start [options] <service-name>\n"
234 " dinitctl [options] stop [options] <service-name>\n"
235 " dinitctl [options] wake [options] <service-name>\n"
236 " dinitctl [options] release [options] <service-name>\n"
237 " dinitctl [options] unpin <service-name>\n"
238 " dinitctl unload <service-name>\n"
240 " dinitctl shutdown\n"
241 " dinitctl add-dep <type> <from-service> <to-service>\n"
242 " dinitctl rm-dep <type> <from-service> <to-service>\n"
244 "Note: An activated service continues running when its dependents stop.\n"
247 " -s, --system : control system daemon instead of user daemon\n"
248 " --quiet : suppress output (except errors)\n"
251 " --help : show this help\n"
252 " --no-wait : don't wait for service startup/shutdown to complete\n"
253 " --pin : pin the service in the requested state\n";
257 signal(SIGPIPE, SIG_IGN);
259 control_socket_path = "/dev/dinitctl";
261 // Locate control socket
263 char * userhome = getenv("HOME");
264 if (userhome == nullptr) {
265 struct passwd * pwuid_p = getpwuid(getuid());
266 if (pwuid_p != nullptr) {
267 userhome = pwuid_p->pw_dir;
271 if (userhome != nullptr) {
272 control_socket_str = userhome;
273 control_socket_str += "/.dinitctl";
274 control_socket_path = control_socket_str.c_str();
277 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
282 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
284 perror("dinitctl: socket");
288 struct sockaddr_un * name;
289 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
290 name = (struct sockaddr_un *) malloc(sockaddr_size);
291 if (name == nullptr) {
292 cerr << "dinitctl: Out of memory" << endl;
296 name->sun_family = AF_UNIX;
297 strcpy(name->sun_path, control_socket_path);
299 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
301 perror("dinitctl: connect");
306 // Start by querying protocol version:
308 check_protocol_version(min_cp_version, max_cp_version, rbuffer, socknum);
310 if (command == command_t::UNPIN_SERVICE) {
311 return unpin_service(socknum, rbuffer, service_name, verbose);
313 else if (command == command_t::UNLOAD_SERVICE) {
314 return unload_service(socknum, rbuffer, service_name);
316 else if (command == command_t::LIST_SERVICES) {
317 return list_services(socknum, rbuffer);
319 else if (command == command_t::SHUTDOWN) {
320 return shutdown_dinit(socknum, rbuffer);
322 else if (command == command_t::ADD_DEPENDENCY || command == command_t::RM_DEPENDENCY) {
323 return add_remove_dependency(socknum, rbuffer, command == command_t::ADD_DEPENDENCY,
324 service_name, to_service_name, dep_type);
326 else if (command == command_t::ENABLE_SERVICE) {
327 // If only one service specified, assume that we enable for 'boot' service:
328 if (service_name == nullptr) {
329 service_name = "boot";
331 return enable_service(socknum, rbuffer, service_name, to_service_name);
334 return start_stop_service(socknum, rbuffer, service_name, command, do_pin,
335 wait_for_service, verbose);
338 catch (cp_old_client_exception &e) {
339 std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl;
342 catch (cp_old_server_exception &e) {
343 std::cerr << "dinitctl: server too old or protocol error" << std::endl;
346 catch (cp_read_exception &e) {
347 cerr << "dinitctl: control socket read failure or protocol error" << endl;
350 catch (cp_write_exception &e) {
351 cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl;
356 // Extract/read a string of specified length from the buffer/socket. The string is consumed
358 static std::string read_string(int socknum, cpbuffer_t &rbuffer, uint32_t length)
360 int rb_len = rbuffer.get_length();
361 if (rb_len >= length) {
362 std::string r = rbuffer.extract_string(0, length);
363 rbuffer.consume(length);
367 std::string r = rbuffer.extract_string(0, rb_len);
368 uint32_t rlen = length - rb_len;
372 rbuffer.fill(socknum);
373 char *bptr = rbuffer.get_ptr(0);
374 clen = rbuffer.get_length();
375 clen = std::min(clen, rlen);
376 r.append(bptr, clen);
380 rbuffer.consume(clen);
385 // Load a service: issue load command, wait for reply. Return true on success, display error message
386 // and return false on failure.
387 // socknum - the socket fd to communicate via
388 // rbuffer - the buffer for communication
389 // name - the name of the service to load
390 // handle - where to store the handle of the loaded service
391 // state - where to store the state of the loaded service (may be null).
392 static bool load_service(int socknum, cpbuffer_t &rbuffer, const char *name, handle_t *handle, service_state_t *state)
394 // Load 'to' service:
395 if (issue_load_service(socknum, name)) {
399 wait_for_reply(rbuffer, socknum);
401 if (check_load_reply(socknum, rbuffer, handle, state) != 0) {
408 // Start/stop a service
409 static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
410 command_t command, bool do_pin, bool wait_for_service, bool verbose)
414 bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE);
416 service_state_t state;
419 if (! load_service(socknum, rbuffer, service_name, &handle, &state)) {
423 service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
426 case command_t::STOP_SERVICE:
427 pcommand = DINIT_CP_STOPSERVICE;
429 case command_t::RELEASE_SERVICE:
430 pcommand = DINIT_CP_RELEASESERVICE;
432 case command_t::START_SERVICE:
433 pcommand = DINIT_CP_STARTSERVICE;
435 case command_t::WAKE_SERVICE:
436 pcommand = DINIT_CP_WAKESERVICE;
441 // Need to issue STOPSERVICE/STARTSERVICE
442 // We'll do this regardless of the current service state / target state, since issuing
443 // start/stop also sets or clears the "explicitly started" flag on the service.
445 char buf[2 + sizeof(handle)];
447 buf[1] = do_pin ? 1 : 0;
448 memcpy(buf + 2, &handle, sizeof(handle));
449 write_all_x(socknum, buf, 2 + sizeof(handle));
451 wait_for_reply(rbuffer, socknum);
452 if (rbuffer[0] == DINIT_RP_ALREADYSS) {
453 bool already = (state == wanted_state);
455 cout << "Service " << (already ? "(already) " : "")
456 << describeState(do_stop) << "." << endl;
458 return 0; // success!
460 if (rbuffer[0] != DINIT_RP_ACK) {
461 cerr << "dinitctl: Protocol error." << endl;
467 if (! wait_for_service) {
469 cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl;
474 service_event_t completionEvent;
475 service_event_t cancelledEvent;
478 completionEvent = service_event_t::STOPPED;
479 cancelledEvent = service_event_t::STOPCANCELLED;
482 completionEvent = service_event_t::STARTED;
483 cancelledEvent = service_event_t::STARTCANCELLED;
486 // Wait until service started:
487 int r = rbuffer.fill_to(socknum, 2);
489 if (rbuffer[0] >= 100) {
490 int pktlen = (unsigned char) rbuffer[1];
491 fill_buffer_to(rbuffer, socknum, pktlen);
493 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
495 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
496 service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
497 if (ev_handle == handle) {
498 if (event == completionEvent) {
500 cout << "Service " << describeState(do_stop) << "." << endl;
504 else if (event == cancelledEvent) {
506 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
510 else if (! do_stop && event == service_event_t::FAILEDSTART) {
512 cout << "Service failed to start." << endl;
519 rbuffer.consume(pktlen);
520 r = rbuffer.fill_to(socknum, 2);
523 // Not an information packet?
524 cerr << "dinitctl: protocol error" << endl;
530 perror("dinitctl: read");
533 cerr << "protocol error (connection closed by server)" << endl;
538 // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for
539 // a response. Returns 1 on failure (with error logged), 0 on success.
540 static int issue_load_service(int socknum, const char *service_name, bool find_only)
543 uint16_t sname_len = strlen(service_name);
544 int bufsize = 3 + sname_len;
546 std::unique_ptr<char[]> ubuf(new char[bufsize]);
547 auto buf = ubuf.get();
549 buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE;
550 memcpy(buf + 1, &sname_len, 2);
551 memcpy(buf + 3, service_name, sname_len);
553 write_all_x(socknum, buf, bufsize);
558 // Check that a "load service" reply was received, and that the requested service was found.
559 // state_p may be null.
560 static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p)
564 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
565 fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
566 rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
567 if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
568 //target_state = static_cast<service_state_t>(rbuffer[2 + sizeof(handle)]);
569 rbuffer.consume(3 + sizeof(*handle_p));
572 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
573 cerr << "dinitctl: failed to find/load service." << endl;
577 cerr << "dinitctl: protocol error." << endl;
582 static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose)
589 if (! load_service(socknum, rbuffer, service_name, &handle, nullptr)) {
593 // Issue UNPIN command.
595 char buf[1 + sizeof(handle)];
596 buf[0] = DINIT_CP_UNPINSERVICE;
597 memcpy(buf + 1, &handle, sizeof(handle));
598 write_all_x(socknum, buf, 2 + sizeof(handle));
600 wait_for_reply(rbuffer, socknum);
601 if (rbuffer[0] != DINIT_RP_ACK) {
602 cerr << "dinitctl: protocol error." << endl;
609 cout << "Service unpinned." << endl;
614 static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name)
618 if (issue_load_service(socknum, service_name, true) == 1) {
622 wait_for_reply(rbuffer, socknum);
626 if (rbuffer[0] == DINIT_RP_NOSERVICE) {
627 cerr << "dinitctl: service not loaded." << endl;
631 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
635 // Issue UNLOAD command.
637 char buf[1 + sizeof(handle)];
638 buf[0] = DINIT_CP_UNLOADSERVICE;
639 memcpy(buf + 1, &handle, sizeof(handle));
640 write_all_x(socknum, buf, 2 + sizeof(handle));
642 wait_for_reply(rbuffer, socknum);
643 if (rbuffer[0] == DINIT_RP_NAK) {
644 cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of "
645 "other service." << endl;
648 if (rbuffer[0] != DINIT_RP_ACK) {
649 cerr << "dinitctl: Protocol error." << endl;
655 cout << "Service unloaded." << endl;
659 static int list_services(int socknum, cpbuffer_t &rbuffer)
663 char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES };
664 write_all_x(socknum, cmdbuf, 1);
666 wait_for_reply(rbuffer, socknum);
667 while (rbuffer[0] == DINIT_RP_SVCINFO) {
668 int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
669 fill_buffer_to(rbuffer, socknum, hdrsize);
670 int nameLen = rbuffer[1];
671 service_state_t current = static_cast<service_state_t>(rbuffer[2]);
672 service_state_t target = static_cast<service_state_t>(rbuffer[3]);
674 int console_flags = rbuffer[4];
675 bool has_console = (console_flags & 2) != 0;
676 bool waiting_console = (console_flags & 1) != 0;
677 bool was_skipped = (console_flags & 4) != 0;
679 stopped_reason_t stop_reason = static_cast<stopped_reason_t>(rbuffer[5]);
683 if (current != service_state_t::STOPPED) {
684 rbuffer.extract((char *)&service_pid, 8, sizeof(service_pid));
687 rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
690 fill_buffer_to(rbuffer, socknum, nameLen + hdrsize);
692 char *name_ptr = rbuffer.get_ptr(hdrsize);
693 int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
695 string name = string(name_ptr, clength);
696 name.append(rbuffer.get_buf_base(), nameLen - clength);
700 cout << (target == service_state_t::STARTED ? "{" : " ");
701 if (current == service_state_t::STARTED) {
702 cout << (was_skipped ? "s" : "+");
707 cout << (target == service_state_t::STARTED ? "}" : " ");
709 if (current == service_state_t::STARTING) {
712 else if (current == service_state_t::STOPPING) {
719 cout << (target == service_state_t::STOPPED ? "{" : " ");
720 if (current == service_state_t::STOPPED) {
721 bool did_fail = false;
722 if (stop_reason == stopped_reason_t::TERMINATED) {
723 if (!WIFEXITED(exit_status) || WEXITSTATUS(exit_status) != 0) {
727 else did_fail = (stop_reason != stopped_reason_t::NORMAL);
729 cout << (did_fail ? "X" : "-");
734 cout << (target == service_state_t::STOPPED ? "}" : " ");
736 cout << "] " << name;
738 if (current != service_state_t::STOPPED && service_pid != -1) {
739 cout << " (pid: " << service_pid << ")";
742 if (current == service_state_t::STOPPED && stop_reason == stopped_reason_t::TERMINATED) {
743 if (WIFEXITED(exit_status)) {
744 cout << " (exit status: " << WEXITSTATUS(exit_status) << ")";
746 else if (WIFSIGNALED(exit_status)) {
747 cout << " (signal: " << WSTOPSIG(exit_status) << ")";
752 cout << " (has console)";
754 else if (waiting_console) {
755 cout << " (waiting for console)";
760 rbuffer.consume(hdrsize + nameLen);
761 wait_for_reply(rbuffer, socknum);
764 if (rbuffer[0] != DINIT_RP_LISTDONE) {
765 cerr << "dinitctl: Control socket protocol error" << endl;
772 static int add_remove_dependency(int socknum, cpbuffer_t &rbuffer, bool add,
773 const char *service_from, const char *service_to, dependency_type dep_type)
778 handle_t from_handle;
781 if (! load_service(socknum, rbuffer, service_from, &from_handle, nullptr)
782 || ! load_service(socknum, rbuffer, service_to, &to_handle, nullptr)) {
786 constexpr int pktsize = 2 + sizeof(handle_t) * 2;
787 char cmdbuf[pktsize] = { add ? (char)DINIT_CP_ADD_DEP : (char)DINIT_CP_REM_DEP, (char)dep_type};
788 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
789 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
790 write_all_x(socknum, cmdbuf, pktsize);
792 wait_for_reply(rbuffer, socknum);
795 if (rbuffer[0] == DINIT_RP_NAK) {
796 cerr << "dinitctl: Could not add dependency: circular dependency or wrong state" << endl;
799 if (rbuffer[0] != DINIT_RP_ACK) {
800 cerr << "dinitctl: Control socket protocol error" << endl;
807 static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer)
809 // TODO support no-wait option.
813 constexpr int bufsize = 2;
816 buf[0] = DINIT_CP_SHUTDOWN;
817 buf[1] = static_cast<char>(shutdown_type_t::HALT);
819 write_all_x(socknum, buf, bufsize);
821 wait_for_reply(rbuffer, socknum);
823 if (rbuffer[0] != DINIT_RP_ACK) {
824 cerr << "dinitctl: Control socket protocol error" << endl;
828 // Now wait for rollback complete:
831 wait_for_info(rbuffer, socknum);
832 if (rbuffer[0] == DINIT_ROLLBACK_COMPLETED) {
837 catch (cp_read_exception &exc) {
838 // Dinit can terminate before replying: let's assume that happened.
839 // TODO: better check, possibly ensure that dinit actually sends rollback complete before
846 // Join two paths; the 2nd must be relative
847 static std::string join_paths(std::string p1, std::string p2)
850 if (*(r.rbegin()) != '/') {
856 // exception for cancelling a service operation
857 class service_op_cancel { };
859 static int enable_service(int socknum, cpbuffer_t &rbuffer, const char *from, const char *to)
863 service_state_t from_state = service_state_t::STARTED;
864 handle_t from_handle;
868 if (! load_service(socknum, rbuffer, from, &from_handle, &from_state)
869 || ! load_service(socknum, rbuffer, to, &to_handle, nullptr)) {
873 // Get service load path
874 char buf[1] = { DINIT_CP_QUERY_LOAD_MECH };
875 write_all_x(socknum, buf, 1);
877 wait_for_reply(rbuffer, socknum);
879 if (rbuffer[0] != DINIT_RP_LOADER_MECH) {
880 cerr << "dinitctl: Control socket protocol error" << endl;
884 // Packet type, load mechanism type, packet size:
885 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t));
887 if (rbuffer[1] != SSET_TYPE_DIRLOAD) {
888 cerr << "dinitctl: unknown configuration, unable to load service descriptions" << endl;
892 vector<string> paths;
895 rbuffer.extract(&pktsize, 2, sizeof(uint32_t));
897 fill_buffer_to(rbuffer, socknum, 2 + sizeof(uint32_t) * 3); // path entries, cwd length
899 uint32_t path_entries; // number of service directories
900 rbuffer.extract(&path_entries, 2 + sizeof(uint32_t), sizeof(uint32_t));
903 rbuffer.extract(&cwd_len, 2 + sizeof(uint32_t) * 2, sizeof(uint32_t));
904 rbuffer.consume(2 + sizeof(uint32_t) * 3);
905 pktsize -= 2 + sizeof(uint32_t) * 3;
907 // Read current working directory of daemon:
908 std::string dinit_cwd = read_string(socknum, rbuffer, cwd_len);
910 // dinit daemon base directory against which service paths are resolved is in dinit_cwd
912 for (int i = 0; i < (int)path_entries; i++) {
914 fill_buffer_to(rbuffer, socknum, sizeof(uint32_t));
915 rbuffer.extract(&plen, 0, sizeof(uint32_t));
916 rbuffer.consume(sizeof(uint32_t));
917 paths.push_back(read_string(socknum, rbuffer, plen));
920 // all service directories are now in the 'paths' vector
921 // Load/read service description for 'from' service:
923 ifstream service_file;
924 string service_file_path;
926 for (std::string path : paths) {
927 string test_path = join_paths(dinit_cwd + '/' + path, from);
929 service_file.open(test_path.c_str(), ios::in);
931 service_file_path = test_path;
936 if (! service_file) {
937 cerr << "dinitctl: could not locate service file for service '" << from << "'" << endl;
941 // We now need to read the service file, identify the waits-for.d directory (bail out if more than one),
942 // make sure the service is not listed as a dependency individually.
947 process_service_file(from, service_file, [&](string &line, string &setting,
948 dinit_load::string_iterator i, dinit_load::string_iterator end) -> void {
949 if (setting == "waits-for" || setting == "depends-on" || setting == "depends-ms") {
950 string dname = dinit_load::read_setting_value(i, end);
952 // There is already a dependency
953 cerr << "dinitctl: there is a fixed dependency to service '" << to
954 << "' in the service description of '" << from << "'." << endl;
955 throw service_op_cancel();
958 else if (setting == "waits-for.d") {
959 string dname = dinit_load::read_setting_value(i, end);
960 if (! waits_for_d.empty()) {
961 cerr << "dinitctl: service '" << from << "' has multiple waits-for.d directories "
962 << "specified in service description" << endl;
963 throw service_op_cancel();
965 waits_for_d = std::move(dname);
969 catch (const service_op_cancel &cexc) {
973 // If the from service has no waits-for.d specified, we can't continue
974 if (waits_for_d.empty()) {
975 cerr << "dinitctl: service '" << from << "' has no waits-for.d directory specified" << endl;
979 // check if dependency already exists
980 string dep_link_path = join_paths(waits_for_d, to);
981 struct stat stat_buf;
982 if (lstat(dep_link_path.c_str(), &stat_buf) == -1) {
983 if (errno != ENOENT) {
984 cerr << "dinitctl: checking for existing dependency link: " << dep_link_path << ": "
985 << strerror(errno) << endl;
990 // dependency already exists
991 cerr << "dinitctl: service already enabled." << endl;
995 // warn if 'from' service is not started
996 if (from_state != service_state_t::STARTED) {
997 cerr << "dinitctl: warning: enabling dependency for non-started service" << endl;
1001 constexpr int enable_pktsize = 2 + sizeof(handle_t) * 2;
1002 char cmdbuf[enable_pktsize] = { char(DINIT_CP_ENABLESERVICE), char(dependency_type::WAITS_FOR)};
1003 memcpy(cmdbuf + 2, &from_handle, sizeof(from_handle));
1004 memcpy(cmdbuf + 2 + sizeof(from_handle), &to_handle, sizeof(to_handle));
1005 write_all_x(socknum, cmdbuf, enable_pktsize);
1007 wait_for_reply(rbuffer, socknum);
1010 if (rbuffer[0] == DINIT_RP_NAK) {
1011 cerr << "dinitctl: Could not enable service: possible circular dependency" << endl;
1014 if (rbuffer[0] != DINIT_RP_ACK) {
1015 cerr << "dinitctl: Control socket protocol error" << endl;
1020 if (symlink(dep_link_path.c_str(), (string("../") + to).c_str()) == -1) {
1021 cerr << "dinitctl: Could not create symlink at " << dep_link_path << ": " << strerror(errno) << "\n"
1022 "dinitctl: Note: service was activated, but will not be enabled on restart." << endl;