6 #include <system_error>
9 #include <sys/socket.h>
14 #include "control-cmds.h"
15 #include "service-constants.h"
18 // dinitctl: utility to control the Dinit daemon, including starting and stopping of services.
20 // This utility communicates with the dinit daemon via a unix socket (/dev/initctl).
22 using handle_t = uint32_t;
29 ReadCPException(int err) : errcode(err) { }
32 static void fillBufferTo(CPBuffer *buf, int fd, int rlength)
34 int r = buf->fillTo(fd, rlength);
36 throw ReadCPException(errno);
39 throw ReadCPException(0);
43 static const char * describeState(bool stopped)
45 return stopped ? "stopped" : "started";
48 static const char * describeVerb(bool stop)
50 return stop ? "stop" : "start";
53 // Wait for a reply packet, skipping over any information packets
54 // that are received in the meantime.
55 static void wait_for_reply(CPBuffer &rbuffer, int fd)
57 fillBufferTo(&rbuffer, fd, 1);
59 while (rbuffer[0] >= 100) {
60 // Information packet; discard.
61 fillBufferTo(&rbuffer, fd, 1);
62 int pktlen = (unsigned char) rbuffer[1];
64 rbuffer.consume(1); // Consume one byte so we'll read one byte of the next packet
65 fillBufferTo(&rbuffer, fd, pktlen);
66 rbuffer.consume(pktlen - 1);
71 // Write *all* the requested buffer and re-try if necessary until
72 // the buffer is written or an unrecoverable error occurs.
73 static int write_all(int fd, const void *buf, size_t count)
75 const char *cbuf = static_cast<const char *>(buf);
78 int r = write(fd, cbuf, count);
80 if (errno == EINTR) continue;
90 int main(int argc, char **argv)
95 bool show_help = argc < 2;
96 char *service_name = nullptr;
98 std::string control_socket_str;
99 const char * control_socket_path = nullptr;
102 bool sys_dinit = false; // communicate with system daemon
103 bool wait_for_service = true;
107 constexpr int START_SERVICE = 1;
108 constexpr int STOP_SERVICE = 2;
110 for (int i = 1; i < argc; i++) {
111 if (argv[i][0] == '-') {
112 if (strcmp(argv[i], "--help") == 0) {
116 else if (strcmp(argv[i], "--no-wait") == 0) {
117 wait_for_service = false;
119 else if (strcmp(argv[i], "--quiet") == 0) {
122 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
126 cerr << "Unrecognized command-line parameter: " << argv[i] << endl;
130 else if (command == 0) {
131 if (strcmp(argv[i], "start") == 0) {
132 command = START_SERVICE;
134 else if (strcmp(argv[i], "stop") == 0) {
135 command = STOP_SERVICE;
144 service_name = argv[i];
145 // TODO support multiple services (or at least give error if multiple
146 // services supplied)
150 if (service_name == nullptr || command == 0) {
155 cout << "dinit-start: start a dinit service" << endl;
156 cout << " --help : show this help" << endl;
157 cout << " --no-wait : don't wait for service startup/shutdown to complete" << endl;
158 cout << " --quiet : suppress output (except errors)" << endl;
159 cout << " -s, --system : control system daemon instead of user daemon" << endl;
160 cout << " <service-name> : start the named service" << endl;
164 do_stop = (command == STOP_SERVICE);
166 control_socket_path = "/dev/dinitctl";
169 char * userhome = getenv("HOME");
170 if (userhome == nullptr) {
171 struct passwd * pwuid_p = getpwuid(getuid());
172 if (pwuid_p != nullptr) {
173 userhome = pwuid_p->pw_dir;
177 if (userhome != nullptr) {
178 control_socket_str = userhome;
179 control_socket_str += "/.dinitctl";
180 control_socket_path = control_socket_str.c_str();
183 cerr << "Cannot locate user home directory (set HOME or check /etc/passwd file)" << endl;
188 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
194 struct sockaddr_un * name;
195 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + strlen(control_socket_path) + 1;
196 name = (struct sockaddr_un *) malloc(sockaddr_size);
197 if (name == nullptr) {
198 cerr << "dinit-start: out of memory" << endl;
202 name->sun_family = AF_UNIX;
203 strcpy(name->sun_path, control_socket_path);
205 int connr = connect(socknum, (struct sockaddr *) name, sockaddr_size);
211 // TODO should start by querying protocol version
214 uint16_t sname_len = strlen(service_name);
215 int bufsize = 3 + sname_len;
216 char * buf = new char[bufsize];
218 buf[0] = DINIT_CP_LOADSERVICE;
219 memcpy(buf + 1, &sname_len, 2);
220 memcpy(buf + 3, service_name, sname_len);
222 int r = write_all(socknum, buf, bufsize);
229 // Now we expect a reply:
230 // NOTE: should skip over information packets.
234 wait_for_reply(rbuffer, socknum);
237 ServiceState target_state;
240 if (rbuffer[0] == DINIT_RP_SERVICERECORD) {
241 fillBufferTo(&rbuffer, socknum, 2 + sizeof(handle));
242 rbuffer.extract((char *) &handle, 2, sizeof(handle));
243 state = static_cast<ServiceState>(rbuffer[1]);
244 target_state = static_cast<ServiceState>(rbuffer[2 + sizeof(handle)]);
245 rbuffer.consume(3 + sizeof(handle));
247 else if (rbuffer[0] == DINIT_RP_NOSERVICE) {
248 cerr << "Failed to find/load service." << endl;
252 cerr << "Protocol error." << endl;
256 ServiceState wanted_state = do_stop ? ServiceState::STOPPED : ServiceState::STARTED;
257 int command = do_stop ? DINIT_CP_STOPSERVICE : DINIT_CP_STARTSERVICE;
259 // Need to issue STOPSERVICE/STARTSERVICE
260 if (target_state != wanted_state) {
261 buf = new char[2 + sizeof(handle)];
263 buf[1] = 0; // don't pin
264 memcpy(buf + 2, &handle, sizeof(handle));
265 r = write_all(socknum, buf, 2 + sizeof(handle));
273 wait_for_reply(rbuffer, socknum);
274 if (rbuffer[0] != DINIT_RP_ACK) {
275 cerr << "Protocol error." << endl;
281 if (state == wanted_state) {
283 cout << "Service already " << describeState(do_stop) << "." << endl;
285 return 0; // success!
288 if (! wait_for_service) {
292 ServiceEvent completionEvent;
293 ServiceEvent cancelledEvent;
296 completionEvent = ServiceEvent::STOPPED;
297 cancelledEvent = ServiceEvent::STOPCANCELLED;
300 completionEvent = ServiceEvent::STARTED;
301 cancelledEvent = ServiceEvent::STARTCANCELLED;
304 // Wait until service started:
305 r = rbuffer.fillTo(socknum, 2);
307 if (rbuffer[0] >= 100) {
308 int pktlen = (unsigned char) rbuffer[1];
309 fillBufferTo(&rbuffer, socknum, pktlen);
311 if (rbuffer[0] == DINIT_IP_SERVICEEVENT) {
313 rbuffer.extract((char *) &ev_handle, 2, sizeof(ev_handle));
314 ServiceEvent event = static_cast<ServiceEvent>(rbuffer[2 + sizeof(ev_handle)]);
315 if (ev_handle == handle) {
316 if (event == completionEvent) {
318 cout << "Service " << describeState(do_stop) << "." << endl;
322 else if (event == cancelledEvent) {
324 cout << "Service " << describeVerb(do_stop) << " cancelled." << endl;
328 else if (! do_stop && event == ServiceEvent::FAILEDSTART) {
330 cout << "Service failed to start." << endl;
337 rbuffer.consume(pktlen);
338 r = rbuffer.fillTo(socknum, 2);
341 // Not an information packet?
342 cerr << "protocol error" << endl;
351 cerr << "protocol error (connection closed by server)" << endl;
355 catch (ReadCPException &exc) {
356 cerr << "control socket read failure or protocol error" << endl;
359 catch (std::bad_alloc &exc) {
360 cerr << "out of memory" << endl;