fixed bug in ex_pipe()

make_pipe() should produce non-inheritable descriptors/handles
added process_tostring() method
master
mark 20 years ago
parent ed9c51ca33
commit 8d00b8c7e7

@ -284,12 +284,21 @@ static int ex_unlock(lua_State *L)
return ex_lock(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) static int make_pipe(FILE **i, FILE **o)
{ {
int fd[2]; int fd[2];
if (-1 == pipe(fd)) if (-1 == pipe(fd))
return 0; return 0;
closeonexec(fd[0]);
closeonexec(fd[1]);
*i = fdopen(fd[0], "r"); *i = fdopen(fd[0], "r");
*o = fdopen(fd[1], "w"); *o = fdopen(fd[1], "w");
return 1; return 1;
@ -306,7 +315,7 @@ static int ex_pipe(lua_State *L)
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
*(pf = lua_newuserdata(L, sizeof *pf)) = o; *(pf = lua_newuserdata(L, sizeof *pf)) = o;
lua_pushvalue(L, -2); lua_pushvalue(L, -3);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
return 2; return 2;
} }
@ -385,12 +394,12 @@ static int ex_spawn(lua_State *L)
static const luaL_reg ex_iolib[] = { static const luaL_reg ex_iolib[] = {
{"pipe", ex_pipe}, {"pipe", ex_pipe},
{0,0} {0,0}
}; };
static const luaL_reg ex_iofile_methods[] = { static const luaL_reg ex_iofile_methods[] = {
{"lock", ex_lock}, {"lock", ex_lock},
{"unlock", ex_unlock}, {"unlock", ex_unlock},
{0,0} {0,0}
}; };
static const luaL_reg ex_oslib[] = { static const luaL_reg ex_oslib[] = {
@ -412,11 +421,12 @@ static const luaL_reg ex_oslib[] = {
{0,0} {0,0}
}; };
static const luaL_reg ex_diriter_methods[] = { static const luaL_reg ex_diriter_methods[] = {
{"__gc", diriter_close}, {"__gc", diriter_close},
{0,0} {0,0}
}; };
static const luaL_reg ex_process_methods[] = { static const luaL_reg ex_process_methods[] = {
{"wait", process_wait}, {"wait", process_wait},
{"__tostring", process_tostring},
{0,0} {0,0}
}; };

@ -133,3 +133,14 @@ int process_wait(lua_State *L)
lua_pushnumber(L, p->status); lua_pushnumber(L, p->status);
return 1; 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;
}

@ -28,5 +28,6 @@ void spawn_param_env(struct spawn_params *p);
void spawn_param_redirects(struct spawn_params *p); void spawn_param_redirects(struct spawn_params *p);
int spawn_param_execute(struct spawn_params *p, struct process *proc); int spawn_param_execute(struct spawn_params *p, struct process *proc);
int process_wait(lua_State *L); int process_wait(lua_State *L);
int process_tostring(lua_State *L);
#endif/*SPAWN_H*/ #endif/*SPAWN_H*/

@ -151,17 +151,6 @@ static FILE *check_file(lua_State *L, int idx)
return *pf; 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) static uint64_t get_size(const char *name)
{ {
HANDLE h = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); 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]; HANDLE ph[2];
if (0 == CreatePipe(ph+0, ph+1, 0, 0)) if (0 == CreatePipe(ph+0, ph+1, 0, 0))
return 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"); *i = _fdopen(_open_osfhandle((long)ph[0], _O_RDONLY), "r");
*o = _fdopen(_open_osfhandle((long)ph[1], _O_WRONLY), "w"); *o = _fdopen(_open_osfhandle((long)ph[1], _O_WRONLY), "w");
return 1; return 1;
@ -376,7 +367,7 @@ static int ex_pipe(lua_State *L)
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
*(pf = lua_newuserdata(L, sizeof *pf)) = o; *(pf = lua_newuserdata(L, sizeof *pf)) = o;
lua_pushvalue(L, -2); lua_pushvalue(L, -3);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
return 2; return 2;
} }
@ -455,12 +446,12 @@ static int ex_spawn(lua_State *L)
static const luaL_reg ex_iolib[] = { static const luaL_reg ex_iolib[] = {
{"pipe", ex_pipe}, {"pipe", ex_pipe},
{0,0} {0,0}
}; };
static const luaL_reg ex_iofile_methods[] = { static const luaL_reg ex_iofile_methods[] = {
{"lock", ex_lock}, {"lock", ex_lock},
{"unlock", ex_unlock}, {"unlock", ex_unlock},
{0,0} {0,0}
}; };
static const luaL_reg ex_oslib[] = { static const luaL_reg ex_oslib[] = {
@ -482,11 +473,12 @@ static const luaL_reg ex_oslib[] = {
{0,0} {0,0}
}; };
static const luaL_reg ex_diriter_methods[] = { static const luaL_reg ex_diriter_methods[] = {
{"__gc", diriter_close}, {"__gc", diriter_close},
{0,0} {0,0}
}; };
static const luaL_reg ex_process_methods[] = { static const luaL_reg ex_process_methods[] = {
{"wait", process_wait}, {"wait", process_wait},
{"__tostring", process_tostring},
{0,0} {0,0}
}; };

@ -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); int ret = CreateProcess(0, c, 0, 0, 0, 0, e, 0, &p->si, &pi);
if (e) free(e); if (e) free(e);
free(c); free(c);
if (ret) proc->hProcess = pi.hProcess; if (ret) {
proc->hProcess = pi.hProcess;
proc->dwProcessId = pi.dwProcessId;
}
return ret; return ret;
} }
@ -153,3 +156,14 @@ int process_wait(lua_State *L)
lua_pushnumber(L, p->status); lua_pushnumber(L, p->status);
return 1; 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;
}

@ -15,6 +15,7 @@ struct spawn_params {
struct process { struct process {
int status; int status;
HANDLE hProcess; HANDLE hProcess;
DWORD dwProcessId;
}; };
void spawn_param_filename(struct spawn_params *p); 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 spawn_param_execute(struct spawn_params *p, struct process *proc);
int process_wait(lua_State *L); int process_wait(lua_State *L);
int process_tostring(lua_State *L);
#endif/*SPAWN_H*/ #endif/*SPAWN_H*/

Loading…
Cancel
Save