VirtualBox

source: kBuild/trunk/src/kash/redir.c@ 2282

Last change on this file since 2282 was 1233, checked in by bird, 17 years ago

keywords.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 9.2 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(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, strerror(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 copyfd(psh, f, fd);
236 shfile_close(&psh->fdtab, f);
237 }
238 INTON;
239 return;
240ecreate:
241 error(psh, "cannot create %s: %s", fname, errmsg(psh, errno, E_CREAT));
242eopen:
243 error(psh, "cannot open %s: %s", fname, errmsg(psh, errno, E_OPEN));
244}
245
246
247/*
248 * Handle here documents. Normally we fork off a process to write the
249 * data to a pipe. If the document is short, we can stuff the data in
250 * the pipe without forking.
251 */
252
253STATIC int
254openhere(shinstance *psh, union node *redir)
255{
256 int pip[2];
257 size_t len = 0;
258
259 if (shfile_pipe(&psh->fdtab, pip) < 0)
260 error(psh, "Pipe call failed");
261 if (redir->type == NHERE) {
262 len = strlen(redir->nhere.doc->narg.text);
263 if (len <= PIPESIZE) {
264 xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
265 goto out;
266 }
267 }
268 if (forkshell(psh, (struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
269 shfile_close(&psh->fdtab, pip[0]);
270 sh_signal(psh, SIGINT, SH_SIG_IGN);
271 sh_signal(psh, SIGQUIT, SH_SIG_IGN);
272 sh_signal(psh, SIGHUP, SH_SIG_IGN);
273#ifdef SIGTSTP
274 sh_signal(psh, SIGTSTP, SH_SIG_IGN);
275#endif
276 sh_signal(psh, SIGPIPE, SH_SIG_DFL);
277 if (redir->type == NHERE)
278 xwrite(psh, pip[1], redir->nhere.doc->narg.text, len);
279 else
280 expandhere(psh, redir->nhere.doc, pip[1]);
281 sh__exit(psh, 0);
282 }
283out:
284 shfile_close(&psh->fdtab, pip[1]);
285 return pip[0];
286}
287
288
289
290/*
291 * Undo the effects of the last redirection.
292 */
293
294void
295popredir(shinstance *psh)
296{
297 struct redirtab *rp = psh->redirlist;
298 int i;
299
300 for (i = 0 ; i < 10 ; i++) {
301 if (rp->renamed[i] != EMPTY) {
302 if (i == 0)
303 psh->fd0_redirected--;
304 shfile_close(&psh->fdtab, i);
305 if (rp->renamed[i] >= 0) {
306 copyfd(psh, rp->renamed[i], i);
307 shfile_close(&psh->fdtab, rp->renamed[i]);
308 }
309 }
310 }
311 INTOFF;
312 psh->redirlist = rp->next;
313 ckfree(rp);
314 INTON;
315}
316
317/*
318 * Undo all redirections. Called on error or interrupt.
319 */
320
321#ifdef mkinit
322
323INCLUDE "redir.h"
324
325RESET {
326 while (psh->redirlist)
327 popredir(psh);
328}
329
330SHELLPROC {
331 clearredir(psh, 0);
332}
333
334#endif
335
336/* Return true if fd 0 has already been redirected at least once. */
337int
338fd0_redirected_p(shinstance *psh) {
339 return psh->fd0_redirected != 0;
340}
341
342/*
343 * Discard all saved file descriptors.
344 */
345
346void
347clearredir(shinstance *psh, int vforked)
348{
349 struct redirtab *rp;
350 int i;
351
352 for (rp = psh->redirlist ; rp ; rp = rp->next) {
353 for (i = 0 ; i < 10 ; i++) {
354 if (rp->renamed[i] >= 0) {
355 shfile_close(&psh->fdtab, rp->renamed[i]);
356 }
357 if (!vforked)
358 rp->renamed[i] = EMPTY;
359 }
360 }
361}
362
363
364
365/*
366 * Copy a file descriptor to be >= to. Returns -1
367 * if the source file descriptor is closed, EMPTY if there are no unused
368 * file descriptors left.
369 */
370
371int
372copyfd(shinstance *psh, int from, int to)
373{
374 int newfd;
375
376 newfd = shfile_fcntl(&psh->fdtab, from, F_DUPFD, to);
377 if (newfd < 0) {
378 if (errno == EMFILE)
379 return EMPTY;
380 else
381 error(psh, "%d: %s", from, strerror(errno));
382 }
383 return newfd;
384}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette