diff --git a/src/lua/init.cc b/src/lua/init.cc index c770b209980c52603ab8bc37d8dd8e12956a9b28..f0307a6fa96ae31504dfdff4ada71ba7c4fb65a9 100644 --- a/src/lua/init.cc +++ b/src/lua/init.cc @@ -154,28 +154,6 @@ tarantool_lua_tostring(struct lua_State *L, int index) return lua_tostring(L, index); } -/** - * Redefine lua 'print' built-in to print to the log file - * When printing to the log file, we use 'say_info'. - */ -static int -lbox_print(struct lua_State *L) -{ - RegionGuard region_guard(&fiber()->gc); - struct tbuf *out = tbuf_new(&fiber()->gc); - /* serialize arguments of 'print' Lua built-in to tbuf */ - int top = lua_gettop(L); - for (int i = 1; i <= top; i++) { - tbuf_printf(out, "%s", tarantool_lua_tostring(L, i)); - if (i != top) { - /* Conventional in Lua print() */ - tbuf_append(out, "\t", 1); - } - } - say_info("%.*s", out->size, tbuf_str(out)); - return 0; -} - /** * Redefine lua 'pcall' built-in to correctly handle exceptions, * produced by 'box' C functions. @@ -298,7 +276,6 @@ tarantool_lua_init() luaL_register(L, boxlib_name, boxlib); lua_pop(L, 1); - lua_register(L, "print", lbox_print); lua_register(L, "pcall", lbox_pcall); lua_register(L, "tonumber64", lbox_tonumber64); @@ -574,17 +551,17 @@ tarantool_lua_load_cfg(struct tarantool_cfg *cfg) } /** - * Load start-up file routine. + * Execute start-up script. */ static void -load_init_script(va_list ap) +run_script(va_list ap) { struct lua_State *L = va_arg(ap, struct lua_State *); const char *path = va_arg(ap, const char *); /* - * Return control to tarantool_lua_load_init_script. - * tarantool_lua_load_init_script then will start an auxiliary event + * Return control to tarantool_lua_run_script. + * tarantool_lua_run_script then will start an auxiliary event * loop and re-schedule this fiber. */ fiber_sleep(0.0); @@ -605,48 +582,13 @@ load_init_script(va_list ap) /* * Lua script finished. Stop the auxiliary event loop and - * return control back to tarantool_lua_load_init_script. + * return control back to tarantool_lua_run_script. */ ev_break(loop(), EVBREAK_ALL); } -#if 0 -/** - * Unset functions in the Lua state which can be used to - * execute external programs or otherwise introduce a breach - * in security. - * - * @param L is a Lua State. - */ -static void -tarantool_lua_sandbox(struct lua_State *L) -{ - /* - * Unset some functions for security reasons: - * 1. Some os.* functions (like os.execute, os.exit, etc..) - * 2. require(), since it can be used to provide access to ffi - * or anything else we unset in 1. - * 3. package, because it can be used to invoke require or to get - * any builtin module using package.loaded - */ - int result = tarantool_lua_dostring(L, - "os.execute = nil\n" - "os.exit = nil\n" - "os.rename = nil\n" - "os.tmpname = nil\n" - "os.remove = nil\n" - "ffi = nil\n" - "io = nil\n" - "require = nil\n" - "package = nil\n"); - - if (result) - panic("%s", lua_tostring(L, -1)); -} -#endif - void -tarantool_lua_load_init_script(char *path) +tarantool_lua_run_script(char *path) { if (path == NULL) return; @@ -657,21 +599,14 @@ tarantool_lua_load_init_script(char *path) * To work this problem around we must run init script in * a separate fiber. */ - struct fiber *loader = fiber_new(basename(path), load_init_script); + struct fiber *loader = fiber_new(basename(path), run_script); fiber_call(loader, tarantool_L, path); /* - * Run an auxiliary event loop to re-schedule load_init_script fiber. + * Run an auxiliary event loop to re-schedule run_script fiber. * When this fiber finishes, it will call ev_break to stop the loop. */ ev_run(loop(), 0); - -#if 0 - /* Outside the startup file require() or ffi are not - * allowed. - */ - tarantool_lua_sandbox(tarantool_L); -#endif } void diff --git a/src/lua/init.h b/src/lua/init.h index e9dd5dc89a36d02acbc53db5184ac34b3ba79e53..00944ac013f5e0f7ade25e49c544a733779607ba 100644 --- a/src/lua/init.h +++ b/src/lua/init.h @@ -81,7 +81,7 @@ tarantool_lua_load_cfg(struct tarantool_cfg *cfg); * @param L is a Lua State. */ void -tarantool_lua_load_init_script(char *path); +tarantool_lua_run_script(char *path); void tarantool_lua(struct lua_State *L, diff --git a/src/tarantool.cc b/src/tarantool.cc index cd5f569d2b699a487f01d5f209cbf897ffbbb223..255f8770ed0763e5584b813dc27e42612caca1bd 100644 --- a/src/tarantool.cc +++ b/src/tarantool.cc @@ -69,7 +69,7 @@ extern "C" { static pid_t master_pid; const char *cfg_filename = NULL; char *cfg_filename_fullpath = NULL; -char *shebang = NULL; +char *script = NULL; char *custom_proc_title; char status[64] = "unknown"; char **main_argv; @@ -523,8 +523,8 @@ tarantool_free(void) tarantool_lua_free(); box_free(); - if (shebang) - free(shebang); + if (script) + free(script); if (cfg_filename_fullpath) free(cfg_filename_fullpath); free_proc_title(main_argc, main_argv); @@ -610,11 +610,11 @@ main(int argc, char **argv) * split multiple options, so "-a -b" comes as * a single value in argv[1]. * - in case one uses #!/usr/bin/env tarantool - * such options (in shebang line) don't work + * such options (in script line) don't work */ argv++; argc--; - shebang = abspath(argv[0]); + script = abspath(argv[0]); random_init(); say_init(argv[0]); @@ -738,7 +738,7 @@ main(int argc, char **argv) * is why script must run only after the server was fully * initialized. */ - tarantool_lua_load_init_script(shebang); + tarantool_lua_run_script(script); start_loop = ev_activecnt(loop()) > events; region_free(&fiber()->gc); if (start_loop) { diff --git a/test/box/print.result b/test/box/print.result index addb4b842292c4d940c696c4c3b1501820da1a65..655d1a4d37c340b32e5826209ad52ac869150408 100644 --- a/test/box/print.result +++ b/test/box/print.result @@ -1,18 +1,10 @@ print("Hello, world") --- ... -box.fiber.wrap(function() print("Hello, world") end) +io = require('io') --- -- null -... -box.fiber.sleep(0.1) ---- -... -Check log line ---- -- "line contains 'Hello'" ... -box.fiber.wrap(function() print("Ehllo, world") end) +box.fiber.wrap(function() print("Ehllo, world") io.flush() end) --- - null ... @@ -21,16 +13,9 @@ box.fiber.sleep(0.1) ... Check log line --- -- "line doesn't contain 'Hello'" -... -box.fiber.wrap(function() print() end) ---- -- null -... -box.fiber.sleep(0.1) ---- +- "line contains 'Hello'" ... Check log line --- -- "line doesn't contain 'PPPPPPPP'" +- "line contains 'Ehllo'" ... diff --git a/test/box/print.test.py b/test/box/print.test.py index b61a7c344a36c9e58923ec25ebfc0e516cd29df2..25ad53d55e6ce7fb8209b13e3bb872767f62c70d 100644 --- a/test/box/print.test.py +++ b/test/box/print.test.py @@ -6,38 +6,30 @@ import re admin('print("Hello, world")') +admin("io = require('io')") log = server.logfile f = open(log, "r") f.seek(0, 2) -admin('box.fiber.wrap(function() print("Hello, world") end)') +admin('box.fiber.wrap(function() print("Ehllo, world") io.flush() end)') admin('box.fiber.sleep(0.1)') line = f.readline() print("Check log line") print("---") -if re.search('(Hello)', line).start(1) > 0: +found = re.search(r'(Hello)', line) +if found and re.search(r'(Hello)', line).start(1) >= 0: print("""- "line contains 'Hello'" """) print("...") - -admin('box.fiber.wrap(function() print("Ehllo, world") end)') -admin('box.fiber.sleep(0.1)') -line = f.readline() -print("Check log line") -print("---") -if re.search('(Hello)', line): - print("""- "line contains 'Hello'" """) else: - print("""- "line doesn't contain 'Hello'" """) -print("...") + print('String "%s" does not contain "Hello"' % line) -admin('box.fiber.wrap(function() print() end)') -admin('box.fiber.sleep(0.1)') line = f.readline() print("Check log line") print("---") -if re.search('(PPPPPPPP)', line): - print("""- "line contains 'PPPPPPPP'" """) +if re.search('(Ehllo)', line): + print("""- "line contains 'Ehllo'" """) else: - print("""- "line doesn't contain 'PPPPPPPP'" """) + print("""- "line doesn't contain 'Ehllo'" """) print("...") +