forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
395 lines (330 loc) · 9.82 KB
/
console.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
#include <dlfcn.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ev.h>
#include <config.h>
#include "console.h"
#include "common.h"
#include "buffer.h"
#include "cli.h"
#include "item.h"
#include "list.h"
#include "log.h"
#include "module.h"
#include "planet.h"
#include "planet_type.h"
#include "port.h"
#include "server.h"
#include "universe.h"
static void write_msg(int fd, struct signal *msg, char *msgdata)
{
if (write(fd, msg, sizeof(*msg)) < 1)
bug("%s", "server signalling fd is closed");
if (msg->cnt && write(fd, msgdata, msg->cnt) < 1)
bug("%s", "server signalling fd is closed");
}
static void enter_sleep(struct console * const console)
{
console->sleep = 1;
ev_io_stop(console->loop, &console->cmd_watcher);
}
static int cmd_ports(void *_console, char *param)
{
struct port_type *type;
printf("%-26s %-26s %-8s %-8s %-8s %-8s\n",
"Name", "Description", "OCEAN", "SURFACE",
"ORBIT", "ROGUE");
list_for_each_entry(type, &univ.port_types, list)
printf("%-26.26s %-26.26s %-8s %-8s %-8s %-8s\n",
type->name, type->desc,
(type->zones[OCEAN] ? "Yes" : "No"),
(type->zones[SURFACE] ? "Yes" : "No"),
(type->zones[ORBIT] ? "Yes" : "No"),
(type->zones[ROGUE] ? "Yes" : "No"));
return 0;
}
static int cmd_help(void *_console, char *param)
{
struct console *console = _console;
cli_print_help(stdout, &console->cli);
return 0;
}
static int cmd_insmod(void *_console, char *param)
{
int r;
if (!param) {
printf("usage: insmod <file name.so>\n");
return 0;
}
r = module_insert(param);
if (r != 0)
printf("Error inserting module: %s\n",
(r == MODULE_DL_ERROR ? dlerror() : module_strerror(r))
);
return 0;
}
static int cmd_items(void *_console, char *param)
{
struct item *i;
printf("%-24s %-8s\n", "Name", "Weight");
list_for_each_entry(i, &univ.items, list)
printf("%-24.24s %-8ld\n", i->name, i->weight);
return 0;
}
static int cmd_lsmod(void *_console, char *param)
{
struct module *m;
if (list_empty(&modules_loaded)) {
printf("No modules are currently loaded\n");
return 0;
}
printf("%-16s %-8s\n", "Module", "Size");
list_for_each_entry(m, &modules_loaded, list)
printf("%-16s %-8d\n", m->name, m->size);
return 0;
}
static int cmd_wall(void *_console, char *message)
{
struct console *console = _console;
if (message) {
struct signal msg = {
.cnt = strlen(message) + 1,
.type = MSG_WALL
};
write_msg(console->server->fd[1], &msg, message);
} else {
printf("usage: wall <message>\n");
}
return 0;
}
static int cmd_pause(void *_console, char *param)
{
struct console *console = _console;
struct signal msg = {
.cnt = 0,
.type = MSG_PAUSE
};
write_msg(console->server->fd[1], &msg, NULL);
return 0;
}
static int cmd_planets(void *_console, char *param)
{
struct planet_type *type;
printf("%-5s %-26s %-4s %-4s %-4s %-12s %-12s %-10s\n",
"Class", "Name", "HOT", "ECO", "COLD",
"Min life", "Max life", "Port types");
list_for_each_entry(type, &univ.planet_types, list)
printf("%c %-26.26s %-4.4s %-4.4s %-4.4s %-12.12s %-12.12s %-10lu\n",
type->c, type->name,
(type->zones[HOT] ? "Yes" : "No"),
(type->zones[ECO] ? "Yes" : "No"),
(type->zones[COLD] ? "Yes" : "No"),
(planet_life_names[type->minlife]),
(planet_life_names[type->maxlife]),
ptrlist_len(&type->port_types));
return 0;
}
static int cmd_resume(void *_console, char *param)
{
struct console *console = _console;
struct signal msg = {
.cnt = 0,
.type = MSG_CONT
};
write_msg(console->server->fd[1], &msg, NULL);
return 0;
}
static void _cmd_rmmod(struct module *m)
{
int r;
r = module_remove(m);
if (r)
printf("Error removing module: %s\n",
(r == MODULE_DL_ERROR ? dlerror() : module_strerror(r))
);
}
static int cmd_rmmod(void *_console, char *name)
{
struct module *m;
if (!name) {
printf("usage: rmmod <module>\n");
return 0;
}
list_for_each_entry(m, &modules_loaded, list) {
if (strcmp(m->name, name) == 0) {
_cmd_rmmod(m);
return 0;
}
}
printf("Module %s is not currently loaded\n", name);
return 0;
}
static int cmd_ships(void *_console, char *param)
{
struct ship_type *type;
printf("%-26s %-26s %-12s\n",
"Name", "Description", "Carry weight");
list_for_each_entry(type, &univ.ship_types, list)
printf("%-26.26s %-26.26s %-12d\n",
type->name, type->desc, type->carry_weight);
return 0;
}
static int cmd_memstat(void *_console, char *param)
{
struct mallinfo minfo = mallinfo();
printf("Memory statistics:\n"
" Memory allocated with sbrk by malloc: %d bytes\n"
" Number of chunks not in use: %d\n"
" Number of chunks allocated with mmap: %d\n"
" Memory allocated with mmap: %d bytes\n"
" Memory occupied by chunks handed out by malloc: %d bytes\n"
" Memory occupied by free chunks: %d bytes\n"
" Size of top-most releasable chunk: %d bytes\n",
minfo.arena, minfo.ordblks, minfo.hblks, minfo.hblkhd,
minfo.uordblks, minfo.fordblks, minfo.keepcost);
return 0;
}
static int cmd_stats(void *_console, char *param)
{
struct tm t;
char created[32];
memset(created, 0, sizeof(created));
localtime_r(&univ.created, &t);
strftime(created, sizeof(created), "%c", &t);
printf("Statistics:\n"
" Size of universe: %lu systems\n"
" Universe created: %s\n"
" Number of users known: %s\n"
" Number of users connected: %s\n",
ptrlist_len(&univ.systems),
created,
"FIXME", "FIXME");
return 0;
}
static int cmd_quit(void *_console, char *param)
{
struct console *console = _console;
printf("Bye!\n");
enter_sleep(console);
kill(0, SIGTERM);
return 0;
}
static int register_console_commands(struct console * console)
{
if (cli_add_cmd(&console->cli, "ports", cmd_ports, console, "List available ports"))
goto err;
if (cli_add_cmd(&console->cli, "help", cmd_help, console, "Display this help text"))
goto err;
if (cli_add_cmd(&console->cli, "insmod", cmd_insmod, console, "Insert a loadable module"))
goto err;
if (cli_add_cmd(&console->cli, "items", cmd_items, console, "List available items"))
goto err;
if (cli_add_cmd(&console->cli, "lsmod", cmd_lsmod, console, "List modules currently loaded"))
goto err;
if (cli_add_cmd(&console->cli, "wall", cmd_wall, console, "Send a message to all connected players"))
goto err;
if (cli_add_cmd(&console->cli, "pause", cmd_pause, console, "Pause all players"))
goto err;
if (cli_add_cmd(&console->cli, "planets", cmd_planets, console, "List available planet types"))
goto err;
if (cli_add_cmd(&console->cli, "resume", cmd_resume, console, "Resume all players"))
goto err;
if (cli_add_cmd(&console->cli, "rmmod", cmd_rmmod, console, "Unload a loadable module"))
goto err;
if (cli_add_cmd(&console->cli, "ships", cmd_ships, console, "List available ship types"))
goto err;
if (cli_add_cmd(&console->cli, "stats", cmd_stats, console, "Display statistics"))
goto err;
if (cli_add_cmd(&console->cli, "memstat", cmd_memstat, console, "Display memory statistics"))
goto err;
if (cli_add_cmd(&console->cli, "quit", cmd_quit, console, "Terminate the server"))
goto err;
return 0;
err:
cli_tree_destroy(&console->cli);
return -1;
}
#define CONSOLE_PROMPT "console> "
static void console_cmd_cb(struct ev_loop * const loop, ev_io * const w, const int revents)
{
struct console *console = w->data;
if (read_into_buffer(STDIN_FILENO, &console->buffer))
return;
if (!buffer_terminate_line(&console->buffer)) {
if (strlen(console->buffer.buf) > 0 && cli_run_cmd(&console->cli, console->buffer.buf) < 0)
printf("Unknown command or syntax error.\n");
if (console->sleep)
return;
buffer_reset(&console->buffer);
printf(CONSOLE_PROMPT);
fflush(stdout);
}
}
static void console_kill_cb(struct ev_loop * const loop, struct ev_async *w, int revents)
{
ev_break(loop, EVBREAK_ALL);
}
static void* console_main(void *_console)
{
struct console *console = _console;
INIT_LIST_HEAD(&console->cli);
console->loop = ev_loop_new(EVFLAG_AUTO | EVFLAG_NOSIGMASK);
if (!console->loop)
die("%s", "Could not initialize event loop");
if (register_console_commands(console))
die("%s", "Could not register console commands");
ev_async_init(&console->kill_watcher, console_kill_cb);
ev_io_init(&console->cmd_watcher, console_cmd_cb, STDIN_FILENO, EV_READ);
console->kill_watcher.data = console;
console->cmd_watcher.data = console;
ev_async_start(console->loop, &console->kill_watcher);
ev_io_start(console->loop, &console->cmd_watcher);
printf("Welcome to YASTG %s, built %s %s.\n\n", PACKAGE_VERSION, __DATE__, __TIME__);
printf("Universe has %lu systems in total\n", ptrlist_len(&univ.systems));
printf("\n" CONSOLE_PROMPT);
fflush(stdout);
ev_run(console->loop, 0);
ev_io_stop(console->loop, &console->cmd_watcher);
ev_async_stop(console->loop, &console->kill_watcher);
cli_tree_destroy(&console->cli);
ev_loop_destroy(console->loop);
return 0;
}
void console_init(struct console * const console, struct server * const server)
{
memset(console, 0, sizeof(*console));
console->server = server;
buffer_init(&console->buffer);
}
void console_free(struct console * const console)
{
buffer_free(&console->buffer);
}
int start_console(struct console * const console)
{
sigset_t old, new;
sigfillset(&new);
if (pthread_sigmask(SIG_SETMASK, &new, &old))
goto err;
if (pthread_create(&console->thread, NULL, console_main, console) != 0)
goto err;
if (pthread_sigmask(SIG_SETMASK, &old, NULL))
goto err_cancel;
return 0;
err_cancel:
pthread_cancel(console->thread);
err:
return -1;
}
void stop_console(struct console * const console)
{
/*
* The asynchronous stuff in libev is the only stuff that is thread-safe,
* that's why we need this complexity for killing the console thread.
*/
ev_async_send(console->loop, &console->kill_watcher);
}