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