sem.c revision 1.1.1.2 1 /*-
2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)sem.c 8.1 (Berkeley) 5/31/93";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #if __STDC__
47 # include <stdarg.h>
48 #else
49 # include <varargs.h>
50 #endif
51
52 #include "csh.h"
53 #include "proc.h"
54 #include "extern.h"
55
56 static void vffree __P((int));
57 static Char *splicepipe __P((struct command *t, Char *));
58 static void doio __P((struct command *t, int *, int *));
59 static void chkclob __P((char *));
60
61 void
62 execute(t, wanttty, pipein, pipeout)
63 register struct command *t;
64 int wanttty, *pipein, *pipeout;
65 {
66 bool forked = 0;
67 struct biltins *bifunc;
68 int pid = 0;
69 int pv[2];
70
71 static sigset_t csigmask;
72
73 static sigset_t ocsigmask;
74 static int onosigchld = 0;
75 static int nosigchld = 0;
76
77 UNREGISTER(forked);
78 UNREGISTER(bifunc);
79 UNREGISTER(wanttty);
80
81 if (t == 0)
82 return;
83
84 if (t->t_dflg & F_AMPERSAND)
85 wanttty = 0;
86 switch (t->t_dtyp) {
87
88 case NODE_COMMAND:
89 if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE)
90 (void) Strcpy(t->t_dcom[0], t->t_dcom[0] + 1);
91 if ((t->t_dflg & F_REPEAT) == 0)
92 Dfix(t); /* $ " ' \ */
93 if (t->t_dcom[0] == 0)
94 return;
95 /* fall into... */
96
97 case NODE_PAREN:
98 if (t->t_dflg & F_PIPEOUT)
99 mypipe(pipeout);
100 /*
101 * Must do << early so parent will know where input pointer should be.
102 * If noexec then this is all we do.
103 */
104 if (t->t_dflg & F_READ) {
105 (void) close(0);
106 heredoc(t->t_dlef);
107 if (noexec)
108 (void) close(0);
109 }
110
111 set(STRstatus, Strsave(STR0));
112
113 /*
114 * This mess is the necessary kludge to handle the prefix builtins:
115 * nice, nohup, time. These commands can also be used by themselves,
116 * and this is not handled here. This will also work when loops are
117 * parsed.
118 */
119 while (t->t_dtyp == NODE_COMMAND)
120 if (eq(t->t_dcom[0], STRnice))
121 if (t->t_dcom[1])
122 if (strchr("+-", t->t_dcom[1][0]))
123 if (t->t_dcom[2]) {
124 setname("nice");
125 t->t_nice =
126 getn(t->t_dcom[1]);
127 lshift(t->t_dcom, 2);
128 t->t_dflg |= F_NICE;
129 }
130 else
131 break;
132 else {
133 t->t_nice = 4;
134 lshift(t->t_dcom, 1);
135 t->t_dflg |= F_NICE;
136 }
137 else
138 break;
139 else if (eq(t->t_dcom[0], STRnohup))
140 if (t->t_dcom[1]) {
141 t->t_dflg |= F_NOHUP;
142 lshift(t->t_dcom, 1);
143 }
144 else
145 break;
146 else if (eq(t->t_dcom[0], STRtime))
147 if (t->t_dcom[1]) {
148 t->t_dflg |= F_TIME;
149 lshift(t->t_dcom, 1);
150 }
151 else
152 break;
153 else
154 break;
155
156 /* is it a command */
157 if (t->t_dtyp == NODE_COMMAND) {
158 /*
159 * Check if we have a builtin function and remember which one.
160 */
161 bifunc = isbfunc(t);
162 if (noexec) {
163 /*
164 * Continue for builtins that are part of the scripting language
165 */
166 if (bifunc->bfunct != dobreak && bifunc->bfunct != docontin &&
167 bifunc->bfunct != doelse && bifunc->bfunct != doend &&
168 bifunc->bfunct != doforeach && bifunc->bfunct != dogoto &&
169 bifunc->bfunct != doif && bifunc->bfunct != dorepeat &&
170 bifunc->bfunct != doswbrk && bifunc->bfunct != doswitch &&
171 bifunc->bfunct != dowhile && bifunc->bfunct != dozip)
172 break;
173 }
174 }
175 else { /* not a command */
176 bifunc = NULL;
177 if (noexec)
178 break;
179 }
180
181 /*
182 * We fork only if we are timed, or are not the end of a parenthesized
183 * list and not a simple builtin function. Simple meaning one that is
184 * not pipedout, niced, nohupped, or &'d. It would be nice(?) to not
185 * fork in some of these cases.
186 */
187 /*
188 * Prevent forking cd, pushd, popd, chdir cause this will cause the
189 * shell not to change dir!
190 */
191 if (bifunc && (bifunc->bfunct == dochngd ||
192 bifunc->bfunct == dopushd ||
193 bifunc->bfunct == dopopd))
194 t->t_dflg &= ~(F_NICE);
195 if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 &&
196 (!bifunc || t->t_dflg &
197 (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) ||
198 /*
199 * We have to fork for eval too.
200 */
201 (bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 &&
202 bifunc->bfunct == doeval))
203 if (t->t_dtyp == NODE_PAREN ||
204 t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) {
205 forked++;
206 /*
207 * We need to block SIGCHLD here, so that if the process does
208 * not die before we can set the process group
209 */
210 if (wanttty >= 0 && !nosigchld) {
211 csigmask = sigblock(sigmask(SIGCHLD));
212 nosigchld = 1;
213 }
214
215 pid = pfork(t, wanttty);
216 if (pid == 0 && nosigchld) {
217 (void) sigsetmask(csigmask);
218 nosigchld = 0;
219 }
220 else if (pid != 0 && (t->t_dflg & F_AMPERSAND))
221 backpid = pid;
222
223 }
224 else {
225 int ochild, osetintr, ohaderr, odidfds;
226 int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp;
227 sigset_t omask;
228
229 /*
230 * Prepare for the vfork by saving everything that the child
231 * corrupts before it exec's. Note that in some signal
232 * implementations which keep the signal info in user space
233 * (e.g. Sun's) it will also be necessary to save and restore
234 * the current sigvec's for the signals the child touches
235 * before it exec's.
236 */
237 if (wanttty >= 0 && !nosigchld && !noexec) {
238 csigmask = sigblock(sigmask(SIGCHLD));
239 nosigchld = 1;
240 }
241 omask = sigblock(sigmask(SIGCHLD) | sigmask(SIGINT));
242 ochild = child;
243 osetintr = setintr;
244 ohaderr = haderr;
245 odidfds = didfds;
246 oSHIN = SHIN;
247 oSHOUT = SHOUT;
248 oSHERR = SHERR;
249 oOLDSTD = OLDSTD;
250 otpgrp = tpgrp;
251 ocsigmask = csigmask;
252 onosigchld = nosigchld;
253 Vsav = Vdp = 0;
254 Vexpath = 0;
255 Vt = 0;
256 pid = vfork();
257
258 if (pid < 0) {
259 (void) sigsetmask(omask);
260 stderror(ERR_NOPROC);
261 }
262 forked++;
263 if (pid) { /* parent */
264 child = ochild;
265 setintr = osetintr;
266 haderr = ohaderr;
267 didfds = odidfds;
268 SHIN = oSHIN;
269 SHOUT = oSHOUT;
270 SHERR = oSHERR;
271 OLDSTD = oOLDSTD;
272 tpgrp = otpgrp;
273 csigmask = ocsigmask;
274 nosigchld = onosigchld;
275
276 xfree((ptr_t) Vsav);
277 Vsav = 0;
278 xfree((ptr_t) Vdp);
279 Vdp = 0;
280 xfree((ptr_t) Vexpath);
281 Vexpath = 0;
282 blkfree((Char **) Vt);
283 Vt = 0;
284 /* this is from pfork() */
285 palloc(pid, t);
286 (void) sigsetmask(omask);
287 }
288 else { /* child */
289 /* this is from pfork() */
290 int pgrp;
291 bool ignint = 0;
292
293 if (nosigchld) {
294 (void) sigsetmask(csigmask);
295 nosigchld = 0;
296 }
297
298 if (setintr)
299 ignint =
300 (tpgrp == -1 &&
301 (t->t_dflg & F_NOINTERRUPT))
302 || (gointr && eq(gointr, STRminus));
303 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
304 child++;
305 if (setintr) {
306 setintr = 0;
307 if (ignint) {
308 (void) signal(SIGINT, SIG_IGN);
309 (void) signal(SIGQUIT, SIG_IGN);
310 }
311 else {
312 (void) signal(SIGINT, vffree);
313 (void) signal(SIGQUIT, SIG_DFL);
314 }
315
316 if (wanttty >= 0) {
317 (void) signal(SIGTSTP, SIG_DFL);
318 (void) signal(SIGTTIN, SIG_DFL);
319 (void) signal(SIGTTOU, SIG_DFL);
320 }
321
322 (void) signal(SIGTERM, parterm);
323 }
324 else if (tpgrp == -1 &&
325 (t->t_dflg & F_NOINTERRUPT)) {
326 (void) signal(SIGINT, SIG_IGN);
327 (void) signal(SIGQUIT, SIG_IGN);
328 }
329
330 pgetty(wanttty, pgrp);
331 if (t->t_dflg & F_NOHUP)
332 (void) signal(SIGHUP, SIG_IGN);
333 if (t->t_dflg & F_NICE)
334 (void) setpriority(PRIO_PROCESS, 0, t->t_nice);
335 }
336
337 }
338 if (pid != 0) {
339 /*
340 * It would be better if we could wait for the whole job when we
341 * knew the last process had been started. Pwait, in fact, does
342 * wait for the whole job anyway, but this test doesn't really
343 * express our intentions.
344 */
345 if (didfds == 0 && t->t_dflg & F_PIPEIN) {
346 (void) close(pipein[0]);
347 (void) close(pipein[1]);
348 }
349 if ((t->t_dflg & F_PIPEOUT) == 0) {
350 if (nosigchld) {
351 (void) sigsetmask(csigmask);
352 nosigchld = 0;
353 }
354 if ((t->t_dflg & F_AMPERSAND) == 0)
355 pwait();
356 }
357 break;
358 }
359 doio(t, pipein, pipeout);
360 if (t->t_dflg & F_PIPEOUT) {
361 (void) close(pipeout[0]);
362 (void) close(pipeout[1]);
363 }
364 /*
365 * Perform a builtin function. If we are not forked, arrange for
366 * possible stopping
367 */
368 if (bifunc) {
369 func(t, bifunc);
370 if (forked)
371 exitstat();
372 break;
373 }
374 if (t->t_dtyp != NODE_PAREN) {
375 doexec(NULL, t);
376 /* NOTREACHED */
377 }
378 /*
379 * For () commands must put new 0,1,2 in FSH* and recurse
380 */
381 OLDSTD = dcopy(0, FOLDSTD);
382 SHOUT = dcopy(1, FSHOUT);
383 SHERR = dcopy(2, FSHERR);
384 (void) close(SHIN);
385 SHIN = -1;
386 didfds = 0;
387 wanttty = -1;
388 t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT;
389 execute(t->t_dspr, wanttty, NULL, NULL);
390 exitstat();
391
392 case NODE_PIPE:
393 t->t_dcar->t_dflg |= F_PIPEOUT |
394 (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT));
395 execute(t->t_dcar, wanttty, pipein, pv);
396 t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg &
397 (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT));
398 if (wanttty > 0)
399 wanttty = 0; /* got tty already */
400 execute(t->t_dcdr, wanttty, pv, pipeout);
401 break;
402
403 case NODE_LIST:
404 if (t->t_dcar) {
405 t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
406 execute(t->t_dcar, wanttty, NULL, NULL);
407 /*
408 * In strange case of A&B make a new job after A
409 */
410 if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr &&
411 (t->t_dcdr->t_dflg & F_AMPERSAND) == 0)
412 pendjob();
413 }
414 if (t->t_dcdr) {
415 t->t_dcdr->t_dflg |= t->t_dflg &
416 (F_NOFORK | F_NOINTERRUPT);
417 execute(t->t_dcdr, wanttty, NULL, NULL);
418 }
419 break;
420
421 case NODE_OR:
422 case NODE_AND:
423 if (t->t_dcar) {
424 t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT;
425 execute(t->t_dcar, wanttty, NULL, NULL);
426 if ((getn(value(STRstatus)) == 0) !=
427 (t->t_dtyp == NODE_AND))
428 return;
429 }
430 if (t->t_dcdr) {
431 t->t_dcdr->t_dflg |= t->t_dflg &
432 (F_NOFORK | F_NOINTERRUPT);
433 execute(t->t_dcdr, wanttty, NULL, NULL);
434 }
435 break;
436 }
437 /*
438 * Fall through for all breaks from switch
439 *
440 * If there will be no more executions of this command, flush all file
441 * descriptors. Places that turn on the F_REPEAT bit are responsible for
442 * doing donefds after the last re-execution
443 */
444 if (didfds && !(t->t_dflg & F_REPEAT))
445 donefds();
446 }
447
448 static void
449 vffree(i)
450 int i;
451 {
452 register Char **v;
453
454 if ((v = gargv) != NULL) {
455 gargv = 0;
456 xfree((ptr_t) v);
457 }
458 if ((v = pargv) != NULL) {
459 pargv = 0;
460 xfree((ptr_t) v);
461 }
462 _exit(i);
463 }
464
465 /*
466 * Expand and glob the words after an i/o redirection.
467 * If more than one word is generated, then update the command vector.
468 *
469 * This is done differently in all the shells:
470 * 1. in the bourne shell and ksh globbing is not performed
471 * 2. Bash/csh say ambiguous
472 * 3. zsh does i/o to/from all the files
473 * 4. itcsh concatenates the words.
474 *
475 * I don't know what is best to do. I think that Ambiguous is better
476 * than restructuring the command vector, because the user can get
477 * unexpected results. In any case, the command vector restructuring
478 * code is present and the user can choose it by setting noambiguous
479 */
480 static Char *
481 splicepipe(t, cp)
482 register struct command *t;
483 Char *cp; /* word after < or > */
484 {
485 Char *blk[2];
486
487 if (adrof(STRnoambiguous)) {
488 Char **pv;
489
490 blk[0] = Dfix1(cp); /* expand $ */
491 blk[1] = NULL;
492
493 gflag = 0, tglob(blk);
494 if (gflag) {
495 pv = globall(blk);
496 if (pv == NULL) {
497 setname(vis_str(blk[0]));
498 xfree((ptr_t) blk[0]);
499 stderror(ERR_NAME | ERR_NOMATCH);
500 }
501 gargv = NULL;
502 if (pv[1] != NULL) { /* we need to fix the command vector */
503 Char **av = blkspl(t->t_dcom, &pv[1]);
504 xfree((ptr_t) t->t_dcom);
505 t->t_dcom = av;
506 }
507 xfree((ptr_t) blk[0]);
508 blk[0] = pv[0];
509 xfree((ptr_t) pv);
510 }
511 }
512 else {
513 blk[0] = globone(blk[1] = Dfix1(cp), G_ERROR);
514 xfree((ptr_t) blk[1]);
515 }
516 return(blk[0]);
517 }
518
519 /*
520 * Perform io redirection.
521 * We may or maynot be forked here.
522 */
523 static void
524 doio(t, pipein, pipeout)
525 register struct command *t;
526 int *pipein, *pipeout;
527 {
528 register int fd;
529 register Char *cp;
530 register int flags = t->t_dflg;
531
532 if (didfds || (flags & F_REPEAT))
533 return;
534 if ((flags & F_READ) == 0) {/* F_READ already done */
535 if (t->t_dlef) {
536 char tmp[MAXPATHLEN+1];
537
538 /*
539 * so < /dev/std{in,out,err} work
540 */
541 (void) dcopy(SHIN, 0);
542 (void) dcopy(SHOUT, 1);
543 (void) dcopy(SHERR, 2);
544 cp = splicepipe(t, t->t_dlef);
545 (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
546 tmp[MAXPATHLEN] = '\0';
547 xfree((ptr_t) cp);
548 if ((fd = open(tmp, O_RDONLY)) < 0)
549 stderror(ERR_SYSTEM, tmp, strerror(errno));
550 (void) dmove(fd, 0);
551 }
552 else if (flags & F_PIPEIN) {
553 (void) close(0);
554 (void) dup(pipein[0]);
555 (void) close(pipein[0]);
556 (void) close(pipein[1]);
557 }
558 else if ((flags & F_NOINTERRUPT) && tpgrp == -1) {
559 (void) close(0);
560 (void) open(_PATH_DEVNULL, O_RDONLY);
561 }
562 else {
563 (void) close(0);
564 (void) dup(OLDSTD);
565 (void) ioctl(0, FIONCLEX, NULL);
566 }
567 }
568 if (t->t_drit) {
569 char tmp[MAXPATHLEN+1];
570
571 cp = splicepipe(t, t->t_drit);
572 (void) strncpy(tmp, short2str(cp), MAXPATHLEN);
573 tmp[MAXPATHLEN] = '\0';
574 xfree((ptr_t) cp);
575 /*
576 * so > /dev/std{out,err} work
577 */
578 (void) dcopy(SHOUT, 1);
579 (void) dcopy(SHERR, 2);
580 if ((flags & F_APPEND) &&
581 #ifdef O_APPEND
582 (fd = open(tmp, O_WRONLY | O_APPEND)) >= 0);
583 #else
584 (fd = open(tmp, O_WRONLY)) >= 0)
585 (void) lseek(1, (off_t) 0, L_XTND);
586 #endif
587 else {
588 if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) {
589 if (flags & F_APPEND)
590 stderror(ERR_SYSTEM, tmp, strerror(errno));
591 chkclob(tmp);
592 }
593 if ((fd = creat(tmp, 0666)) < 0)
594 stderror(ERR_SYSTEM, tmp, strerror(errno));
595 }
596 (void) dmove(fd, 1);
597 }
598 else if (flags & F_PIPEOUT) {
599 (void) close(1);
600 (void) dup(pipeout[1]);
601 }
602 else {
603 (void) close(1);
604 (void) dup(SHOUT);
605 (void) ioctl(1, FIONCLEX, NULL);
606 }
607
608 (void) close(2);
609 if (flags & F_STDERR) {
610 (void) dup(1);
611 }
612 else {
613 (void) dup(SHERR);
614 (void) ioctl(2, FIONCLEX, NULL);
615 }
616 didfds = 1;
617 }
618
619 void
620 mypipe(pv)
621 register int *pv;
622 {
623
624 if (pipe(pv) < 0)
625 goto oops;
626 pv[0] = dmove(pv[0], -1);
627 pv[1] = dmove(pv[1], -1);
628 if (pv[0] >= 0 && pv[1] >= 0)
629 return;
630 oops:
631 stderror(ERR_PIPE);
632 }
633
634 static void
635 chkclob(cp)
636 register char *cp;
637 {
638 struct stat stb;
639
640 if (stat(cp, &stb) < 0)
641 return;
642 if (S_ISCHR(stb.st_mode))
643 return;
644 stderror(ERR_EXISTS, cp);
645 }
646