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