Home | History | Annotate | Line # | Download | only in sh
redir.c revision 1.47
      1  1.47       kre /*	$NetBSD: redir.c,v 1.47 2016/05/12 13:31:37 kre 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.47       kre __RCSID("$NetBSD: redir.c,v 1.47 2016/05/12 13:31:37 kre 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.44       kre #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.43  christos struct renamelist {
     80  1.43  christos 	struct renamelist *next;
     81  1.43  christos 	int orig;
     82  1.43  christos 	int into;
     83  1.43  christos };
     84  1.43  christos 
     85  1.43  christos MKINIT
     86   1.1       cgd struct redirtab {
     87   1.1       cgd 	struct redirtab *next;
     88  1.43  christos 	struct renamelist *renamed;
     89   1.1       cgd };
     90   1.1       cgd 
     91   1.1       cgd 
     92   1.1       cgd MKINIT struct redirtab *redirlist;
     93   1.1       cgd 
     94  1.13  christos /*
     95   1.7       jtc  * We keep track of whether or not fd0 has been redirected.  This is for
     96   1.7       jtc  * background commands, where we want to redirect fd0 to /dev/null only
     97  1.13  christos  * if it hasn't already been redirected.
     98  1.43  christos  */
     99  1.43  christos STATIC int fd0_redirected = 0;
    100   1.1       cgd 
    101  1.43  christos /*
    102  1.43  christos  * And also where to put internal use fds that should be out of the
    103  1.43  christos  * way of user defined fds (normally)
    104  1.43  christos  */
    105  1.43  christos STATIC int big_sh_fd = 0;
    106  1.43  christos 
    107  1.43  christos STATIC const struct renamelist *is_renamed(const struct renamelist *, int);
    108  1.43  christos STATIC void fd_rename(struct redirtab *, int, int);
    109  1.43  christos STATIC void free_rl(struct redirtab *, int);
    110  1.29  christos STATIC void openredirect(union node *, char[10], int);
    111  1.34      yamt STATIC int openhere(const union node *);
    112  1.47       kre STATIC int copyfd(int, int, int);
    113  1.43  christos STATIC void find_big_fd(void);
    114  1.43  christos 
    115  1.43  christos STATIC const struct renamelist *
    116  1.43  christos is_renamed(const struct renamelist *rl, int fd)
    117  1.43  christos {
    118  1.43  christos 	while (rl != NULL) {
    119  1.43  christos 		if (rl->orig == fd)
    120  1.43  christos 			return rl;
    121  1.43  christos 		rl = rl->next;
    122  1.43  christos 	}
    123  1.43  christos 	return NULL;
    124  1.43  christos }
    125  1.43  christos 
    126  1.43  christos STATIC void
    127  1.43  christos free_rl(struct redirtab *rt, int reset)
    128  1.43  christos {
    129  1.43  christos 	struct renamelist *rl, *rn = rt->renamed;
    130  1.43  christos 
    131  1.43  christos 	while ((rl = rn) != NULL) {
    132  1.43  christos 		rn = rl->next;
    133  1.43  christos 		if (rl->orig == 0)
    134  1.43  christos 			fd0_redirected--;
    135  1.43  christos 		if (reset) {
    136  1.43  christos 			if (rl->into < 0)
    137  1.43  christos 				close(rl->orig);
    138  1.43  christos 			else
    139  1.43  christos 				movefd(rl->into, rl->orig);
    140  1.43  christos 		}
    141  1.43  christos 		ckfree(rl);
    142  1.43  christos 	}
    143  1.43  christos 	rt->renamed = NULL;
    144  1.43  christos }
    145   1.1       cgd 
    146  1.43  christos STATIC void
    147  1.43  christos fd_rename(struct redirtab *rt, int from, int to)
    148  1.43  christos {
    149  1.43  christos 	struct renamelist *rl = ckmalloc(sizeof(struct renamelist));
    150  1.43  christos 
    151  1.43  christos 	rl->next = rt->renamed;
    152  1.43  christos 	rt->renamed = rl;
    153  1.43  christos 
    154  1.43  christos 	rl->orig = from;
    155  1.43  christos 	rl->into = to;
    156  1.43  christos }
    157   1.1       cgd 
    158   1.1       cgd /*
    159   1.1       cgd  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
    160   1.1       cgd  * old file descriptors are stashed away so that the redirection can be
    161   1.1       cgd  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
    162   1.1       cgd  * standard output, and the standard error if it becomes a duplicate of
    163   1.1       cgd  * stdout, is saved in memory.
    164   1.1       cgd  */
    165   1.1       cgd 
    166   1.1       cgd void
    167  1.27  christos redirect(union node *redir, int flags)
    168  1.27  christos {
    169   1.1       cgd 	union node *n;
    170  1.16  christos 	struct redirtab *sv = NULL;
    171   1.1       cgd 	int i;
    172   1.1       cgd 	int fd;
    173  1.13  christos 	char memory[10];	/* file descriptors to write to memory */
    174   1.1       cgd 
    175   1.1       cgd 	for (i = 10 ; --i >= 0 ; )
    176   1.1       cgd 		memory[i] = 0;
    177   1.1       cgd 	memory[1] = flags & REDIR_BACKQ;
    178   1.1       cgd 	if (flags & REDIR_PUSH) {
    179  1.24  christos 		/* We don't have to worry about REDIR_VFORK here, as
    180  1.24  christos 		 * flags & REDIR_PUSH is never true if REDIR_VFORK is set.
    181  1.24  christos 		 */
    182   1.1       cgd 		sv = ckmalloc(sizeof (struct redirtab));
    183  1.43  christos 		sv->renamed = NULL;
    184   1.1       cgd 		sv->next = redirlist;
    185   1.1       cgd 		redirlist = sv;
    186   1.1       cgd 	}
    187   1.1       cgd 	for (n = redir ; n ; n = n->nfile.next) {
    188   1.1       cgd 		fd = n->nfile.fd;
    189  1.13  christos 		if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
    190  1.43  christos 		    n->ndup.dupfd == fd) {
    191  1.43  christos 			/* redirect from/to same file descriptor */
    192  1.47       kre 			/* make sure it stays open */
    193  1.47       kre 			if (fcntl(fd, F_SETFD, 0) < 0)
    194  1.47       kre 				error("fd %d: %s", fd, strerror(errno));
    195  1.43  christos 			continue;
    196  1.43  christos 		}
    197  1.15  christos 
    198  1.43  christos 		if ((flags & REDIR_PUSH) && !is_renamed(sv->renamed, fd)) {
    199   1.1       cgd 			INTOFF;
    200  1.43  christos 			if (big_sh_fd < 10)
    201  1.43  christos 				find_big_fd();
    202  1.43  christos 			if ((i = fcntl(fd, F_DUPFD, big_sh_fd)) == -1) {
    203  1.15  christos 				switch (errno) {
    204  1.15  christos 				case EBADF:
    205  1.35      yamt 					i = CLOSED;
    206  1.35      yamt 					break;
    207  1.43  christos 				case EMFILE:
    208  1.43  christos 				case EINVAL:
    209  1.43  christos 					find_big_fd();
    210  1.43  christos 					i = fcntl(fd, F_DUPFD, big_sh_fd);
    211  1.43  christos 					if (i >= 0)
    212  1.43  christos 						break;
    213  1.43  christos 					/* FALLTHRU */
    214  1.15  christos 				default:
    215  1.43  christos 					i = errno;
    216  1.43  christos 					INTON;    /* XXX not needed here ? */
    217  1.43  christos 					error("%d: %s", fd, strerror(i));
    218  1.19   mycroft 					/* NOTREACHED */
    219  1.15  christos 				}
    220  1.43  christos 			}
    221  1.43  christos 			if (i >= 0)
    222  1.35      yamt 				(void)fcntl(i, F_SETFD, FD_CLOEXEC);
    223  1.43  christos 			fd_rename(sv, fd, i);
    224   1.1       cgd 			INTON;
    225   1.1       cgd 		}
    226  1.44       kre 		if (fd == 0)
    227  1.44       kre 			fd0_redirected++;
    228  1.35      yamt 		openredirect(n, memory, flags);
    229   1.1       cgd 	}
    230   1.1       cgd 	if (memory[1])
    231   1.1       cgd 		out1 = &memout;
    232   1.1       cgd 	if (memory[2])
    233   1.1       cgd 		out2 = &memout;
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.1       cgd 
    237   1.1       cgd STATIC void
    238  1.29  christos openredirect(union node *redir, char memory[10], int flags)
    239  1.27  christos {
    240  1.36  christos 	struct stat sb;
    241   1.1       cgd 	int fd = redir->nfile.fd;
    242   1.1       cgd 	char *fname;
    243   1.1       cgd 	int f;
    244  1.42  christos 	int eflags, cloexec;
    245   1.1       cgd 
    246   1.1       cgd 	/*
    247   1.1       cgd 	 * We suppress interrupts so that we won't leave open file
    248   1.1       cgd 	 * descriptors around.  This may not be such a good idea because
    249   1.1       cgd 	 * an open of a device or a fifo can block indefinitely.
    250   1.1       cgd 	 */
    251   1.1       cgd 	INTOFF;
    252  1.43  christos 	if (fd < 10)
    253  1.43  christos 		memory[fd] = 0;
    254   1.1       cgd 	switch (redir->nfile.type) {
    255   1.1       cgd 	case NFROM:
    256   1.1       cgd 		fname = redir->nfile.expfname;
    257  1.29  christos 		if (flags & REDIR_VFORK)
    258  1.29  christos 			eflags = O_NONBLOCK;
    259  1.29  christos 		else
    260  1.29  christos 			eflags = 0;
    261  1.29  christos 		if ((f = open(fname, O_RDONLY|eflags)) < 0)
    262  1.20  christos 			goto eopen;
    263  1.29  christos 		if (eflags)
    264  1.29  christos 			(void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);
    265  1.20  christos 		break;
    266  1.20  christos 	case NFROMTO:
    267  1.20  christos 		fname = redir->nfile.expfname;
    268  1.20  christos 		if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
    269  1.20  christos 			goto ecreate;
    270   1.1       cgd 		break;
    271   1.1       cgd 	case NTO:
    272  1.36  christos 		if (Cflag) {
    273  1.36  christos 			fname = redir->nfile.expfname;
    274  1.37  christos 			if ((f = open(fname, O_WRONLY)) == -1) {
    275  1.36  christos 				if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL,
    276  1.36  christos 				    0666)) < 0)
    277  1.36  christos 					goto ecreate;
    278  1.37  christos 			} else if (fstat(f, &sb) == -1) {
    279  1.37  christos 				int serrno = errno;
    280  1.37  christos 				close(f);
    281  1.37  christos 				errno = serrno;
    282  1.37  christos 				goto ecreate;
    283  1.37  christos 			} else if (S_ISREG(sb.st_mode)) {
    284  1.37  christos 				close(f);
    285  1.36  christos 				errno = EEXIST;
    286  1.36  christos 				goto ecreate;
    287  1.36  christos 			}
    288  1.36  christos 			break;
    289  1.36  christos 		}
    290  1.23  christos 		/* FALLTHROUGH */
    291  1.23  christos 	case NCLOBBER:
    292   1.1       cgd 		fname = redir->nfile.expfname;
    293  1.36  christos 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
    294  1.20  christos 			goto ecreate;
    295  1.20  christos 		break;
    296   1.1       cgd 	case NAPPEND:
    297   1.1       cgd 		fname = redir->nfile.expfname;
    298   1.1       cgd 		if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
    299  1.20  christos 			goto ecreate;
    300  1.20  christos 		break;
    301   1.1       cgd 	case NTOFD:
    302   1.1       cgd 	case NFROMFD:
    303   1.1       cgd 		if (redir->ndup.dupfd >= 0) {	/* if not ">&-" */
    304  1.43  christos 			if (fd < 10 && redir->ndup.dupfd < 10 &&
    305  1.43  christos 			    memory[redir->ndup.dupfd])
    306   1.1       cgd 				memory[fd] = 1;
    307  1.47       kre 			else if (copyfd(redir->ndup.dupfd, fd,
    308  1.47       kre 			    (flags&(REDIR_PUSH|REDIR_KEEP)) == REDIR_PUSH) < 0)
    309  1.47       kre 				error("Redirect (from %d to %d) failed: %s",
    310  1.47       kre 				    redir->ndup.dupfd, fd, strerror(errno));
    311  1.45       kre 		} else
    312  1.47       kre 			(void) close(fd);
    313  1.20  christos 		INTON;
    314  1.20  christos 		return;
    315   1.1       cgd 	case NHERE:
    316   1.1       cgd 	case NXHERE:
    317   1.1       cgd 		f = openhere(redir);
    318  1.20  christos 		break;
    319   1.1       cgd 	default:
    320   1.1       cgd 		abort();
    321   1.1       cgd 	}
    322  1.20  christos 
    323  1.42  christos 	cloexec = fd > 2 && (flags & REDIR_KEEP) == 0;
    324  1.20  christos 	if (f != fd) {
    325  1.47       kre 		if (copyfd(f, fd, cloexec) < 0) {
    326  1.47       kre 			int e = errno;
    327  1.47       kre 
    328  1.47       kre 			close(f);
    329  1.47       kre 			error("redirect reassignment (fd %d) failed: %s", fd,
    330  1.47       kre 			    strerror(e));
    331  1.47       kre 		}
    332  1.20  christos 		close(f);
    333  1.42  christos 	} else if (cloexec)
    334  1.38  christos 		(void)fcntl(f, F_SETFD, FD_CLOEXEC);
    335  1.38  christos 
    336   1.1       cgd 	INTON;
    337  1.20  christos 	return;
    338  1.20  christos ecreate:
    339  1.30   msaitoh 	exerrno = 1;
    340  1.20  christos 	error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
    341  1.20  christos eopen:
    342  1.30   msaitoh 	exerrno = 1;
    343  1.20  christos 	error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
    344   1.1       cgd }
    345   1.1       cgd 
    346   1.1       cgd 
    347   1.1       cgd /*
    348   1.1       cgd  * Handle here documents.  Normally we fork off a process to write the
    349   1.1       cgd  * data to a pipe.  If the document is short, we can stuff the data in
    350   1.1       cgd  * the pipe without forking.
    351   1.1       cgd  */
    352   1.1       cgd 
    353   1.1       cgd STATIC int
    354  1.34      yamt openhere(const union node *redir)
    355  1.27  christos {
    356   1.1       cgd 	int pip[2];
    357  1.12  christos 	int len = 0;
    358   1.1       cgd 
    359   1.1       cgd 	if (pipe(pip) < 0)
    360   1.1       cgd 		error("Pipe call failed");
    361   1.1       cgd 	if (redir->type == NHERE) {
    362   1.1       cgd 		len = strlen(redir->nhere.doc->narg.text);
    363   1.1       cgd 		if (len <= PIPESIZE) {
    364   1.1       cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    365   1.1       cgd 			goto out;
    366   1.1       cgd 		}
    367   1.1       cgd 	}
    368  1.32    plunky 	if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
    369   1.1       cgd 		close(pip[0]);
    370   1.1       cgd 		signal(SIGINT, SIG_IGN);
    371   1.1       cgd 		signal(SIGQUIT, SIG_IGN);
    372   1.1       cgd 		signal(SIGHUP, SIG_IGN);
    373   1.1       cgd #ifdef SIGTSTP
    374   1.1       cgd 		signal(SIGTSTP, SIG_IGN);
    375   1.1       cgd #endif
    376   1.1       cgd 		signal(SIGPIPE, SIG_DFL);
    377   1.1       cgd 		if (redir->type == NHERE)
    378   1.1       cgd 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
    379   1.1       cgd 		else
    380   1.1       cgd 			expandhere(redir->nhere.doc, pip[1]);
    381   1.1       cgd 		_exit(0);
    382   1.1       cgd 	}
    383   1.1       cgd out:
    384   1.1       cgd 	close(pip[1]);
    385   1.1       cgd 	return pip[0];
    386   1.1       cgd }
    387   1.1       cgd 
    388   1.1       cgd 
    389   1.1       cgd 
    390   1.1       cgd /*
    391   1.1       cgd  * Undo the effects of the last redirection.
    392   1.1       cgd  */
    393   1.1       cgd 
    394   1.1       cgd void
    395  1.27  christos popredir(void)
    396  1.27  christos {
    397  1.14       tls 	struct redirtab *rp = redirlist;
    398   1.1       cgd 
    399   1.1       cgd 	INTOFF;
    400  1.43  christos 	free_rl(rp, 1);
    401   1.1       cgd 	redirlist = rp->next;
    402   1.1       cgd 	ckfree(rp);
    403   1.1       cgd 	INTON;
    404   1.1       cgd }
    405   1.1       cgd 
    406   1.1       cgd /*
    407   1.1       cgd  * Undo all redirections.  Called on error or interrupt.
    408   1.1       cgd  */
    409   1.1       cgd 
    410   1.1       cgd #ifdef mkinit
    411   1.1       cgd 
    412   1.1       cgd INCLUDE "redir.h"
    413   1.1       cgd 
    414   1.1       cgd RESET {
    415   1.1       cgd 	while (redirlist)
    416   1.1       cgd 		popredir();
    417   1.1       cgd }
    418   1.1       cgd 
    419   1.1       cgd SHELLPROC {
    420  1.24  christos 	clearredir(0);
    421   1.1       cgd }
    422   1.1       cgd 
    423   1.1       cgd #endif
    424   1.1       cgd 
    425   1.7       jtc /* Return true if fd 0 has already been redirected at least once.  */
    426   1.7       jtc int
    427  1.43  christos fd0_redirected_p(void)
    428  1.43  christos {
    429  1.44       kre 	return fd0_redirected != 0;
    430   1.7       jtc }
    431   1.1       cgd 
    432   1.1       cgd /*
    433   1.1       cgd  * Discard all saved file descriptors.
    434   1.1       cgd  */
    435   1.1       cgd 
    436   1.1       cgd void
    437  1.33      matt clearredir(int vforked)
    438  1.24  christos {
    439  1.14       tls 	struct redirtab *rp;
    440  1.43  christos 	struct renamelist *rl;
    441   1.1       cgd 
    442   1.1       cgd 	for (rp = redirlist ; rp ; rp = rp->next) {
    443  1.43  christos 		if (!vforked)
    444  1.43  christos 			free_rl(rp, 0);
    445  1.43  christos 		else for (rl = rp->renamed; rl; rl = rl->next)
    446  1.43  christos 			if (rl->into >= 0)
    447  1.43  christos 				close(rl->into);
    448   1.1       cgd 	}
    449   1.1       cgd }
    450   1.1       cgd 
    451   1.1       cgd 
    452   1.1       cgd 
    453   1.1       cgd /*
    454  1.47       kre  * Copy a file descriptor to be == to.
    455  1.47       kre  * cloexec indicates if we want close-on-exec or not.
    456  1.47       kre  * Returns -1 if any error occurs.
    457   1.1       cgd  */
    458   1.1       cgd 
    459  1.47       kre STATIC int
    460  1.47       kre copyfd(int from, int to, int cloexec)
    461   1.9       cgd {
    462   1.1       cgd 	int newfd;
    463   1.1       cgd 
    464  1.47       kre 	if (cloexec && to > 2)
    465  1.47       kre 		newfd = dup3(from, to, O_CLOEXEC);
    466  1.47       kre 	else
    467  1.47       kre 		newfd = dup2(from, to);
    468  1.47       kre 
    469   1.1       cgd 	return newfd;
    470   1.1       cgd }
    471  1.43  christos 
    472  1.47       kre /*
    473  1.47       kre  * rename fd from to be fd to (closing from).
    474  1.47       kre  * close-on-exec is never set on 'to' (unless
    475  1.47       kre  * from==to and it was set on from) - ie: a no-op
    476  1.47       kre  * returns to (or errors() if an error occurs).
    477  1.47       kre  *
    478  1.47       kre  * This is mostly used for rearranging the
    479  1.47       kre  * results from pipe().
    480  1.47       kre  */
    481  1.43  christos int
    482  1.43  christos movefd(int from, int to)
    483  1.43  christos {
    484  1.43  christos 	if (from == to)
    485  1.43  christos 		return to;
    486  1.43  christos 
    487  1.43  christos 	(void) close(to);
    488  1.47       kre 	if (copyfd(from, to, 0) != to) {
    489  1.47       kre 		int e = errno;
    490  1.47       kre 
    491  1.47       kre 		(void) close(from);
    492  1.47       kre 		error("Unable to make fd %d: %s", to, strerror(e));
    493  1.47       kre 	}
    494  1.43  christos 	(void) close(from);
    495  1.43  christos 
    496  1.43  christos 	return to;
    497  1.43  christos }
    498  1.43  christos 
    499  1.43  christos STATIC void
    500  1.43  christos find_big_fd(void)
    501  1.43  christos {
    502  1.43  christos 	int i, fd;
    503  1.43  christos 
    504  1.43  christos 	for (i = (1 << 10); i >= 10; i >>= 1) {
    505  1.43  christos 		if ((fd = fcntl(0, F_DUPFD, i - 1)) >= 0) {
    506  1.43  christos 			close(fd);
    507  1.43  christos 			break;
    508  1.43  christos 		}
    509  1.43  christos 	}
    510  1.43  christos 
    511  1.43  christos 	fd = (i / 5) * 4;
    512  1.43  christos 	if ((i - fd) > 100)
    513  1.43  christos 		fd = i - 100;
    514  1.43  christos 	else if (fd < 10)
    515  1.43  christos 		fd = 10;
    516  1.43  christos 
    517  1.43  christos 	big_sh_fd = fd;
    518  1.43  christos }
    519  1.43  christos 
    520  1.47       kre /*
    521  1.47       kre  * If possible, move file descriptor fd out of the way
    522  1.47       kre  * of expected user fd values.   Returns the new fd
    523  1.47       kre  * (which may be the input fd if things do not go well.)
    524  1.47       kre  * Always set close-on-exec on the result, and close
    525  1.47       kre  * the input fd unless it is to be our result.
    526  1.47       kre  */
    527  1.43  christos int
    528  1.43  christos to_upper_fd(int fd)
    529  1.43  christos {
    530  1.43  christos 	int i;
    531  1.43  christos 
    532  1.43  christos 	if (big_sh_fd < 10)
    533  1.43  christos 		find_big_fd();
    534  1.43  christos 	do {
    535  1.46       kre 		i = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd);
    536  1.43  christos 		if (i >= 0) {
    537  1.43  christos 			if (fd != i)
    538  1.43  christos 				close(fd);
    539  1.43  christos 			return i;
    540  1.43  christos 		}
    541  1.43  christos 		if (errno != EMFILE)
    542  1.43  christos 			break;
    543  1.43  christos 		find_big_fd();
    544  1.43  christos 	} while (big_sh_fd > 10);
    545  1.43  christos 
    546  1.46       kre 	/*
    547  1.47       kre 	 * If we wanted to move this fd to some random high number
    548  1.46       kre 	 * we certainly do not intend to pass it through exec, even
    549  1.46       kre 	 * if the reassignment failed.
    550  1.46       kre 	 */
    551  1.46       kre 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    552  1.43  christos 	return fd;
    553  1.43  christos }
    554