6 #include <system_error>
10 #include <sys/socket.h>
16 #include "control-cmds.h"
17 #include "service-constants.h"
19 #include "dinit-client.h"
21 // dinitctl: utility to control the Dinit daemon, including starting and stopping of services.
23 // This utility communicates with the dinit daemon via a unix stream socket (/dev/initctl, or $HOME/.dinitctl).
28 static int issue_load_service(int socknum, const char *service_name, bool find_only = false);
29 static int check_load_reply(int socknum, cpbuffer_t &, handle_t *handle_p, service_state_t *state_p);
30 static int start_stop_service(int socknum, cpbuffer_t &, const char *service_name, command_t command,
31 bool do_pin, bool wait_for_service, bool verbose);
32 static int unpin_service(int socknum, cpbuffer_t &, const char *service_name, bool verbose);
33 static int unload_service(int socknum, cpbuffer_t &, const char *service_name);
34 static int list_services(int socknum, cpbuffer_t &);
35 static int shutdown_dinit(int soclknum, cpbuffer_t &);
38 static const char * describeState(bool stopped)
40 return stopped ? "stopped" : "started";
43 static const char * describeVerb(bool stop)
45 return stop ? "stop" : "start";
48 enum class command_t {
62 int main(int argc, char **argv)
66 bool show_help = argc < 2;
67 char *service_name = nullptr;
69 std::string control_socket_str;
70 const char * control_socket_path = nullptr;
73 bool sys_dinit = false; // communicate with system daemon
74 bool wait_for_service = true;
77 command_t command = command_t::NONE;
79 for (int i = 1; i < argc; i++) {
80 if (argv[i][0] == '-') {
81 if (strcmp(argv[i], "--help") == 0) {
85 else if (strcmp(argv[i], "--no-wait") == 0) {
86 wait_for_service = false;
88 else if (strcmp(argv[i], "--quiet") == 0) {
91 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
94 else if (strcmp(argv[i], "--pin") == 0) {
101 else if (command == command_t::NONE) {
102 if (strcmp(argv[i], "start") == 0) {
103 command = command_t::START_SERVICE;
105 else if (strcmp(argv[i], "wake") == 0) {
106 command = command_t::WAKE_SERVICE;
108 else if (strcmp(argv[i], "stop") == 0) {
109 command = command_t::STOP_SERVICE;
111 else if (strcmp(argv[i], "release") == 0) {
112 command = command_t::RELEASE_SERVICE;
114 else if (strcmp(argv[i], "unpin") == 0) {
115 command = command_t::UNPIN_SERVICE;
117 else if (strcmp(argv[i], "unload") == 0) {
118 command = command_t::UNLOAD_SERVICE;
120 else if (strcmp(argv[i], "list") == 0) {
121 command = command_t::LIST_SERVICES;
123 else if (strcmp(argv[i], "shutdown") == 0) {
124 command = command_t::SHUTDOWN;
133 if (service_name != nullptr) {
137 service_name = argv[i];
138 // TODO support multiple services
142 bool no_service_cmd = (command == command_t::LIST_SERVICES || command == command_t::SHUTDOWN);
144 if (service_name != nullptr && no_service_cmd) {
148 if ((service_name == nullptr && ! no_service_cmd) || command == command_t::NONE) {
153 cout << "dinitctl: control Dinit services" << endl;
155 cout << "\nUsage:" << endl;
156 cout << " dinitctl [options] start [options] <service-name> : start and activate service" << endl;
157 cout << " dinitctl [options] stop [options] <service-name> : stop service and cancel explicit activation" << endl;
158 cout << " dinitctl [options] wake [options] <service-name> : start but do not mark activated" << endl;
159 cout << " dinitctl [options] release [options] <service-name> : release activation, stop if no dependents" << endl;
160 cout << " dinitctl [options] unpin <service-name> : un-pin the service (after a previous pin)" << endl;
161 cout << " dinitctl unload <service-name> : unload the service" << endl;
162 cout << " dinitctl list : list loaded services" << endl;
163 cout << " dinitctl shutdown : stop all services and terminate dinit" << endl;
165 cout << "\nNote: An activated service continues running when its dependents stop." << endl;
167 cout << "\nGeneral options:" << endl;
168 cout << " -s, --system : control system daemon instead of user daemon" << endl;
169 cout << " --quiet : suppress output (except errors)" << endl;
171 cout << "\nCommand options:" << endl;
172 cout << " --help : show this help" << endl;
173 cout << " --no-wait : don't wait for service startup/shutdown to complete" << endl;
174 cout << " --pin : pin the service in the requested (started/stopped) state" << endl;
178 signal(SIGPIPE, SIG_IGN);
180 control_socket_path = "/dev/dinitctl";
182 // Locate control socket
184 char * userhome = getenv("HOME");
185 if (userhome == nullptr) {
186 struct passwd * pwuid_p = getpwuid(getuid());
187 if (pwuid_p != nullptr) {
188 userhome = pwuid_p->pw_dir;
192 if (userhome != nullptr) {
193 control_socket_str = userhome;
194 control_socket_str += "/.dinitctl";
195 control_socket_path = control_socket_str.c_str();
198 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
203 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
205 perror("dinitctl: socket");
209 struct sockaddr_un * name;
210 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
211 name = (struct sockaddr_un *) malloc(sockaddr_size);
212 if (name == nullptr) {
213 cerr << "dinitctl: Out of memory" << endl;
217 name->sun_family = AF_UNIX;
218 strcpy(name->sun_path, control_socket_path);
220 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
222 perror("dinitctl: connect");
227 // Start by querying protocol version:
229 check_protocol_version(0, 0, rbuffer, socknum);
231 if (command == command_t::UNPIN_SERVICE) {
232 return unpin_service(socknum, rbuffer, service_name, verbose);
234 else if (command == command_t::UNLOAD_SERVICE) {
235 return unload_service(socknum, rbuffer, service_name);
237 else if (command == command_t::LIST_SERVICES) {
238 return list_services(socknum, rbuffer);
240 else if (command == command_t::SHUTDOWN) {
241 return shutdown_dinit(socknum, rbuffer);
244 return start_stop_service(socknum, rbuffer, service_name, command, do_pin,
245 wait_for_service, verbose);
248 catch (cp_old_client_exception &e) {
249 std::cerr << "dinitctl: too old (server reports newer protocol version)" << std::endl;
252 catch (cp_old_server_exception &e) {
253 std::cerr << "dinitctl: server too old or protocol error" << std::endl;
256 catch (cp_read_exception &e) {
257 cerr << "dinitctl: control socket read failure or protocol error" << endl;
260 catch (cp_write_exception &e) {
261 cerr << "dinitctl: control socket write error: " << std::strerror(e.errcode) << endl;
266 // Start/stop a service
267 static int start_stop_service(int socknum, cpbuffer_t &rbuffer, const char *service_name,
268 command_t command, bool do_pin, bool wait_for_service, bool verbose)
272 bool do_stop = (command == command_t::STOP_SERVICE || command == command_t::RELEASE_SERVICE);
274 if (issue_load_service(socknum, service_name)) {
278 // Now we expect a reply:
280 wait_for_reply(rbuffer, socknum);
282 service_state_t state;
283 //service_state_t target_state;
286 if (check_load_reply(socknum, rbuffer, &handle, &state) != 0) {
290 service_state_t wanted_state = do_stop ? service_state_t::STOPPED : service_state_t::STARTED;
293 case command_t::STOP_SERVICE:
294 pcommand = DINIT_CP_STOPSERVICE;
296 case command_t::RELEASE_SERVICE:
297 pcommand = DINIT_CP_RELEASESERVICE;
299 case command_t::START_SERVICE:
300 pcommand = DINIT_CP_STARTSERVICE;
302 case command_t::WAKE_SERVICE:
303 pcommand = DINIT_CP_WAKESERVICE;
308 // Need to issue STOPSERVICE/STARTSERVICE
309 // We'll do this regardless of the current service state / target state, since issuing
310 // start/stop also sets or clears the "explicitly started" flag on the service.
312 char buf[2 + sizeof(handle)];
314 buf[1] = do_pin ? 1 : 0;
315 memcpy(buf + 2, &handle, sizeof(handle));
316 write_all_x(socknum, buf, 2 + sizeof(handle));
318 wait_for_reply(rbuffer, socknum);
319 if (rbuffer[0] == DINIT_RP_ALREADYSS) {
320 bool already = (state == wanted_state);
322 cout << "Service " << (already ? "(already) " : "") << describeState(do_stop) << "." << endl;
324 return 0; // success!
326 if (rbuffer[0] != DINIT_RP_ACK) {
327 cerr << "dinitctl: Protocol error." << endl;
333 if (! wait_for_service) {
335 cout << "Issued " << describeVerb(do_stop) << " command successfully." << endl;
340 service_event_t completionEvent;
341 service_event_t cancelledEvent;
344 completionEvent = service_event_t::STOPPED;
345 cancelledEvent = service_event_t::STOPCANCELLED;
348 completionEvent = service_event_t::STARTED;
349 cancelledEvent = service_event_t::STARTCANCELLED;
352 // Wait until service started:
353 int r = rbuffer.fill_to(socknum, 2);
355 if (rbuffer[0] >= 100) {
356 int pktlen = (unsigned char) rbuffer[1];
357 fill_buffer_to(rbuffer, socknum, pktlen);
359 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
361 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
362 service_event_t event = static_cast<service_event_t>(rbuffer[2 + sizeof(ev_handle)]);
363 if (ev_handle == handle) {
364 if (event == completionEvent) {
366 cout << "Service " << describeState(do_stop) << "." << endl;
370 else if (event == cancelledEvent) {
372 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
376 else if (! do_stop && event == service_event_t::FAILEDSTART) {
378 cout << "Service failed to start." << endl;
385 rbuffer.consume(pktlen);
386 r = rbuffer.fill_to(socknum, 2);
389 // Not an information packet?
390 cerr << "dinitctl: protocol error" << endl;
396 perror("dinitctl: read");
399 cerr << "protocol error (connection closed by server)" << endl;
404 // Issue a "load service" command (DINIT_CP_LOADSERVICE), without waiting for
405 // a response. Returns 1 on failure (with error logged), 0 on success.
406 static int issue_load_service(int socknum, const char *service_name, bool find_only)
409 uint16_t sname_len = strlen(service_name);
410 int bufsize = 3 + sname_len;
412 std::unique_ptr<char[]> ubuf(new char[bufsize]);
413 auto buf = ubuf.get();
415 buf[0] = find_only ? DINIT_CP_FINDSERVICE : DINIT_CP_LOADSERVICE;
416 memcpy(buf + 1, &sname_len, 2);
417 memcpy(buf + 3, service_name, sname_len);
419 write_all_x(socknum, buf, bufsize);
424 // Check that a "load service" reply was received, and that the requested service was found.
425 static int check_load_reply(int socknum, cpbuffer_t &rbuffer, handle_t *handle_p, service_state_t *state_p)
429 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
430 fill_buffer_to(rbuffer, socknum, 2 + sizeof(*handle_p));
431 rbuffer.extract((char *) handle_p, 2, sizeof(*handle_p));
432 if (state_p) *state_p = static_cast<service_state_t>(rbuffer[1]);
433 //target_state = static_cast<service_state_t>(rbuffer[2 + sizeof(handle)]);
434 rbuffer.consume(3 + sizeof(*handle_p));
437 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
438 cerr << "dinitctl: failed to find/load service." << endl;
442 cerr << "dinitctl: protocol error." << endl;
447 static int unpin_service(int socknum, cpbuffer_t &rbuffer, const char *service_name, bool verbose)
452 if (issue_load_service(socknum, service_name) == 1) {
456 // Now we expect a reply:
458 wait_for_reply(rbuffer, socknum);
462 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
466 // Issue UNPIN command.
468 char buf[1 + sizeof(handle)];
469 buf[0] = DINIT_CP_UNPINSERVICE;
470 memcpy(buf + 1, &handle, sizeof(handle));
471 write_all_x(socknum, buf, 2 + sizeof(handle));
473 wait_for_reply(rbuffer, socknum);
474 if (rbuffer[0] != DINIT_RP_ACK) {
475 cerr << "dinitctl: protocol error." << endl;
482 cout << "Service unpinned." << endl;
487 static int unload_service(int socknum, cpbuffer_t &rbuffer, const char *service_name)
492 if (issue_load_service(socknum, service_name, true) == 1) {
496 // Now we expect a reply:
498 wait_for_reply(rbuffer, socknum);
502 if (check_load_reply(socknum, rbuffer, &handle, nullptr) != 0) {
506 // Issue UNLOAD command.
508 char buf[1 + sizeof(handle)];
509 buf[0] = DINIT_CP_UNLOADSERVICE;
510 memcpy(buf + 1, &handle, sizeof(handle));
511 write_all_x(socknum, buf, 2 + sizeof(handle));
513 wait_for_reply(rbuffer, socknum);
514 if (rbuffer[0] == DINIT_RP_NAK) {
515 cerr << "dinitctl: Could not unload service; service not stopped, or is a dependency of "
516 "other service." << endl;
519 if (rbuffer[0] != DINIT_RP_ACK) {
520 cerr << "dinitctl: Protocol error." << endl;
526 cout << "Service unloaded." << endl;
530 static int list_services(int socknum, cpbuffer_t &rbuffer)
534 char cmdbuf[] = { (char)DINIT_CP_LISTSERVICES };
535 write_all_x(socknum, cmdbuf, 1);
537 wait_for_reply(rbuffer, socknum);
538 while (rbuffer[0] == DINIT_RP_SVCINFO) {
539 fill_buffer_to(rbuffer, socknum, 8);
540 int nameLen = rbuffer[1];
541 service_state_t current = static_cast<service_state_t>(rbuffer[2]);
542 service_state_t target = static_cast<service_state_t>(rbuffer[3]);
544 fill_buffer_to(rbuffer, socknum, nameLen + 8);
546 char *name_ptr = rbuffer.get_ptr(8);
547 int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
549 string name = string(name_ptr, clength);
550 name.append(rbuffer.get_buf_base(), nameLen - clength);
554 cout << (target == service_state_t::STARTED ? "{" : " ");
555 cout << (current == service_state_t::STARTED ? "+" : " ");
556 cout << (target == service_state_t::STARTED ? "}" : " ");
558 if (current == service_state_t::STARTING) {
561 else if (current == service_state_t::STOPPING) {
568 cout << (target == service_state_t::STOPPED ? "{" : " ");
569 cout << (current == service_state_t::STOPPED ? "-" : " ");
570 cout << (target == service_state_t::STOPPED ? "}" : " ");
572 cout << "] " << name << endl;
574 rbuffer.consume(8 + nameLen);
575 wait_for_reply(rbuffer, socknum);
578 if (rbuffer[0] != DINIT_RP_LISTDONE) {
579 cerr << "dinitctl: Control socket protocol error" << endl;
586 static int shutdown_dinit(int socknum, cpbuffer_t &rbuffer)
588 // TODO support no-wait option.
592 constexpr int bufsize = 2;
595 buf[0] = DINIT_CP_SHUTDOWN;
596 buf[1] = static_cast<char>(shutdown_type_t::HALT);
598 write_all_x(socknum, buf, bufsize);
600 wait_for_reply(rbuffer, socknum);
602 if (rbuffer[0] != DINIT_RP_ACK) {
603 cerr << "dinitctl: Control socket protocol error" << endl;
607 // Now wait for rollback complete:
610 wait_for_info(rbuffer, socknum);
611 if (rbuffer[0] == DINIT_ROLLBACK_COMPLETED) {
616 catch (cp_read_exception &exc) {
617 // Dinit can terminate before replying: let's assume that happened.
618 // TODO: better check, possibly ensure that dinit actually sends rollback complete before