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. 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 */
70 struct redirtab
*next
;
76 static 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.
96 * We suppress interrupts so that we won't leave open file
97 * descriptors around. Because the signal handler remains
98 * installed and we do not use system call restart, interrupts
99 * will still abort blocking opens such as fifos (they will fail
100 * with EINTR). There is, however, a race condition if an interrupt
101 * arrives after INTOFF and before open blocks.
105 redirect(union node
*redir
, int flags
)
108 struct redirtab
*sv
= NULL
;
111 char memory
[10]; /* file descriptors to write to memory */
114 for (i
= 10 ; --i
>= 0 ; )
116 memory
[1] = flags
& REDIR_BACKQ
;
117 if (flags
& REDIR_PUSH
) {
118 sv
= ckmalloc(sizeof (struct redirtab
));
119 for (i
= 0 ; i
< 10 ; i
++)
120 sv
->renamed
[i
] = EMPTY
;
121 sv
->fd0_redirected
= fd0_redirected
;
122 sv
->next
= redirlist
;
125 for (n
= redir
; n
; n
= n
->nfile
.next
) {
129 if ((n
->nfile
.type
== NTOFD
|| n
->nfile
.type
== NFROMFD
) &&
131 continue; /* redirect from/to same file descriptor */
133 if ((flags
& REDIR_PUSH
) && sv
->renamed
[fd
] == EMPTY
) {
135 if ((i
= fcntl(fd
, F_DUPFD_CLOEXEC_MAYBE
, 10)) == -1) {
142 error("%d: %s", fd
, strerror(errno
));
146 #if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC)
148 fcntl(i
, F_SETFD
, FD_CLOEXEC
);
154 openredirect(n
, memory
);
167 openredirect(union node
*redir
, char memory
[10])
170 int fd
= redir
->nfile
.fd
;
176 switch (redir
->nfile
.type
) {
178 fname
= redir
->nfile
.expfname
;
179 if ((f
= open(fname
, O_RDONLY
)) < 0)
180 error("cannot open %s: %s", fname
, strerror(errno
));
183 fname
= redir
->nfile
.expfname
;
184 if ((f
= open(fname
, O_RDWR
|O_CREAT
, 0666)) < 0)
185 error("cannot create %s: %s", fname
, strerror(errno
));
189 fname
= redir
->nfile
.expfname
;
190 if (stat(fname
, &sb
) == -1) {
191 if ((f
= open(fname
, O_WRONLY
|O_CREAT
|O_EXCL
, 0666)) < 0)
192 error("cannot create %s: %s", fname
, strerror(errno
));
193 } else if (!S_ISREG(sb
.st_mode
)) {
194 if ((f
= open(fname
, O_WRONLY
, 0666)) < 0)
195 error("cannot create %s: %s", fname
, strerror(errno
));
196 if (fstat(f
, &sb
) != -1 && S_ISREG(sb
.st_mode
)) {
198 error("cannot create %s: %s", fname
,
202 error("cannot create %s: %s", fname
,
208 fname
= redir
->nfile
.expfname
;
209 if ((f
= open(fname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666)) < 0)
210 error("cannot create %s: %s", fname
, strerror(errno
));
213 fname
= redir
->nfile
.expfname
;
214 if ((f
= open(fname
, O_WRONLY
|O_CREAT
|O_APPEND
, 0666)) < 0)
215 error("cannot create %s: %s", fname
, strerror(errno
));
219 if (redir
->ndup
.dupfd
>= 0) { /* if not ">&-" */
220 if (memory
[redir
->ndup
.dupfd
])
223 if (dup2(redir
->ndup
.dupfd
, fd
) < 0)
224 error("%d: %s", redir
->ndup
.dupfd
,
239 if (dup2(f
, fd
) == -1) {
242 error("%d: %s", fd
, strerror(e
));
250 * Handle here documents. Normally we fork off a process to write the
251 * data to a pipe. If the document is short, we can stuff the data in
252 * the pipe without forking.
256 openhere(union node
*redir
)
265 error("Pipe call failed: %s", strerror(errno
));
267 if (redir
->type
== NXHERE
)
268 p
= redir
->nhere
.expdoc
;
270 p
= redir
->nhere
.doc
->narg
.text
;
274 flags
= fcntl(pip
[1], F_GETFL
, 0);
275 if (flags
!= -1 && fcntl(pip
[1], F_SETFL
, flags
| O_NONBLOCK
) != -1) {
276 written
= write(pip
[1], p
, len
);
279 if ((size_t)written
== len
)
281 fcntl(pip
[1], F_SETFL
, flags
);
284 if (forkshell((struct job
*)NULL
, (union node
*)NULL
, FORK_NOJOB
) == 0) {
286 signal(SIGINT
, SIG_IGN
);
287 signal(SIGQUIT
, SIG_IGN
);
288 signal(SIGHUP
, SIG_IGN
);
289 signal(SIGTSTP
, SIG_IGN
);
290 signal(SIGPIPE
, SIG_DFL
);
291 xwrite(pip
[1], p
+ written
, len
- written
);
302 * Undo the effects of the last redirection.
308 struct redirtab
*rp
= redirlist
;
311 for (i
= 0 ; i
< 10 ; i
++) {
312 if (rp
->renamed
[i
] != EMPTY
) {
313 if (rp
->renamed
[i
] >= 0) {
314 dup2(rp
->renamed
[i
], i
);
315 close(rp
->renamed
[i
]);
322 fd0_redirected
= rp
->fd0_redirected
;
323 redirlist
= rp
->next
;
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
;