-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb.c
304 lines (233 loc) · 7.56 KB
/
qb.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#define UNIX_SOCKET_FILENAME "fd-pass.sock"
volatile sig_atomic_t sig_recv;
void signal_recv(int signal)
{
sig_recv = signal;
}
int send_fd(int unix_sock, int fd)
{
char message[1];
/* NOTE(awiddersheim): Send the number of file descriptors as a single byte
* in the message. This can get used on the receiving end to validate all
* file descriptors were transferred properly. This idea comes from
* Python's multiprocessing.reduction library[1].
* [1]: https://github.com/python/cpython/blob/3.9/Lib/multiprocessing/reduction.py#L148
*/
message[0] = (char)1;
struct iovec iov = {
.iov_base = message,
.iov_len = 1
};
union {
char buf[CMSG_SPACE(sizeof(fd))];
struct cmsghdr align;
} u;
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = u.buf,
.msg_controllen = sizeof(u.buf)
};
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
*cmsg = (struct cmsghdr) {
.cmsg_level = SOL_SOCKET,
.cmsg_type = SCM_RIGHTS,
.cmsg_len = CMSG_LEN(sizeof(fd))
};
memcpy(CMSG_DATA(cmsg), &fd, sizeof(fd));
return sendmsg(unix_sock, &msg, 0);
}
int connect_unix(struct sockaddr_un unix_addr)
{
int result;
int sock;
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
printf("Could not create UNIX socket\n");
return sock;
}
if ((result = connect(sock, (struct sockaddr *)&unix_addr, sizeof(unix_addr))) < 0)
{
close(sock);
return result;
}
return sock;
}
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *argv[])
{
struct sockaddr_in addr = { 0 };
socklen_t addrlen;
struct sockaddr_in client_addr;
int connect_message = 1;
int connected = 0;
int exit_code = 0;
int fatal_error = 0;
int fd;
struct pollfd fds[2];
char message[1024] = { 0 };
int pollret = 0;
int port = 8000;
int reuse = 1;
unsigned int quit = 0;
int sock;
struct timespec ts;
struct sockaddr_un unix_addr = { 0 };
int unix_sock;
sig_recv = 0;
signal(SIGQUIT, signal_recv);
signal(SIGTERM, signal_recv);
signal(SIGINT, signal_recv);
printf("Starting Quarterback with PID (%d)\n", getpid());
addrlen = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
unix_addr.sun_family = AF_UNIX;
strncpy(unix_addr.sun_path, UNIX_SOCKET_FILENAME, sizeof(unix_addr.sun_path) - 1);
snprintf(message, sizeof(message), "Hello from Quarterback on PID (%d)!\n", getpid());
/* Timeout for nanosleep() */
ts.tv_sec = 1;
ts.tv_nsec = 0;
while (quit != 1)
{
if (sig_recv != 0) {
printf("Processing signal (%s)\n", strsignal(sig_recv));
switch (sig_recv) {
case SIGINT:
case SIGQUIT:
case SIGTERM:
quit = 1;
default:
break;
}
sig_recv = 0;
continue;
}
if (connected != 1) {
if (connect_message == 1) {
printf("Connecting to UNIX socket (%s)\n", unix_addr.sun_path);
connect_message = 0;
}
if ((unix_sock = connect_unix(unix_addr)) < 0) {
nanosleep(&ts, &ts);
continue;
}
printf("Connected to UNIX socket (%s)\n", unix_addr.sun_path);
connect_message = 1;
connected = 1;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Could not create socket\n");
fatal_error = 1;
goto cleanup;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
printf("Could not set address reuse\n");
fatal_error = 1;
goto cleanup;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse)) == -1) {
printf("Could not set port reuse\n");
fatal_error = 1;
goto cleanup;
}
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
printf("Could not bind to port (%d)\n", port);
fatal_error = 1;
goto cleanup;
}
if (listen(sock, 128) < 0) {
printf("Could not listen on port (%d)\n", port);
fatal_error = 1;
goto cleanup;
}
memset(fds, 0x0, sizeof(fds));
fds[0].fd = sock;
fds[0].events = POLLIN;
fds[1].fd = unix_sock;
fds[1].events = POLLIN;
printf("Listening on 0.0.0.0:%d\n", port);
}
pollret = poll(fds, 2, 100);
if (pollret == -1 && errno != EINTR) {
printf("Could not poll() on socket (%i)\n", errno);
quit = 1;
}
if (pollret <= 0) {
continue;
}
if (fds[0].revents) {
if (fds[1].revents & POLLNVAL) {
printf("Socket was invalid\n");
connected = 0;
}
else if (fds[1].revents & POLLERR) {
printf("Socket had an error\n");
connected = 0;
}
if (connected != 1) {
close(sock);
close(unix_sock);
continue;
}
if ((fd = accept(sock, (struct sockaddr *)&client_addr, &addrlen)) < 0) {
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
continue;
else
printf(
"Could not accept() connection from (%s:%d)\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port)
);
}
printf(
"Handling connection from (%s:%d)\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port)
);
if (send(fd, message, strlen(message), 0) < 0) {
printf(
"Could not send() to (%s:%d)\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port)
);
}
if (send_fd(unix_sock, fd) < 0)
printf("Could not send_fd() over UNIX socket (%s)\n", unix_addr.sun_path);
printf(
"Closing connection from (%s:%d)\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port)
);
close(fd);
}
else if (fds[1].revents) {
if (fds[1].revents & POLLNVAL) {
printf("Socket closed unexpectedly on UNIX socket (%s)\n", unix_addr.sun_path);
} else {
printf("Connection closed on UNIX socket (%s)\n", unix_addr.sun_path);
close(unix_sock);
}
close(sock);
connected = 0;
}
}
cleanup:
if (fatal_error != 0) {
printf("Encountered fatal error\n");
exit_code = 1;
}
printf("Shutting down\n");
close(unix_sock);
close(sock);
return exit_code;
}