Patches to improve Hurd parts: accept4
Thomas Schwinge
tschwinge@gnu.org
Wed Dec 17 17:16:00 GMT 2008
2008-12-17 Thomas Schwinge <tschwinge@gnu.org>
* sysdeps/mach/hurd/accept4.c: New file, copy from accept.c. Evolve it
to implement accept4.
* sysdeps/mach/hurd/accept.c (accept): Reimplement using accept4.
Recipe:
Copy sysdeps/mach/hurd/accept.c to sysdeps/mach/hurd/accept4.c.
Evolve the latter one to implement accept4:
--- sysdeps/mach/hurd/accept.c.O 2008-12-17 00:55:39.000000000 +0100
+++ sysdeps/mach/hurd/accept4.c 2008-12-17 02:02:42.000000000 +0100
@@ -26,14 +28,16 @@
/* Await a connection on socket FD.
When a connection arrives, open a new socket to communicate with it,
- set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
+ set *ADDRARG (which is *ADDR_LEN bytes long) to the address of the connecting
peer and *ADDR_LEN to the address's actual length, and return the
- new socket's descriptor, or -1 for errors. */
+ new socket's descriptor, or -1 for errors. The operation can be influenced
+ by the FLAGS parameter. */
int
-accept (fd, addrarg, addr_len)
- int fd;
- __SOCKADDR_ARG addrarg;
- socklen_t *addr_len;
+accept4 (fd, addrarg, addr_len, flags)
+ int fd;
+ __SOCKADDR_ARG addrarg;
+ socklen_t *addr_len;
+ int flags;
{
error_t err;
socket_t new;
@@ -43,6 +47,11 @@ accept (fd, addrarg, addr_len)
mach_msg_type_number_t buflen;
int type;
+ flags = sock_to_o_flags (flags);
+
+ if (flags & ~(O_CLOEXEC | O_NONBLOCK))
+ return __hurd_fail (EINVAL);
+
if (err = HURD_DPORT_USE (fd, __socket_accept (port, &new, &aport)))
return __hurd_dfail (fd, err);
@@ -61,6 +70,12 @@ accept (fd, addrarg, addr_len)
}
__mach_port_deallocate (__mach_task_self (), aport);
+ if (! err)
+ {
+ if (flags & O_NONBLOCK)
+ err = __io_set_some_openmodes (new, O_NONBLOCK);
+ }
+
if (err)
{
__mach_port_deallocate (__mach_task_self (), new);
@@ -82,6 +97,6 @@ accept (fd, addrarg, addr_len)
addr->sa_family = type;
}
- return _hurd_intern_fd (new, O_IGNORE_CTTY, 1);
+ return _hurd_intern_fd (new, O_IGNORE_CTTY | flags, 1);
}
-libc_hidden_def (accept)
+libc_hidden_def (accept4)
Reimplement accept using accept4:
diff --git a/sysdeps/mach/hurd/accept.c b/sysdeps/mach/hurd/accept.c
index 362c42c..58720c6 100644
--- a/sysdeps/mach/hurd/accept.c
+++ b/sysdeps/mach/hurd/accept.c
@@ -16,17 +18,11 @@
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
-#include <errno.h>
-#include <fcntl.h>
-#include <string.h>
#include <sys/socket.h>
-#include <hurd.h>
-#include <hurd/fd.h>
-#include <hurd/socket.h>
/* Await a connection on socket FD.
When a connection arrives, open a new socket to communicate with it,
- set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
+ set *ADDRARG (which is *ADDR_LEN bytes long) to the address of the connecting
peer and *ADDR_LEN to the address's actual length, and return the
new socket's descriptor, or -1 for errors. */
int
@@ -35,53 +31,6 @@ accept (fd, addrarg, addr_len)
__SOCKADDR_ARG addrarg;
socklen_t *addr_len;
{
- error_t err;
- socket_t new;
- addr_port_t aport;
- struct sockaddr *addr = addrarg.__sockaddr__;
- char *buf = (char *) addr;
- mach_msg_type_number_t buflen;
- int type;
-
- if (err = HURD_DPORT_USE (fd, __socket_accept (port, &new, &aport)))
- return __hurd_dfail (fd, err);
-
- if (addr != NULL)
- {
- buflen = *addr_len;
- err = __socket_whatis_address (aport, &type, &buf, &buflen);
- if (err == EOPNOTSUPP)
- /* If the protocol server can't tell us the address, just return a
- zero-length one. */
- {
- buf = (char *)addr;
- buflen = 0;
- err = 0;
- }
- }
- __mach_port_deallocate (__mach_task_self (), aport);
-
- if (err)
- {
- __mach_port_deallocate (__mach_task_self (), new);
- return __hurd_dfail (fd, err);
- }
-
- if (addr != NULL)
- {
- if (*addr_len > buflen)
- *addr_len = buflen;
-
- if (buf != (char *) addr)
- {
- memcpy (addr, buf, *addr_len);
- __vm_deallocate (__mach_task_self (), (vm_address_t) buf, buflen);
- }
-
- if (buflen > 0)
- addr->sa_family = type;
- }
-
- return _hurd_intern_fd (new, O_IGNORE_CTTY, 1);
+ return accept4 (fd, addrarg, addr_len, 0);
}
libc_hidden_def (accept)
To discuss, analogous to socket.c:__socket: do we need special err
massaging after the __io_set_some_openmodes call?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 191 bytes
Desc: Digital signature
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20081217/30ea8395/attachment.sig>
More information about the Libc-alpha
mailing list