main.c 9.1 KB
Newer Older
Marek Vavruša's avatar
Marek Vavruša committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*  Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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/>.
 */

Marek Vavruša's avatar
Marek Vavruša committed
17 18 19
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
20
#include <uv.h>
Marek Vavruša's avatar
Marek Vavruša committed
21

22 23
#include "contrib/ucw/mempool.h"
#include "contrib/ccan/asprintf/asprintf.h"
24
#include "lib/defines.h"
25
#include "lib/resolve.h"
26
#include "lib/dnssec.h"
27 28
#include "daemon/network.h"
#include "daemon/worker.h"
29
#include "daemon/engine.h"
30
#include "daemon/bindings.h"
31

32 33 34 35 36 37 38 39
/*
 * Globals
 */
static int g_interactive = 1;

/*
 * TTY control
 */
40 41
static void tty_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
{
42 43 44 45 46 47 48 49 50
	/* Set output streams */
	FILE *out = stdout, *outerr = stderr;
	uv_os_fd_t stream_fd = 0;
	uv_fileno((uv_handle_t *)stream, &stream_fd);
	if (stream_fd != STDIN_FILENO) {
		if (nread <= 0) { /* Close if disconnected */
			uv_close((uv_handle_t *)stream, (uv_close_cb) free);
			return;
		}
51 52
		uv_os_fd_t dup_fd = dup(stream_fd);
		if (dup_fd >= 0) {
53
			out = outerr = fdopen(dup_fd, "w");
54
		}
55 56
	}
	/* Execute */
57 58
	if (stream && buf && nread > 0) {
		char *cmd = buf->base;
59 60 61 62 63 64
		if (cmd[nread - 1] == '\n') {
			cmd[nread - 1] = '\0';
		}
		struct engine *engine = stream->data;
		lua_State *L = engine->L;
		int ret = engine_cmd(engine, cmd);
65
		const char *message = "";
Marek Vavruša's avatar
Marek Vavruša committed
66
		if (lua_gettop(L) > 0) {
67
			message = lua_tostring(L, -1);
Marek Vavruša's avatar
Marek Vavruša committed
68
		}
69 70 71 72 73
		if (stream_fd != STDIN_FILENO) {
			fprintf(stdout, "%s\n", cmd); /* Duplicate command to logs */
			fprintf(out, "%s\n> ", message); /* Duplicate output to sender */
		}
		fprintf(ret ? stderr : stdout, "%s\n> ", message);
Marek Vavruša's avatar
Marek Vavruša committed
74
		lua_settop(L, 0);
75
		free(buf->base);
76
	}
77 78 79 80 81
	fflush(out);
	/* Close if redirected */
	if (stream_fd != STDIN_FILENO) {
		fclose(out); /* outerr is the same */
	}
82 83 84 85

}

static void tty_alloc(uv_handle_t *handle, size_t suggested, uv_buf_t *buf) {
Marek Vavruša's avatar
Marek Vavruša committed
86 87
	buf->len = suggested;
	buf->base = malloc(suggested);
88
}
89

90 91 92 93 94 95
static void tty_accept(uv_stream_t *master, int status)
{
	uv_tcp_t *client = malloc(sizeof(*client));
	if (client) {
		 uv_tcp_init(master->loop, client);
		 if (uv_accept(master, (uv_stream_t *)client) != 0) {
96 97
			free(client);
			return;
98 99 100 101 102 103 104 105 106 107
		 }
		 client->data = master->data;
		 uv_read_start((uv_stream_t *)client, tty_alloc, tty_read);
		 /* Write command line */
		 uv_buf_t buf = { "> ", 2 };
		 uv_try_write((uv_stream_t *)client, &buf, 1);
	}
}

static void signal_handler(uv_signal_t *handle, int signum)
108 109 110 111 112
{
	uv_stop(uv_default_loop());
	uv_signal_stop(handle);
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static const char *set_addr(char *addr, int *port)
{
	char *p = strchr(addr, '#');
	if (p) {
		*port = atoi(p + 1);
		*p = '\0';
	}

	return addr;
}

/*
 * Server operation.
 */

Marek Vavruša's avatar
Marek Vavruša committed
128
static void help(int argc, char *argv[])
Marek Vavruša's avatar
Marek Vavruša committed
129
{
130
	printf("Usage: %s [parameters] [rundir]\n", argv[0]);
Marek Vavruša's avatar
Marek Vavruša committed
131
	printf("\nParameters:\n"
132 133 134 135 136 137
	       " -a, --addr=[addr]    Server address (default: localhost#53).\n"
	       " -k, --keyfile=[path] File containing trust anchors (DS or DNSKEY).\n"
	       " -f, --forks=N        Start N forks sharing the configuration.\n"
	       " -v, --verbose        Run in verbose mode.\n"
	       " -V, --version        Print version of the server.\n"
	       " -h, --help           Print help and usage.\n"
138
	       "Options:\n"
139
	       " [rundir]             Path to the working directory (default: .)\n");
Marek Vavruša's avatar
Marek Vavruša committed
140 141
}

142
static struct worker_ctx *init_worker(uv_loop_t *loop, struct engine *engine, mm_ctx_t *pool, int worker_id)
143
{
144 145 146 147 148
	/* Load bindings */
	engine_lualib(engine, "modules", lib_modules);
	engine_lualib(engine, "net",     lib_net);
	engine_lualib(engine, "cache",   lib_cache);
	engine_lualib(engine, "event",   lib_event);
149
	engine_lualib(engine, "worker",  lib_worker);
150 151 152 153 154

	/* Create main worker. */
	struct worker_ctx *worker = mm_alloc(pool, sizeof(*worker));
	if(!worker) {
		return NULL;
Marek Vavruša's avatar
Marek Vavruša committed
155
	}
156 157 158 159 160
	memset(worker, 0, sizeof(*worker));
	worker->engine = engine,
	worker->loop = loop;
	loop->data = worker;
	worker_reserve(worker, MP_FREELIST_SIZE);
161 162 163
	/* Register worker in Lua thread */
	lua_pushlightuserdata(engine->L, worker);
	lua_setglobal(engine->L, "__worker");
164 165 166 167
	lua_getglobal(engine->L, "worker");
	lua_pushnumber(engine->L, worker_id);
	lua_setfield(engine->L, -2, "id");
	lua_pop(engine->L, 1);
168 169
	return worker;
}
170

171
static int run_worker(uv_loop_t *loop, struct engine *engine)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{
	/* Control sockets or TTY */
	auto_free char *sock_file = NULL;
	uv_pipe_t pipe;
	uv_pipe_init(loop, &pipe, 0);
	pipe.data = engine;
	if (g_interactive) {
		printf("[system] interactive mode\n> ");
		fflush(stdout);
		uv_pipe_open(&pipe, 0);
		uv_read_start((uv_stream_t*) &pipe, tty_alloc, tty_read);
	} else {
		(void) mkdir("tty", S_IRWXU|S_IRWXG);
		sock_file = afmt("tty/%ld", getpid());
		if (sock_file) {
			uv_pipe_bind(&pipe, sock_file);
			uv_listen((uv_stream_t *) &pipe, 16, tty_accept);
		}
	}
	/* Run event loop */
Marek Vavruša's avatar
Marek Vavruša committed
192
	uv_run(loop, UV_RUN_DEFAULT);
193 194 195
	if (sock_file) {
		unlink(sock_file);
	}
Marek Vavruša's avatar
Marek Vavruša committed
196
	return kr_ok();
Marek Vavruša's avatar
Marek Vavruša committed
197 198 199 200
}

int main(int argc, char **argv)
{
Marek Vavruša's avatar
Marek Vavruša committed
201
	int forks = 1;
202 203
	array_t(char*) addr_set;
	array_init(addr_set);
204 205
	char *keyfile = NULL;
	static char keyfile_buf[PATH_MAX + 1];
Marek Vavruša's avatar
Marek Vavruša committed
206 207 208 209

	/* Long options. */
	int c = 0, li = 0, ret = 0;
	struct option opts[] = {
210 211 212 213 214 215
		{"addr", required_argument,   0, 'a'},
		{"keyfile",required_argument, 0, 'k'},
		{"forks",required_argument,   0, 'f'},
		{"verbose",    no_argument,   0, 'v'},
		{"version",   no_argument,    0, 'V'},
		{"help",      no_argument,    0, 'h'},
Marek Vavruša's avatar
Marek Vavruša committed
216 217
		{0, 0, 0, 0}
	};
218
	while ((c = getopt_long(argc, argv, "a:f:k:vVh", opts, &li)) != -1) {
Marek Vavruša's avatar
Marek Vavruša committed
219 220 221
		switch (c)
		{
		case 'a':
222
			array_push(addr_set, optarg);
Marek Vavruša's avatar
Marek Vavruša committed
223
			break;
Marek Vavruša's avatar
Marek Vavruša committed
224
		case 'f':
225
			g_interactive = 0;
Marek Vavruša's avatar
Marek Vavruša committed
226 227
			forks = atoi(optarg);
			if (forks == 0) {
228
				log_error("[system] error '-f' requires number, not '%s'\n", optarg);
Marek Vavruša's avatar
Marek Vavruša committed
229 230
				return EXIT_FAILURE;
			}
231 232 233 234 235 236
#if (!defined(UV_VERSION_HEX)) || (!defined(SO_REUSEPORT))
			if (forks > 1) {
				log_error("[system] libuv 1.7+ is required for SO_REUSEPORT support, multiple forks not supported\n");
				return EXIT_FAILURE;
			}
#endif
Marek Vavruša's avatar
Marek Vavruša committed
237
			break;
238
		case 'k':
239 240 241
			keyfile = realpath(optarg, keyfile_buf);
			if (!keyfile || access(optarg, R_OK|W_OK) != 0) {
				log_error("[system] keyfile '%s': not readable/writeable\n", optarg);
242 243 244
				return EXIT_FAILURE;
			}
			break;
245
		case 'v':
246 247 248 249
			log_debug_enable(true);
			break;
		case 'V':
			log_info("%s, version %s\n", "Knot DNS Resolver", PACKAGE_VERSION);
Marek Vavruša's avatar
Marek Vavruša committed
250 251 252
			return EXIT_SUCCESS;
		case 'h':
		case '?':
Marek Vavruša's avatar
Marek Vavruša committed
253
			help(argc, argv);
Marek Vavruša's avatar
Marek Vavruša committed
254 255
			return EXIT_SUCCESS;
		default:
Marek Vavruša's avatar
Marek Vavruša committed
256
			help(argc, argv);
Marek Vavruša's avatar
Marek Vavruša committed
257 258 259 260
			return EXIT_FAILURE;
		}
	}

261 262
	/* Switch to rundir. */
	if (optind < argc) {
263 264
		const char *rundir = argv[optind];
		if (access(rundir, W_OK) != 0) {
265
			log_error("[system] rundir '%s': not writeable\n", rundir);
266 267 268
			return EXIT_FAILURE;
		}
		ret = chdir(rundir);
269
		if (ret != 0) {
270
			log_error("[system] rundir '%s': %s\n", rundir, strerror(errno));
271 272
			return EXIT_FAILURE;
		}
273
	}
274

275 276
	kr_crypto_init();

277 278 279 280 281 282 283 284 285
	/* Fork subprocesses if requested */
	while (--forks > 0) {
		int pid = fork();
		if (pid < 0) {
			perror("[system] fork");
			return EXIT_FAILURE;
		}
		/* Forked process */
		if (pid == 0) {
286
			kr_crypto_reinit();
287 288 289 290
			break;
		}
	}

291
	/* Block signals. */
292
	uv_loop_t *loop = uv_default_loop();
293
	uv_signal_t sigint, sigterm;
294
	uv_signal_init(loop, &sigint);
295
	uv_signal_init(loop, &sigterm);
296
	uv_signal_start(&sigint, signal_handler, SIGINT);
297
	uv_signal_start(&sigterm, signal_handler, SIGTERM);
298
	/* Create a server engine. */
299 300 301 302
	mm_ctx_t pool = {
		.ctx = mp_new (4096),
		.alloc = (mm_alloc_t) mp_alloc
	};
303 304 305
	struct engine engine;
	ret = engine_init(&engine, &pool);
	if (ret != 0) {
306
		log_error("[system] failed to initialize engine: %s\n", kr_strerror(ret));
307 308
		return EXIT_FAILURE;
	}
309
	/* Create worker */
310
	struct worker_ctx *worker = init_worker(loop, &engine, &pool, forks);
311
	if (!worker) {
312
		log_error("[system] not enough memory\n");
313 314
		return EXIT_FAILURE;
	}
315
	/* Bind to sockets and run */
316 317 318
	for (size_t i = 0; i < addr_set.len; ++i) {
		int port = 53;
		const char *addr = set_addr(addr_set.at[i], &port);
319 320
		ret = network_listen(&engine.net, addr, (uint16_t)port, NET_UDP|NET_TCP);
		if (ret != 0) {
321
			log_error("[system] bind to '%s#%d' %s\n", addr, port, knot_strerror(ret));
322 323 324
			ret = EXIT_FAILURE;
		}
	}
Marek Vavruša's avatar
Marek Vavruša committed
325
	/* Start the scripting engine */
326
	if (ret == 0) {
Marek Vavruša's avatar
Marek Vavruša committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340
		ret = engine_start(&engine);
		if (ret == 0) {
			if (keyfile) {
				auto_free char *cmd = afmt("trust_anchors.file = '%s'", keyfile);
				if (!cmd) {
					log_error("[system] not enough memory\n");
					return EXIT_FAILURE;
				}
				engine_cmd(&engine, cmd);
				lua_settop(engine.L, 0);
			}
			/* Run the event loop */
			ret = run_worker(loop, &engine);
		}
341
	}
342
	/* Cleanup. */
343
	array_clear(addr_set);
344
	engine_deinit(&engine);
345 346
	worker_reclaim(worker);
	mp_delete(pool.ctx);
347 348 349
	if (ret != 0) {
		ret = EXIT_FAILURE;
	}
350
	kr_crypto_cleanup();
351 352
	return ret;
}