2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)redir.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/redir.c,v 1.34 2011/02/04 22:47:55 jilles Exp $
40 #include <sys/types.h>
50 * Code for dealing with input/output redirection.
64 #define EMPTY -2 /* marks an unused slot in redirtab */
65 #define CLOSED -1 /* fd was not open before redir */
66 #define PIPESIZE 4096 /* amount of buffering in a pipe */
71 struct redirtab *next;
76 MKINIT struct redirtab *redirlist;
79 * We keep track of whether or not fd0 has been redirected. This is for
80 * background commands, where we want to redirect fd0 to /dev/null only
81 * if it hasn't already been redirected.
83 static int fd0_redirected = 0;
85 static void openredirect(union node *, char[10 ]);
86 static int openhere(union node *);
90 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
91 * old file descriptors are stashed away so that the redirection can be
92 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
93 * standard output, and the standard error if it becomes a duplicate of
94 * stdout, is saved in memory.
98 redirect(union node *redir, int flags)
101 struct redirtab *sv = NULL;
104 char memory[10]; /* file descriptors to write to memory */
106 for (i = 10 ; --i >= 0 ; )
108 memory[1] = flags & REDIR_BACKQ;
109 if (flags & REDIR_PUSH) {
110 sv = ckmalloc(sizeof (struct redirtab));
111 for (i = 0 ; i < 10 ; i++)
112 sv->renamed[i] = EMPTY;
113 sv->next = redirlist;
116 for (n = redir ; n ; n = n->nfile.next) {
118 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
120 continue; /* redirect from/to same file descriptor */
122 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
124 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
131 error("%d: %s", fd, strerror(errno));
135 (void)fcntl(i, F_SETFD, FD_CLOEXEC);
141 openredirect(n, memory);
151 openredirect(union node *redir, char memory[10])
154 int fd = redir->nfile.fd;
160 * We suppress interrupts so that we won't leave open file
161 * descriptors around. Because the signal handler remains
162 * installed and we do not use system call restart, interrupts
163 * will still abort blocking opens such as fifos (they will fail
164 * with EINTR). There is, however, a race condition if an interrupt
165 * arrives after INTOFF and before open blocks.
169 switch (redir->nfile.type) {
171 fname = redir->nfile.expfname;
172 if ((f = open(fname, O_RDONLY)) < 0)
173 error("cannot open %s: %s", fname, strerror(errno));
176 if (dup2(f, fd) == -1) {
179 error("%d: %s", fd, strerror(e));
185 fname = redir->nfile.expfname;
186 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
187 error("cannot create %s: %s", fname, strerror(errno));
191 fname = redir->nfile.expfname;
192 if (stat(fname, &sb) == -1) {
193 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
194 error("cannot create %s: %s", fname, strerror(errno));
195 } else if (!S_ISREG(sb.st_mode)) {
196 if ((f = open(fname, O_WRONLY, 0666)) < 0)
197 error("cannot create %s: %s", fname, strerror(errno));
198 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
200 error("cannot create %s: %s", fname,
204 error("cannot create %s: %s", fname,
210 fname = redir->nfile.expfname;
211 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
212 error("cannot create %s: %s", fname, strerror(errno));
215 fname = redir->nfile.expfname;
216 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
217 error("cannot create %s: %s", fname, strerror(errno));
221 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
222 if (memory[redir->ndup.dupfd])
225 if (dup2(redir->ndup.dupfd, fd) < 0)
226 error("%d: %s", redir->ndup.dupfd,
245 * Handle here documents. Normally we fork off a process to write the
246 * data to a pipe. If the document is short, we can stuff the data in
247 * the pipe without forking.
251 openhere(union node *redir)
257 error("Pipe call failed: %s", strerror(errno));
258 if (redir->type == NHERE) {
259 len = strlen(redir->nhere.doc->narg.text);
260 if (len <= PIPESIZE) {
261 xwrite(pip[1], redir->nhere.doc->narg.text, len);
265 if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
267 signal(SIGINT, SIG_IGN);
268 signal(SIGQUIT, SIG_IGN);
269 signal(SIGHUP, SIG_IGN);
270 signal(SIGTSTP, SIG_IGN);
271 signal(SIGPIPE, SIG_DFL);
272 if (redir->type == NHERE)
273 xwrite(pip[1], redir->nhere.doc->narg.text, len);
275 expandhere(redir->nhere.doc, pip[1]);
286 * Undo the effects of the last redirection.
292 struct redirtab *rp = redirlist;
295 for (i = 0 ; i < 10 ; i++) {
296 if (rp->renamed[i] != EMPTY) {
299 if (rp->renamed[i] >= 0) {
300 dup2(rp->renamed[i], i);
301 close(rp->renamed[i]);
308 redirlist = rp->next;
314 * Undo all redirections. Called on error or interrupt.
328 /* Return true if fd 0 has already been redirected at least once. */
330 fd0_redirected_p(void)
332 return fd0_redirected != 0;
336 * Discard all saved file descriptors.
345 for (rp = redirlist ; rp ; rp = rp->next) {
346 for (i = 0 ; i < 10 ; i++) {
347 if (rp->renamed[i] >= 0) {
348 close(rp->renamed[i]);
350 rp->renamed[i] = EMPTY;