From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id A80E67030C; Fri, 5 Mar 2021 15:32:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A80E67030C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1614947558; bh=2VaLWhKu33yON7skB96+O7Xam6Oxf+hGqxzlzFKw53U=; h=To:References:Date:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=ze/NkfU0VZ/Lx24ncNhCQdEG4OWvyg8PKnlJoiWkUK54wAipVOoAtMu7WMzyRDPP3 5WXr1LUb7+3u87H9UujdRYngQKitM120lexo4h69VRLO6J7jt9zXKtcPu2B49A7B4J OaJQjGchPIFE3pz1NaI/EDluDyFwTf4ZN40HRyiU= Received: from smtp32.i.mail.ru (smtp32.i.mail.ru [94.100.177.92]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id DEA3D7030C for ; Fri, 5 Mar 2021 15:32:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DEA3D7030C Received: by smtp32.i.mail.ru with esmtpa (envelope-from ) id 1lI9dD-0006Ih-Ov; Fri, 05 Mar 2021 15:32:36 +0300 To: Artem Starshov , Alexander Turenko References: Message-ID: Date: Fri, 5 Mar 2021 15:31:49 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9D3134714A9BDB69BE66984610072BB1D09A72A4B5C8F966400894C459B0CD1B9B38E898F70F0298C87E50780AF26DBAC42343EC933D7C2E375C527A837F11872 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C579B1C3ABE6C709C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE78C6616F30072131EEA1F7E6F0F101C674E70A05D1297E1BBC6CDE5D1141D2B1C71D319092359F446FD1BD250452ECB20F1B63467AD5030F39FA2833FD35BB23D9E625A9149C048EE9ECD01F8117BC8BEA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE4B6963042765DA4BA7B8E9D6D956BB52D8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3ABB3490C75E892802D242C3BD2E3F4C6C4224003CC836476EA7A3FFF5B025636A7F4EDE966BC389F9E8FC8737B5C22497ECBC18655D52CDF089D37D7C0E48F6CCF19DD082D7633A0E7DDDDC251EA7DABAAAE862A0553A39223F8577A6DFFEA7C054662F8F2CA352F43847C11F186F3C5E7DDDDC251EA7DABCC89B49CDF41148FA8EF81845B15A4842623479134186CDE6BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: 0D63561A33F958A509B007F44C79E6CC26FD90AF8842529CE7427928C882A5BBD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75448CF9D3A7B2C848410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C786159FDC4342B0293CE78CB46E90DCFAAD2F5933AC462AEB4168D110A78F95DAB52C783BCDA6871D7E09C32AA3244C589CA61534CB86479F5BDB870599093960759606DA2E136A927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojXmjzTEesUQH/GplpApPyhg== X-Mailru-Sender: 48EFD2E18A91C559803250E574CC3D29814AE32B1DACEB3F862B752F44D65845CA71892AC981DD5FD35D514C7DAC97282D063C67CFD4E84987597EC79699C0E6F21BD4036121A32F7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCHv3 2/2] lua: fix tarantool -e always enters interactive mode X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Leonid Vasiliev via Tarantool-patches Reply-To: Leonid Vasiliev Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi! Thank you for the patch. LGTM. On 3/4/21 12:15 PM, Artem Starshov wrote: > The reason why tarantool -e always enters interactive mode is that > statement after option -e isn't considered as a script. > > In man PUC-Rio lua there are different names for statement -e (stat) > and script, but they have the same behavior regarding interactive > mode. (Also cases, when interpreter loads stdin, have the same behaviour). > > NOTE: test for this code fix uses errinjs, and the last one should work only > in debug mode, so added `release_disabled` in suite.ini. But there is a bug in > test-run: `release_disable` disables tests at each build type. Partially this > problem is descripted in tarantool/test-run#199. > > Fixes #5040 > --- > @ChangeLog: Fixed -e option, when tarantool always entered interactive mode when > stdin is a tty. Now, `tarantool -e 'print"Hello"'` doesn't enter interactive mode > as it was before. > > src/lib/core/errinj.h | 1 + > src/lua/init.c | 27 +++- > ...5040-inter-mode-isatty-via-errinj.test.lua | 126 ++++++++++++++++++ > test/app-tap/suite.ini | 1 + > test/box/errinj.result | 1 + > 5 files changed, 152 insertions(+), 4 deletions(-) > create mode 100755 test/app-tap/gh-5040-inter-mode-isatty-via-errinj.test.lua > > diff --git a/src/lib/core/errinj.h b/src/lib/core/errinj.h > index 10edbe2b9..f131a5cf6 100644 > --- a/src/lib/core/errinj.h > +++ b/src/lib/core/errinj.h > @@ -149,6 +149,7 @@ struct errinj { > _(ERRINJ_AUTO_UPGRADE, ERRINJ_BOOL, {.bparam = false})\ > _(ERRINJ_COIO_WRITE_CHUNK, ERRINJ_BOOL, {.bparam = false}) \ > _(ERRINJ_APPLIER_SLOW_ACK, ERRINJ_BOOL, {.bparam = false}) \ > + _(ERRINJ_STDIN_ISATTY, ERRINJ_INT, {.iparam = -1}) \ > > ENUM0(errinj_id, ERRINJ_LIST); > extern struct errinj errinjs[]; > diff --git a/src/lua/init.c b/src/lua/init.c > index 25e8884a6..4729ad7a2 100644 > --- a/src/lua/init.c > +++ b/src/lua/init.c > @@ -62,6 +62,7 @@ > #include "lua/swim.h" > #include "lua/decimal.h" > #include "digest.h" > +#include "errinj.h" > #include > > #include > @@ -583,6 +584,7 @@ run_script_f(va_list ap) > */ > struct diag *diag = va_arg(ap, struct diag *); > bool aux_loop_is_run = false; > + bool is_option_e_ran = false; > > /* > * Load libraries and execute chunks passed by -l and -e > @@ -613,6 +615,7 @@ run_script_f(va_list ap) > if (luaT_call(L, 0, 0) != 0) > goto error; > lua_settop(L, 0); > + is_option_e_ran = true; > break; > default: > unreachable(); /* checked by getopt() in main() */ > @@ -627,25 +630,41 @@ run_script_f(va_list ap) > fiber_sleep(0.0); > aux_loop_is_run = true; > > + /* > + * Override return value of isatty(STDIN_FILENO) if > + * ERRINJ_STDIN_ISATTY enabled (iparam not set to default). > + * Use iparam in such case, standard return value otherwise. > + * Integer param of errinj is used in order to set different > + * return values. > + */ > + int is_a_tty; > + struct errinj *inj = errinj(ERRINJ_STDIN_ISATTY, ERRINJ_INT); > + if (inj != NULL && inj->iparam >= 0) { > + is_a_tty = inj->iparam; > + } else { > + is_a_tty = isatty(STDIN_FILENO); > + } > + > if (path && strcmp(path, "-") != 0 && access(path, F_OK) == 0) { > /* Execute script. */ > if (luaL_loadfile(L, path) != 0) > goto luajit_error; > if (lua_main(L, argc, argv) != 0) > goto error; > - } else if (!isatty(STDIN_FILENO) || (path && strcmp(path, "-") == 0)) { > + } else if (!is_a_tty || (path && strcmp(path, "-") == 0)) { > /* Execute stdin */ > if (luaL_loadfile(L, NULL) != 0) > goto luajit_error; > if (lua_main(L, argc, argv) != 0) > goto error; > - } else { > + } else if (!is_option_e_ran) { > interactive = true; > } > > /* > - * Start interactive mode when it was explicitly requested > - * by "-i" option or stdin is TTY or there are no script. > + * Start interactive mode in any of the cases: > + * - it was explicitly requested by "-i" option; > + * - stdin is TTY and there are no script (-e is considered as a script). > */ > if (interactive) { > say_crit("%s %s\ntype 'help' for interactive help", > diff --git a/test/app-tap/gh-5040-inter-mode-isatty-via-errinj.test.lua b/test/app-tap/gh-5040-inter-mode-isatty-via-errinj.test.lua > new file mode 100755 > index 000000000..25e2bbc5e > --- /dev/null > +++ b/test/app-tap/gh-5040-inter-mode-isatty-via-errinj.test.lua > @@ -0,0 +1,126 @@ > +#!/usr/bin/env tarantool > + > +local process_timeout = require('process_timeout') > +local ffi = require('ffi') > +local tap = require('tap') > +local fio = require('fio') > + > +-- > +-- Tests to check if the tarantool binary enters > +-- interactive mode or not using error injections > +-- to change return value of isatty(stdin). > +-- > + > +local TARANTOOL_PATH = arg[-1] > +local output_file = fio.abspath('out.txt') > +local cmd_end = (' >%s & echo $!'):format(output_file) > + > +-- Like a default timeout for `cond_wait` in test-run > +local process_waiting_timeout = 60.0 > +local file_read_timeout = 60.0 > +local file_read_interval = 0.2 > +local file_open_timeout = 60.0 > + > +-- Each testcase consists of: > +-- * cmd_args - command line arguments for tarantool binary > +-- * stdin - stdin for tarantool > +-- * interactive - true if interactive mode expected > +-- * empty_output - true if command should have empty output > +local testcases = { > + { > + cmd_args = '', > + stdin = 'tty', > + interactive = true > + }, > + { > + cmd_args = '', > + stdin = '/dev/null', > + interactive = false, > + empty_output = true > + }, > + > + { > + cmd_args = ' -e "print(_VERSION)"', > + stdin = 'tty', > + interactive = false > + }, > + { > + cmd_args = ' -e "print(_VERSION)"', > + stdin = '/dev/null', > + interactive = false > + }, > + > + { > + cmd_args = ' -i -e "print(_VERSION)"', > + stdin = 'tty', > + interactive = true > + }, > + { > + cmd_args = ' -i -e "print(_VERSION)"', > + stdin = '/dev/null', > + interactive = true > + } > +} > + > +local test = tap.test('gh-5040') > + > +test:plan(#testcases) > +for _, cmd in pairs(testcases) do > + local full_cmd = '' > + if cmd.stdin == 'tty' then > + cmd.stdin = '' > + full_cmd = 'ERRINJ_STDIN_ISATTY=1 ' > + else > + cmd.stdin = '< ' .. cmd.stdin > + end > + > + local full_cmd = full_cmd .. ('%s %s %s %s'):format( > + TARANTOOL_PATH, > + cmd.cmd_args, > + cmd.stdin, > + cmd_end > + ) > + test:test(full_cmd, function(test) > + test:plan(cmd.interactive and 1 or 2) > + > + local pid = tonumber(io.popen(full_cmd):read("*line")) > + assert(pid, "pipe error for: " .. cmd.cmd_args) > + > + local fh = process_timeout.open_with_timeout(output_file, > + file_open_timeout) > + assert(fh, 'error while opening ' .. output_file) > + > + if cmd.interactive then > + local data = process_timeout.read_with_timeout(fh, > + file_read_timeout, > + file_read_interval) > + test:like(data, 'tarantool>', 'interactive mode detected') > + else > + local process_completed = process_timeout.wait_process_completion( > + pid, > + process_waiting_timeout) > + test:ok(process_completed, 'process completed') > + > + -- If empty output expected, then don't wait full file_read_timeout > + -- for non-empty output_file, only a little time to be sure that > + -- file is empty. > + local read_timeout = cmd.empty_output and file_read_interval > + or file_read_timeout > + local data = process_timeout.read_with_timeout(fh, read_timeout, > + file_read_interval) > + if cmd.empty_output then > + test:ok(#data == 0, 'output is empty') > + else > + test:unlike(data, 'tarantool>', > + 'iteractive mode wasn\'t detected') > + end > + end > + if process_timeout.process_is_alive(pid) then > + ffi.C.kill(pid, 9) > + end > + fh:close() > + os.remove(output_file) > + end) > +end > + > +os.exit(test:check() and 0 or 1) > diff --git a/test/app-tap/suite.ini b/test/app-tap/suite.ini > index ccf828503..f791ddc5e 100644 > --- a/test/app-tap/suite.ini > +++ b/test/app-tap/suite.ini > @@ -5,6 +5,7 @@ lua_libs = lua/require_mod.lua lua/serializer_test.lua lua/process_timeout.lua > is_parallel = True > pretest_clean = True > use_unix_sockets_iproto = True > +release_disabled = gh-5040-inter-mode-isatty-via-errinj.test.lua > fragile = { > "retries": 10, > "tests": { > diff --git a/test/box/errinj.result b/test/box/errinj.result > index b8c2476c3..a962dbe2d 100644 > --- a/test/box/errinj.result > +++ b/test/box/errinj.result > @@ -74,6 +74,7 @@ evals > - ERRINJ_SNAP_COMMIT_DELAY: false > - ERRINJ_SNAP_WRITE_DELAY: false > - ERRINJ_SQL_NAME_NORMALIZATION: false > + - ERRINJ_STDIN_ISATTY: -1 > - ERRINJ_SWIM_FD_ONLY: false > - ERRINJ_TESTING: false > - ERRINJ_TUPLE_ALLOC: false >