Debian Codegraph @

upstream/debian-trixie/man7/unix.7

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