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

NAME

12       bind — bind a name to a socket
13

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

EXAMPLES

158       The following code segment shows how to create a socket and bind it  to
159       a name in the AF_UNIX domain.
160
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‐2017, <sys_socket.h>
194
196       Portions of this text are reprinted and reproduced in  electronic  form
197       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
198       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
199       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
200       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
201       event of any discrepancy between this version and the original IEEE and
202       The Open Group Standard, the original IEEE and The Open Group  Standard
203       is  the  referee document. The original Standard can be obtained online
204       at http://www.opengroup.org/unix/online.html .
205
206       Any typographical or formatting errors that appear  in  this  page  are
207       most likely to have been introduced during the conversion of the source
208       files to man page format. To report such errors,  see  https://www.ker
209       nel.org/doc/man-pages/reporting_bugs.html .
210
211
212
213IEEE/The Open Group                  2017                             BIND(3P)
Impressum