3 #include "threadimpl.h"
5 static void efork(int[3], int[2], char*, char**);
7 threadexec(Channel *pidc, int fd[3], char *prog, char *args[])
13 _threaddebug(DBGEXEC, "threadexec %s", prog);
16 * We want threadexec to behave like exec; if exec succeeds,
17 * never return, and if it fails, return with errstr set.
18 * Unfortunately, the exec happens in another proc since
19 * we have to wait for the exec'ed process to finish.
20 * To provide the semantics, we open a pipe with the
21 * write end close-on-exec and hand it to the proc that
22 * is doing the exec. If the exec succeeds, the pipe will
23 * close so that our read below fails. If the exec fails,
24 * then the proc doing the exec sends the errstr down the
29 if(fcntl(pfd[0], F_SETFD, 1) < 0)
31 if(fcntl(pfd[1], F_SETFD, 1) < 0)
40 efork(fd, pfd, prog, args);
47 if((n = read(pfd[0], exitstr, ERRMAX-1)) > 0){ /* exec failed */
49 errstr(exitstr, ERRMAX);
57 if(fd[2] != fd[1] && fd[2] != fd[0])
62 _threaddebug(DBGEXEC, "threadexec schedexecwait");
66 _threaddebug(DBGEXEC, "threadexec bad %r");
72 threadexecl(Channel *pidc, int fd[3], char *f, ...)
74 threadexec(pidc, fd, f, &f+1);
78 efork(int stdfd[3], int fd[2], char *prog, char **args)
83 _threaddebug(DBGEXEC, "_schedexec %s -- calling execv", prog);
92 _threaddebug(DBGEXEC, "_schedexec failed: %r");
93 rerrstr(buf, sizeof buf);
95 strcpy(buf, "exec failed");
96 write(fd[1], buf, strlen(buf));