about summary refs log tree commit diff
path: root/nitroctl.c
blob: 09497c7a0a24781a7ade4bd2923cc52412039915 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <sys/socket.h>
#include <sys/un.h>

#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int connfd;
const char *sockpath;
char notifypath[PATH_MAX];
volatile sig_atomic_t timed_out;

enum process_state {
	PROC_DOWN = 1,
	PROC_SETUP,
	PROC_STARTING,
	PROC_UP,
	PROC_ONESHOT,
	PROC_SHUTDOWN,
	PROC_RESTART,
	PROC_FATAL,
	PROC_DELAY,
};

void
noop()
{
	timed_out = 1;
}

void
cleanup_notify()
{
	unlink(notifypath);
}

void
notifysock(const char *service)
{
	static char default_sock[] = "/run/nitro/nitro.sock";
	char *path = strdup(sockpath);
	if (!path || !*path)
		path = default_sock;

	snprintf(notifypath, sizeof notifypath,
	    "%s/notify/%s,%ld", dirname(path), service, (long)getpid());

	struct sockaddr_un my_addr = { 0 };
	my_addr.sun_family = AF_UNIX;
	strncpy(my_addr.sun_path, notifypath, sizeof my_addr.sun_path - 1);
again:
	if (bind(connfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
		if (errno == EADDRINUSE) {
			// ok to delete this, only we can have the current pid.
			if (unlink(notifypath) == 0)
				goto again;
		}
		perror("bind");
		exit(111);
	}
	atexit(cleanup_notify);

	struct sockaddr_un addr = { 0 };
	addr.sun_family = AF_UNIX;
	strncpy(addr.sun_path, sockpath, sizeof addr.sun_path - 1);
	if (connect(connfd, (struct sockaddr *)&addr, sizeof addr) < 0) {
		fprintf(stderr, "nitroctl: could not connect to '%s', is nitro started?\n", sockpath);
		exit(111);
	}
}

int
send_and_wait(char cmd, const char *service)
{
	notifysock(service);

	dprintf(connfd, "%c%s", cmd, service);

	struct sigaction sa = {
		.sa_handler = noop,
		.sa_flags = 0,
	};
	sigaction(SIGALRM, &sa, 0);
	alarm(5);

	while(!timed_out) {
		ssize_t rd;
		char buf[64];

		rd = read(connfd, buf, sizeof buf); /* block */
		if (rd < 0) {
			if (errno == EINTR)
				continue;
			perror("read");
			exit(111);
		}

		buf[rd] = 0;

		int state = 0;
		if (buf[0] >= 'A' && buf[0] <= 'Z')
			state = buf[0] - 64;

		if (buf[0] == 'e') {
			fprintf(stderr, "nitroctl: no such service '%s'\n", service);
			return 111;
		}

		switch (cmd) {
		case 'u':
		case 'r':
			if (state == PROC_UP || state == PROC_ONESHOT)
				return 0;
			if (state == PROC_FATAL) {
				fprintf(stderr,
				    "nitroctl: failed to %sstart '%s'\n", service, cmd == 'u' ? "" : "re");
				return 1;
			}
			break;
		case 'd':
			if (state == PROC_DOWN || state == PROC_FATAL)
				return 0;
			break;
		}
	}

	fprintf(stderr, "nitroctl: action timed out\n");
	return 2;
}

char *
normalize(char *service)
{
	if (!service || (service[0] != '/' && service[0] != '.'))
		return service;

	char *buf = realpath(service, 0);
	if (!buf) {
		fprintf(stderr, "nitroctl: no such service: %s: %m\n", service);
		exit(1);
	}

	char *end = strrchr(buf, '/');
	if (strcmp(end, "/log") == 0)
		while(end > buf && *--end != '/')
			;

	return end + 1;
}


int
main(int argc, char *argv[])
{
	if (argc < 2 || (
	    strcmp(argv[1], "l") != 0 && strcmp(argv[1], "list") != 0 &&
	    strcmp(argv[1], "d") != 0 && strcmp(argv[1], "down") != 0 &&
	    strcmp(argv[1], "u") != 0 && strcmp(argv[1], "up") != 0 &&
	    strcmp(argv[1], "p") != 0 && strcmp(argv[1], "pause") != 0 &&
	    strcmp(argv[1], "c") != 0 && strcmp(argv[1], "cont") != 0 &&
	    strcmp(argv[1], "h") != 0 && strcmp(argv[1], "hup") != 0 &&
	    strcmp(argv[1], "a") != 0 && strcmp(argv[1], "alarm") != 0 &&
	    strcmp(argv[1], "i") != 0 && strcmp(argv[1], "interrupt") != 0 &&
	    strcmp(argv[1], "q") != 0 && strcmp(argv[1], "quit") != 0 &&
	    strcmp(argv[1], "t") != 0 && strcmp(argv[1], "term") != 0 &&
	    strcmp(argv[1], "k") != 0 && strcmp(argv[1], "kill") != 0 &&
	    strcmp(argv[1], "1") != 0 &&
	    strcmp(argv[1], "2") != 0 &&
	    strcmp(argv[1], "check") != 0 &&
	    strcmp(argv[1], "start") != 0 &&
	    strcmp(argv[1], "r") != 0 && strcmp(argv[1], "restart") != 0 &&
	    strcmp(argv[1], "stop") != 0 &&
	    strcmp(argv[1], "Reboot") != 0 &&
	    strcmp(argv[1], "Shutdown") != 0)) {
		dprintf(2, "usage: nitroctl COMMAND [SERVICE]\n");
		exit(2);
	}

	static const char default_sock[] = "/run/nitro/nitro.sock";
	sockpath = getenv("NITRO_SOCK");
	if (!sockpath || !*sockpath)
		sockpath = default_sock;

	connfd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	if (connfd < 0) {
		perror("socket");
		exit(111);
	}

	char cmd = argv[1][0];
	char *service = normalize(argv[2]);

	if (strcmp(argv[1], "start") == 0 && service)
		return send_and_wait('u', service);
	else if (strcmp(argv[1], "stop") == 0 && service)
		return send_and_wait('d', service);
	else if (strcmp(argv[1], "restart") == 0 && service)
		return send_and_wait('r', service);
	else if (strcmp(argv[1], "check") == 0 && service)
		cmd = '?';

	notifysock("");

	dprintf(connfd, "%c%s", cmd, service ? service : "");

	int status = 1;

	ssize_t rd;
	char buf[4096];
	rd = read(connfd, buf, sizeof buf);
	if (rd < 0) {
		perror("read");
		exit(111);
	}
	if (rd > 0)
		status = 0;
	write(1, buf, rd);

	return status;
}