14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Kenneth Almquist. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes * are met: 114b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes * without specific prior written permission. 194b88c807SRodney W. Grimes * 204b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes * SUCH DAMAGE. 314b88c807SRodney W. Grimes */ 324b88c807SRodney W. Grimes 334b88c807SRodney W. Grimes #ifndef lint 343d7b5b93SPhilippe Charnier #if 0 353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95"; 363d7b5b93SPhilippe Charnier #endif 374b88c807SRodney W. Grimes #endif /* not lint */ 382749b141SDavid E. O'Brien #include <sys/cdefs.h> 392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 404b88c807SRodney W. Grimes 41aa9caaf6SPeter Wemm #include <sys/types.h> 421a958c66STim J. Robbins #include <sys/stat.h> 43aa9caaf6SPeter Wemm #include <signal.h> 44aa9caaf6SPeter Wemm #include <string.h> 45aa9caaf6SPeter Wemm #include <fcntl.h> 46aa9caaf6SPeter Wemm #include <errno.h> 47aa9caaf6SPeter Wemm #include <unistd.h> 48aa9caaf6SPeter Wemm #include <stdlib.h> 49aa9caaf6SPeter Wemm 504b88c807SRodney W. Grimes /* 514b88c807SRodney W. Grimes * Code for dealing with input/output redirection. 524b88c807SRodney W. Grimes */ 534b88c807SRodney W. Grimes 544b88c807SRodney W. Grimes #include "shell.h" 554b88c807SRodney W. Grimes #include "nodes.h" 564b88c807SRodney W. Grimes #include "jobs.h" 574b88c807SRodney W. Grimes #include "expand.h" 584b88c807SRodney W. Grimes #include "redir.h" 594b88c807SRodney W. Grimes #include "output.h" 604b88c807SRodney W. Grimes #include "memalloc.h" 614b88c807SRodney W. Grimes #include "error.h" 621a958c66STim J. Robbins #include "options.h" 634b88c807SRodney W. Grimes 644b88c807SRodney W. Grimes 654b88c807SRodney W. Grimes #define EMPTY -2 /* marks an unused slot in redirtab */ 66e1ef3141SJilles Tjoelker #define CLOSED -1 /* fd was not open before redir */ 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes 694b88c807SRodney W. Grimes struct redirtab { 704b88c807SRodney W. Grimes struct redirtab *next; 715b99fa05STim J. Robbins int renamed[10]; 721b57cec7SJilles Tjoelker int fd0_redirected; 73*bd9b38d1SJilles Tjoelker unsigned int empty_redirs; 744b88c807SRodney W. Grimes }; 754b88c807SRodney W. Grimes 764b88c807SRodney W. Grimes 770bdd3871SJilles Tjoelker static struct redirtab *redirlist; 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes /* 804b88c807SRodney W. Grimes * We keep track of whether or not fd0 has been redirected. This is for 814b88c807SRodney W. Grimes * background commands, where we want to redirect fd0 to /dev/null only 824b88c807SRodney W. Grimes * if it hasn't already been redirected. 834b88c807SRodney W. Grimes */ 84aa7b6f82SDavid E. O'Brien static int fd0_redirected = 0; 854b88c807SRodney W. Grimes 86*bd9b38d1SJilles Tjoelker /* Number of redirtabs that have not been allocated. */ 87*bd9b38d1SJilles Tjoelker static unsigned int empty_redirs = 0; 88*bd9b38d1SJilles Tjoelker 8988328642SDavid E. O'Brien static void openredirect(union node *, char[10 ]); 9088328642SDavid E. O'Brien static int openhere(union node *); 914b88c807SRodney W. Grimes 924b88c807SRodney W. Grimes 934b88c807SRodney W. Grimes /* 944b88c807SRodney W. Grimes * Process a list of redirection commands. If the REDIR_PUSH flag is set, 954b88c807SRodney W. Grimes * old file descriptors are stashed away so that the redirection can be 964b88c807SRodney W. Grimes * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 974b88c807SRodney W. Grimes * standard output, and the standard error if it becomes a duplicate of 984b88c807SRodney W. Grimes * stdout, is saved in memory. 991632bf1aSJilles Tjoelker * 1001632bf1aSJilles Tjoelker * We suppress interrupts so that we won't leave open file 1011632bf1aSJilles Tjoelker * descriptors around. Because the signal handler remains 1021632bf1aSJilles Tjoelker * installed and we do not use system call restart, interrupts 1031632bf1aSJilles Tjoelker * will still abort blocking opens such as fifos (they will fail 1041632bf1aSJilles Tjoelker * with EINTR). There is, however, a race condition if an interrupt 1051632bf1aSJilles Tjoelker * arrives after INTOFF and before open blocks. 1064b88c807SRodney W. Grimes */ 1074b88c807SRodney W. Grimes 1084b88c807SRodney W. Grimes void 1095134c3f7SWarner Losh redirect(union node *redir, int flags) 1104b88c807SRodney W. Grimes { 1114b88c807SRodney W. Grimes union node *n; 112aa9caaf6SPeter Wemm struct redirtab *sv = NULL; 1134b88c807SRodney W. Grimes int i; 1144b88c807SRodney W. Grimes int fd; 1154b88c807SRodney W. Grimes char memory[10]; /* file descriptors to write to memory */ 1164b88c807SRodney W. Grimes 1171632bf1aSJilles Tjoelker INTOFF; 1184b88c807SRodney W. Grimes for (i = 10 ; --i >= 0 ; ) 1194b88c807SRodney W. Grimes memory[i] = 0; 1204b88c807SRodney W. Grimes memory[1] = flags & REDIR_BACKQ; 1214b88c807SRodney W. Grimes if (flags & REDIR_PUSH) { 122*bd9b38d1SJilles Tjoelker empty_redirs++; 123*bd9b38d1SJilles Tjoelker if (redir != NULL) { 1244b88c807SRodney W. Grimes sv = ckmalloc(sizeof (struct redirtab)); 1254b88c807SRodney W. Grimes for (i = 0 ; i < 10 ; i++) 1264b88c807SRodney W. Grimes sv->renamed[i] = EMPTY; 1271b57cec7SJilles Tjoelker sv->fd0_redirected = fd0_redirected; 128*bd9b38d1SJilles Tjoelker sv->empty_redirs = empty_redirs - 1; 1294b88c807SRodney W. Grimes sv->next = redirlist; 1304b88c807SRodney W. Grimes redirlist = sv; 131*bd9b38d1SJilles Tjoelker empty_redirs = 0; 132*bd9b38d1SJilles Tjoelker } 1334b88c807SRodney W. Grimes } 1344b88c807SRodney W. Grimes for (n = redir ; n ; n = n->nfile.next) { 1354b88c807SRodney W. Grimes fd = n->nfile.fd; 1361b57cec7SJilles Tjoelker if (fd == 0) 1371b57cec7SJilles Tjoelker fd0_redirected = 1; 138769bbc65SJoerg Wunsch if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && 139769bbc65SJoerg Wunsch n->ndup.dupfd == fd) 140ab0a2172SSteve Price continue; /* redirect from/to same file descriptor */ 14179f56947SSteve Price 1424b88c807SRodney W. Grimes if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 1434b88c807SRodney W. Grimes INTOFF; 1445aa6dfdaSJilles Tjoelker if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) { 14579f56947SSteve Price switch (errno) { 14679f56947SSteve Price case EBADF: 147e1ef3141SJilles Tjoelker i = CLOSED; 148e1ef3141SJilles Tjoelker break; 14979f56947SSteve Price default: 15079f56947SSteve Price INTON; 15179f56947SSteve Price error("%d: %s", fd, strerror(errno)); 15279f56947SSteve Price break; 15379f56947SSteve Price } 1545aa6dfdaSJilles Tjoelker } 1554b88c807SRodney W. Grimes sv->renamed[fd] = i; 1564b88c807SRodney W. Grimes INTON; 1574b88c807SRodney W. Grimes } 1584b88c807SRodney W. Grimes openredirect(n, memory); 1591632bf1aSJilles Tjoelker INTON; 1601632bf1aSJilles Tjoelker INTOFF; 1614b88c807SRodney W. Grimes } 1624b88c807SRodney W. Grimes if (memory[1]) 1634b88c807SRodney W. Grimes out1 = &memout; 1644b88c807SRodney W. Grimes if (memory[2]) 1654b88c807SRodney W. Grimes out2 = &memout; 1661632bf1aSJilles Tjoelker INTON; 1674b88c807SRodney W. Grimes } 1684b88c807SRodney W. Grimes 1694b88c807SRodney W. Grimes 17088328642SDavid E. O'Brien static void 1715134c3f7SWarner Losh openredirect(union node *redir, char memory[10]) 1724b88c807SRodney W. Grimes { 1731a958c66STim J. Robbins struct stat sb; 1744b88c807SRodney W. Grimes int fd = redir->nfile.fd; 17561346cbdSJilles Tjoelker const char *fname; 1764b88c807SRodney W. Grimes int f; 17709683f46SJilles Tjoelker int e; 1784b88c807SRodney W. Grimes 1794b88c807SRodney W. Grimes memory[fd] = 0; 1804b88c807SRodney W. Grimes switch (redir->nfile.type) { 1814b88c807SRodney W. Grimes case NFROM: 1824b88c807SRodney W. Grimes fname = redir->nfile.expfname; 1834b88c807SRodney W. Grimes if ((f = open(fname, O_RDONLY)) < 0) 1841c59560dSTim J. Robbins error("cannot open %s: %s", fname, strerror(errno)); 1854b88c807SRodney W. Grimes break; 1864682f420SBrian Somers case NFROMTO: 1874682f420SBrian Somers fname = redir->nfile.expfname; 1884682f420SBrian Somers if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) 1891c59560dSTim J. Robbins error("cannot create %s: %s", fname, strerror(errno)); 19033c5acf0SJilles Tjoelker break; 1914b88c807SRodney W. Grimes case NTO: 192deb090cbSJilles Tjoelker if (Cflag) { 1934b88c807SRodney W. Grimes fname = redir->nfile.expfname; 194deb090cbSJilles Tjoelker if (stat(fname, &sb) == -1) { 195deb090cbSJilles Tjoelker if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) 196deb090cbSJilles Tjoelker error("cannot create %s: %s", fname, strerror(errno)); 197deb090cbSJilles Tjoelker } else if (!S_ISREG(sb.st_mode)) { 198deb090cbSJilles Tjoelker if ((f = open(fname, O_WRONLY, 0666)) < 0) 199deb090cbSJilles Tjoelker error("cannot create %s: %s", fname, strerror(errno)); 200deb090cbSJilles Tjoelker if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) { 201deb090cbSJilles Tjoelker close(f); 2021a958c66STim J. Robbins error("cannot create %s: %s", fname, 2031c59560dSTim J. Robbins strerror(EEXIST)); 204deb090cbSJilles Tjoelker } 205deb090cbSJilles Tjoelker } else 206deb090cbSJilles Tjoelker error("cannot create %s: %s", fname, 207deb090cbSJilles Tjoelker strerror(EEXIST)); 20833c5acf0SJilles Tjoelker break; 209deb090cbSJilles Tjoelker } 210deb090cbSJilles Tjoelker /* FALLTHROUGH */ 2111a958c66STim J. Robbins case NCLOBBER: 2121a958c66STim J. Robbins fname = redir->nfile.expfname; 2131a958c66STim J. Robbins if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 2141c59560dSTim J. Robbins error("cannot create %s: %s", fname, strerror(errno)); 21533c5acf0SJilles Tjoelker break; 2164b88c807SRodney W. Grimes case NAPPEND: 2174b88c807SRodney W. Grimes fname = redir->nfile.expfname; 2184b88c807SRodney W. Grimes if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 2191c59560dSTim J. Robbins error("cannot create %s: %s", fname, strerror(errno)); 22033c5acf0SJilles Tjoelker break; 2214b88c807SRodney W. Grimes case NTOFD: 2224b88c807SRodney W. Grimes case NFROMFD: 2234b88c807SRodney W. Grimes if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 2244b88c807SRodney W. Grimes if (memory[redir->ndup.dupfd]) 2254b88c807SRodney W. Grimes memory[fd] = 1; 2263dec7d0cSJilles Tjoelker else { 2273dec7d0cSJilles Tjoelker if (dup2(redir->ndup.dupfd, fd) < 0) 2283dec7d0cSJilles Tjoelker error("%d: %s", redir->ndup.dupfd, 2293dec7d0cSJilles Tjoelker strerror(errno)); 2303dec7d0cSJilles Tjoelker } 2317e1c7266SDag-Erling Smørgrav } else { 23263f901efSSergey Babkin close(fd); 2334b88c807SRodney W. Grimes } 23433c5acf0SJilles Tjoelker return; 2354b88c807SRodney W. Grimes case NHERE: 2364b88c807SRodney W. Grimes case NXHERE: 2374b88c807SRodney W. Grimes f = openhere(redir); 23833c5acf0SJilles Tjoelker break; 2394b88c807SRodney W. Grimes default: 2404b88c807SRodney W. Grimes abort(); 2414b88c807SRodney W. Grimes } 24233c5acf0SJilles Tjoelker if (f != fd) { 24333c5acf0SJilles Tjoelker if (dup2(f, fd) == -1) { 24433c5acf0SJilles Tjoelker e = errno; 24533c5acf0SJilles Tjoelker close(f); 24633c5acf0SJilles Tjoelker error("%d: %s", fd, strerror(e)); 24733c5acf0SJilles Tjoelker } 24833c5acf0SJilles Tjoelker close(f); 24933c5acf0SJilles Tjoelker } 2504b88c807SRodney W. Grimes } 2514b88c807SRodney W. Grimes 2524b88c807SRodney W. Grimes 2534b88c807SRodney W. Grimes /* 2544b88c807SRodney W. Grimes * Handle here documents. Normally we fork off a process to write the 2554b88c807SRodney W. Grimes * data to a pipe. If the document is short, we can stuff the data in 2564b88c807SRodney W. Grimes * the pipe without forking. 2574b88c807SRodney W. Grimes */ 2584b88c807SRodney W. Grimes 25988328642SDavid E. O'Brien static int 2605134c3f7SWarner Losh openhere(union node *redir) 2614b88c807SRodney W. Grimes { 26261346cbdSJilles Tjoelker const char *p; 2634b88c807SRodney W. Grimes int pip[2]; 264c6a453a4SJilles Tjoelker size_t len = 0; 265c6a453a4SJilles Tjoelker int flags; 266c6a453a4SJilles Tjoelker ssize_t written = 0; 2674b88c807SRodney W. Grimes 2684b88c807SRodney W. Grimes if (pipe(pip) < 0) 2696c48b6cfSMartin Cracauer error("Pipe call failed: %s", strerror(errno)); 2704dc6bdd3SJilles Tjoelker 2714dc6bdd3SJilles Tjoelker if (redir->type == NXHERE) 2724dc6bdd3SJilles Tjoelker p = redir->nhere.expdoc; 2734dc6bdd3SJilles Tjoelker else 2744dc6bdd3SJilles Tjoelker p = redir->nhere.doc->narg.text; 2754dc6bdd3SJilles Tjoelker len = strlen(p); 276c6a453a4SJilles Tjoelker if (len == 0) 2774b88c807SRodney W. Grimes goto out; 278c6a453a4SJilles Tjoelker flags = fcntl(pip[1], F_GETFL, 0); 279c6a453a4SJilles Tjoelker if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) { 280c6a453a4SJilles Tjoelker written = write(pip[1], p, len); 281c6a453a4SJilles Tjoelker if (written < 0) 282c6a453a4SJilles Tjoelker written = 0; 283c6a453a4SJilles Tjoelker if ((size_t)written == len) 284c6a453a4SJilles Tjoelker goto out; 285c6a453a4SJilles Tjoelker fcntl(pip[1], F_SETFL, flags); 2864b88c807SRodney W. Grimes } 2874dc6bdd3SJilles Tjoelker 2884b88c807SRodney W. Grimes if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 2894b88c807SRodney W. Grimes close(pip[0]); 2904b88c807SRodney W. Grimes signal(SIGINT, SIG_IGN); 2914b88c807SRodney W. Grimes signal(SIGQUIT, SIG_IGN); 2924b88c807SRodney W. Grimes signal(SIGHUP, SIG_IGN); 2934b88c807SRodney W. Grimes signal(SIGTSTP, SIG_IGN); 2944b88c807SRodney W. Grimes signal(SIGPIPE, SIG_DFL); 295c6a453a4SJilles Tjoelker xwrite(pip[1], p + written, len - written); 2964b88c807SRodney W. Grimes _exit(0); 2974b88c807SRodney W. Grimes } 2984b88c807SRodney W. Grimes out: 2994b88c807SRodney W. Grimes close(pip[1]); 3004b88c807SRodney W. Grimes return pip[0]; 3014b88c807SRodney W. Grimes } 3024b88c807SRodney W. Grimes 3034b88c807SRodney W. Grimes 3044b88c807SRodney W. Grimes 3054b88c807SRodney W. Grimes /* 3064b88c807SRodney W. Grimes * Undo the effects of the last redirection. 3074b88c807SRodney W. Grimes */ 3084b88c807SRodney W. Grimes 3094b88c807SRodney W. Grimes void 3105134c3f7SWarner Losh popredir(void) 3115134c3f7SWarner Losh { 31279f56947SSteve Price struct redirtab *rp = redirlist; 3134b88c807SRodney W. Grimes int i; 3144b88c807SRodney W. Grimes 315*bd9b38d1SJilles Tjoelker INTOFF; 316*bd9b38d1SJilles Tjoelker if (empty_redirs > 0) { 317*bd9b38d1SJilles Tjoelker empty_redirs--; 318*bd9b38d1SJilles Tjoelker INTON; 319*bd9b38d1SJilles Tjoelker return; 320*bd9b38d1SJilles Tjoelker } 3214b88c807SRodney W. Grimes for (i = 0 ; i < 10 ; i++) { 3224b88c807SRodney W. Grimes if (rp->renamed[i] != EMPTY) { 3234b88c807SRodney W. Grimes if (rp->renamed[i] >= 0) { 3247e1c7266SDag-Erling Smørgrav dup2(rp->renamed[i], i); 3254b88c807SRodney W. Grimes close(rp->renamed[i]); 3267e1c7266SDag-Erling Smørgrav } else { 3277e1c7266SDag-Erling Smørgrav close(i); 3284b88c807SRodney W. Grimes } 3294b88c807SRodney W. Grimes } 3304b88c807SRodney W. Grimes } 3311b57cec7SJilles Tjoelker fd0_redirected = rp->fd0_redirected; 332*bd9b38d1SJilles Tjoelker empty_redirs = rp->empty_redirs; 3334b88c807SRodney W. Grimes redirlist = rp->next; 3344b88c807SRodney W. Grimes ckfree(rp); 3354b88c807SRodney W. Grimes INTON; 3364b88c807SRodney W. Grimes } 3374b88c807SRodney W. Grimes 3384b88c807SRodney W. Grimes /* Return true if fd 0 has already been redirected at least once. */ 3394b88c807SRodney W. Grimes int 3405134c3f7SWarner Losh fd0_redirected_p(void) 3415134c3f7SWarner Losh { 3424b88c807SRodney W. Grimes return fd0_redirected != 0; 3434b88c807SRodney W. Grimes } 3444b88c807SRodney W. Grimes 3454b88c807SRodney W. Grimes /* 3464b88c807SRodney W. Grimes * Discard all saved file descriptors. 3474b88c807SRodney W. Grimes */ 3484b88c807SRodney W. Grimes 3494b88c807SRodney W. Grimes void 3505134c3f7SWarner Losh clearredir(void) 3515134c3f7SWarner Losh { 35279f56947SSteve Price struct redirtab *rp; 3534b88c807SRodney W. Grimes int i; 3544b88c807SRodney W. Grimes 3554b88c807SRodney W. Grimes for (rp = redirlist ; rp ; rp = rp->next) { 3564b88c807SRodney W. Grimes for (i = 0 ; i < 10 ; i++) { 3574b88c807SRodney W. Grimes if (rp->renamed[i] >= 0) { 3584b88c807SRodney W. Grimes close(rp->renamed[i]); 3594b88c807SRodney W. Grimes } 3604b88c807SRodney W. Grimes rp->renamed[i] = EMPTY; 3614b88c807SRodney W. Grimes } 3624b88c807SRodney W. Grimes } 3634b88c807SRodney W. Grimes } 364