xref: /freebsd/bin/sh/redir.c (revision 7e1c72660f308aa7a5d708da6825a5eb6e182ed1)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)redir.c	8.2 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
414b88c807SRodney W. Grimes #endif /* not lint */
422749b141SDavid E. O'Brien #include <sys/cdefs.h>
432749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <sys/types.h>
461a958c66STim J. Robbins #include <sys/stat.h>
47aa9caaf6SPeter Wemm #include <signal.h>
48aa9caaf6SPeter Wemm #include <string.h>
49aa9caaf6SPeter Wemm #include <fcntl.h>
50aa9caaf6SPeter Wemm #include <errno.h>
51aa9caaf6SPeter Wemm #include <unistd.h>
52aa9caaf6SPeter Wemm #include <stdlib.h>
53aa9caaf6SPeter Wemm 
544b88c807SRodney W. Grimes /*
554b88c807SRodney W. Grimes  * Code for dealing with input/output redirection.
564b88c807SRodney W. Grimes  */
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes #include "shell.h"
594b88c807SRodney W. Grimes #include "nodes.h"
604b88c807SRodney W. Grimes #include "jobs.h"
614b88c807SRodney W. Grimes #include "expand.h"
624b88c807SRodney W. Grimes #include "redir.h"
634b88c807SRodney W. Grimes #include "output.h"
644b88c807SRodney W. Grimes #include "memalloc.h"
654b88c807SRodney W. Grimes #include "error.h"
661a958c66STim J. Robbins #include "options.h"
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes #define EMPTY -2		/* marks an unused slot in redirtab */
704b88c807SRodney W. Grimes #define PIPESIZE 4096		/* amount of buffering in a pipe */
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes MKINIT
744b88c807SRodney W. Grimes struct redirtab {
754b88c807SRodney W. Grimes 	struct redirtab *next;
765b99fa05STim J. Robbins 	int renamed[10];
774b88c807SRodney W. Grimes };
784b88c807SRodney W. Grimes 
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes MKINIT struct redirtab *redirlist;
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes /*
834b88c807SRodney W. Grimes  * We keep track of whether or not fd0 has been redirected.  This is for
844b88c807SRodney W. Grimes  * background commands, where we want to redirect fd0 to /dev/null only
854b88c807SRodney W. Grimes  * if it hasn't already been redirected.
864b88c807SRodney W. Grimes */
872ba1b30bSDiomidis Spinellis STATIC int fd0_redirected = 0;
884b88c807SRodney W. Grimes 
895134c3f7SWarner Losh STATIC void openredirect(union node *, char[10 ]);
905134c3f7SWarner Losh STATIC int openhere(union node *);
914b88c807SRodney W. Grimes 
924b88c807SRodney W. Grimes 
934b88c807SRodney W. Grimes /*
944b88c807SRodney W. Grimes  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
954b88c807SRodney W. Grimes  * old file descriptors are stashed away so that the redirection can be
964b88c807SRodney W. Grimes  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
974b88c807SRodney W. Grimes  * standard output, and the standard error if it becomes a duplicate of
984b88c807SRodney W. Grimes  * stdout, is saved in memory.
994b88c807SRodney W. Grimes  */
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes void
1025134c3f7SWarner Losh redirect(union node *redir, int flags)
1034b88c807SRodney W. Grimes {
1044b88c807SRodney W. Grimes 	union node *n;
105aa9caaf6SPeter Wemm 	struct redirtab *sv = NULL;
1064b88c807SRodney W. Grimes 	int i;
1074b88c807SRodney W. Grimes 	int fd;
10879f56947SSteve Price 	int try;
1094b88c807SRodney W. Grimes 	char memory[10];	/* file descriptors to write to memory */
1104b88c807SRodney W. Grimes 
1114b88c807SRodney W. Grimes 	for (i = 10 ; --i >= 0 ; )
1124b88c807SRodney W. Grimes 		memory[i] = 0;
1134b88c807SRodney W. Grimes 	memory[1] = flags & REDIR_BACKQ;
1144b88c807SRodney W. Grimes 	if (flags & REDIR_PUSH) {
1154b88c807SRodney W. Grimes 		sv = ckmalloc(sizeof (struct redirtab));
1164b88c807SRodney W. Grimes 		for (i = 0 ; i < 10 ; i++)
1174b88c807SRodney W. Grimes 			sv->renamed[i] = EMPTY;
1184b88c807SRodney W. Grimes 		sv->next = redirlist;
1194b88c807SRodney W. Grimes 		redirlist = sv;
1204b88c807SRodney W. Grimes 	}
1214b88c807SRodney W. Grimes 	for (n = redir ; n ; n = n->nfile.next) {
1224b88c807SRodney W. Grimes 		fd = n->nfile.fd;
12379f56947SSteve Price 		try = 0;
124769bbc65SJoerg Wunsch 		if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
125769bbc65SJoerg Wunsch 		    n->ndup.dupfd == fd)
126ab0a2172SSteve Price 			continue; /* redirect from/to same file descriptor */
12779f56947SSteve Price 
1284b88c807SRodney W. Grimes 		if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
1294b88c807SRodney W. Grimes 			INTOFF;
13079f56947SSteve Price again:
13179f56947SSteve Price 			if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
13279f56947SSteve Price 				switch (errno) {
13379f56947SSteve Price 				case EBADF:
13479f56947SSteve Price 					if (!try) {
13579f56947SSteve Price 						openredirect(n, memory);
13679f56947SSteve Price 						try++;
13779f56947SSteve Price 						goto again;
13879f56947SSteve Price 					}
13979f56947SSteve Price 					/* FALLTHROUGH*/
14079f56947SSteve Price 				default:
14179f56947SSteve Price 					INTON;
14279f56947SSteve Price 					error("%d: %s", fd, strerror(errno));
14379f56947SSteve Price 					break;
14479f56947SSteve Price 				}
14579f56947SSteve Price 			}
14679f56947SSteve Price 			if (!try) {
1474b88c807SRodney W. Grimes 				sv->renamed[fd] = i;
1484b88c807SRodney W. Grimes 			}
1494b88c807SRodney W. Grimes 			INTON;
1504b88c807SRodney W. Grimes 		}
1514b88c807SRodney W. Grimes 		if (fd == 0)
1524b88c807SRodney W. Grimes 			fd0_redirected++;
15379f56947SSteve Price 		if (!try)
1544b88c807SRodney W. Grimes 			openredirect(n, memory);
1554b88c807SRodney W. Grimes 	}
1564b88c807SRodney W. Grimes 	if (memory[1])
1574b88c807SRodney W. Grimes 		out1 = &memout;
1584b88c807SRodney W. Grimes 	if (memory[2])
1594b88c807SRodney W. Grimes 		out2 = &memout;
1604b88c807SRodney W. Grimes }
1614b88c807SRodney W. Grimes 
1624b88c807SRodney W. Grimes 
1634b88c807SRodney W. Grimes STATIC void
1645134c3f7SWarner Losh openredirect(union node *redir, char memory[10])
1654b88c807SRodney W. Grimes {
1661a958c66STim J. Robbins 	struct stat sb;
1674b88c807SRodney W. Grimes 	int fd = redir->nfile.fd;
1684b88c807SRodney W. Grimes 	char *fname;
1694b88c807SRodney W. Grimes 	int f;
1704b88c807SRodney W. Grimes 
1714b88c807SRodney W. Grimes 	/*
1724b88c807SRodney W. Grimes 	 * We suppress interrupts so that we won't leave open file
1734b88c807SRodney W. Grimes 	 * descriptors around.  This may not be such a good idea because
1744b88c807SRodney W. Grimes 	 * an open of a device or a fifo can block indefinitely.
1754b88c807SRodney W. Grimes 	 */
1764b88c807SRodney W. Grimes 	INTOFF;
1774b88c807SRodney W. Grimes 	memory[fd] = 0;
1784b88c807SRodney W. Grimes 	switch (redir->nfile.type) {
1794b88c807SRodney W. Grimes 	case NFROM:
1804b88c807SRodney W. Grimes 		fname = redir->nfile.expfname;
1814b88c807SRodney W. Grimes 		if ((f = open(fname, O_RDONLY)) < 0)
1821c59560dSTim J. Robbins 			error("cannot open %s: %s", fname, strerror(errno));
1834b88c807SRodney W. Grimes movefd:
1844b88c807SRodney W. Grimes 		if (f != fd) {
1857e1c7266SDag-Erling Smørgrav 			dup2(f, fd);
1864b88c807SRodney W. Grimes 			close(f);
1874b88c807SRodney W. Grimes 		}
1884b88c807SRodney W. Grimes 		break;
1894682f420SBrian Somers 	case NFROMTO:
1904682f420SBrian Somers 		fname = redir->nfile.expfname;
1914682f420SBrian Somers 		if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
1921c59560dSTim J. Robbins 			error("cannot create %s: %s", fname, strerror(errno));
1934682f420SBrian Somers 		goto movefd;
1944b88c807SRodney W. Grimes 	case NTO:
1954b88c807SRodney W. Grimes 		fname = redir->nfile.expfname;
1961a958c66STim J. Robbins 		if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode))
1971a958c66STim J. Robbins 			error("cannot create %s: %s", fname,
1981c59560dSTim J. Robbins 			    strerror(EEXIST));
1994b88c807SRodney W. Grimes 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
2001c59560dSTim J. Robbins 			error("cannot create %s: %s", fname, strerror(errno));
2014b88c807SRodney W. Grimes 		goto movefd;
2021a958c66STim J. Robbins 	case NCLOBBER:
2031a958c66STim J. Robbins 		fname = redir->nfile.expfname;
2041a958c66STim J. Robbins 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
2051c59560dSTim J. Robbins 			error("cannot create %s: %s", fname, strerror(errno));
2061a958c66STim J. Robbins 		goto movefd;
2074b88c807SRodney W. Grimes 	case NAPPEND:
2084b88c807SRodney W. Grimes 		fname = redir->nfile.expfname;
2094b88c807SRodney W. Grimes 		if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
2101c59560dSTim J. Robbins 			error("cannot create %s: %s", fname, strerror(errno));
2114b88c807SRodney W. Grimes 		goto movefd;
2124b88c807SRodney W. Grimes 	case NTOFD:
2134b88c807SRodney W. Grimes 	case NFROMFD:
2144b88c807SRodney W. Grimes 		if (redir->ndup.dupfd >= 0) {	/* if not ">&-" */
2154b88c807SRodney W. Grimes 			if (memory[redir->ndup.dupfd])
2164b88c807SRodney W. Grimes 				memory[fd] = 1;
2177e1c7266SDag-Erling Smørgrav 			else
2187e1c7266SDag-Erling Smørgrav 				dup2(redir->ndup.dupfd, fd);
2197e1c7266SDag-Erling Smørgrav 		} else {
22063f901efSSergey Babkin 			close(fd);
2214b88c807SRodney W. Grimes 		}
2224b88c807SRodney W. Grimes 		break;
2234b88c807SRodney W. Grimes 	case NHERE:
2244b88c807SRodney W. Grimes 	case NXHERE:
2254b88c807SRodney W. Grimes 		f = openhere(redir);
2264b88c807SRodney W. Grimes 		goto movefd;
2274b88c807SRodney W. Grimes 	default:
2284b88c807SRodney W. Grimes 		abort();
2294b88c807SRodney W. Grimes 	}
2304b88c807SRodney W. Grimes 	INTON;
2314b88c807SRodney W. Grimes }
2324b88c807SRodney W. Grimes 
2334b88c807SRodney W. Grimes 
2344b88c807SRodney W. Grimes /*
2354b88c807SRodney W. Grimes  * Handle here documents.  Normally we fork off a process to write the
2364b88c807SRodney W. Grimes  * data to a pipe.  If the document is short, we can stuff the data in
2374b88c807SRodney W. Grimes  * the pipe without forking.
2384b88c807SRodney W. Grimes  */
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes STATIC int
2415134c3f7SWarner Losh openhere(union node *redir)
2424b88c807SRodney W. Grimes {
2434b88c807SRodney W. Grimes 	int pip[2];
244aa9caaf6SPeter Wemm 	int len = 0;
2454b88c807SRodney W. Grimes 
2464b88c807SRodney W. Grimes 	if (pipe(pip) < 0)
2476c48b6cfSMartin Cracauer 		error("Pipe call failed: %s", strerror(errno));
2484b88c807SRodney W. Grimes 	if (redir->type == NHERE) {
2494b88c807SRodney W. Grimes 		len = strlen(redir->nhere.doc->narg.text);
2504b88c807SRodney W. Grimes 		if (len <= PIPESIZE) {
2514b88c807SRodney W. Grimes 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
2524b88c807SRodney W. Grimes 			goto out;
2534b88c807SRodney W. Grimes 		}
2544b88c807SRodney W. Grimes 	}
2554b88c807SRodney W. Grimes 	if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
2564b88c807SRodney W. Grimes 		close(pip[0]);
2574b88c807SRodney W. Grimes 		signal(SIGINT, SIG_IGN);
2584b88c807SRodney W. Grimes 		signal(SIGQUIT, SIG_IGN);
2594b88c807SRodney W. Grimes 		signal(SIGHUP, SIG_IGN);
2604b88c807SRodney W. Grimes 		signal(SIGTSTP, SIG_IGN);
2614b88c807SRodney W. Grimes 		signal(SIGPIPE, SIG_DFL);
2624b88c807SRodney W. Grimes 		if (redir->type == NHERE)
2634b88c807SRodney W. Grimes 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
2644b88c807SRodney W. Grimes 		else
2654b88c807SRodney W. Grimes 			expandhere(redir->nhere.doc, pip[1]);
2664b88c807SRodney W. Grimes 		_exit(0);
2674b88c807SRodney W. Grimes 	}
2684b88c807SRodney W. Grimes out:
2694b88c807SRodney W. Grimes 	close(pip[1]);
2704b88c807SRodney W. Grimes 	return pip[0];
2714b88c807SRodney W. Grimes }
2724b88c807SRodney W. Grimes 
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes /*
2764b88c807SRodney W. Grimes  * Undo the effects of the last redirection.
2774b88c807SRodney W. Grimes  */
2784b88c807SRodney W. Grimes 
2794b88c807SRodney W. Grimes void
2805134c3f7SWarner Losh popredir(void)
2815134c3f7SWarner Losh {
28279f56947SSteve Price 	struct redirtab *rp = redirlist;
2834b88c807SRodney W. Grimes 	int i;
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes 	for (i = 0 ; i < 10 ; i++) {
2864b88c807SRodney W. Grimes 		if (rp->renamed[i] != EMPTY) {
2874b88c807SRodney W. Grimes                         if (i == 0)
2884b88c807SRodney W. Grimes                                 fd0_redirected--;
2894b88c807SRodney W. Grimes 			if (rp->renamed[i] >= 0) {
2907e1c7266SDag-Erling Smørgrav 				dup2(rp->renamed[i], i);
2914b88c807SRodney W. Grimes 				close(rp->renamed[i]);
2927e1c7266SDag-Erling Smørgrav 			} else {
2937e1c7266SDag-Erling Smørgrav 				close(i);
2944b88c807SRodney W. Grimes 			}
2954b88c807SRodney W. Grimes 		}
2964b88c807SRodney W. Grimes 	}
2974b88c807SRodney W. Grimes 	INTOFF;
2984b88c807SRodney W. Grimes 	redirlist = rp->next;
2994b88c807SRodney W. Grimes 	ckfree(rp);
3004b88c807SRodney W. Grimes 	INTON;
3014b88c807SRodney W. Grimes }
3024b88c807SRodney W. Grimes 
3034b88c807SRodney W. Grimes /*
3044b88c807SRodney W. Grimes  * Undo all redirections.  Called on error or interrupt.
3054b88c807SRodney W. Grimes  */
3064b88c807SRodney W. Grimes 
3074b88c807SRodney W. Grimes #ifdef mkinit
3084b88c807SRodney W. Grimes 
3094b88c807SRodney W. Grimes INCLUDE "redir.h"
3104b88c807SRodney W. Grimes 
3114b88c807SRodney W. Grimes RESET {
3124b88c807SRodney W. Grimes 	while (redirlist)
3134b88c807SRodney W. Grimes 		popredir();
3144b88c807SRodney W. Grimes }
3154b88c807SRodney W. Grimes 
3164b88c807SRodney W. Grimes SHELLPROC {
3174b88c807SRodney W. Grimes 	clearredir();
3184b88c807SRodney W. Grimes }
3194b88c807SRodney W. Grimes 
3204b88c807SRodney W. Grimes #endif
3214b88c807SRodney W. Grimes 
3224b88c807SRodney W. Grimes /* Return true if fd 0 has already been redirected at least once.  */
3234b88c807SRodney W. Grimes int
3245134c3f7SWarner Losh fd0_redirected_p(void)
3255134c3f7SWarner Losh {
3264b88c807SRodney W. Grimes         return fd0_redirected != 0;
3274b88c807SRodney W. Grimes }
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes /*
3304b88c807SRodney W. Grimes  * Discard all saved file descriptors.
3314b88c807SRodney W. Grimes  */
3324b88c807SRodney W. Grimes 
3334b88c807SRodney W. Grimes void
3345134c3f7SWarner Losh clearredir(void)
3355134c3f7SWarner Losh {
33679f56947SSteve Price 	struct redirtab *rp;
3374b88c807SRodney W. Grimes 	int i;
3384b88c807SRodney W. Grimes 
3394b88c807SRodney W. Grimes 	for (rp = redirlist ; rp ; rp = rp->next) {
3404b88c807SRodney W. Grimes 		for (i = 0 ; i < 10 ; i++) {
3414b88c807SRodney W. Grimes 			if (rp->renamed[i] >= 0) {
3424b88c807SRodney W. Grimes 				close(rp->renamed[i]);
3434b88c807SRodney W. Grimes 			}
3444b88c807SRodney W. Grimes 			rp->renamed[i] = EMPTY;
3454b88c807SRodney W. Grimes 		}
3464b88c807SRodney W. Grimes 	}
3474b88c807SRodney W. Grimes }
348