xref: /freebsd/lib/libsys/bind.2 (revision a91a246563dffa876a52f53a98de4af9fa364c52)
1.\" Copyright (c) 1983, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd March 30, 2020
29.Dt BIND 2
30.Os
31.Sh NAME
32.Nm bind
33.Nd assign a local protocol address to a socket
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In sys/socket.h
38.Ft int
39.Fn bind "int s" "const struct sockaddr *addr" "socklen_t addrlen"
40.Sh DESCRIPTION
41The
42.Fn bind
43system call
44assigns the local protocol address to a socket.
45When a socket is created
46with
47.Xr socket 2
48it exists in an address family space but has no protocol address assigned.
49The
50.Fn bind
51system call requests that
52.Fa addr
53be assigned to the socket.
54.Sh NOTES
55Binding an address in the UNIX domain creates a socket in the file
56system that must be deleted by the caller when it is no longer
57needed (using
58.Xr unlink 2 ) .
59.Pp
60The rules used in address binding vary between communication domains.
61Consult the manual entries in section 4 for detailed information.
62.Pp
63For maximum portability, you should always zero the socket address structure
64before populating it and passing it to
65.Fn bind .
66.Sh RETURN VALUES
67.Rv -std bind
68.Sh ERRORS
69The
70.Fn bind
71system call will fail if:
72.Bl -tag -width Er
73.It Bq Er EAGAIN
74Kernel resources to complete the request are
75temporarily unavailable.
76.It Bq Er EBADF
77The
78.Fa s
79argument
80is not a valid descriptor.
81.It Bq Er EINVAL
82The socket is already bound to an address, and the protocol does not support
83binding to a new address; or the socket has been shut down.
84.It Bq Er EINVAL
85The
86.Fa addrlen
87argument is not a valid length for the address family.
88.It Bq Er ENOTSOCK
89The
90.Fa s
91argument
92is not a socket.
93.It Bq Er EADDRNOTAVAIL
94The specified address is not available from the local machine.
95.It Bq Er EADDRINUSE
96The specified address is already in use.
97.It Bq Er EAFNOSUPPORT
98Addresses in the specified address family cannot be used with this socket.
99.It Bq Er EACCES
100The requested address is protected, and the current user
101has inadequate permission to access it.
102.It Bq Er EFAULT
103The
104.Fa addr
105argument is not in a valid part of the user
106address space.
107.El
108.Pp
109The following errors are specific to binding addresses in the UNIX domain.
110.Bl -tag -width EADDRNOTAVA
111.It Bq Er ENOTDIR
112A component of the path prefix is not a directory.
113.It Bq Er ENAMETOOLONG
114A component of a pathname exceeded 255 characters,
115or an entire path name exceeded 1023 characters.
116.It Bq Er ENOENT
117A prefix component of the path name does not exist.
118.It Bq Er ELOOP
119Too many symbolic links were encountered in translating the pathname.
120.It Bq Er EIO
121An I/O error occurred while making the directory entry or allocating the inode.
122.It Bq Er EINTEGRITY
123Corrupted data was detected while reading from the file system.
124.It Bq Er EROFS
125The name would reside on a read-only file system.
126.It Bq Er EISDIR
127An empty pathname was specified.
128.El
129.Sh SEE ALSO
130.Xr connect 2 ,
131.Xr getsockname 2 ,
132.Xr listen 2 ,
133.Xr socket 2
134.Sh HISTORY
135The
136.Fn bind
137system call appeared in
138.Bx 4.2 .
139