xref: /freebsd-14.2/lib/libc/sys/bind.2 (revision b2c76c41)
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.\"     @(#)bind.2	8.1 (Berkeley) 6/4/93
29.\"
30.Dd March 30, 2020
31.Dt BIND 2
32.Os
33.Sh NAME
34.Nm bind
35.Nd assign a local protocol address to a socket
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In sys/socket.h
40.Ft int
41.Fn bind "int s" "const struct sockaddr *addr" "socklen_t addrlen"
42.Sh DESCRIPTION
43The
44.Fn bind
45system call
46assigns the local protocol address to a socket.
47When a socket is created
48with
49.Xr socket 2
50it exists in an address family space but has no protocol address assigned.
51The
52.Fn bind
53system call requests that
54.Fa addr
55be assigned to the socket.
56.Sh NOTES
57Binding an address in the UNIX domain creates a socket in the file
58system that must be deleted by the caller when it is no longer
59needed (using
60.Xr unlink 2 ) .
61.Pp
62The rules used in address binding vary between communication domains.
63Consult the manual entries in section 4 for detailed information.
64.Pp
65For maximum portability, you should always zero the socket address structure
66before populating it and passing it to
67.Fn bind .
68.Sh RETURN VALUES
69.Rv -std bind
70.Sh ERRORS
71The
72.Fn bind
73system call will fail if:
74.Bl -tag -width Er
75.It Bq Er EAGAIN
76Kernel resources to complete the request are
77temporarily unavailable.
78.It Bq Er EBADF
79The
80.Fa s
81argument
82is not a valid descriptor.
83.It Bq Er EINVAL
84The socket is already bound to an address, and the protocol does not support
85binding to a new address; or the socket has been shut down.
86.It Bq Er EINVAL
87The
88.Fa addrlen
89argument is not a valid length for the address family.
90.It Bq Er ENOTSOCK
91The
92.Fa s
93argument
94is not a socket.
95.It Bq Er EADDRNOTAVAIL
96The specified address is not available from the local machine.
97.It Bq Er EADDRINUSE
98The specified address is already in use.
99.It Bq Er EAFNOSUPPORT
100Addresses in the specified address family cannot be used with this socket.
101.It Bq Er EACCES
102The requested address is protected, and the current user
103has inadequate permission to access it.
104.It Bq Er EFAULT
105The
106.Fa addr
107argument is not in a valid part of the user
108address space.
109.El
110.Pp
111The following errors are specific to binding addresses in the UNIX domain.
112.Bl -tag -width EADDRNOTAVA
113.It Bq Er ENOTDIR
114A component of the path prefix is not a directory.
115.It Bq Er ENAMETOOLONG
116A component of a pathname exceeded 255 characters,
117or an entire path name exceeded 1023 characters.
118.It Bq Er ENOENT
119A prefix component of the path name does not exist.
120.It Bq Er ELOOP
121Too many symbolic links were encountered in translating the pathname.
122.It Bq Er EIO
123An I/O error occurred while making the directory entry or allocating the inode.
124.It Bq Er EINTEGRITY
125Corrupted data was detected while reading from the file system.
126.It Bq Er EROFS
127The name would reside on a read-only file system.
128.It Bq Er EISDIR
129An empty pathname was specified.
130.El
131.Sh SEE ALSO
132.Xr connect 2 ,
133.Xr getsockname 2 ,
134.Xr listen 2 ,
135.Xr socket 2
136.Sh HISTORY
137The
138.Fn bind
139system call appeared in
140.Bx 4.2 .
141