]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/unix.7
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man7 / unix.7
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>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
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.
10 .\" %%%LICENSE_END
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 .\"
20 .TH UNIX 7 2021-03-22 "Linux man-pages (unreleased)"
21 .SH NAME
22 unix \- sockets for local interprocess communication
23 .SH SYNOPSIS
24 .nf
25 .B #include <sys/socket.h>
26 .B #include <sys/un.h>
27 .PP
28 .IB unix_socket " = socket(AF_UNIX, type, 0);"
29 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
30 .fi
31 .SH DESCRIPTION
32 The
33 .B AF_UNIX
34 (also known as
35 .BR AF_LOCAL )
36 socket family is used to communicate between processes on the same machine
37 efficiently.
38 Traditionally, UNIX domain sockets can be either unnamed,
39 or bound to a filesystem pathname (marked as being of type socket).
40 Linux also supports an abstract namespace which is independent of the
41 filesystem.
42 .PP
43 Valid socket types in the UNIX domain are:
44 .BR SOCK_STREAM ,
45 for a stream-oriented socket;
46 .BR SOCK_DGRAM ,
47 for a datagram-oriented socket that preserves message boundaries
48 (as on most UNIX implementations, UNIX domain datagram
49 sockets are always reliable and don't reorder datagrams);
50 and (since Linux 2.6.4)
51 .BR SOCK_SEQPACKET ,
52 for a sequenced-packet socket that is connection-oriented,
53 preserves message boundaries,
54 and delivers messages in the order that they were sent.
55 .PP
56 UNIX domain sockets support passing file descriptors or process credentials
57 to other processes using ancillary data.
58 .SS Address format
59 A UNIX domain socket address is represented in the following structure:
60 .PP
61 .in +4n
62 .EX
63 .\" #define UNIX_PATH_MAX 108
64 .\"
65 struct sockaddr_un {
66 sa_family_t sun_family; /* AF_UNIX */
67 char sun_path[108]; /* Pathname */
68 };
69 .EE
70 .in
71 .PP
72 The
73 .I sun_family
74 field always contains
75 .BR AF_UNIX .
76 On Linux,
77 .I sun_path
78 is 108 bytes in size; see also NOTES, below.
79 .PP
80 Various systems calls (for example,
81 .BR bind (2),
82 .BR connect (2),
83 and
84 .BR sendto (2))
85 take a
86 .I sockaddr_un
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.
95 .PP
96 Three types of address are distinguished in the
97 .I sockaddr_un
98 structure:
99 .IP * 3
100 .IR pathname :
101 a UNIX domain socket can be bound to a null-terminated
102 filesystem pathname using
103 .BR bind (2).
104 When the address of a pathname socket is returned
105 (by one of the system calls noted above),
106 its length is
107 .IP
108 .in +4n
109 .EX
110 offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
111 .EE
112 .in
113 .IP
114 and
115 .I sun_path
116 contains the null-terminated pathname.
117 (On Linux, the above
118 .BR offsetof ()
119 expression equates to the same value as
120 .IR sizeof(sa_family_t) ,
121 but some other implementations include other fields before
122 .IR sun_path ,
123 so the
124 .BR offsetof ()
125 expression more portably describes the size of the address structure.)
126 .IP
127 For further details of pathname sockets, see below.
128 .IP *
129 .IR unnamed :
130 A stream socket that has not been bound to a pathname using
131 .BR bind (2)
132 has no name.
133 Likewise, the two sockets created by
134 .BR socketpair (2)
135 are unnamed.
136 When the address of an unnamed socket is returned,
137 its length is
138 .IR "sizeof(sa_family_t)" ,
139 and
140 .I sun_path
141 should not be inspected.
142 .\" There is quite some variation across implementations: FreeBSD
143 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
144 .IP *
145 .IR abstract :
146 an abstract socket address is distinguished (from a pathname socket)
147 by the fact that
148 .I sun_path[0]
149 is a null byte (\(aq\e0\(aq).
150 The socket's address in this namespace is given by the additional
151 bytes in
152 .I sun_path
153 that are covered by the specified length of the address structure.
154 (Null bytes in the name have no special significance.)
155 The name has no connection with filesystem pathnames.
156 When the address of an abstract socket is returned,
157 the returned
158 .I addrlen
159 is greater than
160 .I sizeof(sa_family_t)
161 (i.e., greater than 2), and the name of the socket is contained in
162 the first
163 .I (addrlen \- sizeof(sa_family_t))
164 bytes of
165 .IR sun_path .
166 .SS Pathname sockets
167 When binding a socket to a pathname, a few rules should be observed
168 for maximum portability and ease of coding:
169 .IP * 3
170 The pathname in
171 .I sun_path
172 should be null-terminated.
173 .IP *
174 The length of the pathname, including the terminating null byte,
175 should not exceed the size of
176 .IR sun_path .
177 .IP *
178 The
179 .I addrlen
180 argument that describes the enclosing
181 .I sockaddr_un
182 structure should have a value of at least:
183 .IP
184 .in +4n
185 .EX
186 offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
187 .EE
188 .in
189 .IP
190 or, more simply,
191 .I addrlen
192 can be specified as
193 .IR "sizeof(struct sockaddr_un)" .
194 .PP
195 There is some variation in how implementations handle UNIX domain
196 socket addresses that do not follow the above rules.
197 For example, some (but not all) implementations
198 .\" Linux does this, including for the case where the supplied path
199 .\" is 108 bytes
200 append a null terminator if none is present in the supplied
201 .IR sun_path .
202 .PP
203 When coding portable applications,
204 keep in mind that some implementations
205 .\" HP-UX
206 have
207 .I sun_path
208 as short as 92 bytes.
209 .\" Modern BSDs generally have 104, Tru64 and AIX have 104,
210 .\" Solaris and Irix have 108
211 .PP
212 Various system calls
213 .RB ( accept (2),
214 .BR recvfrom (2),
215 .BR getsockname (2),
216 .BR getpeername (2))
217 return socket address structures.
218 When applied to UNIX domain sockets, the value-result
219 .I addrlen
220 argument supplied to the call should be initialized as above.
221 Upon return, the argument is set to indicate the
222 .I actual
223 size of the address structure.
224 The caller should check the value returned in this argument:
225 if the output value exceeds the input value,
226 then there is no guarantee that a null terminator is present in
227 .IR sun_path .
228 (See BUGS.)
229 .\"
230 .SS Pathname socket ownership and permissions
231 In the Linux implementation,
232 pathname sockets honor the permissions of the directory they are in.
233 Creation of a new socket fails if the process does not have write and
234 search (execute) permission on the directory in which the socket is created.
235 .PP
236 On Linux,
237 connecting to a stream socket object requires write permission on that socket;
238 sending a datagram to a datagram socket likewise
239 requires write permission on that socket.
240 POSIX does not make any statement about the effect of the permissions
241 on a socket file, and on some systems (e.g., older BSDs),
242 the socket permissions are ignored.
243 Portable programs should not rely on
244 this feature for security.
245 .PP
246 When creating a new socket, the owner and group of the socket file
247 are set according to the usual rules.
248 The socket file has all permissions enabled,
249 other than those that are turned off by the process
250 .BR umask (2).
251 .PP
252 The owner, group, and permissions of a pathname socket can be changed (using
253 .BR chown (2)
254 and
255 .BR chmod (2)).
256 .\" However, fchown() and fchmod() do not seem to have an effect
257 .\"
258 .SS Abstract sockets
259 Socket permissions have no meaning for abstract sockets:
260 the process
261 .BR umask (2)
262 has no effect when binding an abstract socket,
263 and changing the ownership and permissions of the object (via
264 .BR fchown (2)
265 and
266 .BR fchmod (2))
267 has no effect on the accessibility of the socket.
268 .PP
269 Abstract sockets automatically disappear when all open references
270 to the socket are closed.
271 .PP
272 The abstract socket namespace is a nonportable Linux extension.
273 .\"
274 .SS Socket options
275 For historical reasons, these socket options are specified with a
276 .B SOL_SOCKET
277 type even though they are
278 .B AF_UNIX
279 specific.
280 They can be set with
281 .BR setsockopt (2)
282 and read with
283 .BR getsockopt (2)
284 by specifying
285 .B SOL_SOCKET
286 as the socket family.
287 .TP
288 .B SO_PASSCRED
289 Enabling this socket option causes receipt of the credentials of
290 the sending process in an
291 .B SCM_CREDENTIALS ancillary
292 message in each subsequently received message.
293 The returned credentials are those specified by the sender using
294 .BR SCM_CREDENTIALS ,
295 or a default that includes the sender's PID, real user ID, and real group ID,
296 if the sender did not specify
297 .B SCM_CREDENTIALS
298 ancillary data.
299 .IP
300 When this option is set and the socket is not yet connected,
301 a unique name in the abstract namespace will be generated automatically.
302 .IP
303 The value given as an argument to
304 .BR setsockopt (2)
305 and returned as the result of
306 .BR getsockopt (2)
307 is an integer boolean flag.
308 .TP
309 .B SO_PASSSEC
310 Enables receiving of the SELinux security label of the peer socket
311 in an ancillary message of type
312 .B SCM_SECURITY
313 (see below).
314 .IP
315 The value given as an argument to
316 .BR setsockopt (2)
317 and returned as the result of
318 .BR getsockopt (2)
319 is an integer boolean flag.
320 .IP
321 The
322 .B SO_PASSSEC
323 option is supported for UNIX domain datagram sockets
324 .\" commit 877ce7c1b3afd69a9b1caeb1b9964c992641f52a
325 since Linux 2.6.18;
326 support for UNIX domain stream sockets was added
327 .\" commit 37a9a8df8ce9de6ea73349c9ac8bdf6ba4ec4f70
328 in Linux 4.2.
329 .TP
330 .B SO_PEEK_OFF
331 See
332 .BR socket (7).
333 .TP
334 .B SO_PEERCRED
335 This read-only socket option returns the
336 credentials of the peer process connected to this socket.
337 The returned credentials are those that were in effect at the time
338 of the call to
339 .BR connect (2)
340 or
341 .BR socketpair (2).
342 .IP
343 The argument to
344 .BR getsockopt (2)
345 is a pointer to a
346 .I ucred
347 structure; define the
348 .B _GNU_SOURCE
349 feature test macro to obtain the definition of that structure from
350 .IR <sys/socket.h> .
351 .IP
352 The use of this option is possible only for connected
353 .B AF_UNIX
354 stream sockets and for
355 .B AF_UNIX
356 stream and datagram socket pairs created using
357 .BR socketpair (2).
358 .TP
359 .B SO_PEERSEC
360 This read-only socket option returns the
361 security context of the peer socket connected to this socket.
362 By default, this will be the same as the security context of
363 the process that created the peer socket unless overridden
364 by the policy or by a process with the required permissions.
365 .IP
366 The argument to
367 .BR getsockopt (2)
368 is a pointer to a buffer of the specified length in bytes
369 into which the security context string will be copied.
370 If the buffer length is less than the length of the security
371 context string, then
372 .BR getsockopt (2)
373 returns \-1, sets
374 .I errno
375 to
376 .BR ERANGE ,
377 and returns the required length via
378 .IR optlen .
379 The caller should allocate at least
380 .B NAME_MAX
381 bytes for the buffer initially, although this is not guaranteed
382 to be sufficient.
383 Resizing the buffer to the returned length
384 and retrying may be necessary.
385 .IP
386 The security context string may include a terminating null character
387 in the returned length, but is not guaranteed to do so: a security
388 context "foo" might be represented as either {'f','o','o'} of length 3
389 or {'f','o','o','\\0'} of length 4, which are considered to be
390 interchangeable.
391 The string is printable, does not contain non-terminating null characters,
392 and is in an unspecified encoding (in particular, it
393 is not guaranteed to be ASCII or UTF-8).
394 .IP
395 The use of this option for sockets in the
396 .B AF_UNIX
397 address family is supported since Linux 2.6.2 for connected stream sockets,
398 and since Linux 4.18
399 .\" commit 0b811db2cb2aabc910e53d34ebb95a15997c33e7
400 also for stream and datagram socket pairs created using
401 .BR socketpair (2).
402 .\"
403 .SS Autobind feature
404 If a
405 .BR bind (2)
406 call specifies
407 .I addrlen
408 as
409 .IR sizeof(sa_family_t) ,
410 .\" i.e., sizeof(short)
411 or the
412 .B SO_PASSCRED
413 socket option was specified for a socket that was
414 not explicitly bound to an address,
415 then the socket is autobound to an abstract address.
416 The address consists of a null byte
417 followed by 5 bytes in the character set
418 .IR [0\-9a\-f] .
419 Thus, there is a limit of 2^20 autobind addresses.
420 (From Linux 2.1.15, when the autobind feature was added,
421 8 bytes were used, and the limit was thus 2^32 autobind addresses.
422 The change to 5 bytes came in Linux 2.3.15.)
423 .SS Sockets API
424 The following paragraphs describe domain-specific details and
425 unsupported features of the sockets API for UNIX domain sockets on Linux.
426 .PP
427 UNIX domain sockets do not support the transmission of
428 out-of-band data (the
429 .B MSG_OOB
430 flag for
431 .BR send (2)
432 and
433 .BR recv (2)).
434 .PP
435 The
436 .BR send (2)
437 .B MSG_MORE
438 flag is not supported by UNIX domain sockets.
439 .PP
440 Before Linux 3.4,
441 .\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f
442 the use of
443 .B MSG_TRUNC
444 in the
445 .I flags
446 argument of
447 .BR recv (2)
448 was not supported by UNIX domain sockets.
449 .PP
450 The
451 .B SO_SNDBUF
452 socket option does have an effect for UNIX domain sockets, but the
453 .B SO_RCVBUF
454 option does not.
455 For datagram sockets, the
456 .B SO_SNDBUF
457 value imposes an upper limit on the size of outgoing datagrams.
458 This limit is calculated as the doubled (see
459 .BR socket (7))
460 option value less 32 bytes used for overhead.
461 .SS Ancillary messages
462 Ancillary data is sent and received using
463 .BR sendmsg (2)
464 and
465 .BR recvmsg (2).
466 For historical reasons, the ancillary message types listed below
467 are specified with a
468 .B SOL_SOCKET
469 type even though they are
470 .B AF_UNIX
471 specific.
472 To send them, set the
473 .I cmsg_level
474 field of the struct
475 .I cmsghdr
476 to
477 .B SOL_SOCKET
478 and the
479 .I cmsg_type
480 field to the type.
481 For more information, see
482 .BR cmsg (3).
483 .TP
484 .B SCM_RIGHTS
485 Send or receive a set of open file descriptors from another process.
486 The data portion contains an integer array of the file descriptors.
487 .IP
488 Commonly, this operation is referred to as "passing a file descriptor"
489 to another process.
490 However, more accurately,
491 what is being passed is a reference to an open file description (see
492 .BR open (2)),
493 and in the receiving process it is likely that a different
494 file descriptor number will be used.
495 Semantically, this operation is equivalent to duplicating
496 .RB ( dup (2))
497 a file descriptor into the file descriptor table of another process.
498 .IP
499 If the buffer used to receive the ancillary data containing
500 file descriptors is too small (or is absent),
501 then the ancillary data is truncated (or discarded)
502 and the excess file descriptors are automatically closed
503 in the receiving process.
504 .IP
505 If the number of file descriptors received in the ancillary data would
506 cause the process to exceed its
507 .B RLIMIT_NOFILE
508 resource limit (see
509 .BR getrlimit (2)),
510 the excess file descriptors are automatically closed
511 in the receiving process.
512 .IP
513 The kernel constant
514 .B SCM_MAX_FD
515 defines a limit on the number of file descriptors in the array.
516 Attempting to send an array larger than this limit causes
517 .BR sendmsg (2)
518 to fail with the error
519 .BR EINVAL .
520 .B SCM_MAX_FD
521 has the value 253
522 (or 255 in kernels
523 .\" commit bba14de98753cb6599a2dae0e520714b2153522d
524 before 2.6.38).
525 .TP
526 .B SCM_CREDENTIALS
527 Send or receive UNIX credentials.
528 This can be used for authentication.
529 The credentials are passed as a
530 .I struct ucred
531 ancillary message.
532 This structure is defined in
533 .I <sys/socket.h>
534 as follows:
535 .IP
536 .in +4n
537 .EX
538 struct ucred {
539 pid_t pid; /* Process ID of the sending process */
540 uid_t uid; /* User ID of the sending process */
541 gid_t gid; /* Group ID of the sending process */
542 };
543 .EE
544 .in
545 .IP
546 Since glibc 2.8, the
547 .B _GNU_SOURCE
548 feature test macro must be defined (before including
549 .I any
550 header files) in order to obtain the definition
551 of this structure.
552 .IP
553 The credentials which the sender specifies are checked by the kernel.
554 A privileged process is allowed to specify values that do not match its own.
555 The sender must specify its own process ID (unless it has the capability
556 .BR CAP_SYS_ADMIN ,
557 in which case the PID of any existing process may be specified),
558 its real user ID, effective user ID, or saved set-user-ID (unless it has
559 .BR CAP_SETUID ),
560 and its real group ID, effective group ID, or saved set-group-ID
561 (unless it has
562 .BR CAP_SETGID ).
563 .IP
564 To receive a
565 .I struct ucred
566 message, the
567 .B SO_PASSCRED
568 option must be enabled on the socket.
569 .TP
570 .B SCM_SECURITY
571 Receive the SELinux security context (the security label)
572 of the peer socket.
573 The received ancillary data is a null-terminated string containing
574 the security context.
575 The receiver should allocate at least
576 .B NAME_MAX
577 bytes in the data portion of the ancillary message for this data.
578 .IP
579 To receive the security context, the
580 .B SO_PASSSEC
581 option must be enabled on the socket (see above).
582 .PP
583 When sending ancillary data with
584 .BR sendmsg (2),
585 only one item of each of the above types may be included in the sent message.
586 .PP
587 At least one byte of real data should be sent when sending ancillary data.
588 On Linux, this is required to successfully send ancillary data over
589 a UNIX domain stream socket.
590 When sending ancillary data over a UNIX domain datagram socket,
591 it is not necessary on Linux to send any accompanying real data.
592 However, portable applications should also include at least one byte
593 of real data when sending ancillary data over a datagram socket.
594 .PP
595 When receiving from a stream socket,
596 ancillary data forms a kind of barrier for the received data.
597 For example, suppose that the sender transmits as follows:
598 .PP
599 .RS
600 .PD 0
601 .IP 1. 3
602 .BR sendmsg (2)
603 of four bytes, with no ancillary data.
604 .IP 2.
605 .BR sendmsg (2)
606 of one byte, with ancillary data.
607 .IP 3.
608 .BR sendmsg (2)
609 of four bytes, with no ancillary data.
610 .PD
611 .RE
612 .PP
613 Suppose that the receiver now performs
614 .BR recvmsg (2)
615 calls each with a buffer size of 20 bytes.
616 The first call will receive five bytes of data,
617 along with the ancillary data sent by the second
618 .BR sendmsg (2)
619 call.
620 The next call will receive the remaining four bytes of data.
621 .PP
622 If the space allocated for receiving incoming ancillary data is too small
623 then the ancillary data is truncated to the number of headers
624 that will fit in the supplied buffer (or, in the case of an
625 .B SCM_RIGHTS
626 file descriptor list, the list of file descriptors may be truncated).
627 If no buffer is provided for incoming ancillary data (i.e., the
628 .I msg_control
629 field of the
630 .I msghdr
631 structure supplied to
632 .BR recvmsg (2)
633 is NULL),
634 then the incoming ancillary data is discarded.
635 In both of these cases, the
636 .B MSG_CTRUNC
637 flag will be set in the
638 .I msg.msg_flags
639 value returned by
640 .BR recvmsg (2).
641 .\"
642 .SS Ioctls
643 The following
644 .BR ioctl (2)
645 calls return information in
646 .IR value .
647 The correct syntax is:
648 .PP
649 .RS
650 .nf
651 .BI int " value";
652 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
653 .fi
654 .RE
655 .PP
656 .I ioctl_type
657 can be:
658 .TP
659 .B SIOCINQ
660 For
661 .B SOCK_STREAM
662 sockets, this call returns the number of unread bytes in the receive buffer.
663 The socket must not be in LISTEN state, otherwise an error
664 .RB ( EINVAL )
665 is returned.
666 .B SIOCINQ
667 is defined in
668 .IR <linux/sockios.h> .
669 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
670 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
671 Alternatively,
672 you can use the synonymous
673 .BR FIONREAD ,
674 defined in
675 .IR <sys/ioctl.h> .
676 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
677 .\" quite what userland might expect. It seems to return the number
678 .\" of bytes allocated for buffers containing pending output.
679 .\" That number is normally larger than the number of bytes of pending
680 .\" output. Since this info is, from userland's point of view, imprecise,
681 .\" and it may well change, probably best not to document this now.
682 For
683 .B SOCK_DGRAM
684 sockets,
685 the returned value is the same as
686 for Internet domain datagram sockets;
687 see
688 .BR udp (7).
689 .SH ERRORS
690 .TP
691 .B EADDRINUSE
692 The specified local address is already in use or the filesystem socket
693 object already exists.
694 .TP
695 .B EBADF
696 This error can occur for
697 .BR sendmsg (2)
698 when sending a file descriptor as ancillary data over
699 a UNIX domain socket (see the description of
700 .BR SCM_RIGHTS ,
701 above), and indicates that the file descriptor number that
702 is being sent is not valid (e.g., it is not an open file descriptor).
703 .TP
704 .B ECONNREFUSED
705 The remote address specified by
706 .BR connect (2)
707 was not a listening socket.
708 This error can also occur if the target pathname is not a socket.
709 .TP
710 .B ECONNRESET
711 Remote socket was unexpectedly closed.
712 .TP
713 .B EFAULT
714 User memory address was not valid.
715 .TP
716 .B EINVAL
717 Invalid argument passed.
718 A common cause is that the value
719 .B AF_UNIX
720 was not specified in the
721 .I sun_type
722 field of passed addresses, or the socket was in an
723 invalid state for the applied operation.
724 .TP
725 .B EISCONN
726 .BR connect (2)
727 called on an already connected socket or a target address was
728 specified on a connected socket.
729 .TP
730 .B ENFILE
731 The system-wide limit on the total number of open files has been reached.
732 .TP
733 .B ENOENT
734 The pathname in the remote address specified to
735 .BR connect (2)
736 did not exist.
737 .TP
738 .B ENOMEM
739 Out of memory.
740 .TP
741 .B ENOTCONN
742 Socket operation needs a target address, but the socket is not connected.
743 .TP
744 .B EOPNOTSUPP
745 Stream operation called on non-stream oriented socket or tried to
746 use the out-of-band data option.
747 .TP
748 .B EPERM
749 The sender passed invalid credentials in the
750 .IR "struct ucred" .
751 .TP
752 .B EPIPE
753 Remote socket was closed on a stream socket.
754 If enabled, a
755 .B SIGPIPE
756 is sent as well.
757 This can be avoided by passing the
758 .B MSG_NOSIGNAL
759 flag to
760 .BR send (2)
761 or
762 .BR sendmsg (2).
763 .TP
764 .B EPROTONOSUPPORT
765 Passed protocol is not
766 .BR AF_UNIX .
767 .TP
768 .B EPROTOTYPE
769 Remote socket does not match the local socket type
770 .RB ( SOCK_DGRAM
771 versus
772 .BR SOCK_STREAM ).
773 .TP
774 .B ESOCKTNOSUPPORT
775 Unknown socket type.
776 .TP
777 .B ESRCH
778 While sending an ancillary message containing credentials
779 .RB ( SCM_CREDENTIALS ),
780 the caller specified a PID that does not match any existing process.
781 .TP
782 .B ETOOMANYREFS
783 This error can occur for
784 .BR sendmsg (2)
785 when sending a file descriptor as ancillary data over
786 a UNIX domain socket (see the description of
787 .BR SCM_RIGHTS ,
788 above).
789 It occurs if the number of "in-flight" file descriptors exceeds the
790 .B RLIMIT_NOFILE
791 resource limit and the caller does not have the
792 .B CAP_SYS_RESOURCE
793 capability.
794 An in-flight file descriptor is one that has been sent using
795 .BR sendmsg (2)
796 but has not yet been accepted in the recipient process using
797 .BR recvmsg (2).
798 .IP
799 This error is diagnosed since mainline Linux 4.5
800 (and in some earlier kernel versions where the fix has been backported).
801 .\" commit 712f4aad406bb1ed67f3f98d04c044191f0ff593
802 In earlier kernel versions,
803 it was possible to place an unlimited number of file descriptors in flight,
804 by sending each file descriptor with
805 .BR sendmsg (2)
806 and then closing the file descriptor so that it was not accounted against the
807 .B RLIMIT_NOFILE
808 resource limit.
809 .PP
810 Other errors can be generated by the generic socket layer or
811 by the filesystem while generating a filesystem socket object.
812 See the appropriate manual pages for more information.
813 .SH VERSIONS
814 .B SCM_CREDENTIALS
815 and the abstract namespace were introduced with Linux 2.2 and should not
816 be used in portable programs.
817 (Some BSD-derived systems also support credential passing,
818 but the implementation details differ.)
819 .SH NOTES
820 Binding to a socket with a filename creates a socket
821 in the filesystem that must be deleted by the caller when it is no
822 longer needed (using
823 .BR unlink (2)).
824 The usual UNIX close-behind semantics apply; the socket can be unlinked
825 at any time and will be finally removed from the filesystem when the last
826 reference to it is closed.
827 .PP
828 To pass file descriptors or credentials over a
829 .B SOCK_STREAM
830 socket, you must
831 send or receive at least one byte of nonancillary data in the same
832 .BR sendmsg (2)
833 or
834 .BR recvmsg (2)
835 call.
836 .PP
837 UNIX domain stream sockets do not support the notion of out-of-band data.
838 .\"
839 .SH BUGS
840 When binding a socket to an address,
841 Linux is one of the implementations that appends a null terminator
842 if none is supplied in
843 .IR sun_path .
844 In most cases this is unproblematic:
845 when the socket address is retrieved,
846 it will be one byte longer than that supplied when the socket was bound.
847 However, there is one case where confusing behavior can result:
848 if 108 non-null bytes are supplied when a socket is bound,
849 then the addition of the null terminator takes the length of
850 the pathname beyond
851 .IR sizeof(sun_path) .
852 Consequently, when retrieving the socket address
853 (for example, via
854 .BR accept (2)),
855 .\" The behavior on Solaris is quite similar.
856 if the input
857 .I addrlen
858 argument for the retrieving call is specified as
859 .IR "sizeof(struct sockaddr_un)" ,
860 then the returned address structure
861 .I won't
862 have a null terminator in
863 .IR sun_path .
864 .PP
865 In addition, some implementations
866 .\" i.e., traditional BSD
867 don't require a null terminator when binding a socket (the
868 .I addrlen
869 argument is used to determine the length of
870 .IR sun_path )
871 and when the socket address is retrieved on these implementations,
872 there is no null terminator in
873 .IR sun_path .
874 .PP
875 Applications that retrieve socket addresses can (portably) code
876 to handle the possibility that there is no null terminator in
877 .I sun_path
878 by respecting the fact that the number of valid bytes in the pathname is:
879 .PP
880 .in +4n
881 .EX
882 strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path))
883 .EE
884 .in
885 .\" The following patch to amend kernel behavior was rejected:
886 .\" http://thread.gmane.org/gmane.linux.kernel.api/2437
887 .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path
888 .\" 2012-04-17
889 .\" And there was a related discussion in the Austin list:
890 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735
891 .\" Subject: Having a sun_path with no null terminator
892 .\" 2012-04-18
893 .\"
894 .\" FIXME . Track http://austingroupbugs.net/view.php?id=561
895 .PP
896 Alternatively, an application can retrieve
897 the socket address by allocating a buffer of size
898 .I "sizeof(struct sockaddr_un)+1"
899 that is zeroed out before the retrieval.
900 The retrieving call can specify
901 .I addrlen
902 as
903 .IR "sizeof(struct sockaddr_un)" ,
904 and the extra zero byte ensures that there will be
905 a null terminator for the string returned in
906 .IR sun_path :
907 .PP
908 .in +4n
909 .EX
910 void *addrp;
911
912 addrlen = sizeof(struct sockaddr_un);
913 addrp = malloc(addrlen + 1);
914 if (addrp == NULL)
915 /* Handle error */ ;
916 memset(addrp, 0, addrlen + 1);
917
918 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1)
919 /* handle error */ ;
920
921 printf("sun_path = %s\en", ((struct sockaddr_un *) addrp)\->sun_path);
922 .EE
923 .in
924 .PP
925 This sort of messiness can be avoided if it is guaranteed
926 that the applications that
927 .I create
928 pathname sockets follow the rules outlined above under
929 .IR "Pathname sockets" .
930 .SH EXAMPLES
931 The following code demonstrates the use of sequenced-packet
932 sockets for local interprocess communication.
933 It consists of two programs.
934 The server program waits for a connection from the client program.
935 The client sends each of its command-line arguments in separate messages.
936 The server treats the incoming messages as integers and adds them up.
937 The client sends the command string "END".
938 The server sends back a message containing the sum of the client's integers.
939 The client prints the sum and exits.
940 The server waits for the next client to connect.
941 To stop the server, the client is called with the command-line argument "DOWN".
942 .PP
943 The following output was recorded while running the server in the background
944 and repeatedly executing the client.
945 Execution of the server program ends when it receives the "DOWN" command.
946 .SS Example output
947 .in +4n
948 .EX
949 $ \fB./server &\fP
950 [1] 25887
951 $ \fB./client 3 4\fP
952 Result = 7
953 $ \fB./client 11 \-5\fP
954 Result = 6
955 $ \fB./client DOWN\fP
956 Result = 0
957 [1]+ Done ./server
958 $
959 .EE
960 .in
961 .SS Program source
962 \&
963 .EX
964 /*
965 * File connection.h
966 */
967
968 #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
969 #define BUFFER_SIZE 12
970
971 /*
972 * File server.c
973 */
974
975 #include <stdio.h>
976 #include <stdlib.h>
977 #include <string.h>
978 #include <sys/socket.h>
979 #include <sys/un.h>
980 #include <unistd.h>
981 #include "connection.h"
982
983 int
984 main(int argc, char *argv[])
985 {
986 struct sockaddr_un name;
987 int down_flag = 0;
988 int ret;
989 int connection_socket;
990 int data_socket;
991 int result;
992 char buffer[BUFFER_SIZE];
993
994 /* Create local socket. */
995
996 connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
997 if (connection_socket == \-1) {
998 perror("socket");
999 exit(EXIT_FAILURE);
1000 }
1001
1002 /*
1003 * For portability clear the whole structure, since some
1004 * implementations have additional (nonstandard) fields in
1005 * the structure.
1006 */
1007
1008 memset(&name, 0, sizeof(name));
1009
1010 /* Bind socket to socket name. */
1011
1012 name.sun_family = AF_UNIX;
1013 strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1);
1014
1015 ret = bind(connection_socket, (const struct sockaddr *) &name,
1016 sizeof(name));
1017 if (ret == \-1) {
1018 perror("bind");
1019 exit(EXIT_FAILURE);
1020 }
1021
1022 /*
1023 * Prepare for accepting connections. The backlog size is set
1024 * to 20. So while one request is being processed other requests
1025 * can be waiting.
1026 */
1027
1028 ret = listen(connection_socket, 20);
1029 if (ret == \-1) {
1030 perror("listen");
1031 exit(EXIT_FAILURE);
1032 }
1033
1034 /* This is the main loop for handling connections. */
1035
1036 for (;;) {
1037
1038 /* Wait for incoming connection. */
1039
1040 data_socket = accept(connection_socket, NULL, NULL);
1041 if (data_socket == \-1) {
1042 perror("accept");
1043 exit(EXIT_FAILURE);
1044 }
1045
1046 result = 0;
1047 for (;;) {
1048
1049 /* Wait for next data packet. */
1050
1051 ret = read(data_socket, buffer, sizeof(buffer));
1052 if (ret == \-1) {
1053 perror("read");
1054 exit(EXIT_FAILURE);
1055 }
1056
1057 /* Ensure buffer is 0\-terminated. */
1058
1059 buffer[sizeof(buffer) \- 1] = 0;
1060
1061 /* Handle commands. */
1062
1063 if (!strncmp(buffer, "DOWN", sizeof(buffer))) {
1064 down_flag = 1;
1065 break;
1066 }
1067
1068 if (!strncmp(buffer, "END", sizeof(buffer))) {
1069 break;
1070 }
1071
1072 /* Add received summand. */
1073
1074 result += atoi(buffer);
1075 }
1076
1077 /* Send result. */
1078
1079 sprintf(buffer, "%d", result);
1080 ret = write(data_socket, buffer, sizeof(buffer));
1081 if (ret == \-1) {
1082 perror("write");
1083 exit(EXIT_FAILURE);
1084 }
1085
1086 /* Close socket. */
1087
1088 close(data_socket);
1089
1090 /* Quit on DOWN command. */
1091
1092 if (down_flag) {
1093 break;
1094 }
1095 }
1096
1097 close(connection_socket);
1098
1099 /* Unlink the socket. */
1100
1101 unlink(SOCKET_NAME);
1102
1103 exit(EXIT_SUCCESS);
1104 }
1105
1106 /*
1107 * File client.c
1108 */
1109
1110 #include <errno.h>
1111 #include <stdio.h>
1112 #include <stdlib.h>
1113 #include <string.h>
1114 #include <sys/socket.h>
1115 #include <sys/un.h>
1116 #include <unistd.h>
1117 #include "connection.h"
1118
1119 int
1120 main(int argc, char *argv[])
1121 {
1122 struct sockaddr_un addr;
1123 int ret;
1124 int data_socket;
1125 char buffer[BUFFER_SIZE];
1126
1127 /* Create local socket. */
1128
1129 data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1130 if (data_socket == \-1) {
1131 perror("socket");
1132 exit(EXIT_FAILURE);
1133 }
1134
1135 /*
1136 * For portability clear the whole structure, since some
1137 * implementations have additional (nonstandard) fields in
1138 * the structure.
1139 */
1140
1141 memset(&addr, 0, sizeof(addr));
1142
1143 /* Connect socket to socket address. */
1144
1145 addr.sun_family = AF_UNIX;
1146 strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1);
1147
1148 ret = connect(data_socket, (const struct sockaddr *) &addr,
1149 sizeof(addr));
1150 if (ret == \-1) {
1151 fprintf(stderr, "The server is down.\en");
1152 exit(EXIT_FAILURE);
1153 }
1154
1155 /* Send arguments. */
1156
1157 for (int i = 1; i < argc; ++i) {
1158 ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
1159 if (ret == \-1) {
1160 perror("write");
1161 break;
1162 }
1163 }
1164
1165 /* Request result. */
1166
1167 strcpy(buffer, "END");
1168 ret = write(data_socket, buffer, strlen(buffer) + 1);
1169 if (ret == \-1) {
1170 perror("write");
1171 exit(EXIT_FAILURE);
1172 }
1173
1174 /* Receive result. */
1175
1176 ret = read(data_socket, buffer, sizeof(buffer));
1177 if (ret == \-1) {
1178 perror("read");
1179 exit(EXIT_FAILURE);
1180 }
1181
1182 /* Ensure buffer is 0\-terminated. */
1183
1184 buffer[sizeof(buffer) \- 1] = 0;
1185
1186 printf("Result = %s\en", buffer);
1187
1188 /* Close socket. */
1189
1190 close(data_socket);
1191
1192 exit(EXIT_SUCCESS);
1193 }
1194 .EE
1195 .PP
1196 For examples of the use of
1197 .BR SCM_RIGHTS ,
1198 see
1199 .BR cmsg (3)
1200 and
1201 .BR seccomp_unotify (2).
1202 .SH SEE ALSO
1203 .BR recvmsg (2),
1204 .BR sendmsg (2),
1205 .BR socket (2),
1206 .BR socketpair (2),
1207 .BR cmsg (3),
1208 .BR capabilities (7),
1209 .BR credentials (7),
1210 .BR socket (7),
1211 .BR udp (7)