1 | /* $NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $ */
|
---|
2 |
|
---|
3 | /*-
|
---|
4 | * Copyright (c) 1991, 1993
|
---|
5 | * The Regents of the University of California. All rights reserved.
|
---|
6 | *
|
---|
7 | * This code is derived from software contributed to Berkeley by
|
---|
8 | * Kenneth Almquist.
|
---|
9 | *
|
---|
10 | * Redistribution and use in source and binary forms, with or without
|
---|
11 | * modification, are permitted provided that the following conditions
|
---|
12 | * are met:
|
---|
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.
|
---|
21 | *
|
---|
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
|
---|
32 | * SUCH DAMAGE.
|
---|
33 | */
|
---|
34 |
|
---|
35 | #ifdef HAVE_SYS_CDEFS_H
|
---|
36 | #include <sys/cdefs.h>
|
---|
37 | #endif
|
---|
38 | #ifndef lint
|
---|
39 | #if 0
|
---|
40 | static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
|
---|
41 | #else
|
---|
42 | __RCSID("$NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $");
|
---|
43 | #endif
|
---|
44 | #endif /* not lint */
|
---|
45 |
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <sys/param.h> /* PIPE_BUF */
|
---|
48 | #include <signal.h>
|
---|
49 | #include <string.h>
|
---|
50 | #include <fcntl.h>
|
---|
51 | #include <errno.h>
|
---|
52 | #include <unistd.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * Code for dealing with input/output redirection.
|
---|
57 | */
|
---|
58 |
|
---|
59 | #include "main.h"
|
---|
60 | #include "shell.h"
|
---|
61 | #include "nodes.h"
|
---|
62 | #include "jobs.h"
|
---|
63 | #include "options.h"
|
---|
64 | #include "expand.h"
|
---|
65 | #include "redir.h"
|
---|
66 | #include "output.h"
|
---|
67 | #include "memalloc.h"
|
---|
68 | #include "error.h"
|
---|
69 | #include "shinstance.h"
|
---|
70 |
|
---|
71 |
|
---|
72 | #define EMPTY -2 /* marks an unused slot in redirtab */
|
---|
73 | #ifndef PIPE_BUF
|
---|
74 | # define PIPESIZE 4096 /* amount of buffering in a pipe */
|
---|
75 | #else
|
---|
76 | # define PIPESIZE PIPE_BUF
|
---|
77 | #endif
|
---|
78 |
|
---|
79 |
|
---|
80 | MKINIT
|
---|
81 | struct redirtab {
|
---|
82 | struct redirtab *next;
|
---|
83 | short renamed[10];
|
---|
84 | };
|
---|
85 |
|
---|
86 |
|
---|
87 | //MKINIT struct redirtab *redirlist;
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * We keep track of whether or not fd0 has been redirected. This is for
|
---|
91 | * background commands, where we want to redirect fd0 to /dev/null only
|
---|
92 | * if it hasn't already been redirected.
|
---|
93 | */
|
---|
94 | //int fd0_redirected = 0;
|
---|
95 |
|
---|
96 | STATIC void openredirect(shinstance *, union node *, char[10], int);
|
---|
97 | STATIC int openhere(shinstance *, union node *);
|
---|
98 |
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Process a list of redirection commands. If the REDIR_PUSH flag is set,
|
---|
102 | * old file descriptors are stashed away so that the redirection can be
|
---|
103 | * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
|
---|
104 | * standard output, and the standard error if it becomes a duplicate of
|
---|
105 | * stdout, is saved in memory.
|
---|
106 | */
|
---|
107 |
|
---|
108 | void
|
---|
109 | redirect(shinstance *psh, union node *redir, int flags)
|
---|
110 | {
|
---|
111 | union node *n;
|
---|
112 | struct redirtab *sv = NULL;
|
---|
113 | int i;
|
---|
114 | int fd;
|
---|
115 | int try;
|
---|
116 | char memory[10]; /* file descriptors to write to memory */
|
---|
117 |
|
---|
118 | for (i = 10 ; --i >= 0 ; )
|
---|
119 | memory[i] = 0;
|
---|
120 | memory[1] = flags & REDIR_BACKQ;
|
---|
121 | if (flags & REDIR_PUSH) {
|
---|
122 | /* We don't have to worry about REDIR_VFORK here, as
|
---|
123 | * flags & REDIR_PUSH is never true if REDIR_VFORK is set.
|
---|
124 | */
|
---|
125 | sv = ckmalloc(sizeof (struct redirtab));
|
---|
126 | for (i = 0 ; i < 10 ; i++)
|
---|
127 | sv->renamed[i] = EMPTY;
|
---|
128 | sv->next = psh->redirlist;
|
---|
129 | psh->redirlist = sv;
|
---|
130 | }
|
---|
131 | for (n = redir ; n ; n = n->nfile.next) {
|
---|
132 | fd = n->nfile.fd;
|
---|
133 | try = 0;
|
---|
134 | if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
|
---|
135 | n->ndup.dupfd == fd)
|
---|
136 | continue; /* redirect from/to same file descriptor */
|
---|
137 |
|
---|
138 | if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
|
---|
139 | INTOFF;
|
---|
140 | again:
|
---|
141 | if ((i = shfile_fcntl(&psh->fdtab, fd, F_DUPFD, 10)) == -1) {
|
---|
142 | switch (errno) {
|
---|
143 | case EBADF:
|
---|
144 | if (!try) {
|
---|
145 | openredirect(psh, n, memory, flags);
|
---|
146 | try++;
|
---|
147 | goto again;
|
---|
148 | }
|
---|
149 | /* FALLTHROUGH*/
|
---|
150 | default:
|
---|
151 | INTON;
|
---|
152 | error(psh, "%d: %s", fd, strerror(errno));
|
---|
153 | /* NOTREACHED */
|
---|
154 | }
|
---|
155 | }
|
---|
156 | if (!try) {
|
---|
157 | sv->renamed[fd] = i;
|
---|
158 | shfile_close(&psh->fdtab, fd);
|
---|
159 | }
|
---|
160 | INTON;
|
---|
161 | } else {
|
---|
162 | shfile_close(&psh->fdtab, fd);
|
---|
163 | }
|
---|
164 | if (fd == 0)
|
---|
165 | psh->fd0_redirected++;
|
---|
166 | if (!try)
|
---|
167 | openredirect(psh, n, memory, flags);
|
---|
168 | }
|
---|
169 | if (memory[1])
|
---|
170 | psh->out1 = &psh->memout;
|
---|
171 | if (memory[2])
|
---|
172 | psh->out2 = &psh->memout;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | STATIC void
|
---|
177 | openredirect(shinstance *psh, union node *redir, char memory[10], int flags)
|
---|
178 | {
|
---|
179 | int fd = redir->nfile.fd;
|
---|
180 | char *fname;
|
---|
181 | int f;
|
---|
182 | int oflags = O_WRONLY|O_CREAT|O_TRUNC, eflags;
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * We suppress interrupts so that we won't leave open file
|
---|
186 | * descriptors around. This may not be such a good idea because
|
---|
187 | * an open of a device or a fifo can block indefinitely.
|
---|
188 | */
|
---|
189 | INTOFF;
|
---|
190 | memory[fd] = 0;
|
---|
191 | switch (redir->nfile.type) {
|
---|
192 | case NFROM:
|
---|
193 | fname = redir->nfile.expfname;
|
---|
194 | if (flags & REDIR_VFORK)
|
---|
195 | eflags = O_NONBLOCK;
|
---|
196 | else
|
---|
197 | eflags = 0;
|
---|
198 | if ((f = open(fname, O_RDONLY|eflags)) < 0)
|
---|
199 | goto eopen;
|
---|
200 | if (eflags)
|
---|
201 | (void)shfile_fcntl(&psh->fdtab, f, F_SETFL, shfile_fcntl(&psh->fdtab, f, F_GETFL, 0) & ~eflags);
|
---|
202 | break;
|
---|
203 | case NFROMTO:
|
---|
204 | fname = redir->nfile.expfname;
|
---|
205 | if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
|
---|
206 | goto ecreate;
|
---|
207 | break;
|
---|
208 | case NTO:
|
---|
209 | if (Cflag(psh))
|
---|
210 | oflags |= O_EXCL;
|
---|
211 | /* FALLTHROUGH */
|
---|
212 | case NCLOBBER:
|
---|
213 | fname = redir->nfile.expfname;
|
---|
214 | if ((f = open(fname, oflags, 0666)) < 0)
|
---|
215 | goto ecreate;
|
---|
216 | break;
|
---|
217 | case NAPPEND:
|
---|
218 | fname = redir->nfile.expfname;
|
---|
219 | if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
|
---|
220 | goto ecreate;
|
---|
221 | break;
|
---|
222 | case NTOFD:
|
---|
223 | case NFROMFD:
|
---|
224 | if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
|
---|
225 | if (memory[redir->ndup.dupfd])
|
---|
226 | memory[fd] = 1;
|
---|
227 | else
|
---|
228 | copyfd(psh, redir->ndup.dupfd, fd);
|
---|
229 | }
|
---|
230 | INTON;
|
---|
231 | return;
|
---|
232 | case NHERE:
|
---|
233 | case NXHERE:
|
---|
234 | f = openhere(psh, redir);
|
---|
235 | break;
|
---|
236 | default:
|
---|
237 | abort();
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (f != fd) {
|
---|
241 | copyfd(psh, f, fd);
|
---|
242 | shfile_close(&psh->fdtab, f);
|
---|
243 | }
|
---|
244 | INTON;
|
---|
245 | return;
|
---|
246 | ecreate:
|
---|
247 | error(psh, "cannot create %s: %s", fname, errmsg(psh, errno, E_CREAT));
|
---|
248 | eopen:
|
---|
249 | error(psh, "cannot open %s: %s", fname, errmsg(psh, errno, E_OPEN));
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Handle here documents. Normally we fork off a process to write the
|
---|
255 | * data to a pipe. If the document is short, we can stuff the data in
|
---|
256 | * the pipe without forking.
|
---|
257 | */
|
---|
258 |
|
---|
259 | STATIC int
|
---|
260 | openhere(shinstance *psh, union node *redir)
|
---|
261 | {
|
---|
262 | int pip[2];
|
---|
263 | size_t len = 0;
|
---|
264 |
|
---|
265 | if (shfile_pipe(&psh->fdtab, pip) < 0)
|
---|
266 | error(psh, "Pipe call failed");
|
---|
267 | if (redir->type == NHERE) {
|
---|
268 | len = strlen(redir->nhere.doc->narg.text);
|
---|
269 | if (len <= PIPESIZE) {
|
---|
270 | xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
|
---|
271 | goto out;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | if (forkshell(psh, (struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
|
---|
275 | shfile_close(&psh->fdtab, pip[0]);
|
---|
276 | sh_signal(psh, SIGINT, SH_SIG_IGN);
|
---|
277 | sh_signal(psh, SIGQUIT, SH_SIG_IGN);
|
---|
278 | sh_signal(psh, SIGHUP, SH_SIG_IGN);
|
---|
279 | #ifdef SIGTSTP
|
---|
280 | sh_signal(psh, SIGTSTP, SH_SIG_IGN);
|
---|
281 | #endif
|
---|
282 | sh_signal(psh, SIGPIPE, SH_SIG_DFL);
|
---|
283 | if (redir->type == NHERE)
|
---|
284 | xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
|
---|
285 | else
|
---|
286 | expandhere(psh, redir->nhere.doc, pip[1]);
|
---|
287 | sh__exit(psh, 0);
|
---|
288 | }
|
---|
289 | out:
|
---|
290 | shfile_close(&psh->fdtab, pip[1]);
|
---|
291 | return pip[0];
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Undo the effects of the last redirection.
|
---|
298 | */
|
---|
299 |
|
---|
300 | void
|
---|
301 | popredir(shinstance *psh)
|
---|
302 | {
|
---|
303 | struct redirtab *rp = psh->redirlist;
|
---|
304 | int i;
|
---|
305 |
|
---|
306 | for (i = 0 ; i < 10 ; i++) {
|
---|
307 | if (rp->renamed[i] != EMPTY) {
|
---|
308 | if (i == 0)
|
---|
309 | psh->fd0_redirected--;
|
---|
310 | shfile_close(&psh->fdtab, i);
|
---|
311 | if (rp->renamed[i] >= 0) {
|
---|
312 | copyfd(psh, rp->renamed[i], i);
|
---|
313 | shfile_close(&psh->fdtab, rp->renamed[i]);
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|
317 | INTOFF;
|
---|
318 | psh->redirlist = rp->next;
|
---|
319 | ckfree(rp);
|
---|
320 | INTON;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * Undo all redirections. Called on error or interrupt.
|
---|
325 | */
|
---|
326 |
|
---|
327 | #ifdef mkinit
|
---|
328 |
|
---|
329 | INCLUDE "redir.h"
|
---|
330 |
|
---|
331 | RESET {
|
---|
332 | while (psh->redirlist)
|
---|
333 | popredir(psh);
|
---|
334 | }
|
---|
335 |
|
---|
336 | SHELLPROC {
|
---|
337 | clearredir(psh, 0);
|
---|
338 | }
|
---|
339 |
|
---|
340 | #endif
|
---|
341 |
|
---|
342 | /* Return true if fd 0 has already been redirected at least once. */
|
---|
343 | int
|
---|
344 | fd0_redirected_p(shinstance *psh) {
|
---|
345 | return psh->fd0_redirected != 0;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Discard all saved file descriptors.
|
---|
350 | */
|
---|
351 |
|
---|
352 | void
|
---|
353 | clearredir(shinstance *psh, int vforked)
|
---|
354 | {
|
---|
355 | struct redirtab *rp;
|
---|
356 | int i;
|
---|
357 |
|
---|
358 | for (rp = psh->redirlist ; rp ; rp = rp->next) {
|
---|
359 | for (i = 0 ; i < 10 ; i++) {
|
---|
360 | if (rp->renamed[i] >= 0) {
|
---|
361 | shfile_close(&psh->fdtab, rp->renamed[i]);
|
---|
362 | }
|
---|
363 | if (!vforked)
|
---|
364 | rp->renamed[i] = EMPTY;
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Copy a file descriptor to be >= to. Returns -1
|
---|
373 | * if the source file descriptor is closed, EMPTY if there are no unused
|
---|
374 | * file descriptors left.
|
---|
375 | */
|
---|
376 |
|
---|
377 | int
|
---|
378 | copyfd(shinstance *psh, int from, int to)
|
---|
379 | {
|
---|
380 | int newfd;
|
---|
381 |
|
---|
382 | newfd = shfile_fcntl(&psh->fdtab, from, F_DUPFD, to);
|
---|
383 | if (newfd < 0) {
|
---|
384 | if (errno == EMFILE)
|
---|
385 | return EMPTY;
|
---|
386 | else
|
---|
387 | error(psh, "%d: %s", from, strerror(errno));
|
---|
388 | }
|
---|
389 | return newfd;
|
---|
390 | }
|
---|