Blob


1 #include <u.h>
2 #include <signal.h>
3 #if defined(PLAN9PORT) && defined(__sun__)
4 # define BSD_COMP /* sigh. for TIOCNOTTY */
5 #endif
6 #include <sys/ioctl.h>
7 #include "rc.h"
8 #include "getflags.h"
9 #include "exec.h"
10 #include "io.h"
11 #include "fns.h"
13 int havefork = 1;
15 void
16 Xasync(void)
17 {
18 int null = open("/dev/null", 0);
19 int tty;
20 int pid;
21 char npid[10];
22 if(null<0){
23 Xerror("Can't open /dev/null\n");
24 return;
25 }
26 switch(pid = rfork(RFFDG|RFPROC|RFNOTEG)){
27 case -1:
28 close(null);
29 Xerror("try again");
30 break;
31 case 0:
32 clearwaitpids();
33 /*
34 * I don't know what the right thing to do here is,
35 * so this is all experimentally determined.
36 * If we just dup /dev/null onto 0, then running
37 * ssh foo & will reopen /dev/tty, try to read a password,
38 * get a signal, and repeat, in a tight loop, forever.
39 * Arguably this is a bug in ssh (it behaves the same
40 * way under bash as under rc) but I'm fixing it here
41 * anyway. If we dissociate the process from the tty,
42 * then it won't be able to open /dev/tty ever again.
43 * The SIG_IGN on SIGTTOU makes writing the tty
44 * (via fd 1 or 2, for example) succeed even though
45 * our pgrp is not the terminal's controlling pgrp.
46 */
47 if((tty = open("/dev/tty", OREAD)) >= 0){
48 /*
49 * Should make reads of tty fail, writes succeed.
50 */
51 signal(SIGTTIN, SIG_IGN);
52 signal(SIGTTOU, SIG_IGN);
53 ioctl(tty, TIOCNOTTY);
54 close(tty);
55 }
56 if(isatty(0))
57 pushredir(ROPEN, null, 0);
58 else
59 close(null);
60 start(runq->code, runq->pc+1, runq->local);
61 runq->ret = 0;
62 break;
63 default:
64 addwaitpid(pid);
65 close(null);
66 runq->pc = runq->code[runq->pc].i;
67 inttoascii(npid, pid);
68 setvar("apid", newword(npid, (word *)0));
69 break;
70 }
71 }
73 void
74 Xpipe(void)
75 {
76 struct thread *p = runq;
77 int pc = p->pc, forkid;
78 int lfd = p->code[pc++].i;
79 int rfd = p->code[pc++].i;
80 int pfd[2];
81 if(pipe(pfd)<0){
82 Xerror("can't get pipe");
83 return;
84 }
85 switch(forkid = fork()){
86 case -1:
87 Xerror("try again");
88 break;
89 case 0:
90 clearwaitpids();
91 start(p->code, pc+2, runq->local);
92 runq->ret = 0;
93 close(pfd[PRD]);
94 pushredir(ROPEN, pfd[PWR], lfd);
95 break;
96 default:
97 addwaitpid(forkid);
98 start(p->code, p->code[pc].i, runq->local);
99 close(pfd[PWR]);
100 pushredir(ROPEN, pfd[PRD], rfd);
101 p->pc = p->code[pc+1].i;
102 p->pid = forkid;
103 break;
107 /*
108 * Who should wait for the exit from the fork?
109 */
110 void
111 Xbackq(void)
113 char wd[8193];
114 int c;
115 char *s, *ewd=&wd[8192], *stop;
116 struct io *f;
117 var *ifs = vlook("ifs");
118 word *v, *nextv;
119 int pfd[2];
120 int pid;
121 stop = ifs->val?ifs->val->word:"";
122 if(pipe(pfd)<0){
123 Xerror("can't make pipe");
124 return;
126 switch(pid = fork()){
127 case -1:
128 Xerror("try again");
129 close(pfd[PRD]);
130 close(pfd[PWR]);
131 return;
132 case 0:
133 clearwaitpids();
134 close(pfd[PRD]);
135 start(runq->code, runq->pc+1, runq->local);
136 pushredir(ROPEN, pfd[PWR], 1);
137 return;
138 default:
139 addwaitpid(pid);
140 close(pfd[PWR]);
141 f = openfd(pfd[PRD]);
142 s = wd;
143 v = 0;
144 while((c = rchr(f))!=EOF){
145 if(strchr(stop, c) || s==ewd){
146 if(s!=wd){
147 *s='\0';
148 v = newword(wd, v);
149 s = wd;
152 else *s++=c;
154 if(s!=wd){
155 *s='\0';
156 v = newword(wd, v);
158 closeio(f);
159 Waitfor(pid, 0);
160 /* v points to reversed arglist -- reverse it onto argv */
161 while(v){
162 nextv = v->next;
163 v->next = runq->argv->words;
164 runq->argv->words = v;
165 v = nextv;
167 runq->pc = runq->code[runq->pc].i;
168 return;
172 void
173 Xpipefd(void)
175 struct thread *p = runq;
176 int pc = p->pc, pid;
177 char name[40];
178 int pfd[2];
179 int sidefd, mainfd;
180 if(pipe(pfd)<0){
181 Xerror("can't get pipe");
182 return;
184 if(p->code[pc].i==READ){
185 sidefd = pfd[PWR];
186 mainfd = pfd[PRD];
188 else{
189 sidefd = pfd[PRD];
190 mainfd = pfd[PWR];
192 switch(pid = fork()){
193 case -1:
194 Xerror("try again");
195 break;
196 case 0:
197 clearwaitpids();
198 start(p->code, pc+2, runq->local);
199 close(mainfd);
200 pushredir(ROPEN, sidefd, p->code[pc].i==READ?1:0);
201 runq->ret = 0;
202 break;
203 default:
204 addwaitpid(pid);
205 close(sidefd);
206 pushredir(ROPEN, mainfd, mainfd); /* isn't this a noop? */
207 strcpy(name, Fdprefix);
208 inttoascii(name+strlen(name), mainfd);
209 pushword(name);
210 p->pc = p->code[pc+1].i;
211 break;
215 void
216 Xsubshell(void)
218 int pid;
219 switch(pid = fork()){
220 case -1:
221 Xerror("try again");
222 break;
223 case 0:
224 clearwaitpids();
225 start(runq->code, runq->pc+1, runq->local);
226 runq->ret = 0;
227 break;
228 default:
229 addwaitpid(pid);
230 Waitfor(pid, 1);
231 runq->pc = runq->code[runq->pc].i;
232 break;
236 int
237 execforkexec(void)
239 int pid;
240 int n;
241 char buf[ERRMAX];
243 switch(pid = fork()){
244 case -1:
245 return -1;
246 case 0:
247 clearwaitpids();
248 pushword("exec");
249 execexec();
250 strcpy(buf, "can't exec: ");
251 n = strlen(buf);
252 errstr(buf+n, ERRMAX-n);
253 Exit(buf);
255 addwaitpid(pid);
256 return pid;