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 637936EC58; Thu, 18 Feb 2021 22:31:27 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 637936EC58 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1613676687; bh=j3Mspm0o4+oNfLAvKbRkW7IyIz6NArbUwSMjBfGnt00=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=VzCY6ezYR/fIa1n0Ya13Rpv6Zl/s44ClIpxlqImUPBGQ+7HCy7psx0jW+pDP0+5t/ rHuDVXHImJUg5PZzM2nNcHzb2t7uN0EiJLdyydiphgwiUpQ8yWKcgz6o1sj7igdMUG LIze28JbdNr9JgW0odds72CFwfztsuYfr7ft7hzo= Received: from smtp53.i.mail.ru (smtp53.i.mail.ru [94.100.177.113]) (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 986D96C186 for ; Thu, 18 Feb 2021 22:30:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 986D96C186 Received: by smtp53.i.mail.ru with esmtpa (envelope-from ) id 1lCp0N-0001dz-Kp; Thu, 18 Feb 2021 22:30:28 +0300 To: Alexander Turenko , Sergey Bronnikov Date: Thu, 18 Feb 2021 22:30:19 +0300 Message-Id: <612d38a3f3349c1c8cb6d3f549fd85720b7a1a23.1613674486.git.artemreyt@tarantool.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD975C3EC174F56692243410BA6471F01664CBE9F2A03BCE963182A05F5380850401E77B4C302E576B94E4E18A4880617ABE95C9C645453A4FF803EE63837D27569 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE705C173BDDADFC82AEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378F6D32451C4A3CAA8638F802B75D45FF5571747095F342E8C7A0BC55FA0FE5FC12ACF8D6BDD292359978726518BF2D51BA9F1510BD43FBCB389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C0A29E2F051442AF778941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6A70DDFFB3186CBC5CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8D32BA5DBAC0009BE9E8FC8737B5C224957B6FA06B2BC03E176E601842F6C81A12EF20D2F80756B5F7E9C4E3C761E06A776E601842F6C81A127C277FBC8AE2E8BFACEAEEF4D0B62133AA81AA40904B5D9DBF02ECDB25306B2B25CBF701D1BE8734AD6D5ED66289B5278DA827A17800CE7CA4B174CFBB40A0467F23339F89546C5A8DF7F3B2552694A6FED454B719173D6725E5C173C3A84C3C3E5FCF1FE4BE79D35872C767BF85DA2F004C906525384306FED454B719173D6462275124DF8B9C9DE2850DD75B2526BE5BFE6E7EFDEDCD789D4C264860C145E X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46D08ED0C15A2537EB5355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CF81F587C2CCB1933F314D815A0AE28F3001534041AC67B939C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF31C0090ACECF247D699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34AF61ACC197BF517A9A817E37D6F3047125778A7D7684738EEB3B0C48300D489C84FA96FE871C39C11D7E09C32AA3244CE3CE685454B3284BBE57AB2F42BD24C98894E9C85370243E83B48618A63566E0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXg4WSo14jlZAKO9FjVh7v3T X-Mailru-Sender: 65CD03CFE33A0EC7E4F6C6702325C427094F778B4ECEAC848929AD89EF871C5133BF3A5CCF92E6A5B08A37556DE0CB7BB7E364AFDB3F206FD953F29DB7D76D89B1E6F68DAE724AB9112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCHv2 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: Artem Starshov via Tarantool-patches Reply-To: Artem Starshov Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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). 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 | 20 ++- ...40-always-enters-interactive-mode.test.lua | 131 ++++++++++++++++++ test/box/errinj.result | 1 + 4 files changed, 149 insertions(+), 4 deletions(-) create mode 100755 test/app-tap/gh-5040-always-enters-interactive-mode.test.lua diff --git a/src/lib/core/errinj.h b/src/lib/core/errinj.h index d76aedf7a..d5fdbc209 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..e7e0fe006 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 option_e_runned = 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); + option_e_runned = true; break; default: unreachable(); /* checked by getopt() in main() */ @@ -627,25 +630,34 @@ run_script_f(va_list ap) fiber_sleep(0.0); aux_loop_is_run = true; + int is_atty; + struct errinj *inj = errinj(ERRINJ_STDIN_ISATTY, ERRINJ_INT); + if (inj != NULL && inj->iparam >= 0) { + is_atty = inj->iparam; + } else { + is_atty = 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_atty || (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 (!option_e_runned) { 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-always-enters-interactive-mode.test.lua b/test/app-tap/gh-5040-always-enters-interactive-mode.test.lua new file mode 100755 index 000000000..df3ee2f2d --- /dev/null +++ b/test/app-tap/gh-5040-always-enters-interactive-mode.test.lua @@ -0,0 +1,131 @@ +#!/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. +-- + +local build_target = require('tarantool').build.target +local is_debug = build_target:match('Debug$') ~= nil + +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') + +if not is_debug then + test:plan(1) + test:skip('This test runs only with Debug build. Build: ' .. build_target) + os.exit(test:check() and 0 or 1) +end + +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/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 -- 2.28.0