1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu)
2 .\" Portions extracted from /usr/include/sys/socket.h, which does not have
3 .\" any authorship information in it. It is probably available under the GPL.
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
28 .\" Other portions are from the 6.9 (Berkeley) 3/10/91 man page:
30 .\" Copyright (c) 1983 The Regents of the University of California.
31 .\" All rights reserved.
33 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
34 .\" Redistribution and use in source and binary forms, with or without
35 .\" modification, are permitted provided that the following conditions
37 .\" 1. Redistributions of source code must retain the above copyright
38 .\" notice, this list of conditions and the following disclaimer.
39 .\" 2. Redistributions in binary form must reproduce the above copyright
40 .\" notice, this list of conditions and the following disclaimer in the
41 .\" documentation and/or other materials provided with the distribution.
42 .\" 3. All advertising materials mentioning features or use of this software
43 .\" must display the following acknowledgement:
44 .\" This product includes software developed by the University of
45 .\" California, Berkeley and its contributors.
46 .\" 4. Neither the name of the University nor the names of its contributors
47 .\" may be used to endorse or promote products derived from this software
48 .\" without specific prior written permission.
50 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
64 .\" Modified 1998 by Andi Kleen
65 .\" $Id: bind.2,v 1.3 1999/04/23 19:56:07 freitag Exp $
66 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
68 .TH BIND 2 2007-12-28 "Linux" "Linux Programmer's Manual"
70 bind \- bind a name to a socket
73 .BR "#include <sys/types.h>" " /* See NOTES */"
74 .B #include <sys/socket.h>
76 .BI "int bind(int " sockfd ", const struct sockaddr *" addr ,
77 .BI " socklen_t " addrlen );
80 When a socket is created with
82 it exists in a name space (address family) but has no address assigned to it.
84 assigns the address specified by
86 to the socket referred to by the file descriptor
89 specifies the size, in bytes, of the address structure pointed to by
91 Traditionally, this operation is called \(lqassigning a name to a socket\(rq.
93 It is normally necessary to assign a local address using
97 socket may receive connections (see
100 The rules used in name binding vary between address families.
101 Consult the manual entries in Section 7 for detailed information.
131 The actual structure passed for the
133 argument will depend on the address family.
136 structure is defined as something like:
141 sa_family_t sa_family;
147 The only purpose of this structure is to cast the structure
150 in order to avoid compiler warnings.
153 On success, zero is returned.
154 On error, \-1 is returned, and
156 is set appropriately.
160 .\" e.g., privileged port in AF_INET domain
161 The address is protected, and the user is not the superuser.
164 The given address is already in use.
168 is not a valid descriptor.
171 The socket is already bound to an address.
172 .\" This may change in the future: see
173 .\" .I linux/unix/sock.c for details.
177 is a descriptor for a file, not a socket.
179 The following errors are specific to UNIX domain
184 Search permission is denied on a component of the path prefix.
186 .BR path_resolution (7).)
189 A nonexistent interface was requested or the requested
190 address was not local.
194 points outside the user's accessible address space.
199 is wrong, or the socket was not in the
204 Too many symbolic links were encountered in resolving
212 The file does not exist.
215 Insufficient kernel memory was available.
218 A component of the path prefix is not a directory.
221 The socket inode would reside on a read-only filesystem.
223 SVr4, 4.4BSD, POSIX.1-2001
225 first appeared in 4.2BSD).
226 .\" SVr4 documents an additional
228 .\" general error condition, and
233 .\" UNIX-domain error conditions.
235 POSIX.1-2001 does not require the inclusion of
237 and this header file is not required on Linux.
238 However, some historical (BSD) implementations required this header
239 file, and portable applications are probably wise to include it.
241 The third argument of
245 (and this is what 4.x BSD and libc4 and libc5 have).
246 Some POSIX confusion resulted in the present
252 The transparent proxy options are not described.
253 .\" FIXME What *are* transparent proxy options?
255 An example of the use of
257 with Internet domain sockets can be found in
260 The following example shows how to bind a stream socket in the UNIX
262 domain, and accept connections:
263 .\" listen.7 refers to this example.
264 .\" accept.7 refers to this example.
265 .\" unix.7 refers to this example.
268 #include <sys/socket.h>
274 #define MY_SOCK_PATH "/somepath"
275 #define LISTEN_BACKLOG 50
277 #define handle_error(msg) \\
278 do { perror(msg); exit(EXIT_FAILURE); } while (0)
281 main(int argc, char *argv[])
284 struct sockaddr_un my_addr, peer_addr;
285 socklen_t peer_addr_size;
287 sfd = socket(AF_UNIX, SOCK_STREAM, 0);
289 handle_error("socket");
291 memset(&my_addr, 0, sizeof(struct sockaddr_un));
292 /* Clear structure */
293 my_addr.sun_family = AF_UNIX;
294 strncpy(my_addr.sun_path, MY_SOCK_PATH,
295 sizeof(my_addr.sun_path) \- 1);
297 if (bind(sfd, (struct sockaddr *) &my_addr,
298 sizeof(struct sockaddr_un)) == \-1)
299 handle_error("bind");
301 if (listen(sfd, LISTEN_BACKLOG) == \-1)
302 handle_error("listen");
304 /* Now we can accept incoming connections one
305 at a time using accept(2) */
307 peer_addr_size = sizeof(struct sockaddr_un);
308 cfd = accept(sfd, (struct sockaddr *) &peer_addr,
311 handle_error("accept");
313 /* Code to deal with incoming connection(s)... */
315 /* When no longer required, the socket pathname, MY_SOCK_PATH
316 should be deleted using unlink(2) or remove(3) */
329 .BR path_resolution (7),
333 This page is part of release 3.64 of the Linux
336 A description of the project,
337 and information about reporting bugs,
339 \%http://www.kernel.org/doc/man\-pages/.