-
Notifications
You must be signed in to change notification settings - Fork 2
/
zookd.c
140 lines (115 loc) · 3.46 KB
/
zookd.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
/* dispatch daemon */
#include "http.h"
#include <err.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
static void process_client(int);
static int run_server(const char *portstr);
static int start_server(const char *portstr);
int main(int argc, char **argv)
{
if (argc != 2)
errx(1, "Wrong arguments");
if (!strcmp(argv[1], "--print-unlink-addr")) {
printf("%p\n", unlink);
exit(0);
}
run_server(argv[1]);
}
/* socket-bind-listen idiom */
static int start_server(const char *portstr)
{
struct addrinfo hints = {0}, *res;
int sockfd;
int e, opt = 1;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((e = getaddrinfo(NULL, portstr, &hints, &res)))
errx(1, "getaddrinfo: %s", gai_strerror(e));
if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
err(1, "socket");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
err(1, "setsockopt");
if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0)
err(1, "fcntl");
if (bind(sockfd, res->ai_addr, res->ai_addrlen))
err(1, "bind");
if (listen(sockfd, 5))
err(1, "listen");
freeaddrinfo(res);
unlink("/tmp/zook-start-wait");
return sockfd;
}
static void sigchld(int sig, siginfo_t *si, void *context)
{
if (si->si_signo == SIGCHLD && WIFSIGNALED(si->si_status)) {
printf("Child process %d terminated incorrectly, receiving signal %d\n",
si->si_pid, WTERMSIG(si->si_status));
}
}
static int run_server(const char *port)
{
signal(SIGPIPE, SIG_IGN);
struct sigaction sigchld_sa;
memset(&sigchld_sa, 0, sizeof(sigchld_sa));
sigchld_sa.sa_flags = SA_SIGINFO | SA_RESTART;
sigchld_sa.sa_sigaction = sigchld;
sigaction(SIGCHLD, &sigchld_sa, NULL);
int sockfd = start_server(port);
for (;;) {
int cltfd = accept(sockfd, NULL, NULL);
int pid;
if (cltfd < 0)
err(1, "accept");
/* fork a new process for each client process, because the process
* builds up state specific for a client (e.g. cookie and other
* enviroment variables that are set by request). We want to get rid off
* that state when we have processed the request and start the next
* request in a pristine state.
*/
switch ((pid = fork()))
{
case -1:
err(1, "fork");
case 0:
process_client(cltfd);
exit(0);
break;
default:
close(cltfd);
break;
}
}
}
static void process_client(int fd)
{
static char env[8192]; /* static variables are not on the stack */
static size_t env_len = 8192;
char reqpath[4096];
const char *errmsg;
/* get the request line */
if ((errmsg = http_request_line(fd, reqpath, env, &env_len)))
return http_err(fd, 500, "http_request_line: %s", errmsg);
env_deserialize(env, sizeof(env));
/* get all headers */
if ((errmsg = http_request_headers(fd)))
http_err(fd, 500, "http_request_headers: %s", errmsg);
else
http_serve(fd, getenv("REQUEST_URI"));
close(fd);
}
void accidentally(void)
{
__asm__("mov 16(%%rbp), %%rdi": : :"rdi");
}