Update.
[glibc.git] / sysdeps / mach / hurd / bind.c
bloba42b78ac07c7a72041f07650860567b6e9547a30
1 /* Copyright (C) 1992,94,95,96,97,98,2001,02 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <hurd.h>
22 #include <hurd/fd.h>
23 #include <hurd/socket.h>
24 #include <hurd/paths.h>
25 #include <fcntl.h>
26 #include <stddef.h>
27 #include <hurd/ifsock.h>
28 #include <sys/un.h>
29 #include <string.h>
31 /* Give the socket FD the local address ADDR (which is LEN bytes long). */
32 int
33 __bind (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len)
35 addr_port_t aport;
36 error_t err;
37 const struct sockaddr_un *addr = addrarg.__sockaddr_un__;
39 if (addr->sun_family == AF_LOCAL)
41 /* For the local domain, we must create a node in the filesystem
42 using the ifsock translator and then fetch the address from it. */
43 file_t dir, node;
44 char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n;
46 strncpy (name, addr->sun_path, sizeof name - 1);
47 name[sizeof name - 1] = '\0'; /* Make sure */
49 dir = __file_name_split (name, &n);
50 if (dir == MACH_PORT_NULL)
51 return -1;
53 /* Create a new, unlinked node in the target directory. */
54 err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node);
56 if (! err)
58 /* Set the node's translator to make it a local-domain socket. */
59 err = __file_set_translator (node,
60 FS_TRANS_EXCL | FS_TRANS_SET,
61 FS_TRANS_EXCL | FS_TRANS_SET, 0,
62 _HURD_IFSOCK, sizeof _HURD_IFSOCK,
63 MACH_PORT_NULL,
64 MACH_MSG_TYPE_COPY_SEND);
65 if (! err)
67 /* Link the node, now a socket, into the target directory. */
68 err = __dir_link (dir, node, n, 1);
69 if (err == EEXIST)
70 err = EADDRINUSE;
72 __mach_port_deallocate (__mach_task_self (), node);
73 if (! err)
75 /* Get a port to the ifsock translator. */
76 file_t ifsock = __file_name_lookup_under (dir, n, 0, 0);
77 if (ifsock == MACH_PORT_NULL)
79 err = errno;
80 /* If we failed, get rid of the node we created. */
81 __dir_unlink (dir, n);
83 else
85 /* Get the address port. */
86 err = __ifsock_getsockaddr (ifsock, &aport);
87 if (err == MIG_BAD_ID || err == EOPNOTSUPP)
88 /* We are not talking to /hurd/ifsock. Probably
89 someone came in after we linked our node, unlinked
90 it, and replaced it with a different node, before we
91 did our lookup. Treat it as if our link had failed
92 with EEXIST. */
93 err = EADDRINUSE;
95 __mach_port_deallocate (__mach_task_self (), ifsock);
98 __mach_port_deallocate (__mach_task_self (), dir);
100 if (err)
101 return __hurd_fail (err);
103 else
104 err = EIEIO;
106 err = HURD_DPORT_USE (fd,
108 if (err)
109 err = __socket_create_address (port,
110 addr->sun_family,
111 (char *) addr, len,
112 &aport);
113 if (! err)
115 err = __socket_bind (port, aport);
116 __mach_port_deallocate (__mach_task_self (),
117 aport);
119 err;
120 }));
122 return err ? __hurd_dfail (fd, err) : 0;
125 weak_alias (__bind, bind)