Home | History | Annotate | Line # | Download | only in sh
redir.c revision 1.6
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * This code is derived from software contributed to Berkeley by
      6  1.1      cgd  * Kenneth Almquist.
      7  1.1      cgd  *
      8  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1      cgd  * modification, are permitted provided that the following conditions
     10  1.1      cgd  * are met:
     11  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1      cgd  *    must display the following acknowledgement:
     18  1.1      cgd  *	This product includes software developed by the University of
     19  1.1      cgd  *	California, Berkeley and its contributors.
     20  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1      cgd  *    may be used to endorse or promote products derived from this software
     22  1.1      cgd  *    without specific prior written permission.
     23  1.1      cgd  *
     24  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1      cgd  * SUCH DAMAGE.
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.1      cgd #ifndef lint
     38  1.5  mycroft /*static char sccsid[] = "from: @(#)redir.c	5.1 (Berkeley) 3/7/91";*/
     39  1.6      cgd static char rcsid[] = "$Id: redir.c,v 1.6 1994/04/25 18:47:17 cgd Exp $";
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd /*
     43  1.1      cgd  * Code for dealing with input/output redirection.
     44  1.1      cgd  */
     45  1.1      cgd 
     46  1.6      cgd #include <sys/types.h>
     47  1.1      cgd #include "shell.h"
     48  1.1      cgd #include "nodes.h"
     49  1.1      cgd #include "jobs.h"
     50  1.1      cgd #include "expand.h"
     51  1.1      cgd #include "redir.h"
     52  1.1      cgd #include "output.h"
     53  1.1      cgd #include "memalloc.h"
     54  1.1      cgd #include "error.h"
     55  1.1      cgd #include <signal.h>
     56  1.1      cgd #include <fcntl.h>
     57  1.1      cgd #include <errno.h>
     58  1.1      cgd 
     59  1.1      cgd 
     60  1.1      cgd #define EMPTY -2		/* marks an unused slot in redirtab */
     61  1.1      cgd #define PIPESIZE 4096		/* amount of buffering in a pipe */
     62  1.1      cgd 
     63  1.1      cgd 
     64  1.1      cgd MKINIT
     65  1.1      cgd struct redirtab {
     66  1.1      cgd 	struct redirtab *next;
     67  1.1      cgd 	short renamed[10];
     68  1.1      cgd };
     69  1.1      cgd 
     70  1.1      cgd 
     71  1.1      cgd MKINIT struct redirtab *redirlist;
     72  1.1      cgd 
     73  1.4      sef /* We keep track of whether or not fd0 has been redirected.  This is for
     74  1.4      sef    background commands, where we want to redirect fd0 to /dev/null only
     75  1.4      sef    if it hasn't already been redirected.  */
     76  1.4      sef int fd0_redirected = 0;
     77  1.1      cgd 
     78  1.1      cgd #ifdef __STDC__
     79  1.1      cgd STATIC void openredirect(union node *, char *);
     80  1.1      cgd STATIC int openhere(union node *);
     81  1.1      cgd #else
     82  1.1      cgd STATIC void openredirect();
     83  1.1      cgd STATIC int openhere();
     84  1.1      cgd #endif
     85  1.1      cgd 
     86  1.1      cgd 
     87  1.1      cgd 
     88  1.1      cgd /*
     89  1.1      cgd  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
     90  1.1      cgd  * old file descriptors are stashed away so that the redirection can be
     91  1.1      cgd  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
     92  1.1      cgd  * standard output, and the standard error if it becomes a duplicate of
     93  1.1      cgd  * stdout, is saved in memory.
     94  1.1      cgd  */
     95  1.1      cgd 
     96  1.1      cgd void
     97  1.1      cgd redirect(redir, flags)
     98  1.1      cgd 	union node *redir;
     99  1.1      cgd 	int flags;
    100  1.1      cgd 	{
    101  1.1      cgd 	union node *n;
    102  1.1      cgd 	struct redirtab *sv;
    103  1.1      cgd 	int i;
    104  1.1      cgd 	int fd;
    105  1.1      cgd 	char memory[10];		/* file descriptors to write to memory */
    106  1.1      cgd 
    107  1.1      cgd 	for (i = 10 ; --i >= 0 ; )
    108  1.1      cgd 		memory[i] = 0;
    109  1.1      cgd 	memory[1] = flags & REDIR_BACKQ;
    110  1.1      cgd 	if (flags & REDIR_PUSH) {
    111  1.1      cgd 		sv = ckmalloc(sizeof (struct redirtab));
    112  1.1      cgd 		for (i = 0 ; i < 10 ; i++)
    113  1.1      cgd 			sv->renamed[i] = EMPTY;
    114  1.1      cgd 		sv->next = redirlist;
    115  1.1      cgd 		redirlist = sv;
    116  1.1      cgd 	}
    117  1.1      cgd 	for (n = redir ; n ; n = n->nfile.next) {
    118  1.1      cgd 		fd = n->nfile.fd;
    119  1.1      cgd 		if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
    120  1.1      cgd 			INTOFF;
    121  1.1      cgd 			if ((i = copyfd(fd, 10)) != EMPTY) {
    122  1.1      cgd 				sv->renamed[fd] = i;
    123  1.1      cgd 				close(fd);
    124  1.1      cgd 			}
    125  1.1      cgd 			INTON;
    126  1.1      cgd 			if (i == EMPTY)
    127  1.1      cgd 				error("Out of file descriptors");
    128  1.1      cgd 		} else {
    129  1.1      cgd 			close(fd);
    130  1.1      cgd 		}
    131  1.4      sef 		if (fd == 0)
    132  1.4      sef 			fd0_redirected++;
    133  1.1      cgd 		openredirect(n, memory);
    134  1.1      cgd 	}
    135  1.1      cgd 	if (memory[1])
    136  1.1      cgd 		out1 = &memout;
    137  1.1      cgd 	if (memory[2])
    138  1.1      cgd 		out2 = &memout;
    139  1.1      cgd }
    140  1.1      cgd 
    141  1.1      cgd 
    142  1.1      cgd STATIC void
    143  1.1      cgd openredirect(redir, memory)
    144  1.1      cgd 	union node *redir;
    145  1.1      cgd 	char memory[10];
    146  1.1      cgd 	{
    147  1.1      cgd 	int fd = redir->nfile.fd;
    148  1.1      cgd 	char *fname;
    149  1.1      cgd 	int f;
    150  1.1      cgd 
    151  1.1      cgd 	/*
    152  1.1      cgd 	 * We suppress interrupts so that we won't leave open file
    153  1.1      cgd 	 * descriptors around.  This may not be such a good idea because
    154  1.1      cgd 	 * an open of a device or a fifo can block indefinitely.
    155  1.1      cgd 	 */
    156  1.1      cgd 	INTOFF;
    157  1.1      cgd 	memory[fd] = 0;
    158  1.1      cgd 	switch (redir->nfile.type) {
    159  1.1      cgd 	case NFROM:
    160  1.1      cgd 		fname = redir->nfile.expfname;
    161  1.1      cgd 		if ((f = open(fname, O_RDONLY)) < 0)
    162  1.1      cgd 			error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
    163  1.1      cgd movefd:
    164  1.1      cgd 		if (f != fd) {
    165  1.1      cgd 			copyfd(f, fd);
    166  1.1      cgd 			close(f);
    167  1.1      cgd 		}
    168  1.1      cgd 		break;
    169  1.1      cgd 	case NTO:
    170  1.1      cgd 		fname = redir->nfile.expfname;
    171  1.1      cgd #ifdef O_CREAT
    172  1.1      cgd 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
    173  1.1      cgd 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    174  1.1      cgd #else
    175  1.1      cgd 		if ((f = creat(fname, 0666)) < 0)
    176  1.1      cgd 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    177  1.1      cgd #endif
    178  1.1      cgd 		goto movefd;
    179  1.1      cgd 	case NAPPEND:
    180  1.1      cgd 		fname = redir->nfile.expfname;
    181  1.1      cgd #ifdef O_APPEND
    182  1.1      cgd 		if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
    183  1.1      cgd 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    184  1.1      cgd #else
    185  1.1      cgd 		if ((f = open(fname, O_WRONLY)) < 0
    186  1.1      cgd 		 && (f = creat(fname, 0666)) < 0)
    187  1.1      cgd 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    188  1.6      cgd 		lseek(f, 0, 2);
    189  1.1      cgd #endif
    190  1.1      cgd 		goto movefd;
    191  1.1      cgd 	case NTOFD:
    192  1.1      cgd 	case NFROMFD:
    193  1.1      cgd 		if (redir->ndup.dupfd >= 0) {	/* if not ">&-" */
    194  1.1      cgd 			if (memory[redir->ndup.dupfd])
    195  1.1      cgd 				memory[fd] = 1;
    196  1.1      cgd 			else
    197  1.1      cgd 				copyfd(redir->ndup.dupfd, fd);
    198  1.1      cgd 		}
    199  1.1      cgd 		break;
    200  1.1      cgd 	case NHERE:
    201  1.1      cgd 	case NXHERE:
    202  1.1      cgd 		f = openhere(redir);
    203  1.1      cgd 		goto movefd;
    204  1.1      cgd 	default:
    205  1.1      cgd 		abort();
    206  1.1      cgd 	}
    207  1.1      cgd 	INTON;
    208  1.1      cgd }
    209  1.1      cgd 
    210  1.1      cgd 
    211  1.1      cgd /*
    212  1.1      cgd  * Handle here documents.  Normally we fork off a process to write the
    213  1.1      cgd  * data to a pipe.  If the document is short, we can stuff the data in
    214  1.1      cgd  * the pipe without forking.
    215  1.1      cgd  */
    216  1.1      cgd 
    217  1.1      cgd STATIC int
    218  1.1      cgd openhere(redir)
    219  1.1      cgd 	union node *redir;
    220  1.1      cgd 	{
    221  1.1      cgd 	int pip[2];
    222  1.1      cgd 	int len;
    223  1.1      cgd 
    224  1.1      cgd 	if (pipe(pip) < 0)
    225  1.1      cgd 		error("Pipe call failed");
    226  1.1      cgd 	if (redir->type == NHERE) {
    227  1.1      cgd 		len = strlen(redir->nhere.doc->narg.text);
    228  1.1      cgd 		if (len <= PIPESIZE) {
    229  1.1      cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    230  1.1      cgd 			goto out;
    231  1.1      cgd 		}
    232  1.1      cgd 	}
    233  1.1      cgd 	if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
    234  1.1      cgd 		close(pip[0]);
    235  1.1      cgd 		signal(SIGINT, SIG_IGN);
    236  1.1      cgd 		signal(SIGQUIT, SIG_IGN);
    237  1.1      cgd 		signal(SIGHUP, SIG_IGN);
    238  1.1      cgd #ifdef SIGTSTP
    239  1.1      cgd 		signal(SIGTSTP, SIG_IGN);
    240  1.1      cgd #endif
    241  1.1      cgd 		signal(SIGPIPE, SIG_DFL);
    242  1.1      cgd 		if (redir->type == NHERE)
    243  1.1      cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    244  1.1      cgd 		else
    245  1.1      cgd 			expandhere(redir->nhere.doc, pip[1]);
    246  1.1      cgd 		_exit(0);
    247  1.1      cgd 	}
    248  1.1      cgd out:
    249  1.1      cgd 	close(pip[1]);
    250  1.1      cgd 	return pip[0];
    251  1.1      cgd }
    252  1.1      cgd 
    253  1.1      cgd 
    254  1.1      cgd 
    255  1.1      cgd /*
    256  1.1      cgd  * Undo the effects of the last redirection.
    257  1.1      cgd  */
    258  1.1      cgd 
    259  1.1      cgd void
    260  1.1      cgd popredir() {
    261  1.1      cgd 	register struct redirtab *rp = redirlist;
    262  1.1      cgd 	int i;
    263  1.1      cgd 
    264  1.1      cgd 	for (i = 0 ; i < 10 ; i++) {
    265  1.1      cgd 		if (rp->renamed[i] != EMPTY) {
    266  1.4      sef 			if (i == 0)
    267  1.4      sef 				fd0_redirected--;
    268  1.1      cgd 			close(i);
    269  1.1      cgd 			if (rp->renamed[i] >= 0) {
    270  1.1      cgd 				copyfd(rp->renamed[i], i);
    271  1.1      cgd 				close(rp->renamed[i]);
    272  1.1      cgd 			}
    273  1.1      cgd 		}
    274  1.1      cgd 	}
    275  1.1      cgd 	INTOFF;
    276  1.1      cgd 	redirlist = rp->next;
    277  1.1      cgd 	ckfree(rp);
    278  1.1      cgd 	INTON;
    279  1.1      cgd }
    280  1.1      cgd 
    281  1.1      cgd 
    282  1.1      cgd 
    283  1.1      cgd /*
    284  1.1      cgd  * Undo all redirections.  Called on error or interrupt.
    285  1.1      cgd  */
    286  1.1      cgd 
    287  1.1      cgd #ifdef mkinit
    288  1.1      cgd 
    289  1.1      cgd INCLUDE "redir.h"
    290  1.1      cgd 
    291  1.1      cgd RESET {
    292  1.1      cgd 	while (redirlist)
    293  1.1      cgd 		popredir();
    294  1.1      cgd }
    295  1.1      cgd 
    296  1.1      cgd SHELLPROC {
    297  1.1      cgd 	clearredir();
    298  1.1      cgd }
    299  1.1      cgd 
    300  1.1      cgd #endif
    301  1.1      cgd 
    302  1.1      cgd 
    303  1.1      cgd /*
    304  1.1      cgd  * Discard all saved file descriptors.
    305  1.1      cgd  */
    306  1.1      cgd 
    307  1.1      cgd void
    308  1.1      cgd clearredir() {
    309  1.1      cgd 	register struct redirtab *rp;
    310  1.1      cgd 	int i;
    311  1.1      cgd 
    312  1.1      cgd 	for (rp = redirlist ; rp ; rp = rp->next) {
    313  1.1      cgd 		for (i = 0 ; i < 10 ; i++) {
    314  1.1      cgd 			if (rp->renamed[i] >= 0) {
    315  1.1      cgd 				close(rp->renamed[i]);
    316  1.1      cgd 			}
    317  1.1      cgd 			rp->renamed[i] = EMPTY;
    318  1.1      cgd 		}
    319  1.1      cgd 	}
    320  1.1      cgd }
    321  1.1      cgd 
    322  1.1      cgd 
    323  1.1      cgd 
    324  1.1      cgd /*
    325  1.1      cgd  * Copy a file descriptor, like the F_DUPFD option of fcntl.  Returns -1
    326  1.1      cgd  * if the source file descriptor is closed, EMPTY if there are no unused
    327  1.1      cgd  * file descriptors left.
    328  1.1      cgd  */
    329  1.1      cgd 
    330  1.1      cgd int
    331  1.1      cgd copyfd(from, to) {
    332  1.1      cgd #ifdef F_DUPFD
    333  1.1      cgd 	int newfd;
    334  1.1      cgd 
    335  1.1      cgd 	newfd = fcntl(from, F_DUPFD, to);
    336  1.1      cgd 	if (newfd < 0 && errno == EMFILE)
    337  1.1      cgd 		return EMPTY;
    338  1.1      cgd 	return newfd;
    339  1.1      cgd #else
    340  1.1      cgd 	char toclose[32];
    341  1.1      cgd 	int i;
    342  1.1      cgd 	int newfd;
    343  1.1      cgd 	int e;
    344  1.1      cgd 
    345  1.1      cgd 	for (i = 0 ; i < to ; i++)
    346  1.1      cgd 		toclose[i] = 0;
    347  1.1      cgd 	INTOFF;
    348  1.1      cgd 	while ((newfd = dup(from)) >= 0 && newfd < to)
    349  1.1      cgd 		toclose[newfd] = 1;
    350  1.1      cgd 	e = errno;
    351  1.1      cgd 	for (i = 0 ; i < to ; i++) {
    352  1.1      cgd 		if (toclose[i])
    353  1.1      cgd 			close(i);
    354  1.1      cgd 	}
    355  1.1      cgd 	INTON;
    356  1.1      cgd 	if (newfd < 0 && e == EMFILE)
    357  1.1      cgd 		return EMPTY;
    358  1.1      cgd 	return newfd;
    359  1.1      cgd #endif
    360  1.4      sef }
    361  1.4      sef 
    362  1.4      sef /* Return true if fd 0 has already been redirected at least once.  */
    363  1.4      sef int
    364  1.4      sef fd0_redirected_p () {
    365  1.4      sef 	return fd0_redirected != 0;
    366  1.1      cgd }
    367