redir.c revision 11601
194541Sru/*- 294541Sru * Copyright (c) 1991, 1993 394541Sru * The Regents of the University of California. All rights reserved. 494541Sru * 594541Sru * This code is derived from software contributed to Berkeley by 694541Sru * Kenneth Almquist. 794541Sru * 894541Sru * Redistribution and use in source and binary forms, with or without 994541Sru * modification, are permitted provided that the following conditions 1094541Sru * are met: 1194541Sru * 1. Redistributions of source code must retain the above copyright 1294541Sru * notice, this list of conditions and the following disclaimer. 1394541Sru * 2. Redistributions in binary form must reproduce the above copyright 1494541Sru * notice, this list of conditions and the following disclaimer in the 1594541Sru * documentation and/or other materials provided with the distribution. 1694541Sru * 3. All advertising materials mentioning features or use of this software 1794541Sru * must display the following acknowledgement: 1894541Sru * This product includes software developed by the University of 1994541Sru * California, Berkeley and its contributors. 2094541Sru * 4. Neither the name of the University nor the names of its contributors 2194541Sru * may be used to endorse or promote products derived from this software 2294541Sru * without specific prior written permission. 2394541Sru * 2494541Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2594541Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2694541Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2794541Sru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2894541Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2994541Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3094541Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3194541Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3294541Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3394541Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3494541Sru * SUCH DAMAGE. 3594541Sru * 3694541Sru * $Id: redir.c,v 1.3 1995/05/30 00:07:22 rgrimes Exp $ 3794541Sru */ 3894541Sru 3994541Sru#ifndef lint 4094541Srustatic char sccsid[] = "@(#)redir.c 8.1 (Berkeley) 5/31/93"; 4194541Sru#endif /* not lint */ 4294541Sru 4394541Sru/* 4494541Sru * Code for dealing with input/output redirection. 4594541Sru */ 4694541Sru 4794541Sru#include "shell.h" 48115122Sru#include "nodes.h" 4994541Sru#include "jobs.h" 5097388Sru#include "expand.h" 51153838Sdfr#include "redir.h" 52128184Sru#include "output.h" 5394541Sru#include "memalloc.h" 5494541Sru#include "error.h" 5594541Sru#include <sys/types.h> 5694541Sru#include <signal.h> 5794541Sru#include <fcntl.h> 5894541Sru#include <errno.h> 5994541Sru#include <unistd.h> 6094541Sru 6194541Sru 6294541Sru#define EMPTY -2 /* marks an unused slot in redirtab */ 6394541Sru#define PIPESIZE 4096 /* amount of buffering in a pipe */ 6494541Sru 6594541Sru 6694541SruMKINIT 6794541Srustruct redirtab { 68164581Sdelphij struct redirtab *next; 6994541Sru short renamed[10]; 7094541Sru}; 7194541Sru 7294541Sru 7394541SruMKINIT struct redirtab *redirlist; 7494541Sru 7594541Sru/* 7694541Sru * We keep track of whether or not fd0 has been redirected. This is for 7794541Sru * background commands, where we want to redirect fd0 to /dev/null only 7894541Sru * if it hasn't already been redirected. 7994541Sru*/ 8094541Sruint fd0_redirected = 0; 8194541Sru 8294541Sru#ifdef __STDC__ 8394541SruSTATIC void openredirect(union node *, char *); 8494541SruSTATIC int openhere(union node *); 8594541Sru#else 8694541SruSTATIC void openredirect(); 8794541SruSTATIC int openhere(); 8894541Sru#endif 8994541Sru 9094541Sru 9194541Sru 9294541Sru/* 9394541Sru * Process a list of redirection commands. If the REDIR_PUSH flag is set, 9494541Sru * old file descriptors are stashed away so that the redirection can be 9594541Sru * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 9694541Sru * standard output, and the standard error if it becomes a duplicate of 9794541Sru * stdout, is saved in memory. 9894541Sru */ 9994541Sru 10094541Sruvoid 10194541Sruredirect(redir, flags) 10294541Sru union node *redir; 10394541Sru int flags; 10494541Sru { 10594541Sru union node *n; 10694541Sru struct redirtab *sv; 10794541Sru int i; 10894541Sru int fd; 10994541Sru char memory[10]; /* file descriptors to write to memory */ 11094541Sru 11194541Sru for (i = 10 ; --i >= 0 ; ) 11294541Sru memory[i] = 0; 11394541Sru memory[1] = flags & REDIR_BACKQ; 11494541Sru if (flags & REDIR_PUSH) { 11594541Sru 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 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && 124 n->ndup.dupfd == fd) 125 continue; /* redirect from/to myself */ 126 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 127 INTOFF; 128 if ((i = copyfd(fd, 10)) != EMPTY) { 129 sv->renamed[fd] = i; 130 close(fd); 131 } 132 INTON; 133 if (i == EMPTY) 134 error("Out of file descriptors"); 135 } else { 136 close(fd); 137 } 138 if (fd == 0) 139 fd0_redirected++; 140 openredirect(n, memory); 141 } 142 if (memory[1]) 143 out1 = &memout; 144 if (memory[2]) 145 out2 = &memout; 146} 147 148 149STATIC void 150openredirect(redir, memory) 151 union node *redir; 152 char memory[10]; 153 { 154 int fd = redir->nfile.fd; 155 char *fname; 156 int f; 157 158 /* 159 * We suppress interrupts so that we won't leave open file 160 * descriptors around. This may not be such a good idea because 161 * an open of a device or a fifo can block indefinitely. 162 */ 163 INTOFF; 164 memory[fd] = 0; 165 switch (redir->nfile.type) { 166 case NFROM: 167 fname = redir->nfile.expfname; 168 if ((f = open(fname, O_RDONLY)) < 0) 169 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); 170movefd: 171 if (f != fd) { 172 copyfd(f, fd); 173 close(f); 174 } 175 break; 176 case NTO: 177 fname = redir->nfile.expfname; 178#ifdef O_CREAT 179 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 180 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 181#else 182 if ((f = creat(fname, 0666)) < 0) 183 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 184#endif 185 goto movefd; 186 case NAPPEND: 187 fname = redir->nfile.expfname; 188#ifdef O_APPEND 189 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 190 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 191#else 192 if ((f = open(fname, O_WRONLY)) < 0 193 && (f = creat(fname, 0666)) < 0) 194 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 195 lseek(f, (off_t)0, 2); 196#endif 197 goto movefd; 198 case NTOFD: 199 case NFROMFD: 200 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 201 if (memory[redir->ndup.dupfd]) 202 memory[fd] = 1; 203 else 204 copyfd(redir->ndup.dupfd, fd); 205 } 206 break; 207 case NHERE: 208 case NXHERE: 209 f = openhere(redir); 210 goto movefd; 211 default: 212 abort(); 213 } 214 INTON; 215} 216 217 218/* 219 * Handle here documents. Normally we fork off a process to write the 220 * data to a pipe. If the document is short, we can stuff the data in 221 * the pipe without forking. 222 */ 223 224STATIC int 225openhere(redir) 226 union node *redir; 227 { 228 int pip[2]; 229 int len; 230 231 if (pipe(pip) < 0) 232 error("Pipe call failed"); 233 if (redir->type == NHERE) { 234 len = strlen(redir->nhere.doc->narg.text); 235 if (len <= PIPESIZE) { 236 xwrite(pip[1], redir->nhere.doc->narg.text, len); 237 goto out; 238 } 239 } 240 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 241 close(pip[0]); 242 signal(SIGINT, SIG_IGN); 243 signal(SIGQUIT, SIG_IGN); 244 signal(SIGHUP, SIG_IGN); 245#ifdef SIGTSTP 246 signal(SIGTSTP, SIG_IGN); 247#endif 248 signal(SIGPIPE, SIG_DFL); 249 if (redir->type == NHERE) 250 xwrite(pip[1], redir->nhere.doc->narg.text, len); 251 else 252 expandhere(redir->nhere.doc, pip[1]); 253 _exit(0); 254 } 255out: 256 close(pip[1]); 257 return pip[0]; 258} 259 260 261 262/* 263 * Undo the effects of the last redirection. 264 */ 265 266void 267popredir() { 268 register struct redirtab *rp = redirlist; 269 int i; 270 271 for (i = 0 ; i < 10 ; i++) { 272 if (rp->renamed[i] != EMPTY) { 273 if (i == 0) 274 fd0_redirected--; 275 close(i); 276 if (rp->renamed[i] >= 0) { 277 copyfd(rp->renamed[i], i); 278 close(rp->renamed[i]); 279 } 280 } 281 } 282 INTOFF; 283 redirlist = rp->next; 284 ckfree(rp); 285 INTON; 286} 287 288/* 289 * Undo all redirections. Called on error or interrupt. 290 */ 291 292#ifdef mkinit 293 294INCLUDE "redir.h" 295 296RESET { 297 while (redirlist) 298 popredir(); 299} 300 301SHELLPROC { 302 clearredir(); 303} 304 305#endif 306 307/* Return true if fd 0 has already been redirected at least once. */ 308int 309fd0_redirected_p () { 310 return fd0_redirected != 0; 311} 312 313/* 314 * Discard all saved file descriptors. 315 */ 316 317void 318clearredir() { 319 register struct redirtab *rp; 320 int i; 321 322 for (rp = redirlist ; rp ; rp = rp->next) { 323 for (i = 0 ; i < 10 ; i++) { 324 if (rp->renamed[i] >= 0) { 325 close(rp->renamed[i]); 326 } 327 rp->renamed[i] = EMPTY; 328 } 329 } 330} 331 332 333 334/* 335 * Copy a file descriptor to be >= to. Returns -1 336 * if the source file descriptor is closed, EMPTY if there are no unused 337 * file descriptors left. 338 */ 339 340int 341copyfd(from, to) { 342 int newfd; 343 344 newfd = fcntl(from, F_DUPFD, to); 345 if (newfd < 0 && errno == EMFILE) 346 return EMPTY; 347 if (newfd < 0) 348 error("%d: %s", from, strerror(errno)); 349 return newfd; 350} 351