4 #include "threadimpl.h"
6 static void efork(int[3], int[2], char*, char**);
8 _threadexec(Channel *pidc, int fd[3], char *prog, char *args[], int freeargs)
14 _threaddebug(DBGEXEC, "threadexec %s", prog);
17 * We want threadexec to behave like exec; if exec succeeds,
18 * never return, and if it fails, return with errstr set.
19 * Unfortunately, the exec happens in another proc since
20 * we have to wait for the exec'ed process to finish.
21 * To provide the semantics, we open a pipe with the
22 * write end close-on-exec and hand it to the proc that
23 * is doing the exec. If the exec succeeds, the pipe will
24 * close so that our read below fails. If the exec fails,
25 * then the proc doing the exec sends the errstr down the
30 if(fcntl(pfd[0], F_SETFD, 1) < 0)
32 if(fcntl(pfd[1], F_SETFD, 1) < 0)
41 efork(fd, pfd, prog, args);
50 if((n = read(pfd[0], exitstr, ERRMAX-1)) > 0){ /* exec failed */
52 errstr(exitstr, ERRMAX);
60 if(fd[2] != fd[1] && fd[2] != fd[0])
65 _threaddebug(DBGEXEC, "threadexec schedexecwait");
69 _threaddebug(DBGEXEC, "threadexec bad %r");
76 threadexec(Channel *pidc, int fd[3], char *prog, char *args[])
78 if(_threadexec(pidc, fd, prog, args, 0) >= 0)
83 threadspawn(int fd[3], char *prog, char *args[])
85 return _threadexec(nil, fd, prog, args, 0);
89 * The &f+1 trick doesn't work on SunOS, so we might
90 * as well bite the bullet and do this correctly.
93 threadexecl(Channel *pidc, int fd[3], char *f, ...)
100 for(n=0; va_arg(arg, char*) != 0; n++)
105 args = malloc(n*sizeof(args[0]));
113 for(n=0; (s=va_arg(arg, char*)) != 0; n++)
118 _threadexec(pidc, fd, f, args, 1);
122 efork(int stdfd[3], int fd[2], char *prog, char **args)
127 _threaddebug(DBGEXEC, "_schedexec %s -- calling execv", prog);
136 _threaddebug(DBGEXEC, "_schedexec failed: %r");
137 rerrstr(buf, sizeof buf);
139 strcpy(buf, "exec failed");
140 write(fd[1], buf, strlen(buf));