From 376b5e18a554c8d4b607a21c0973ffa3b786a056 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Fri, 15 Feb 2019 11:10:45 +0100 Subject: [PATCH] e2lib: support waitpid WNOHANG with wait(pid, true) Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 5 +++-- generic/le2lib.c | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index ac9cfcb..862a348 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -224,12 +224,13 @@ end --- Wait for process to terminate. -- @param pid Process ID, -1 to wait for any child. +-- @param wnohang True to set WNOHANG flag, pid == 0 indicates running process. -- @return Exit status of process, signal + 128, or false on error. -- @return Process ID of the terminated child or error object on failure. -- @return Number of signal that killed the process, if any. -function e2lib.wait(pid) +function e2lib.wait(pid, wnohang) while true do - local rc, childpid, sig = le2lib.wait(pid) + local rc, childpid, sig = le2lib.wait(pid, wnohang) if not rc then if sig ~= errno.def2errnum("EINTR") then return false, diff --git a/generic/le2lib.c b/generic/le2lib.c index 233317d..67a4ff6 100644 --- a/generic/le2lib.c +++ b/generic/le2lib.c @@ -284,13 +284,23 @@ process_wait(lua_State *lua) { pid_t pid = luaL_checkinteger(lua, 1); int rc, status; - rc = waitpid(pid, &status, 0); + int wnohang = lua_gettop(lua) > 1 && lua_toboolean(lua, 2); + int options = 0; + + if (wnohang) + options = WNOHANG; + + rc = waitpid(pid, &status, options); if (rc < 0) { int e = errno; lua_pushboolean(lua, 0); lua_pushstring(lua, strerror(e)); lua_pushinteger(lua, e); return 3; + } else if (rc == 0) { + lua_pushnumber(lua, 255); /* not relevant */ + lua_pushnumber(lua, rc); /* pid == 0 -> process still running */ + return 2; } if (WIFEXITED(status)) { @@ -494,7 +504,8 @@ do_execvp(lua_State *L) argv = malloc((argc+1) * sizeof(char *)); if (argv == NULL) { lua_pushboolean(L, 0); - lua_pushstring(L, "do_execvp: 1+ argv arguments required"); + lua_pushfstring(L, "do_execvp: malloc failed: %s", + strerror(errno)); return 2; } -- 2.39.5