-
Notifications
You must be signed in to change notification settings - Fork 1
/
scgi.c
456 lines (372 loc) · 8.78 KB
/
scgi.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "scgi.h"
static void die(const char *msg)
{
perror(msg);
abort();
}
static int recv_fd(int sockfd)
{
char tmp[CMSG_SPACE(sizeof(int))];
struct cmsghdr *cmsg;
struct iovec iov;
struct msghdr msg;
char ch = '\0';
memset(&msg, 0, sizeof(msg));
iov.iov_base = &ch;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = tmp;
msg.msg_controllen = sizeof(tmp);
if (recvmsg(sockfd, &msg, 0) <= 0)
return -1;
cmsg = CMSG_FIRSTHDR(&msg);
return *(int *)CMSG_DATA(cmsg);
}
static int send_fd(int sockfd, int fd)
{
char tmp[CMSG_SPACE(sizeof(int))];
struct cmsghdr *cmsg;
struct iovec iov;
struct msghdr msg;
char ch = '\0';
memset(&msg, 0, sizeof(msg));
msg.msg_control = (caddr_t) tmp;
msg.msg_controllen = CMSG_LEN(sizeof(int));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;
iov.iov_base = &ch;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (sendmsg(sockfd, &msg, 0) != 1)
return -1;
return 0;
}
static int ns_read_size(int input)
{
char str[64];
int i = 0;
char c;
while (1) {
if (read(input, &c, 1) <= 0)
return -1;
if (c == ':')
break;
else
str[i++] = c;
}
str[i] = '\0';
return atoi(str);
}
static void *ns_reads(int input, int *nbytes)
{
int r, n = 0;
void *buf;
char t;
int size = ns_read_size(input);
if (size < 0)
die("bad header size");
buf = malloc(size);
while (size > 0) {
r = read(input, buf + n, size);
if (r <= 0)
die("bad header");
size -= r;
n += r;
}
if (read(input, &t, 1) <= 0 || t != ',')
die("missing ns terminator");
*nbytes = n;
return buf;
}
void read_env(int conn)
{
int size;
void *name, *value;
void *headers = ns_reads(conn, &size);
void *str = headers;
void *end = headers + size;
while (str != end) {
name = str;
value = rawmemchr(str, '\0') + 1;
setenv(name, value, 1);
str = rawmemchr(value, '\0') + 1;
}
free(headers);
}
static void add_child(struct children *children, pid_t pid, int fd)
{
struct child *c = malloc(sizeof(struct child));
c->pid = pid;
c->fd = fd;
c->closed = 0;
c->next = NULL;
*(children->last) = c;
children->last = &(c->next);
children->size++;
}
static void remove_child(struct children *children, struct child *child)
{
struct child *c;
if (children->first == child) {
if ((children->first = children->first->next) == NULL)
children->last = &children->first;
} else {
c = children->first;
while (c->next != child)
c = c->next;
if ((c->next = c->next->next) == NULL)
children->last = &c->next;
}
children->size--;
free(child);
}
static int fill_children_fdset(struct children *children, fd_set *fds)
{
struct child *c;
int highest_fd = -1;
FD_ZERO(fds);
for (c = children->first; c; c = c->next) {
if (!c->closed) {
FD_SET(c->fd, fds);
if (c->fd > highest_fd)
highest_fd = c->fd;
}
}
return highest_fd;
}
static struct child *get_ready_child(struct children *children, fd_set *fds)
{
struct child *c = NULL;
for (c = children->first; c; c = c->next)
if (!c->closed && FD_ISSET(c->fd, fds))
break;
return c;
}
static struct child *get_child(struct children *children, pid_t pid)
{
struct child *c = NULL;
for (c = children->first; c; c = c->next)
if (c->pid == pid)
break;
return c;
}
static int spawn_child(struct scgi_server *server, int conn)
{
int flag = 1;
int fd[2]; /* parent, child */
char magic = '1';
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
return -1;
}
/* make child fd non-blocking */
if ((flag = fcntl(fd[1], F_GETFL, 0)) < 0 ||
fcntl(fd[1], F_SETFL, flag | O_NONBLOCK) < 0) {
return -1;
}
pid_t pid = fork();
if (pid == 0) {
close(fd[1]);
/* in the midst of handling a request,
close the connection in the child
*/
if (conn != -1)
close(conn);
if (server->handler->child_init_hook)
server->handler->child_init_hook();
/* serving connection */
while (1) {
if (write(fd[0], &magic, 1) < 0)
die("write magic byte error");
conn = recv_fd(fd[0]);
if (conn == -1)
die("recv_fd error");
server->handler->handle_connection(conn);
}
exit(0);
} else if (pid > 0) {
close(fd[0]);
add_child(&server->children, pid, fd[1]);
} else {
perror("fork failed");
return -1;
}
return 0;
}
static void reap_children(struct scgi_server *server)
{
pid_t pid;
struct child *child;
while (server->children.size) {
pid = waitpid(-1, NULL, WNOHANG);
if (pid <= 0)
break;
child = get_child(&server->children, pid);
if (!child->closed)
close(child->fd);
remove_child(&server->children, child);
}
}
int restart = 0; /* pending restart */
static void hup_signal(int sig)
{
restart = 1;
}
static void do_stop(struct scgi_server *server)
{
struct child *c;
/* Close connections to the children, which will cause them to exit
after finishing what they are doing. */
for (c = server->children.first; c; c = c->next) {
if (!c->closed) {
close(c->fd);
c->closed = 1;
}
}
}
static void do_restart(struct scgi_server *server)
{
do_stop(server);
restart = 0;
}
static int delegate_request(struct scgi_server *server, int conn)
{
fd_set fds;
int highest_fd, r;
struct child *child;
struct timeval timeout;
char magic;
timeout.tv_usec = 0;
timeout.tv_sec = 0;
while (1) {
highest_fd = fill_children_fdset(&server->children, &fds);
r = select(highest_fd + 1, &fds, NULL, NULL, &timeout);
if (r < 0) {
if (errno == EINTR) {
continue;
} else {
die("select error");
}
} else if (r > 0) {
/* One or more children look like they are ready.
Do the same walk order so that we keep preferring
the same child.
*/
child = get_ready_child(&server->children, &fds);
if (child == NULL) {
/* should never get here */
fputs("Ooops\n", stderr);
continue;
}
/*
Try to read the single byte written by the child.
This can fail if the child died or the pipe really
wasn't ready (select returns a hint only). The fd has
been made non-blocking by spawn_child. If this fails
we fall through to the "reap_children" logic and will
retry the select call.
*/
r = read(child->fd, &magic, 1);
if (r == -1 && errno != EAGAIN) {
die("read byte error");
} else if (r == 1) {
assert(magic == '1');
/*
The byte was read okay, now we need to pass the fd
of the request to the child. This can also fail
if the child died. Again, if this fails we fall
through to the "reap_children" logic and will
retry the select call.
*/
if (send_fd(child->fd, conn) == -1 && errno != EPIPE) {
die("sendfd error");
} else {
/*
fd was apparently passed okay to the child.
The child could die before completing the
request but that's not our problem anymore.
*/
return 0;
}
}
}
/* didn't find any child, check if any died */
reap_children(server);
/* start more children if we haven't met max_children limit */
if (server->children.size < server->max_children)
spawn_child(server, conn);
/* Start blocking inside select. We might have reached
max_children limit and they are all busy.
*/
timeout.tv_sec = 2;
} /* end of while */
return 0;
}
void init_scgi(struct scgi_server *server, unsigned short port,
int max_children, struct scgi_handler *handler)
{
server->listen_port = port;
server->max_children = max_children;
server->children.first = NULL;
server->children.last = &server->children.first;
server->children.size = 0;
server->handler = handler;
spawn_child(server, -1);
}
int serve_scgi(struct scgi_server *server)
{
int flag, sock, conn;
struct sockaddr_in bindaddr;
struct sockaddr_in cliaddr;
socklen_t addrlen = sizeof(cliaddr);
sock = socket(AF_INET, SOCK_STREAM, 0);
flag = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) < 0) {
perror("setsockopt error");
return -1;
}
bindaddr.sin_family = AF_INET;
bindaddr.sin_port = htons(server->listen_port);
bindaddr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
perror("bind addr error");
return -1;
}
if (listen(sock, 256) < 0) {
perror("listen error");
close(sock);
return -1;
}
signal(SIGHUP, hup_signal);
siginterrupt(SIGHUP, 1);
while (1) {
conn = accept(sock, &cliaddr, &addrlen);
if (conn != -1) {
delegate_request(server, conn);
close(conn);
} else if (errno != EINTR) {
perror("accept error");
return -1;
}
if (restart) {
do_restart(server);
}
}
return 0;
}