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
|
/*
* NetCosm - a MUD server
* Copyright (C) 2016 Franklin Wei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "netcosm.h"
#define PORT 1234
#define BACKLOG 16
/* global data */
bool are_child = false;
void *child_map = NULL;
/* assume int is atomic */
volatile int num_clients = 0;
/* local data */
static uint16_t port;
static int server_socket;
void __attribute__((noreturn)) error(const char *fmt, ...)
{
char buf[128];
memset(buf, 0, sizeof(buf));
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
perror(buf);
exit(EXIT_FAILURE);
}
static void free_child_data(void *ptr)
{
struct child_data *child = ptr;
if(child->user)
free(child->user);
free(ptr);
}
static void handle_disconnects(void)
{
int saved_errno = errno;
pid_t pid;
while((pid = waitpid(-1, NULL, WNOHANG)) > 0)
{
sig_debugf("Client disconnect.\n");
struct child_data *child = hash_lookup(child_map, &pid);
ev_io_stop(child->loop, child->io_watcher);
free(child->io_watcher);
--num_clients;
hash_remove(child_map, &pid);
}
errno = saved_errno;
}
static void sigchld_handler(int s)
{
(void) s;
handle_disconnects();
}
static void handle_client(int fd, struct sockaddr_in *addr,
int nclients, int to, int from)
{
client_main(fd, addr, nclients, to, from);
}
static void __attribute__((noreturn)) serv_cleanup(void)
{
sig_debugf("Shutdown server.\n");
if(shutdown(server_socket, SHUT_RDWR) > 0)
error("shutdown");
close(server_socket);
world_free();
reqmap_free();
hash_free(child_map);
child_map = NULL;
ev_default_destroy();
userdb_shutdown();
_exit(0);
}
static void __attribute__((noreturn)) sigint_handler(int s)
{
(void) s;
serv_cleanup();
}
static void init_signals(void)
{
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGCHLD);
sa.sa_handler = sigchld_handler;
sa.sa_flags = SA_RESTART;
if(sigaction(SIGCHLD, &sa, NULL) < 0)
error("sigaction");
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGINT);
sa.sa_handler = sigint_handler;
sa.sa_flags = SA_RESTART;
if(sigaction(SIGINT, &sa, NULL) < 0)
error("sigaction");
if(sigaction(SIGTERM, &sa, NULL) < 0)
error("sigaction");
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGRTMIN+1);
sa.sa_sigaction = master_ack_handler;
sa.sa_flags = SA_SIGINFO | SA_RESTART;
if(sigaction(SIGRTMIN+1, &sa, NULL) < 0)
error("sigaction");
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGPIPE);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
if(sigaction(SIGPIPE, &sa, NULL) < 0)
error("sigaction");
/* we set this now so there's no race condition after a fork() */
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGRTMIN);
sigaddset(&sa.sa_mask, SIGPIPE);
sa.sa_sigaction = sig_rt_0_handler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
if(sigaction(SIGRTMIN, &sa, NULL) < 0)
error("sigaction");
}
static void check_userfile(void)
{
if(access(USERFILE, F_OK) < 0 || userdb_size() == 0)
first_run_setup();
if(access(USERFILE, R_OK | W_OK) < 0)
error("cannot access "USERFILE);
}
static void load_worldfile(void)
{
if(access(WORLDFILE, F_OK) < 0)
{
world_init(netcosm_world, netcosm_world_sz, netcosm_world_name);
world_save(WORLDFILE);
}
else if(access(WORLDFILE, R_OK | W_OK) < 0)
error("cannot access "WORLDFILE);
else
if(!world_load(WORLDFILE, netcosm_world, netcosm_world_sz, netcosm_world_name))
error("Failed to load world from disk.\nTry removing "WORLDFILE".\n");
}
static int server_bind(void)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0)
error("socket");
int tmp = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof tmp) < 0)
error("setsockopt");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr*) &addr, sizeof addr) < 0)
error("bind");
if(listen(sock, BACKLOG) < 0)
error("listen");
return sock;
}
static SIMP_HASH(pid_t, pid_hash);
static SIMP_EQUAL(pid_t, pid_equal);
static void childreq_cb(EV_P_ ev_io *w, int revents)
{
(void) EV_A;
(void) w;
(void) revents;
/* data from a child's pipe */
if(!handle_child_req(w->fd))
{
handle_disconnects();
}
}
static void new_connection_cb(EV_P_ ev_io *w, int revents)
{
(void) EV_A;
(void) w;
(void) revents;
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
int new_sock = accept(server_socket, (struct sockaddr*) &client, &client_len);
if(new_sock < 0)
error("accept");
++num_clients;
int readpipe[2]; /* child->parent */
int outpipe [2]; /* parent->child */
if(pipe(readpipe) < 0)
error("pipe");
if(pipe(outpipe) < 0)
error("pipe");
int flags = fcntl(outpipe[0], F_GETFL, 0);
if(fcntl(outpipe[0], F_SETFL, flags | O_NONBLOCK) < 0)
error("fcntl");
pid_t pid = fork();
if(pid < 0)
error("fork");
if(!pid)
{
/* child */
are_child = true;
close(readpipe[0]);
close(outpipe[1]);
close(server_socket);
/* only the master process controls the world */
world_free();
reqmap_free();
hash_free(child_map);
child_map = NULL;
/* we don't need libev anymore */
ev_default_destroy();
/* user DB requests go through the master */
userdb_shutdown();
debugf("Child with PID %d spawned\n", getpid());
server_socket = new_sock;
handle_client(new_sock, &client, num_clients, readpipe[1], outpipe[0]);
exit(0);
}
else
{
/* parent */
close(readpipe[1]);
close(outpipe[0]);
close(new_sock);
/* add the child to the child map */
struct child_data *new = calloc(1, sizeof(struct child_data));
memcpy(new->outpipe, outpipe, sizeof(outpipe));
memcpy(new->readpipe, readpipe, sizeof(readpipe));
new->addr = client.sin_addr;
new->pid = pid;
new->state = STATE_INIT;
new->user = NULL;
ev_io *new_io_watcher = calloc(1, sizeof(ev_io));
ev_io_init(new_io_watcher, childreq_cb, new->readpipe[0], EV_READ);
ev_io_start(EV_A_ new_io_watcher);
new->io_watcher = new_io_watcher;
new->loop = loop;
pid_t *pidbuf = malloc(sizeof(pid_t));
*pidbuf = pid;
hash_insert(child_map, pidbuf, new);
}
}
int main(int argc, char *argv[])
{
if(argc != 2)
port = PORT;
else
port = strtol(argv[1], NULL, 0);
srand(time(0));
server_socket = server_bind();
userdb_init(USERFILE);
check_userfile();
load_worldfile();
reqmap_init();
/* this initial size very low to make iteration faster */
child_map = hash_init(16, pid_hash, pid_equal);
hash_setfreedata_cb(child_map, free_child_data);
hash_setfreekey_cb(child_map, free);
debugf("Listening on port %d\n", port);
struct ev_loop *loop = ev_default_loop(0);
/* set up signal handlers AFTER creating the default loop, because it will grab SIGCHLD */
init_signals();
ev_io server_watcher;
ev_io_init(&server_watcher, new_connection_cb, server_socket, EV_READ);
ev_io_start(EV_A_ &server_watcher);
ev_loop(loop, 0);
/* should never get here */
error("unexpected termination");
}
|