diff options
author | jilles <jilles@FreeBSD.org> | 2010-12-31 18:20:17 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2010-12-31 18:20:17 +0000 |
commit | e3df947be8e062fd21aacfd052dcc53447dccf9e (patch) | |
tree | 32b7bc3e92c269428ce2433b4a73155bde72d7db | |
parent | 1ce0b656f0e212413dbb5847eafd6214a278a66b (diff) | |
download | FreeBSD-src-e3df947be8e062fd21aacfd052dcc53447dccf9e.zip FreeBSD-src-e3df947be8e062fd21aacfd052dcc53447dccf9e.tar.gz |
sh: Check if dup2 for redirection from/to a file succeeds.
A failure (e.g. caused by ulimit -n being set very low) is a redirection
error.
Example:
ulimit -n 9; exec 9<.
-rw-r--r-- | bin/sh/redir.c | 7 | ||||
-rw-r--r-- | tools/regression/bin/sh/errors/redirection-error7.0 | 7 |
2 files changed, 13 insertions, 1 deletions
diff --git a/bin/sh/redir.c b/bin/sh/redir.c index 4f0a40c..6e6f7a5 100644 --- a/bin/sh/redir.c +++ b/bin/sh/redir.c @@ -155,6 +155,7 @@ openredirect(union node *redir, char memory[10]) int fd = redir->nfile.fd; char *fname; int f; + int e; /* * We suppress interrupts so that we won't leave open file @@ -173,7 +174,11 @@ openredirect(union node *redir, char memory[10]) error("cannot open %s: %s", fname, strerror(errno)); movefd: if (f != fd) { - dup2(f, fd); + if (dup2(f, fd) == -1) { + e = errno; + close(f); + error("%d: %s", fd, strerror(e)); + } close(f); } break; diff --git a/tools/regression/bin/sh/errors/redirection-error7.0 b/tools/regression/bin/sh/errors/redirection-error7.0 new file mode 100644 index 0000000..5b20f04 --- /dev/null +++ b/tools/regression/bin/sh/errors/redirection-error7.0 @@ -0,0 +1,7 @@ +# $FreeBSD$ + +! dummy=$( + exec 3>&1 >&2 2>&3 + ulimit -n 9 + exec 9<. +) && [ -n "$dummy" ] |