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