2004-07-08 07:57:33 +04:00
|
|
|
/* $NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-05 01:01:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
1995-05-12 01:28:33 +04:00
|
|
|
static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
|
1995-03-21 12:01:59 +03:00
|
|
|
#else
|
2004-07-08 07:57:33 +04:00
|
|
|
__RCSID("$NetBSD: redir.c,v 1.29 2004/07/08 03:57:33 christos Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <sys/types.h>
|
1998-07-27 21:12:45 +04:00
|
|
|
#include <sys/param.h> /* PIPE_BUF */
|
1995-05-12 01:28:33 +04:00
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Code for dealing with input/output redirection.
|
|
|
|
*/
|
|
|
|
|
2002-09-28 02:56:24 +04:00
|
|
|
#include "main.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "shell.h"
|
|
|
|
#include "nodes.h"
|
|
|
|
#include "jobs.h"
|
2002-05-15 20:33:35 +04:00
|
|
|
#include "options.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "expand.h"
|
|
|
|
#include "redir.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define EMPTY -2 /* marks an unused slot in redirtab */
|
1998-07-27 21:12:45 +04:00
|
|
|
#ifndef PIPE_BUF
|
|
|
|
# define PIPESIZE 4096 /* amount of buffering in a pipe */
|
|
|
|
#else
|
|
|
|
# define PIPESIZE PIPE_BUF
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
MKINIT
|
|
|
|
struct redirtab {
|
|
|
|
struct redirtab *next;
|
|
|
|
short renamed[10];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
MKINIT struct redirtab *redirlist;
|
|
|
|
|
1996-10-16 19:16:04 +04:00
|
|
|
/*
|
1994-05-11 21:09:42 +04:00
|
|
|
* We keep track of whether or not fd0 has been redirected. This is for
|
|
|
|
* background commands, where we want to redirect fd0 to /dev/null only
|
1996-10-16 19:16:04 +04:00
|
|
|
* if it hasn't already been redirected.
|
1994-05-11 21:09:42 +04:00
|
|
|
*/
|
1993-05-02 05:28:40 +04:00
|
|
|
int fd0_redirected = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-07-08 07:57:33 +04:00
|
|
|
STATIC void openredirect(union node *, char[10], int);
|
2002-11-25 01:35:38 +03:00
|
|
|
STATIC int openhere(union node *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a list of redirection commands. If the REDIR_PUSH flag is set,
|
|
|
|
* old file descriptors are stashed away so that the redirection can be
|
|
|
|
* undone by calling popredir. If the REDIR_BACKQ flag is set, then the
|
|
|
|
* standard output, and the standard error if it becomes a duplicate of
|
|
|
|
* stdout, is saved in memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
redirect(union node *redir, int flags)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
union node *n;
|
1997-07-05 01:01:48 +04:00
|
|
|
struct redirtab *sv = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
int i;
|
|
|
|
int fd;
|
1997-04-21 16:38:25 +04:00
|
|
|
int try;
|
1996-10-16 19:16:04 +04:00
|
|
|
char memory[10]; /* file descriptors to write to memory */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (i = 10 ; --i >= 0 ; )
|
|
|
|
memory[i] = 0;
|
|
|
|
memory[1] = flags & REDIR_BACKQ;
|
|
|
|
if (flags & REDIR_PUSH) {
|
2002-09-27 22:56:50 +04:00
|
|
|
/* We don't have to worry about REDIR_VFORK here, as
|
|
|
|
* flags & REDIR_PUSH is never true if REDIR_VFORK is set.
|
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
sv = ckmalloc(sizeof (struct redirtab));
|
|
|
|
for (i = 0 ; i < 10 ; i++)
|
|
|
|
sv->renamed[i] = EMPTY;
|
|
|
|
sv->next = redirlist;
|
|
|
|
redirlist = sv;
|
|
|
|
}
|
|
|
|
for (n = redir ; n ; n = n->nfile.next) {
|
|
|
|
fd = n->nfile.fd;
|
1997-04-21 16:38:25 +04:00
|
|
|
try = 0;
|
1996-10-16 19:16:04 +04:00
|
|
|
if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
|
|
|
|
n->ndup.dupfd == fd)
|
|
|
|
continue; /* redirect from/to same file descriptor */
|
1997-04-21 16:38:25 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
|
|
|
|
INTOFF;
|
1997-04-21 16:38:25 +04:00
|
|
|
again:
|
|
|
|
if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case EBADF:
|
|
|
|
if (!try) {
|
2004-07-08 07:57:33 +04:00
|
|
|
openredirect(n, memory, flags);
|
1997-04-21 16:38:25 +04:00
|
|
|
try++;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH*/
|
|
|
|
default:
|
|
|
|
INTON;
|
|
|
|
error("%d: %s", fd, strerror(errno));
|
1998-07-28 15:41:40 +04:00
|
|
|
/* NOTREACHED */
|
1997-04-21 16:38:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!try) {
|
1993-03-21 12:45:37 +03:00
|
|
|
sv->renamed[fd] = i;
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
INTON;
|
|
|
|
} else {
|
|
|
|
close(fd);
|
|
|
|
}
|
1994-05-11 21:09:42 +04:00
|
|
|
if (fd == 0)
|
|
|
|
fd0_redirected++;
|
1997-04-21 16:38:25 +04:00
|
|
|
if (!try)
|
2004-07-08 07:57:33 +04:00
|
|
|
openredirect(n, memory, flags);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (memory[1])
|
|
|
|
out1 = &memout;
|
|
|
|
if (memory[2])
|
|
|
|
out2 = &memout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
STATIC void
|
2004-07-08 07:57:33 +04:00
|
|
|
openredirect(union node *redir, char memory[10], int flags)
|
2002-11-25 01:35:38 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int fd = redir->nfile.fd;
|
|
|
|
char *fname;
|
|
|
|
int f;
|
2004-07-08 07:57:33 +04:00
|
|
|
int oflags = O_WRONLY|O_CREAT|O_TRUNC, eflags;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We suppress interrupts so that we won't leave open file
|
|
|
|
* descriptors around. This may not be such a good idea because
|
|
|
|
* an open of a device or a fifo can block indefinitely.
|
|
|
|
*/
|
|
|
|
INTOFF;
|
|
|
|
memory[fd] = 0;
|
|
|
|
switch (redir->nfile.type) {
|
|
|
|
case NFROM:
|
|
|
|
fname = redir->nfile.expfname;
|
2004-07-08 07:57:33 +04:00
|
|
|
if (flags & REDIR_VFORK)
|
|
|
|
eflags = O_NONBLOCK;
|
|
|
|
else
|
|
|
|
eflags = 0;
|
|
|
|
if ((f = open(fname, O_RDONLY|eflags)) < 0)
|
1999-02-04 19:17:39 +03:00
|
|
|
goto eopen;
|
2004-07-08 07:57:33 +04:00
|
|
|
if (eflags)
|
|
|
|
(void)fcntl(f, F_SETFL, fcntl(f, F_GETFL, 0) & ~eflags);
|
1999-02-04 19:17:39 +03:00
|
|
|
break;
|
|
|
|
case NFROMTO:
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if ((f = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0)
|
|
|
|
goto ecreate;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case NTO:
|
2002-05-15 20:33:35 +04:00
|
|
|
if (Cflag)
|
2004-07-08 07:57:33 +04:00
|
|
|
oflags |= O_EXCL;
|
2002-05-15 20:33:35 +04:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
case NCLOBBER:
|
1993-03-21 12:45:37 +03:00
|
|
|
fname = redir->nfile.expfname;
|
2004-07-08 07:57:33 +04:00
|
|
|
if ((f = open(fname, oflags, 0666)) < 0)
|
1999-02-04 19:17:39 +03:00
|
|
|
goto ecreate;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case NAPPEND:
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
|
1999-02-04 19:17:39 +03:00
|
|
|
goto ecreate;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case NTOFD:
|
|
|
|
case NFROMFD:
|
|
|
|
if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
|
|
|
|
if (memory[redir->ndup.dupfd])
|
|
|
|
memory[fd] = 1;
|
|
|
|
else
|
|
|
|
copyfd(redir->ndup.dupfd, fd);
|
|
|
|
}
|
1999-02-04 19:17:39 +03:00
|
|
|
INTON;
|
|
|
|
return;
|
1993-03-21 12:45:37 +03:00
|
|
|
case NHERE:
|
|
|
|
case NXHERE:
|
|
|
|
f = openhere(redir);
|
1999-02-04 19:17:39 +03:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
1999-02-04 19:17:39 +03:00
|
|
|
|
|
|
|
if (f != fd) {
|
|
|
|
copyfd(f, fd);
|
|
|
|
close(f);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
INTON;
|
1999-02-04 19:17:39 +03:00
|
|
|
return;
|
|
|
|
ecreate:
|
|
|
|
error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
|
|
|
|
eopen:
|
|
|
|
error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle here documents. Normally we fork off a process to write the
|
|
|
|
* data to a pipe. If the document is short, we can stuff the data in
|
|
|
|
* the pipe without forking.
|
|
|
|
*/
|
|
|
|
|
|
|
|
STATIC int
|
2002-11-25 01:35:38 +03:00
|
|
|
openhere(union node *redir)
|
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int pip[2];
|
1995-05-12 01:28:33 +04:00
|
|
|
int len = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (pipe(pip) < 0)
|
|
|
|
error("Pipe call failed");
|
|
|
|
if (redir->type == NHERE) {
|
|
|
|
len = strlen(redir->nhere.doc->narg.text);
|
|
|
|
if (len <= PIPESIZE) {
|
|
|
|
xwrite(pip[1], redir->nhere.doc->narg.text, len);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2002-09-28 05:25:01 +04:00
|
|
|
if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
close(pip[0]);
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
signal(SIGTSTP, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
if (redir->type == NHERE)
|
|
|
|
xwrite(pip[1], redir->nhere.doc->narg.text, len);
|
|
|
|
else
|
|
|
|
expandhere(redir->nhere.doc, pip[1]);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
close(pip[1]);
|
|
|
|
return pip[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undo the effects of the last redirection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-11-25 01:35:38 +03:00
|
|
|
popredir(void)
|
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
struct redirtab *rp = redirlist;
|
1993-03-21 12:45:37 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < 10 ; i++) {
|
|
|
|
if (rp->renamed[i] != EMPTY) {
|
1994-05-11 21:09:42 +04:00
|
|
|
if (i == 0)
|
|
|
|
fd0_redirected--;
|
1993-03-21 12:45:37 +03:00
|
|
|
close(i);
|
|
|
|
if (rp->renamed[i] >= 0) {
|
|
|
|
copyfd(rp->renamed[i], i);
|
|
|
|
close(rp->renamed[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INTOFF;
|
|
|
|
redirlist = rp->next;
|
|
|
|
ckfree(rp);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undo all redirections. Called on error or interrupt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef mkinit
|
|
|
|
|
|
|
|
INCLUDE "redir.h"
|
|
|
|
|
|
|
|
RESET {
|
|
|
|
while (redirlist)
|
|
|
|
popredir();
|
|
|
|
}
|
|
|
|
|
|
|
|
SHELLPROC {
|
2002-09-27 22:56:50 +04:00
|
|
|
clearredir(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1994-05-11 21:09:42 +04:00
|
|
|
/* Return true if fd 0 has already been redirected at least once. */
|
|
|
|
int
|
|
|
|
fd0_redirected_p () {
|
|
|
|
return fd0_redirected != 0;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Discard all saved file descriptors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-09-27 22:56:50 +04:00
|
|
|
clearredir(vforked)
|
|
|
|
int vforked;
|
|
|
|
{
|
1997-01-11 05:04:27 +03:00
|
|
|
struct redirtab *rp;
|
1993-03-21 12:45:37 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (rp = redirlist ; rp ; rp = rp->next) {
|
|
|
|
for (i = 0 ; i < 10 ; i++) {
|
|
|
|
if (rp->renamed[i] >= 0) {
|
|
|
|
close(rp->renamed[i]);
|
|
|
|
}
|
2002-09-27 22:56:50 +04:00
|
|
|
if (!vforked)
|
|
|
|
rp->renamed[i] = EMPTY;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-05-11 21:09:42 +04:00
|
|
|
* Copy a file descriptor to be >= to. Returns -1
|
1993-03-21 12:45:37 +03:00
|
|
|
* if the source file descriptor is closed, EMPTY if there are no unused
|
|
|
|
* file descriptors left.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2002-11-25 01:35:38 +03:00
|
|
|
copyfd(int from, int to)
|
1994-12-05 22:07:32 +03:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
int newfd;
|
|
|
|
|
|
|
|
newfd = fcntl(from, F_DUPFD, to);
|
1996-10-16 19:16:04 +04:00
|
|
|
if (newfd < 0) {
|
|
|
|
if (errno == EMFILE)
|
|
|
|
return EMPTY;
|
|
|
|
else
|
|
|
|
error("%d: %s", from, strerror(errno));
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return newfd;
|
1993-05-02 05:28:40 +04:00
|
|
|
}
|