redir.c revision 1556
11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1991, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Kenneth Almquist. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 16148834Sstefanf * 3. All advertising materials mentioning features or use of this software 171573Srgrimes * must display the following acknowledgement: 181573Srgrimes * This product includes software developed by the University of 191573Srgrimes * California, Berkeley and its contributors. 201573Srgrimes * 4. Neither the name of the University nor the names of its contributors 211573Srgrimes * may be used to endorse or promote products derived from this software 221573Srgrimes * without specific prior written permission. 231573Srgrimes * 241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3184260Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32148834Sstefanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341573Srgrimes * SUCH DAMAGE. 351573Srgrimes */ 361573Srgrimes 371573Srgrimes#ifndef lint 3884260Sobrienstatic char sccsid[] = "@(#)redir.c 8.1 (Berkeley) 5/31/93"; 3984260Sobrien#endif /* not lint */ 401573Srgrimes 411573Srgrimes/* 421573Srgrimes * Code for dealing with input/output redirection. 431573Srgrimes */ 441573Srgrimes 451573Srgrimes#include "shell.h" 461573Srgrimes#include "nodes.h" 471573Srgrimes#include "jobs.h" 4884260Sobrien#include "expand.h" 4984260Sobrien#include "redir.h" 501573Srgrimes#include "output.h" 511573Srgrimes#include "memalloc.h" 521573Srgrimes#include "error.h" 531573Srgrimes#include <sys/types.h> 541573Srgrimes#include <signal.h> 551573Srgrimes#include <fcntl.h> 56148834Sstefanf#include <errno.h> 571573Srgrimes#include <unistd.h> 5884260Sobrien 5984260Sobrien 6084260Sobrien#define EMPTY -2 /* marks an unused slot in redirtab */ 611573Srgrimes#define PIPESIZE 4096 /* amount of buffering in a pipe */ 621573Srgrimes 631573Srgrimes 6484260SobrienMKINIT 6584260Sobrienstruct redirtab { 6684260Sobrien struct redirtab *next; 6784260Sobrien short renamed[10]; 6884260Sobrien}; 69148834Sstefanf 7084260Sobrien 7184260SobrienMKINIT struct redirtab *redirlist; 7284260Sobrien 7384260Sobrien/* 7484260Sobrien * We keep track of whether or not fd0 has been redirected. This is for 7584260Sobrien * background commands, where we want to redirect fd0 to /dev/null only 7684260Sobrien * if it hasn't already been redirected. 771573Srgrimes*/ 781573Srgrimesint fd0_redirected = 0; 791573Srgrimes 801573Srgrimes#ifdef __STDC__ 811573SrgrimesSTATIC void openredirect(union node *, char *); 821573SrgrimesSTATIC int openhere(union node *); 831573Srgrimes#else 8484260SobrienSTATIC void openredirect(); 851573SrgrimesSTATIC int openhere(); 8684260Sobrien#endif 8784260Sobrien 88237448Spfg 891573Srgrimes 9084260Sobrien/* 9184260Sobrien * Process a list of redirection commands. If the REDIR_PUSH flag is set, 9284260Sobrien * old file descriptors are stashed away so that the redirection can be 9384260Sobrien * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 941573Srgrimes * standard output, and the standard error if it becomes a duplicate of 95237448Spfg * stdout, is saved in memory. 96237448Spfg */ 97237448Spfg 98237448Spfgvoid 99237448Spfgredirect(redir, flags) 100237448Spfg union node *redir; 101237448Spfg int flags; 102237448Spfg { 103237448Spfg union node *n; 104237448Spfg struct redirtab *sv; 105237448Spfg int i; 10684260Sobrien int fd; 10784260Sobrien char memory[10]; /* file descriptors to write to memory */ 10884260Sobrien 1091573Srgrimes for (i = 10 ; --i >= 0 ; ) 1101573Srgrimes memory[i] = 0; 1111573Srgrimes memory[1] = flags & REDIR_BACKQ; 1121573Srgrimes if (flags & REDIR_PUSH) { 1131573Srgrimes sv = ckmalloc(sizeof (struct redirtab)); 1148870Srgrimes for (i = 0 ; i < 10 ; i++) 11584260Sobrien sv->renamed[i] = EMPTY; 1161573Srgrimes sv->next = redirlist; 1171573Srgrimes redirlist = sv; 11884260Sobrien } 11984260Sobrien for (n = redir ; n ; n = n->nfile.next) { 12084260Sobrien fd = n->nfile.fd; 121237448Spfg if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 12284260Sobrien INTOFF; 12384260Sobrien if ((i = copyfd(fd, 10)) != EMPTY) { 12484260Sobrien sv->renamed[fd] = i; 125237448Spfg close(fd); 126237448Spfg } 12784260Sobrien INTON; 1281573Srgrimes if (i == EMPTY) 12984260Sobrien error("Out of file descriptors"); 1301573Srgrimes } else { 1311573Srgrimes close(fd); 1321573Srgrimes } 1331573Srgrimes if (fd == 0) 1348870Srgrimes fd0_redirected++; 135148834Sstefanf openredirect(n, memory); 1361573Srgrimes } 13784260Sobrien if (memory[1]) 1381573Srgrimes out1 = &memout; 1391573Srgrimes if (memory[2]) 1401573Srgrimes out2 = &memout; 1411573Srgrimes} 1421573Srgrimes 1438870Srgrimes 144237448SpfgSTATIC void 1451573Srgrimesopenredirect(redir, memory) 14684260Sobrien union node *redir; 14784260Sobrien char memory[10]; 148237448Spfg { 14984260Sobrien int fd = redir->nfile.fd; 15084260Sobrien char *fname; 15184260Sobrien int f; 152237448Spfg 15384260Sobrien /* 154237448Spfg * We suppress interrupts so that we won't leave open file 15584260Sobrien * descriptors around. This may not be such a good idea because 15684260Sobrien * an open of a device or a fifo can block indefinitely. 15784260Sobrien */ 15884260Sobrien INTOFF; 15984260Sobrien memory[fd] = 0; 160237448Spfg switch (redir->nfile.type) { 161237448Spfg case NFROM: 162237448Spfg fname = redir->nfile.expfname; 16384260Sobrien if ((f = open(fname, O_RDONLY)) < 0) 16484260Sobrien error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); 165237448Spfgmovefd: 166237448Spfg if (f != fd) { 16784260Sobrien copyfd(f, fd); 16884260Sobrien close(f); 16984260Sobrien } 17084260Sobrien break; 17184260Sobrien case NTO: 17284260Sobrien fname = redir->nfile.expfname; 17384260Sobrien#ifdef O_CREAT 174237448Spfg if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 17584260Sobrien error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 176237448Spfg#else 17784260Sobrien if ((f = creat(fname, 0666)) < 0) 17884260Sobrien error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 179237448Spfg#endif 180237448Spfg goto movefd; 18184260Sobrien case NAPPEND: 182237448Spfg fname = redir->nfile.expfname; 18384260Sobrien#ifdef O_APPEND 184237448Spfg if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 185237448Spfg error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 186237448Spfg#else 187237448Spfg if ((f = open(fname, O_WRONLY)) < 0 188237448Spfg && (f = creat(fname, 0666)) < 0) 189237448Spfg error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 190237448Spfg lseek(f, (off_t)0, 2); 191237448Spfg#endif 19284260Sobrien goto movefd; 193 case NTOFD: 194 case NFROMFD: 195 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 196 if (memory[redir->ndup.dupfd]) 197 memory[fd] = 1; 198 else 199 copyfd(redir->ndup.dupfd, fd); 200 } 201 break; 202 case NHERE: 203 case NXHERE: 204 f = openhere(redir); 205 goto movefd; 206 default: 207 abort(); 208 } 209 INTON; 210} 211 212 213/* 214 * Handle here documents. Normally we fork off a process to write the 215 * data to a pipe. If the document is short, we can stuff the data in 216 * the pipe without forking. 217 */ 218 219STATIC int 220openhere(redir) 221 union node *redir; 222 { 223 int pip[2]; 224 int len; 225 226 if (pipe(pip) < 0) 227 error("Pipe call failed"); 228 if (redir->type == NHERE) { 229 len = strlen(redir->nhere.doc->narg.text); 230 if (len <= PIPESIZE) { 231 xwrite(pip[1], redir->nhere.doc->narg.text, len); 232 goto out; 233 } 234 } 235 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 236 close(pip[0]); 237 signal(SIGINT, SIG_IGN); 238 signal(SIGQUIT, SIG_IGN); 239 signal(SIGHUP, SIG_IGN); 240#ifdef SIGTSTP 241 signal(SIGTSTP, SIG_IGN); 242#endif 243 signal(SIGPIPE, SIG_DFL); 244 if (redir->type == NHERE) 245 xwrite(pip[1], redir->nhere.doc->narg.text, len); 246 else 247 expandhere(redir->nhere.doc, pip[1]); 248 _exit(0); 249 } 250out: 251 close(pip[1]); 252 return pip[0]; 253} 254 255 256 257/* 258 * Undo the effects of the last redirection. 259 */ 260 261void 262popredir() { 263 register struct redirtab *rp = redirlist; 264 int i; 265 266 for (i = 0 ; i < 10 ; i++) { 267 if (rp->renamed[i] != EMPTY) { 268 if (i == 0) 269 fd0_redirected--; 270 close(i); 271 if (rp->renamed[i] >= 0) { 272 copyfd(rp->renamed[i], i); 273 close(rp->renamed[i]); 274 } 275 } 276 } 277 INTOFF; 278 redirlist = rp->next; 279 ckfree(rp); 280 INTON; 281} 282 283/* 284 * Undo all redirections. Called on error or interrupt. 285 */ 286 287#ifdef mkinit 288 289INCLUDE "redir.h" 290 291RESET { 292 while (redirlist) 293 popredir(); 294} 295 296SHELLPROC { 297 clearredir(); 298} 299 300#endif 301 302/* Return true if fd 0 has already been redirected at least once. */ 303int 304fd0_redirected_p () { 305 return fd0_redirected != 0; 306} 307 308/* 309 * Discard all saved file descriptors. 310 */ 311 312void 313clearredir() { 314 register struct redirtab *rp; 315 int i; 316 317 for (rp = redirlist ; rp ; rp = rp->next) { 318 for (i = 0 ; i < 10 ; i++) { 319 if (rp->renamed[i] >= 0) { 320 close(rp->renamed[i]); 321 } 322 rp->renamed[i] = EMPTY; 323 } 324 } 325} 326 327 328 329/* 330 * Copy a file descriptor to be >= to. Returns -1 331 * if the source file descriptor is closed, EMPTY if there are no unused 332 * file descriptors left. 333 */ 334 335int 336copyfd(from, to) { 337 int newfd; 338 339 newfd = fcntl(from, F_DUPFD, to); 340 if (newfd < 0 && errno == EMFILE) 341 return EMPTY; 342 return newfd; 343} 344