2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
53 * Code for dealing with input/output redirection.
67 #define EMPTY -2 /* marks an unused slot in redirtab */
68 #define CLOSED -1 /* fd was not open before redir */
72 struct redirtab *next;
75 unsigned int empty_redirs;
79 static struct redirtab *redirlist;
82 * We keep track of whether or not fd0 has been redirected. This is for
83 * background commands, where we want to redirect fd0 to /dev/null only
84 * if it hasn't already been redirected.
86 static int fd0_redirected = 0;
88 /* Number of redirtabs that have not been allocated. */
89 static unsigned int empty_redirs = 0;
91 static void openredirect(union node *, char[10 ]);
92 static int openhere(union node *);
96 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
97 * old file descriptors are stashed away so that the redirection can be
98 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
99 * standard output, and the standard error if it becomes a duplicate of
100 * stdout, is saved in memory.
102 * We suppress interrupts so that we won't leave open file
103 * descriptors around. Because the signal handler remains
104 * installed and we do not use system call restart, interrupts
105 * will still abort blocking opens such as fifos (they will fail
106 * with EINTR). There is, however, a race condition if an interrupt
107 * arrives after INTOFF and before open blocks.
111 redirect(union node *redir, int flags)
114 struct redirtab *sv = NULL;
117 char memory[10]; /* file descriptors to write to memory */
120 for (i = 10 ; --i >= 0 ; )
122 memory[1] = flags & REDIR_BACKQ;
123 if (flags & REDIR_PUSH) {
126 sv = ckmalloc(sizeof (struct redirtab));
127 for (i = 0 ; i < 10 ; i++)
128 sv->renamed[i] = EMPTY;
129 sv->fd0_redirected = fd0_redirected;
130 sv->empty_redirs = empty_redirs - 1;
131 sv->next = redirlist;
136 for (n = redir ; n ; n = n->nfile.next) {
140 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
142 continue; /* redirect from/to same file descriptor */
144 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
146 if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) {
153 error("%d: %s", fd, strerror(errno));
160 openredirect(n, memory);
173 openredirect(union node *redir, char memory[10])
176 int fd = redir->nfile.fd;
182 switch (redir->nfile.type) {
184 fname = redir->nfile.expfname;
185 if ((f = open(fname, O_RDONLY)) < 0)
186 error("cannot open %s: %s", fname, strerror(errno));
189 fname = redir->nfile.expfname;
190 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
191 error("cannot create %s: %s", fname, strerror(errno));
195 fname = redir->nfile.expfname;
196 if (stat(fname, &sb) == -1) {
197 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
198 error("cannot create %s: %s", fname, strerror(errno));
199 } else if (!S_ISREG(sb.st_mode)) {
200 if ((f = open(fname, O_WRONLY, 0666)) < 0)
201 error("cannot create %s: %s", fname, strerror(errno));
202 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
204 error("cannot create %s: %s", fname,
208 error("cannot create %s: %s", fname,
214 fname = redir->nfile.expfname;
215 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
216 error("cannot create %s: %s", fname, strerror(errno));
219 fname = redir->nfile.expfname;
220 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
221 error("cannot create %s: %s", fname, strerror(errno));
225 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
226 if (memory[redir->ndup.dupfd])
229 if (dup2(redir->ndup.dupfd, fd) < 0)
230 error("%d: %s", redir->ndup.dupfd,
245 if (dup2(f, fd) == -1) {
248 error("%d: %s", fd, strerror(e));
256 * Handle here documents. Normally we fork off a process to write the
257 * data to a pipe. If the document is short, we can stuff the data in
258 * the pipe without forking.
262 openhere(union node *redir)
271 error("Pipe call failed: %s", strerror(errno));
273 if (redir->type == NXHERE)
274 p = redir->nhere.expdoc;
276 p = redir->nhere.doc->narg.text;
280 flags = fcntl(pip[1], F_GETFL, 0);
281 if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) {
282 written = write(pip[1], p, len);
285 if ((size_t)written == len)
287 fcntl(pip[1], F_SETFL, flags);
290 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
292 signal(SIGINT, SIG_IGN);
293 signal(SIGQUIT, SIG_IGN);
294 signal(SIGHUP, SIG_IGN);
295 signal(SIGTSTP, SIG_IGN);
296 signal(SIGPIPE, SIG_DFL);
297 xwrite(pip[1], p + written, len - written);
308 * Undo the effects of the last redirection.
314 struct redirtab *rp = redirlist;
318 if (empty_redirs > 0) {
323 for (i = 0 ; i < 10 ; i++) {
324 if (rp->renamed[i] != EMPTY) {
325 if (rp->renamed[i] >= 0) {
326 dup2(rp->renamed[i], i);
327 close(rp->renamed[i]);
333 fd0_redirected = rp->fd0_redirected;
334 empty_redirs = rp->empty_redirs;
335 redirlist = rp->next;
340 /* Return true if fd 0 has already been redirected at least once. */
342 fd0_redirected_p(void)
344 return fd0_redirected != 0;
348 * Discard all saved file descriptors.
357 for (rp = redirlist ; rp ; rp = rp->next) {
358 for (i = 0 ; i < 10 ; i++) {
359 if (rp->renamed[i] >= 0) {
360 close(rp->renamed[i]);
362 rp->renamed[i] = EMPTY;