--- 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,
{
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)) {
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;
}