2 #include <sys/socket.h>
4 // #include <netinet/in.h>
11 // dinit-start: utility to start a dinit service
13 // This utility communicates with the dinit daemon via a unix socket (/dev/initctl).
15 // TODO move these into a common include file:
16 constexpr static int DINIT_CP_STARTSERVICE = 0;
17 constexpr static int DINIT_CP_STOPSERVICE = 1;
20 int main(int argc, char **argv)
24 bool show_help = argc < 2;
25 char *service_name = nullptr;
27 for (int i = 1; i < argc; i++) {
28 if (argv[i][0] == '-') {
29 if (strcmp(argv[i], "--help") == 0) {
34 cerr << "Unrecognized command-line parameter: " << argv[i] << endl;
40 service_name = argv[i];
41 // TODO support multiple services (or at least give error if multiple services
47 cout << "dinit-start: start a dinit service" << endl;
48 cout << " --help : show this help" << endl;
49 cout << " <service-name> : start the named service" << endl;
53 int socknum = socket(AF_UNIX, SOCK_STREAM, 0);
59 const char *naddr = "/dev/dinitctl";
61 struct sockaddr_un name;
62 name.sun_family = AF_UNIX;
63 // memset(name.sun_path, 0, sizeof(name.sun_path));
64 strcpy(name.sun_path /* + 1 */, naddr);
65 int sunlen = 2 + strlen(naddr); // family, (string), nul
67 int connr = connect(socknum, (struct sockaddr *) &name, sunlen);
74 uint16_t sname_len = strlen(service_name);
75 int bufsize = 3 + sname_len;
76 char * buf = new char[bufsize];
78 buf[0] = DINIT_CP_STARTSERVICE;
79 memcpy(buf + 1, &sname_len, 2);
80 memcpy(buf + 3, service_name, sname_len);
82 int r = write(socknum, buf, bufsize);