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 39 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95"; 40 #endif 41 static const char rcsid[] = 42 "$FreeBSD$"; 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <signal.h> 47 #include <string.h> 48 #include <fcntl.h> 49 #include <errno.h> 50 #include <unistd.h> 51 #include <stdlib.h> 52 53 /* 54 * Code for dealing with input/output redirection. 55 */ 56 57 #include "shell.h" 58 #include "nodes.h" 59 #include "jobs.h" 60 #include "expand.h" 61 #include "redir.h" 62 #include "output.h" 63 #include "memalloc.h" 64 #include "error.h" 65 66 67 #define EMPTY -2 /* marks an unused slot in redirtab */ 68 #define PIPESIZE 4096 /* amount of buffering in a pipe */ 69 70 71 MKINIT 72 struct redirtab { 73 struct redirtab *next; 74 short renamed[10]; 75 }; 76 77 78 MKINIT struct redirtab *redirlist; 79 80 /* 81 * We keep track of whether or not fd0 has been redirected. This is for 82 * background commands, where we want to redirect fd0 to /dev/null only 83 * if it hasn't already been redirected. 84 */ 85 int fd0_redirected = 0; 86 87 STATIC void openredirect(union node *, char[10 ]); 88 STATIC int openhere(union node *); 89 90 91 /* 92 * Process a list of redirection commands. If the REDIR_PUSH flag is set, 93 * old file descriptors are stashed away so that the redirection can be 94 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 95 * standard output, and the standard error if it becomes a duplicate of 96 * stdout, is saved in memory. 97 */ 98 99 void 100 redirect(union node *redir, int flags) 101 { 102 union node *n; 103 struct redirtab *sv = NULL; 104 int i; 105 int fd; 106 int try; 107 char memory[10]; /* file descriptors to write to memory */ 108 109 for (i = 10 ; --i >= 0 ; ) 110 memory[i] = 0; 111 memory[1] = flags & REDIR_BACKQ; 112 if (flags & REDIR_PUSH) { 113 sv = ckmalloc(sizeof (struct redirtab)); 114 for (i = 0 ; i < 10 ; i++) 115 sv->renamed[i] = EMPTY; 116 sv->next = redirlist; 117 redirlist = sv; 118 } 119 for (n = redir ; n ; n = n->nfile.next) { 120 fd = n->nfile.fd; 121 try = 0; 122 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && 123 n->ndup.dupfd == fd) 124 continue; /* redirect from/to same file descriptor */ 125 126 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 127 INTOFF; 128 again: 129 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) { 130 switch (errno) { 131 case EBADF: 132 if (!try) { 133 openredirect(n, memory); 134 try++; 135 goto again; 136 } 137 /* FALLTHROUGH*/ 138 default: 139 INTON; 140 error("%d: %s", fd, strerror(errno)); 141 break; 142 } 143 } 144 if (!try) { 145 sv->renamed[fd] = i; 146 } 147 INTON; 148 } 149 if (fd == 0) 150 fd0_redirected++; 151 if (!try) 152 openredirect(n, memory); 153 } 154 if (memory[1]) 155 out1 = &memout; 156 if (memory[2]) 157 out2 = &memout; 158 } 159 160 161 STATIC void 162 openredirect(union node *redir, char memory[10]) 163 { 164 int fd = redir->nfile.fd; 165 char *fname; 166 int f; 167 168 /* 169 * We suppress interrupts so that we won't leave open file 170 * descriptors around. This may not be such a good idea because 171 * an open of a device or a fifo can block indefinitely. 172 */ 173 INTOFF; 174 memory[fd] = 0; 175 switch (redir->nfile.type) { 176 case NFROM: 177 fname = redir->nfile.expfname; 178 if ((f = open(fname, O_RDONLY)) < 0) 179 error("cannot open %s: %s", fname, errmsg(errno, E_OPEN)); 180 movefd: 181 if (f != fd) { 182 close(fd); 183 copyfd(f, fd); 184 close(f); 185 } 186 break; 187 case NFROMTO: 188 fname = redir->nfile.expfname; 189 #ifdef O_CREAT 190 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) 191 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 192 #else 193 if ((f = open(fname, O_RDWR, 0666)) < 0) { 194 if (errno != ENOENT) 195 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 196 else if ((f = creat(fname, 0666)) < 0) 197 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 198 else { 199 close(f); 200 if ((f = open(fname, O_RDWR)) < 0) { 201 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 202 remove(fname); 203 } 204 } 205 } 206 #endif 207 goto movefd; 208 case NTO: 209 fname = redir->nfile.expfname; 210 #ifdef O_CREAT 211 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 212 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 213 #else 214 if ((f = creat(fname, 0666)) < 0) 215 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 216 #endif 217 goto movefd; 218 case NAPPEND: 219 fname = redir->nfile.expfname; 220 #ifdef O_APPEND 221 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 222 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 223 #else 224 if ((f = open(fname, O_WRONLY)) < 0 225 && (f = creat(fname, 0666)) < 0) 226 error("cannot create %s: %s", fname, errmsg(errno, E_CREAT)); 227 lseek(f, (off_t)0, 2); 228 #endif 229 goto movefd; 230 case NTOFD: 231 case NFROMFD: 232 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 233 if (memory[redir->ndup.dupfd]) 234 memory[fd] = 1; 235 else { 236 close(fd); 237 copyfd(redir->ndup.dupfd, fd); 238 } 239 } 240 break; 241 case NHERE: 242 case NXHERE: 243 f = openhere(redir); 244 goto movefd; 245 default: 246 abort(); 247 } 248 INTON; 249 } 250 251 252 /* 253 * Handle here documents. Normally we fork off a process to write the 254 * data to a pipe. If the document is short, we can stuff the data in 255 * the pipe without forking. 256 */ 257 258 STATIC int 259 openhere(union node *redir) 260 { 261 int pip[2]; 262 int len = 0; 263 264 if (pipe(pip) < 0) 265 error("Pipe call failed: %s", strerror(errno)); 266 if (redir->type == NHERE) { 267 len = strlen(redir->nhere.doc->narg.text); 268 if (len <= PIPESIZE) { 269 xwrite(pip[1], redir->nhere.doc->narg.text, len); 270 goto out; 271 } 272 } 273 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 274 close(pip[0]); 275 signal(SIGINT, SIG_IGN); 276 signal(SIGQUIT, SIG_IGN); 277 signal(SIGHUP, SIG_IGN); 278 #ifdef SIGTSTP 279 signal(SIGTSTP, SIG_IGN); 280 #endif 281 signal(SIGPIPE, SIG_DFL); 282 if (redir->type == NHERE) 283 xwrite(pip[1], redir->nhere.doc->narg.text, len); 284 else 285 expandhere(redir->nhere.doc, pip[1]); 286 _exit(0); 287 } 288 out: 289 close(pip[1]); 290 return pip[0]; 291 } 292 293 294 295 /* 296 * Undo the effects of the last redirection. 297 */ 298 299 void 300 popredir(void) 301 { 302 struct redirtab *rp = redirlist; 303 int i; 304 305 for (i = 0 ; i < 10 ; i++) { 306 if (rp->renamed[i] != EMPTY) { 307 if (i == 0) 308 fd0_redirected--; 309 close(i); 310 if (rp->renamed[i] >= 0) { 311 copyfd(rp->renamed[i], i); 312 close(rp->renamed[i]); 313 } 314 } 315 } 316 INTOFF; 317 redirlist = rp->next; 318 ckfree(rp); 319 INTON; 320 } 321 322 /* 323 * Undo all redirections. Called on error or interrupt. 324 */ 325 326 #ifdef mkinit 327 328 INCLUDE "redir.h" 329 330 RESET { 331 while (redirlist) 332 popredir(); 333 } 334 335 SHELLPROC { 336 clearredir(); 337 } 338 339 #endif 340 341 /* Return true if fd 0 has already been redirected at least once. */ 342 int 343 fd0_redirected_p(void) 344 { 345 return fd0_redirected != 0; 346 } 347 348 /* 349 * Discard all saved file descriptors. 350 */ 351 352 void 353 clearredir(void) 354 { 355 struct redirtab *rp; 356 int i; 357 358 for (rp = redirlist ; rp ; rp = rp->next) { 359 for (i = 0 ; i < 10 ; i++) { 360 if (rp->renamed[i] >= 0) { 361 close(rp->renamed[i]); 362 } 363 rp->renamed[i] = EMPTY; 364 } 365 } 366 } 367 368 369 370 /* 371 * Copy a file descriptor to be >= to. Returns -1 372 * if the source file descriptor is closed, EMPTY if there are no unused 373 * file descriptors left. 374 */ 375 376 int 377 copyfd(int from, int to) 378 { 379 int newfd; 380 381 newfd = fcntl(from, F_DUPFD, to); 382 if (newfd < 0) { 383 if (errno == EMFILE) 384 return EMPTY; 385 else 386 error("%d: %s", from, strerror(errno)); 387 } 388 return newfd; 389 } 390