redir.c revision 1.62 1 1.62 kamil /* $NetBSD: redir.c,v 1.62 2018/11/26 20:03:39 kamil Exp $ */
2 1.11 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1991, 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.28 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.16 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.11 cgd #if 0
38 1.12 christos static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
39 1.11 cgd #else
40 1.62 kamil __RCSID("$NetBSD: redir.c,v 1.62 2018/11/26 20:03:39 kamil Exp $");
41 1.11 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.12 christos #include <sys/types.h>
45 1.17 christos #include <sys/param.h> /* PIPE_BUF */
46 1.48 christos #include <sys/stat.h>
47 1.12 christos #include <signal.h>
48 1.12 christos #include <string.h>
49 1.12 christos #include <fcntl.h>
50 1.12 christos #include <errno.h>
51 1.12 christos #include <unistd.h>
52 1.12 christos #include <stdlib.h>
53 1.12 christos
54 1.1 cgd /*
55 1.1 cgd * Code for dealing with input/output redirection.
56 1.1 cgd */
57 1.1 cgd
58 1.25 christos #include "main.h"
59 1.50 christos #include "builtins.h"
60 1.1 cgd #include "shell.h"
61 1.1 cgd #include "nodes.h"
62 1.1 cgd #include "jobs.h"
63 1.23 christos #include "options.h"
64 1.1 cgd #include "expand.h"
65 1.1 cgd #include "redir.h"
66 1.1 cgd #include "output.h"
67 1.1 cgd #include "memalloc.h"
68 1.51 kre #include "mystring.h"
69 1.1 cgd #include "error.h"
70 1.59 kre #include "show.h"
71 1.1 cgd
72 1.1 cgd
73 1.1 cgd #define EMPTY -2 /* marks an unused slot in redirtab */
74 1.44 kre #define CLOSED -1 /* fd was not open before redir */
75 1.17 christos #ifndef PIPE_BUF
76 1.17 christos # define PIPESIZE 4096 /* amount of buffering in a pipe */
77 1.17 christos #else
78 1.17 christos # define PIPESIZE PIPE_BUF
79 1.17 christos #endif
80 1.1 cgd
81 1.1 cgd
82 1.1 cgd MKINIT
83 1.43 christos struct renamelist {
84 1.43 christos struct renamelist *next;
85 1.43 christos int orig;
86 1.43 christos int into;
87 1.43 christos };
88 1.43 christos
89 1.43 christos MKINIT
90 1.1 cgd struct redirtab {
91 1.1 cgd struct redirtab *next;
92 1.43 christos struct renamelist *renamed;
93 1.1 cgd };
94 1.1 cgd
95 1.1 cgd
96 1.1 cgd MKINIT struct redirtab *redirlist;
97 1.1 cgd
98 1.13 christos /*
99 1.7 jtc * We keep track of whether or not fd0 has been redirected. This is for
100 1.7 jtc * background commands, where we want to redirect fd0 to /dev/null only
101 1.13 christos * if it hasn't already been redirected.
102 1.43 christos */
103 1.43 christos STATIC int fd0_redirected = 0;
104 1.1 cgd
105 1.43 christos /*
106 1.43 christos * And also where to put internal use fds that should be out of the
107 1.43 christos * way of user defined fds (normally)
108 1.43 christos */
109 1.43 christos STATIC int big_sh_fd = 0;
110 1.43 christos
111 1.43 christos STATIC const struct renamelist *is_renamed(const struct renamelist *, int);
112 1.43 christos STATIC void fd_rename(struct redirtab *, int, int);
113 1.43 christos STATIC void free_rl(struct redirtab *, int);
114 1.29 christos STATIC void openredirect(union node *, char[10], int);
115 1.34 yamt STATIC int openhere(const union node *);
116 1.47 kre STATIC int copyfd(int, int, int);
117 1.43 christos STATIC void find_big_fd(void);
118 1.43 christos
119 1.54 kre
120 1.54 kre struct shell_fds { /* keep track of internal shell fds */
121 1.54 kre struct shell_fds *nxt;
122 1.54 kre void (*cb)(int, int);
123 1.54 kre int fd;
124 1.54 kre };
125 1.54 kre
126 1.54 kre STATIC struct shell_fds *sh_fd_list;
127 1.54 kre
128 1.54 kre STATIC void renumber_sh_fd(struct shell_fds *);
129 1.54 kre STATIC struct shell_fds *sh_fd(int);
130 1.54 kre
131 1.43 christos STATIC const struct renamelist *
132 1.43 christos is_renamed(const struct renamelist *rl, int fd)
133 1.43 christos {
134 1.43 christos while (rl != NULL) {
135 1.43 christos if (rl->orig == fd)
136 1.43 christos return rl;
137 1.43 christos rl = rl->next;
138 1.43 christos }
139 1.43 christos return NULL;
140 1.43 christos }
141 1.43 christos
142 1.43 christos STATIC void
143 1.43 christos free_rl(struct redirtab *rt, int reset)
144 1.43 christos {
145 1.43 christos struct renamelist *rl, *rn = rt->renamed;
146 1.43 christos
147 1.43 christos while ((rl = rn) != NULL) {
148 1.43 christos rn = rl->next;
149 1.43 christos if (rl->orig == 0)
150 1.43 christos fd0_redirected--;
151 1.60 kre VTRACE(DBG_REDIR, ("popredir %d%s: %s",
152 1.60 kre rl->orig, rl->orig==0 ? " (STDIN)" : "",
153 1.60 kre reset ? "" : "no reset\n"));
154 1.43 christos if (reset) {
155 1.60 kre if (rl->into < 0) {
156 1.60 kre VTRACE(DBG_REDIR, ("closed\n"));
157 1.43 christos close(rl->orig);
158 1.60 kre } else {
159 1.60 kre VTRACE(DBG_REDIR, ("from %d\n", rl->into));
160 1.43 christos movefd(rl->into, rl->orig);
161 1.60 kre }
162 1.43 christos }
163 1.43 christos ckfree(rl);
164 1.43 christos }
165 1.43 christos rt->renamed = NULL;
166 1.43 christos }
167 1.1 cgd
168 1.43 christos STATIC void
169 1.43 christos fd_rename(struct redirtab *rt, int from, int to)
170 1.43 christos {
171 1.60 kre /* XXX someday keep a short list (8..10) of freed renamelists XXX */
172 1.43 christos struct renamelist *rl = ckmalloc(sizeof(struct renamelist));
173 1.43 christos
174 1.43 christos rl->next = rt->renamed;
175 1.43 christos rt->renamed = rl;
176 1.43 christos
177 1.43 christos rl->orig = from;
178 1.43 christos rl->into = to;
179 1.43 christos }
180 1.1 cgd
181 1.1 cgd /*
182 1.1 cgd * Process a list of redirection commands. If the REDIR_PUSH flag is set,
183 1.1 cgd * old file descriptors are stashed away so that the redirection can be
184 1.1 cgd * undone by calling popredir. If the REDIR_BACKQ flag is set, then the
185 1.1 cgd * standard output, and the standard error if it becomes a duplicate of
186 1.1 cgd * stdout, is saved in memory.
187 1.1 cgd */
188 1.1 cgd
189 1.1 cgd void
190 1.27 christos redirect(union node *redir, int flags)
191 1.27 christos {
192 1.1 cgd union node *n;
193 1.16 christos struct redirtab *sv = NULL;
194 1.1 cgd int i;
195 1.1 cgd int fd;
196 1.13 christos char memory[10]; /* file descriptors to write to memory */
197 1.1 cgd
198 1.60 kre CTRACE(DBG_REDIR, ("redirect(F=0x%x):%s\n", flags, redir?"":" NONE"));
199 1.1 cgd for (i = 10 ; --i >= 0 ; )
200 1.1 cgd memory[i] = 0;
201 1.1 cgd memory[1] = flags & REDIR_BACKQ;
202 1.1 cgd if (flags & REDIR_PUSH) {
203 1.60 kre /*
204 1.60 kre * We don't have to worry about REDIR_VFORK here, as
205 1.24 christos * flags & REDIR_PUSH is never true if REDIR_VFORK is set.
206 1.24 christos */
207 1.1 cgd sv = ckmalloc(sizeof (struct redirtab));
208 1.43 christos sv->renamed = NULL;
209 1.1 cgd sv->next = redirlist;
210 1.1 cgd redirlist = sv;
211 1.1 cgd }
212 1.1 cgd for (n = redir ; n ; n = n->nfile.next) {
213 1.1 cgd fd = n->nfile.fd;
214 1.60 kre VTRACE(DBG_REDIR, ("redir %d (max=%d) ", fd, max_user_fd));
215 1.53 kre if (fd > max_user_fd)
216 1.53 kre max_user_fd = fd;
217 1.54 kre renumber_sh_fd(sh_fd(fd));
218 1.13 christos if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
219 1.43 christos n->ndup.dupfd == fd) {
220 1.43 christos /* redirect from/to same file descriptor */
221 1.47 kre /* make sure it stays open */
222 1.47 kre if (fcntl(fd, F_SETFD, 0) < 0)
223 1.47 kre error("fd %d: %s", fd, strerror(errno));
224 1.60 kre VTRACE(DBG_REDIR, ("!cloexec\n"));
225 1.43 christos continue;
226 1.43 christos }
227 1.15 christos
228 1.43 christos if ((flags & REDIR_PUSH) && !is_renamed(sv->renamed, fd)) {
229 1.1 cgd INTOFF;
230 1.43 christos if (big_sh_fd < 10)
231 1.43 christos find_big_fd();
232 1.43 christos if ((i = fcntl(fd, F_DUPFD, big_sh_fd)) == -1) {
233 1.15 christos switch (errno) {
234 1.15 christos case EBADF:
235 1.35 yamt i = CLOSED;
236 1.35 yamt break;
237 1.43 christos case EMFILE:
238 1.43 christos case EINVAL:
239 1.43 christos find_big_fd();
240 1.43 christos i = fcntl(fd, F_DUPFD, big_sh_fd);
241 1.43 christos if (i >= 0)
242 1.43 christos break;
243 1.43 christos /* FALLTHRU */
244 1.15 christos default:
245 1.43 christos i = errno;
246 1.43 christos INTON; /* XXX not needed here ? */
247 1.43 christos error("%d: %s", fd, strerror(i));
248 1.19 mycroft /* NOTREACHED */
249 1.15 christos }
250 1.43 christos }
251 1.43 christos if (i >= 0)
252 1.35 yamt (void)fcntl(i, F_SETFD, FD_CLOEXEC);
253 1.43 christos fd_rename(sv, fd, i);
254 1.60 kre VTRACE(DBG_REDIR, ("saved as %d ", i));
255 1.1 cgd INTON;
256 1.1 cgd }
257 1.60 kre VTRACE(DBG_REDIR, ("%s\n", fd == 0 ? "STDIN" : ""));
258 1.44 kre if (fd == 0)
259 1.44 kre fd0_redirected++;
260 1.35 yamt openredirect(n, memory, flags);
261 1.1 cgd }
262 1.1 cgd if (memory[1])
263 1.1 cgd out1 = &memout;
264 1.1 cgd if (memory[2])
265 1.1 cgd out2 = &memout;
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd
269 1.1 cgd STATIC void
270 1.29 christos openredirect(union node *redir, char memory[10], int flags)
271 1.27 christos {
272 1.36 christos struct stat sb;
273 1.1 cgd int fd = redir->nfile.fd;
274 1.1 cgd char *fname;
275 1.1 cgd int f;
276 1.42 christos int eflags, cloexec;
277 1.1 cgd
278 1.1 cgd /*
279 1.1 cgd * We suppress interrupts so that we won't leave open file
280 1.1 cgd * descriptors around. This may not be such a good idea because
281 1.1 cgd * an open of a device or a fifo can block indefinitely.
282 1.1 cgd */
283 1.1 cgd INTOFF;
284 1.43 christos if (fd < 10)
285 1.43 christos memory[fd] = 0;
286 1.1 cgd switch (redir->nfile.type) {
287 1.1 cgd case NFROM:
288 1.1 cgd fname = redir->nfile.expfname;
289 1.29 christos if (flags & REDIR_VFORK)
290 1.29 christos eflags = O_NONBLOCK;
291 1.29 christos else
292 1.29 christos eflags = 0;
293 1.29 christos if ((f = open(fname, O_RDONLY|eflags)) < 0)
294 1.20 christos goto eopen;
295 1.59 kre VTRACE(DBG_REDIR, ("openredirect(< '%s') -> %d [%#x]",
296 1.59 kre fname, f, eflags));
297 1.29 christos if (eflags)
298 1.29 christos (void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);
299 1.20 christos break;
300 1.20 christos case NFROMTO:
301 1.20 christos fname = redir->nfile.expfname;
302 1.61 kre if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
303 1.20 christos goto ecreate;
304 1.59 kre VTRACE(DBG_REDIR, ("openredirect(<> '%s') -> %d", fname, f));
305 1.1 cgd break;
306 1.1 cgd case NTO:
307 1.36 christos if (Cflag) {
308 1.36 christos fname = redir->nfile.expfname;
309 1.37 christos if ((f = open(fname, O_WRONLY)) == -1) {
310 1.36 christos if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL,
311 1.36 christos 0666)) < 0)
312 1.36 christos goto ecreate;
313 1.37 christos } else if (fstat(f, &sb) == -1) {
314 1.37 christos int serrno = errno;
315 1.37 christos close(f);
316 1.37 christos errno = serrno;
317 1.37 christos goto ecreate;
318 1.37 christos } else if (S_ISREG(sb.st_mode)) {
319 1.37 christos close(f);
320 1.36 christos errno = EEXIST;
321 1.36 christos goto ecreate;
322 1.36 christos }
323 1.60 kre VTRACE(DBG_REDIR, ("openredirect(>| '%s') -> %d",
324 1.60 kre fname, f));
325 1.36 christos break;
326 1.36 christos }
327 1.23 christos /* FALLTHROUGH */
328 1.23 christos case NCLOBBER:
329 1.1 cgd fname = redir->nfile.expfname;
330 1.36 christos if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
331 1.20 christos goto ecreate;
332 1.59 kre VTRACE(DBG_REDIR, ("openredirect(> '%s') -> %d", fname, f));
333 1.20 christos break;
334 1.1 cgd case NAPPEND:
335 1.1 cgd fname = redir->nfile.expfname;
336 1.1 cgd if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
337 1.20 christos goto ecreate;
338 1.59 kre VTRACE(DBG_REDIR, ("openredirect(>> '%s') -> %d", fname, f));
339 1.20 christos break;
340 1.1 cgd case NTOFD:
341 1.1 cgd case NFROMFD:
342 1.1 cgd if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
343 1.43 christos if (fd < 10 && redir->ndup.dupfd < 10 &&
344 1.43 christos memory[redir->ndup.dupfd])
345 1.1 cgd memory[fd] = 1;
346 1.47 kre else if (copyfd(redir->ndup.dupfd, fd,
347 1.49 christos (flags & REDIR_KEEP) == 0) < 0)
348 1.47 kre error("Redirect (from %d to %d) failed: %s",
349 1.47 kre redir->ndup.dupfd, fd, strerror(errno));
350 1.59 kre VTRACE(DBG_REDIR, ("openredirect: %d%c&%d\n", fd,
351 1.59 kre "<>"[redir->nfile.type==NTOFD], redir->ndup.dupfd));
352 1.59 kre } else {
353 1.47 kre (void) close(fd);
354 1.59 kre VTRACE(DBG_REDIR, ("openredirect: %d%c&-\n", fd,
355 1.59 kre "<>"[redir->nfile.type==NTOFD]));
356 1.59 kre }
357 1.20 christos INTON;
358 1.20 christos return;
359 1.1 cgd case NHERE:
360 1.1 cgd case NXHERE:
361 1.60 kre VTRACE(DBG_REDIR, ("openredirect: %d<<...", fd));
362 1.1 cgd f = openhere(redir);
363 1.20 christos break;
364 1.1 cgd default:
365 1.1 cgd abort();
366 1.1 cgd }
367 1.20 christos
368 1.55 kre cloexec = fd > 2 && (flags & REDIR_KEEP) == 0 && !posix;
369 1.20 christos if (f != fd) {
370 1.59 kre VTRACE(DBG_REDIR, (" -> %d", fd));
371 1.47 kre if (copyfd(f, fd, cloexec) < 0) {
372 1.47 kre int e = errno;
373 1.47 kre
374 1.47 kre close(f);
375 1.47 kre error("redirect reassignment (fd %d) failed: %s", fd,
376 1.47 kre strerror(e));
377 1.47 kre }
378 1.20 christos close(f);
379 1.42 christos } else if (cloexec)
380 1.38 christos (void)fcntl(f, F_SETFD, FD_CLOEXEC);
381 1.59 kre VTRACE(DBG_REDIR, ("%s\n", cloexec ? " cloexec" : ""));
382 1.38 christos
383 1.1 cgd INTON;
384 1.20 christos return;
385 1.60 kre ecreate:
386 1.30 msaitoh exerrno = 1;
387 1.20 christos error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
388 1.60 kre eopen:
389 1.30 msaitoh exerrno = 1;
390 1.20 christos error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
391 1.1 cgd }
392 1.1 cgd
393 1.1 cgd
394 1.1 cgd /*
395 1.1 cgd * Handle here documents. Normally we fork off a process to write the
396 1.1 cgd * data to a pipe. If the document is short, we can stuff the data in
397 1.1 cgd * the pipe without forking.
398 1.1 cgd */
399 1.1 cgd
400 1.1 cgd STATIC int
401 1.34 yamt openhere(const union node *redir)
402 1.27 christos {
403 1.1 cgd int pip[2];
404 1.12 christos int len = 0;
405 1.1 cgd
406 1.1 cgd if (pipe(pip) < 0)
407 1.1 cgd error("Pipe call failed");
408 1.1 cgd if (redir->type == NHERE) {
409 1.1 cgd len = strlen(redir->nhere.doc->narg.text);
410 1.1 cgd if (len <= PIPESIZE) {
411 1.1 cgd xwrite(pip[1], redir->nhere.doc->narg.text, len);
412 1.1 cgd goto out;
413 1.1 cgd }
414 1.1 cgd }
415 1.60 kre VTRACE(DBG_REDIR, (" forking [%d,%d]\n", pip[0], pip[1]));
416 1.32 plunky if (forkshell(NULL, NULL, FORK_NOJOB) == 0) {
417 1.1 cgd close(pip[0]);
418 1.1 cgd signal(SIGINT, SIG_IGN);
419 1.1 cgd signal(SIGQUIT, SIG_IGN);
420 1.1 cgd signal(SIGHUP, SIG_IGN);
421 1.1 cgd #ifdef SIGTSTP
422 1.1 cgd signal(SIGTSTP, SIG_IGN);
423 1.1 cgd #endif
424 1.1 cgd signal(SIGPIPE, SIG_DFL);
425 1.1 cgd if (redir->type == NHERE)
426 1.1 cgd xwrite(pip[1], redir->nhere.doc->narg.text, len);
427 1.1 cgd else
428 1.1 cgd expandhere(redir->nhere.doc, pip[1]);
429 1.1 cgd _exit(0);
430 1.1 cgd }
431 1.60 kre VTRACE(DBG_REDIR, ("openhere (closing %d)", pip[1]));
432 1.60 kre out:
433 1.1 cgd close(pip[1]);
434 1.60 kre VTRACE(DBG_REDIR, (" (pipe fd=%d)", pip[0]));
435 1.1 cgd return pip[0];
436 1.1 cgd }
437 1.1 cgd
438 1.1 cgd
439 1.1 cgd
440 1.1 cgd /*
441 1.1 cgd * Undo the effects of the last redirection.
442 1.1 cgd */
443 1.1 cgd
444 1.1 cgd void
445 1.27 christos popredir(void)
446 1.27 christos {
447 1.14 tls struct redirtab *rp = redirlist;
448 1.1 cgd
449 1.1 cgd INTOFF;
450 1.43 christos free_rl(rp, 1);
451 1.1 cgd redirlist = rp->next;
452 1.1 cgd ckfree(rp);
453 1.1 cgd INTON;
454 1.1 cgd }
455 1.1 cgd
456 1.1 cgd /*
457 1.1 cgd * Undo all redirections. Called on error or interrupt.
458 1.1 cgd */
459 1.1 cgd
460 1.1 cgd #ifdef mkinit
461 1.1 cgd
462 1.1 cgd INCLUDE "redir.h"
463 1.1 cgd
464 1.1 cgd RESET {
465 1.1 cgd while (redirlist)
466 1.1 cgd popredir();
467 1.1 cgd }
468 1.1 cgd
469 1.1 cgd SHELLPROC {
470 1.24 christos clearredir(0);
471 1.1 cgd }
472 1.1 cgd
473 1.1 cgd #endif
474 1.1 cgd
475 1.7 jtc /* Return true if fd 0 has already been redirected at least once. */
476 1.7 jtc int
477 1.43 christos fd0_redirected_p(void)
478 1.43 christos {
479 1.44 kre return fd0_redirected != 0;
480 1.7 jtc }
481 1.1 cgd
482 1.1 cgd /*
483 1.1 cgd * Discard all saved file descriptors.
484 1.1 cgd */
485 1.1 cgd
486 1.1 cgd void
487 1.33 matt clearredir(int vforked)
488 1.24 christos {
489 1.14 tls struct redirtab *rp;
490 1.43 christos struct renamelist *rl;
491 1.1 cgd
492 1.1 cgd for (rp = redirlist ; rp ; rp = rp->next) {
493 1.43 christos if (!vforked)
494 1.43 christos free_rl(rp, 0);
495 1.43 christos else for (rl = rp->renamed; rl; rl = rl->next)
496 1.43 christos if (rl->into >= 0)
497 1.43 christos close(rl->into);
498 1.1 cgd }
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd
502 1.1 cgd
503 1.1 cgd /*
504 1.47 kre * Copy a file descriptor to be == to.
505 1.47 kre * cloexec indicates if we want close-on-exec or not.
506 1.47 kre * Returns -1 if any error occurs.
507 1.1 cgd */
508 1.1 cgd
509 1.47 kre STATIC int
510 1.47 kre copyfd(int from, int to, int cloexec)
511 1.9 cgd {
512 1.1 cgd int newfd;
513 1.1 cgd
514 1.47 kre if (cloexec && to > 2)
515 1.47 kre newfd = dup3(from, to, O_CLOEXEC);
516 1.47 kre else
517 1.47 kre newfd = dup2(from, to);
518 1.47 kre
519 1.1 cgd return newfd;
520 1.1 cgd }
521 1.43 christos
522 1.47 kre /*
523 1.47 kre * rename fd from to be fd to (closing from).
524 1.47 kre * close-on-exec is never set on 'to' (unless
525 1.47 kre * from==to and it was set on from) - ie: a no-op
526 1.60 kre * returns to (or errors() if an error occurs).
527 1.47 kre *
528 1.47 kre * This is mostly used for rearranging the
529 1.47 kre * results from pipe().
530 1.47 kre */
531 1.43 christos int
532 1.43 christos movefd(int from, int to)
533 1.43 christos {
534 1.43 christos if (from == to)
535 1.43 christos return to;
536 1.43 christos
537 1.43 christos (void) close(to);
538 1.47 kre if (copyfd(from, to, 0) != to) {
539 1.47 kre int e = errno;
540 1.47 kre
541 1.47 kre (void) close(from);
542 1.47 kre error("Unable to make fd %d: %s", to, strerror(e));
543 1.47 kre }
544 1.43 christos (void) close(from);
545 1.43 christos
546 1.43 christos return to;
547 1.43 christos }
548 1.43 christos
549 1.43 christos STATIC void
550 1.43 christos find_big_fd(void)
551 1.43 christos {
552 1.43 christos int i, fd;
553 1.57 kre static int last_start = 3; /* aim to keep sh fd's under 20 */
554 1.43 christos
555 1.54 kre if (last_start < 10)
556 1.54 kre last_start++;
557 1.54 kre
558 1.54 kre for (i = (1 << last_start); i >= 10; i >>= 1) {
559 1.43 christos if ((fd = fcntl(0, F_DUPFD, i - 1)) >= 0) {
560 1.43 christos close(fd);
561 1.43 christos break;
562 1.43 christos }
563 1.43 christos }
564 1.43 christos
565 1.43 christos fd = (i / 5) * 4;
566 1.54 kre if (fd < 10)
567 1.43 christos fd = 10;
568 1.43 christos
569 1.43 christos big_sh_fd = fd;
570 1.43 christos }
571 1.43 christos
572 1.47 kre /*
573 1.47 kre * If possible, move file descriptor fd out of the way
574 1.47 kre * of expected user fd values. Returns the new fd
575 1.47 kre * (which may be the input fd if things do not go well.)
576 1.47 kre * Always set close-on-exec on the result, and close
577 1.47 kre * the input fd unless it is to be our result.
578 1.47 kre */
579 1.43 christos int
580 1.43 christos to_upper_fd(int fd)
581 1.43 christos {
582 1.43 christos int i;
583 1.43 christos
584 1.59 kre VTRACE(DBG_REDIR|DBG_OUTPUT, ("to_upper_fd(%d)", fd));
585 1.43 christos if (big_sh_fd < 10)
586 1.43 christos find_big_fd();
587 1.43 christos do {
588 1.46 kre i = fcntl(fd, F_DUPFD_CLOEXEC, big_sh_fd);
589 1.43 christos if (i >= 0) {
590 1.43 christos if (fd != i)
591 1.43 christos close(fd);
592 1.59 kre VTRACE(DBG_REDIR|DBG_OUTPUT, ("-> %d\n", i));
593 1.43 christos return i;
594 1.43 christos }
595 1.54 kre if (errno != EMFILE && errno != EINVAL)
596 1.43 christos break;
597 1.43 christos find_big_fd();
598 1.43 christos } while (big_sh_fd > 10);
599 1.43 christos
600 1.46 kre /*
601 1.47 kre * If we wanted to move this fd to some random high number
602 1.46 kre * we certainly do not intend to pass it through exec, even
603 1.46 kre * if the reassignment failed.
604 1.46 kre */
605 1.46 kre (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
606 1.59 kre VTRACE(DBG_REDIR|DBG_OUTPUT, (" fails ->%d\n", fd));
607 1.43 christos return fd;
608 1.43 christos }
609 1.50 christos
610 1.54 kre void
611 1.54 kre register_sh_fd(int fd, void (*cb)(int, int))
612 1.54 kre {
613 1.54 kre struct shell_fds *fp;
614 1.54 kre
615 1.54 kre fp = ckmalloc(sizeof (struct shell_fds));
616 1.54 kre if (fp != NULL) {
617 1.54 kre fp->nxt = sh_fd_list;
618 1.54 kre sh_fd_list = fp;
619 1.54 kre
620 1.54 kre fp->fd = fd;
621 1.54 kre fp->cb = cb;
622 1.54 kre }
623 1.54 kre }
624 1.54 kre
625 1.54 kre void
626 1.54 kre sh_close(int fd)
627 1.54 kre {
628 1.54 kre struct shell_fds **fpp, *fp;
629 1.54 kre
630 1.54 kre fpp = &sh_fd_list;
631 1.54 kre while ((fp = *fpp) != NULL) {
632 1.54 kre if (fp->fd == fd) {
633 1.54 kre *fpp = fp->nxt;
634 1.54 kre ckfree(fp);
635 1.54 kre break;
636 1.54 kre }
637 1.54 kre fpp = &fp->nxt;
638 1.54 kre }
639 1.54 kre (void)close(fd);
640 1.54 kre }
641 1.54 kre
642 1.54 kre STATIC struct shell_fds *
643 1.54 kre sh_fd(int fd)
644 1.54 kre {
645 1.54 kre struct shell_fds *fp;
646 1.54 kre
647 1.54 kre for (fp = sh_fd_list; fp != NULL; fp = fp->nxt)
648 1.54 kre if (fp->fd == fd)
649 1.54 kre return fp;
650 1.54 kre return NULL;
651 1.54 kre }
652 1.54 kre
653 1.54 kre STATIC void
654 1.54 kre renumber_sh_fd(struct shell_fds *fp)
655 1.54 kre {
656 1.54 kre int to;
657 1.54 kre
658 1.54 kre if (fp == NULL)
659 1.54 kre return;
660 1.54 kre
661 1.54 kre #ifndef F_DUPFD_CLOEXEC
662 1.54 kre #define F_DUPFD_CLOEXEC F_DUPFD
663 1.54 kre #define CLOEXEC(fd) (fcntl((fd), F_SETFD, fcntl((fd),F_GETFD) | FD_CLOEXEC))
664 1.54 kre #else
665 1.54 kre #define CLOEXEC(fd)
666 1.54 kre #endif
667 1.54 kre
668 1.57 kre /*
669 1.57 kre * if we have had a collision, and the sh fd was a "big" one
670 1.57 kre * try moving the sh fd base to a higher number (if possible)
671 1.57 kre * so future sh fds are less likely to be in the user's sights
672 1.57 kre * (incl this one when moved)
673 1.57 kre */
674 1.57 kre if (fp->fd >= big_sh_fd)
675 1.57 kre find_big_fd();
676 1.57 kre
677 1.54 kre to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd);
678 1.54 kre if (to == -1)
679 1.54 kre to = fcntl(fp->fd, F_DUPFD_CLOEXEC, big_sh_fd/2);
680 1.54 kre if (to == -1)
681 1.54 kre to = fcntl(fp->fd, F_DUPFD_CLOEXEC, fp->fd + 1);
682 1.54 kre if (to == -1)
683 1.54 kre to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 10);
684 1.54 kre if (to == -1)
685 1.54 kre to = fcntl(fp->fd, F_DUPFD_CLOEXEC, 3);
686 1.54 kre if (to == -1)
687 1.54 kre error("insufficient file descriptors available");
688 1.54 kre CLOEXEC(to);
689 1.54 kre
690 1.54 kre if (fp->fd == to) /* impossible? */
691 1.54 kre return;
692 1.54 kre
693 1.54 kre (*fp->cb)(fp->fd, to);
694 1.54 kre (void)close(fp->fd);
695 1.54 kre fp->fd = to;
696 1.54 kre }
697 1.54 kre
698 1.51 kre static const struct flgnames {
699 1.50 christos const char *name;
700 1.56 kre uint16_t minch;
701 1.50 christos uint32_t value;
702 1.50 christos } nv[] = {
703 1.50 christos #ifdef O_APPEND
704 1.56 kre { "append", 2, O_APPEND },
705 1.50 christos #endif
706 1.50 christos #ifdef O_ASYNC
707 1.56 kre { "async", 2, O_ASYNC },
708 1.50 christos #endif
709 1.50 christos #ifdef O_SYNC
710 1.56 kre { "sync", 2, O_SYNC },
711 1.50 christos #endif
712 1.50 christos #ifdef O_NONBLOCK
713 1.56 kre { "nonblock", 3, O_NONBLOCK },
714 1.50 christos #endif
715 1.50 christos #ifdef O_FSYNC
716 1.56 kre { "fsync", 2, O_FSYNC },
717 1.50 christos #endif
718 1.50 christos #ifdef O_DSYNC
719 1.56 kre { "dsync", 2, O_DSYNC },
720 1.50 christos #endif
721 1.50 christos #ifdef O_RSYNC
722 1.56 kre { "rsync", 2, O_RSYNC },
723 1.50 christos #endif
724 1.62 kamil #ifdef O_ALT_IO
725 1.56 kre { "altio", 2, O_ALT_IO },
726 1.50 christos #endif
727 1.50 christos #ifdef O_DIRECT
728 1.56 kre { "direct", 2, O_DIRECT },
729 1.50 christos #endif
730 1.50 christos #ifdef O_NOSIGPIPE
731 1.56 kre { "nosigpipe", 3, O_NOSIGPIPE },
732 1.50 christos #endif
733 1.50 christos #ifdef O_CLOEXEC
734 1.56 kre { "cloexec", 2, O_CLOEXEC },
735 1.50 christos #endif
736 1.56 kre { 0, 0, 0 }
737 1.50 christos };
738 1.50 christos #define ALLFLAGS (O_APPEND|O_ASYNC|O_SYNC|O_NONBLOCK|O_DSYNC|O_RSYNC|\
739 1.50 christos O_ALT_IO|O_DIRECT|O_NOSIGPIPE|O_CLOEXEC)
740 1.50 christos
741 1.50 christos static int
742 1.50 christos getflags(int fd, int p)
743 1.50 christos {
744 1.50 christos int c, f;
745 1.50 christos
746 1.54 kre if (sh_fd(fd) != NULL) {
747 1.54 kre if (!p)
748 1.54 kre return -1;
749 1.54 kre error("Can't get status for fd=%d (%s)", fd,
750 1.54 kre "Bad file descriptor"); /*XXX*/
751 1.54 kre }
752 1.54 kre
753 1.50 christos if ((c = fcntl(fd, F_GETFD)) == -1) {
754 1.50 christos if (!p)
755 1.50 christos return -1;
756 1.50 christos error("Can't get status for fd=%d (%s)", fd, strerror(errno));
757 1.50 christos }
758 1.50 christos if ((f = fcntl(fd, F_GETFL)) == -1) {
759 1.50 christos if (!p)
760 1.50 christos return -1;
761 1.50 christos error("Can't get flags for fd=%d (%s)", fd, strerror(errno));
762 1.50 christos }
763 1.51 kre if (c & FD_CLOEXEC)
764 1.50 christos f |= O_CLOEXEC;
765 1.50 christos return f & ALLFLAGS;
766 1.50 christos }
767 1.50 christos
768 1.50 christos static void
769 1.51 kre printone(int fd, int p, int verbose, int pfd)
770 1.50 christos {
771 1.50 christos int f = getflags(fd, p);
772 1.51 kre const struct flgnames *fn;
773 1.50 christos
774 1.50 christos if (f == -1)
775 1.50 christos return;
776 1.50 christos
777 1.51 kre if (pfd)
778 1.51 kre outfmt(out1, "%d: ", fd);
779 1.51 kre for (fn = nv; fn->name; fn++) {
780 1.51 kre if (f & fn->value) {
781 1.51 kre outfmt(out1, "%s%s", verbose ? "+" : "", fn->name);
782 1.51 kre f &= ~fn->value;
783 1.50 christos } else if (verbose)
784 1.51 kre outfmt(out1, "-%s", fn->name);
785 1.50 christos else
786 1.50 christos continue;
787 1.51 kre if (f || (verbose && fn[1].name))
788 1.50 christos outfmt(out1, ",");
789 1.50 christos }
790 1.51 kre if (verbose && f) /* f should be normally be 0 */
791 1.51 kre outfmt(out1, " +%#x", f);
792 1.50 christos outfmt(out1, "\n");
793 1.50 christos }
794 1.50 christos
795 1.51 kre static void
796 1.50 christos parseflags(char *s, int *p, int *n)
797 1.50 christos {
798 1.51 kre int *v, *w;
799 1.51 kre const struct flgnames *fn;
800 1.56 kre size_t len;
801 1.50 christos
802 1.50 christos *p = 0;
803 1.50 christos *n = 0;
804 1.50 christos for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
805 1.51 kre switch (*s++) {
806 1.50 christos case '+':
807 1.50 christos v = p;
808 1.51 kre w = n;
809 1.50 christos break;
810 1.50 christos case '-':
811 1.50 christos v = n;
812 1.51 kre w = p;
813 1.50 christos break;
814 1.50 christos default:
815 1.51 kre error("Missing +/- indicator before flag %s", s-1);
816 1.50 christos }
817 1.60 kre
818 1.56 kre len = strlen(s);
819 1.51 kre for (fn = nv; fn->name; fn++)
820 1.56 kre if (len >= fn->minch && strncmp(s,fn->name,len) == 0) {
821 1.51 kre *v |= fn->value;
822 1.51 kre *w &=~ fn->value;
823 1.50 christos break;
824 1.50 christos }
825 1.51 kre if (fn->name == 0)
826 1.50 christos error("Bad flag `%s'", s);
827 1.50 christos }
828 1.50 christos }
829 1.50 christos
830 1.50 christos static void
831 1.50 christos setone(int fd, int pos, int neg, int verbose)
832 1.50 christos {
833 1.50 christos int f = getflags(fd, 1);
834 1.51 kre int n, cloexec;
835 1.51 kre
836 1.50 christos if (f == -1)
837 1.50 christos return;
838 1.50 christos
839 1.51 kre cloexec = -1;
840 1.50 christos if ((pos & O_CLOEXEC) && !(f & O_CLOEXEC))
841 1.50 christos cloexec = FD_CLOEXEC;
842 1.50 christos if ((neg & O_CLOEXEC) && (f & O_CLOEXEC))
843 1.50 christos cloexec = 0;
844 1.50 christos
845 1.50 christos if (cloexec != -1 && fcntl(fd, F_SETFD, cloexec) == -1)
846 1.50 christos error("Can't set status for fd=%d (%s)", fd, strerror(errno));
847 1.50 christos
848 1.50 christos pos &= ~O_CLOEXEC;
849 1.50 christos neg &= ~O_CLOEXEC;
850 1.50 christos f &= ~O_CLOEXEC;
851 1.51 kre n = f;
852 1.50 christos n |= pos;
853 1.50 christos n &= ~neg;
854 1.50 christos if (n != f && fcntl(fd, F_SETFL, n) == -1)
855 1.50 christos error("Can't set flags for fd=%d (%s)", fd, strerror(errno));
856 1.50 christos if (verbose)
857 1.51 kre printone(fd, 1, verbose, 1);
858 1.50 christos }
859 1.50 christos
860 1.50 christos int
861 1.50 christos fdflagscmd(int argc, char *argv[])
862 1.50 christos {
863 1.50 christos char *num;
864 1.50 christos int verbose = 0, ch, pos = 0, neg = 0;
865 1.50 christos char *setflags = NULL;
866 1.50 christos
867 1.50 christos optreset = 1; optind = 1; /* initialize getopt */
868 1.50 christos while ((ch = getopt(argc, argv, ":vs:")) != -1)
869 1.50 christos switch ((char)ch) {
870 1.50 christos case 'v':
871 1.50 christos verbose = 1;
872 1.50 christos break;
873 1.50 christos case 's':
874 1.51 kre if (setflags)
875 1.51 kre goto msg;
876 1.50 christos setflags = optarg;
877 1.50 christos break;
878 1.50 christos case '?':
879 1.50 christos default:
880 1.50 christos msg:
881 1.51 kre error("Usage: fdflags [-v] [-s <flags> fd] [fd...]");
882 1.50 christos /* NOTREACHED */
883 1.50 christos }
884 1.50 christos
885 1.50 christos argc -= optind, argv += optind;
886 1.50 christos
887 1.51 kre if (setflags)
888 1.51 kre parseflags(setflags, &pos, &neg);
889 1.50 christos
890 1.50 christos if (argc == 0) {
891 1.53 kre int i;
892 1.51 kre
893 1.50 christos if (setflags)
894 1.50 christos goto msg;
895 1.51 kre
896 1.53 kre for (i = 0; i <= max_user_fd; i++)
897 1.51 kre printone(i, 0, verbose, 1);
898 1.50 christos return 0;
899 1.50 christos }
900 1.50 christos
901 1.50 christos while ((num = *argv++) != NULL) {
902 1.51 kre int fd = number(num);
903 1.51 kre
904 1.52 kre while (num[0] == '0' && num[1] != '\0') /* skip 0's */
905 1.52 kre num++;
906 1.51 kre if (strlen(num) > 5)
907 1.51 kre error("%s too big to be a file descriptor", num);
908 1.51 kre
909 1.50 christos if (setflags)
910 1.50 christos setone(fd, pos, neg, verbose);
911 1.50 christos else
912 1.51 kre printone(fd, 1, verbose, argc > 1);
913 1.50 christos }
914 1.50 christos return 0;
915 1.50 christos }
916 1.58 kre
917 1.58 kre #undef MAX /* in case we inherited them from somewhere */
918 1.58 kre #undef MIN
919 1.58 kre
920 1.58 kre #define MIN(a,b) (/*CONSTCOND*/((a)<=(b)) ? (a) : (b))
921 1.58 kre #define MAX(a,b) (/*CONSTCOND*/((a)>=(b)) ? (a) : (b))
922 1.58 kre
923 1.58 kre /* now make the compiler work for us... */
924 1.58 kre #define MIN_REDIR MIN(MIN(MIN(MIN(NTO,NFROM), MIN(NTOFD,NFROMFD)), \
925 1.58 kre MIN(MIN(NCLOBBER,NAPPEND), MIN(NHERE,NXHERE))), NFROMTO)
926 1.58 kre #define MAX_REDIR MAX(MAX(MAX(MAX(NTO,NFROM), MAX(NTOFD,NFROMFD)), \
927 1.58 kre MAX(MAX(NCLOBBER,NAPPEND), MAX(NHERE,NXHERE))), NFROMTO)
928 1.58 kre
929 1.58 kre static const char *redir_sym[MAX_REDIR - MIN_REDIR + 1] = {
930 1.58 kre [NTO - MIN_REDIR]= ">",
931 1.58 kre [NFROM - MIN_REDIR]= "<",
932 1.58 kre [NTOFD - MIN_REDIR]= ">&",
933 1.58 kre [NFROMFD - MIN_REDIR]= "<&",
934 1.58 kre [NCLOBBER - MIN_REDIR]= ">|",
935 1.58 kre [NAPPEND - MIN_REDIR]= ">>",
936 1.58 kre [NHERE - MIN_REDIR]= "<<",
937 1.58 kre [NXHERE - MIN_REDIR]= "<<",
938 1.58 kre [NFROMTO - MIN_REDIR]= "<>",
939 1.58 kre };
940 1.58 kre
941 1.58 kre int
942 1.58 kre outredir(struct output *out, union node *n, int sep)
943 1.58 kre {
944 1.58 kre if (n == NULL)
945 1.58 kre return 0;
946 1.58 kre if (n->type < MIN_REDIR || n->type > MAX_REDIR ||
947 1.58 kre redir_sym[n->type - MIN_REDIR] == NULL)
948 1.58 kre return 0;
949 1.58 kre
950 1.58 kre if (sep)
951 1.58 kre outc(sep, out);
952 1.58 kre
953 1.58 kre /*
954 1.58 kre * ugly, but all redir node types have "fd" in same slot...
955 1.58 kre * (and code other places assumes it as well)
956 1.58 kre */
957 1.58 kre if ((redir_sym[n->type - MIN_REDIR][0] == '<' && n->nfile.fd != 0) ||
958 1.58 kre (redir_sym[n->type - MIN_REDIR][0] == '>' && n->nfile.fd != 1))
959 1.58 kre outfmt(out, "%d", n->nfile.fd);
960 1.58 kre
961 1.58 kre outstr(redir_sym[n->type - MIN_REDIR], out);
962 1.58 kre
963 1.58 kre switch (n->type) {
964 1.58 kre case NHERE:
965 1.58 kre outstr("'...'", out);
966 1.58 kre break;
967 1.58 kre case NXHERE:
968 1.58 kre outstr("...", out);
969 1.58 kre break;
970 1.58 kre case NTOFD:
971 1.58 kre case NFROMFD:
972 1.58 kre if (n->ndup.dupfd < 0)
973 1.58 kre outc('-', out);
974 1.58 kre else
975 1.58 kre outfmt(out, "%d", n->ndup.dupfd);
976 1.58 kre break;
977 1.58 kre default:
978 1.58 kre outstr(n->nfile.expfname, out);
979 1.58 kre break;
980 1.58 kre }
981 1.58 kre return 1;
982 1.58 kre }
983