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<.
This commit is contained in:
parent
55cb182a23
commit
09683f46b9
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=216851
@ -155,6 +155,7 @@ openredirect(union node *redir, char memory[10])
|
|||||||
int fd = redir->nfile.fd;
|
int fd = redir->nfile.fd;
|
||||||
char *fname;
|
char *fname;
|
||||||
int f;
|
int f;
|
||||||
|
int e;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We suppress interrupts so that we won't leave open file
|
* 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));
|
error("cannot open %s: %s", fname, strerror(errno));
|
||||||
movefd:
|
movefd:
|
||||||
if (f != fd) {
|
if (f != fd) {
|
||||||
dup2(f, fd);
|
if (dup2(f, fd) == -1) {
|
||||||
|
e = errno;
|
||||||
|
close(f);
|
||||||
|
error("%d: %s", fd, strerror(e));
|
||||||
|
}
|
||||||
close(f);
|
close(f);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
7
tools/regression/bin/sh/errors/redirection-error7.0
Normal file
7
tools/regression/bin/sh/errors/redirection-error7.0
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# $FreeBSD$
|
||||||
|
|
||||||
|
! dummy=$(
|
||||||
|
exec 3>&1 >&2 2>&3
|
||||||
|
ulimit -n 9
|
||||||
|
exec 9<.
|
||||||
|
) && [ -n "$dummy" ]
|
Loading…
Reference in New Issue
Block a user