VirtualBox

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

Last change on this file since 2629 was 2424, checked in by bird, 14 years ago

kash: Some S-bahn optimizations.

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