Back to index

glibc  2.9
bind.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,94,95,96,97,98,2001,02 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <sys/socket.h>
00021 #include <hurd.h>
00022 #include <hurd/fd.h>
00023 #include <hurd/socket.h>
00024 #include <hurd/paths.h>
00025 #include <fcntl.h>
00026 #include <stddef.h>
00027 #include <hurd/ifsock.h>
00028 #include <sys/un.h>
00029 #include <string.h>
00030 
00031 /* Give the socket FD the local address ADDR (which is LEN bytes long).  */
00032 int
00033 __bind  (int fd, __CONST_SOCKADDR_ARG addrarg, socklen_t len)
00034 {
00035   addr_port_t aport;
00036   error_t err;
00037   const struct sockaddr_un *addr = addrarg.__sockaddr_un__;
00038 
00039   if (addr->sun_family == AF_LOCAL)
00040     {
00041       /* For the local domain, we must create a node in the filesystem
00042         using the ifsock translator and then fetch the address from it.  */
00043       file_t dir, node;
00044       char name[len - offsetof (struct sockaddr_un, sun_path) + 1], *n;
00045 
00046       strncpy (name, addr->sun_path, sizeof name - 1);
00047       name[sizeof name - 1] = '\0'; /* Make sure */
00048 
00049       dir = __file_name_split (name, &n);
00050       if (dir == MACH_PORT_NULL)
00051        return -1;
00052 
00053       /* Create a new, unlinked node in the target directory.  */
00054       err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node);
00055 
00056       if (! err)
00057        {
00058          /* Set the node's translator to make it a local-domain socket.  */
00059          err = __file_set_translator (node,
00060                                    FS_TRANS_EXCL | FS_TRANS_SET,
00061                                    FS_TRANS_EXCL | FS_TRANS_SET, 0,
00062                                    _HURD_IFSOCK, sizeof _HURD_IFSOCK,
00063                                    MACH_PORT_NULL,
00064                                    MACH_MSG_TYPE_COPY_SEND);
00065          if (! err)
00066            {
00067              /* Link the node, now a socket, into the target directory.  */
00068              err = __dir_link (dir, node, n, 1);
00069              if (err == EEXIST)
00070               err = EADDRINUSE;
00071            }
00072          __mach_port_deallocate (__mach_task_self (), node);
00073          if (! err)
00074            {
00075              /* Get a port to the ifsock translator.  */
00076              file_t ifsock = __file_name_lookup_under (dir, n, 0, 0);
00077              if (ifsock == MACH_PORT_NULL)
00078               {
00079                 err = errno;
00080                 /* If we failed, get rid of the node we created.  */
00081                 __dir_unlink (dir, n);
00082               }
00083              else
00084               {
00085                 /* Get the address port.  */
00086                 err = __ifsock_getsockaddr (ifsock, &aport);
00087                 if (err == MIG_BAD_ID || err == EOPNOTSUPP)
00088                   /* We are not talking to /hurd/ifsock.  Probably
00089                      someone came in after we linked our node, unlinked
00090                      it, and replaced it with a different node, before we
00091                      did our lookup.  Treat it as if our link had failed
00092                      with EEXIST.  */
00093                   err = EADDRINUSE;
00094               }
00095              __mach_port_deallocate (__mach_task_self (), ifsock);
00096            }
00097        }
00098       __mach_port_deallocate (__mach_task_self (), dir);
00099 
00100       if (err)
00101        return __hurd_fail (err);
00102     }
00103   else
00104     err = EIEIO;
00105 
00106   err = HURD_DPORT_USE (fd,
00107                      ({
00108                        if (err)
00109                          err = __socket_create_address (port,
00110                                                     addr->sun_family,
00111                                                     (char *) addr, len,
00112                                                     &aport);
00113                        if (! err)
00114                          {
00115                            err = __socket_bind (port, aport);
00116                            __mach_port_deallocate (__mach_task_self (),
00117                                                 aport);
00118                          }
00119                        err;
00120                      }));
00121 
00122   return err ? __hurd_dfail (fd, err) : 0;
00123 }
00124 
00125 weak_alias (__bind, bind)