diff options
author | Christian Pointner <equinox@spreadspace.org> | 2010-11-22 18:32:44 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2010-11-22 18:32:44 +0000 |
commit | 79072a3a38c6bba8a704835a9a46772e809469bd (patch) | |
tree | 4ea1fbe99a5ddb00f674d5b94d2f0e20d1be4db1 | |
parent | exec works now (with error pipe back) (diff) |
exec works now with prepared stdio setup
git-svn-id: https://svn.spreadspace.org/gcsd/trunk@50 ac14a137-c7f1-4531-abe0-07747231d213
-rw-r--r-- | src/l_util.c | 30 | ||||
-rw-r--r-- | src/main_loop.lua | 7 |
2 files changed, 26 insertions, 11 deletions
diff --git a/src/l_util.c b/src/l_util.c index 07c3332..0b943a5 100644 --- a/src/l_util.c +++ b/src/l_util.c @@ -334,9 +334,34 @@ void child_clear(lua_State *L, pid_t pid) } } +static void prepare_fds(lua_State *L, int *child_stdio, int pipefd) +{ + int fd; + for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors + if(fd != pipefd && fd != child_stdio[0] && fd != child_stdio[1] && fd != child_stdio[2]) close(fd); + fcntl(pipefd, F_SETFD, FD_CLOEXEC); + + int i, ret; + for(i = 0; i<3; ++i) { + if(child_stdio[i] >= 0) { + fd = dup2(child_stdio[i], i); + if(fd == -1) { + ret = write(pipefd, (void*)(&errno), sizeof(errno)); + lua_close(L); + exit(ret); + } + close(child_stdio[i]); + } + } +} + static int l_util_exec(lua_State *L) { const char* script = luaL_checkstring(L, 1); + int child_stdio[3]; + child_stdio[0] = luaL_optint(L, 4, -1); + child_stdio[1] = luaL_optint(L, 5, -1); + child_stdio[2] = luaL_optint(L, 6, -1); int pipefd[2]; if(pipe(pipefd) == -1) { @@ -354,10 +379,7 @@ static int l_util_exec(lua_State *L) } if(!pid) { - 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); + prepare_fds(L, child_stdio, pipefd[1]); char** argv = table_to_argv(L, script, 2); char** evp = table_to_evp(L, 3); diff --git a/src/main_loop.lua b/src/main_loop.lua index a428e44..9b9d535 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -74,13 +74,6 @@ function main_loop(opt) log.printf(log.NOTICE, "main_loop started") local sig = signal.init() - local pid, err = util.exec("./tmp.sh", { "hello world", "gcsd!" }, { "gcsd=super", "dscg=repus" }) - if(pid == nil) then - log.printf(log.DEBUG, "exec script failed: %s", err) - else - log.printf(log.DEBUG, "script started with pid %d", pid) - end - local return_value = module_list:init(opt) if(return_value == defines.KILL_DAEMON) then return_value = -1 |