redir.c revision 1.16 1 1.16 christos /* $NetBSD: redir.c,v 1.16 1997/07/04 21:02:21 christos 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.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.16 christos #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.11 cgd #if 0
42 1.12 christos static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
43 1.11 cgd #else
44 1.16 christos __RCSID("$NetBSD: redir.c,v 1.16 1997/07/04 21:02:21 christos Exp $");
45 1.11 cgd #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.12 christos #include <sys/types.h>
49 1.12 christos #include <signal.h>
50 1.12 christos #include <string.h>
51 1.12 christos #include <fcntl.h>
52 1.12 christos #include <errno.h>
53 1.12 christos #include <unistd.h>
54 1.12 christos #include <stdlib.h>
55 1.12 christos
56 1.1 cgd /*
57 1.1 cgd * Code for dealing with input/output redirection.
58 1.1 cgd */
59 1.1 cgd
60 1.1 cgd #include "shell.h"
61 1.1 cgd #include "nodes.h"
62 1.1 cgd #include "jobs.h"
63 1.1 cgd #include "expand.h"
64 1.1 cgd #include "redir.h"
65 1.1 cgd #include "output.h"
66 1.1 cgd #include "memalloc.h"
67 1.1 cgd #include "error.h"
68 1.1 cgd
69 1.1 cgd
70 1.1 cgd #define EMPTY -2 /* marks an unused slot in redirtab */
71 1.1 cgd #define PIPESIZE 4096 /* amount of buffering in a pipe */
72 1.1 cgd
73 1.1 cgd
74 1.1 cgd MKINIT
75 1.1 cgd struct redirtab {
76 1.1 cgd struct redirtab *next;
77 1.1 cgd short renamed[10];
78 1.1 cgd };
79 1.1 cgd
80 1.1 cgd
81 1.1 cgd MKINIT struct redirtab *redirlist;
82 1.1 cgd
83 1.13 christos /*
84 1.7 jtc * We keep track of whether or not fd0 has been redirected. This is for
85 1.7 jtc * background commands, where we want to redirect fd0 to /dev/null only
86 1.13 christos * if it hasn't already been redirected.
87 1.7 jtc */
88 1.4 sef int fd0_redirected = 0;
89 1.1 cgd
90 1.12 christos STATIC void openredirect __P((union node *, char[10 ]));
91 1.12 christos STATIC int openhere __P((union node *));
92 1.1 cgd
93 1.1 cgd
94 1.1 cgd /*
95 1.1 cgd * Process a list of redirection commands. If the REDIR_PUSH flag is set,
96 1.1 cgd * old file descriptors are stashed away so that the redirection can be
97 1.1 cgd * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
98 1.1 cgd * standard output, and the standard error if it becomes a duplicate of
99 1.1 cgd * stdout, is saved in memory.
100 1.1 cgd */
101 1.1 cgd
102 1.1 cgd void
103 1.1 cgd redirect(redir, flags)
104 1.1 cgd union node *redir;
105 1.1 cgd int flags;
106 1.1 cgd {
107 1.1 cgd union node *n;
108 1.16 christos struct redirtab *sv = NULL;
109 1.1 cgd int i;
110 1.1 cgd int fd;
111 1.15 christos int try;
112 1.13 christos char memory[10]; /* file descriptors to write to memory */
113 1.1 cgd
114 1.1 cgd for (i = 10 ; --i >= 0 ; )
115 1.1 cgd memory[i] = 0;
116 1.1 cgd memory[1] = flags & REDIR_BACKQ;
117 1.1 cgd if (flags & REDIR_PUSH) {
118 1.1 cgd sv = ckmalloc(sizeof (struct redirtab));
119 1.1 cgd for (i = 0 ; i < 10 ; i++)
120 1.1 cgd sv->renamed[i] = EMPTY;
121 1.1 cgd sv->next = redirlist;
122 1.1 cgd redirlist = sv;
123 1.1 cgd }
124 1.1 cgd for (n = redir ; n ; n = n->nfile.next) {
125 1.1 cgd fd = n->nfile.fd;
126 1.15 christos try = 0;
127 1.13 christos if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
128 1.13 christos n->ndup.dupfd == fd)
129 1.13 christos continue; /* redirect from/to same file descriptor */
130 1.15 christos
131 1.1 cgd if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
132 1.1 cgd INTOFF;
133 1.15 christos again:
134 1.15 christos if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
135 1.15 christos switch (errno) {
136 1.15 christos case EBADF:
137 1.15 christos if (!try) {
138 1.15 christos openredirect(n, memory);
139 1.15 christos try++;
140 1.15 christos goto again;
141 1.15 christos }
142 1.15 christos /* FALLTHROUGH*/
143 1.15 christos default:
144 1.15 christos INTON;
145 1.15 christos error("%d: %s", fd, strerror(errno));
146 1.15 christos break;
147 1.15 christos }
148 1.15 christos }
149 1.15 christos if (!try) {
150 1.1 cgd sv->renamed[fd] = i;
151 1.1 cgd close(fd);
152 1.1 cgd }
153 1.1 cgd INTON;
154 1.1 cgd } else {
155 1.1 cgd close(fd);
156 1.1 cgd }
157 1.7 jtc if (fd == 0)
158 1.7 jtc fd0_redirected++;
159 1.15 christos if (!try)
160 1.15 christos openredirect(n, memory);
161 1.1 cgd }
162 1.1 cgd if (memory[1])
163 1.1 cgd out1 = &memout;
164 1.1 cgd if (memory[2])
165 1.1 cgd out2 = &memout;
166 1.1 cgd }
167 1.1 cgd
168 1.1 cgd
169 1.1 cgd STATIC void
170 1.1 cgd openredirect(redir, memory)
171 1.1 cgd union node *redir;
172 1.1 cgd char memory[10];
173 1.1 cgd {
174 1.1 cgd int fd = redir->nfile.fd;
175 1.1 cgd char *fname;
176 1.1 cgd int f;
177 1.1 cgd
178 1.1 cgd /*
179 1.1 cgd * We suppress interrupts so that we won't leave open file
180 1.1 cgd * descriptors around. This may not be such a good idea because
181 1.1 cgd * an open of a device or a fifo can block indefinitely.
182 1.1 cgd */
183 1.1 cgd INTOFF;
184 1.1 cgd memory[fd] = 0;
185 1.1 cgd switch (redir->nfile.type) {
186 1.1 cgd case NFROM:
187 1.1 cgd fname = redir->nfile.expfname;
188 1.1 cgd if ((f = open(fname, O_RDONLY)) < 0)
189 1.1 cgd error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
190 1.1 cgd movefd:
191 1.1 cgd if (f != fd) {
192 1.1 cgd copyfd(f, fd);
193 1.1 cgd close(f);
194 1.1 cgd }
195 1.1 cgd break;
196 1.1 cgd case NTO:
197 1.1 cgd fname = redir->nfile.expfname;
198 1.1 cgd #ifdef O_CREAT
199 1.1 cgd if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
200 1.1 cgd error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
201 1.1 cgd #else
202 1.1 cgd if ((f = creat(fname, 0666)) < 0)
203 1.1 cgd error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
204 1.1 cgd #endif
205 1.1 cgd goto movefd;
206 1.1 cgd case NAPPEND:
207 1.1 cgd fname = redir->nfile.expfname;
208 1.1 cgd #ifdef O_APPEND
209 1.1 cgd if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
210 1.1 cgd error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
211 1.1 cgd #else
212 1.1 cgd if ((f = open(fname, O_WRONLY)) < 0
213 1.1 cgd && (f = creat(fname, 0666)) < 0)
214 1.1 cgd error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
215 1.7 jtc lseek(f, (off_t)0, 2);
216 1.1 cgd #endif
217 1.1 cgd goto movefd;
218 1.1 cgd case NTOFD:
219 1.1 cgd case NFROMFD:
220 1.1 cgd if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
221 1.1 cgd if (memory[redir->ndup.dupfd])
222 1.1 cgd memory[fd] = 1;
223 1.1 cgd else
224 1.1 cgd copyfd(redir->ndup.dupfd, fd);
225 1.1 cgd }
226 1.1 cgd break;
227 1.1 cgd case NHERE:
228 1.1 cgd case NXHERE:
229 1.1 cgd f = openhere(redir);
230 1.1 cgd goto movefd;
231 1.1 cgd default:
232 1.1 cgd abort();
233 1.1 cgd }
234 1.1 cgd INTON;
235 1.1 cgd }
236 1.1 cgd
237 1.1 cgd
238 1.1 cgd /*
239 1.1 cgd * Handle here documents. Normally we fork off a process to write the
240 1.1 cgd * data to a pipe. If the document is short, we can stuff the data in
241 1.1 cgd * the pipe without forking.
242 1.1 cgd */
243 1.1 cgd
244 1.1 cgd STATIC int
245 1.1 cgd openhere(redir)
246 1.1 cgd union node *redir;
247 1.1 cgd {
248 1.1 cgd int pip[2];
249 1.12 christos int len = 0;
250 1.1 cgd
251 1.1 cgd if (pipe(pip) < 0)
252 1.1 cgd error("Pipe call failed");
253 1.1 cgd if (redir->type == NHERE) {
254 1.1 cgd len = strlen(redir->nhere.doc->narg.text);
255 1.1 cgd if (len <= PIPESIZE) {
256 1.1 cgd xwrite(pip[1], redir->nhere.doc->narg.text, len);
257 1.1 cgd goto out;
258 1.1 cgd }
259 1.1 cgd }
260 1.1 cgd if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
261 1.1 cgd close(pip[0]);
262 1.1 cgd signal(SIGINT, SIG_IGN);
263 1.1 cgd signal(SIGQUIT, SIG_IGN);
264 1.1 cgd signal(SIGHUP, SIG_IGN);
265 1.1 cgd #ifdef SIGTSTP
266 1.1 cgd signal(SIGTSTP, SIG_IGN);
267 1.1 cgd #endif
268 1.1 cgd signal(SIGPIPE, SIG_DFL);
269 1.1 cgd if (redir->type == NHERE)
270 1.1 cgd xwrite(pip[1], redir->nhere.doc->narg.text, len);
271 1.1 cgd else
272 1.1 cgd expandhere(redir->nhere.doc, pip[1]);
273 1.1 cgd _exit(0);
274 1.1 cgd }
275 1.1 cgd out:
276 1.1 cgd close(pip[1]);
277 1.1 cgd return pip[0];
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd
281 1.1 cgd
282 1.1 cgd /*
283 1.1 cgd * Undo the effects of the last redirection.
284 1.1 cgd */
285 1.1 cgd
286 1.1 cgd void
287 1.1 cgd popredir() {
288 1.14 tls struct redirtab *rp = redirlist;
289 1.1 cgd int i;
290 1.1 cgd
291 1.1 cgd for (i = 0 ; i < 10 ; i++) {
292 1.1 cgd if (rp->renamed[i] != EMPTY) {
293 1.7 jtc if (i == 0)
294 1.7 jtc fd0_redirected--;
295 1.1 cgd close(i);
296 1.1 cgd if (rp->renamed[i] >= 0) {
297 1.1 cgd copyfd(rp->renamed[i], i);
298 1.1 cgd close(rp->renamed[i]);
299 1.1 cgd }
300 1.1 cgd }
301 1.1 cgd }
302 1.1 cgd INTOFF;
303 1.1 cgd redirlist = rp->next;
304 1.1 cgd ckfree(rp);
305 1.1 cgd INTON;
306 1.1 cgd }
307 1.1 cgd
308 1.1 cgd /*
309 1.1 cgd * Undo all redirections. Called on error or interrupt.
310 1.1 cgd */
311 1.1 cgd
312 1.1 cgd #ifdef mkinit
313 1.1 cgd
314 1.1 cgd INCLUDE "redir.h"
315 1.1 cgd
316 1.1 cgd RESET {
317 1.1 cgd while (redirlist)
318 1.1 cgd popredir();
319 1.1 cgd }
320 1.1 cgd
321 1.1 cgd SHELLPROC {
322 1.1 cgd clearredir();
323 1.1 cgd }
324 1.1 cgd
325 1.1 cgd #endif
326 1.1 cgd
327 1.7 jtc /* Return true if fd 0 has already been redirected at least once. */
328 1.7 jtc int
329 1.7 jtc fd0_redirected_p () {
330 1.7 jtc return fd0_redirected != 0;
331 1.7 jtc }
332 1.1 cgd
333 1.1 cgd /*
334 1.1 cgd * Discard all saved file descriptors.
335 1.1 cgd */
336 1.1 cgd
337 1.1 cgd void
338 1.1 cgd clearredir() {
339 1.14 tls struct redirtab *rp;
340 1.1 cgd int i;
341 1.1 cgd
342 1.1 cgd for (rp = redirlist ; rp ; rp = rp->next) {
343 1.1 cgd for (i = 0 ; i < 10 ; i++) {
344 1.1 cgd if (rp->renamed[i] >= 0) {
345 1.1 cgd close(rp->renamed[i]);
346 1.1 cgd }
347 1.1 cgd rp->renamed[i] = EMPTY;
348 1.1 cgd }
349 1.1 cgd }
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd
353 1.1 cgd
354 1.1 cgd /*
355 1.7 jtc * Copy a file descriptor to be >= to. Returns -1
356 1.1 cgd * if the source file descriptor is closed, EMPTY if there are no unused
357 1.1 cgd * file descriptors left.
358 1.1 cgd */
359 1.1 cgd
360 1.1 cgd int
361 1.13 christos copyfd(from, to)
362 1.9 cgd int from;
363 1.9 cgd int to;
364 1.9 cgd {
365 1.1 cgd int newfd;
366 1.1 cgd
367 1.1 cgd newfd = fcntl(from, F_DUPFD, to);
368 1.13 christos if (newfd < 0) {
369 1.13 christos if (errno == EMFILE)
370 1.13 christos return EMPTY;
371 1.13 christos else
372 1.13 christos error("%d: %s", from, strerror(errno));
373 1.13 christos }
374 1.1 cgd return newfd;
375 1.1 cgd }
376