diff options
| author | ruki <[email protected]> | 2019-08-17 11:04:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-08-17 11:04:25 +0800 |
| commit | 45a548d4329975c113ac0f225f98afcf5f2b0724 (patch) | |
| tree | b38458c965b21ce78eee5bc04f9019cd375a350c | |
| parent | 1de49ce846981a065ce3bea73591b797718ae993 (diff) | |
improve process
| -rw-r--r-- | core/src/xmake/machine.c | 23 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 5 | ||||
| -rw-r--r-- | core/src/xmake/process/close.c (renamed from core/src/xmake/process/subprocess___tostring.c) | 35 | ||||
| -rw-r--r-- | core/src/xmake/process/open.c | 22 | ||||
| -rw-r--r-- | core/src/xmake/process/openv.c | 25 | ||||
| -rw-r--r-- | core/src/xmake/process/prefix.h | 80 | ||||
| -rw-r--r-- | core/src/xmake/process/subprocess_close___gc.c | 88 | ||||
| -rw-r--r-- | core/src/xmake/process/wait.c (renamed from core/src/xmake/process/subprocess_wait.c) | 27 | ||||
| -rw-r--r-- | core/src/xmake/process/waitlist.c | 17 | ||||
| -rw-r--r-- | tests/modules/process/test.lua | 12 | ||||
| -rw-r--r-- | xmake/actions/update/main.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 7 | ||||
| -rw-r--r-- | xmake/core/base/process.lua | 118 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/process.lua | 77 |
14 files changed, 226 insertions, 312 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 84c286b09..0a37ff643 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -147,12 +147,8 @@ tb_int_t xm_string_startswith(lua_State* lua); tb_int_t xm_process_open(lua_State* lua); tb_int_t xm_process_openv(lua_State* lua); tb_int_t xm_process_waitlist(lua_State* lua); - -// the process/subprocess functions -tb_int_t xm_process_subprocess_wait(lua_State* lua); -tb_int_t xm_process_subprocess_close(lua_State* lua); -tb_int_t xm_process_subprocess___gc(lua_State* lua); -tb_int_t xm_process_subprocess___tostring(lua_State* lua); +tb_int_t xm_process_wait(lua_State* lua); +tb_int_t xm_process_close(lua_State* lua); // the sandbox functions tb_int_t xm_sandbox_interactive(lua_State* lua); @@ -305,19 +301,11 @@ static luaL_Reg const g_process_functions[] = { "open", xm_process_open } , { "openv", xm_process_openv } , { "waitlist", xm_process_waitlist } +, { "wait", xm_process_wait } +, { "close", xm_process_close } , { tb_null, tb_null } }; -// the process/subprocess functions -static luaL_Reg const g_process_subprocess_functions[] = -{ - { "close", xm_process_subprocess_close } -, { "wait", xm_process_subprocess_wait } -, { "__gc", xm_process_subprocess___gc } -, { "__tostring", xm_process_subprocess___tostring } -, { tb_null, tb_null } -}; - // the sandbox functions static luaL_Reg const g_sandbox_functions[] = { @@ -661,9 +649,6 @@ xm_machine_ref_t xm_machine_init() // bind process functions luaL_register(machine->lua, "process", g_process_functions); - // bind process._subprocess (metatable) functions - xm_machine_register_metatable(machine, "process", "_subprocess", "process._subprocess*", g_process_subprocess_functions); - // bind sandbox functions luaL_register(machine->lua, "sandbox", g_sandbox_functions); diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index af8d469d0..7f8e5e4c5 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -75,10 +75,9 @@ xmake_C_FILES += \ string/startswith \ process/open \ process/openv \ + process/wait \ process/waitlist \ - process/subprocess_wait \ - process/subprocess___tostring \ - process/subprocess_close___gc \ + process/close \ sandbox/interactive \ semver/parse \ semver/compare \ diff --git a/core/src/xmake/process/subprocess___tostring.c b/core/src/xmake/process/close.c index ab93455b0..dc1a465a3 100644 --- a/core/src/xmake/process/subprocess___tostring.c +++ b/core/src/xmake/process/close.c @@ -11,19 +11,19 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * Copyright (C) 2015 - 2019, TBOOX Open Source Group. * * @author ruki - * @file subprocess___tostring.c + * @file close.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "subprocess___tostring" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "process.close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,17 +34,26 @@ * implementation */ -/* - * tostring(subprocess) - */ -tb_int_t xm_process_subprocess___tostring(lua_State* lua) +// process.close(p) +tb_int_t xm_process_close(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // get subprocess name as string - xm_subprocess_t* subprocess = xm_subprocess_get(lua); - lua_pushstring(lua, subprocess->name); - xm_subprocess_return_success(); -} + // is user data? + if (!lua_isuserdata(lua, 1)) + return 0; + + // get the process + tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(process, 0); + // exit process + tb_process_exit(process); + + // save result: ok + lua_pushboolean(lua, tb_true); + + // ok + return 1; +} diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c index bbb78ad27..fad3e8fec 100644 --- a/core/src/xmake/process/open.c +++ b/core/src/xmake/process/open.c @@ -133,27 +133,7 @@ tb_int_t xm_process_open(lua_State* lua) // init process tb_process_ref_t process = (tb_process_ref_t)tb_process_init_cmd(command, &attr); - if (process) - { - // init subprocess - xm_subprocess_t* subprocess = xm_subprocess_new(lua); - subprocess->process = process; - subprocess->is_opened = tb_true; - - // attach subprocess - tb_process_priv_set(process, subprocess); - - // save subprocess name - tb_size_t name_maxn = tb_arrayn(subprocess->name); - tb_strlcpy(subprocess->name, "subprocess: ", name_maxn); - if (command_size < name_maxn - tb_arrayn("subprocess: ")) - tb_strcat(subprocess->name, command); - else - { - tb_strcat(subprocess->name, "..."); - tb_strcat(subprocess->name, command + (command_size - name_maxn + tb_arrayn("subprocess: ") + tb_arrayn("..."))); - } - } + if (process) lua_pushlightuserdata(lua, (tb_pointer_t)process); else lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c index 0ff26988c..459a56832 100644 --- a/core/src/xmake/process/openv.c +++ b/core/src/xmake/process/openv.c @@ -50,8 +50,7 @@ tb_int_t xm_process_openv(lua_State* lua) } // get shellname - size_t shellname_size = 0; - tb_char_t const* shellname = luaL_checklstring(lua, 1, &shellname_size); + tb_char_t const* shellname = lua_tostring(lua, 1); tb_check_return_val(shellname, 0); // get the arguments count @@ -176,27 +175,7 @@ tb_int_t xm_process_openv(lua_State* lua) // init process tb_process_ref_t process = (tb_process_ref_t)tb_process_init(shellname, argv, &attr); - if (process) - { - // init subprocess - xm_subprocess_t* subprocess = xm_subprocess_new(lua); - subprocess->process = process; - subprocess->is_opened = tb_true; - - // attach subprocess - tb_process_priv_set(process, subprocess); - - // save subprocess name - tb_size_t name_maxn = tb_arrayn(subprocess->name); - tb_strlcpy(subprocess->name, "subprocess: ", name_maxn); - if (shellname_size < name_maxn - tb_arrayn("subprocess: ")) - tb_strcat(subprocess->name, shellname); - else - { - tb_strcat(subprocess->name, "..."); - tb_strcat(subprocess->name, shellname + (shellname_size - name_maxn + tb_arrayn("subprocess: ") + tb_arrayn("..."))); - } - } + if (process) lua_pushlightuserdata(lua, (tb_pointer_t)process); else lua_pushnil(lua); // exit argv diff --git a/core/src/xmake/process/prefix.h b/core/src/xmake/process/prefix.h index 2b0fec706..becbb93e6 100644 --- a/core/src/xmake/process/prefix.h +++ b/core/src/xmake/process/prefix.h @@ -1,7 +1,7 @@ /*!A cross-platform build utility based on Lua * * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this subprocess except in compliance with the License. + * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 @@ -15,7 +15,7 @@ * Copyright (C) 2015 - 2019, TBOOX Open Source Group. * * @author ruki - * @subprocess prefix.h + * @file prefix.h * */ #ifndef XM_PROCESS_PREFIX_H @@ -26,82 +26,6 @@ */ #include "../prefix.h" -/* ////////////////////////////////////////////////////////////////////////////////////// - * macross - */ - -// the subprocess udata type -#define xm_subprocess_udata "process._subprocess*" - -// return lock success -#define xm_subprocess_return_success() do { return 1; } while (0) - -// return lock error with reason -#define xm_subprocess_return_error(lua, subprocess, reason) \ - do \ - { \ - lua_pushnil(lua); \ - lua_pushfstring(lua, "error: %s (%s)", reason, subprocess->name); \ - return 2; \ - } while (0) - -// return closed error -#define xm_subprocess_return_error_closed(lua) \ - do \ - { \ - lua_pushnil(lua); \ - lua_pushliteral(lua, "error: subprocess has been closed"); \ - return 2; \ - } while (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * types - */ - -// the subprocess type -typedef struct __xm_subprocess_t -{ - // the process reference - tb_process_ref_t process; - - // is opened? - tb_bool_t is_opened; - - // the process name - tb_char_t name[32]; - -} xm_subprocess_t; - -/* ////////////////////////////////////////////////////////////////////////////////////// - * interfaces - */ -static __tb_inline__ xm_subprocess_t* xm_subprocess_new(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, tb_null); - - // new subprocess - xm_subprocess_t* subprocess = (xm_subprocess_t*)lua_newuserdata(lua, sizeof(xm_subprocess_t)); - tb_assert_and_check_return_val(subprocess, tb_null); - - // init subprocess - luaL_getmetatable(lua, xm_subprocess_udata); - lua_setmetatable(lua, -2); - tb_memset(subprocess, 0, sizeof(xm_subprocess_t)); - return subprocess; -} - -static __tb_inline__ xm_subprocess_t* xm_subprocess_get(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, tb_null); - - // get subprocess - xm_subprocess_t* subprocess = (xm_subprocess_t*)luaL_checkudata(lua, 1, xm_subprocess_udata); - tb_assert(subprocess); - return subprocess; -} - #endif diff --git a/core/src/xmake/process/subprocess_close___gc.c b/core/src/xmake/process/subprocess_close___gc.c deleted file mode 100644 index 30c3f3656..000000000 --- a/core/src/xmake/process/subprocess_close___gc.c +++ /dev/null @@ -1,88 +0,0 @@ -/*!A cross-platform build utility based on Lua - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2015 - 2019, TBOOX Open Source Group. - * - * @author ruki - * @file subprocess_close___gc.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "subprocess_close___gc" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -static tb_int_t xm_subprocess_close_impl(lua_State* lua, tb_bool_t allow_closed_subprocess) -{ - // check - tb_assert_and_check_return_val(lua, 0); - - // close subprocess - xm_subprocess_t* subprocess = xm_subprocess_get(lua); - if (!subprocess->is_opened) - { - if (allow_closed_subprocess) - { - lua_pushboolean(lua, tb_true); - xm_subprocess_return_success(); - } - else xm_subprocess_return_error_closed(lua); - } - - // check - tb_assert(subprocess->process); - - // close process - tb_process_exit(subprocess->process); - subprocess->process = tb_null; - subprocess->is_opened = tb_false; - - // mark this subprocess as closed - tb_strlcpy(subprocess->name, "subprocess: (closed subprocess)", tb_arrayn(subprocess->name)); - - // close ok - lua_pushboolean(lua, tb_true); - xm_subprocess_return_success(); -} - -/* ////////////////////////////////////////////////////////////////////////////////////// - * interfaces - */ - -/* - * subprocess:close() - */ -tb_int_t xm_process_subprocess_close(lua_State* lua) -{ - return xm_subprocess_close_impl(lua, tb_false); -} - -/* - * subprocess:close() - */ -tb_int_t xm_process_subprocess___gc(lua_State* lua) -{ - return xm_subprocess_close_impl(lua, tb_true); -} diff --git a/core/src/xmake/process/subprocess_wait.c b/core/src/xmake/process/wait.c index c7cb82377..b20b81d74 100644 --- a/core/src/xmake/process/subprocess_wait.c +++ b/core/src/xmake/process/wait.c @@ -15,14 +15,14 @@ * Copyright (C) 2015 - 2019, TBOOX Open Source Group. * * @author ruki - * @file subprocess_wait.c + * @file wait.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "subprocess_wait" +#define TB_TRACE_MODULE_NAME "process.wait" #define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// @@ -34,24 +34,31 @@ * implementation */ -// ok, status = subprocess:wait(timeout) -tb_int_t xm_process_subprocess_wait(lua_State* lua) +// ok, status = process.wait(proc, timeout) +tb_int_t xm_process_wait(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); - // get subprocess - xm_subprocess_t* subprocess = xm_subprocess_get(lua); - if (!subprocess->is_opened) - xm_subprocess_return_error_closed(lua); - tb_assert(subprocess->process); + // is user data? + if (!lua_isuserdata(lua, 1)) + { + // error + lua_pushfstring(lua, "invalid argument type(%s) for process.wait", luaL_typename(lua, 1)); + lua_error(lua); + return 0; + } + + // get the process + tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_check_return_val(process, 0); // get the timeout tb_long_t timeout = (tb_long_t)luaL_checkinteger(lua, 2); // wait it tb_long_t status = 0; - tb_long_t ok = tb_process_wait(subprocess->process, &status, timeout); + tb_long_t ok = tb_process_wait(process, &status, timeout); // save result lua_pushinteger(lua, ok); diff --git a/core/src/xmake/process/waitlist.c b/core/src/xmake/process/waitlist.c index 98015c2d4..2254f199f 100644 --- a/core/src/xmake/process/waitlist.c +++ b/core/src/xmake/process/waitlist.c @@ -81,15 +81,15 @@ tb_int_t xm_process_waitlist(lua_State* lua) lua_pushinteger(lua, i + 1); lua_gettable(lua, 1); - // save this process - xm_subprocess_t* subprocess = (xm_subprocess_t*)luaL_checkudata(lua, -1, xm_subprocess_udata); - if (subprocess) + // is userdata? + if (lua_isuserdata(lua, -1)) { - processes[i] = subprocess->process; - if (!subprocess->is_opened || !processes[i]) + // save this process + processes[i] = (tb_process_ref_t)lua_touserdata(lua, -1); + if (!processes[i]) { // error - lua_pushfstring(lua, "process[%d] is null or closed for process.waitlist", i); + lua_pushfstring(lua, "process[%d] is null for process.waitlist", i); lua_error(lua); } } @@ -124,10 +124,7 @@ tb_int_t xm_process_waitlist(lua_State* lua) { // save one process info lua_newtable(lua); - tb_process_ref_t process = infolist[i].process; - if (process) - lua_pushlightuserdata(lua, tb_process_priv(process)); - else lua_pushnil(lua); + lua_pushlightuserdata(lua, (tb_pointer_t)infolist[i].process); lua_rawseti(lua, -2, 1); lua_pushinteger(lua, infolist[i].index + 1); lua_rawseti(lua, -2, 2); diff --git a/tests/modules/process/test.lua b/tests/modules/process/test.lua index c5ee27d9f..24846fd02 100644 --- a/tests/modules/process/test.lua +++ b/tests/modules/process/test.lua @@ -7,20 +7,14 @@ function test_single_process(t) local stdout = os.tmpfile() local stderr = os.tmpfile() for i = 1, 2 do - local pro = process.open("echo -n awd", {outpath = stdout, errpath = stderr}) - process.wait(pro, inftimeout) - process.close(pro) + local proc = process.open("echo -n awd", {outpath = stdout, errpath = stderr}) + proc:wait(inftimeout) + proc:close() t:are_equal(io.readfile(stdout), "awd") end end function test_hack(t) - -- hack test - t:will_raise(function () - process.wait("awd", inftimeout) - end) - - t:require_not(process.close("awd")) t:will_raise(function () process.waitlist("awd", inftimeout) diff --git a/xmake/actions/update/main.lua b/xmake/actions/update/main.lua index b30ba0799..8a476b180 100644 --- a/xmake/actions/update/main.lua +++ b/xmake/actions/update/main.lua @@ -98,7 +98,7 @@ function _run_win_v(program, commands, admin) os.cp(sudo_vbs, temp_vbs) local params = table.join("/Nologo", temp_vbs, "W" .. (admin and "A" or "N") , program, commands) local proc = process.openv("cscript", params) - if proc then process.close(proc) end + if proc then proc:close() end return proc ~= nil end diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 18dc682da..81d7eb3ec 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -28,6 +28,7 @@ local path = require("base/path") local table = require("base/table") local utils = require("base/utils") local string = require("base/string") +local process = require("base/process") -- save original interfaces os._uid = os._uid or os.uid @@ -640,7 +641,7 @@ function os.execv(program, argv, opt) -- wait it repeat -- poll it - waitok, status = process.wait(proc, 0) + waitok, status = proc:wait(0) if waitok == 0 then waitok, status = coroutine.yield(proc) end @@ -649,7 +650,7 @@ function os.execv(program, argv, opt) -- resume the current directory os.cd(curdir) else - waitok, status = process.wait(proc, -1) + waitok, status = proc:wait(-1) end -- get status @@ -658,7 +659,7 @@ function os.execv(program, argv, opt) end -- close process - process.close(proc) + proc:close() end -- ok? diff --git a/xmake/core/base/process.lua b/xmake/core/base/process.lua index 70d007c30..3102da184 100644 --- a/xmake/core/base/process.lua +++ b/xmake/core/base/process.lua @@ -19,13 +19,129 @@ -- -- define module: process -local process = process or {} +local process = process or {} +local _subprocess = _subprocess or {} -- load modules +local path = require("base/path") local utils = require("base/utils") local string = require("base/string") local coroutine = require("base/coroutine") +-- save original interfaces +process._open = process._open or process.open +process._openv = process._openv or process.openv +process._wait = process._wait or process.wait +process._waitlist = process._waitlist or process.waitlist +process._close = process._close or process.close +process.wait = nil +process.close = nil +process._subprocess = _subprocess + +-- new an subprocess +function _subprocess.new(name, proc) + local subprocess = table.inherit(_subprocess) + subprocess._NAME = name + subprocess._PROC = proc + setmetatable(subprocess, _subprocess) + return subprocess +end + +-- get the process name +function _subprocess:name() + return self._NAME +end + +-- wait subprocess +-- +-- @param timeout the timeout +-- +-- @return ok, status +-- +function _subprocess:wait(timeout) + if not self._PROC then + return -1, 0, string.format("subprocess(%s) has been closed!", self:name()) + end + return process._wait(self._PROC, timeout or -1) +end + +-- close subprocess +function _subprocess:close(timeout) + if not self._PROC then + return false, string.format("subprocess(%s) has been closed!", self:name()) + end + local ok = process._close(self._PROC) + if ok then + self._PROC = nil + end + return ok +end + +-- tostring(subprocess) +function _subprocess:__tostring() + return "subprocess: " .. self:name() +end + +-- gc(subprocess) +function _subprocess:__gc() + local ok = self._PROC and process._close(self._PROC) or false + if ok then + self._PROC = nil + end +end + +-- open a subprocess +-- +-- @param command the process command +-- @param opt the option arguments, e.g. {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}}) +-- +-- @return the subprocess +-- +function process.open(command, opt) + local proc = process._open(command, opt) + if proc then + return _subprocess.new(path.filename(command:split(' ', {plain = true})[1]), proc) + else + return nil, string.format("open process(%s) failed!", command) + end +end + +-- open a subprocess with the arguments list +-- +-- @param shellname the shell name +-- @param argv the arguments list +-- @param opt the option arguments, e.g. {outpath = "", errpath = "", envs = {"PATH=xxx", "XXX=yyy"}}) +-- +-- @return the subprocess +-- +function process.openv(shellname, argv, opt) + local proc = process._open(shellname, argv, opt) + if proc then + return _subprocess.new(path.filename(shellname), proc) + else + return nil, string.format("openv process(%s, %s) failed!", shellname, table.concat(argv, " ")) + end +end + +-- wait subprocess list +-- +-- count, list = process.waitlist(proclist, timeout) +-- +-- count: +-- +-- the finished count: > 0 +-- timeout: 0 +-- failed: -1 +-- +-- for _, procinfo in ipairs(list) do +-- print("proc: ", procinfo[1]) +-- print("index: ", procinfo[2]) +-- print("status: ", procinfo[3]) +-- end +-- +function process.waitlist(proclist, timeout) +end + -- async run task and echo waiting info function process.asyncrun(task, waitchars) diff --git a/xmake/core/sandbox/modules/process.lua b/xmake/core/sandbox/modules/process.lua index 3a4faa31d..24872e8c1 100644 --- a/xmake/core/sandbox/modules/process.lua +++ b/xmake/core/sandbox/modules/process.lua @@ -25,7 +25,26 @@ local raise = require("sandbox/modules/raise") local vformat = require("sandbox/modules/vformat") -- define module -local sandbox_process = sandbox_process or {} +local sandbox_process = sandbox_process or {} +local sandbox_process_subprocess = sandbox_process_subprocess or {} +sandbox_process._subprocess = sandbox_process._subprocess or process._subprocess + +-- wait subprocess +function sandbox_process_subprocess.wait(proc, timeout) + local ok, status, errors = proc:_wait(timeout) + if errors then + raise(errors) + end + return ok, status +end + +-- close subprocess +function sandbox_process_subprocess.close(proc) + local ok, errors = proc:_close() + if not ok then + raise(errors) + end +end -- open process --- @@ -41,12 +60,21 @@ function sandbox_process.open(command, opt) command = vformat(command) -- open process - local proc = process.open(command, opt) + local proc, errors = process.open(command, opt) if not proc then - raise("open process(%s) failed!", command) + raise(errors) end - -- ok + -- hook subprocess interfaces + for name, func in pairs(proc) do + if not name:startswith("_") and type(func) == "function" then + local newfunc = sandbox_process_subprocess[name] + if newfunc ~= nil then + proc["_" .. name] = proc["_" .. name] or func + proc[name] = newfunc + end + end + end return proc end @@ -65,39 +93,22 @@ function sandbox_process.openv(filename, argv, opt) filename = vformat(filename) -- open process - local proc = process.openv(filename, argv, opt) + local proc, errors = process.openv(filename, argv, opt) if not proc then - raise("openv process(%s, %s) failed!", filename, table.concat(argv, " ")) + raise(errors) end - -- ok - return proc -end - --- close process -function sandbox_process.close(proc) - - -- check - assert(proc) - - -- close it - process.close(proc) -end - --- wait process -function sandbox_process.wait(proc, timeout) - - -- check - assert(proc) - - -- wait it - local ok, status = process.wait(proc, timeout) - if ok < 0 then - raise("wait process failed(%d)", ok) + -- hook subprocess interfaces + for name, func in pairs(proc) do + if not name:startswith("_") and type(func) == "function" then + local newfunc = sandbox_process_subprocess[name] + if newfunc ~= nil then + proc["_" .. name] = proc["_" .. name] or func + proc[name] = newfunc + end + end end - - -- timeout or finished - return ok, status + return proc end -- wait processes |
