]>
Commit | Line | Data |
---|---|---|
15545eb6 HS |
1 | .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>, |
2 | .\" Copyright (C) 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>, | |
3 | .\" and Copyright (C) 2016, Heinrich Schuchardt <xypron.glpk@gmx.de> | |
2297bf0e | 4 | .\" |
00acdba1 | 5 | .\" %%%LICENSE_START(VERBATIM_ONE_PARA) |
77117f4f MK |
6 | .\" Permission is granted to distribute possibly modified copies |
7 | .\" of this page provided the header is included verbatim, | |
8 | .\" and in case of nontrivial modification author and date | |
9 | .\" of the modification is added to the header. | |
8ff7380d | 10 | .\" %%%LICENSE_END |
77117f4f MK |
11 | .\" |
12 | .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com> | |
13 | .\" Modified, 2003-09-23, Adam Langley | |
14 | .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com> | |
15 | .\" Added SOCK_SEQPACKET | |
16 | .\" 2008-05-27, mtk, Provide a clear description of the three types of | |
17 | .\" address that can appear in the sockaddr_un structure: pathname, | |
18 | .\" unnamed, and abstract. | |
19 | .\" | |
31a1b45e | 20 | .TH UNIX 7 2017-03-13 "Linux" "Linux Programmer's Manual" |
77117f4f | 21 | .SH NAME |
f68512e9 | 22 | unix \- sockets for local interprocess communication |
77117f4f MK |
23 | .SH SYNOPSIS |
24 | .B #include <sys/socket.h> | |
25 | .br | |
26 | .B #include <sys/un.h> | |
f90f031e | 27 | .PP |
d4c8c97c | 28 | .IB unix_socket " = socket(AF_UNIX, type, 0);" |
77117f4f | 29 | .br |
d4c8c97c | 30 | .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");" |
77117f4f MK |
31 | .SH DESCRIPTION |
32 | The | |
d4c8c97c | 33 | .B AF_UNIX |
77117f4f | 34 | (also known as |
d4c8c97c | 35 | .BR AF_LOCAL ) |
77117f4f MK |
36 | socket family is used to communicate between processes on the same machine |
37 | efficiently. | |
4891f52a | 38 | Traditionally, UNIX domain sockets can be either unnamed, |
9ee4a2b6 | 39 | or bound to a filesystem pathname (marked as being of type socket). |
77117f4f | 40 | Linux also supports an abstract namespace which is independent of the |
9ee4a2b6 | 41 | filesystem. |
5711c04f | 42 | .PP |
d02879f7 | 43 | Valid socket types in the UNIX domain are: |
77117f4f | 44 | .BR SOCK_STREAM , |
d02879f7 | 45 | for a stream-oriented socket; |
77117f4f MK |
46 | .BR SOCK_DGRAM , |
47 | for a datagram-oriented socket that preserves message boundaries | |
008f1ecc | 48 | (as on most UNIX implementations, UNIX domain datagram |
77117f4f MK |
49 | sockets are always reliable and don't reorder datagrams); |
50 | and (since Linux 2.6.4) | |
51 | .BR SOCK_SEQPACKET , | |
0d7e8d59 MK |
52 | for a sequenced-packet socket that is connection-oriented, |
53 | preserves message boundaries, | |
77117f4f | 54 | and delivers messages in the order that they were sent. |
5711c04f | 55 | .PP |
4891f52a | 56 | UNIX domain sockets support passing file descriptors or process credentials |
77117f4f | 57 | to other processes using ancillary data. |
c634028a | 58 | .SS Address format |
008f1ecc | 59 | A UNIX domain socket address is represented in the following structure: |
77117f4f MK |
60 | .in +4n |
61 | .nf | |
62 | ||
63bc262c | 63 | .\" #define UNIX_PATH_MAX 108 |
5ffdc2fd | 64 | .\" |
77117f4f MK |
65 | struct sockaddr_un { |
66 | sa_family_t sun_family; /* AF_UNIX */ | |
63bc262c | 67 | char sun_path[108]; /* pathname */ |
77117f4f MK |
68 | }; |
69 | .fi | |
70 | .in | |
71 | .PP | |
d02879f7 | 72 | The |
77117f4f | 73 | .I sun_family |
d02879f7 | 74 | field always contains |
77117f4f | 75 | .BR AF_UNIX . |
840aa3c7 MK |
76 | On Linux |
77 | .I sun_path | |
78 | is 108 bytes in size; see also NOTES, below. | |
5711c04f | 79 | .PP |
d02879f7 MK |
80 | Various systems calls (for example, |
81 | .BR bind (2), | |
82 | .BR connect (2), | |
83 | and | |
84 | .BR sendto (2)) | |
85 | take a | |
b8017cf5 | 86 | .I sockaddr_un |
d02879f7 MK |
87 | argument as input. |
88 | Some other system calls (for example, | |
89 | .BR getsockname (2), | |
90 | .BR getpeername (2), | |
91 | .BR recvfrom (2), | |
92 | and | |
93 | .BR accept (2)) | |
94 | return an argument of this type. | |
5711c04f | 95 | .PP |
d02879f7 MK |
96 | Three types of address are distinguished in the |
97 | .I sockaddr_un | |
98 | structure: | |
77117f4f MK |
99 | .IP * 3 |
100 | .IR pathname : | |
1c7b2458 MK |
101 | a UNIX domain socket can be bound to a null-terminated |
102 | filesystem pathname using | |
77117f4f | 103 | .BR bind (2). |
d02879f7 MK |
104 | When the address of a pathname socket is returned |
105 | (by one of the system calls noted above), | |
77117f4f | 106 | its length is |
5711c04f | 107 | .IP |
6cd06646 | 108 | offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1 |
5711c04f | 109 | .IP |
77117f4f MK |
110 | and |
111 | .I sun_path | |
112 | contains the null-terminated pathname. | |
d02879f7 MK |
113 | (On Linux, the above |
114 | .BR offsetof () | |
115 | expression equates to the same value as | |
116 | .IR sizeof(sa_family_t) , | |
117 | but some other implementations include other fields before | |
118 | .IR sun_path , | |
119 | so the | |
120 | .BR offsetof () | |
121 | expression more portably describes the size of the address structure.) | |
122 | .IP | |
123 | For further details of pathname sockets, see below. | |
77117f4f MK |
124 | .IP * |
125 | .IR unnamed : | |
126 | A stream socket that has not been bound to a pathname using | |
127 | .BR bind (2) | |
128 | has no name. | |
129 | Likewise, the two sockets created by | |
130 | .BR socketpair (2) | |
131 | are unnamed. | |
d02879f7 | 132 | When the address of an unnamed socket is returned, |
77117f4f MK |
133 | its length is |
134 | .IR "sizeof(sa_family_t)" , | |
135 | and | |
136 | .I sun_path | |
137 | should not be inspected. | |
138 | .\" There is quite some variation across implementations: FreeBSD | |
139 | .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes. | |
140 | .IP * | |
141 | .IR abstract : | |
d02879f7 MK |
142 | an abstract socket address is distinguished (from a pathname socket) |
143 | by the fact that | |
77117f4f | 144 | .IR sun_path[0] |
836830b4 | 145 | is a null byte (\(aq\\0\(aq). |
156a0e0d MK |
146 | The socket's address in this namespace is given by the additional |
147 | bytes in | |
148 | .IR sun_path | |
149 | that are covered by the specified length of the address structure. | |
77117f4f | 150 | (Null bytes in the name have no special significance.) |
9ee4a2b6 | 151 | The name has no connection with filesystem pathnames. |
d02879f7 | 152 | When the address of an abstract socket is returned, |
156a0e0d MK |
153 | the returned |
154 | .I addrlen | |
155 | is greater than | |
156 | .IR "sizeof(sa_family_t)" | |
157 | (i.e., greater than 2), and the name of the socket is contained in | |
158 | the first | |
159 | .IR "(addrlen \- sizeof(sa_family_t))" | |
160 | bytes of | |
161 | .IR sun_path . | |
d02879f7 MK |
162 | .SS Pathname sockets |
163 | When binding a socket to a pathname, a few rules should be observed | |
164 | for maximum portability and ease of coding: | |
165 | .IP * 3 | |
166 | The pathname in | |
167 | .I sun_path | |
168 | should be null-terminated. | |
169 | .IP * | |
170 | The length of the pathname, including the terminating null byte, | |
171 | should not exceed the size of | |
172 | .IR sun_path . | |
173 | .IP * | |
174 | The | |
175 | .I addrlen | |
176 | argument that describes the enclosing | |
177 | .I sockaddr_un | |
178 | structure should have a value of at least: | |
5711c04f | 179 | .IP |
d02879f7 MK |
180 | .nf |
181 | offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1 | |
182 | .fi | |
183 | .IP | |
b8017cf5 | 184 | or, more simply, |
d02879f7 MK |
185 | .I addrlen |
186 | can be specified as | |
187 | .IR "sizeof(struct sockaddr_un)" . | |
188 | .PP | |
189 | There is some variation in how implementations handle UNIX domain | |
190 | socket addresses that do not follow the above rules. | |
191 | For example, some (but not all) implementations | |
192 | .\" Linux does this, including for the case where the supplied path | |
193 | .\" is 108 bytes | |
194 | append a null terminator if none is present in the supplied | |
195 | .IR sun_path . | |
5711c04f | 196 | .PP |
d02879f7 MK |
197 | When coding portable applications, |
198 | keep in mind that some implementations | |
199 | .\" HP-UX | |
200 | have | |
201 | .I sun_path | |
202 | as short as 92 bytes. | |
203 | .\" Modern BSDs generally have 104, Tru64 and AIX have 104, | |
204 | .\" Solaris and Irix have 108 | |
5711c04f | 205 | .PP |
d02879f7 MK |
206 | Various system calls |
207 | .RB ( accept (2), | |
208 | .BR recvfrom (2), | |
209 | .BR getsockname (2), | |
210 | .BR getpeername (2)) | |
211 | return socket address structures. | |
212 | When applied to UNIX domain sockets, the value-result | |
213 | .I addrlen | |
214 | argument supplied to the call should be initialized as above. | |
215 | Upon return, the argument is set to indicate the | |
216 | .I actual | |
217 | size of the address structure. | |
218 | The caller should check the value returned in this argument: | |
219 | if the output value exceeds the input value, | |
220 | then there is no guarantee that a null terminator is present in | |
221 | .IR sun_path . | |
222 | (See BUGS.) | |
9f213833 MK |
223 | .\" |
224 | .SS Pathname socket ownership and permissions | |
225 | In the Linux implementation, | |
226 | pathname sockets honor the permissions of the directory they are in. | |
227 | Creation of a new socket will fail if the process does not have write and | |
228 | search (execute) permission on the directory in which the socket is created. | |
5711c04f | 229 | .PP |
9f213833 MK |
230 | On Linux, |
231 | connecting to a stream socket object requires write permission on that socket; | |
232 | sending a datagram to a datagram socket likewise | |
233 | requires write permission on that socket. | |
234 | POSIX does not make any statement about the effect of the permissions | |
7f98a239 | 235 | on a socket file, and on some systems (e.g., older BSDs), |
9f213833 MK |
236 | the socket permissions are ignored. |
237 | Portable programs should not rely on | |
238 | this feature for security. | |
5711c04f | 239 | .PP |
9f213833 MK |
240 | When creating a new socket, the owner and group of the socket file |
241 | are set according to the usual rules. | |
242 | The socket file has all permissions enabled, | |
243 | other than those that are turned off by the process | |
244 | .BR umask (2). | |
5711c04f | 245 | .PP |
9f213833 MK |
246 | The owner, group, and permissions of a pathname socket can be changed (using |
247 | .BR chown (2) | |
248 | and | |
249 | .BR chmod (2)). | |
250 | .\" However, fchown() and fchmod() do not seem to have an effect | |
251 | .\" | |
d1875c13 | 252 | .SS Abstract sockets |
44cca454 MK |
253 | Socket permissions have no meaning for abstract sockets: |
254 | the process | |
255 | .BR umask (2) | |
256 | has no effect when binding an abstract socket, | |
257 | and changing the ownership and permissions of the object (via | |
258 | .BR fchown (2) | |
259 | and | |
260 | .BR fchmod (2)) | |
261 | has no effect on the accessibility of the socket. | |
5711c04f | 262 | .PP |
d1875c13 MK |
263 | Abstract sockets automatically disappear when all open references |
264 | to the socket are closed. | |
5711c04f | 265 | .PP |
d1875c13 MK |
266 | The abstract socket namespace is a nonportable Linux extension. |
267 | .\" | |
c634028a | 268 | .SS Socket options |
464b254b | 269 | For historical reasons, these socket options are specified with a |
77117f4f MK |
270 | .B SOL_SOCKET |
271 | type even though they are | |
d4c8c97c | 272 | .B AF_UNIX |
77117f4f MK |
273 | specific. |
274 | They can be set with | |
275 | .BR setsockopt (2) | |
276 | and read with | |
277 | .BR getsockopt (2) | |
278 | by specifying | |
279 | .B SOL_SOCKET | |
280 | as the socket family. | |
281 | .TP | |
282 | .B SO_PASSCRED | |
6074c3e6 | 283 | Enables the receiving of the credentials of the sending process in an |
77117f4f MK |
284 | ancillary message. |
285 | When this option is set and the socket is not yet connected | |
286 | a unique name in the abstract namespace will be generated automatically. | |
287 | Expects an integer boolean flag. | |
c634028a | 288 | .SS Autobind feature |
0cf2caa4 | 289 | If a |
0b80cf56 | 290 | .BR bind (2) |
0cf2caa4 MK |
291 | call specifies |
292 | .I addrlen | |
293 | as | |
294 | .IR sizeof(sa_family_t) , | |
449dd4e2 | 295 | .\" i.e., sizeof(short) |
0cf2caa4 MK |
296 | or the |
297 | .BR SO_PASSCRED | |
298 | socket option was specified for a socket that was | |
299 | not explicitly bound to an address, | |
300 | then the socket is autobound to an abstract address. | |
301 | The address consists of a null byte | |
302 | followed by 5 bytes in the character set | |
303 | .IR [0-9a-f] . | |
1e4e3bad MK |
304 | Thus, there is a limit of 2^20 autobind addresses. |
305 | (From Linux 2.1.15, when the autobind feature was added, | |
306 | 8 bytes were used, and the limit was thus 2^32 autobind addresses. | |
307 | The change to 5 bytes came in Linux 2.3.15.) | |
19e19f5f | 308 | .SS Sockets API |
77117f4f | 309 | The following paragraphs describe domain-specific details and |
008f1ecc | 310 | unsupported features of the sockets API for UNIX domain sockets on Linux. |
5711c04f | 311 | .PP |
008f1ecc | 312 | UNIX domain sockets do not support the transmission of |
77117f4f MK |
313 | out-of-band data (the |
314 | .B MSG_OOB | |
315 | flag for | |
316 | .BR send (2) | |
317 | and | |
318 | .BR recv (2)). | |
5711c04f | 319 | .PP |
77117f4f MK |
320 | The |
321 | .BR send (2) | |
322 | .B MSG_MORE | |
008f1ecc | 323 | flag is not supported by UNIX domain sockets. |
5711c04f | 324 | .PP |
c9a39fea MK |
325 | Before Linux 3.4, |
326 | .\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f | |
327 | the use of | |
77e75b90 MK |
328 | .B MSG_TRUNC |
329 | in the | |
330 | .I flags | |
331 | argument of | |
332 | .BR recv (2) | |
c9a39fea | 333 | was not supported by UNIX domain sockets. |
5711c04f | 334 | .PP |
77117f4f MK |
335 | The |
336 | .B SO_SNDBUF | |
008f1ecc | 337 | socket option does have an effect for UNIX domain sockets, but the |
77117f4f MK |
338 | .B SO_RCVBUF |
339 | option does not. | |
340 | For datagram sockets, the | |
341 | .B SO_SNDBUF | |
342 | value imposes an upper limit on the size of outgoing datagrams. | |
343 | This limit is calculated as the doubled (see | |
344 | .BR socket (7)) | |
345 | option value less 32 bytes used for overhead. | |
c634028a | 346 | .SS Ancillary messages |
77117f4f MK |
347 | Ancillary data is sent and received using |
348 | .BR sendmsg (2) | |
349 | and | |
350 | .BR recvmsg (2). | |
351 | For historical reasons the ancillary message types listed below | |
352 | are specified with a | |
353 | .B SOL_SOCKET | |
354 | type even though they are | |
d4c8c97c | 355 | .B AF_UNIX |
77117f4f MK |
356 | specific. |
357 | To send them set the | |
358 | .I cmsg_level | |
359 | field of the struct | |
360 | .I cmsghdr | |
361 | to | |
362 | .B SOL_SOCKET | |
363 | and the | |
364 | .I cmsg_type | |
365 | field to the type. | |
366 | For more information see | |
367 | .BR cmsg (3). | |
368 | .TP | |
369 | .B SCM_RIGHTS | |
370 | Send or receive a set of open file descriptors from another process. | |
371 | The data portion contains an integer array of the file descriptors. | |
372 | The passed file descriptors behave as though they have been created with | |
373 | .BR dup (2). | |
374 | .TP | |
375 | .B SCM_CREDENTIALS | |
008f1ecc | 376 | Send or receive UNIX credentials. |
77117f4f MK |
377 | This can be used for authentication. |
378 | The credentials are passed as a | |
379 | .I struct ucred | |
380 | ancillary message. | |
b1587ca8 MK |
381 | Thus structure is defined in |
382 | .I <sys/socket.h> | |
383 | as follows: | |
5711c04f | 384 | .IP |
77117f4f MK |
385 | .in +4n |
386 | .nf | |
387 | struct ucred { | |
388 | pid_t pid; /* process ID of the sending process */ | |
389 | uid_t uid; /* user ID of the sending process */ | |
390 | gid_t gid; /* group ID of the sending process */ | |
391 | }; | |
392 | .fi | |
393 | .in | |
5711c04f | 394 | .IP |
b1587ca8 | 395 | Since glibc 2.8, the |
1bc510f5 | 396 | .B _GNU_SOURCE |
e417acb0 MK |
397 | feature test macro must be defined (before including |
398 | .I any | |
399 | header files) in order to obtain the definition | |
b1587ca8 | 400 | of this structure. |
5711c04f | 401 | .IP |
77117f4f MK |
402 | The credentials which the sender specifies are checked by the kernel. |
403 | A process with effective user ID 0 is allowed to specify values that do | |
404 | not match its own. | |
405 | The sender must specify its own process ID (unless it has the capability | |
406 | .BR CAP_SYS_ADMIN ), | |
06b8a13b | 407 | its real user ID, effective user ID, or saved set-user-ID (unless it has |
77117f4f | 408 | .BR CAP_SETUID ), |
06b8a13b | 409 | and its real group ID, effective group ID, or saved set-group-ID |
77117f4f MK |
410 | (unless it has |
411 | .BR CAP_SETGID ). | |
412 | To receive a | |
413 | .I struct ucred | |
414 | message the | |
415 | .B SO_PASSCRED | |
416 | option must be enabled on the socket. | |
fbea0f81 MK |
417 | .SS Ioctls |
418 | The following | |
419 | .BR ioctl (2) | |
420 | calls return information in | |
421 | .IR value . | |
422 | The correct syntax is: | |
423 | .PP | |
424 | .RS | |
425 | .nf | |
426 | .BI int " value"; | |
f0d77d97 | 427 | .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");" |
fbea0f81 MK |
428 | .fi |
429 | .RE | |
430 | .PP | |
431 | .I ioctl_type | |
432 | can be: | |
433 | .TP | |
434 | .B SIOCINQ | |
170e5f0d JC |
435 | For |
436 | .B SOCK_STREAM | |
437 | socket the function returns the amount of queued unread data in the receive buffer. | |
fbea0f81 MK |
438 | The socket must not be in LISTEN state, otherwise an error |
439 | .RB ( EINVAL ) | |
440 | is returned. | |
441 | .B SIOCINQ | |
442 | is defined in | |
443 | .IR <linux/sockios.h> . | |
bea08fec | 444 | .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002, |
fbea0f81 MK |
445 | .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers |
446 | Alternatively, | |
447 | you can use the synonymous | |
448 | .BR FIONREAD , | |
449 | defined in | |
450 | .IR <sys/ioctl.h> . | |
7aed61d9 | 451 | .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not |
fbea0f81 MK |
452 | .\" quite what userland might expect. It seems to return the number |
453 | .\" of bytes allocated for buffers containing pending output. | |
454 | .\" That number is normally larger than the number of bytes of pending | |
455 | .\" output. Since this info is, from userland's point of view, imprecise, | |
456 | .\" and it may well change, probably best not to document this now. | |
170e5f0d JC |
457 | For |
458 | .B SOCK_DGRAM | |
459 | socket, | |
460 | the returned value is the same as | |
461 | for Internet domain datagram socket; | |
462 | see | |
463 | .BR udp (7). | |
77117f4f MK |
464 | .SH ERRORS |
465 | .TP | |
466 | .B EADDRINUSE | |
9ee4a2b6 | 467 | The specified local address is already in use or the filesystem socket |
77117f4f MK |
468 | object already exists. |
469 | .TP | |
470 | .B ECONNREFUSED | |
1fe284ab | 471 | The remote address specified by |
77117f4f | 472 | .BR connect (2) |
1fe284ab | 473 | was not a listening socket. |
d02879f7 | 474 | This error can also occur if the target pathname is not a socket. |
77117f4f MK |
475 | .TP |
476 | .B ECONNRESET | |
477 | Remote socket was unexpectedly closed. | |
478 | .TP | |
479 | .B EFAULT | |
480 | User memory address was not valid. | |
481 | .TP | |
482 | .B EINVAL | |
483 | Invalid argument passed. | |
1fe284ab | 484 | A common cause is that the value |
40656bc7 | 485 | .B AF_UNIX |
1fe284ab | 486 | was not specified in the |
77117f4f | 487 | .I sun_type |
1fe284ab | 488 | field of passed addresses, or the socket was in an |
77117f4f MK |
489 | invalid state for the applied operation. |
490 | .TP | |
491 | .B EISCONN | |
492 | .BR connect (2) | |
493 | called on an already connected socket or a target address was | |
494 | specified on a connected socket. | |
495 | .TP | |
ec55a2b6 MK |
496 | .B ENOENT |
497 | The pathname in the remote address specified to | |
9470f355 | 498 | .BR connect (2) |
ec55a2b6 MK |
499 | did not exist. |
500 | .TP | |
77117f4f MK |
501 | .B ENOMEM |
502 | Out of memory. | |
503 | .TP | |
504 | .B ENOTCONN | |
505 | Socket operation needs a target address, but the socket is not connected. | |
506 | .TP | |
507 | .B EOPNOTSUPP | |
508 | Stream operation called on non-stream oriented socket or tried to | |
509 | use the out-of-band data option. | |
510 | .TP | |
511 | .B EPERM | |
512 | The sender passed invalid credentials in the | |
513 | .IR "struct ucred" . | |
514 | .TP | |
515 | .B EPIPE | |
516 | Remote socket was closed on a stream socket. | |
517 | If enabled, a | |
518 | .B SIGPIPE | |
519 | is sent as well. | |
520 | This can be avoided by passing the | |
521 | .B MSG_NOSIGNAL | |
522 | flag to | |
110039c1 | 523 | .BR send (2) |
77117f4f | 524 | or |
110039c1 | 525 | .BR sendmsg (2). |
77117f4f MK |
526 | .TP |
527 | .B EPROTONOSUPPORT | |
cd0221ea MK |
528 | Passed protocol is not |
529 | .BR AF_UNIX . | |
77117f4f MK |
530 | .TP |
531 | .B EPROTOTYPE | |
532 | Remote socket does not match the local socket type | |
533 | .RB ( SOCK_DGRAM | |
d1c9ea80 | 534 | versus |
9ca2e0c1 | 535 | .BR SOCK_STREAM ). |
77117f4f MK |
536 | .TP |
537 | .B ESOCKTNOSUPPORT | |
538 | Unknown socket type. | |
dc4eea68 MK |
539 | .TP |
540 | .B ETOOMANYREFS | |
541 | This error can occur for | |
542 | .BR sendmsg (2) | |
4529d4e5 | 543 | when sending a file descriptor as ancillary data over |
dc4eea68 MK |
544 | a UNIX domain socket (see the description of |
545 | .BR SCM_RIGHTS , | |
546 | above). | |
547 | It occurs if the number of "in-flight" file descriptors exceeds the | |
548 | .B RLIMIT_NOFILE | |
549 | resource limit and the caller does not have the | |
550 | .BR CAP_SYS_RESOURCE | |
551 | capability. | |
552 | An in-flight file descriptor is one that has been sent using | |
553 | .BR sendmsg (2) | |
554 | but has not yet been accepted in the recipient process using | |
555 | .BR recvmsg (2). | |
5711c04f | 556 | .IP |
70fdcbc2 MK |
557 | This error is diagnosed since mainline Linux 4.5 |
558 | (and in some earlier kernel versions where the fix has been backported). | |
dc4eea68 MK |
559 | .\" commit 712f4aad406bb1ed67f3f98d04c044191f0ff593 |
560 | In earlier kernel versions, | |
561 | it was possible to place an unlimited number of file descriptors in flight, | |
562 | by sending each file descriptor with | |
563 | .BR sendmsg (2) | |
564 | and then closing the file descriptor so that it was not accounted against the | |
565 | .B RLIMIT_NOFILE | |
566 | resource limit. | |
77117f4f MK |
567 | .PP |
568 | Other errors can be generated by the generic socket layer or | |
9ee4a2b6 | 569 | by the filesystem while generating a filesystem socket object. |
77117f4f MK |
570 | See the appropriate manual pages for more information. |
571 | .SH VERSIONS | |
572 | .B SCM_CREDENTIALS | |
573 | and the abstract namespace were introduced with Linux 2.2 and should not | |
574 | be used in portable programs. | |
575 | (Some BSD-derived systems also support credential passing, | |
576 | but the implementation details differ.) | |
577 | .SH NOTES | |
00b78c5f MK |
578 | Binding to a socket with a filename creates a socket |
579 | in the filesystem that must be deleted by the caller when it is no | |
580 | longer needed (using | |
581 | .BR unlink (2)). | |
582 | The usual UNIX close-behind semantics apply; the socket can be unlinked | |
583 | at any time and will be finally removed from the filesystem when the last | |
584 | reference to it is closed. | |
5711c04f | 585 | .PP |
00b78c5f MK |
586 | To pass file descriptors or credentials over a |
587 | .BR SOCK_STREAM , | |
588 | you need | |
589 | to send or receive at least one byte of nonancillary data in the same | |
590 | .BR sendmsg (2) | |
591 | or | |
592 | .BR recvmsg (2) | |
593 | call. | |
5711c04f | 594 | .PP |
00b78c5f MK |
595 | UNIX domain stream sockets do not support the notion of out-of-band data. |
596 | .\" | |
d02879f7 MK |
597 | .SH BUGS |
598 | When binding a socket to an address, | |
599 | Linux is one of the implementations that appends a null terminator | |
600 | if none is supplied in | |
601 | .IR sun_path . | |
602 | In most cases this is unproblematic: | |
603 | when the socket address is retrieved, | |
604 | it will be one byte longer than that supplied when the socket was bound. | |
605 | However, there is one case where confusing behavior can result: | |
606 | if 108 non-null bytes are supplied when a socket is bound, | |
607 | then the addition of the null terminator takes the length of | |
608 | the pathname beyond | |
609 | .IR sizeof(sun_path) . | |
610 | Consequently, when retrieving the socket address | |
611 | (for example, via | |
612 | .BR accept (2)), | |
613 | .\" The behavior on Solaris is quite similar. | |
614 | if the input | |
615 | .I addrlen | |
616 | argument for the retrieving call is specified as | |
617 | .IR "sizeof(struct sockaddr_un)" , | |
618 | then the returned address structure | |
619 | .I won't | |
620 | have a null terminator in | |
621 | .IR sun_path . | |
5711c04f | 622 | .PP |
d02879f7 MK |
623 | In addition, some implementations |
624 | .\" i.e., traditional BSD | |
625 | don't require a null terminator when binding a socket (the | |
626 | .I addrlen | |
627 | argument is used to determine the length of | |
628 | .IR sun_path ) | |
629 | and when the socket address is retrieved on these implementations, | |
630 | there is no null terminator in | |
631 | .IR sun_path . | |
5711c04f | 632 | .PP |
b8017cf5 | 633 | Applications that retrieve socket addresses can (portably) code |
d02879f7 MK |
634 | to handle the possibility that there is no null terminator in |
635 | .IR sun_path | |
636 | by respecting the fact that the number of valid bytes in the pathname is: | |
5711c04f | 637 | .PP |
d02879f7 MK |
638 | strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path)) |
639 | .\" The following patch to amend kernel behavior was rejected: | |
640 | .\" http://thread.gmane.org/gmane.linux.kernel.api/2437 | |
641 | .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path | |
642 | .\" 2012-04-17 | |
643 | .\" And there was a related discussion in the Austin list: | |
644 | .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735 | |
645 | .\" Subject: Having a sun_path with no null terminator | |
646 | .\" 2012-04-18 | |
647 | .\" | |
648 | .\" FIXME . Track http://austingroupbugs.net/view.php?id=561 | |
5711c04f | 649 | .PP |
d02879f7 MK |
650 | Alternatively, an application can retrieve |
651 | the socket address by allocating a buffer of size | |
652 | .I "sizeof(struct sockaddr_un)+1" | |
653 | that is zeroed out before the retrieval. | |
654 | The retrieving call can specify | |
655 | .I addrlen | |
656 | as | |
657 | .IR "sizeof(struct sockaddr_un)" , | |
658 | and the extra zero byte ensures that there will be | |
659 | a null terminator for the string returned in | |
660 | .IR sun_path : | |
5711c04f | 661 | .PP |
d02879f7 MK |
662 | .nf |
663 | .in +3 | |
664 | void *addrp; | |
665 | ||
666 | addrlen = sizeof(struct sockaddr_un); | |
667 | addrp = malloc(addrlen + 1); | |
668 | if (addrp == NULL) | |
669 | /* Handle error */ ; | |
670 | memset(addrp, 0, addrlen + 1); | |
671 | ||
3e35b19b | 672 | if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1) |
d02879f7 MK |
673 | /* handle error */ ; |
674 | ||
675 | printf("sun_path = %s\\n", ((struct sockaddr_un *) addrp)\->sun_path); | |
676 | .in | |
677 | .fi | |
5711c04f | 678 | .PP |
d02879f7 MK |
679 | This sort of messiness can be avoided if it is guaranteed |
680 | that the applications that | |
681 | .I create | |
682 | pathname sockets follow the rules outlined above under | |
683 | .IR "Pathname sockets" . | |
77117f4f | 684 | .SH EXAMPLE |
84c8cae2 MK |
685 | The following code demonstrates the use of sequenced-packet |
686 | sockets for local interprocess communication. | |
eb73e8ad | 687 | It consists of two programs. |
15545eb6 | 688 | The server program waits for a connection from the client program. |
84c8cae2 MK |
689 | The client sends each of its command-line arguments in separate messages. |
690 | The server treats the incoming messages as integers and adds them up. | |
eb73e8ad | 691 | The client sends the command string "END". |
84c8cae2 MK |
692 | The server sends back a message containing the sum of the client's integers. |
693 | The client prints the sum and exits. | |
15545eb6 | 694 | The server waits for the next client to connect. |
84c8cae2 | 695 | To stop the server, the client is called with the command-line argument "DOWN". |
15545eb6 HS |
696 | .PP |
697 | The following output was recorded while running the server in the background | |
84c8cae2 MK |
698 | and repeatedly executing the client. |
699 | Execution of the server program ends when it receives the "DOWN" command. | |
15545eb6 HS |
700 | .SS Example output |
701 | .in +4n | |
702 | .nf | |
eb73e8ad | 703 | $ \fB./server &\fP |
15545eb6 | 704 | [1] 25887 |
eb73e8ad | 705 | $ \fB./client 3 4\fP |
15545eb6 | 706 | Result = 7 |
eb73e8ad | 707 | $ \fB./client 11 \-5\fP |
15545eb6 | 708 | Result = 6 |
eb73e8ad | 709 | $ \fB./client DOWN\fP |
15545eb6 HS |
710 | Result = 0 |
711 | [1]+ Done ./server | |
712 | $ | |
713 | .fi | |
714 | .in | |
715 | .SS Program source | |
e7d0bb47 | 716 | .EX |
15545eb6 HS |
717 | /* |
718 | * File connection.h | |
719 | */ | |
720 | ||
721 | #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket" | |
722 | #define BUFFER_SIZE 12 | |
723 | ||
724 | /* | |
725 | * File server.c | |
726 | */ | |
727 | ||
728 | #include <stdio.h> | |
729 | #include <stdlib.h> | |
730 | #include <string.h> | |
731 | #include <sys/socket.h> | |
732 | #include <sys/un.h> | |
733 | #include <unistd.h> | |
734 | #include "connection.h" | |
735 | ||
736 | int | |
737 | main(int argc, char *argv[]) | |
738 | { | |
739 | struct sockaddr_un name; | |
740 | int down_flag = 0; | |
741 | int ret; | |
742 | int connection_socket; | |
743 | int data_socket; | |
744 | int result; | |
745 | char buffer[BUFFER_SIZE]; | |
746 | ||
747 | /* | |
eb73e8ad | 748 | * In case the program exited inadvertently on the last run, |
15545eb6 HS |
749 | * remove the socket. |
750 | */ | |
751 | ||
752 | unlink(SOCKET_NAME); | |
753 | ||
754 | /* Create local socket. */ | |
755 | ||
756 | connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); | |
757 | if (connection_socket == \-1) { | |
758 | perror("socket"); | |
759 | exit(EXIT_FAILURE); | |
760 | } | |
761 | ||
762 | /* | |
eb73e8ad MK |
763 | * For portability clear the whole structure, since some |
764 | * implementations have additional (nonstandard) fields in | |
765 | * the structure. | |
15545eb6 HS |
766 | */ |
767 | ||
768 | memset(&name, 0, sizeof(struct sockaddr_un)); | |
769 | ||
770 | /* Bind socket to socket name. */ | |
771 | ||
772 | name.sun_family = AF_UNIX; | |
773 | strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1); | |
774 | ||
775 | ret = bind(connection_socket, (const struct sockaddr *) &name, | |
776 | sizeof(struct sockaddr_un)); | |
777 | if (ret == \-1) { | |
778 | perror("bind"); | |
779 | exit(EXIT_FAILURE); | |
780 | } | |
781 | ||
782 | /* | |
eb73e8ad MK |
783 | * Prepare for accepting connections. The backlog size is set |
784 | * to 20. So while one request is being processed other requests | |
785 | * can be waiting. | |
15545eb6 HS |
786 | */ |
787 | ||
788 | ret = listen(connection_socket, 20); | |
789 | if (ret == \-1) { | |
790 | perror("listen"); | |
791 | exit(EXIT_FAILURE); | |
792 | } | |
793 | ||
794 | /* This is the main loop for handling connections. */ | |
795 | ||
796 | for (;;) { | |
797 | ||
15545eb6 HS |
798 | /* Wait for incoming connection. */ |
799 | ||
800 | data_socket = accept(connection_socket, NULL, NULL); | |
3cb43b95 | 801 | if (data_socket == \-1) { |
15545eb6 HS |
802 | perror("accept"); |
803 | exit(EXIT_FAILURE); | |
804 | } | |
805 | ||
806 | result = 0; | |
807 | for(;;) { | |
808 | ||
809 | /* Wait for next data packet. */ | |
810 | ||
811 | ret = read(data_socket, buffer, BUFFER_SIZE); | |
812 | if (ret == \-1) { | |
eb73e8ad | 813 | perror("read"); |
15545eb6 HS |
814 | exit(EXIT_FAILURE); |
815 | } | |
816 | ||
817 | /* Ensure buffer is 0\-terminated. */ | |
818 | ||
819 | buffer[BUFFER_SIZE \- 1] = 0; | |
820 | ||
821 | /* Handle commands. */ | |
822 | ||
823 | if (!strncmp(buffer, "DOWN", BUFFER_SIZE)) { | |
824 | down_flag = 1; | |
825 | break; | |
826 | } | |
827 | ||
828 | if (!strncmp(buffer, "END", BUFFER_SIZE)) { | |
829 | break; | |
830 | } | |
831 | ||
832 | /* Add received summand. */ | |
833 | ||
834 | result += atoi(buffer); | |
835 | } | |
c751683c | 836 | |
15545eb6 HS |
837 | /* Send result. */ |
838 | ||
839 | sprintf(buffer, "%d", result); | |
840 | ret = write(data_socket, buffer, BUFFER_SIZE); | |
841 | ||
842 | if (ret == \-1) { | |
eb73e8ad | 843 | perror("write"); |
15545eb6 HS |
844 | exit(EXIT_FAILURE); |
845 | } | |
846 | ||
847 | /* Close socket. */ | |
848 | ||
849 | close(data_socket); | |
850 | ||
851 | /* Quit on DOWN command. */ | |
852 | ||
853 | if (down_flag) { | |
854 | break; | |
855 | } | |
856 | } | |
857 | ||
858 | close(connection_socket); | |
859 | ||
860 | /* Unlink the socket. */ | |
861 | ||
862 | unlink(SOCKET_NAME); | |
863 | ||
864 | exit(EXIT_SUCCESS); | |
865 | } | |
866 | ||
867 | /* | |
868 | * File client.c | |
869 | */ | |
870 | ||
871 | #include <errno.h> | |
872 | #include <stdio.h> | |
873 | #include <stdlib.h> | |
874 | #include <string.h> | |
875 | #include <sys/socket.h> | |
876 | #include <sys/un.h> | |
877 | #include <unistd.h> | |
878 | #include "connection.h" | |
879 | ||
880 | int | |
881 | main(int argc, char *argv[]) | |
882 | { | |
24a31d63 | 883 | struct sockaddr_un addr; |
15545eb6 HS |
884 | int i; |
885 | int ret; | |
886 | int data_socket; | |
887 | char buffer[BUFFER_SIZE]; | |
888 | ||
889 | /* Create local socket. */ | |
890 | ||
891 | data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); | |
892 | if (data_socket == \-1) { | |
893 | perror("socket"); | |
894 | exit(EXIT_FAILURE); | |
895 | } | |
896 | ||
897 | /* | |
eb73e8ad MK |
898 | * For portability clear the whole structure, since some |
899 | * implementations have additional (nonstandard) fields in | |
900 | * the structure. | |
15545eb6 HS |
901 | */ |
902 | ||
24a31d63 | 903 | memset(&addr, 0, sizeof(struct sockaddr_un)); |
15545eb6 | 904 | |
24a31d63 | 905 | /* Connect socket to socket address */ |
15545eb6 | 906 | |
24a31d63 MK |
907 | addr.sun_family = AF_UNIX; |
908 | strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1); | |
15545eb6 | 909 | |
24a31d63 | 910 | ret = connect (data_socket, (const struct sockaddr *) &addr, |
15545eb6 HS |
911 | sizeof(struct sockaddr_un)); |
912 | if (ret == \-1) { | |
913 | fprintf(stderr, "The server is down.\\n"); | |
914 | exit(EXIT_FAILURE); | |
915 | } | |
916 | ||
917 | /* Send arguments. */ | |
918 | ||
919 | for (i = 1; i < argc; ++i) { | |
920 | ret = write(data_socket, argv[i], strlen(argv[i]) + 1); | |
921 | if (ret == \-1) { | |
eb73e8ad | 922 | perror("write"); |
15545eb6 HS |
923 | break; |
924 | } | |
925 | } | |
926 | ||
927 | /* Request result. */ | |
928 | ||
929 | strcpy (buffer, "END"); | |
930 | ret = write(data_socket, buffer, strlen(buffer) + 1); | |
931 | if (ret == \-1) { | |
eb73e8ad | 932 | perror("write"); |
15545eb6 HS |
933 | exit(EXIT_FAILURE); |
934 | } | |
935 | ||
15545eb6 HS |
936 | /* Receive result. */ |
937 | ||
938 | ret = read(data_socket, buffer, BUFFER_SIZE); | |
939 | if (ret == \-1) { | |
eb73e8ad | 940 | perror("read"); |
15545eb6 HS |
941 | exit(EXIT_FAILURE); |
942 | } | |
943 | ||
944 | /* Ensure buffer is 0\-terminated. */ | |
945 | ||
946 | buffer[BUFFER_SIZE \- 1] = 0; | |
947 | ||
948 | printf("Result = %s\\n", buffer); | |
949 | ||
950 | /* Close socket. */ | |
951 | ||
952 | close(data_socket); | |
953 | ||
954 | exit(EXIT_SUCCESS); | |
955 | } | |
e7d0bb47 | 956 | .EE |
15545eb6 | 957 | .PP |
c751683c MK |
958 | For an example of the use of |
959 | .BR SCM_RIGHTS | |
960 | see | |
961 | .BR cmsg (3). | |
47297adb | 962 | .SH SEE ALSO |
77117f4f MK |
963 | .BR recvmsg (2), |
964 | .BR sendmsg (2), | |
965 | .BR socket (2), | |
966 | .BR socketpair (2), | |
967 | .BR cmsg (3), | |
968 | .BR capabilities (7), | |
969 | .BR credentials (7), | |
170e5f0d JC |
970 | .BR socket (7), |
971 | .BR udp (7) |