diff options
author | kaiwu <kaiwu2004@gmail.com> | 2025-03-01 12:42:23 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2025-03-01 12:42:23 +0800 |
commit | 3f33461e4948bf05e60bdff35ec6c57a649c7860 (patch) | |
tree | 284c2ba95a41536ae1bff6bea710db0709a64739 /lua-resty-shell-0.03 | |
download | openresty-3f33461e4948bf05e60bdff35ec6c57a649c7860.tar.gz openresty-3f33461e4948bf05e60bdff35ec6c57a649c7860.zip |
openresty bundle
Diffstat (limited to 'lua-resty-shell-0.03')
-rw-r--r-- | lua-resty-shell-0.03/README.md | 126 | ||||
-rw-r--r-- | lua-resty-shell-0.03/lib/resty/shell.lua | 195 | ||||
-rw-r--r-- | lua-resty-shell-0.03/t/TestShell.pm | 32 | ||||
-rw-r--r-- | lua-resty-shell-0.03/t/status.t | 69 | ||||
-rw-r--r-- | lua-resty-shell-0.03/t/stderr.t | 98 | ||||
-rw-r--r-- | lua-resty-shell-0.03/t/stdin.t | 39 | ||||
-rwxr-xr-x | lua-resty-shell-0.03/t/stdout.t | 186 | ||||
-rw-r--r-- | lua-resty-shell-0.03/valgrind.suppress | 30 |
8 files changed, 775 insertions, 0 deletions
diff --git a/lua-resty-shell-0.03/README.md b/lua-resty-shell-0.03/README.md new file mode 100644 index 0000000..20ce974 --- /dev/null +++ b/lua-resty-shell-0.03/README.md @@ -0,0 +1,126 @@ +Name +==== + +lua-resty-shell - Lua module for nonblocking system shell command executions + +Table of Contents +================= + +* [Name](#name) +* [Synopsis](#synopsis) +* [Functions](#functions) + * [run](#run) +* [Dependencies](#dependencies) +* [Author](#author) +* [Copyright & Licenses](#copyright--licenses) + +Synopsis +======== + +```lua +local shell = require "resty.shell" + +local stdin = "hello" +local timeout = 1000 -- ms +local max_size = 4096 -- byte + +local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "he\n"; print <>']], stdin, timeout, max_size) +if not ok then + -- ... +end +``` + +Functions +========= + +run +--- + +**syntax:** `ok, stdout, stderr, reason, status = shell.run(cmd, stdin?, timeout?, max_size?)` + +**context:** `all phases supporting yielding` + +Runs a shell command, `cmd`, with an optional stdin. + +The `cmd` argument can either be a single string value (e.g. `"echo 'hello, +world'"`) or an array-like Lua table (e.g. `{"echo", "hello, world"}`). The +former is equivalent to `{"/bin/sh", "-c", "echo 'hello, world'"}`, but simpler +and slightly faster. + +When the `stdin` argument is `nil` or `""`, the stdin device will immediately +be closed. + +The `timeout` argument specifies the timeout threshold (in ms) for +stderr/stdout reading timeout, stdin writing timeout, and process waiting +timeout. + +The `max_size` argument specifies the maximum size allowed for each output +data stream of stdout and stderr. When exceeding the limit, the `run()` +function will immediately stop reading any more data from the stream and return +an error string in the `reason` return value: `"failed to read stdout: too much +data"`. + +Upon terminating successfully (with a zero exit status), `ok` will be `true`, +`reason` will be `"exit"`, and `status` will hold the sub-process exit status. + +Upon terminating abnormally (non-zero exit status), `ok` will be `false`, +`reason` will be `"exit"`, and `status` will hold the sub-process exit status. + +Upon exceeding a timeout threshold or any other unexpected error, `ok` will be +`nil`, and `reason` will be a string describing the error. + +When a timeout threshold is exceeded, the sub-process will be terminated as +such: + +1. first, by receiving a `SIGTERM` signal from this library, +2. then, after 1ms, by receiving a `SIGKILL` signal from this library. + +Note that child processes of the sub-process (if any) will not be terminated. +You may need to terminate these processes yourself. + +When the sub-process is terminated by a UNIX signal, the `reason` return value +will be `"signal"` and the `status` return value will hold the signal number. + +[Back to TOC](#table-of-contents) + +Dependencies +============ + +This library depends on + +* the [lua-resty-signal](https://github.com/openresty/lua-resty-signal) library. +* the [ngx.pipe](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/pipe.md#readme) +API of OpenResty. +* the [lua-tablepool](https://github.com/openresty/lua-tablepool) library. + +[Back to TOC](#table-of-contents) + +Author +====== + +Yichun Zhang (agentzh) <yichun@openresty.com> + +[Back to TOC](#table-of-contents) + +Copyright & Licenses +==================== + +This module is licensed under the BSD license. + +Copyright (C) 2018-2019, [OpenResty Inc.](https://openresty.com) + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +[Back to TOC](#table-of-contents) + diff --git a/lua-resty-shell-0.03/lib/resty/shell.lua b/lua-resty-shell-0.03/lib/resty/shell.lua new file mode 100644 index 0000000..24a457b --- /dev/null +++ b/lua-resty-shell-0.03/lib/resty/shell.lua @@ -0,0 +1,195 @@ +local _M = { + version = 0.03 +} + + +local resty_sig = require "resty.signal" +local ngx_pipe = require "ngx.pipe" +local new_tab = require "table.new" +local tablepool = require "tablepool" + + +local kill = resty_sig.kill +local pipe_spawn = ngx_pipe.spawn +local tostring = tostring +local spawn_thread = ngx.thread.spawn +local wait_thread = ngx.thread.wait +local concat = table.concat +local fetch_tab = tablepool.fetch +local release_tab = tablepool.release +local sleep = ngx.sleep + + +local spawn_opts = { + buffer_size = 1024 * 32 -- 32K +} + + +local tab_pool_tag = "resty.shell" + + +local function cleanup_proc(proc) + local pid = proc:pid() + if pid then + local ok, err = kill(pid, "TERM") + if not ok then + return nil, "failed to kill process " .. pid + .. ": " .. tostring(err) + end + sleep(0.001) -- only wait for 1 msec + kill(pid, "KILL") + end + + return true +end + + +local function concat_err(err1, err2) + return tostring(err1) .. "; " .. tostring(err2) +end + + +local function read_stream(proc, buf, max_size, meth_name) + local pos = 1 + local len = 0 + + while len <= max_size do + local data, err, partial = proc[meth_name](proc, max_size - len + 1) + if not data then + if partial then + buf[pos] = partial + pos = pos + 1 + len = len + #partial + end + + if err == "closed" then + return pos - 1 + end + + return pos - 1, err + end + + buf[pos] = data + pos = pos + 1 + len = len + #data + end + + if len > max_size then + return pos - 1, "too much data" + end + + return pos - 1 +end + + +function _M.run(cmd, stdin, timeout, max_size) + if not max_size then + max_size = 128 * 1024 -- 128KB + end + + local proc, err = pipe_spawn(cmd, spawn_opts) + if not proc then + return nil, nil, nil, "failed to spawn: " .. tostring(err) + end + + proc:set_timeouts(timeout, timeout, timeout, timeout) + + if stdin and stdin ~= "" then + local bytes, err = proc:write(stdin) + if not bytes then + local ok2, err2 = cleanup_proc(proc) + if not ok2 then + err = concat_err(err, err2) + end + return nil, nil, nil, "failed to write to stdin: " .. tostring(err) + end + end + + local ok + ok, err = proc:shutdown("stdin") + if not ok then + local ok2, err2 = cleanup_proc(proc) + if not ok2 then + err = concat_err(err, err2) + end + return nil, nil, nil, "failed to shutdown stdin: " .. tostring(err) + end + + local stdout_tab = fetch_tab(tab_pool_tag, 4, 0) + local stderr_tab = fetch_tab(tab_pool_tag, 4, 0) + + local thr_out = spawn_thread(read_stream, proc, stdout_tab, max_size, + "stdout_read_any") + local thr_err = spawn_thread(read_stream, proc, stderr_tab, max_size, + "stderr_read_any") + + local reason, status + ok, reason, status = proc:wait() + + if ok == nil and reason ~= "exited" then + err = reason + local ok2, err2 = cleanup_proc(proc) + if not ok2 then + err = concat_err(err, err2) + end + + local stdout = concat(stdout_tab) + release_tab(tab_pool_tag, stdout_tab) + + local stderr = concat(stderr_tab) + release_tab(tab_pool_tag, stderr_tab) + + return nil, stdout, stderr, + "failed to wait for process: " .. tostring(err) + end + + local ok2, stdout_pos, err2 = wait_thread(thr_out) + if not ok2 then + local stdout = concat(stdout_tab) + release_tab(tab_pool_tag, stdout_tab) + + local stderr = concat(stderr_tab) + release_tab(tab_pool_tag, stderr_tab) + + return nil, stdout, stderr, "failed to wait stdout thread: " + .. tostring(stdout_pos) + end + + if err2 then + local stdout = concat(stdout_tab, "", 1, stdout_pos) + release_tab(tab_pool_tag, stdout_tab) + + local stderr = concat(stderr_tab) + release_tab(tab_pool_tag, stderr_tab) + + return nil, stdout, stderr, "failed to read stdout: " .. tostring(err2) + end + + local stderr_pos + ok2, stderr_pos, err2 = wait_thread(thr_err) + if not ok2 then + local stdout = concat(stdout_tab, "", 1, stdout_pos) + release_tab(tab_pool_tag, stdout_tab) + + local stderr = concat(stderr_tab) + release_tab(tab_pool_tag, stderr_tab) + + return nil, stdout, stderr, "failed to wait stderr thread: " + .. tostring(stderr_pos) + end + + local stdout = concat(stdout_tab, "", 1, stdout_pos) + release_tab(tab_pool_tag, stdout_tab) + + local stderr = concat(stderr_tab, "", 1, stderr_pos) + release_tab(tab_pool_tag, stderr_tab) + + if err2 then + return nil, stdout, stderr, "failed to read stderr: " .. tostring(err2) + end + + return ok, stdout, stderr, reason, status +end + + +return _M diff --git a/lua-resty-shell-0.03/t/TestShell.pm b/lua-resty-shell-0.03/t/TestShell.pm new file mode 100644 index 0000000..5c608b1 --- /dev/null +++ b/lua-resty-shell-0.03/t/TestShell.pm @@ -0,0 +1,32 @@ +package t::TestShell; + +use v5.10.1; +use Test::Nginx::Socket::Lua -Base; + +add_block_preprocessor(sub { + my $block = shift; + + my $http_config = $block->http_config // ''; + my $init_by_lua_block = $block->init_by_lua_block // 'require "resty.core"'; + + $http_config .= <<_EOC_; + + lua_package_path "./lib/?.lua;../lua-tablepool/lib/?.lua;../lua-resty-signal/lib/?.lua;../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;"; + lua_package_cpath "../lua-resty-signal/?.so;;"; + init_by_lua_block { + $init_by_lua_block + } +_EOC_ + + $block->set_value("http_config", $http_config); + + if (!defined $block->error_log) { + $block->set_value("no_error_log", "[error]"); + } + + if (!defined $block->request) { + $block->set_value("request", "GET /t"); + } +}); + +1; diff --git a/lua-resty-shell-0.03/t/status.t b/lua-resty-shell-0.03/t/status.t new file mode 100644 index 0000000..f497571 --- /dev/null +++ b/lua-resty-shell-0.03/t/status.t @@ -0,0 +1,69 @@ +# vi:ft= + +use lib '.'; +use t::TestShell; + +plan tests => 3 * blocks(); + +no_long_string(); +#no_diff(); + +run_tests(); + +__DATA__ + +=== TEST 1: exit 1 +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "he\n"; print "yes"; exit 1']], nil, 2000) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: false +stdout: yes +stderr: he + +reason: exit +status: 1 + + + +=== TEST 2: exit 255 +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'print "yes"; die;']], nil, 2000) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: false +stdout: yes +stderr: Died at -e line 1. + +reason: exit +status: 255 diff --git a/lua-resty-shell-0.03/t/stderr.t b/lua-resty-shell-0.03/t/stderr.t new file mode 100644 index 0000000..b16ce84 --- /dev/null +++ b/lua-resty-shell-0.03/t/stderr.t @@ -0,0 +1,98 @@ +# vi:ft= + +use lib '.'; +use t::TestShell; + +plan tests => 3 * blocks(); + +no_long_string(); +#no_diff(); + +run_tests(); + +__DATA__ + +=== TEST 1: too much stderr data (1 byte more) +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "hel\n"; print "yes"']], nil, 3000, 3) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: nil +stdout: yes +stderr: hel + +reason: failed to read stderr: too much data +status: nil + + + +=== TEST 2: too much stderr data (several bytes more) +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "hello world\n"; print "yes"']], + nil, 3000, 3) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: nil +stdout: yes +stderr: hell +reason: failed to read stderr: too much data +status: nil + + + +=== TEST 3: stderr timeout +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'print "yes"; sleep 10; warn "he\n";']], + nil, 1, 3) + say("ok: ", ok) + say("stdout: '", stdout, "'") + say("stderr: '", stderr, "'") + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: nil +stdout: '' +stderr: '' +reason: failed to wait for process: timeout +status: nil diff --git a/lua-resty-shell-0.03/t/stdin.t b/lua-resty-shell-0.03/t/stdin.t new file mode 100644 index 0000000..4ee38fd --- /dev/null +++ b/lua-resty-shell-0.03/t/stdin.t @@ -0,0 +1,39 @@ +# vi:ft= + +use lib '.'; +use t::TestShell; + +plan tests => 3 * blocks(); + +no_long_string(); +#no_diff(); + +run_tests(); + +__DATA__ + +=== TEST 1: good case +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'my $ln = <>; print $ln']], "hello", 3000) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: true +stdout: hello +stderr: +reason: exit +status: 0 diff --git a/lua-resty-shell-0.03/t/stdout.t b/lua-resty-shell-0.03/t/stdout.t new file mode 100755 index 0000000..ba70ad8 --- /dev/null +++ b/lua-resty-shell-0.03/t/stdout.t @@ -0,0 +1,186 @@ +# vi:ft= + +use lib '.'; +use t::TestShell; + +plan tests => 3 * blocks(); + +no_long_string(); +#no_diff(); + +run_tests(); + +__DATA__ + +=== TEST 1: good case (single shell cmd string) +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "he\n"; print "yes"']], nil, 3000, 3) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: true +stdout: yes +stderr: he + +reason: exit +status: 0 + + + +=== TEST 2: good case (shell cmd arg vector) +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run({'perl', '-e', [[warn "he\n"; print "yes"]]}, nil, 3000) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: true +stdout: yes +stderr: he + +reason: exit +status: 0 + + + +=== TEST 3: too much stdout data (1 byte more) +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "he\n"; print "yes!"']], nil, 3000, 3) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: nil +stdout: yes! +stderr: he + +reason: failed to read stdout: too much data +status: nil + + + +=== TEST 4: too much stdout data (several bytes more) +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "he\n"; print "yes!!!yes~"']], + nil, 3000, 3) + say("ok: ", ok) + say("stdout: ", stdout) + say("stderr: ", stderr) + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body +ok: nil +stdout: yes! +stderr: he + +reason: failed to read stdout: too much data +status: nil + + + +=== TEST 5: stdout timeout +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + + do + local ok, stdout, stderr, reason, status = + shell.run([[perl -e 'warn "he\n"; sleep 10; print "yes"']], + nil, 1, 3) + say("ok: ", ok) + say("stdout: '", stdout, "'") + say("stderr: '", stderr, "'") + say("reason: ", reason) + say("status: ", status) + end + collectgarbage() + } + } +--- response_body_like chomp +\Aok: nil +stdout: '' +stderr: '(?:|he +)' +reason: failed to wait for process: timeout +status: nil +\z + + + +=== TEST 6: clean up the sub-process when failed to wait +--- config + location = /t { + content_by_lua_block { + local say = ngx.say + local shell = require "resty.shell" + local ok, stdout, stderr, reason, status = + shell.run([[echo aaaaa && sleep 10]], nil, 100, 3) + say("ok: ", ok) + say("stdout: '", stdout, "'") + say("stderr: '", stderr, "'") + say("reason: ", reason) + say("status: ", status) + } + } +--- response_body +ok: nil +stdout: 'aaaa' +stderr: '' +reason: failed to wait for process: timeout +status: nil +--- error_log +lua pipe SIGCHLD fd read pid: +--- wait: 0.2 diff --git a/lua-resty-shell-0.03/valgrind.suppress b/lua-resty-shell-0.03/valgrind.suppress new file mode 100644 index 0000000..1cac506 --- /dev/null +++ b/lua-resty-shell-0.03/valgrind.suppress @@ -0,0 +1,30 @@ +{ + <insert_a_suppression_name_here> + Memcheck:Param + epoll_ctl(event) + fun:epoll_ctl + fun:ngx_epoll_test_rdhup + fun:ngx_epoll_init + fun:ngx_event_process_init + fun:ngx_single_process_cycle + fun:main +} +{ + <insert_a_suppression_name_here> + Memcheck:Leak + match-leak-kinds: definite + fun:malloc + fun:ngx_alloc + fun:ngx_set_environment + fun:ngx_single_process_cycle + fun:main +} +{ + <insert_a_suppression_name_here> + Memcheck:Leak + match-leak-kinds: definite + fun:malloc + fun:ngx_alloc + fun:ngx_set_environment + fun:ngx_worker_process_init +} |