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