summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2010-11-22 17:37:13 +0000
committerChristian Pointner <equinox@spreadspace.org>2010-11-22 17:37:13 +0000
commitce7feee003f12ab80ac43d05fb5ccd36a852efdc (patch)
tree4cdbd9efb7028ad964630b47ec4282bd749a933a
parentadded child clear (not called yet) (diff)
exec works now (with error pipe back)
git-svn-id: https://svn.spreadspace.org/gcsd/trunk@49 ac14a137-c7f1-4531-abe0-07747231d213
-rw-r--r--src/l_util.c37
-rw-r--r--src/main_loop.lua2
2 files changed, 19 insertions, 20 deletions
diff --git a/src/l_util.c b/src/l_util.c
index 3697c09..07c3332 100644
--- a/src/l_util.c
+++ b/src/l_util.c
@@ -40,6 +40,7 @@
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <fcntl.h>
#include "l_util.h"
@@ -337,16 +338,16 @@ static int l_util_exec(lua_State *L)
{
const char* script = luaL_checkstring(L, 1);
- pid_t pid;
- pid = fork();
- if(pid == -1) {
+ int pipefd[2];
+ if(pipe(pipefd) == -1) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno)); // FIXXXXXME: strerror is not threadsafe!!!
return 2;
}
- int pipefd[2];
- if(pipe(pipefd) == -1) {
+ pid_t pid;
+ pid = fork();
+ if(pid == -1) {
lua_pushnil(L);
lua_pushstring(L, strerror(errno)); // FIXXXXXME: strerror is not threadsafe!!!
return 2;
@@ -356,13 +357,16 @@ static int l_util_exec(lua_State *L)
int fd;
for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
if(fd != pipefd[1]) close(fd);
+ fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
char** argv = table_to_argv(L, script, 2);
char** evp = table_to_evp(L, 3);
if(!argv) {
- lua_pushnil(L);
- lua_pushstring(L, "bad alloc");
- return 2;
+ int err = ENOMEM;
+ int ret = write(pipefd[1], (void*)(&err), sizeof(err));
+ free_ptrptr(evp);
+ lua_close(L);
+ exit(ret);
}
execve(script, argv, evp);
@@ -399,17 +403,12 @@ static int l_util_waitpid(lua_State *L)
lua_pushinteger(L, pid);
child_t* child = child_find(L, pid);
if(child) {
- fd_set rfds;
- FD_ZERO(&rfds);
- FD_SET(child->fd_, &rfds);
- struct timeval tv = { 0 , 0 };
- if(select(child->fd_+1, &rfds, NULL, NULL, &tv) == 1) {
- int err = 0;
- if(read(child->fd_, (void*)(&err), sizeof(err)) >= sizeof(err)) {
- lua_pushliteral(L, "error");
- lua_pushstring(L, strerror(errno)); // FIXXXXXME: strerror is not threadsafe!!!
- return 3;
- }
+ int err = 0;
+ if(read(child->fd_, (void*)(&err), sizeof(err)) >= sizeof(err)) {
+ lua_pushliteral(L, "error");
+ lua_pushstring(L, strerror(err)); // FIXXXXXME: strerror is not threadsafe!!!
+ child_remove(L, pid);
+ return 3;
}
child_remove(L, pid);
}
diff --git a/src/main_loop.lua b/src/main_loop.lua
index 14f83a2..a428e44 100644
--- a/src/main_loop.lua
+++ b/src/main_loop.lua
@@ -96,7 +96,7 @@ function main_loop(opt)
if(pid == nil) then
log.printf(log.DEBUG, "waitpid failed: " .. reason)
elseif(pid > 0) then
- log.printf(log.DEBUG, "child with pid %d stopped, reason: %s, code: %d", pid, reason, status)
+ log.printf(log.DEBUG, "child with pid %d stopped, reason: %s, code: %s", pid, reason, tostring(status))
end
elseif(err == "signal") then
-- ignore this