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

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

kash: refactoring forkshell(); simple stuff.

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