Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / sysdeps / mach / hurd / accept4.c
1 /* Copyright (C) 1992-2013 Free Software Foundation, Inc.
2
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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/>.  */
18
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>
27
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)
36 {
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;
44
45   flags = sock_to_o_flags (flags);
46
47   if (flags & ~(O_CLOEXEC | O_NONBLOCK))
48     return __hurd_fail (EINVAL);
49
50   if (err = HURD_DPORT_USE (fd, __socket_accept (port, &new, &aport)))
51     return __hurd_dfail (fd, err);
52
53   if (addr != NULL)
54     {
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.  */
60         {
61           buf = (char *)addr;
62           buflen = 0;
63           err = 0;
64         }
65     }
66   __mach_port_deallocate (__mach_task_self (), aport);
67
68   if (! err)
69     {
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?  */
73     }
74
75   if (err)
76     {
77       __mach_port_deallocate (__mach_task_self (), new);
78       return __hurd_dfail (fd, err);
79     }
80
81   if (addr != NULL)
82     {
83       if (*addr_len > buflen)
84         *addr_len = buflen;
85
86       if (buf != (char *) addr)
87         {
88           memcpy (addr, buf, *addr_len);
89           __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
90         }
91
92       if (buflen > 0)
93         addr->sa_family = type;
94     }
95
96   return _hurd_intern_fd (new, O_IGNORE_CTTY | flags, 1);
97 }
98 libc_hidden_def (__libc_accept4)
99 weak_alias (__libc_accept4, accept4)