1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)redir.c 8.2 (Berkeley) 5/4/95";
|
|
|
|
#endif
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <sys/types.h>
|
2002-05-19 06:03:05 +00:00
|
|
|
#include <sys/stat.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
/*
|
|
|
|
* Code for dealing with input/output redirection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shell.h"
|
|
|
|
#include "nodes.h"
|
|
|
|
#include "jobs.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "redir.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "memalloc.h"
|
|
|
|
#include "error.h"
|
2002-05-19 06:03:05 +00:00
|
|
|
#include "options.h"
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define EMPTY -2 /* marks an unused slot in redirtab */
|
2009-11-29 22:33:59 +00:00
|
|
|
#define CLOSED -1 /* fd was not open before redir */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct redirtab {
|
|
|
|
struct redirtab *next;
|
2002-07-18 12:24:35 +00:00
|
|
|
int renamed[10];
|
2013-11-24 23:12:13 +00:00
|
|
|
int fd0_redirected;
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-25 19:48:15 +00:00
|
|
|
static struct redirtab *redirlist;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
1995-05-30 00:07:29 +00:00
|
|
|
/*
|
1994-05-26 06:18:55 +00: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
|
1995-05-30 00:07:29 +00:00
|
|
|
* if it hasn't already been redirected.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
2010-10-13 04:01:01 +00:00
|
|
|
static int fd0_redirected = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void openredirect(union node *, char[10 ]);
|
|
|
|
static int openhere(union node *);
|
1994-05-26 06:18:55 +00: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.
|
2014-03-26 20:43:40 +00:00
|
|
|
*
|
|
|
|
* We suppress interrupts so that we won't leave open file
|
|
|
|
* descriptors around. Because the signal handler remains
|
|
|
|
* installed and we do not use system call restart, interrupts
|
|
|
|
* will still abort blocking opens such as fifos (they will fail
|
|
|
|
* with EINTR). There is, however, a race condition if an interrupt
|
|
|
|
* arrives after INTOFF and before open blocks.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
redirect(union node *redir, int flags)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
union node *n;
|
1996-09-01 10:22:36 +00:00
|
|
|
struct redirtab *sv = NULL;
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
|
|
|
int fd;
|
1996-12-14 06:20:03 +00:00
|
|
|
char memory[10]; /* file descriptors to write to memory */
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2014-03-26 20:43:40 +00:00
|
|
|
INTOFF;
|
1994-05-26 06:18:55 +00:00
|
|
|
for (i = 10 ; --i >= 0 ; )
|
|
|
|
memory[i] = 0;
|
|
|
|
memory[1] = flags & REDIR_BACKQ;
|
|
|
|
if (flags & REDIR_PUSH) {
|
|
|
|
sv = ckmalloc(sizeof (struct redirtab));
|
|
|
|
for (i = 0 ; i < 10 ; i++)
|
|
|
|
sv->renamed[i] = EMPTY;
|
2013-11-24 23:12:13 +00:00
|
|
|
sv->fd0_redirected = fd0_redirected;
|
1994-05-26 06:18:55 +00:00
|
|
|
sv->next = redirlist;
|
|
|
|
redirlist = sv;
|
|
|
|
}
|
|
|
|
for (n = redir ; n ; n = n->nfile.next) {
|
|
|
|
fd = n->nfile.fd;
|
2013-11-24 23:12:13 +00:00
|
|
|
if (fd == 0)
|
|
|
|
fd0_redirected = 1;
|
1995-10-21 00:47:32 +00:00
|
|
|
if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
|
|
|
|
n->ndup.dupfd == fd)
|
1996-12-14 06:20:03 +00:00
|
|
|
continue; /* redirect from/to same file descriptor */
|
1997-04-28 03:25:13 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
|
|
|
|
INTOFF;
|
2013-05-05 10:51:40 +00:00
|
|
|
if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) {
|
1997-04-28 03:25:13 +00:00
|
|
|
switch (errno) {
|
|
|
|
case EBADF:
|
2009-11-29 22:33:59 +00:00
|
|
|
i = CLOSED;
|
|
|
|
break;
|
1997-04-28 03:25:13 +00:00
|
|
|
default:
|
|
|
|
INTON;
|
|
|
|
error("%d: %s", fd, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2013-05-05 10:51:40 +00:00
|
|
|
}
|
2009-11-29 22:33:59 +00:00
|
|
|
sv->renamed[fd] = i;
|
1994-05-26 06:18:55 +00:00
|
|
|
INTON;
|
|
|
|
}
|
2009-11-29 22:33:59 +00:00
|
|
|
openredirect(n, memory);
|
2014-03-26 20:43:40 +00:00
|
|
|
INTON;
|
|
|
|
INTOFF;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
if (memory[1])
|
|
|
|
out1 = &memout;
|
|
|
|
if (memory[2])
|
|
|
|
out2 = &memout;
|
2014-03-26 20:43:40 +00:00
|
|
|
INTON;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
openredirect(union node *redir, char memory[10])
|
|
|
|
{
|
2002-05-19 06:03:05 +00:00
|
|
|
struct stat sb;
|
1994-05-26 06:18:55 +00:00
|
|
|
int fd = redir->nfile.fd;
|
2014-03-14 21:45:37 +00:00
|
|
|
const char *fname;
|
1994-05-26 06:18:55 +00:00
|
|
|
int f;
|
2010-12-31 18:20:17 +00:00
|
|
|
int e;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
memory[fd] = 0;
|
|
|
|
switch (redir->nfile.type) {
|
|
|
|
case NFROM:
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if ((f = open(fname, O_RDONLY)) < 0)
|
2002-09-29 11:37:39 +00:00
|
|
|
error("cannot open %s: %s", fname, strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
movefd:
|
|
|
|
if (f != fd) {
|
2010-12-31 18:20:17 +00:00
|
|
|
if (dup2(f, fd) == -1) {
|
|
|
|
e = errno;
|
|
|
|
close(f);
|
|
|
|
error("%d: %s", fd, strerror(e));
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
close(f);
|
|
|
|
}
|
|
|
|
break;
|
2000-10-03 23:13:14 +00:00
|
|
|
case NFROMTO:
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if ((f = open(fname, O_RDWR|O_CREAT, 0666)) < 0)
|
2002-09-29 11:37:39 +00:00
|
|
|
error("cannot create %s: %s", fname, strerror(errno));
|
2000-10-03 23:13:14 +00:00
|
|
|
goto movefd;
|
1994-05-26 06:18:55 +00:00
|
|
|
case NTO:
|
2009-06-20 20:44:27 +00:00
|
|
|
if (Cflag) {
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if (stat(fname, &sb) == -1) {
|
|
|
|
if ((f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0)
|
|
|
|
error("cannot create %s: %s", fname, strerror(errno));
|
|
|
|
} else if (!S_ISREG(sb.st_mode)) {
|
|
|
|
if ((f = open(fname, O_WRONLY, 0666)) < 0)
|
|
|
|
error("cannot create %s: %s", fname, strerror(errno));
|
|
|
|
if (fstat(f, &sb) != -1 && S_ISREG(sb.st_mode)) {
|
|
|
|
close(f);
|
|
|
|
error("cannot create %s: %s", fname,
|
|
|
|
strerror(EEXIST));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
error("cannot create %s: %s", fname,
|
|
|
|
strerror(EEXIST));
|
|
|
|
goto movefd;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2002-05-19 06:03:05 +00:00
|
|
|
case NCLOBBER:
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
|
2002-09-29 11:37:39 +00:00
|
|
|
error("cannot create %s: %s", fname, strerror(errno));
|
2002-05-19 06:03:05 +00:00
|
|
|
goto movefd;
|
1994-05-26 06:18:55 +00:00
|
|
|
case NAPPEND:
|
|
|
|
fname = redir->nfile.expfname;
|
|
|
|
if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
|
2002-09-29 11:37:39 +00:00
|
|
|
error("cannot create %s: %s", fname, strerror(errno));
|
1994-05-26 06:18:55 +00:00
|
|
|
goto movefd;
|
|
|
|
case NTOFD:
|
|
|
|
case NFROMFD:
|
|
|
|
if (redir->ndup.dupfd >= 0) { /* if not ">&-" */
|
|
|
|
if (memory[redir->ndup.dupfd])
|
|
|
|
memory[fd] = 1;
|
2010-10-24 20:09:49 +00:00
|
|
|
else {
|
|
|
|
if (dup2(redir->ndup.dupfd, fd) < 0)
|
|
|
|
error("%d: %s", redir->ndup.dupfd,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2004-01-21 12:50:01 +00:00
|
|
|
} else {
|
2002-07-09 08:55:46 +00:00
|
|
|
close(fd);
|
2004-01-21 12:50:01 +00:00
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
break;
|
|
|
|
case NHERE:
|
|
|
|
case NXHERE:
|
|
|
|
f = openhere(redir);
|
|
|
|
goto movefd;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-10-13 22:18:03 +00:00
|
|
|
static int
|
2002-02-02 06:50:57 +00:00
|
|
|
openhere(union node *redir)
|
|
|
|
{
|
2014-03-14 21:45:37 +00:00
|
|
|
const char *p;
|
1994-05-26 06:18:55 +00:00
|
|
|
int pip[2];
|
2013-04-02 21:34:38 +00:00
|
|
|
size_t len = 0;
|
|
|
|
int flags;
|
|
|
|
ssize_t written = 0;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
if (pipe(pip) < 0)
|
1999-11-29 19:11:01 +00:00
|
|
|
error("Pipe call failed: %s", strerror(errno));
|
2013-02-03 15:54:57 +00:00
|
|
|
|
|
|
|
if (redir->type == NXHERE)
|
|
|
|
p = redir->nhere.expdoc;
|
|
|
|
else
|
|
|
|
p = redir->nhere.doc->narg.text;
|
|
|
|
len = strlen(p);
|
2013-04-02 21:34:38 +00:00
|
|
|
if (len == 0)
|
2013-02-03 15:54:57 +00:00
|
|
|
goto out;
|
2013-04-02 21:34:38 +00:00
|
|
|
flags = fcntl(pip[1], F_GETFL, 0);
|
|
|
|
if (flags != -1 && fcntl(pip[1], F_SETFL, flags | O_NONBLOCK) != -1) {
|
|
|
|
written = write(pip[1], p, len);
|
|
|
|
if (written < 0)
|
|
|
|
written = 0;
|
|
|
|
if ((size_t)written == len)
|
|
|
|
goto out;
|
|
|
|
fcntl(pip[1], F_SETFL, flags);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2013-02-03 15:54:57 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
|
|
|
|
close(pip[0]);
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
signal(SIGTSTP, SIG_IGN);
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
2013-04-02 21:34:38 +00:00
|
|
|
xwrite(pip[1], p + written, len - written);
|
1994-05-26 06:18:55 +00:00
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
close(pip[1]);
|
|
|
|
return pip[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Undo the effects of the last redirection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
popredir(void)
|
|
|
|
{
|
1997-04-28 03:25:13 +00:00
|
|
|
struct redirtab *rp = redirlist;
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < 10 ; i++) {
|
|
|
|
if (rp->renamed[i] != EMPTY) {
|
|
|
|
if (rp->renamed[i] >= 0) {
|
2004-01-21 12:50:01 +00:00
|
|
|
dup2(rp->renamed[i], i);
|
1994-05-26 06:18:55 +00:00
|
|
|
close(rp->renamed[i]);
|
2004-01-21 12:50:01 +00:00
|
|
|
} else {
|
|
|
|
close(i);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INTOFF;
|
2013-11-24 23:12:13 +00:00
|
|
|
fd0_redirected = rp->fd0_redirected;
|
1994-05-26 06:18:55 +00:00
|
|
|
redirlist = rp->next;
|
|
|
|
ckfree(rp);
|
|
|
|
INTON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return true if fd 0 has already been redirected at least once. */
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
fd0_redirected_p(void)
|
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
return fd0_redirected != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Discard all saved file descriptors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2002-02-02 06:50:57 +00:00
|
|
|
clearredir(void)
|
|
|
|
{
|
1997-04-28 03:25:13 +00:00
|
|
|
struct redirtab *rp;
|
1994-05-26 06:18:55 +00: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]);
|
|
|
|
}
|
|
|
|
rp->renamed[i] = EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|