aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitrij Tejblum <dt@FreeBSD.org>1998-09-13 12:48:47 +0000
committerDmitrij Tejblum <dt@FreeBSD.org>1998-09-13 12:48:47 +0000
commit99c167bba42a748e5e967393b7ffe75e0413b7c5 (patch)
tree417d5f0ae94729dd29e3fc226d73f3b1a538cd9f /lib
parent2b1470681afa6e6232e0316bd156dff9ed1430d0 (diff)
Notes
Diffstat (limited to 'lib')
-rw-r--r--lib/libc_r/uthread/uthread_dup2.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/libc_r/uthread/uthread_dup2.c b/lib/libc_r/uthread/uthread_dup2.c
index e9edddd35c10..3e4ed27d359e 100644
--- a/lib/libc_r/uthread/uthread_dup2.c
+++ b/lib/libc_r/uthread/uthread_dup2.c
@@ -30,6 +30,7 @@
* SUCH DAMAGE.
*
*/
+#include <errno.h>
#include <unistd.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
@@ -39,11 +40,20 @@ int
dup2(int fd, int newfd)
{
int ret;
+ int newfd_opened;
+
+ /* Check if the file descriptor is out of range: */
+ if (newfd < 0 || newfd >= _thread_dtablesize) {
+ /* Return a bad file descriptor error: */
+ errno = EBADF;
+ ret = -1;
+ }
/* Lock the file descriptor: */
- if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
+ else if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
/* Lock the file descriptor: */
- if ((ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) {
+ if (!(newfd_opened = (_thread_fd_table[newfd] != NULL)) ||
+ (ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) {
/* Perform the 'dup2' syscall: */
if ((ret = _thread_sys_dup2(fd, newfd)) < 0) {
}
@@ -63,7 +73,8 @@ dup2(int fd, int newfd)
}
/* Unlock the file descriptor: */
- _FD_UNLOCK(newfd, FD_RDWR);
+ if (newfd_opened)
+ _FD_UNLOCK(newfd, FD_RDWR);
}
/* Unlock the file descriptor: */
_FD_UNLOCK(fd, FD_RDWR);