1 /* Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://www.gnu.org/licenses/>. */
21 #include <fcntl-internal.h>
23 #include <sys/socket.h>
26 #include <hurd/socket.h>
28 /* Await a connection on socket FD.
29 When a connection arrives, open a new socket to communicate with it,
30 set *ADDRARG (which is *ADDR_LEN bytes long) to the address of the connecting
31 peer and *ADDR_LEN to the address's actual length, and return the
32 new socket's descriptor, or -1 for errors. The operation can be influenced
33 by the FLAGS parameter. */
35 __libc_accept4 (int fd, __SOCKADDR_ARG addrarg, socklen_t *addr_len, int flags)
40 struct sockaddr *addr = addrarg.__sockaddr__;
41 char *buf = (char *) addr;
42 mach_msg_type_number_t buflen;
45 flags = sock_to_o_flags (flags);
47 if (flags & ~(O_CLOEXEC | O_NONBLOCK))
48 return __hurd_fail (EINVAL);
50 if (err = HURD_DPORT_USE (fd, __socket_accept (port, &new, &aport)))
51 return __hurd_dfail (fd, err);
56 err = __socket_whatis_address (aport, &type, &buf, &buflen);
57 if (err == EOPNOTSUPP)
58 /* If the protocol server can't tell us the address, just return a
66 __mach_port_deallocate (__mach_task_self (), aport);
70 if (flags & O_NONBLOCK)
71 err = __io_set_some_openmodes (new, O_NONBLOCK);
72 /* TODO: do we need special ERR massaging after the previous call? */
77 __mach_port_deallocate (__mach_task_self (), new);
78 return __hurd_dfail (fd, err);
83 if (*addr_len > buflen)
86 if (buf != (char *) addr)
88 memcpy (addr, buf, *addr_len);
89 __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
93 addr->sa_family = type;
96 return _hurd_intern_fd (new, O_IGNORE_CTTY | flags, 1);
98 libc_hidden_def (__libc_accept4)
99 weak_alias (__libc_accept4, accept4)