redir.c revision 194560
1266094Sian/*- 2210040Scognet * Copyright (c) 1991, 1993 3210040Scognet * The Regents of the University of California. All rights reserved. 4210040Scognet * 5210040Scognet * This code is derived from software contributed to Berkeley by 6210040Scognet * Kenneth Almquist. 7210040Scognet * 8210040Scognet * Redistribution and use in source and binary forms, with or without 9210040Scognet * modification, are permitted provided that the following conditions 10210040Scognet * are met: 11210040Scognet * 1. Redistributions of source code must retain the above copyright 12210040Scognet * notice, this list of conditions and the following disclaimer. 13210040Scognet * 2. Redistributions in binary form must reproduce the above copyright 14236988Simp * notice, this list of conditions and the following disclaimer in the 15236988Simp * documentation and/or other materials provided with the distribution. 16210040Scognet * 4. Neither the name of the University nor the names of its contributors 17210040Scognet * may be used to endorse or promote products derived from this software 18210040Scognet * without specific prior written permission. 19210040Scognet * 20210040Scognet * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21210040Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22210040Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23210040Scognet * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24210040Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25210040Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26210040Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27210040Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28266328Sian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29210040Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30210040Scognet * SUCH DAMAGE. 31210040Scognet */ 32266328Sian 33266328Sian#ifndef lint 34266328Sian#if 0 35266328Sianstatic char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95"; 36266328Sian#endif 37266328Sian#endif /* not lint */ 38266328Sian#include <sys/cdefs.h> 39266328Sian__FBSDID("$FreeBSD: head/bin/sh/redir.c 194560 2009-06-20 20:44:27Z jilles $"); 40266087Sian 41266087Sian#include <sys/types.h> 42266094Sian#include <sys/stat.h> 43266328Sian#include <signal.h> 44266328Sian#include <string.h> 45266328Sian#include <fcntl.h> 46266328Sian#include <errno.h> 47266094Sian#include <unistd.h> 48266094Sian#include <stdlib.h> 49266094Sian 50210040Scognet/* 51266094Sian * Code for dealing with input/output redirection. 52210040Scognet */ 53240572Sjmg 54210040Scognet#include "shell.h" 55266328Sian#include "nodes.h" 56266328Sian#include "jobs.h" 57266328Sian#include "expand.h" 58266328Sian#include "redir.h" 59266328Sian#include "output.h" 60266328Sian#include "memalloc.h" 61266328Sian#include "error.h" 62266328Sian#include "options.h" 63266328Sian 64266328Sian 65266328Sian#define EMPTY -2 /* marks an unused slot in redirtab */ 66266328Sian#define PIPESIZE 4096 /* amount of buffering in a pipe */ 67266328Sian 68210040Scognet 69210040ScognetMKINIT 70210040Scognetstruct redirtab { 71210040Scognet struct redirtab *next; 72210040Scognet int renamed[10]; 73210040Scognet}; 74210040Scognet 75210040Scognet 76238957SimpMKINIT struct redirtab *redirlist; 77210040Scognet 78210040Scognet/* 79210040Scognet * We keep track of whether or not fd0 has been redirected. This is for 80210040Scognet * background commands, where we want to redirect fd0 to /dev/null only 81266328Sian * if it hasn't already been redirected. 82266328Sian*/ 83266328SianSTATIC int fd0_redirected = 0; 84266328Sian 85210040ScognetSTATIC void openredirect(union node *, char[10 ]); 86210040ScognetSTATIC int openhere(union node *); 87210040Scognet 88266094Sian 89210040Scognet/* 90210040Scognet * Process a list of redirection commands. If the REDIR_PUSH flag is set, 91266328Sian * old file descriptors are stashed away so that the redirection can be 92266328Sian * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 93266328Sian * standard output, and the standard error if it becomes a duplicate of 94266328Sian * stdout, is saved in memory. 95266328Sian */ 96266328Sian 97210040Scognetvoid 98210040Scognetredirect(union node *redir, int flags) 99210040Scognet{ 100266328Sian union node *n; 101266328Sian struct redirtab *sv = NULL; 102266328Sian int i; 103266328Sian int fd; 104266328Sian int try; 105266328Sian char memory[10]; /* file descriptors to write to memory */ 106266328Sian 107210040Scognet for (i = 10 ; --i >= 0 ; ) 108266328Sian memory[i] = 0; 109266328Sian memory[1] = flags & REDIR_BACKQ; 110266328Sian if (flags & REDIR_PUSH) { 111266328Sian sv = ckmalloc(sizeof (struct redirtab)); 112210040Scognet for (i = 0 ; i < 10 ; i++) 113266328Sian sv->renamed[i] = EMPTY; 114266328Sian sv->next = redirlist; 115266328Sian redirlist = sv; 116266328Sian } 117210040Scognet for (n = redir ; n ; n = n->nfile.next) { 118266328Sian fd = n->nfile.fd; 119266328Sian try = 0; 120266328Sian if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && 121266328Sian n->ndup.dupfd == fd) 122266328Sian continue; /* redirect from/to same file descriptor */ 123240572Sjmg 124253845Sobrien if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 125266094Sian INTOFF; 126266328Sianagain: 127266094Sian if ((i = fcntl(fd, F_DUPFD, 10)) == -1) { 128266094Sian switch (errno) { 129266094Sian case EBADF: 130266094Sian if (!try) { 131266110Sian openredirect(n, memory); 132266094Sian try++; 133266094Sian goto again; 134266094Sian } 135266094Sian /* FALLTHROUGH*/ 136 default: 137 INTON; 138 error("%d: %s", fd, strerror(errno)); 139 break; 140 } 141 } 142 if (!try) { 143 sv->renamed[fd] = i; 144 } 145 INTON; 146 } 147 if (fd == 0) 148 fd0_redirected++; 149 if (!try) 150 openredirect(n, memory); 151 } 152 if (memory[1]) 153 out1 = &memout; 154 if (memory[2]) 155 out2 = &memout; 156} 157 158 159STATIC void 160openredirect(union node *redir, char memory[10]) 161{ 162 struct stat sb; 163 int fd = redir->nfile.fd; 164 char *fname; 165 int f; 166 167 /* 168 * We suppress interrupts so that we won't leave open file 169 * descriptors around. This may not be such a good idea because 170 * an open of a device or a fifo can block indefinitely. 171 */ 172 INTOFF; 173 memory[fd] = 0; 174 switch (redir->nfile.type) { 175 case NFROM: 176 fname = redir->nfile.expfname; 177 if ((f = open(fname, O_RDONLY)) < 0) 178 error("cannot open %s: %s", fname, strerror(errno)); 179movefd: 180 if (f != fd) { 181 dup2(f, fd); 182 close(f); 183 } 184 break; 185 case NFROMTO: 186 fname = redir->nfile.expfname; 187 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) 188 error("cannot create %s: %s", fname, strerror(errno)); 189 goto movefd; 190 case NTO: 191 if (Cflag) { 192 fname = redir->nfile.expfname; 193 if (stat(fname, &sb) == -1) { 194 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) 195 error("cannot create %s: %s", fname, strerror(errno)); 196 } else if (!S_ISREG(sb.st_mode)) { 197 if ((f = open(fname, O_WRONLY, 0666)) < 0) 198 error("cannot create %s: %s", fname, strerror(errno)); 199 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) { 200 close(f); 201 error("cannot create %s: %s", fname, 202 strerror(EEXIST)); 203 } 204 } else 205 error("cannot create %s: %s", fname, 206 strerror(EEXIST)); 207 goto movefd; 208 } 209 /* FALLTHROUGH */ 210 case NCLOBBER: 211 fname = redir->nfile.expfname; 212 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 213 error("cannot create %s: %s", fname, strerror(errno)); 214 goto movefd; 215 case NAPPEND: 216 fname = redir->nfile.expfname; 217 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 218 error("cannot create %s: %s", fname, strerror(errno)); 219 goto movefd; 220 case NTOFD: 221 case NFROMFD: 222 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 223 if (memory[redir->ndup.dupfd]) 224 memory[fd] = 1; 225 else 226 dup2(redir->ndup.dupfd, fd); 227 } else { 228 close(fd); 229 } 230 break; 231 case NHERE: 232 case NXHERE: 233 f = openhere(redir); 234 goto movefd; 235 default: 236 abort(); 237 } 238 INTON; 239} 240 241 242/* 243 * Handle here documents. Normally we fork off a process to write the 244 * data to a pipe. If the document is short, we can stuff the data in 245 * the pipe without forking. 246 */ 247 248STATIC int 249openhere(union node *redir) 250{ 251 int pip[2]; 252 int len = 0; 253 254 if (pipe(pip) < 0) 255 error("Pipe call failed: %s", strerror(errno)); 256 if (redir->type == NHERE) { 257 len = strlen(redir->nhere.doc->narg.text); 258 if (len <= PIPESIZE) { 259 xwrite(pip[1], redir->nhere.doc->narg.text, len); 260 goto out; 261 } 262 } 263 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 264 close(pip[0]); 265 signal(SIGINT, SIG_IGN); 266 signal(SIGQUIT, SIG_IGN); 267 signal(SIGHUP, SIG_IGN); 268 signal(SIGTSTP, SIG_IGN); 269 signal(SIGPIPE, SIG_DFL); 270 if (redir->type == NHERE) 271 xwrite(pip[1], redir->nhere.doc->narg.text, len); 272 else 273 expandhere(redir->nhere.doc, pip[1]); 274 _exit(0); 275 } 276out: 277 close(pip[1]); 278 return pip[0]; 279} 280 281 282 283/* 284 * Undo the effects of the last redirection. 285 */ 286 287void 288popredir(void) 289{ 290 struct redirtab *rp = redirlist; 291 int i; 292 293 for (i = 0 ; i < 10 ; i++) { 294 if (rp->renamed[i] != EMPTY) { 295 if (i == 0) 296 fd0_redirected--; 297 if (rp->renamed[i] >= 0) { 298 dup2(rp->renamed[i], i); 299 close(rp->renamed[i]); 300 } else { 301 close(i); 302 } 303 } 304 } 305 INTOFF; 306 redirlist = rp->next; 307 ckfree(rp); 308 INTON; 309} 310 311/* 312 * Undo all redirections. Called on error or interrupt. 313 */ 314 315#ifdef mkinit 316 317INCLUDE "redir.h" 318 319RESET { 320 while (redirlist) 321 popredir(); 322} 323 324SHELLPROC { 325 clearredir(); 326} 327 328#endif 329 330/* Return true if fd 0 has already been redirected at least once. */ 331int 332fd0_redirected_p(void) 333{ 334 return fd0_redirected != 0; 335} 336 337/* 338 * Discard all saved file descriptors. 339 */ 340 341void 342clearredir(void) 343{ 344 struct redirtab *rp; 345 int i; 346 347 for (rp = redirlist ; rp ; rp = rp->next) { 348 for (i = 0 ; i < 10 ; i++) { 349 if (rp->renamed[i] >= 0) { 350 close(rp->renamed[i]); 351 } 352 rp->renamed[i] = EMPTY; 353 } 354 } 355} 356