redir.c revision 124780
1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95"; 40#endif 41#endif /* not lint */ 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/bin/sh/redir.c 124780 2004-01-21 12:50:01Z des $"); 44 45#include <sys/types.h> 46#include <sys/stat.h> 47#include <signal.h> 48#include <string.h> 49#include <fcntl.h> 50#include <errno.h> 51#include <unistd.h> 52#include <stdlib.h> 53 54/* 55 * Code for dealing with input/output redirection. 56 */ 57 58#include "shell.h" 59#include "nodes.h" 60#include "jobs.h" 61#include "expand.h" 62#include "redir.h" 63#include "output.h" 64#include "memalloc.h" 65#include "error.h" 66#include "options.h" 67 68 69#define EMPTY -2 /* marks an unused slot in redirtab */ 70#define PIPESIZE 4096 /* amount of buffering in a pipe */ 71 72 73MKINIT 74struct redirtab { 75 struct redirtab *next; 76 int renamed[10]; 77}; 78 79 80MKINIT struct redirtab *redirlist; 81 82/* 83 * We keep track of whether or not fd0 has been redirected. This is for 84 * background commands, where we want to redirect fd0 to /dev/null only 85 * if it hasn't already been redirected. 86*/ 87STATIC int fd0_redirected = 0; 88 89STATIC void openredirect(union node *, char[10 ]); 90STATIC int openhere(union node *); 91 92 93/* 94 * Process a list of redirection commands. If the REDIR_PUSH flag is set, 95 * old file descriptors are stashed away so that the redirection can be 96 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 97 * standard output, and the standard error if it becomes a duplicate of 98 * stdout, is saved in memory. 99 */ 100 101void 102redirect(union node *redir, int flags) 103{ 104 union node *n; 105 struct redirtab *sv = NULL; 106 int i; 107 int fd; 108 int try; 109 char memory[10]; /* file descriptors to write to memory */ 110 111 for (i = 10 ; --i >= 0 ; ) 112 memory[i] = 0; 113 memory[1] = flags & REDIR_BACKQ; 114 if (flags & REDIR_PUSH) { 115 sv = ckmalloc(sizeof (struct redirtab)); 116 for (i = 0 ; i < 10 ; i++) 117 sv->renamed[i] = EMPTY; 118 sv->next = redirlist; 119 redirlist = sv; 120 } 121 for (n = redir ; n ; n = n->nfile.next) { 122 fd = n->nfile.fd; 123 try = 0; 124 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && 125 n->ndup.dupfd == fd) 126 continue; /* redirect from/to same file descriptor */ 127 128 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 129 INTOFF; 130again: 131 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) { 132 switch (errno) { 133 case EBADF: 134 if (!try) { 135 openredirect(n, memory); 136 try++; 137 goto again; 138 } 139 /* FALLTHROUGH*/ 140 default: 141 INTON; 142 error("%d: %s", fd, strerror(errno)); 143 break; 144 } 145 } 146 if (!try) { 147 sv->renamed[fd] = i; 148 } 149 INTON; 150 } 151 if (fd == 0) 152 fd0_redirected++; 153 if (!try) 154 openredirect(n, memory); 155 } 156 if (memory[1]) 157 out1 = &memout; 158 if (memory[2]) 159 out2 = &memout; 160} 161 162 163STATIC void 164openredirect(union node *redir, char memory[10]) 165{ 166 struct stat sb; 167 int fd = redir->nfile.fd; 168 char *fname; 169 int f; 170 171 /* 172 * We suppress interrupts so that we won't leave open file 173 * descriptors around. This may not be such a good idea because 174 * an open of a device or a fifo can block indefinitely. 175 */ 176 INTOFF; 177 memory[fd] = 0; 178 switch (redir->nfile.type) { 179 case NFROM: 180 fname = redir->nfile.expfname; 181 if ((f = open(fname, O_RDONLY)) < 0) 182 error("cannot open %s: %s", fname, strerror(errno)); 183movefd: 184 if (f != fd) { 185 dup2(f, fd); 186 close(f); 187 } 188 break; 189 case NFROMTO: 190 fname = redir->nfile.expfname; 191 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) 192 error("cannot create %s: %s", fname, strerror(errno)); 193 goto movefd; 194 case NTO: 195 fname = redir->nfile.expfname; 196 if (Cflag && stat(fname, &sb) != -1 && S_ISREG(sb.st_mode)) 197 error("cannot create %s: %s", fname, 198 strerror(EEXIST)); 199 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 200 error("cannot create %s: %s", fname, strerror(errno)); 201 goto movefd; 202 case NCLOBBER: 203 fname = redir->nfile.expfname; 204 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 205 error("cannot create %s: %s", fname, strerror(errno)); 206 goto movefd; 207 case NAPPEND: 208 fname = redir->nfile.expfname; 209 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 210 error("cannot create %s: %s", fname, strerror(errno)); 211 goto movefd; 212 case NTOFD: 213 case NFROMFD: 214 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 215 if (memory[redir->ndup.dupfd]) 216 memory[fd] = 1; 217 else 218 dup2(redir->ndup.dupfd, fd); 219 } else { 220 close(fd); 221 } 222 break; 223 case NHERE: 224 case NXHERE: 225 f = openhere(redir); 226 goto movefd; 227 default: 228 abort(); 229 } 230 INTON; 231} 232 233 234/* 235 * Handle here documents. Normally we fork off a process to write the 236 * data to a pipe. If the document is short, we can stuff the data in 237 * the pipe without forking. 238 */ 239 240STATIC int 241openhere(union node *redir) 242{ 243 int pip[2]; 244 int len = 0; 245 246 if (pipe(pip) < 0) 247 error("Pipe call failed: %s", strerror(errno)); 248 if (redir->type == NHERE) { 249 len = strlen(redir->nhere.doc->narg.text); 250 if (len <= PIPESIZE) { 251 xwrite(pip[1], redir->nhere.doc->narg.text, len); 252 goto out; 253 } 254 } 255 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 256 close(pip[0]); 257 signal(SIGINT, SIG_IGN); 258 signal(SIGQUIT, SIG_IGN); 259 signal(SIGHUP, SIG_IGN); 260 signal(SIGTSTP, SIG_IGN); 261 signal(SIGPIPE, SIG_DFL); 262 if (redir->type == NHERE) 263 xwrite(pip[1], redir->nhere.doc->narg.text, len); 264 else 265 expandhere(redir->nhere.doc, pip[1]); 266 _exit(0); 267 } 268out: 269 close(pip[1]); 270 return pip[0]; 271} 272 273 274 275/* 276 * Undo the effects of the last redirection. 277 */ 278 279void 280popredir(void) 281{ 282 struct redirtab *rp = redirlist; 283 int i; 284 285 for (i = 0 ; i < 10 ; i++) { 286 if (rp->renamed[i] != EMPTY) { 287 if (i == 0) 288 fd0_redirected--; 289 if (rp->renamed[i] >= 0) { 290 dup2(rp->renamed[i], i); 291 close(rp->renamed[i]); 292 } else { 293 close(i); 294 } 295 } 296 } 297 INTOFF; 298 redirlist = rp->next; 299 ckfree(rp); 300 INTON; 301} 302 303/* 304 * Undo all redirections. Called on error or interrupt. 305 */ 306 307#ifdef mkinit 308 309INCLUDE "redir.h" 310 311RESET { 312 while (redirlist) 313 popredir(); 314} 315 316SHELLPROC { 317 clearredir(); 318} 319 320#endif 321 322/* Return true if fd 0 has already been redirected at least once. */ 323int 324fd0_redirected_p(void) 325{ 326 return fd0_redirected != 0; 327} 328 329/* 330 * Discard all saved file descriptors. 331 */ 332 333void 334clearredir(void) 335{ 336 struct redirtab *rp; 337 int i; 338 339 for (rp = redirlist ; rp ; rp = rp->next) { 340 for (i = 0 ; i < 10 ; i++) { 341 if (rp->renamed[i] >= 0) { 342 close(rp->renamed[i]); 343 } 344 rp->renamed[i] = EMPTY; 345 } 346 } 347} 348