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