Update.
[glibc.git] / sysdeps / mach / hurd / dup2.c
blobfefca37873a8aa581dbe7b27022bf4ed5c6a8b46
1 /* Copyright (C) 1991, 92, 93, 94, 95, 97 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <hurd.h>
23 #include <hurd/fd.h>
26 /* Duplicate FD to FD2, closing the old FD2 and making FD2 be
27 open on the same file as FD is. Return FD2 or -1. */
28 int
29 __dup2 (fd, fd2)
30 int fd;
31 int fd2;
33 struct hurd_fd *d;
35 /* Extract the ports and flags from FD. */
36 d = _hurd_fd_get (fd);
37 if (d == NULL)
39 errno = EBADF;
40 return -1;
43 HURD_CRITICAL_BEGIN;
45 __spin_lock (&d->port.lock);
46 if (d->port.port == MACH_PORT_NULL)
48 __spin_unlock (&d->port.lock);
49 errno = EBADF;
50 fd2 = -1;
52 else if (fd2 == fd)
53 /* FD is valid and FD2 is already the same; just return it. */
54 __spin_unlock (&d->port.lock);
55 else
57 struct hurd_userlink ulink, ctty_ulink;
58 int flags = d->flags;
59 io_t ctty = _hurd_port_get (&d->ctty, &ctty_ulink);
60 io_t port = _hurd_port_locked_get (&d->port, &ulink); /* Unlocks D. */
62 if (fd2 < 0)
64 errno = EBADF;
65 fd2 = -1;
67 else
69 /* Get a hold of the destination descriptor. */
70 struct hurd_fd *d2;
72 if (fd2 >= _hurd_dtablesize)
74 /* The table is not large enough to hold the destination
75 descriptor. Enlarge it as necessary to allocate this
76 descriptor. */
77 __mutex_unlock (&_hurd_dtable_lock);
78 /* We still hold FD1's lock, but this is safe because
79 _hurd_alloc_fd will only examine the cells starting
80 at FD2. */
81 d2 = _hurd_alloc_fd (NULL, fd2);
82 if (d2)
83 __spin_unlock (&d2->port.lock);
84 __mutex_lock (&_hurd_dtable_lock);
86 else
88 d2 = _hurd_dtable[fd2];
89 if (d2 == NULL)
91 /* Must allocate a new one. We don't initialize the port
92 cells with this call so that if it fails (out of
93 memory), we will not have already added user
94 references for the ports, which we would then have to
95 deallocate. */
96 d2 = _hurd_dtable[fd2] = _hurd_new_fd (MACH_PORT_NULL,
97 MACH_PORT_NULL);
101 if (d2 == NULL)
103 fd2 = -1;
104 if (errno == EINVAL)
105 errno = EBADF; /* POSIX.1-1990 6.2.1.2 ll 54-55. */
107 else
109 /* Give the ports each a user ref for the new descriptor. */
110 __mach_port_mod_refs (__mach_task_self (), port,
111 MACH_PORT_RIGHT_SEND, 1);
112 if (ctty != MACH_PORT_NULL)
113 __mach_port_mod_refs (__mach_task_self (), ctty,
114 MACH_PORT_RIGHT_SEND, 1);
116 /* Install the ports and flags in the new descriptor slot. */
117 __spin_lock (&d2->port.lock);
118 d2->flags = flags & ~FD_CLOEXEC; /* Dup clears FD_CLOEXEC. */
119 _hurd_port_set (&d2->ctty, ctty);
120 _hurd_port_locked_set (&d2->port, port); /* Unlocks D2. */
123 __mutex_unlock (&_hurd_dtable_lock);
125 _hurd_port_free (&d->port, &ulink, port);
126 if (ctty != MACH_PORT_NULL)
127 _hurd_port_free (&d->ctty, &ctty_ulink, port);
130 HURD_CRITICAL_END;
132 return fd2;
135 weak_alias (__dup2, dup2)