Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / accept4.c
blob142eafc780dd6f69375813cb0c6c79e353be1ea3
1 /* Copyright (C) 1992-2014 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/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <fcntl-internal.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <hurd.h>
25 #include <hurd/fd.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. */
34 int
35 __libc_accept4 (int fd, __SOCKADDR_ARG addrarg, socklen_t *addr_len, int flags)
37 error_t err;
38 socket_t new;
39 addr_port_t aport;
40 struct sockaddr *addr = addrarg.__sockaddr__;
41 char *buf = (char *) addr;
42 mach_msg_type_number_t buflen;
43 int type;
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);
53 if (addr != NULL)
55 buflen = *addr_len;
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
59 zero-length one. */
61 buf = (char *)addr;
62 buflen = 0;
63 err = 0;
66 __mach_port_deallocate (__mach_task_self (), aport);
68 if (! err)
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? */
75 if (err)
77 __mach_port_deallocate (__mach_task_self (), new);
78 return __hurd_dfail (fd, err);
81 if (addr != NULL)
83 if (*addr_len > buflen)
84 *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);
92 if (buflen > 0)
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)