-
Notifications
You must be signed in to change notification settings - Fork 6
/
miniquark.c
170 lines (143 loc) · 3 KB
/
miniquark.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
/* miniquark: see LICENSE file for copyright and license details. */
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "http.h"
#include "sock.h"
struct server {
char *host;
char *port;
} s;
static void
serve(int infd, struct sockaddr_storage *in_sa) {
struct request r;
enum status status;
char inaddr[INET6_ADDRSTRLEN /* > INET_ADDRSTRLEN */];
/* set connection timeout */
if (sock_set_timeout(infd, 30)) {
goto cleanup;
}
/* handle request */
if (!(status = http_get_request(infd, &r))) {
status = http_send_response(infd, &r);
}
if (sock_get_inaddr_str(in_sa, inaddr, LEN(inaddr))) {
goto cleanup;
}
printf("%lu\t%s\t%d\t%s\t%s\n", r.bytes_sent, inaddr, status, r.field[REQ_AGENT], r.target);
cleanup:
/* clean up and finish */
shutdown(infd, SHUT_RD);
shutdown(infd, SHUT_WR);
close(infd);
}
static void
sigcleanup(int sig) {
kill(0, sig);
_exit(1);
}
static void
handlesignals(void (*hdl)(int)) {
struct sigaction sa = {
.sa_handler = hdl,
};
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
}
static void
usage(void) {
fprintf(stderr, "release: %s\n", RELEASE);
fprintf(stderr, "usage: miniquark -p port [-h host] [-d dir]\n");
exit(1);
}
int
main(int argc, char *argv[]) {
int ch;
struct sockaddr_storage in_sa;
socklen_t in_sa_len;
int insock, status = 0, infd;
/* defaults */
char *servedir = ".";
s.host = "127.0.0.1";
s.port = NULL;
opterr = 0;
while ((ch = getopt(argc, argv, "p:h:d:")) != -1) {
switch (ch) {
case 'd':
servedir = optarg;
break;
case 'h':
s.host = optarg;
break;
case 'p':
s.port = optarg;
break;
default:
usage();
}
}
if (argc > optind + 1)
usage();
if (s.port == NULL)
usage();
/* Open a new process group */
setpgid(0, 0);
handlesignals(sigcleanup);
printf("listening on: http://%s:%s/\n", s.host, s.port);
fflush(stdout);
switch (fork()) {
case -1:
warn("fork");
break;
case 0:
/* restore default handlers */
handlesignals(SIG_DFL);
/* reap children automatically */
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
err(1, "Failed to set SIG_IGN on SIGCHLD");
}
if (chdir(servedir) < 0)
err(1, "chdir '%s'", servedir);
/* chroot */
if (getuid() == 0) {
if (chroot(".") < 0) {
err(1, "chroot .");
}
}
insock = sock_get_ips(s.host, s.port);
/* accept incoming connections */
while (1) {
in_sa_len = sizeof(in_sa);
if ((infd = accept(insock, (struct sockaddr *) &in_sa, &in_sa_len)) < 0) {
warn("accept");
continue;
}
/* fork and handle */
switch (fork()) {
case 0:
serve(infd, &in_sa);
exit(0);
break;
case -1:
warn("fork");
/* fallthrough */
default:
/* close the connection in the parent */
close(infd);
}
}
exit(0);
default:
while (wait(&status) > 0)
;
}
return status;
}