Updated.
[jlayton/glibc.git] / sysdeps / mach / hurd / bind.c
1 /* Copyright (C) 1992, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
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    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
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>
30
31 /* Give the socket FD the local address ADDR (which is LEN bytes long).  */
32 int
33 bind (fd, addrarg, len)
34      int fd;
35      __CONST_SOCKADDR_ARG addrarg;
36      size_t len;
37 {
38   addr_port_t aport;
39   error_t err;
40   struct sockaddr_un *addr = addrarg.__sockaddr_un__;
41
42   if (addr->sun_family == AF_LOCAL)
43     {
44       /* For the local domain, we must create a node in the filesystem
45          using the ifsock translator and then fetch the address from it.  */
46       file_t dir, node;
47       char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n;
48
49       strncpy (name, addr->sun_path, sizeof name - 1);
50       name[sizeof name - 1] = '\0'; /* Make sure */
51
52       dir = __file_name_split (name, &n);
53       if (dir == MACH_PORT_NULL)
54         return -1;
55
56       /* Create a new, unlinked node in the target directory.  */
57       err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node);
58
59       if (! err)
60         {
61           file_t ifsock;
62           /* Set the node's translator to make it a local-domain socket.  */
63           err = __file_set_translator (node,
64                                        FS_TRANS_EXCL | FS_TRANS_SET,
65                                        FS_TRANS_EXCL | FS_TRANS_SET, 0,
66                                        _HURD_IFSOCK, sizeof _HURD_IFSOCK,
67                                        MACH_PORT_NULL,
68                                        MACH_MSG_TYPE_COPY_SEND);
69           if (! err)
70             {
71               /* Link the node, now a socket, into the target directory.  */
72               err = __dir_link (dir, node, n, 1);
73               if (err == EEXIST)
74                 err = EADDRINUSE;
75             }
76           __mach_port_deallocate (__mach_task_self (), node);
77           if (! err)
78             {
79               /* Get a port to the ifsock translator.  */
80               ifsock = __file_name_lookup_under (dir, n, 0, 0);
81               if (ifsock == MACH_PORT_NULL)
82                 {
83                   err = errno;
84                   /* If we failed, get rid of the node we created.  */
85                   __dir_unlink (dir, n);
86                 }
87             }
88           if (! err)
89             {
90               /* Get the address port.  */
91               err = __ifsock_getsockaddr (ifsock, &aport);
92               if (err == MIG_BAD_ID || err == EOPNOTSUPP)
93                 /* We are not talking to /hurd/ifsock.  Probably someone
94                    came in after we linked our node, unlinked it, and
95                    replaced it with a different node, before we did our
96                    lookup.  Treat it as if our link had failed with EEXIST.  */
97                 err = EADDRINUSE;
98             }
99           __mach_port_deallocate (__mach_task_self (), ifsock);
100         }
101       __mach_port_deallocate (__mach_task_self (), dir);
102
103       if (err)
104         return __hurd_fail (err);
105     }
106   else
107     err = EIEIO;
108
109   err = HURD_DPORT_USE (fd,
110                         ({
111                           if (err)
112                             err = __socket_create_address (port,
113                                                            addr->sun_family,
114                                                            (char *) addr, len,
115                                                            &aport);
116                           if (! err)
117                             {
118                               err = __socket_bind (port, aport);
119                               __mach_port_deallocate (__mach_task_self (),
120                                                       aport);
121                             }
122                           err;
123                         }));
124
125   return err ? __hurd_dfail (fd, err) : 0;
126 }