From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladislav Shpilevoy Subject: [PATCH v2 1/4] console: forbid arbitrary session type setting Date: Tue, 11 Dec 2018 19:10:20 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: To: tarantool-patches@freelists.org Cc: vdavydov.dev@gmail.com List-ID: The only place where session type is changed after its creation is console. Console sets 'repl' session type once console.start() is called, or 'console', if a remote client connected to console server using console.connect(). But function to set session type is too common. Using it any other session type can be set. Even console can be changed to applier or binary. Lets split it into two more specific functions setting only 'repl' and 'console'. Also, it simplifies next patches, making vtab part of struct session. --- src/box/lua/console.lua | 4 ++-- src/box/lua/session.c | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua index 028001127..19ea4bb0a 100644 --- a/src/box/lua/console.lua +++ b/src/box/lua/console.lua @@ -448,7 +448,7 @@ local function start() self.history_file = home_dir .. '/.tarantool_history' internal.load_history(self.history_file) end - session_internal.create(1, "repl") -- stdin fileno + session_internal.create_repl(1) -- stdin fileno repl(self) started = false end @@ -521,7 +521,7 @@ local function connect(uri, opts) end local function client_handler(client, peer) - session_internal.create(client:fd(), "console") + session_internal.create_console(client:fd()) session_internal.run_on_connect() session_internal.run_on_auth(box.session.user(), true) local state = setmetatable({ diff --git a/src/box/lua/session.c b/src/box/lua/session.c index d3d27643f..f4944baea 100644 --- a/src/box/lua/session.c +++ b/src/box/lua/session.c @@ -47,22 +47,33 @@ static const char *sessionlib_name = "box.session"; /* Create session and pin it to fiber */ static int -lbox_session_create(struct lua_State *L) +lbox_session_create(struct lua_State *L, enum session_type type) { struct session *session = fiber_get_session(fiber()); if (session == NULL) { session = session_create_on_demand(); if (session == NULL) return luaT_error(L); - session->meta.fd = luaL_optinteger(L, 1, -1); + session->meta.fd = lua_tointeger(L, 1); } - /* If a session already exists, simply reset its type */ - session->type = STR2ENUM(session_type, luaL_optstring(L, 2, "console")); + session->type = type; lua_pushnumber(L, session->id); return 1; } +static int +lbox_session_create_console(struct lua_State *L) +{ + return lbox_session_create(L, SESSION_TYPE_CONSOLE); +} + +static int +lbox_session_create_repl(struct lua_State *L) +{ + return lbox_session_create(L, SESSION_TYPE_REPL); +} + /** * Return a unique monotonic session * identifier. The identifier can be used @@ -446,7 +457,8 @@ void box_lua_session_init(struct lua_State *L) { static const struct luaL_Reg session_internal_lib[] = { - {"create", lbox_session_create}, + {"create_console", lbox_session_create_console}, + {"create_repl", lbox_session_create_repl}, {"run_on_connect", lbox_session_run_on_connect}, {"run_on_disconnect", lbox_session_run_on_disconnect}, {"run_on_auth", lbox_session_run_on_auth}, -- 2.17.2 (Apple Git-113)