]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: -c to set config file outside of rundir
authorMarek Vavruša <marek.vavrusa@nic.cz>
Sun, 18 Oct 2015 21:18:33 +0000 (23:18 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Sun, 18 Oct 2015 21:19:42 +0000 (23:19 +0200)
daemon/engine.c
daemon/engine.h
daemon/main.c

index 7a1ee2137887e58a218495dab9fb5df99c9dd549..954f78eab252bcd1c194fc26b276b93c83ee44a1 100644 (file)
@@ -489,7 +489,7 @@ int engine_cmd(struct engine *engine, const char *str)
 #define l_dosandboxfile(L, filename) \
        (luaL_loadfile((L), (filename)) || engine_pcall((L), 0))
 
-static int engine_loadconf(struct engine *engine)
+static int engine_loadconf(struct engine *engine, const char *config_path)
 {
        /* Use module path for including Lua scripts */
        static const char l_paths[] = "package.path = package.path..';" PREFIX MODULEDIR "/?.lua'";
@@ -507,8 +507,8 @@ static int engine_loadconf(struct engine *engine)
                return kr_error(ENOEXEC);
        }
        /* Load config file */
-       if(access("config", F_OK ) != -1 ) {
-               ret = l_dosandboxfile(engine->L, "config");
+       if(access(config_path, F_OK ) != -1 ) {
+               ret = l_dosandboxfile(engine->L, config_path);
        }
        if (ret == 0) {
                /* Load defaults */
@@ -526,10 +526,10 @@ static int engine_loadconf(struct engine *engine)
        return ret;
 }
 
-int engine_start(struct engine *engine)
+int engine_start(struct engine *engine, const char *config_path)
 {
        /* Load configuration. */
-       int ret = engine_loadconf(engine);
+       int ret = engine_loadconf(engine, config_path);
        if (ret != 0) {
                return ret;
        }
index 66367097d782d9ef522d3861efd16403b7d7effe..05f98da88dd560a9f8ef3869bfca62183f8ebc2c 100644 (file)
@@ -61,7 +61,7 @@ int engine_init(struct engine *engine, mm_ctx_t *pool);
 void engine_deinit(struct engine *engine);
 /** @warning This function leaves 1 string result on stack. */
 int engine_cmd(struct engine *engine, const char *str);
-int engine_start(struct engine *engine);
+int engine_start(struct engine *engine, const char *config_path);
 void engine_stop(struct engine *engine);
 int engine_register(struct engine *engine, const char *module, const char *precedence, const char* ref);
 int engine_unregister(struct engine *engine, const char *module);
index 80ad35c7bcb29daba36d5605801faaf9b1fdbea5..c0b6d3b085e8df823b5a19d844ddea56ee81b43c 100644 (file)
@@ -130,6 +130,7 @@ static void help(int argc, char *argv[])
        printf("Usage: %s [parameters] [rundir]\n", argv[0]);
        printf("\nParameters:\n"
               " -a, --addr=[addr]    Server address (default: localhost#53).\n"
+              " -c, --config=[path]  Config file path (relative to [rundir]) (default: config).\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"
@@ -202,12 +203,14 @@ int main(int argc, char **argv)
        array_t(char*) addr_set;
        array_init(addr_set);
        char *keyfile = NULL;
+       const char *config = NULL;
        static char keyfile_buf[PATH_MAX + 1];
 
        /* Long options. */
        int c = 0, li = 0, ret = 0;
        struct option opts[] = {
                {"addr", required_argument,   0, 'a'},
+               {"config", required_argument, 0, 'c'},
                {"keyfile",required_argument, 0, 'k'},
                {"forks",required_argument,   0, 'f'},
                {"verbose",    no_argument,   0, 'v'},
@@ -215,12 +218,15 @@ int main(int argc, char **argv)
                {"help",      no_argument,    0, 'h'},
                {0, 0, 0, 0}
        };
-       while ((c = getopt_long(argc, argv, "a:f:k:vVh", opts, &li)) != -1) {
+       while ((c = getopt_long(argc, argv, "a:c:f:k:vVh", opts, &li)) != -1) {
                switch (c)
                {
                case 'a':
                        array_push(addr_set, optarg);
                        break;
+               case 'c':
+                       config = optarg;
+                       break;
                case 'f':
                        g_interactive = 0;
                        forks = atoi(optarg);
@@ -262,7 +268,7 @@ int main(int argc, char **argv)
        if (optind < argc) {
                const char *rundir = argv[optind];
                if (access(rundir, W_OK) != 0) {
-                       log_error("[system] rundir '%s': not writeable\n", rundir);
+                       log_error("[system] rundir '%s': %s\n", rundir, strerror(errno));
                        return EXIT_FAILURE;
                }
                ret = chdir(rundir);
@@ -270,6 +276,11 @@ int main(int argc, char **argv)
                        log_error("[system] rundir '%s': %s\n", rundir, strerror(errno));
                        return EXIT_FAILURE;
                }
+               if(config && access(config, R_OK) != 0) {
+                       log_error("[system] rundir '%s'\n", rundir);
+                       log_error("[system] config '%s': %s\n", config, strerror(errno));
+                       return EXIT_FAILURE;
+               }
        }
 
        kr_crypto_init();
@@ -324,7 +335,7 @@ int main(int argc, char **argv)
        }
        /* Start the scripting engine */
        if (ret == 0) {
-               ret = engine_start(&engine);
+               ret = engine_start(&engine, config ? config : "config");
                if (ret == 0) {
                        if (keyfile) {
                                auto_free char *cmd = afmt("trust_anchors.file = '%s'", keyfile);