]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
Allow to override hostname() with a lua call hostname("example.com")
authorOndřej Surý <ondrej@sury.org>
Sat, 12 Nov 2016 05:25:12 +0000 (06:25 +0100)
committerOndřej Surý <ondrej@sury.org>
Sat, 12 Nov 2016 09:11:37 +0000 (10:11 +0100)
daemon/README.rst
daemon/engine.c
daemon/engine.h

index 12619e685b88464a8127d243392dad90d18798b1..b3958eecc28831a9f285eb942bc9c06fc3efc4f8 100644 (file)
@@ -369,10 +369,15 @@ Environment
 
        env.USER -- equivalent to $USER in shell
 
-.. function:: hostname()
+.. function:: hostname([fqdn])
 
    :return: Machine hostname.
 
+   If called with a parameter, it will set kresd's internal
+   hostname. If called without a parameter, it will return kresd's
+   internal hostname, or the system's POSIX hostname (see
+   gethostname(2)) if kresd's internal hostname is unset.
+           
 .. function:: verbose(true | false)
 
    :return: Toggle verbose logging.
index 9977f434ede7feb9a7c8e55eae18612fecc3c215..e4d05d0115f928d108957296084065d8538d1d5f 100644 (file)
@@ -147,12 +147,56 @@ static int l_verbose(lua_State *L)
        return 1;
 }
 
+char *engine_get_hostname(struct engine *engine) {
+       static char hostname_str[KNOT_DNAME_MAXLEN];
+       if (!engine) {
+               return NULL;
+       }
+
+       if (!engine->hostname) {
+               if (gethostname(hostname_str, sizeof(hostname_str)) != 0)
+                       return NULL;
+               return hostname_str;
+       }
+       return engine->hostname;
+}
+
+int engine_set_hostname(struct engine *engine, const char *hostname) {
+       if (!engine || !hostname) {
+               return kr_error(EINVAL);
+       }
+
+       char *new_hostname = strdup(hostname);
+       if (!new_hostname) {
+               return kr_error(ENOMEM);
+       }
+       if (engine->hostname) {
+               free(engine->hostname);
+       }
+       engine->hostname = new_hostname;
+
+       return 0;
+}
+
 /** Return hostname. */
 static int l_hostname(lua_State *L)
 {
-       char host_str[KNOT_DNAME_MAXLEN];
-       gethostname(host_str, sizeof(host_str));
-       lua_pushstring(L, host_str);
+       struct engine *engine = engine_luaget(L);
+       if (lua_gettop(L) == 0) {
+               lua_pushstring(L, engine_get_hostname(engine));
+               return 1;
+       }
+       if ((lua_gettop(L) != 1) || !lua_isstring(L, 1)) {
+               lua_pushstring(L, "hostname takes at most one parameter: (\"fqdn\")");
+               lua_error(L);
+       }
+
+       if (engine_set_hostname(engine, lua_tostring(L, 1)) != 0) {
+               lua_pushstring(L, "setting hostname failed");
+               lua_error(L);
+       }
+
+       lua_pushstring(L, engine_get_hostname(engine)); 
        return 1;
 }
 
index 01809380d71d33d80dfcbccf252d790bd14cc3a7..dc2fc57a081b0eed18775b48120d89a760aa7b6a 100644 (file)
@@ -59,6 +59,7 @@ struct engine {
     fd_array_t ipc_set;
     knot_mm_t *pool;
     uv_timer_t *updater;
+    char *hostname;
     struct lua_State *L;
 };
 
@@ -78,3 +79,7 @@ int engine_pcall(struct lua_State *L, int argc);
 
 /** Return engine light userdata. */
 struct engine *engine_luaget(struct lua_State *L);
+
+/** Set/get the per engine hostname */
+char *engine_get_hostname(struct engine *engine);
+int engine_set_hostname(struct engine *engine, const char *hostname);