Merge branch 'vendor/OPENSSL'
[dragonfly.git] / bin / sh / redir.c
CommitLineData
984263bc
MD
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.
dc71b7ab 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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.
1de703da
MD
31 *
32 * @(#)redir.c 8.2 (Berkeley) 5/4/95
b1abb130 33 * $FreeBSD: head/bin/sh/redir.c 253658 2013-07-25 19:48:15Z jilles $
984263bc
MD
34 */
35
984263bc
MD
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <signal.h>
39#include <string.h>
40#include <fcntl.h>
41#include <errno.h>
42#include <unistd.h>
43#include <stdlib.h>
44
45/*
46 * Code for dealing with input/output redirection.
47 */
48
49#include "shell.h"
50#include "nodes.h"
51#include "jobs.h"
52#include "expand.h"
53#include "redir.h"
54#include "output.h"
55#include "memalloc.h"
56#include "error.h"
57#include "options.h"
58
59
60#define EMPTY -2 /* marks an unused slot in redirtab */
99512ac4 61#define CLOSED -1 /* fd was not open before redir */
984263bc
MD
62
63
984263bc
MD
64struct redirtab {
65 struct redirtab *next;
66 int renamed[10];
67};
68
69
b1abb130 70static struct redirtab *redirlist;
984263bc
MD
71
72/*
73 * We keep track of whether or not fd0 has been redirected. This is for
74 * background commands, where we want to redirect fd0 to /dev/null only
75 * if it hasn't already been redirected.
76*/
99512ac4 77static int fd0_redirected = 0;
984263bc 78
99512ac4
PA
79static void openredirect(union node *, char[10 ]);
80static int openhere(union node *);
984263bc
MD
81
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
91void
92redirect(union node *redir, int flags)
93{
94 union node *n;
95 struct redirtab *sv = NULL;
96 int i;
97 int fd;
984263bc
MD
98 char memory[10]; /* file descriptors to write to memory */
99
100 for (i = 10 ; --i >= 0 ; )
101 memory[i] = 0;
102 memory[1] = flags & REDIR_BACKQ;
103 if (flags & REDIR_PUSH) {
104 sv = ckmalloc(sizeof (struct redirtab));
105 for (i = 0 ; i < 10 ; i++)
106 sv->renamed[i] = EMPTY;
107 sv->next = redirlist;
108 redirlist = sv;
109 }
110 for (n = redir ; n ; n = n->nfile.next) {
111 fd = n->nfile.fd;
984263bc
MD
112 if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
113 n->ndup.dupfd == fd)
114 continue; /* redirect from/to same file descriptor */
115
116 if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
117 INTOFF;
39a1ee09
PA
118#ifndef F_DUPFD_CLOEXEC
119 if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
120#else
b1abb130 121 if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) {
39a1ee09 122#endif
984263bc
MD
123 switch (errno) {
124 case EBADF:
99512ac4
PA
125 i = CLOSED;
126 break;
984263bc
MD
127 default:
128 INTON;
129 error("%d: %s", fd, strerror(errno));
130 break;
131 }
b1abb130 132 }
39a1ee09
PA
133#if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC)
134 else
135 fcntl(i, F_SETFD, FD_CLOEXEC);
136#endif
99512ac4 137 sv->renamed[fd] = i;
984263bc
MD
138 INTON;
139 }
140 if (fd == 0)
141 fd0_redirected++;
99512ac4 142 openredirect(n, memory);
984263bc
MD
143 }
144 if (memory[1])
145 out1 = &memout;
146 if (memory[2])
147 out2 = &memout;
148}
149
150
99512ac4 151static void
984263bc
MD
152openredirect(union node *redir, char memory[10])
153{
154 struct stat sb;
155 int fd = redir->nfile.fd;
156 char *fname;
99512ac4
PA
157 int f = 0;
158 int e;
984263bc
MD
159
160 /*
161 * We suppress interrupts so that we won't leave open file
99512ac4
PA
162 * descriptors around. Because the signal handler remains
163 * installed and we do not use system call restart, interrupts
164 * will still abort blocking opens such as fifos (they will fail
165 * with EINTR). There is, however, a race condition if an interrupt
166 * arrives after INTOFF and before open blocks.
984263bc
MD
167 */
168 INTOFF;
169 memory[fd] = 0;
170 switch (redir->nfile.type) {
171 case NFROM:
172 fname = redir->nfile.expfname;
173 if ((f = open(fname, O_RDONLY)) < 0)
010f7689 174 error("cannot open %s: %s", fname, strerror(errno));
984263bc
MD
175movefd:
176 if (f != fd) {
99512ac4
PA
177 if (dup2(f, fd) == -1) {
178 e = errno;
179 close(f);
180 error("%d: %s", fd, strerror(e));
181 }
984263bc
MD
182 close(f);
183 }
184 break;
185 case NFROMTO:
186 fname = redir->nfile.expfname;
984263bc 187 if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
010f7689 188 error("cannot create %s: %s", fname, strerror(errno));
984263bc
MD
189 goto movefd;
190 case NTO:
99512ac4
PA
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 */
984263bc
MD
210 case NCLOBBER:
211 fname = redir->nfile.expfname;
212 if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
010f7689 213 error("cannot create %s: %s", fname, strerror(errno));
984263bc
MD
214 goto movefd;
215 case NAPPEND:
216 fname = redir->nfile.expfname;
984263bc 217 if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
010f7689 218 error("cannot create %s: %s", fname, strerror(errno));
984263bc
MD
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;
99512ac4
PA
225 else {
226 if (dup2(redir->ndup.dupfd, fd) < 0)
227 error("%d: %s", redir->ndup.dupfd,
228 strerror(errno));
229 }
996bd0d6 230 } else {
984263bc 231 close(fd);
996bd0d6 232 }
984263bc
MD
233 break;
234 case NHERE:
235 case NXHERE:
236 f = openhere(redir);
237 goto movefd;
238 default:
239 abort();
240 }
241 INTON;
242}
243
244
245/*
246 * Handle here documents. Normally we fork off a process to write the
247 * data to a pipe. If the document is short, we can stuff the data in
248 * the pipe without forking.
249 */
250
99512ac4 251static int
984263bc
MD
252openhere(union node *redir)
253{
f00eae14 254 char *p;
984263bc 255 int pip[2];
b1abb130
PA
256 size_t len = 0;
257 int flags;
258 ssize_t written = 0;
984263bc
MD
259
260 if (pipe(pip) < 0)
261 error("Pipe call failed: %s", strerror(errno));
f00eae14
PA
262
263 if (redir->type == NXHERE)
264 p = redir->nhere.expdoc;
265 else
266 p = redir->nhere.doc->narg.text;
267 len = strlen(p);
b1abb130 268 if (len == 0)
f00eae14 269 goto out;
b1abb130
PA
270 flags = fcntl(pip[1], F_GETFL, 0);
271 if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) {
272 written = write(pip[1], p, len);
273 if (written < 0)
274 written = 0;
275 if ((size_t)written == len)
276 goto out;
277 fcntl(pip[1], F_SETFL, flags);
984263bc 278 }
f00eae14 279
2038fb68 280 if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
984263bc
MD
281 close(pip[0]);
282 signal(SIGINT, SIG_IGN);
283 signal(SIGQUIT, SIG_IGN);
284 signal(SIGHUP, SIG_IGN);
984263bc 285 signal(SIGTSTP, SIG_IGN);
984263bc 286 signal(SIGPIPE, SIG_DFL);
b1abb130 287 xwrite(pip[1], p + written, len - written);
984263bc
MD
288 _exit(0);
289 }
290out:
291 close(pip[1]);
292 return pip[0];
293}
294
295
296
297/*
298 * Undo the effects of the last redirection.
299 */
300
301void
302popredir(void)
303{
304 struct redirtab *rp = redirlist;
305 int i;
306
307 for (i = 0 ; i < 10 ; i++) {
308 if (rp->renamed[i] != EMPTY) {
309 if (i == 0)
310 fd0_redirected--;
984263bc 311 if (rp->renamed[i] >= 0) {
996bd0d6 312 dup2(rp->renamed[i], i);
984263bc 313 close(rp->renamed[i]);
996bd0d6
PA
314 } else {
315 close(i);
984263bc
MD
316 }
317 }
318 }
319 INTOFF;
320 redirlist = rp->next;
321 ckfree(rp);
322 INTON;
323}
324
325/*
326 * Undo all redirections. Called on error or interrupt.
327 */
328
b1abb130
PA
329void
330resetredir(void)
331{
984263bc
MD
332 while (redirlist)
333 popredir();
334}
335
984263bc
MD
336
337/* Return true if fd 0 has already been redirected at least once. */
338int
339fd0_redirected_p(void)
340{
341 return fd0_redirected != 0;
342}
343
344/*
345 * Discard all saved file descriptors.
346 */
347
348void
349clearredir(void)
350{
351 struct redirtab *rp;
352 int i;
353
354 for (rp = redirlist ; rp ; rp = rp->next) {
355 for (i = 0 ; i < 10 ; i++) {
356 if (rp->renamed[i] >= 0) {
357 close(rp->renamed[i]);
358 }
359 rp->renamed[i] = EMPTY;
360 }
361 }
362}