1BIND(3P)                   POSIX Programmer's Manual                  BIND(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       bind — bind a name to a socket
14

SYNOPSIS

16       #include <sys/socket.h>
17
18       int bind(int socket, const struct sockaddr *address,
19           socklen_t address_len);
20

DESCRIPTION

22       The bind() function shall assign a local socket address  address  to  a
23       socket identified by descriptor socket that has no local socket address
24       assigned. Sockets created with  the  socket()  function  are  initially
25       unnamed; they are identified only by their address family.
26
27       The bind() function takes the following arguments:
28
29       socket      Specifies the file descriptor of the socket to be bound.
30
31       address     Points to a sockaddr structure containing the address to be
32                   bound to the socket. The length and format of  the  address
33                   depend on the address family of the socket.
34
35       address_len Specifies  the  length of the sockaddr structure pointed to
36                   by the address argument.
37
38       The socket specified by socket may require the process to  have  appro‐
39       priate privileges to use the bind() function.
40
41       If  the  address  family  of  the socket is AF_UNIX and the pathname in
42       address names a symbolic link, bind() shall fail and set errno to [EAD‐
43       DRINUSE].
44
45       If  the socket address cannot be assigned immediately and O_NONBLOCK is
46       set for the file descriptor for the socket, bind() shall fail  and  set
47       errno  to  [EINPROGRESS],  but  the  assignment  request  shall  not be
48       aborted, and the assignment shall be completed  asynchronously.  Subse‐
49       quent  calls  to  bind()  for the same socket, before the assignment is
50       completed, shall fail and set errno to [EALREADY].
51
52       When the  assignment  has  been  performed  asynchronously,  pselect(),
53       select(),  and  poll()  shall indicate that the file descriptor for the
54       socket is ready for reading and writing.
55

RETURN VALUE

57       Upon successful completion, bind() shall return 0; otherwise, −1  shall
58       be returned and errno set to indicate the error.
59

ERRORS

61       The bind() function shall fail if:
62
63       EADDRINUSE
64              The specified address is already in use.
65
66       EADDRNOTAVAIL
67              The specified address is not available from the local machine.
68
69       EAFNOSUPPORT
70              The  specified  address  is  not a valid address for the address
71              family of the specified socket.
72
73       EALREADY
74              An assignment request is already in progress for  the  specified
75              socket.
76
77       EBADF  The socket argument is not a valid file descriptor.
78
79       EINPROGRESS
80              O_NONBLOCK is set for the file descriptor for the socket and the
81              assignment cannot be immediately performed; the assignment shall
82              be performed asynchronously.
83
84       EINVAL The socket is already bound to an address, and the protocol does
85              not support binding to a new address; or  the  socket  has  been
86              shut down.
87
88       ENOBUFS
89              Insufficient resources were available to complete the call.
90
91       ENOTSOCK
92              The socket argument does not refer to a socket.
93
94       EOPNOTSUPP
95              The socket type of the specified socket does not support binding
96              to an address.
97
98       If the address family of the socket is AF_UNIX, then bind() shall  fail
99       if:
100
101       EACCES A  component of the path prefix denies search permission, or the
102              requested name requires writing in a directory with a mode  that
103              denies write permission.
104
105       EDESTADDRREQ or EISDIR
106              The address argument is a null pointer.
107
108       EIO    An I/O error occurred.
109
110       ELOOP  A loop exists in symbolic links encountered during resolution of
111              the pathname in address.
112
113       ENAMETOOLONG
114              The  length  of  a  component  of  a  pathname  is  longer  than
115              {NAME_MAX}.
116
117       ENOENT A  component  of the path prefix of the pathname in address does
118              not name an existing file or the pathname is an empty string.
119
120       ENOENT or ENOTDIR
121              The pathname in address contains at least one non-<slash>  char‐
122              acter  and ends with one or more trailing <slash> characters. If
123              the pathname names an existing file, an [ENOENT] error shall not
124              occur.
125
126       ENOTDIR
127              A  component of the path prefix of the pathname in address names
128              an existing file that is neither a directory nor a symbolic link
129              to a directory, or the pathname in address contains at least one
130              non-<slash> character and ends with one or more trailing <slash>
131              characters  and  the  last  pathname component names an existing
132              file that is neither a directory nor a symbolic link to a direc‐
133              tory.
134
135       EROFS  The name would reside on a read-only file system.
136
137       The bind() function may fail if:
138
139       EACCES The specified address is protected and the current user does not
140              have permission to bind to it.
141
142       EINVAL The address_len argument is not a valid length for  the  address
143              family.
144
145       EISCONN
146              The socket is already connected.
147
148       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
149              resolution of the pathname in address.
150
151       ENAMETOOLONG
152              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
153              tion  of  a symbolic link produced an intermediate result with a
154              length that exceeds {PATH_MAX}.
155
156       The following sections are informative.
157

EXAMPLES

159       The following code segment shows how to create a socket and bind it  to
160       a name in the AF_UNIX domain.
161
162           #define MY_SOCK_PATH "/somepath"
163
164           int sfd;
165           struct sockaddr_un my_addr;
166
167           sfd = socket(AF_UNIX, SOCK_STREAM, 0);
168           if (sfd == −1)
169               /* Handle error */;
170
171           memset(&my_addr, '\0', sizeof(struct sockaddr_un));
172                                /* Clear structure */
173           my_addr.sun_family = AF_UNIX;
174           strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) −1);
175
176           if (bind(sfd, (struct sockaddr *) &my_addr,
177               sizeof(struct sockaddr_un)) == −1)
178               /* Handle error */;
179

APPLICATION USAGE

181       An  application  program can retrieve the assigned socket name with the
182       getsockname() function.
183

RATIONALE

185       None.
186

FUTURE DIRECTIONS

188       None.
189

SEE ALSO

191       connect(), getsockname(), listen(), socket()
192
193       The Base Definitions volume of POSIX.1‐2008, <sys_socket.h>
194
196       Portions of this text are reprinted and reproduced in  electronic  form
197       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
198       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
199       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
200       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
201       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
202       event of any discrepancy between this version and the original IEEE and
203       The  Open Group Standard, the original IEEE and The Open Group Standard
204       is the referee document. The original Standard can be  obtained  online
205       at http://www.unix.org/online.html .
206
207       Any  typographical  or  formatting  errors that appear in this page are
208       most likely to have been introduced during the conversion of the source
209       files  to  man page format. To report such errors, see https://www.ker
210       nel.org/doc/man-pages/reporting_bugs.html .
211
212
213
214IEEE/The Open Group                  2013                             BIND(3P)
Impressum