1 76193d7c 2003-09-30 devnull #include <fcntl.h>
2 76193d7c 2003-09-30 devnull #include <unistd.h>
3 76193d7c 2003-09-30 devnull #include "threadimpl.h"
6 76193d7c 2003-09-30 devnull procexec(Channel *pidc, char *prog, char *args[])
10 76193d7c 2003-09-30 devnull Thread *t;
12 76193d7c 2003-09-30 devnull _threaddebug(DBGEXEC, "procexec %s", prog);
13 76193d7c 2003-09-30 devnull /* must be only thread in proc */
14 76193d7c 2003-09-30 devnull p = _threadgetproc();
15 76193d7c 2003-09-30 devnull t = p->thread;
16 76193d7c 2003-09-30 devnull if(p->threads.head != t || p->threads.head->nextt != nil){
17 76193d7c 2003-09-30 devnull werrstr("not only thread in proc");
19 912fba95 2003-11-24 devnull _threaddebug(DBGEXEC, "procexec bad %r");
21 76193d7c 2003-09-30 devnull sendul(pidc, ~0);
26 76193d7c 2003-09-30 devnull * We want procexec to behave like exec; if exec succeeds,
27 76193d7c 2003-09-30 devnull * never return, and if it fails, return with errstr set.
28 76193d7c 2003-09-30 devnull * Unfortunately, the exec happens in another proc since
29 76193d7c 2003-09-30 devnull * we have to wait for the exec'ed process to finish.
30 76193d7c 2003-09-30 devnull * To provide the semantics, we open a pipe with the
31 76193d7c 2003-09-30 devnull * write end close-on-exec and hand it to the proc that
32 76193d7c 2003-09-30 devnull * is doing the exec. If the exec succeeds, the pipe will
33 76193d7c 2003-09-30 devnull * close so that our read below fails. If the exec fails,
34 76193d7c 2003-09-30 devnull * then the proc doing the exec sends the errstr down the
35 76193d7c 2003-09-30 devnull * pipe to us.
37 76193d7c 2003-09-30 devnull if(pipe(p->exec.fd) < 0)
38 76193d7c 2003-09-30 devnull goto Bad;
39 912fba95 2003-11-24 devnull if(fcntl(p->exec.fd[0], F_SETFD, 1) < 0)
40 912fba95 2003-11-24 devnull goto Bad;
41 76193d7c 2003-09-30 devnull if(fcntl(p->exec.fd[1], F_SETFD, 1) < 0)
42 76193d7c 2003-09-30 devnull goto Bad;
44 76193d7c 2003-09-30 devnull /* exec in parallel via the scheduler */
45 76193d7c 2003-09-30 devnull assert(p->needexec==0);
46 76193d7c 2003-09-30 devnull p->exec.prog = prog;
47 76193d7c 2003-09-30 devnull p->exec.args = args;
48 76193d7c 2003-09-30 devnull p->needexec = 1;
49 76193d7c 2003-09-30 devnull _sched();
51 76193d7c 2003-09-30 devnull close(p->exec.fd[1]);
52 76193d7c 2003-09-30 devnull if((n = read(p->exec.fd[0], p->exitstr, ERRMAX-1)) > 0){ /* exec failed */
53 76193d7c 2003-09-30 devnull p->exitstr[n] = '\0';
54 76193d7c 2003-09-30 devnull errstr(p->exitstr, ERRMAX);
55 76193d7c 2003-09-30 devnull close(p->exec.fd[0]);
56 76193d7c 2003-09-30 devnull goto Bad;
58 76193d7c 2003-09-30 devnull close(p->exec.fd[0]);
61 76193d7c 2003-09-30 devnull sendul(pidc, t->ret);
63 912fba95 2003-11-24 devnull _threaddebug(DBGEXEC, "procexec schedexecwait");
64 76193d7c 2003-09-30 devnull /* wait for exec'ed program, then exit */
65 76193d7c 2003-09-30 devnull _schedexecwait();
69 76193d7c 2003-09-30 devnull procexecl(Channel *pidc, char *f, ...)
71 76193d7c 2003-09-30 devnull procexec(pidc, f, &f+1);
75 76193d7c 2003-09-30 devnull _schedexecwait(void)
78 76193d7c 2003-09-30 devnull Channel *c;
80 76193d7c 2003-09-30 devnull Thread *t;
81 76193d7c 2003-09-30 devnull Waitmsg *w;
83 76193d7c 2003-09-30 devnull p = _threadgetproc();
84 76193d7c 2003-09-30 devnull t = p->thread;
85 76193d7c 2003-09-30 devnull pid = t->ret;
86 76193d7c 2003-09-30 devnull _threaddebug(DBGEXEC, "_schedexecwait %d", t->ret);
89 76193d7c 2003-09-30 devnull w = wait();
90 76193d7c 2003-09-30 devnull if(w == nil)
92 76193d7c 2003-09-30 devnull if(w->pid == pid)
96 76193d7c 2003-09-30 devnull if(w != nil){
97 76193d7c 2003-09-30 devnull if((c = _threadwaitchan) != nil)
98 76193d7c 2003-09-30 devnull sendp(c, w);
100 76193d7c 2003-09-30 devnull free(w);
102 76193d7c 2003-09-30 devnull threadexits("procexec");
105 76193d7c 2003-09-30 devnull static void
106 76193d7c 2003-09-30 devnull efork(void *ve)
108 76193d7c 2003-09-30 devnull char buf[ERRMAX];
109 76193d7c 2003-09-30 devnull Execargs *e;
112 912fba95 2003-11-24 devnull _threaddebug(DBGEXEC, "_schedexec %s -- calling execv", e->prog);
113 76193d7c 2003-09-30 devnull execv(e->prog, e->args);
114 76193d7c 2003-09-30 devnull _threaddebug(DBGEXEC, "_schedexec failed: %r");
115 76193d7c 2003-09-30 devnull rerrstr(buf, sizeof buf);
116 76193d7c 2003-09-30 devnull if(buf[0]=='\0')
117 76193d7c 2003-09-30 devnull strcpy(buf, "exec failed");
118 76193d7c 2003-09-30 devnull write(e->fd[1], buf, strlen(buf));
119 76193d7c 2003-09-30 devnull close(e->fd[1]);
120 76193d7c 2003-09-30 devnull _exits(buf);
124 76193d7c 2003-09-30 devnull _schedexec(Execargs *e)
126 912fba95 2003-11-24 devnull int pid;
128 912fba95 2003-11-24 devnull pid = fork();
129 912fba95 2003-11-24 devnull if(pid == 0){
130 912fba95 2003-11-24 devnull efork(e);
131 912fba95 2003-11-24 devnull _exit(1);
133 912fba95 2003-11-24 devnull return pid;