From: Ondřej Surý Date: Sat, 12 Nov 2016 05:25:12 +0000 (+0100) Subject: Allow to override hostname() with a lua call hostname("example.com") X-Git-Tag: v1.2.0-rc1~80^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7497f318a18c67fce5773d9dedda0fd0023c21d;p=thirdparty%2Fknot-resolver.git Allow to override hostname() with a lua call hostname("example.com") --- diff --git a/daemon/README.rst b/daemon/README.rst index 12619e685..b3958eecc 100644 --- a/daemon/README.rst +++ b/daemon/README.rst @@ -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. diff --git a/daemon/engine.c b/daemon/engine.c index 9977f434e..e4d05d011 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -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; } diff --git a/daemon/engine.h b/daemon/engine.h index 01809380d..dc2fc57a0 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -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);