Home | History | Annotate | Line # | Download | only in sh
redir.c revision 1.53.2.1
      1  1.53.2.1  pgoyette /*	$NetBSD: redir.c,v 1.53.2.1 2017/05/02 03:19:14 pgoyette 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.53.2.1  pgoyette __RCSID("$NetBSD: redir.c,v 1.53.2.1 2017/05/02 03:19:14 pgoyette 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.48  christos #include <sys/stat.h>
     47      1.12  christos #include <signal.h>
     48      1.12  christos #include <string.h>
     49      1.12  christos #include <fcntl.h>
     50      1.12  christos #include <errno.h>
     51      1.12  christos #include <unistd.h>
     52      1.12  christos #include <stdlib.h>
     53      1.12  christos 
     54       1.1       cgd /*
     55       1.1       cgd  * Code for dealing with input/output redirection.
     56       1.1       cgd  */
     57       1.1       cgd 
     58      1.25  christos #include "main.h"
     59      1.50  christos #include "builtins.h"
     60       1.1       cgd #include "shell.h"
     61       1.1       cgd #include "nodes.h"
     62       1.1       cgd #include "jobs.h"
     63      1.23  christos #include "options.h"
     64       1.1       cgd #include "expand.h"
     65       1.1       cgd #include "redir.h"
     66       1.1       cgd #include "output.h"
     67       1.1       cgd #include "memalloc.h"
     68      1.51       kre #include "mystring.h"
     69       1.1       cgd #include "error.h"
     70       1.1       cgd 
     71       1.1       cgd 
     72       1.1       cgd #define EMPTY -2		/* marks an unused slot in redirtab */
     73      1.44       kre #define CLOSED -1		/* fd was not open before redir */
     74      1.17  christos #ifndef PIPE_BUF
     75      1.17  christos # define PIPESIZE 4096		/* amount of buffering in a pipe */
     76      1.17  christos #else
     77      1.17  christos # define PIPESIZE PIPE_BUF
     78      1.17  christos #endif
     79       1.1       cgd 
     80       1.1       cgd 
     81       1.1       cgd MKINIT
     82      1.43  christos struct renamelist {
     83      1.43  christos 	struct renamelist *next;
     84      1.43  christos 	int orig;
     85      1.43  christos 	int into;
     86      1.43  christos };
     87      1.43  christos 
     88      1.43  christos MKINIT
     89       1.1       cgd struct redirtab {
     90       1.1       cgd 	struct redirtab *next;
     91      1.43  christos 	struct renamelist *renamed;
     92       1.1       cgd };
     93       1.1       cgd 
     94       1.1       cgd 
     95       1.1       cgd MKINIT struct redirtab *redirlist;
     96       1.1       cgd 
     97      1.13  christos /*
     98       1.7       jtc  * We keep track of whether or not fd0 has been redirected.  This is for
     99       1.7       jtc  * background commands, where we want to redirect fd0 to /dev/null only
    100      1.13  christos  * if it hasn't already been redirected.
    101      1.43  christos  */
    102      1.43  christos STATIC int fd0_redirected = 0;
    103       1.1       cgd 
    104      1.43  christos /*
    105      1.43  christos  * And also where to put internal use fds that should be out of the
    106      1.43  christos  * way of user defined fds (normally)
    107      1.43  christos  */
    108      1.43  christos STATIC int big_sh_fd = 0;
    109      1.43  christos 
    110      1.43  christos STATIC const struct renamelist *is_renamed(const struct renamelist *, int);
    111      1.43  christos STATIC void fd_rename(struct redirtab *, int, int);
    112      1.43  christos STATIC void free_rl(struct redirtab *, int);
    113      1.29  christos STATIC void openredirect(union node *, char[10], int);
    114      1.34      yamt STATIC int openhere(const union node *);
    115      1.47       kre STATIC int copyfd(int, int, int);
    116      1.43  christos STATIC void find_big_fd(void);
    117      1.43  christos 
    118  1.53.2.1  pgoyette 
    119  1.53.2.1  pgoyette struct shell_fds {		/* keep track of internal shell fds */
    120  1.53.2.1  pgoyette 	struct shell_fds *nxt;
    121  1.53.2.1  pgoyette 	void (*cb)(int, int);
    122  1.53.2.1  pgoyette 	int fd;
    123  1.53.2.1  pgoyette };
    124  1.53.2.1  pgoyette 
    125  1.53.2.1  pgoyette STATIC struct shell_fds *sh_fd_list;
    126  1.53.2.1  pgoyette 
    127  1.53.2.1  pgoyette STATIC void renumber_sh_fd(struct shell_fds *);
    128  1.53.2.1  pgoyette STATIC struct shell_fds *sh_fd(int);
    129  1.53.2.1  pgoyette 
    130      1.43  christos STATIC const struct renamelist *
    131      1.43  christos is_renamed(const struct renamelist *rl, int fd)
    132      1.43  christos {
    133      1.43  christos 	while (rl != NULL) {
    134      1.43  christos 		if (rl->orig == fd)
    135      1.43  christos 			return rl;
    136      1.43  christos 		rl = rl->next;
    137      1.43  christos 	}
    138      1.43  christos 	return NULL;
    139      1.43  christos }
    140      1.43  christos 
    141      1.43  christos STATIC void
    142      1.43  christos free_rl(struct redirtab *rt, int reset)
    143      1.43  christos {
    144      1.43  christos 	struct renamelist *rl, *rn = rt->renamed;
    145      1.43  christos 
    146      1.43  christos 	while ((rl = rn) != NULL) {
    147      1.43  christos 		rn = rl->next;
    148      1.43  christos 		if (rl->orig == 0)
    149      1.43  christos 			fd0_redirected--;
    150      1.43  christos 		if (reset) {
    151      1.43  christos 			if (rl->into < 0)
    152      1.43  christos 				close(rl->orig);
    153      1.43  christos 			else
    154      1.43  christos 				movefd(rl->into, rl->orig);
    155      1.43  christos 		}
    156      1.43  christos 		ckfree(rl);
    157      1.43  christos 	}
    158      1.43  christos 	rt->renamed = NULL;
    159      1.43  christos }
    160       1.1       cgd 
    161      1.43  christos STATIC void
    162      1.43  christos fd_rename(struct redirtab *rt, int from, int to)
    163      1.43  christos {
    164      1.43  christos 	struct renamelist *rl = ckmalloc(sizeof(struct renamelist));
    165      1.43  christos 
    166      1.43  christos 	rl->next = rt->renamed;
    167      1.43  christos 	rt->renamed = rl;
    168      1.43  christos 
    169      1.43  christos 	rl->orig = from;
    170      1.43  christos 	rl->into = to;
    171      1.43  christos }
    172       1.1       cgd 
    173       1.1       cgd /*
    174       1.1       cgd  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
    175       1.1       cgd  * old file descriptors are stashed away so that the redirection can be
    176       1.1       cgd  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
    177       1.1       cgd  * standard output, and the standard error if it becomes a duplicate of
    178       1.1       cgd  * stdout, is saved in memory.
    179       1.1       cgd  */
    180       1.1       cgd 
    181       1.1       cgd void
    182      1.27  christos redirect(union node *redir, int flags)
    183      1.27  christos {
    184       1.1       cgd 	union node *n;
    185      1.16  christos 	struct redirtab *sv = NULL;
    186       1.1       cgd 	int i;
    187       1.1       cgd 	int fd;
    188      1.13  christos 	char memory[10];	/* file descriptors to write to memory */
    189       1.1       cgd 
    190       1.1       cgd 	for (i = 10 ; --i >= 0 ; )
    191       1.1       cgd 		memory[i] = 0;
    192       1.1       cgd 	memory[1] = flags & REDIR_BACKQ;
    193       1.1       cgd 	if (flags & REDIR_PUSH) {
    194      1.24  christos 		/* We don't have to worry about REDIR_VFORK here, as
    195      1.24  christos 		 * flags & REDIR_PUSH is never true if REDIR_VFORK is set.
    196      1.24  christos 		 */
    197       1.1       cgd 		sv = ckmalloc(sizeof (struct redirtab));
    198      1.43  christos 		sv->renamed = NULL;
    199       1.1       cgd 		sv->next = redirlist;
    200       1.1       cgd 		redirlist = sv;
    201       1.1       cgd 	}
    202       1.1       cgd 	for (n = redir ; n ; n = n->nfile.next) {
    203       1.1       cgd 		fd = n->nfile.fd;
    204      1.53       kre 		if (fd > max_user_fd)
    205      1.53       kre 			max_user_fd = fd;
    206  1.53.2.1  pgoyette 		renumber_sh_fd(sh_fd(fd));
    207      1.13  christos 		if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
    208      1.43  christos 		    n->ndup.dupfd == fd) {
    209      1.43  christos 			/* redirect from/to same file descriptor */
    210      1.47       kre 			/* make sure it stays open */
    211      1.47       kre 			if (fcntl(fd, F_SETFD, 0) < 0)
    212      1.47       kre 				error("fd %d: %s", fd, strerror(errno));
    213      1.43  christos 			continue;
    214      1.43  christos 		}
    215      1.15  christos 
    216      1.43  christos 		if ((flags & REDIR_PUSH) && !is_renamed(sv->renamed, fd)) {
    217       1.1       cgd 			INTOFF;
    218      1.43  christos 			if (big_sh_fd < 10)
    219      1.43  christos 				find_big_fd();
    220      1.43  christos 			if ((i = fcntl(fd, F_DUPFD, big_sh_fd)) == -1) {
    221      1.15  christos 				switch (errno) {
    222      1.15  christos 				case EBADF:
    223      1.35      yamt 					i = CLOSED;
    224      1.35      yamt 					break;
    225      1.43  christos 				case EMFILE:
    226      1.43  christos 				case EINVAL:
    227      1.43  christos 					find_big_fd();
    228      1.43  christos 					i = fcntl(fd, F_DUPFD, big_sh_fd);
    229      1.43  christos 					if (i >= 0)
    230      1.43  christos 						break;
    231      1.43  christos 					/* FALLTHRU */
    232      1.15  christos 				default:
    233      1.43  christos 					i = errno;
    234      1.43  christos 					INTON;    /* XXX not needed here ? */
    235      1.43  christos 					error("%d: %s", fd, strerror(i));
    236      1.19   mycroft 					/* NOTREACHED */
    237      1.15  christos 				}
    238      1.43  christos 			}
    239      1.43  christos 			if (i >= 0)
    240      1.35      yamt 				(void)fcntl(i, F_SETFD, FD_CLOEXEC);
    241      1.43  christos 			fd_rename(sv, fd, i);
    242       1.1       cgd 			INTON;
    243       1.1       cgd 		}
    244      1.44       kre 		if (fd == 0)
    245      1.44       kre 			fd0_redirected++;
    246      1.35      yamt 		openredirect(n, memory, flags);
    247       1.1       cgd 	}
    248       1.1       cgd 	if (memory[1])
    249       1.1       cgd 		out1 = &memout;
    250       1.1       cgd 	if (memory[2])
    251       1.1       cgd 		out2 = &memout;
    252       1.1       cgd }
    253       1.1       cgd 
    254       1.1       cgd 
    255       1.1       cgd STATIC void
    256      1.29  christos openredirect(union node *redir, char memory[10], int flags)
    257      1.27  christos {
    258      1.36  christos 	struct stat sb;
    259       1.1       cgd 	int fd = redir->nfile.fd;
    260       1.1       cgd 	char *fname;
    261       1.1       cgd 	int f;
    262      1.42  christos 	int eflags, cloexec;
    263       1.1       cgd 
    264       1.1       cgd 	/*
    265       1.1       cgd 	 * We suppress interrupts so that we won't leave open file
    266       1.1       cgd 	 * descriptors around.  This may not be such a good idea because
    267       1.1       cgd 	 * an open of a device or a fifo can block indefinitely.
    268       1.1       cgd 	 */
    269       1.1       cgd 	INTOFF;
    270      1.43  christos 	if (fd < 10)
    271      1.43  christos 		memory[fd] = 0;
    272       1.1       cgd 	switch (redir->nfile.type) {
    273       1.1       cgd 	case NFROM:
    274       1.1       cgd 		fname = redir->nfile.expfname;
    275      1.29  christos 		if (flags & REDIR_VFORK)
    276      1.29  christos 			eflags = O_NONBLOCK;
    277      1.29  christos 		else
    278      1.29  christos 			eflags = 0;
    279      1.29  christos 		if ((f = open(fname, O_RDONLY|eflags)) < 0)
    280      1.20  christos 			goto eopen;
    281      1.29  christos 		if (eflags)
    282      1.29  christos 			(void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);
    283      1.20  christos 		break;
    284      1.20  christos 	case NFROMTO:
    285      1.20  christos 		fname = redir->nfile.expfname;
    286      1.20  christos 		if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
    287      1.20  christos 			goto ecreate;
    288       1.1       cgd 		break;
    289       1.1       cgd 	case NTO:
    290      1.36  christos 		if (Cflag) {
    291      1.36  christos 			fname = redir->nfile.expfname;
    292      1.37  christos 			if ((f = open(fname, O_WRONLY)) == -1) {
    293      1.36  christos 				if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL,
    294      1.36  christos 				    0666)) < 0)
    295      1.36  christos 					goto ecreate;
    296      1.37  christos 			} else if (fstat(f, &sb) == -1) {
    297      1.37  christos 				int serrno = errno;
    298      1.37  christos 				close(f);
    299      1.37  christos 				errno = serrno;
    300      1.37  christos 				goto ecreate;
    301      1.37  christos 			} else if (S_ISREG(sb.st_mode)) {
    302      1.37  christos 				close(f);
    303      1.36  christos 				errno = EEXIST;
    304      1.36  christos 				goto ecreate;
    305      1.36  christos 			}
    306      1.36  christos 			break;
    307      1.36  christos 		}
    308      1.23  christos 		/* FALLTHROUGH */
    309      1.23  christos 	case NCLOBBER:
    310       1.1       cgd 		fname = redir->nfile.expfname;
    311      1.36  christos 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
    312      1.20  christos 			goto ecreate;
    313      1.20  christos 		break;
    314       1.1       cgd 	case NAPPEND:
    315       1.1       cgd 		fname = redir->nfile.expfname;
    316       1.1       cgd 		if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
    317      1.20  christos 			goto ecreate;
    318      1.20  christos 		break;
    319       1.1       cgd 	case NTOFD:
    320       1.1       cgd 	case NFROMFD:
    321       1.1       cgd 		if (redir->ndup.dupfd >= 0) {	/* if not ">&-" */
    322      1.43  christos 			if (fd < 10 && redir->ndup.dupfd < 10 &&
    323      1.43  christos 			    memory[redir->ndup.dupfd])
    324       1.1       cgd 				memory[fd] = 1;
    325      1.47       kre 			else if (copyfd(redir->ndup.dupfd, fd,
    326      1.49  christos 			    (flags & REDIR_KEEP) == 0) < 0)
    327      1.47       kre 				error("Redirect (from %d to %d) failed: %s",
    328      1.47       kre 				    redir->ndup.dupfd, fd, strerror(errno));
    329      1.45       kre 		} else
    330      1.47       kre 			(void) close(fd);
    331      1.20  christos 		INTON;
    332      1.20  christos 		return;
    333       1.1       cgd 	case NHERE:
    334       1.1       cgd 	case NXHERE:
    335       1.1       cgd 		f = openhere(redir);
    336      1.20  christos 		break;
    337       1.1       cgd 	default:
    338       1.1       cgd 		abort();
    339       1.1       cgd 	}
    340      1.20  christos 
    341      1.42  christos 	cloexec = fd > 2 && (flags & REDIR_KEEP) == 0;
    342      1.20  christos 	if (f != fd) {
    343      1.47       kre 		if (copyfd(f, fd, cloexec) < 0) {
    344      1.47       kre 			int e = errno;
    345      1.47       kre 
    346      1.47       kre 			close(f);
    347      1.47       kre 			error("redirect reassignment (fd %d) failed: %s", fd,
    348      1.47       kre 			    strerror(e));
    349      1.47       kre 		}
    350      1.20  christos 		close(f);
    351      1.42  christos 	} else if (cloexec)
    352      1.38  christos 		(void)fcntl(f, F_SETFD, FD_CLOEXEC);
    353      1.38  christos 
    354       1.1       cgd 	INTON;
    355      1.20  christos 	return;
    356      1.20  christos ecreate:
    357      1.30   msaitoh 	exerrno = 1;
    358      1.20  christos 	error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    359      1.20  christos eopen:
    360      1.30   msaitoh 	exerrno = 1;
    361      1.20  christos 	error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
    362       1.1       cgd }
    363       1.1       cgd 
    364       1.1       cgd 
    365       1.1       cgd /*
    366       1.1       cgd  * Handle here documents.  Normally we fork off a process to write the
    367       1.1       cgd  * data to a pipe.  If the document is short, we can stuff the data in
    368       1.1       cgd  * the pipe without forking.
    369       1.1       cgd  */
    370       1.1       cgd 
    371       1.1       cgd STATIC int
    372      1.34      yamt openhere(const union node *redir)
    373      1.27  christos {
    374       1.1       cgd 	int pip[2];
    375      1.12  christos 	int len = 0;
    376       1.1       cgd 
    377       1.1       cgd 	if (pipe(pip) < 0)
    378       1.1       cgd 		error("Pipe call failed");
    379       1.1       cgd 	if (redir->type == NHERE) {
    380       1.1       cgd 		len = strlen(redir->nhere.doc->narg.text);
    381       1.1       cgd 		if (len <= PIPESIZE) {
    382       1.1       cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    383       1.1       cgd 			goto out;
    384       1.1       cgd 		}
    385       1.1       cgd 	}
    386      1.32    plunky 	if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
    387       1.1       cgd 		close(pip[0]);
    388       1.1       cgd 		signal(SIGINT, SIG_IGN);
    389       1.1       cgd 		signal(SIGQUIT, SIG_IGN);
    390       1.1       cgd 		signal(SIGHUP, SIG_IGN);
    391       1.1       cgd #ifdef SIGTSTP
    392       1.1       cgd 		signal(SIGTSTP, SIG_IGN);
    393       1.1       cgd #endif
    394       1.1       cgd 		signal(SIGPIPE, SIG_DFL);
    395       1.1       cgd 		if (redir->type == NHERE)
    396       1.1       cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    397       1.1       cgd 		else
    398       1.1       cgd 			expandhere(redir->nhere.doc, pip[1]);
    399       1.1       cgd 		_exit(0);
    400       1.1       cgd 	}
    401       1.1       cgd out:
    402       1.1       cgd 	close(pip[1]);
    403       1.1       cgd 	return pip[0];
    404       1.1       cgd }
    405       1.1       cgd 
    406       1.1       cgd 
    407       1.1       cgd 
    408       1.1       cgd /*
    409       1.1       cgd  * Undo the effects of the last redirection.
    410       1.1       cgd  */
    411       1.1       cgd 
    412       1.1       cgd void
    413      1.27  christos popredir(void)
    414      1.27  christos {
    415      1.14       tls 	struct redirtab *rp = redirlist;
    416       1.1       cgd 
    417       1.1       cgd 	INTOFF;
    418      1.43  christos 	free_rl(rp, 1);
    419       1.1       cgd 	redirlist = rp->next;
    420       1.1       cgd 	ckfree(rp);
    421       1.1       cgd 	INTON;
    422       1.1       cgd }
    423       1.1       cgd 
    424       1.1       cgd /*
    425       1.1       cgd  * Undo all redirections.  Called on error or interrupt.
    426       1.1       cgd  */
    427       1.1       cgd 
    428       1.1       cgd #ifdef mkinit
    429       1.1       cgd 
    430       1.1       cgd INCLUDE "redir.h"
    431       1.1       cgd 
    432       1.1       cgd RESET {
    433       1.1       cgd 	while (redirlist)
    434       1.1       cgd 		popredir();
    435       1.1       cgd }
    436       1.1       cgd 
    437       1.1       cgd SHELLPROC {
    438      1.24  christos 	clearredir(0);
    439       1.1       cgd }
    440       1.1       cgd 
    441       1.1       cgd #endif
    442       1.1       cgd 
    443       1.7       jtc /* Return true if fd 0 has already been redirected at least once.  */
    444       1.7       jtc int
    445      1.43  christos fd0_redirected_p(void)
    446      1.43  christos {
    447      1.44       kre 	return fd0_redirected != 0;
    448       1.7       jtc }
    449       1.1       cgd 
    450       1.1       cgd /*
    451       1.1       cgd  * Discard all saved file descriptors.
    452       1.1       cgd  */
    453       1.1       cgd 
    454       1.1       cgd void
    455      1.33      matt clearredir(int vforked)
    456      1.24  christos {
    457      1.14       tls 	struct redirtab *rp;
    458      1.43  christos 	struct renamelist *rl;
    459       1.1       cgd 
    460       1.1       cgd 	for (rp = redirlist ; rp ; rp = rp->next) {
    461      1.43  christos 		if (!vforked)
    462      1.43  christos 			free_rl(rp, 0);
    463      1.43  christos 		else for (rl = rp->renamed; rl; rl = rl->next)
    464      1.43  christos 			if (rl->into >= 0)
    465      1.43  christos 				close(rl->into);
    466       1.1       cgd 	}
    467       1.1       cgd }
    468       1.1       cgd 
    469       1.1       cgd 
    470       1.1       cgd 
    471       1.1       cgd /*
    472      1.47       kre  * Copy a file descriptor to be == to.
    473      1.47       kre  * cloexec indicates if we want close-on-exec or not.
    474      1.47       kre  * Returns -1 if any error occurs.
    475       1.1       cgd  */
    476       1.1       cgd 
    477      1.47       kre STATIC int
    478      1.47       kre copyfd(int from, int to, int cloexec)
    479       1.9       cgd {
    480       1.1       cgd 	int newfd;
    481       1.1       cgd 
    482      1.47       kre 	if (cloexec && to > 2)
    483      1.47       kre 		newfd = dup3(from, to, O_CLOEXEC);
    484      1.47       kre 	else
    485      1.47       kre 		newfd = dup2(from, to);
    486      1.47       kre 
    487       1.1       cgd 	return newfd;
    488       1.1       cgd }
    489      1.43  christos 
    490      1.47       kre /*
    491      1.47       kre  * rename fd from to be fd to (closing from).
    492      1.47       kre  * close-on-exec is never set on 'to' (unless
    493      1.47       kre  * from==to and it was set on from) - ie: a no-op
    494      1.47       kre  * returns to (or errors() if an error occurs).
    495      1.47       kre  *
    496      1.47       kre  * This is mostly used for rearranging the
    497      1.47       kre  * results from pipe().
    498      1.47       kre  */
    499      1.43  christos int
    500      1.43  christos movefd(int from, int to)
    501      1.43  christos {
    502      1.43  christos 	if (from == to)
    503      1.43  christos 		return to;
    504      1.43  christos 
    505      1.43  christos 	(void) close(to);
    506      1.47       kre 	if (copyfd(from, to, 0) != to) {
    507      1.47       kre 		int e = errno;
    508      1.47       kre 
    509      1.47       kre 		(void) close(from);
    510      1.47       kre 		error("Unable to make fd %d: %s", to, strerror(e));
    511      1.47       kre 	}
    512      1.43  christos 	(void) close(from);
    513      1.43  christos 
    514      1.43  christos 	return to;
    515      1.43  christos }
    516      1.43  christos 
    517      1.43  christos STATIC void
    518      1.43  christos find_big_fd(void)
    519      1.43  christos {
    520      1.43  christos 	int i, fd;
    521  1.53.2.1  pgoyette 	static int last_start = 6;
    522      1.43  christos 
    523  1.53.2.1  pgoyette 	if (last_start < 10)
    524  1.53.2.1  pgoyette 		last_start++;
    525  1.53.2.1  pgoyette 
    526  1.53.2.1  pgoyette 	for (i = (1 << last_start); i >= 10; i >>= 1) {
    527      1.43  christos 		if ((fd = fcntl(0, F_DUPFD, i - 1)) >= 0) {
    528      1.43  christos 			close(fd);
    529      1.43  christos 			break;
    530      1.43  christos 		}
    531      1.43  christos 	}
    532      1.43  christos 
    533      1.43  christos 	fd = (i / 5) * 4;
    534  1.53.2.1  pgoyette 	if (fd < 10)
    535      1.43  christos 		fd = 10;
    536      1.43  christos 
    537      1.43  christos 	big_sh_fd = fd;
    538      1.43  christos }
    539      1.43  christos 
    540      1.47       kre /*
    541      1.47       kre  * If possible, move file descriptor fd out of the way
    542      1.47       kre  * of expected user fd values.   Returns the new fd
    543      1.47       kre  * (which may be the input fd if things do not go well.)
    544      1.47       kre  * Always set close-on-exec on the result, and close
    545      1.47       kre  * the input fd unless it is to be our result.
    546      1.47       kre  */
    547      1.43  christos int
    548      1.43  christos to_upper_fd(int fd)
    549      1.43  christos {
    550      1.43  christos 	int i;
    551      1.43  christos 
    552      1.43  christos 	if (big_sh_fd < 10)
    553      1.43  christos 		find_big_fd();
    554      1.43  christos 	do {
    555      1.46       kre 		i = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd);
    556      1.43  christos 		if (i >= 0) {
    557      1.43  christos 			if (fd != i)
    558      1.43  christos 				close(fd);
    559      1.43  christos 			return i;
    560      1.43  christos 		}
    561  1.53.2.1  pgoyette 		if (errno != EMFILE && errno != EINVAL)
    562      1.43  christos 			break;
    563      1.43  christos 		find_big_fd();
    564      1.43  christos 	} while (big_sh_fd > 10);
    565      1.43  christos 
    566      1.46       kre 	/*
    567      1.47       kre 	 * If we wanted to move this fd to some random high number
    568      1.46       kre 	 * we certainly do not intend to pass it through exec, even
    569      1.46       kre 	 * if the reassignment failed.
    570      1.46       kre 	 */
    571      1.46       kre 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    572      1.43  christos 	return fd;
    573      1.43  christos }
    574      1.50  christos 
    575  1.53.2.1  pgoyette void
    576  1.53.2.1  pgoyette register_sh_fd(int fd, void (*cb)(int, int))
    577  1.53.2.1  pgoyette {
    578  1.53.2.1  pgoyette 	struct shell_fds *fp;
    579  1.53.2.1  pgoyette 
    580  1.53.2.1  pgoyette 	fp = ckmalloc(sizeof (struct shell_fds));
    581  1.53.2.1  pgoyette 	if (fp != NULL) {
    582  1.53.2.1  pgoyette 		fp->nxt = sh_fd_list;
    583  1.53.2.1  pgoyette 		sh_fd_list = fp;
    584  1.53.2.1  pgoyette 
    585  1.53.2.1  pgoyette 		fp->fd = fd;
    586  1.53.2.1  pgoyette 		fp->cb = cb;
    587  1.53.2.1  pgoyette 	}
    588  1.53.2.1  pgoyette }
    589  1.53.2.1  pgoyette 
    590  1.53.2.1  pgoyette void
    591  1.53.2.1  pgoyette sh_close(int fd)
    592  1.53.2.1  pgoyette {
    593  1.53.2.1  pgoyette 	struct shell_fds **fpp, *fp;
    594  1.53.2.1  pgoyette 
    595  1.53.2.1  pgoyette 	fpp = &sh_fd_list;
    596  1.53.2.1  pgoyette 	while ((fp = *fpp) != NULL) {
    597  1.53.2.1  pgoyette 		if (fp->fd == fd) {
    598  1.53.2.1  pgoyette 			*fpp = fp->nxt;
    599  1.53.2.1  pgoyette 			ckfree(fp);
    600  1.53.2.1  pgoyette 			break;
    601  1.53.2.1  pgoyette 		}
    602  1.53.2.1  pgoyette 		fpp = &fp->nxt;
    603  1.53.2.1  pgoyette 	}
    604  1.53.2.1  pgoyette 	(void)close(fd);
    605  1.53.2.1  pgoyette }
    606  1.53.2.1  pgoyette 
    607  1.53.2.1  pgoyette STATIC struct shell_fds *
    608  1.53.2.1  pgoyette sh_fd(int fd)
    609  1.53.2.1  pgoyette {
    610  1.53.2.1  pgoyette 	struct shell_fds *fp;
    611  1.53.2.1  pgoyette 
    612  1.53.2.1  pgoyette 	for (fp = sh_fd_list; fp != NULL; fp = fp->nxt)
    613  1.53.2.1  pgoyette 		if (fp->fd == fd)
    614  1.53.2.1  pgoyette 			return fp;
    615  1.53.2.1  pgoyette 	return NULL;
    616  1.53.2.1  pgoyette }
    617  1.53.2.1  pgoyette 
    618  1.53.2.1  pgoyette STATIC void
    619  1.53.2.1  pgoyette renumber_sh_fd(struct shell_fds *fp)
    620  1.53.2.1  pgoyette {
    621  1.53.2.1  pgoyette 	int to;
    622  1.53.2.1  pgoyette 
    623  1.53.2.1  pgoyette 	if (fp == NULL)
    624  1.53.2.1  pgoyette 		return;
    625  1.53.2.1  pgoyette 
    626  1.53.2.1  pgoyette #ifndef	F_DUPFD_CLOEXEC
    627  1.53.2.1  pgoyette #define	F_DUPFD_CLOEXEC	F_DUPFD
    628  1.53.2.1  pgoyette #define	CLOEXEC(fd)	(fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC))
    629  1.53.2.1  pgoyette #else
    630  1.53.2.1  pgoyette #define	CLOEXEC(fd)
    631  1.53.2.1  pgoyette #endif
    632  1.53.2.1  pgoyette 
    633  1.53.2.1  pgoyette 	to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd);
    634  1.53.2.1  pgoyette 	if (to == -1)
    635  1.53.2.1  pgoyette 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2);
    636  1.53.2.1  pgoyette 	if (to == -1)
    637  1.53.2.1  pgoyette 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, fp->fd + 1);
    638  1.53.2.1  pgoyette 	if (to == -1)
    639  1.53.2.1  pgoyette 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 10);
    640  1.53.2.1  pgoyette 	if (to == -1)
    641  1.53.2.1  pgoyette 		to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 3);
    642  1.53.2.1  pgoyette 	if (to == -1)
    643  1.53.2.1  pgoyette 		error("insufficient file descriptors available");
    644  1.53.2.1  pgoyette 	CLOEXEC(to);
    645  1.53.2.1  pgoyette 
    646  1.53.2.1  pgoyette 	if (fp->fd == to)	/* impossible? */
    647  1.53.2.1  pgoyette 		return;
    648  1.53.2.1  pgoyette 
    649  1.53.2.1  pgoyette 	(*fp->cb)(fp->fd, to);
    650  1.53.2.1  pgoyette 	(void)close(fp->fd);
    651  1.53.2.1  pgoyette 	fp->fd = to;
    652  1.53.2.1  pgoyette }
    653  1.53.2.1  pgoyette 
    654      1.51       kre static const struct flgnames {
    655      1.50  christos 	const char *name;
    656      1.50  christos 	uint32_t value;
    657      1.50  christos } nv[] = {
    658      1.50  christos #ifdef O_APPEND
    659      1.50  christos 	{ "append",	O_APPEND 	},
    660      1.50  christos #endif
    661      1.50  christos #ifdef O_ASYNC
    662      1.50  christos 	{ "async",	O_ASYNC		},
    663      1.50  christos #endif
    664      1.50  christos #ifdef O_SYNC
    665      1.50  christos 	{ "sync",	O_SYNC		},
    666      1.50  christos #endif
    667      1.50  christos #ifdef O_NONBLOCK
    668      1.50  christos 	{ "nonblock",	O_NONBLOCK	},
    669      1.50  christos #endif
    670      1.50  christos #ifdef O_FSYNC
    671      1.50  christos 	{ "fsync",	O_FSYNC		},
    672      1.50  christos #endif
    673      1.50  christos #ifdef O_DSYNC
    674      1.50  christos 	{ "dsync",	O_DSYNC		},
    675      1.50  christos #endif
    676      1.50  christos #ifdef O_RSYNC
    677      1.50  christos 	{ "rsync",	O_RSYNC		},
    678      1.50  christos #endif
    679      1.50  christos #ifdef O_ALTIO
    680      1.50  christos 	{ "altio",	O_ALT_IO	},
    681      1.50  christos #endif
    682      1.50  christos #ifdef O_DIRECT
    683      1.50  christos 	{ "direct",	O_DIRECT	},
    684      1.50  christos #endif
    685      1.50  christos #ifdef O_NOSIGPIPE
    686      1.50  christos 	{ "nosigpipe",	O_NOSIGPIPE	},
    687      1.50  christos #endif
    688      1.50  christos #ifdef O_CLOEXEC
    689      1.50  christos 	{ "cloexec",	O_CLOEXEC	},
    690      1.50  christos #endif
    691      1.51       kre 	{ 0, 0 }
    692      1.50  christos };
    693      1.50  christos #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\
    694      1.50  christos     O_ALT_IO|O_DIRECT|O_NOSIGPIPE|O_CLOEXEC)
    695      1.50  christos 
    696      1.50  christos static int
    697      1.50  christos getflags(int fd, int p)
    698      1.50  christos {
    699      1.50  christos 	int c, f;
    700      1.50  christos 
    701  1.53.2.1  pgoyette 	if (sh_fd(fd) != NULL) {
    702  1.53.2.1  pgoyette 		if (!p)
    703  1.53.2.1  pgoyette 			return -1;
    704  1.53.2.1  pgoyette 		error("Can't get status for fd=%d (%s)", fd,
    705  1.53.2.1  pgoyette 		    "Bad file descriptor");			/*XXX*/
    706  1.53.2.1  pgoyette 	}
    707  1.53.2.1  pgoyette 
    708      1.50  christos 	if ((c = fcntl(fd, F_GETFD)) == -1) {
    709      1.50  christos 		if (!p)
    710      1.50  christos 			return -1;
    711      1.50  christos 		error("Can't get status for fd=%d (%s)", fd, strerror(errno));
    712      1.50  christos 	}
    713      1.50  christos 	if ((f = fcntl(fd, F_GETFL)) == -1) {
    714      1.50  christos 		if (!p)
    715      1.50  christos 			return -1;
    716      1.50  christos 		error("Can't get flags for fd=%d (%s)", fd, strerror(errno));
    717      1.50  christos 	}
    718      1.51       kre 	if (c & FD_CLOEXEC)
    719      1.50  christos 		f |= O_CLOEXEC;
    720      1.50  christos 	return f & ALLFLAGS;
    721      1.50  christos }
    722      1.50  christos 
    723      1.50  christos static void
    724      1.51       kre printone(int fd, int p, int verbose, int pfd)
    725      1.50  christos {
    726      1.50  christos 	int f = getflags(fd, p);
    727      1.51       kre 	const struct flgnames *fn;
    728      1.50  christos 
    729      1.50  christos 	if (f == -1)
    730      1.50  christos 		return;
    731      1.50  christos 
    732      1.51       kre 	if (pfd)
    733      1.51       kre 		outfmt(out1, "%d: ", fd);
    734      1.51       kre 	for (fn = nv; fn->name; fn++) {
    735      1.51       kre 		if (f & fn->value) {
    736      1.51       kre 			outfmt(out1, "%s%s", verbose ? "+" : "", fn->name);
    737      1.51       kre 			f &= ~fn->value;
    738      1.50  christos 		} else if (verbose)
    739      1.51       kre 			outfmt(out1, "-%s", fn->name);
    740      1.50  christos 		else
    741      1.50  christos 			continue;
    742      1.51       kre 		if (f || (verbose && fn[1].name))
    743      1.50  christos 			outfmt(out1, ",");
    744      1.50  christos 	}
    745      1.51       kre 	if (verbose && f)		/* f should be normally be 0 */
    746      1.51       kre 		outfmt(out1, " +%#x", f);
    747      1.50  christos 	outfmt(out1, "\n");
    748      1.50  christos }
    749      1.50  christos 
    750      1.51       kre static void
    751      1.50  christos parseflags(char *s, int *p, int *n)
    752      1.50  christos {
    753      1.51       kre 	int *v, *w;
    754      1.51       kre 	const struct flgnames *fn;
    755      1.50  christos 
    756      1.50  christos 	*p = 0;
    757      1.50  christos 	*n = 0;
    758      1.50  christos 	for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
    759      1.51       kre 		switch (*s++) {
    760      1.50  christos 		case '+':
    761      1.50  christos 			v = p;
    762      1.51       kre 			w = n;
    763      1.50  christos 			break;
    764      1.50  christos 		case '-':
    765      1.50  christos 			v = n;
    766      1.51       kre 			w = p;
    767      1.50  christos 			break;
    768      1.50  christos 		default:
    769      1.51       kre 			error("Missing +/- indicator before flag %s", s-1);
    770      1.50  christos 		}
    771      1.50  christos 
    772      1.51       kre 		for (fn = nv; fn->name; fn++)
    773      1.51       kre 			if (strcmp(s, fn->name) == 0) {
    774      1.51       kre 				*v |= fn->value;
    775      1.51       kre 				*w &=~ fn->value;
    776      1.50  christos 				break;
    777      1.50  christos 			}
    778      1.51       kre 		if (fn->name == 0)
    779      1.50  christos 			error("Bad flag `%s'", s);
    780      1.50  christos 	}
    781      1.50  christos }
    782      1.50  christos 
    783      1.50  christos static void
    784      1.50  christos setone(int fd, int pos, int neg, int verbose)
    785      1.50  christos {
    786      1.50  christos 	int f = getflags(fd, 1);
    787      1.51       kre 	int n, cloexec;
    788      1.51       kre 
    789      1.50  christos 	if (f == -1)
    790      1.50  christos 		return;
    791      1.50  christos 
    792      1.51       kre 	cloexec = -1;
    793      1.50  christos 	if ((pos & O_CLOEXEC) && !(f & O_CLOEXEC))
    794      1.50  christos 		cloexec = FD_CLOEXEC;
    795      1.50  christos 	if ((neg & O_CLOEXEC) && (f & O_CLOEXEC))
    796      1.50  christos 		cloexec = 0;
    797      1.50  christos 
    798      1.50  christos 	if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1)
    799      1.50  christos 		error("Can't set status for fd=%d (%s)", fd, strerror(errno));
    800      1.50  christos 
    801      1.50  christos 	pos &= ~O_CLOEXEC;
    802      1.50  christos 	neg &= ~O_CLOEXEC;
    803      1.50  christos 	f &= ~O_CLOEXEC;
    804      1.51       kre 	n = f;
    805      1.50  christos 	n |= pos;
    806      1.50  christos 	n &= ~neg;
    807      1.50  christos 	if (n != f && fcntl(fd, F_SETFL, n) == -1)
    808      1.50  christos 		error("Can't set flags for fd=%d (%s)", fd, strerror(errno));
    809      1.50  christos 	if (verbose)
    810      1.51       kre 		printone(fd, 1, verbose, 1);
    811      1.50  christos }
    812      1.50  christos 
    813      1.50  christos int
    814      1.50  christos fdflagscmd(int argc, char *argv[])
    815      1.50  christos {
    816      1.50  christos 	char *num;
    817      1.50  christos 	int verbose = 0, ch, pos = 0, neg = 0;
    818      1.50  christos 	char *setflags = NULL;
    819      1.50  christos 
    820      1.50  christos 	optreset = 1; optind = 1; /* initialize getopt */
    821      1.50  christos 	while ((ch = getopt(argc, argv, ":vs:")) != -1)
    822      1.50  christos 		switch ((char)ch) {
    823      1.50  christos 		case 'v':
    824      1.50  christos 			verbose = 1;
    825      1.50  christos 			break;
    826      1.50  christos 		case 's':
    827      1.51       kre 			if (setflags)
    828      1.51       kre 				goto msg;
    829      1.50  christos 			setflags = optarg;
    830      1.50  christos 			break;
    831      1.50  christos 		case '?':
    832      1.50  christos 		default:
    833      1.50  christos 		msg:
    834      1.51       kre 			error("Usage: fdflags [-v] [-s <flags> fd] [fd...]");
    835      1.50  christos 			/* NOTREACHED */
    836      1.50  christos 		}
    837      1.50  christos 
    838      1.50  christos 	argc -= optind, argv += optind;
    839      1.50  christos 
    840      1.51       kre 	if (setflags)
    841      1.51       kre 		parseflags(setflags, &pos, &neg);
    842      1.50  christos 
    843      1.50  christos 	if (argc == 0) {
    844      1.53       kre 		int i;
    845      1.51       kre 
    846      1.50  christos 		if (setflags)
    847      1.50  christos 			goto msg;
    848      1.51       kre 
    849      1.51       kre 		/*
    850      1.51       kre 		 * XXX  we should only ever operate on user defined fds
    851      1.51       kre 		 * XXX  not on sh internal fds that might be open.
    852      1.51       kre 		 * XXX  but for that we need to know their range (later)
    853      1.51       kre 		 */
    854      1.53       kre 		for (i = 0; i <= max_user_fd; i++)
    855      1.51       kre 			printone(i, 0, verbose, 1);
    856      1.50  christos 		return 0;
    857      1.50  christos 	}
    858      1.50  christos 
    859      1.50  christos 	while ((num = *argv++) != NULL) {
    860      1.51       kre 		int fd = number(num);
    861      1.51       kre 
    862      1.52       kre 		while (num[0] == '0' && num[1] != '\0')		/* skip 0's */
    863      1.52       kre 			num++;
    864      1.51       kre 		if (strlen(num) > 5)
    865      1.51       kre 			error("%s too big to be a file descriptor", num);
    866      1.51       kre 
    867      1.50  christos 		if (setflags)
    868      1.50  christos 			setone(fd, pos, neg, verbose);
    869      1.50  christos 		else
    870      1.51       kre 			printone(fd, 1, verbose, argc > 1);
    871      1.50  christos 	}
    872      1.50  christos 	return 0;
    873      1.50  christos }
    874