-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
255 lines (211 loc) · 6.03 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* Settings ---------------------------------------------------------- */
#define DEFAULT_PORT 8080
#define DEFAULT_ROOT "/var/www"
#define BUF_SIZE 1024
#define USE_SYSLOG 0
/* ------------------------------------------------------------------- */
#define BACKLOG 5
extern char *__progname;
static uint16_t port = DEFAULT_PORT;
static const char* document_root = DEFAULT_ROOT;
static const char* response =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<!DOCTYPE html>\r\n"
"<html>"
"<head>\r\n"
"<title>it works</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<p>it works</p>\r\n"
"</body>\r\n"
"</html>\r\n";
#if USE_SYSLOG
#define SYSLOG(...) syslog(__VA_ARGS__)
#else
#define SYSLOG(...)
#endif
void sigchld (int n);
int main(int argc, char *argv[])
{
int c;
int sockfd;
int optval = 1;
struct sockaddr_in sa_in;
int connection_fd;
struct sockaddr client_addr;
pid_t pid;
/* オプション解析 */
while ((c = getopt(argc, argv, "p:d:")) != -1) {
switch (c) {
case 'p':
port = (uint16_t)atoi(optarg);
break;
case 'd':
document_root = optarg;
break;
default:
exit(EXIT_FAILURE);
}
}
if ((argc - optind) != 0) {
/* オプション解析エラー */
exit(EXIT_FAILURE);
}
/* Syslog */
#if USE_SYSLOG
openlog(__progname, LOG_PID, LOG_DAEMON);
#endif
/* ソケットを開く */
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
SYSLOG(LOG_ERR, "socket: %m");
perror("socket");
exit(EXIT_FAILURE);
}
/*-
* https://www.jpcert.or.jp/sc-rules/c-fio22-c.html
* FD_CLOEXEC
*/
if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) < 0) {
SYSLOG(LOG_ERR, "fcntl: %m");
perror("fcntl");
exit(EXIT_FAILURE);
}
/* TIME_WAITでも使えるように */
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(optval)) < 0) {
SYSLOG(LOG_ERR, "setsockopt: %m");
perror("setsockopt");
exit(EXIT_FAILURE);
}
/* バインド(IPv4) */
memset(&sa_in, 0, sizeof(struct sockaddr_in));
sa_in.sin_family = AF_INET;
sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
sa_in.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr*)&sa_in, sizeof(struct sockaddr)) < 0) {
SYSLOG(LOG_ERR, "bind: %m");
perror("bind");
exit(EXIT_FAILURE);
}
/* リッスンを始める */
if (listen(sockfd, BACKLOG) < 0) {
SYSLOG(LOG_ERR, "listen: %m");
perror("listen");
exit(EXIT_FAILURE);
}
/* Accept Filter */
#ifdef SO_ACCEPTFILTER
{
struct accept_filter_arg afa;
bzero(&afa, sizeof(afa));
strcpy(afa.af_name, "httpready");
setsockopt(sockfd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
}
#elif TCP_DEFER_ACCEPT
{
int optval = 1;
setsockopt(sockfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &optval, sizeof(optval));
}
#endif
/* TODO: Daemonize */
setsid();
/*-
* シグナルの設定
* https://www.jpcert.or.jp/sc-rules/c-sig01-c.html
*/
{
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = sigchld;
if (sigemptyset(&act.sa_mask) < 0) {
SYSLOG(LOG_ERR, "sigemptyset: %m");
perror("sigemptyset");
exit(EXIT_FAILURE);
}
act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
if (sigaction(SIGCHLD, &act, NULL) < 0) {
SYSLOG(LOG_ERR, "sigaction: %m");
perror("sigaction");
exit(EXIT_FAILURE);
}
}
for (;;) {
socklen_t client_addr_size = sizeof(client_addr);
connection_fd = accept(sockfd, &client_addr, &client_addr_size);
if (connection_fd < 0) {
if (errno == EINTR || errno == EAGAIN || errno == ECONNABORTED)
continue;
// EPROTO continue
SYSLOG(LOG_ERR, "accept: %m");
perror("accept");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid < 0) {
SYSLOG(LOG_ERR, "fork: %m");
perror("fork");
exit(EXIT_FAILURE);
}
/* 子プロセスの処理 */
if (pid == 0) {
close(sockfd);
/* Request */
{
char inet_addrstr[INET_ADDRSTRLEN];
char buf[BUF_SIZE];
if (inet_ntop(AF_INET, &((struct sockaddr_in *)&client_addr)->sin_addr, inet_addrstr, sizeof(inet_addrstr)) < 0) {
SYSLOG(LOG_WARNING, "inet_ntop: %m");
perror("inet_ntop");
puts("accessed from UNKNOWN HOST");
} else {
printf("accessed from %s\n", inet_addrstr);
}
read(connection_fd, buf, sizeof(buf));
write(connection_fd, response, strlen(response));
}
if (close(connection_fd) < 0) {
SYSLOG(LOG_ERR, "close: %m");
perror("close");
exit(EXIT_FAILURE);
}
/* 子プロセスの終了 */
exit(EXIT_SUCCESS);
}
close(connection_fd);
}
return 0;
}
/* SIGCHLDシグナルハンドラ */
void sigchld (int n)
{
pid_t pid;
int status;
do {
pid = waitpid((pid_t)-1, &status, WNOHANG);
if (pid < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
if (errno != ECHILD) {
SYSLOG(LOG_ERR, "waitpid: %m");
perror("waitpid");
}
break;
}
} while (pid > 0);
}