3 #include "threadimpl.h"
6 procexec(Channel *pidc, char *prog, char *args[])
12 _threaddebug(DBGEXEC, "procexec %s", prog);
13 /* must be only thread in proc */
16 if(p->threads.head != t || p->threads.head->nextt != nil){
17 werrstr("not only thread in proc");
25 * We want procexec to behave like exec; if exec succeeds,
26 * never return, and if it fails, return with errstr set.
27 * Unfortunately, the exec happens in another proc since
28 * we have to wait for the exec'ed process to finish.
29 * To provide the semantics, we open a pipe with the
30 * write end close-on-exec and hand it to the proc that
31 * is doing the exec. If the exec succeeds, the pipe will
32 * close so that our read below fails. If the exec fails,
33 * then the proc doing the exec sends the errstr down the
36 if(pipe(p->exec.fd) < 0)
38 if(fcntl(p->exec.fd[1], F_SETFD, 1) < 0)
41 /* exec in parallel via the scheduler */
42 assert(p->needexec==0);
49 if((n = read(p->exec.fd[0], p->exitstr, ERRMAX-1)) > 0){ /* exec failed */
51 errstr(p->exitstr, ERRMAX);
60 /* wait for exec'ed program, then exit */
65 procexecl(Channel *pidc, char *f, ...)
67 procexec(pidc, f, &f+1);
82 _threaddebug(DBGEXEC, "_schedexecwait %d", t->ret);
93 if((c = _threadwaitchan) != nil)
98 threadexits("procexec");
108 _threaddebug(DBGEXEC, "_schedexec %s", e->prog);
110 execv(e->prog, e->args);
111 _threaddebug(DBGEXEC, "_schedexec failed: %r");
112 rerrstr(buf, sizeof buf);
114 strcpy(buf, "exec failed");
115 write(e->fd[1], buf, strlen(buf));
121 _schedexec(Execargs *e)
123 return ffork(RFFDG|RFPROC|RFMEM, efork, e);