Updated to fedora-glibc-20080410T1907
[glibc.git] / sysdeps / mach / hurd / dup2.c
blob3abd30cfbe23f5c919e66ed70b5f88643e64d941
1 /* Copyright (C) 1991, 92, 93, 94, 95, 97, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <hurd.h>
23 #include <hurd/fd.h>
25 /* Duplicate FD to FD2, closing the old FD2 and making FD2 be
26 open on the same file as FD is. Return FD2 or -1. */
27 int
28 __dup2 (fd, fd2)
29 int fd;
30 int fd2;
32 struct hurd_fd *d;
34 /* Extract the ports and flags from FD. */
35 d = _hurd_fd_get (fd);
36 if (d == NULL)
38 errno = EBADF;
39 return -1;
42 HURD_CRITICAL_BEGIN;
44 __spin_lock (&d->port.lock);
45 if (d->port.port == MACH_PORT_NULL)
47 __spin_unlock (&d->port.lock);
48 errno = EBADF;
49 fd2 = -1;
51 else if (fd2 == fd)
52 /* FD is valid and FD2 is already the same; just return it. */
53 __spin_unlock (&d->port.lock);
54 else
56 struct hurd_userlink ulink, ctty_ulink;
57 int flags = d->flags;
58 io_t ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
59 io_t port = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D. */
61 if (fd2 < 0)
63 errno = EBADF;
64 fd2 = -1;
66 else
68 /* Get a hold of the destination descriptor. */
69 struct hurd_fd *d2;
71 if (fd2 >= _hurd_dtablesize)
73 /* The table is not large enough to hold the destination
74 descriptor. Enlarge it as necessary to allocate this
75 descriptor. */
76 __mutex_unlock (&_hurd_dtable_lock);
77 /* We still hold FD1's lock, but this is safe because
78 _hurd_alloc_fd will only examine the cells starting
79 at FD2. */
80 d2 = _hurd_alloc_fd (NULL, fd2);
81 if (d2)
82 __spin_unlock (&d2->port.lock);
83 __mutex_lock (&_hurd_dtable_lock);
85 else
87 d2 = _hurd_dtable[fd2];
88 if (d2 == NULL)
90 /* Must allocate a new one. We don't initialize the port
91 cells with this call so that if it fails (out of
92 memory), we will not have already added user
93 references for the ports, which we would then have to
94 deallocate. */
95 d2 = _hurd_dtable[fd2] = _hurd_new_fd (MACH_PORT_NULL,
96 MACH_PORT_NULL);
100 if (d2 == NULL)
102 fd2 = -1;
103 if (errno == EINVAL)
104 errno = EBADF; /* POSIX.1-1990 6.2.1.2 ll 54-55. */
106 else
108 /* Give the ports each a user ref for the new descriptor. */
109 __mach_port_mod_refs (__mach_task_self (), port,
110 MACH_PORT_RIGHT_SEND, 1);
111 if (ctty != MACH_PORT_NULL)
112 __mach_port_mod_refs (__mach_task_self (), ctty,
113 MACH_PORT_RIGHT_SEND, 1);
115 /* Install the ports and flags in the new descriptor slot. */
116 __spin_lock (&d2->port.lock);
117 d2->flags = flags & ~FD_CLOEXEC; /* Dup clears FD_CLOEXEC. */
118 _hurd_port_set (&d2->ctty, ctty);
119 _hurd_port_locked_set (&d2->port, port); /* Unlocks D2. */
122 __mutex_unlock (&_hurd_dtable_lock);
124 _hurd_port_free (&d->port, &ulink, port);
125 if (ctty != MACH_PORT_NULL)
126 _hurd_port_free (&d->ctty, &ctty_ulink, port);
129 HURD_CRITICAL_END;
131 return fd2;
133 libc_hidden_def (__dup2)
134 weak_alias (__dup2, dup2)