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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
51 * Code for dealing with input/output redirection.
65 #define EMPTY -2 /* marks an unused slot in redirtab */
66 #define CLOSED -1 /* fd was not open before redir */
67 #define PIPESIZE 4096 /* amount of buffering in a pipe */
72 struct redirtab *next;
77 MKINIT struct redirtab *redirlist;
80 * We keep track of whether or not fd0 has been redirected. This is for
81 * background commands, where we want to redirect fd0 to /dev/null only
82 * if it hasn't already been redirected.
84 static int fd0_redirected = 0;
86 static void openredirect(union node *, char[10 ]);
87 static int openhere(union node *);
91 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
92 * old file descriptors are stashed away so that the redirection can be
93 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
94 * standard output, and the standard error if it becomes a duplicate of
95 * stdout, is saved in memory.
99 redirect(union node *redir, int flags)
102 struct redirtab *sv = NULL;
105 char memory[10]; /* file descriptors to write to memory */
107 for (i = 10 ; --i >= 0 ; )
109 memory[1] = flags & REDIR_BACKQ;
110 if (flags & REDIR_PUSH) {
111 sv = ckmalloc(sizeof (struct redirtab));
112 for (i = 0 ; i < 10 ; i++)
113 sv->renamed[i] = EMPTY;
114 sv->next = redirlist;
117 for (n = redir ; n ; n = n->nfile.next) {
119 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
121 continue; /* redirect from/to same file descriptor */
123 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
125 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
132 error("%d: %s", fd, strerror(errno));
136 (void)fcntl(i, F_SETFD, FD_CLOEXEC);
142 openredirect(n, memory);
152 openredirect(union node *redir, char memory[10])
155 int fd = redir->nfile.fd;
161 * We suppress interrupts so that we won't leave open file
162 * descriptors around. Because the signal handler remains
163 * installed and we do not use system call restart, interrupts
164 * will still abort blocking opens such as fifos (they will fail
165 * with EINTR). There is, however, a race condition if an interrupt
166 * arrives after INTOFF and before open blocks.
170 switch (redir->nfile.type) {
172 fname = redir->nfile.expfname;
173 if ((f = open(fname, O_RDONLY)) < 0)
174 error("cannot open %s: %s", fname, strerror(errno));
177 if (dup2(f, fd) == -1) {
180 error("%d: %s", fd, strerror(e));
186 fname = redir->nfile.expfname;
187 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
188 error("cannot create %s: %s", fname, strerror(errno));
192 fname = redir->nfile.expfname;
193 if (stat(fname, &sb) == -1) {
194 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
195 error("cannot create %s: %s", fname, strerror(errno));
196 } else if (!S_ISREG(sb.st_mode)) {
197 if ((f = open(fname, O_WRONLY, 0666)) < 0)
198 error("cannot create %s: %s", fname, strerror(errno));
199 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
201 error("cannot create %s: %s", fname,
205 error("cannot create %s: %s", fname,
211 fname = redir->nfile.expfname;
212 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
213 error("cannot create %s: %s", fname, strerror(errno));
216 fname = redir->nfile.expfname;
217 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
218 error("cannot create %s: %s", fname, strerror(errno));
222 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
223 if (memory[redir->ndup.dupfd])
226 if (dup2(redir->ndup.dupfd, fd) < 0)
227 error("%d: %s", redir->ndup.dupfd,
246 * Handle here documents. Normally we fork off a process to write the
247 * data to a pipe. If the document is short, we can stuff the data in
248 * the pipe without forking.
252 openhere(union node *redir)
258 error("Pipe call failed: %s", strerror(errno));
259 if (redir->type == NHERE) {
260 len = strlen(redir->nhere.doc->narg.text);
261 if (len <= PIPESIZE) {
262 xwrite(pip[1], redir->nhere.doc->narg.text, len);
266 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
268 signal(SIGINT, SIG_IGN);
269 signal(SIGQUIT, SIG_IGN);
270 signal(SIGHUP, SIG_IGN);
271 signal(SIGTSTP, SIG_IGN);
272 signal(SIGPIPE, SIG_DFL);
273 if (redir->type == NHERE)
274 xwrite(pip[1], redir->nhere.doc->narg.text, len);
276 expandhere(redir->nhere.doc, pip[1]);
287 * Undo the effects of the last redirection.
293 struct redirtab *rp = redirlist;
296 for (i = 0 ; i < 10 ; i++) {
297 if (rp->renamed[i] != EMPTY) {
300 if (rp->renamed[i] >= 0) {
301 dup2(rp->renamed[i], i);
302 close(rp->renamed[i]);
309 redirlist = rp->next;
315 * Undo all redirections. Called on error or interrupt.
329 /* Return true if fd 0 has already been redirected at least once. */
331 fd0_redirected_p(void)
333 return fd0_redirected != 0;
337 * Discard all saved file descriptors.
346 for (rp = redirlist ; rp ; rp = rp->next) {
347 for (i = 0 ; i < 10 ; i++) {
348 if (rp->renamed[i] >= 0) {
349 close(rp->renamed[i]);
351 rp->renamed[i] = EMPTY;