source: trunk/src/kash/redir.c@ 3442

Last change on this file since 3442 was 3442, checked in by bird, 5 years ago

kash: Turns out the redirlist needs to be copied too.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
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
37static 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
74MKINIT
75struct 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
90STATIC void openredirect(shinstance *, union node *, char[10], int);
91STATIC int openhere(shinstance *, union node *);
92
93
94#ifndef SH_FORKED_MODE
95void
96subshellinitredir(shinstance *psh, shinstance *inherit)
97{
98 /* We can have a redirlist here if we're handling backtick while expanding
99 arguments, just copy it even if the subshell probably doesn't need it. */
100 struct redirtab *src = inherit->redirlist;
101 if (src)
102 {
103 struct redirtab **dstp = &psh->redirlist;
104 do
105 {
106 struct redirtab *dst = ckmalloc(psh, sizeof(dst));
107 memcpy(dst->renamed, src->renamed, sizeof(dst->renamed));
108 *dstp = dst;
109 dstp = &dst->next;
110 src = src->next;
111 } while (src);
112 *dstp = NULL;
113
114 psh->fd0_redirected = inherit->fd0_redirected;
115 }
116}
117#endif /* !SH_FORKED_MODE */
118
119
120/*
121 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
122 * old file descriptors are stashed away so that the redirection can be
123 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
124 * standard output, and the standard error if it becomes a duplicate of
125 * stdout, is saved in memory.
126 */
127
128void
129redirect(shinstance *psh, union node *redir, int flags)
130{
131 union node *n;
132 struct redirtab *sv = NULL;
133 int i;
134 int fd;
135 int try;
136 char memory[10]; /* file descriptors to write to memory */
137
138 for (i = 10 ; --i >= 0 ; )
139 memory[i] = 0;
140 memory[1] = flags & REDIR_BACKQ;
141 if (flags & REDIR_PUSH) {
142 sv = ckmalloc(psh, sizeof (struct redirtab));
143 for (i = 0 ; i < 10 ; i++)
144 sv->renamed[i] = EMPTY;
145 sv->next = psh->redirlist;
146 psh->redirlist = sv;
147 }
148 for (n = redir ; n ; n = n->nfile.next) {
149 fd = n->nfile.fd;
150 try = 0;
151 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
152 n->ndup.dupfd == fd)
153 continue; /* redirect from/to same file descriptor */
154
155 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
156 INTOFF;
157again:
158 if ((i = shfile_fcntl(&psh->fdtab, fd, F_DUPFD, 10)) == -1) {
159 switch (errno) {
160 case EBADF:
161 if (!try) {
162 openredirect(psh, n, memory, flags);
163 try++;
164 goto again;
165 }
166 /* FALLTHROUGH*/
167 default:
168 INTON;
169 error(psh, "%d: %s", fd, sh_strerror(psh, errno));
170 /* NOTREACHED */
171 }
172 }
173 if (!try) {
174 sv->renamed[fd] = i;
175 shfile_close(&psh->fdtab, fd);
176 }
177 INTON;
178 } else {
179 shfile_close(&psh->fdtab, fd);
180 }
181 if (fd == 0)
182 psh->fd0_redirected++;
183 if (!try)
184 openredirect(psh, n, memory, flags);
185 }
186 if (memory[1])
187 psh->out1 = &psh->memout;
188 if (memory[2])
189 psh->out2 = &psh->memout;
190}
191
192
193STATIC void
194openredirect(shinstance *psh, union node *redir, char memory[10], int flags)
195{
196 int fd = redir->nfile.fd;
197 char *fname;
198 int f;
199 int oflags = O_WRONLY|O_CREAT|O_TRUNC;
200
201 /*
202 * We suppress interrupts so that we won't leave open file
203 * descriptors around. This may not be such a good idea because
204 * an open of a device or a fifo can block indefinitely.
205 */
206 INTOFF;
207 memory[fd] = 0;
208 switch (redir->nfile.type) {
209 case NFROM:
210 fname = redir->nfile.expfname;
211 if ((f = shfile_open(&psh->fdtab, fname, O_RDONLY, 0)) < 0)
212 goto eopen;
213 break;
214 case NFROMTO:
215 fname = redir->nfile.expfname;
216 if ((f = shfile_open(&psh->fdtab, fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
217 goto ecreate;
218 break;
219 case NTO:
220 if (Cflag(psh))
221 oflags |= O_EXCL;
222 /* FALLTHROUGH */
223 case NCLOBBER:
224 fname = redir->nfile.expfname;
225 if ((f = shfile_open(&psh->fdtab, fname, oflags, 0666)) < 0)
226 goto ecreate;
227 break;
228 case NAPPEND:
229 fname = redir->nfile.expfname;
230 if ((f = shfile_open(&psh->fdtab, fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
231 goto ecreate;
232 break;
233 case NTOFD:
234 case NFROMFD:
235 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
236 if (memory[redir->ndup.dupfd])
237 memory[fd] = 1;
238 else
239 copyfd(psh, redir->ndup.dupfd, fd);
240 }
241 INTON;
242 return;
243 case NHERE:
244 case NXHERE:
245 f = openhere(psh, redir);
246 break;
247 default:
248 sh_abort(psh);
249 }
250
251 if (f != fd) {
252 movefd(psh, f, fd);
253 }
254 INTON;
255 return;
256ecreate:
257 error(psh, "cannot create %s: %s", fname, errmsg(psh, errno, E_CREAT));
258eopen:
259 error(psh, "cannot open %s: %s", fname, errmsg(psh, errno, E_OPEN));
260}
261
262#ifdef KASH_USE_FORKSHELL2
263struct openherechild
264{
265 int pip[2];
266 size_t len;
267};
268static int openhere_child(shinstance *psh, union node *n, void *argp)
269{
270 struct openherechild args = *(struct openherechild *)argp;
271
272 shfile_close(&psh->fdtab, args.pip[0]);
273 sh_signal(psh, SIGINT, SH_SIG_IGN);
274 sh_signal(psh, SIGQUIT, SH_SIG_IGN);
275 sh_signal(psh, SIGHUP, SH_SIG_IGN);
276# ifdef SIGTSTP
277 sh_signal(psh, SIGTSTP, SH_SIG_IGN);
278# endif
279 sh_signal(psh, SIGPIPE, SH_SIG_DFL);
280 if (n->type == NHERE)
281 xwrite(psh, args.pip[1], n->nhere.doc->narg.text, args.len);
282 else
283 expandhere(psh, n->nhere.doc, args.pip[1]);
284 return 0;
285}
286
287#endif /* KASH_USE_FORKSHELL2*/
288
289/*
290 * Handle here documents. Normally we fork off a process to write the
291 * data to a pipe. If the document is short, we can stuff the data in
292 * the pipe without forking.
293 */
294
295STATIC int
296openhere(shinstance *psh, union node *redir)
297{
298 int pip[2];
299 size_t len = 0;
300
301 if (shfile_pipe(&psh->fdtab, pip) < 0)
302 error(psh, "Pipe call failed");
303 if (redir->type == NHERE) {
304 len = strlen(redir->nhere.doc->narg.text);
305 if (len <= PIPESIZE) {
306 xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
307 goto out;
308 }
309 }
310#ifdef KASH_USE_FORKSHELL2
311 {
312 struct openherechild args;
313 args.pip[0] = pip[0];
314 args.pip[1] = pip[1];
315 args.len = len;
316 forkshell2(psh, (struct job *)NULL, (union node *)NULL,
317 FORK_NOJOB | FORK_JUST_IO,
318 openhere_child, redir, &args, sizeof(args), NULL);
319 }
320#else
321 if (forkshell(psh, (struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
322 shfile_close(&psh->fdtab, pip[0]);
323 sh_signal(psh, SIGINT, SH_SIG_IGN);
324 sh_signal(psh, SIGQUIT, SH_SIG_IGN);
325 sh_signal(psh, SIGHUP, SH_SIG_IGN);
326#ifdef SIGTSTP
327 sh_signal(psh, SIGTSTP, SH_SIG_IGN);
328#endif
329 sh_signal(psh, SIGPIPE, SH_SIG_DFL);
330 if (redir->type == NHERE)
331 xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
332 else
333 expandhere(psh, redir->nhere.doc, pip[1]);
334 sh__exit(psh, 0);
335 }
336#endif
337out:
338 shfile_close(&psh->fdtab, pip[1]);
339 return pip[0];
340}
341
342
343
344/*
345 * Undo the effects of the last redirection.
346 */
347
348void
349popredir(shinstance *psh)
350{
351 struct redirtab *rp = psh->redirlist;
352 int i;
353
354 for (i = 0 ; i < 10 ; i++) {
355 if (rp->renamed[i] != EMPTY) {
356 if (i == 0)
357 psh->fd0_redirected--;
358 if (rp->renamed[i] >= 0) {
359 movefd(psh, rp->renamed[i], i);
360 } else {
361 shfile_close(&psh->fdtab, i);
362 }
363 }
364 }
365 INTOFF;
366 psh->redirlist = rp->next;
367 ckfree(psh, rp);
368 INTON;
369}
370
371/*
372 * Undo all redirections. Called on error or interrupt.
373 */
374
375#ifdef mkinit
376
377INCLUDE "redir.h"
378
379RESET {
380 while (psh->redirlist)
381 popredir(psh);
382}
383
384SHELLPROC {
385 clearredir(psh);
386}
387
388#endif
389
390/* Return true if fd 0 has already been redirected at least once. */
391int
392fd0_redirected_p(shinstance *psh) {
393 return psh->fd0_redirected != 0;
394}
395
396/*
397 * Discard all saved file descriptors.
398 */
399
400void
401clearredir(shinstance *psh)
402{
403 struct redirtab *rp;
404 int i;
405
406 for (rp = psh->redirlist ; rp ; rp = rp->next) {
407 for (i = 0 ; i < 10 ; i++) {
408 if (rp->renamed[i] >= 0) {
409 shfile_close(&psh->fdtab, rp->renamed[i]);
410 }
411 rp->renamed[i] = EMPTY;
412 }
413 }
414}
415
416
417
418/*
419 * Copy a file descriptor to be >= to. Returns -1
420 * if the source file descriptor is closed, EMPTY if there are no unused
421 * file descriptors left.
422 */
423
424int
425copyfd(shinstance *psh, int from, int to)
426{
427 int newfd;
428
429 newfd = shfile_fcntl(&psh->fdtab, from, F_DUPFD, to);
430 if (newfd < 0) {
431 if (errno == EMFILE)
432 return EMPTY;
433 error(psh, "%d: %s", from, sh_strerror(psh, errno));
434 }
435 return newfd;
436}
437
438
439/*
440 * Move a file descriptor to be == to. Returns -1
441 * if the source file descriptor is closed, EMPTY if there are no unused
442 * file descriptors left.
443 */
444
445int
446movefd(shinstance *psh, int from, int to)
447{
448 int newfd;
449
450 newfd = shfile_movefd(&psh->fdtab, from, to);
451 if (newfd < 0) {
452 if (errno == EMFILE)
453 return EMPTY;
454 error(psh, "%d: %s", from, sh_strerror(psh, errno));
455 }
456 return newfd;
457}
458
459
460/*
461 * Move a file descriptor to be >= to. Returns -1
462 * if the source file descriptor is closed, EMPTY if there are no unused
463 * file descriptors left.
464 */
465
466int
467movefd_above(shinstance *psh, int from, int to)
468{
469 int newfd;
470
471 newfd = shfile_movefd_above(&psh->fdtab, from, to);
472 if (newfd < 0) {
473 if (errno == EMFILE)
474 return EMPTY;
475 error(psh, "%d: %s", from, sh_strerror(psh, errno));
476 }
477 return newfd;
478}
479
Note: See TracBrowser for help on using the repository browser.