xref: /freebsd-14.2/lib/libc/sys/bind.2 (revision 4d889d1c)
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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)bind.2	8.1 (Berkeley) 6/4/93
33.\"
34.Dd June 4, 1993
35.Dt BIND 2
36.Os BSD 4.2
37.Sh NAME
38.Nm bind
39.Nd bind a name to a socket
40.Sh SYNOPSIS
41.Fd #include <sys/types.h>
42.Fd #include <sys/socket.h>
43.Ft int
44.Fn bind "int s" "const struct sockaddr *name" "int namelen"
45.Sh DESCRIPTION
46.Fn Bind
47assigns a name to an unnamed socket.
48When a socket is created
49with
50.Xr socket 2
51it exists in a name space (address family)
52but has no name assigned.
53.Fn Bind
54requests that
55.Fa name
56be assigned to the socket.
57.Sh NOTES
58Binding a name in the UNIX domain creates a socket in the file
59system that must be deleted by the caller when it is no longer
60needed (using
61.Xr unlink 2 ) .
62.Pp
63The rules used in name binding vary between communication domains.
64Consult the manual entries in section 4 for detailed information.
65.Sh IMPLEMENTATION NOTES
66.Pp
67In the non-threaded library
68.Fn bind
69is implemented as the
70.Va bind
71syscall.
72.Pp
73In the threaded library, the
74.Va bind
75syscall is assembled to
76.Fn _thread_sys_bind
77and
78.Fn bind
79is implemented as a function which locks
80.Va s
81for read and write, then calls
82.Fn _thread_sys_bind .
83Before returning,
84.Fn bind
85unlocks
86.Va s .
87.Sh RETURN VALUES
88If the bind is successful, a 0 value is returned.
89A return value of -1 indicates an error, which is
90further specified in the global
91.Va errno .
92.Sh ERRORS
93The
94.Fn bind
95call will fail if:
96.Bl -tag -width EADDRNOTAVA
97.It Bq Er EBADF
98.Fa S
99is not a valid descriptor.
100.It Bq Er ENOTSOCK
101.Fa S
102is not a socket.
103.It Bq Er EADDRNOTAVAIL
104The specified address is not available from the local machine.
105.It Bq Er EADDRINUSE
106The specified address is already in use.
107.It Bq Er EACCES
108The requested address is protected, and the current user
109has inadequate permission to access it.
110.It Bq Er EFAULT
111The
112.Fa name
113parameter is not in a valid part of the user
114address space.
115.El
116.Pp
117The following errors are specific to binding names in the UNIX domain.
118.Bl -tag -width EADDRNOTAVA
119.It Bq Er ENOTDIR
120A component of the path prefix is not a directory.
121.It Bq Er EINVAL
122The pathname contains a character with the high-order bit set.
123.It Bq Er ENAMETOOLONG
124A component of a pathname exceeded 255 characters,
125or an entire path name exceeded 1023 characters.
126.It Bq Er ENOENT
127A prefix component of the path name does not exist.
128.It Bq Er ELOOP
129Too many symbolic links were encountered in translating the pathname.
130.It Bq Er EIO
131An I/O error occurred while making the directory entry or allocating the inode.
132.It Bq Er EROFS
133The name would reside on a read-only file system.
134.It Bq Er EISDIR
135An empty pathname was specified.
136.El
137.Sh SEE ALSO
138.Xr connect 2 ,
139.Xr getsockname 2 ,
140.Xr listen 2 ,
141.Xr socket 2
142.Sh HISTORY
143The
144.Fn bind
145function call appeared in
146.Bx 4.2 .
147