1 /* Copyright (C) 1992, 94, 95, 96 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 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.
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.
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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
21 #include <sys/socket.h>
24 #include <hurd/socket.h>
25 #include <hurd/paths.h>
28 #include <hurd/ifsock.h>
32 /* Give the socket FD the local address ADDR (which is LEN bytes long). */
34 DEFUN(bind
, (fd
, addr
, len
),
35 int fd AND
const struct sockaddr_un
*addr AND
size_t len
)
40 if (addr
->sun_family
== AF_LOCAL
)
42 /* For the local domain, we must create a node in the filesystem
43 using the ifsock translator and then fetch the address from it. */
45 char name
[len
- offsetof (struct sockaddr_un
, sun_path
) + 1], *n
;
47 strncpy (name
, addr
->sun_path
, sizeof name
- 1);
48 name
[sizeof name
- 1] = '\0'; /* Make sure */
50 dir
= __file_name_split (name
, &n
);
51 if (dir
== MACH_PORT_NULL
)
54 /* Create a new, unlinked node in the target directory. */
55 err
= __dir_mkfile (dir
, O_CREAT
, 0666 & ~_hurd_umask
, &node
);
60 /* Set the node's translator to make it a local-domain socket. */
61 err
= __file_set_translator (node
,
62 FS_TRANS_EXCL
| FS_TRANS_SET
,
63 FS_TRANS_EXCL
| FS_TRANS_SET
, 0,
64 _HURD_IFSOCK
, sizeof _HURD_IFSOCK
,
66 MACH_MSG_TYPE_COPY_SEND
);
69 /* Link the node, now a socket, into the target directory. */
70 err
= __dir_link (dir
, node
, n
, 1);
74 __mach_port_deallocate (__mach_task_self (), node
);
77 /* Get a port to the ifsock translator. */
78 ifsock
= __file_name_lookup_under (dir
, n
, 0, 0);
79 if (ifsock
== MACH_PORT_NULL
)
82 /* If we failed, get rid of the node we created. */
83 __dir_unlink (dir
, n
);
88 /* Get the address port. */
89 err
= __ifsock_getsockaddr (ifsock
, &aport
);
90 if (err
== MIG_BAD_ID
|| err
== EOPNOTSUPP
)
91 /* We are not talking to /hurd/ifsock. Probably someone
92 came in after we linked our node, unlinked it, and
93 replaced it with a different node, before we did our
94 lookup. Treat it as if our link had failed with EEXIST. */
97 __mach_port_deallocate (__mach_task_self (), ifsock
);
99 __mach_port_deallocate (__mach_task_self (), dir
);
102 return __hurd_fail (err
);
107 err
= HURD_DPORT_USE (fd
,
110 err
= __socket_create_address (port
,
116 err
= __socket_bind (port
, aport
);
117 __mach_port_deallocate (__mach_task_self (),
123 return err
? __hurd_dfail (fd
, err
) : 0;