Blame


1 be22ae2d 2004-03-26 devnull #include <u.h>
2 76193d7c 2003-09-30 devnull #include <fcntl.h>
3 76193d7c 2003-09-30 devnull #include <unistd.h>
4 76193d7c 2003-09-30 devnull #include "threadimpl.h"
5 76193d7c 2003-09-30 devnull
6 5a8e63b2 2004-02-29 devnull static void efork(int[3], int[2], char*, char**);
7 8ad51794 2004-03-25 devnull static void
8 8ad51794 2004-03-25 devnull _threadexec(Channel *pidc, int fd[3], char *prog, char *args[], int freeargs)
9 76193d7c 2003-09-30 devnull {
10 5a8e63b2 2004-02-29 devnull int pfd[2];
11 5a8e63b2 2004-02-29 devnull int n, pid;
12 5a8e63b2 2004-02-29 devnull char exitstr[ERRMAX];
13 76193d7c 2003-09-30 devnull
14 5a8e63b2 2004-02-29 devnull _threaddebug(DBGEXEC, "threadexec %s", prog);
15 5a8e63b2 2004-02-29 devnull
16 76193d7c 2003-09-30 devnull /*
17 5a8e63b2 2004-02-29 devnull * We want threadexec to behave like exec; if exec succeeds,
18 76193d7c 2003-09-30 devnull * never return, and if it fails, return with errstr set.
19 76193d7c 2003-09-30 devnull * Unfortunately, the exec happens in another proc since
20 76193d7c 2003-09-30 devnull * we have to wait for the exec'ed process to finish.
21 76193d7c 2003-09-30 devnull * To provide the semantics, we open a pipe with the
22 76193d7c 2003-09-30 devnull * write end close-on-exec and hand it to the proc that
23 76193d7c 2003-09-30 devnull * is doing the exec. If the exec succeeds, the pipe will
24 76193d7c 2003-09-30 devnull * close so that our read below fails. If the exec fails,
25 76193d7c 2003-09-30 devnull * then the proc doing the exec sends the errstr down the
26 76193d7c 2003-09-30 devnull * pipe to us.
27 76193d7c 2003-09-30 devnull */
28 5a8e63b2 2004-02-29 devnull if(pipe(pfd) < 0)
29 76193d7c 2003-09-30 devnull goto Bad;
30 5a8e63b2 2004-02-29 devnull if(fcntl(pfd[0], F_SETFD, 1) < 0)
31 912fba95 2003-11-24 devnull goto Bad;
32 5a8e63b2 2004-02-29 devnull if(fcntl(pfd[1], F_SETFD, 1) < 0)
33 76193d7c 2003-09-30 devnull goto Bad;
34 76193d7c 2003-09-30 devnull
35 5a8e63b2 2004-02-29 devnull switch(pid = fork()){
36 5a8e63b2 2004-02-29 devnull case -1:
37 5a8e63b2 2004-02-29 devnull close(pfd[0]);
38 5a8e63b2 2004-02-29 devnull close(pfd[1]);
39 5a8e63b2 2004-02-29 devnull goto Bad;
40 5a8e63b2 2004-02-29 devnull case 0:
41 5a8e63b2 2004-02-29 devnull efork(fd, pfd, prog, args);
42 5a8e63b2 2004-02-29 devnull _exit(0);
43 5a8e63b2 2004-02-29 devnull default:
44 8ad51794 2004-03-25 devnull if(freeargs)
45 8ad51794 2004-03-25 devnull free(args);
46 5a8e63b2 2004-02-29 devnull break;
47 5a8e63b2 2004-02-29 devnull }
48 76193d7c 2003-09-30 devnull
49 5a8e63b2 2004-02-29 devnull close(pfd[1]);
50 5a8e63b2 2004-02-29 devnull if((n = read(pfd[0], exitstr, ERRMAX-1)) > 0){ /* exec failed */
51 5a8e63b2 2004-02-29 devnull exitstr[n] = '\0';
52 5a8e63b2 2004-02-29 devnull errstr(exitstr, ERRMAX);
53 5a8e63b2 2004-02-29 devnull close(pfd[0]);
54 76193d7c 2003-09-30 devnull goto Bad;
55 76193d7c 2003-09-30 devnull }
56 5a8e63b2 2004-02-29 devnull close(pfd[0]);
57 32f69c36 2003-12-11 devnull close(fd[0]);
58 32f69c36 2003-12-11 devnull if(fd[1] != fd[0])
59 32f69c36 2003-12-11 devnull close(fd[1]);
60 32f69c36 2003-12-11 devnull if(fd[2] != fd[1] && fd[2] != fd[0])
61 32f69c36 2003-12-11 devnull close(fd[2]);
62 76193d7c 2003-09-30 devnull if(pidc)
63 5a8e63b2 2004-02-29 devnull sendul(pidc, pid);
64 76193d7c 2003-09-30 devnull
65 5a8e63b2 2004-02-29 devnull _threaddebug(DBGEXEC, "threadexec schedexecwait");
66 5a8e63b2 2004-02-29 devnull threadexits(0);
67 76193d7c 2003-09-30 devnull
68 5a8e63b2 2004-02-29 devnull Bad:
69 5a8e63b2 2004-02-29 devnull _threaddebug(DBGEXEC, "threadexec bad %r");
70 5a8e63b2 2004-02-29 devnull if(pidc)
71 5a8e63b2 2004-02-29 devnull sendul(pidc, ~0);
72 76193d7c 2003-09-30 devnull }
73 76193d7c 2003-09-30 devnull
74 76193d7c 2003-09-30 devnull void
75 8ad51794 2004-03-25 devnull threadexec(Channel *pidc, int fd[3], char *prog, char *args[])
76 8ad51794 2004-03-25 devnull {
77 8ad51794 2004-03-25 devnull _threadexec(pidc, fd, prog, args, 0);
78 8ad51794 2004-03-25 devnull }
79 8ad51794 2004-03-25 devnull
80 8ad51794 2004-03-25 devnull /*
81 8ad51794 2004-03-25 devnull * The &f+1 trick doesn't work on SunOS, so we might
82 8ad51794 2004-03-25 devnull * as well bite the bullet and do this correctly.
83 8ad51794 2004-03-25 devnull */
84 8ad51794 2004-03-25 devnull void
85 5a8e63b2 2004-02-29 devnull threadexecl(Channel *pidc, int fd[3], char *f, ...)
86 76193d7c 2003-09-30 devnull {
87 8ad51794 2004-03-25 devnull char **args, *s;
88 8ad51794 2004-03-25 devnull int n;
89 8ad51794 2004-03-25 devnull va_list arg;
90 8ad51794 2004-03-25 devnull
91 8ad51794 2004-03-25 devnull va_start(arg, f);
92 8ad51794 2004-03-25 devnull for(n=0; va_arg(arg, char*) != 0; n++)
93 8ad51794 2004-03-25 devnull ;
94 8ad51794 2004-03-25 devnull n++;
95 8ad51794 2004-03-25 devnull va_end(arg);
96 8ad51794 2004-03-25 devnull
97 8ad51794 2004-03-25 devnull args = malloc(n*sizeof(args[0]));
98 8ad51794 2004-03-25 devnull if(args == nil){
99 8ad51794 2004-03-25 devnull if(pidc)
100 8ad51794 2004-03-25 devnull sendul(pidc, ~0);
101 8ad51794 2004-03-25 devnull return;
102 8ad51794 2004-03-25 devnull }
103 8ad51794 2004-03-25 devnull
104 8ad51794 2004-03-25 devnull va_start(arg, f);
105 8ad51794 2004-03-25 devnull for(n=0; (s=va_arg(arg, char*)) != 0; n++)
106 8ad51794 2004-03-25 devnull args[n] = s;
107 8ad51794 2004-03-25 devnull args[n] = 0;
108 8ad51794 2004-03-25 devnull va_end(arg);
109 8ad51794 2004-03-25 devnull
110 8ad51794 2004-03-25 devnull _threadexec(pidc, fd, f, args, 1);
111 76193d7c 2003-09-30 devnull }
112 76193d7c 2003-09-30 devnull
113 76193d7c 2003-09-30 devnull static void
114 5a8e63b2 2004-02-29 devnull efork(int stdfd[3], int fd[2], char *prog, char **args)
115 76193d7c 2003-09-30 devnull {
116 76193d7c 2003-09-30 devnull char buf[ERRMAX];
117 32f69c36 2003-12-11 devnull int i;
118 76193d7c 2003-09-30 devnull
119 5a8e63b2 2004-02-29 devnull _threaddebug(DBGEXEC, "_schedexec %s -- calling execv", prog);
120 5a8e63b2 2004-02-29 devnull dup(stdfd[0], 0);
121 5a8e63b2 2004-02-29 devnull dup(stdfd[1], 1);
122 5a8e63b2 2004-02-29 devnull dup(stdfd[2], 2);
123 32f69c36 2003-12-11 devnull for(i=3; i<40; i++)
124 5a8e63b2 2004-02-29 devnull if(i != fd[1])
125 32f69c36 2003-12-11 devnull close(i);
126 49588d5d 2003-12-17 devnull rfork(RFNOTEG);
127 5a8e63b2 2004-02-29 devnull execvp(prog, args);
128 76193d7c 2003-09-30 devnull _threaddebug(DBGEXEC, "_schedexec failed: %r");
129 76193d7c 2003-09-30 devnull rerrstr(buf, sizeof buf);
130 76193d7c 2003-09-30 devnull if(buf[0]=='\0')
131 76193d7c 2003-09-30 devnull strcpy(buf, "exec failed");
132 5a8e63b2 2004-02-29 devnull write(fd[1], buf, strlen(buf));
133 5a8e63b2 2004-02-29 devnull close(fd[1]);
134 76193d7c 2003-09-30 devnull _exits(buf);
135 76193d7c 2003-09-30 devnull }
136 76193d7c 2003-09-30 devnull