]> git.e2factory.org Git - e2factory.git/commitdiff
e2lib: support waitpid WNOHANG with wait(pid, true)
authorTobias Ulmer <tu@emlix.com>
Fri, 15 Feb 2019 10:10:45 +0000 (11:10 +0100)
committerTobias Ulmer <tu@emlix.com>
Fri, 15 Feb 2019 10:15:07 +0000 (11:15 +0100)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua
generic/le2lib.c

index ac9cfcbf6faa9806e15777b9aa5e94ede638eb1c..862a348861278d850064850ab12599e5d5aaec5e 100644 (file)
@@ -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,
index 233317d44df39a2de0311cad6e66ab6d826aa68f..67a4ff60217882bd08df1b70c6d0d848b5865314 100644 (file)
@@ -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;
        }