]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc_r/uthread/uthread_dup2.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc_r / uthread / uthread_dup2.c
1 /*
2  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <errno.h>
32 #include <unistd.h>
33 #include <pthread.h>
34 #include "pthread_private.h"
35
36 __weak_reference(_dup2, dup2);
37
38 int
39 _dup2(int fd, int newfd)
40 {
41         int             ret;
42         int             newfd_opened;
43
44         /* Check if the file descriptor is out of range: */
45         if (newfd < 0 || newfd >= _thread_dtablesize ||
46             newfd == _thread_kern_pipe[0] || newfd == _thread_kern_pipe[1]) {
47                 /* Return a bad file descriptor error: */
48                 errno = EBADF;
49                 ret = -1;
50         }
51
52         /* Lock the file descriptor: */
53         else if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) {
54                 /* Lock the file descriptor: */
55                 if (!(newfd_opened = (_thread_fd_table[newfd] != NULL)) || 
56                     (ret = _FD_LOCK(newfd, FD_RDWR, NULL)) == 0) {
57                         /* Perform the 'dup2' syscall: */
58                         ret = __sys_dup2(fd, newfd);
59                         if (ret >= 0) {
60                                 /*
61                                  * If we are duplicating one of the standard
62                                  * file descriptors, update its flags in the
63                                  * table of pthread stdio descriptor flags.
64                                  */
65                                 if (ret < 3) {
66                                         _pthread_stdio_flags[ret] =
67                                             _thread_fd_table[fd]->flags;
68                                 }
69                                 /* Initialise the file descriptor table entry */
70                                 if (_thread_fd_table_init(ret) != 0) {
71                                         /* Quietly close the file: */
72                                         __sys_close(ret);
73
74                                         /* Reset the file descriptor: */
75                                         ret = -1;
76                                 } else {
77                                         /*
78                                          * Save the file open flags so that
79                                          * they can be checked later: 
80                                          */
81                                         _thread_fd_setflags(ret,
82                                             _thread_fd_getflags(fd));
83                                 }
84                         }
85
86                         /* Unlock the file descriptor: */
87                         if (newfd_opened)
88                                 _FD_UNLOCK(newfd, FD_RDWR);
89                 }
90                 /* Unlock the file descriptor: */
91                 _FD_UNLOCK(fd, FD_RDWR);
92         }
93         /* Return the completion status: */
94         return (ret);
95 }