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 <sys/types.h>
36 #include <sys/stat.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43
44 /*
45 * Code for dealing with input/output redirection.
46 */
47
48 #include "shell.h"
49 #include "nodes.h"
50 #include "jobs.h"
51 #include "expand.h"
52 #include "redir.h"
53 #include "output.h"
54 #include "memalloc.h"
55 #include "error.h"
56 #include "options.h"
57
58
59 #define EMPTY -2 /* marks an unused slot in redirtab */
60 #define CLOSED -1 /* fd was not open before redir */
61
62
63 struct redirtab {
64 struct redirtab *next;
65 int renamed[10];
66 int fd0_redirected;
67 unsigned int empty_redirs;
68 };
69
70
71 static struct redirtab *redirlist;
72
73 /*
74 * We keep track of whether or not fd0 has been redirected. This is for
75 * background commands, where we want to redirect fd0 to /dev/null only
76 * if it hasn't already been redirected.
77 */
78 static int fd0_redirected = 0;
79
80 /* Number of redirtabs that have not been allocated. */
81 static unsigned int empty_redirs = 0;
82
83 static void openredirect(union node *, char[10 ]);
84 static int openhere(union node *);
85
86
87 /*
88 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
89 * old file descriptors are stashed away so that the redirection can be
90 * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
91 * standard output, and the standard error if it becomes a duplicate of
92 * stdout, is saved in memory.
93 *
94 * We suppress interrupts so that we won't leave open file
95 * descriptors around. Because the signal handler remains
96 * installed and we do not use system call restart, interrupts
97 * will still abort blocking opens such as fifos (they will fail
98 * with EINTR). There is, however, a race condition if an interrupt
99 * arrives after INTOFF and before open blocks.
100 */
101
102 void
redirect(union node * redir,int flags)103 redirect(union node *redir, int flags)
104 {
105 union node *n;
106 struct redirtab *sv = NULL;
107 int i;
108 int fd;
109 char memory[10]; /* file descriptors to write to memory */
110
111 INTOFF;
112 for (i = 10 ; --i >= 0 ; )
113 memory[i] = 0;
114 memory[1] = flags & REDIR_BACKQ;
115 if (flags & REDIR_PUSH) {
116 empty_redirs++;
117 if (redir != NULL) {
118 sv = ckmalloc(sizeof (struct redirtab));
119 for (i = 0 ; i < 10 ; i++)
120 sv->renamed[i] = EMPTY;
121 sv->fd0_redirected = fd0_redirected;
122 sv->empty_redirs = empty_redirs - 1;
123 sv->next = redirlist;
124 redirlist = sv;
125 empty_redirs = 0;
126 }
127 }
128 for (n = redir ; n ; n = n->nfile.next) {
129 fd = n->nfile.fd;
130 if (fd == 0)
131 fd0_redirected = 1;
132 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
133 n->ndup.dupfd == fd)
134 continue; /* redirect from/to same file descriptor */
135
136 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
137 INTOFF;
138 if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) {
139 switch (errno) {
140 case EBADF:
141 i = CLOSED;
142 break;
143 default:
144 INTON;
145 error("%d: %s", fd, strerror(errno));
146 break;
147 }
148 }
149 sv->renamed[fd] = i;
150 INTON;
151 }
152 openredirect(n, memory);
153 INTON;
154 INTOFF;
155 }
156 if (memory[1])
157 out1 = &memout;
158 if (memory[2])
159 out2 = &memout;
160 INTON;
161 }
162
163
164 static void
openredirect(union node * redir,char memory[10])165 openredirect(union node *redir, char memory[10])
166 {
167 struct stat sb;
168 int fd = redir->nfile.fd;
169 const char *fname;
170 int f;
171 int e;
172
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));
179 break;
180 case NFROMTO:
181 fname = redir->nfile.expfname;
182 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
183 error("cannot create %s: %s", fname, strerror(errno));
184 break;
185 case NTO:
186 if (Cflag) {
187 fname = redir->nfile.expfname;
188 if (stat(fname, &sb) == -1) {
189 if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
190 error("cannot create %s: %s", fname, strerror(errno));
191 } else if (!S_ISREG(sb.st_mode)) {
192 if ((f = open(fname, O_WRONLY, 0666)) < 0)
193 error("cannot create %s: %s", fname, strerror(errno));
194 if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
195 close(f);
196 error("cannot create %s: %s", fname,
197 strerror(EEXIST));
198 }
199 } else
200 error("cannot create %s: %s", fname,
201 strerror(EEXIST));
202 break;
203 }
204 /* FALLTHROUGH */
205 case NCLOBBER:
206 fname = redir->nfile.expfname;
207 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
208 error("cannot create %s: %s", fname, strerror(errno));
209 break;
210 case NAPPEND:
211 fname = redir->nfile.expfname;
212 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
213 error("cannot create %s: %s", fname, strerror(errno));
214 break;
215 case NTOFD:
216 case NFROMFD:
217 if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
218 if (memory[redir->ndup.dupfd])
219 memory[fd] = 1;
220 else {
221 if (dup2(redir->ndup.dupfd, fd) < 0)
222 error("%d: %s", redir->ndup.dupfd,
223 strerror(errno));
224 }
225 } else {
226 close(fd);
227 }
228 return;
229 case NHERE:
230 case NXHERE:
231 f = openhere(redir);
232 break;
233 default:
234 abort();
235 }
236 if (f != fd) {
237 if (dup2(f, fd) == -1) {
238 e = errno;
239 close(f);
240 error("%d: %s", fd, strerror(e));
241 }
242 close(f);
243 }
244 }
245
246
247 /*
248 * Handle here documents. Normally we fork off a process to write the
249 * data to a pipe. If the document is short, we can stuff the data in
250 * the pipe without forking.
251 */
252
253 static int
openhere(union node * redir)254 openhere(union node *redir)
255 {
256 const char *p;
257 int pip[2];
258 size_t len = 0;
259 int flags;
260 ssize_t written = 0;
261
262 if (pipe(pip) < 0)
263 error("Pipe call failed: %s", strerror(errno));
264
265 if (redir->type == NXHERE)
266 p = redir->nhere.expdoc;
267 else
268 p = redir->nhere.doc->narg.text;
269 len = strlen(p);
270 if (len == 0)
271 goto out;
272 flags = fcntl(pip[1], F_GETFL, 0);
273 if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) {
274 written = write(pip[1], p, len);
275 if (written < 0)
276 written = 0;
277 if ((size_t)written == len)
278 goto out;
279 fcntl(pip[1], F_SETFL, flags);
280 }
281
282 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
283 close(pip[0]);
284 signal(SIGINT, SIG_IGN);
285 signal(SIGQUIT, SIG_IGN);
286 signal(SIGHUP, SIG_IGN);
287 signal(SIGTSTP, SIG_IGN);
288 signal(SIGPIPE, SIG_DFL);
289 xwrite(pip[1], p + written, len - written);
290 _exit(0);
291 }
292 out:
293 close(pip[1]);
294 return pip[0];
295 }
296
297
298
299 /*
300 * Undo the effects of the last redirection.
301 */
302
303 void
popredir(void)304 popredir(void)
305 {
306 struct redirtab *rp = redirlist;
307 int i;
308
309 INTOFF;
310 if (empty_redirs > 0) {
311 empty_redirs--;
312 INTON;
313 return;
314 }
315 for (i = 0 ; i < 10 ; i++) {
316 if (rp->renamed[i] != EMPTY) {
317 if (rp->renamed[i] >= 0) {
318 dup2(rp->renamed[i], i);
319 close(rp->renamed[i]);
320 } else {
321 close(i);
322 }
323 }
324 }
325 fd0_redirected = rp->fd0_redirected;
326 empty_redirs = rp->empty_redirs;
327 redirlist = rp->next;
328 ckfree(rp);
329 INTON;
330 }
331
332 /* Return true if fd 0 has already been redirected at least once. */
333 int
fd0_redirected_p(void)334 fd0_redirected_p(void)
335 {
336 return fd0_redirected != 0;
337 }
338
339 /*
340 * Discard all saved file descriptors.
341 */
342
343 void
clearredir(void)344 clearredir(void)
345 {
346 struct redirtab *rp;
347 int i;
348
349 for (rp = redirlist ; rp ; rp = rp->next) {
350 for (i = 0 ; i < 10 ; i++) {
351 if (rp->renamed[i] >= 0) {
352 close(rp->renamed[i]);
353 }
354 rp->renamed[i] = EMPTY;
355 }
356 }
357 }
358