From 8d00b8c7e7e71968c96f17aeb22892f7cab50c4e Mon Sep 17 00:00:00 2001 From: mark Date: Tue, 7 Feb 2006 08:56:04 +0000 Subject: [PATCH] fixed bug in ex_pipe() make_pipe() should produce non-inheritable descriptors/handles added process_tostring() method --- posix/ex.c | 22 ++++++++++++++++------ posix/spawn.c | 11 +++++++++++ posix/spawn.h | 1 + w32api/ex.c | 26 +++++++++----------------- w32api/spawn.c | 16 +++++++++++++++- w32api/spawn.h | 2 ++ 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/posix/ex.c b/posix/ex.c index 369288c..f9dc58d 100755 --- a/posix/ex.c +++ b/posix/ex.c @@ -284,12 +284,21 @@ static int ex_unlock(lua_State *L) return ex_lock(L); } +static int closeonexec(int d) +{ + int fl = fcntl(d, F_GETFD); + if (fl != -1) + fl = fcntl(d, F_SETFD, fl | FD_CLOEXEC); + return fl; +} static int make_pipe(FILE **i, FILE **o) { int fd[2]; if (-1 == pipe(fd)) return 0; + closeonexec(fd[0]); + closeonexec(fd[1]); *i = fdopen(fd[0], "r"); *o = fdopen(fd[1], "w"); return 1; @@ -306,7 +315,7 @@ static int ex_pipe(lua_State *L) lua_pushvalue(L, -2); lua_setmetatable(L, -2); *(pf = lua_newuserdata(L, sizeof *pf)) = o; - lua_pushvalue(L, -2); + lua_pushvalue(L, -3); lua_setmetatable(L, -2); return 2; } @@ -385,12 +394,12 @@ static int ex_spawn(lua_State *L) static const luaL_reg ex_iolib[] = { - {"pipe", ex_pipe}, + {"pipe", ex_pipe}, {0,0} }; static const luaL_reg ex_iofile_methods[] = { - {"lock", ex_lock}, - {"unlock", ex_unlock}, + {"lock", ex_lock}, + {"unlock", ex_unlock}, {0,0} }; static const luaL_reg ex_oslib[] = { @@ -412,11 +421,12 @@ static const luaL_reg ex_oslib[] = { {0,0} }; static const luaL_reg ex_diriter_methods[] = { - {"__gc", diriter_close}, + {"__gc", diriter_close}, {0,0} }; static const luaL_reg ex_process_methods[] = { - {"wait", process_wait}, + {"wait", process_wait}, + {"__tostring", process_tostring}, {0,0} }; diff --git a/posix/spawn.c b/posix/spawn.c index 63eda45..e06bcaa 100755 --- a/posix/spawn.c +++ b/posix/spawn.c @@ -133,3 +133,14 @@ int process_wait(lua_State *L) lua_pushnumber(L, p->status); return 1; } + +/* proc -- string */ +int process_tostring(lua_State *L) +{ + struct process *p = checkuserdata(L, 1, PROCESS_HANDLE); + char buf[40]; + lua_pushlstring(L, buf, + sprintf(buf, "process (%lu, %s)", (unsigned long)p->pid, + p->status==-1 ? "running" : "terminated")); + return 1; +} diff --git a/posix/spawn.h b/posix/spawn.h index 39167de..64fcee1 100755 --- a/posix/spawn.h +++ b/posix/spawn.h @@ -28,5 +28,6 @@ void spawn_param_env(struct spawn_params *p); void spawn_param_redirects(struct spawn_params *p); int spawn_param_execute(struct spawn_params *p, struct process *proc); int process_wait(lua_State *L); +int process_tostring(lua_State *L); #endif/*SPAWN_H*/ diff --git a/w32api/ex.c b/w32api/ex.c index 5b22f3d..7dd74f2 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -151,17 +151,6 @@ static FILE *check_file(lua_State *L, int idx) return *pf; } -static BOOL GetFileInformationByPath(LPCSTR name, BY_HANDLE_FILE_INFORMATION *pinfo) -{ - HANDLE h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); - BOOL ret = h != INVALID_HANDLE_VALUE; - if (ret) { - ret = GetFileInformationByHandle(h, pinfo); - CloseHandle(h); - } - return ret; -} - static uint64_t get_size(const char *name) { HANDLE h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); @@ -360,6 +349,8 @@ static int make_pipe(FILE **i, FILE **o) HANDLE ph[2]; if (0 == CreatePipe(ph+0, ph+1, 0, 0)) return 0; + SetHandleInformation(ph[0], HANDLE_FLAG_INHERIT, 0); + SetHandleInformation(ph[1], HANDLE_FLAG_INHERIT, 0); *i = _fdopen(_open_osfhandle((long)ph[0], _O_RDONLY), "r"); *o = _fdopen(_open_osfhandle((long)ph[1], _O_WRONLY), "w"); return 1; @@ -376,7 +367,7 @@ static int ex_pipe(lua_State *L) lua_pushvalue(L, -2); lua_setmetatable(L, -2); *(pf = lua_newuserdata(L, sizeof *pf)) = o; - lua_pushvalue(L, -2); + lua_pushvalue(L, -3); lua_setmetatable(L, -2); return 2; } @@ -455,12 +446,12 @@ static int ex_spawn(lua_State *L) static const luaL_reg ex_iolib[] = { - {"pipe", ex_pipe}, + {"pipe", ex_pipe}, {0,0} }; static const luaL_reg ex_iofile_methods[] = { - {"lock", ex_lock}, - {"unlock", ex_unlock}, + {"lock", ex_lock}, + {"unlock", ex_unlock}, {0,0} }; static const luaL_reg ex_oslib[] = { @@ -482,11 +473,12 @@ static const luaL_reg ex_oslib[] = { {0,0} }; static const luaL_reg ex_diriter_methods[] = { - {"__gc", diriter_close}, + {"__gc", diriter_close}, {0,0} }; static const luaL_reg ex_process_methods[] = { - {"wait", process_wait}, + {"wait", process_wait}, + {"__tostring", process_tostring}, {0,0} }; diff --git a/w32api/spawn.c b/w32api/spawn.c index 9d3c96d..dda5e76 100755 --- a/w32api/spawn.c +++ b/w32api/spawn.c @@ -135,7 +135,10 @@ int spawn_param_execute(struct spawn_params *p, struct process *proc) int ret = CreateProcess(0, c, 0, 0, 0, 0, e, 0, &p->si, &pi); if (e) free(e); free(c); - if (ret) proc->hProcess = pi.hProcess; + if (ret) { + proc->hProcess = pi.hProcess; + proc->dwProcessId = pi.dwProcessId; + } return ret; } @@ -153,3 +156,14 @@ int process_wait(lua_State *L) lua_pushnumber(L, p->status); return 1; } + +/* proc -- string */ +int process_tostring(lua_State *L) +{ + struct process *p = checkuserdata(L, 1, PROCESS_HANDLE); + char buf[40]; + lua_pushlstring(L, buf, + sprintf(buf, "process (%lu, %s)", (unsigned long)p->dwProcessId, + p->status==-1 ? "running" : "terminated")); + return 1; +} diff --git a/w32api/spawn.h b/w32api/spawn.h index 1d91235..309348c 100755 --- a/w32api/spawn.h +++ b/w32api/spawn.h @@ -15,6 +15,7 @@ struct spawn_params { struct process { int status; HANDLE hProcess; + DWORD dwProcessId; }; void spawn_param_filename(struct spawn_params *p); @@ -25,5 +26,6 @@ void spawn_param_redirects(struct spawn_params *p); int spawn_param_execute(struct spawn_params *p, struct process *proc); int process_wait(lua_State *L); +int process_tostring(lua_State *L); #endif/*SPAWN_H*/