engine.h 3.01 KB
Newer Older
1
/*  Copyright (C) 2015-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    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
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16 17 18
 */

#pragma once

19 20 21 22
/*
 * @internal These are forward decls to allow building modules with engine but without Lua.
 */
struct lua_State;
23

24
#include "lib/utils.h"
25
#include "lib/resolve.h"
26
#include "daemon/network.h"
27

28 29 30
/* @internal Array of file descriptors shorthand. */
typedef array_t(int) fd_array_t;

31 32
struct engine {
    struct kr_context resolver;
33
    struct network net;
34
    module_array_t modules;
35
    array_t(const struct kr_cdb_api *) backends;
36
    fd_array_t ipc_set;
37
    knot_mm_t *pool;
38
    char *hostname;
39
    struct lua_State *L;
40
    char *moduledir;
41 42
};

43
int engine_init(struct engine *engine, knot_mm_t *pool);
44
void engine_deinit(struct engine *engine);
45 46 47 48 49 50

/** Perform a lua command within the sandbox.
 *
 *  @return zero on success.
 *  The result will be returned on the lua stack - an error message in case of failure.
 *  http://www.lua.org/manual/5.1/manual.html#lua_pcall */
51
int engine_cmd(struct lua_State *L, const char *str, bool raw);
52 53 54 55

/** Execute current chunk in the sandbox */
int engine_pcall(struct lua_State *L, int argc);

56
int engine_ipc(struct engine *engine, const char *expr);
57

58 59 60 61 62 63 64

int engine_load_sandbox(struct engine *engine);
int engine_loadconf(struct engine *engine, const char *config_path);
int engine_load_defaults(struct engine *engine);

/** Start the lua engine and execute the config. */
int engine_start(struct engine *engine);
65
void engine_stop(struct engine *engine);
66 67
int engine_register(struct engine *engine, const char *name, const char *precedence, const char* ref);
int engine_unregister(struct engine *engine, const char *name);
68
void engine_lualib(struct engine *engine, const char *name, int (*lib_cb) (struct lua_State *));
69

70

71
/** Return engine light userdata. */
72
struct engine *engine_luaget(struct lua_State *L);
73 74 75 76

/** Set/get the per engine hostname */
char *engine_get_hostname(struct engine *engine);
int engine_set_hostname(struct engine *engine, const char *hostname);
77 78 79 80

/** Set/get the per engine moduledir */
char *engine_get_moduledir(struct engine *engine);
int engine_set_moduledir(struct engine *engine, const char *moduledir);
81 82 83 84

/** Load root hints from a zonefile (or config-time default if NULL).
 *
 * @return error message or NULL (statically allocated)
85
 * @note exported to be usable from the hints module.
86
 */
87
KR_EXPORT
88
const char* engine_hint_root_file(struct kr_context *ctx, const char *file);
89