Skip to content
Snippets Groups Projects
Commit b7497f31 authored by Ondřej Surý's avatar Ondřej Surý
Browse files

Allow to override hostname() with a lua call hostname("example.com")

parent 9c200269
Branches
Tags
1 merge request!72Allow to override hostname() with a lua call hostname("example.com")
......@@ -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.
......
......@@ -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;
}
......
......@@ -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);
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment