Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / mach / hurd / dup2.c
blobd2329c936ff5a0e0cfa730b21544b0bf07d36834
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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <hurd.h>
22 #include <hurd/fd.h>
24 /* Duplicate FD to FD2, closing the old FD2 and making FD2 be
25 open on the same file as FD is. Return FD2 or -1. */
26 int
27 __dup2 (fd, fd2)
28 int fd;
29 int fd2;
31 struct hurd_fd *d;
33 /* Extract the ports and flags from FD. */
34 d = _hurd_fd_get (fd);
35 if (d == NULL)
37 errno = EBADF;
38 return -1;
41 HURD_CRITICAL_BEGIN;
43 __spin_lock (&d->port.lock);
44 if (d->port.port == MACH_PORT_NULL)
46 __spin_unlock (&d->port.lock);
47 errno = EBADF;
48 fd2 = -1;
50 else if (fd2 == fd)
51 /* FD is valid and FD2 is already the same; just return it. */
52 __spin_unlock (&d->port.lock);
53 else
55 struct hurd_userlink ulink, ctty_ulink;
56 int flags = d->flags;
57 io_t ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
58 io_t port = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D. */
60 if (fd2 < 0)
62 errno = EBADF;
63 fd2 = -1;
65 else
67 /* Get a hold of the destination descriptor. */
68 struct hurd_fd *d2;
70 if (fd2 >= _hurd_dtablesize)
72 /* The table is not large enough to hold the destination
73 descriptor. Enlarge it as necessary to allocate this
74 descriptor. */
75 __mutex_unlock (&_hurd_dtable_lock);
76 /* We still hold FD1's lock, but this is safe because
77 _hurd_alloc_fd will only examine the cells starting
78 at FD2. */
79 d2 = _hurd_alloc_fd (NULL, fd2);
80 if (d2)
81 __spin_unlock (&d2->port.lock);
82 __mutex_lock (&_hurd_dtable_lock);
84 else
86 d2 = _hurd_dtable[fd2];
87 if (d2 == NULL)
89 /* Must allocate a new one. We don't initialize the port
90 cells with this call so that if it fails (out of
91 memory), we will not have already added user
92 references for the ports, which we would then have to
93 deallocate. */
94 d2 = _hurd_dtable[fd2] = _hurd_new_fd (MACH_PORT_NULL,
95 MACH_PORT_NULL);
99 if (d2 == NULL)
101 fd2 = -1;
102 if (errno == EINVAL)
103 errno = EBADF; /* POSIX.1-1990 6.2.1.2 ll 54-55. */
105 else
107 /* Give the ports each a user ref for the new descriptor. */
108 __mach_port_mod_refs (__mach_task_self (), port,
109 MACH_PORT_RIGHT_SEND, 1);
110 if (ctty != MACH_PORT_NULL)
111 __mach_port_mod_refs (__mach_task_self (), ctty,
112 MACH_PORT_RIGHT_SEND, 1);
114 /* Install the ports and flags in the new descriptor slot. */
115 __spin_lock (&d2->port.lock);
116 d2->flags = flags & ~FD_CLOEXEC; /* Dup clears FD_CLOEXEC. */
117 _hurd_port_set (&d2->ctty, ctty);
118 _hurd_port_locked_set (&d2->port, port); /* Unlocks D2. */
121 __mutex_unlock (&_hurd_dtable_lock);
123 _hurd_port_free (&d->port, &ulink, port);
124 if (ctty != MACH_PORT_NULL)
125 _hurd_port_free (&d->ctty, &ctty_ulink, port);
128 HURD_CRITICAL_END;
130 return fd2;
132 libc_hidden_def (__dup2)
133 weak_alias (__dup2, dup2)