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");
75 threadexec(Channel *pidc, int fd[3], char *prog, char *args[])
77 _threadexec(pidc, fd, prog, args, 0);
81 * The &f+1 trick doesn't work on SunOS, so we might
82 * as well bite the bullet and do this correctly.
85 threadexecl(Channel *pidc, int fd[3], char *f, ...)
92 for(n=0; va_arg(arg, char*) != 0; n++)
97 args = malloc(n*sizeof(args[0]));
105 for(n=0; (s=va_arg(arg, char*)) != 0; n++)
110 _threadexec(pidc, fd, f, args, 1);
114 efork(int stdfd[3], int fd[2], char *prog, char **args)
119 _threaddebug(DBGEXEC, "_schedexec %s -- calling execv", prog);
128 _threaddebug(DBGEXEC, "_schedexec failed: %r");
129 rerrstr(buf, sizeof buf);
131 strcpy(buf, "exec failed");
132 write(fd[1], buf, strlen(buf));