diff --git a/Makefile b/Makefile index 37b6f14..76334b5 100755 --- a/Makefile +++ b/Makefile @@ -5,7 +5,11 @@ INCLUDES = -I$(LUA)/include LUA = /home/mark/src/lang/lua/lua-5.1-rc1 -mingw:; $(MAKE) -C w32api ex.dll +default:; echo Choose platform: mingw cygwin posix +all: mingw cygwin +mingw:; $(MAKE) -C w32api ex.dll cygwin:; $(MAKE) -C posix ex.dll +posix:; $(MAKE) -C posix ex.so + #"EX_LIB=ex.so" "DEFINES=-D_XOPEN_SOURCE=600" diff --git a/posix/ex.c b/posix/ex.c index 43a689f..7ec5a87 100755 --- a/posix/ex.c +++ b/posix/ex.c @@ -289,6 +289,7 @@ static int ex_unlock(lua_State *L) return ex_lock(L); } + static int closeonexec(int d) { int fl = fcntl(d, F_GETFD); diff --git a/posix/posix_spawn.c b/posix/posix_spawn.c index 9a80628..cc69844 100755 --- a/posix/posix_spawn.c +++ b/posix/posix_spawn.c @@ -8,8 +8,6 @@ #include "posix_spawn.h" -#define nelemof(A) (sizeof A / sizeof *A) - int posix_spawn_file_actions_init(posix_spawn_file_actions_t *act) { act->dups[0] = act->dups[1] = act->dups[2] = -1; @@ -22,11 +20,11 @@ int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *act, int d, int errno = EBADF; return -1; } - if (2 < d) { + if (2 < n) { errno = EINVAL; return -1; } - act->dups[d] = n; + act->dups[n] = d; return 0; } @@ -46,20 +44,19 @@ int posix_spawnp(pid_t *restrict ppid, return EINVAL; if (attrp) return EINVAL; - /* check act actions? */ switch (*ppid = fork()) { case -1: return -1; default: return 0; case 0: if (act) { - size_t i; - for (i = 0; i < nelemof(act->dups); i++) - if (act->dups[i] > -1) - dup2(i, act->dups[i]); + int i; + for (i = 0; i < 3; i++) + if (act->dups[i] != -1 && -1 == dup2(act->dups[i], i)) + _exit(111); } environ = (char **)envp; execvp(path, argv); - _exit(EXIT_FAILURE); + _exit(111); /*NOTREACHED*/ } } diff --git a/posix/spawn.c b/posix/spawn.c index 5c492a1..e1cc7f7 100755 --- a/posix/spawn.c +++ b/posix/spawn.c @@ -1,15 +1,12 @@ -#include - #include -#include #include #include #include -#ifndef MISSING_POSIX_SPAWN -#include -#else +#if MISSING_POSIX_SPAWN #include "posix_spawn.h" +#else +#include #endif #include "spawn.h" @@ -21,7 +18,6 @@ struct spawn_params { }; extern int push_error(lua_State *L); -extern FILE *check_file(lua_State *L, int idx, const char *argname); struct spawn_params *spawn_param_init(lua_State *L) { @@ -105,7 +101,6 @@ void spawn_param_redirect(struct spawn_params *p, const char *stdname, FILE *f) case 'o': d = STDOUT_FILENO; break; case 'e': d = STDERR_FILENO; break; } - debug("duplicating %d to %d\n", fileno(f), d); posix_spawn_file_actions_adddup2(&p->redirect, fileno(f), d); } @@ -135,9 +130,6 @@ int spawn_param_execute(struct spawn_params *p) return ret != 0 ? push_error(L) : 1; } - -extern int push_error(lua_State *L); - /* proc -- exitcode/nil error */ int process_wait(lua_State *L) { diff --git a/w32api/ex.c b/w32api/ex.c index d0ccde8..87ba0cc 100755 --- a/w32api/ex.c +++ b/w32api/ex.c @@ -350,7 +350,6 @@ static int ex_unlock(lua_State *L) } -/* -- LUA_FILEHANDLE file file */ static int make_pipe(FILE **i, FILE **o) { HANDLE ph[2]; diff --git a/w32api/spawn.c b/w32api/spawn.c index 7932f5a..978a2fe 100755 --- a/w32api/spawn.c +++ b/w32api/spawn.c @@ -1,19 +1,17 @@ #include #include -#include #include #include #include "spawn.h" -#include -#define debug(...) fprintf(stderr, __VA_ARGS__) -#include "../lds.c" +#define debug(...) /* fprintf(stderr, __VA_ARGS__) */ +#define debug_stack(L) /* #include "../lds.c" */ -extern HANDLE get_handle(FILE *f); extern int push_error(lua_State *L); +extern HANDLE get_handle(FILE *f); static int needs_quoting(const char *s) { @@ -41,7 +39,6 @@ struct spawn_params *spawn_param_init(lua_State *L) return p; } -/* filename ... */ void spawn_param_filename(struct spawn_params *p, const char *filename) { p->cmdline = filename; @@ -82,7 +79,7 @@ void spawn_param_args(struct spawn_params *p) p->cmdline = lua_tostring(L, -1); } -/* ... envtab */ +/* ... envtab/nil */ void spawn_param_env(struct spawn_params *p) { lua_State *L = p->L; @@ -126,8 +123,8 @@ void spawn_param_redirect(struct spawn_params *p, const char *stdname, FILE *f) } struct process { - int status; - HANDLE hProcess; + int status; + HANDLE hProcess; DWORD dwProcessId; };