diff options
author | Christian Pointner <equinox@spreadspace.org> | 2010-11-20 22:44:43 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2010-11-20 22:44:43 +0000 |
commit | 9cdadf047654c8b66af56204df89b9c6ee098b67 (patch) | |
tree | d6511e14d9bc48dd036d259e3085cbb64fb39c4e | |
parent | added first verion of (diff) |
util.exec now supports arguments
git-svn-id: https://svn.spreadspace.org/gcsd/trunk@45 ac14a137-c7f1-4531-abe0-07747231d213
-rw-r--r-- | src/l_util.c | 75 | ||||
-rw-r--r-- | src/main_loop.lua | 2 |
2 files changed, 74 insertions, 3 deletions
diff --git a/src/l_util.c b/src/l_util.c index 734253f..1a13721 100644 --- a/src/l_util.c +++ b/src/l_util.c @@ -142,11 +142,71 @@ static int l_util_select(lua_State *L) return 2; } +static char** table_to_argv(lua_State *L, const char* script, int index) +{ + size_t n = 0; + if(lua_istable(L, 2)) + n = lua_objlen(L, index); + + char** my_ptrptr; + my_ptrptr = malloc((n+2)*sizeof(char*)); + if(!my_ptrptr) + return NULL; + + my_ptrptr[n+1] = NULL; + my_ptrptr[0] = strdup(script); + if(!my_ptrptr[0]) { + free(my_ptrptr); + return NULL; + } + + int i; + for(i = 1; i <= n; ++i) { + lua_pushinteger(L, i); + lua_gettable(L, index); + my_ptrptr[i] = strdup(luaL_checkstring(L, -1)); + lua_pop(L, 1); + if(!my_ptrptr[i]) { + i--; + for(; i >= 0; --i) + free(my_ptrptr[i]); + + free(my_ptrptr); + return NULL; + } + } + + return my_ptrptr; +} + +static char** table_to_evp(lua_State *L, int index) +{ + /* lua_pushnil(L); */ + /* int i = 0; */ + /* while(lua_next(L, index) != 0) { */ + + + /* lua_pop(L, 1); */ + /* i++; */ + /* } */ + return NULL; +} + +void free_ptrptr(char** ptrptr) +{ + if(!ptrptr) + return; + + int i; + for(i = 0; ptrptr[i]; ++i) + free(ptrptr[i]); + + free(ptrptr); +} + static int l_util_exec(lua_State *L) { const char* script = luaL_checkstring(L, 1); - char** argv = NULL; - char** evp = NULL; pid_t pid; pid = fork(); @@ -161,7 +221,18 @@ static int l_util_exec(lua_State *L) for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors close(fd); + 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; + } + execve(script, argv, evp); + free_ptrptr(argv); + free_ptrptr(evp); + lua_close(L); exit(-1); } diff --git a/src/main_loop.lua b/src/main_loop.lua index c186722..1ebb75d 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -74,7 +74,7 @@ function main_loop(opt) log.printf(log.NOTICE, "main_loop started") local sig = signal.init() - local pid, err = util.exec("./tmp.sh") + local pid, err = util.exec("./tmp.sh", { "hello world", "gcsd!" }) if(pid == nil) then log.printf(log.DEBUG, "exec script failed: %s", err) else |