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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95"; 36 #endif 37 #endif /* not lint */ 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <signal.h> 44 #include <string.h> 45 #include <fcntl.h> 46 #include <errno.h> 47 #include <unistd.h> 48 #include <stdlib.h> 49 50 /* 51 * Code for dealing with input/output redirection. 52 */ 53 54 #include "shell.h" 55 #include "nodes.h" 56 #include "jobs.h" 57 #include "expand.h" 58 #include "redir.h" 59 #include "output.h" 60 #include "memalloc.h" 61 #include "error.h" 62 #include "options.h" 63 64 65 #define EMPTY -2 /* marks an unused slot in redirtab */ 66 #define CLOSED -1 /* fd was not open before redir */ 67 68 69 struct redirtab { 70 struct redirtab *next; 71 int renamed[10]; 72 }; 73 74 75 static struct redirtab *redirlist; 76 77 /* 78 * We keep track of whether or not fd0 has been redirected. This is for 79 * background commands, where we want to redirect fd0 to /dev/null only 80 * if it hasn't already been redirected. 81 */ 82 static int fd0_redirected = 0; 83 84 static void openredirect(union node *, char[10 ]); 85 static int openhere(union node *); 86 87 88 /* 89 * Process a list of redirection commands. If the REDIR_PUSH flag is set, 90 * old file descriptors are stashed away so that the redirection can be 91 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the 92 * standard output, and the standard error if it becomes a duplicate of 93 * stdout, is saved in memory. 94 */ 95 96 void 97 redirect(union node *redir, int flags) 98 { 99 union node *n; 100 struct redirtab *sv = NULL; 101 int i; 102 int fd; 103 char memory[10]; /* file descriptors to write to memory */ 104 105 for (i = 10 ; --i >= 0 ; ) 106 memory[i] = 0; 107 memory[1] = flags & REDIR_BACKQ; 108 if (flags & REDIR_PUSH) { 109 sv = ckmalloc(sizeof (struct redirtab)); 110 for (i = 0 ; i < 10 ; i++) 111 sv->renamed[i] = EMPTY; 112 sv->next = redirlist; 113 redirlist = sv; 114 } 115 for (n = redir ; n ; n = n->nfile.next) { 116 fd = n->nfile.fd; 117 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) && 118 n->ndup.dupfd == fd) 119 continue; /* redirect from/to same file descriptor */ 120 121 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) { 122 INTOFF; 123 if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) { 124 switch (errno) { 125 case EBADF: 126 i = CLOSED; 127 break; 128 default: 129 INTON; 130 error("%d: %s", fd, strerror(errno)); 131 break; 132 } 133 } 134 sv->renamed[fd] = i; 135 INTON; 136 } 137 if (fd == 0) 138 fd0_redirected++; 139 openredirect(n, memory); 140 } 141 if (memory[1]) 142 out1 = &memout; 143 if (memory[2]) 144 out2 = &memout; 145 } 146 147 148 static void 149 openredirect(union node *redir, char memory[10]) 150 { 151 struct stat sb; 152 int fd = redir->nfile.fd; 153 char *fname; 154 int f; 155 int e; 156 157 /* 158 * We suppress interrupts so that we won't leave open file 159 * descriptors around. Because the signal handler remains 160 * installed and we do not use system call restart, interrupts 161 * will still abort blocking opens such as fifos (they will fail 162 * with EINTR). There is, however, a race condition if an interrupt 163 * arrives after INTOFF and before open blocks. 164 */ 165 INTOFF; 166 memory[fd] = 0; 167 switch (redir->nfile.type) { 168 case NFROM: 169 fname = redir->nfile.expfname; 170 if ((f = open(fname, O_RDONLY)) < 0) 171 error("cannot open %s: %s", fname, strerror(errno)); 172 movefd: 173 if (f != fd) { 174 if (dup2(f, fd) == -1) { 175 e = errno; 176 close(f); 177 error("%d: %s", fd, strerror(e)); 178 } 179 close(f); 180 } 181 break; 182 case NFROMTO: 183 fname = redir->nfile.expfname; 184 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0) 185 error("cannot create %s: %s", fname, strerror(errno)); 186 goto movefd; 187 case NTO: 188 if (Cflag) { 189 fname = redir->nfile.expfname; 190 if (stat(fname, &sb) == -1) { 191 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) 192 error("cannot create %s: %s", fname, strerror(errno)); 193 } else if (!S_ISREG(sb.st_mode)) { 194 if ((f = open(fname, O_WRONLY, 0666)) < 0) 195 error("cannot create %s: %s", fname, strerror(errno)); 196 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) { 197 close(f); 198 error("cannot create %s: %s", fname, 199 strerror(EEXIST)); 200 } 201 } else 202 error("cannot create %s: %s", fname, 203 strerror(EEXIST)); 204 goto movefd; 205 } 206 /* FALLTHROUGH */ 207 case NCLOBBER: 208 fname = redir->nfile.expfname; 209 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) 210 error("cannot create %s: %s", fname, strerror(errno)); 211 goto movefd; 212 case NAPPEND: 213 fname = redir->nfile.expfname; 214 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) 215 error("cannot create %s: %s", fname, strerror(errno)); 216 goto movefd; 217 case NTOFD: 218 case NFROMFD: 219 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */ 220 if (memory[redir->ndup.dupfd]) 221 memory[fd] = 1; 222 else { 223 if (dup2(redir->ndup.dupfd, fd) < 0) 224 error("%d: %s", redir->ndup.dupfd, 225 strerror(errno)); 226 } 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 248 static int 249 openhere(union node *redir) 250 { 251 char *p; 252 int pip[2]; 253 size_t len = 0; 254 int flags; 255 ssize_t written = 0; 256 257 if (pipe(pip) < 0) 258 error("Pipe call failed: %s", strerror(errno)); 259 260 if (redir->type == NXHERE) 261 p = redir->nhere.expdoc; 262 else 263 p = redir->nhere.doc->narg.text; 264 len = strlen(p); 265 if (len == 0) 266 goto out; 267 flags = fcntl(pip[1], F_GETFL, 0); 268 if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) { 269 written = write(pip[1], p, len); 270 if (written < 0) 271 written = 0; 272 if ((size_t)written == len) 273 goto out; 274 fcntl(pip[1], F_SETFL, flags); 275 } 276 277 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { 278 close(pip[0]); 279 signal(SIGINT, SIG_IGN); 280 signal(SIGQUIT, SIG_IGN); 281 signal(SIGHUP, SIG_IGN); 282 signal(SIGTSTP, SIG_IGN); 283 signal(SIGPIPE, SIG_DFL); 284 xwrite(pip[1], p + written, len - written); 285 _exit(0); 286 } 287 out: 288 close(pip[1]); 289 return pip[0]; 290 } 291 292 293 294 /* 295 * Undo the effects of the last redirection. 296 */ 297 298 void 299 popredir(void) 300 { 301 struct redirtab *rp = redirlist; 302 int i; 303 304 for (i = 0 ; i < 10 ; i++) { 305 if (rp->renamed[i] != EMPTY) { 306 if (i == 0) 307 fd0_redirected--; 308 if (rp->renamed[i] >= 0) { 309 dup2(rp->renamed[i], i); 310 close(rp->renamed[i]); 311 } else { 312 close(i); 313 } 314 } 315 } 316 INTOFF; 317 redirlist = rp->next; 318 ckfree(rp); 319 INTON; 320 } 321 322 /* Return true if fd 0 has already been redirected at least once. */ 323 int 324 fd0_redirected_p(void) 325 { 326 return fd0_redirected != 0; 327 } 328 329 /* 330 * Discard all saved file descriptors. 331 */ 332 333 void 334 clearredir(void) 335 { 336 struct redirtab *rp; 337 int i; 338 339 for (rp = redirlist ; rp ; rp = rp->next) { 340 for (i = 0 ; i < 10 ; i++) { 341 if (rp->renamed[i] >= 0) { 342 close(rp->renamed[i]); 343 } 344 rp->renamed[i] = EMPTY; 345 } 346 } 347 } 348