1 .\" SPDX-License-Identifier: Linux-man-pages-1-para
3 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>,
4 .\" Copyright (C) 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>,
5 .\" and Copyright (C) 2016, Heinrich Schuchardt <xypron.glpk@gmx.de>
7 .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com>
8 .\" Modified, 2003-09-23, Adam Langley
9 .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
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.
15 .TH UNIX 7 (date) "Linux man-pages (unreleased)"
17 unix \- sockets for local interprocess communication
20 .B #include <sys/socket.h>
21 .B #include <sys/un.h>
23 .IB unix_socket " = socket(AF_UNIX, type, 0);"
24 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
31 socket family is used to communicate between processes on the same machine
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
38 Valid socket types in the UNIX domain are:
40 for a stream-oriented socket;
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)
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.
51 UNIX domain sockets support passing file descriptors or process credentials
52 to other processes using ancillary data.
54 A UNIX domain socket address is represented in the following structure:
58 .\" #define UNIX_PATH_MAX 108
61 sa_family_t sun_family; /* AF_UNIX */
62 char sun_path[108]; /* Pathname */
73 is 108 bytes in size; see also BUGS, below.
75 Various systems calls (for example,
83 Some other system calls (for example,
89 return an argument of this type.
91 Three types of address are distinguished in the
96 a UNIX domain socket can be bound to a null-terminated
97 filesystem pathname using
99 When the address of a pathname socket is returned
100 (by one of the system calls noted above),
105 offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
111 contains the null-terminated pathname.
114 expression equates to the same value as
115 .IR sizeof(sa_family_t) ,
116 but some other implementations include other fields before
120 expression more portably describes the size of the address structure.)
122 For further details of pathname sockets, see below.
125 A stream socket that has not been bound to a pathname using
128 Likewise, the two sockets created by
131 When the address of an unnamed socket is returned,
133 .IR "sizeof(sa_family_t)" ,
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.
141 an abstract socket address is distinguished (from a pathname socket)
144 is a null byte (\[aq]\e0\[aq]).
145 The socket's address in this namespace is given by the additional
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,
155 .I sizeof(sa_family_t)
156 (i.e., greater than 2), and the name of the socket is contained in
158 .I (addrlen \- sizeof(sa_family_t))
162 When binding a socket to a pathname, a few rules should be observed
163 for maximum portability and ease of coding:
167 should be null-terminated.
169 The length of the pathname, including the terminating null byte,
170 should not exceed the size of
175 argument that describes the enclosing
177 structure should have a value of at least:
181 offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
188 .IR "sizeof(struct sockaddr_un)" .
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
195 append a null terminator if none is present in the supplied
198 When coding portable applications,
199 keep in mind that some implementations
203 as short as 92 bytes.
204 .\" Modern BSDs generally have 104, Tru64 and AIX have 104,
205 .\" Solaris and Irix have 108
212 return socket address structures.
213 When applied to UNIX domain sockets, the value-result
215 argument supplied to the call should be initialized as above.
216 Upon return, the argument is set to indicate the
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
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.
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.
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
247 The owner, group, and permissions of a pathname socket can be changed (using
251 .\" However, fchown() and fchmod() do not seem to have an effect
254 Socket permissions have no meaning for abstract sockets:
257 has no effect when binding an abstract socket,
258 and changing the ownership and permissions of the object (via
262 has no effect on the accessibility of the socket.
264 Abstract sockets automatically disappear when all open references
265 to the socket are closed.
267 The abstract socket namespace is a nonportable Linux extension.
270 For historical reasons, these socket options are specified with a
272 type even though they are
281 as the socket family.
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
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.
298 The value given as an argument to
300 and returned as the result of
302 is an integer boolean flag.
305 Enables receiving of the SELinux security label of the peer socket
306 in an ancillary message of type
310 The value given as an argument to
312 and returned as the result of
314 is an integer boolean flag.
318 option is supported for UNIX domain datagram sockets
319 .\" commit 877ce7c1b3afd69a9b1caeb1b9964c992641f52a
321 support for UNIX domain stream sockets was added
322 .\" commit 37a9a8df8ce9de6ea73349c9ac8bdf6ba4ec4f70
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
342 structure; define the
344 feature test macro to obtain the definition of that structure from
347 The use of this option is possible only for connected
349 stream sockets and for
351 stream and datagram socket pairs created using
355 This read-only socket option returns the
356 security context of the peer socket connected to this socket.
357 By default, this will be the same as the security context of
358 the process that created the peer socket unless overridden
359 by the policy or by a process with the required permissions.
363 is a pointer to a buffer of the specified length in bytes
364 into which the security context string will be copied.
365 If the buffer length is less than the length of the security
372 and returns the required length via
374 The caller should allocate at least
376 bytes for the buffer initially, although this is not guaranteed
378 Resizing the buffer to the returned length
379 and retrying may be necessary.
381 The security context string may include a terminating null character
382 in the returned length, but is not guaranteed to do so: a security
383 context "foo" might be represented as either {'f','o','o'} of length 3
384 or {'f','o','o','\\0'} of length 4, which are considered to be
386 The string is printable, does not contain non-terminating null characters,
387 and is in an unspecified encoding (in particular, it
388 is not guaranteed to be ASCII or UTF-8).
390 The use of this option for sockets in the
392 address family is supported since Linux 2.6.2 for connected stream sockets,
394 .\" commit 0b811db2cb2aabc910e53d34ebb95a15997c33e7
395 also for stream and datagram socket pairs created using
404 .IR sizeof(sa_family_t) ,
405 .\" i.e., sizeof(short)
408 socket option was specified for a socket that was
409 not explicitly bound to an address,
410 then the socket is autobound to an abstract address.
411 The address consists of a null byte
412 followed by 5 bytes in the character set
414 Thus, there is a limit of 2\[ha]20 autobind addresses.
415 (From Linux 2.1.15, when the autobind feature was added,
416 8 bytes were used, and the limit was thus 2\[ha]32 autobind addresses.
417 The change to 5 bytes came in Linux 2.3.15.)
419 The following paragraphs describe domain-specific details and
420 unsupported features of the sockets API for UNIX domain sockets on Linux.
422 UNIX domain sockets do not support the transmission of
423 out-of-band data (the
433 flag is not supported by UNIX domain sockets.
436 .\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f
443 was not supported by UNIX domain sockets.
447 socket option does have an effect for UNIX domain sockets, but the
450 For datagram sockets, the
452 value imposes an upper limit on the size of outgoing datagrams.
453 This limit is calculated as the doubled (see
455 option value less 32 bytes used for overhead.
456 .SS Ancillary messages
457 Ancillary data is sent and received using
461 For historical reasons, the ancillary message types listed below
464 type even though they are
467 To send them, set the
476 For more information, see
480 Send or receive a set of open file descriptors from another process.
481 The data portion contains an integer array of the file descriptors.
483 Commonly, this operation is referred to as "passing a file descriptor"
485 However, more accurately,
486 what is being passed is a reference to an open file description (see
488 and in the receiving process it is likely that a different
489 file descriptor number will be used.
490 Semantically, this operation is equivalent to duplicating
492 a file descriptor into the file descriptor table of another process.
494 If the buffer used to receive the ancillary data containing
495 file descriptors is too small (or is absent),
496 then the ancillary data is truncated (or discarded)
497 and the excess file descriptors are automatically closed
498 in the receiving process.
500 If the number of file descriptors received in the ancillary data would
501 cause the process to exceed its
505 the excess file descriptors are automatically closed
506 in the receiving process.
510 defines a limit on the number of file descriptors in the array.
511 Attempting to send an array larger than this limit causes
513 to fail with the error
517 .\" commit bba14de98753cb6599a2dae0e520714b2153522d
518 (or 255 before Linux 2.6.38).
521 Send or receive UNIX credentials.
522 This can be used for authentication.
523 The credentials are passed as a
526 This structure is defined in
533 pid_t pid; /* Process ID of the sending process */
534 uid_t uid; /* User ID of the sending process */
535 gid_t gid; /* Group ID of the sending process */
542 feature test macro must be defined (before including
544 header files) in order to obtain the definition
547 The credentials which the sender specifies are checked by the kernel.
548 A privileged process is allowed to specify values that do not match its own.
549 The sender must specify its own process ID (unless it has the capability
551 in which case the PID of any existing process may be specified),
552 its real user ID, effective user ID, or saved set-user-ID (unless it has
554 and its real group ID, effective group ID, or saved set-group-ID
562 option must be enabled on the socket.
565 Receive the SELinux security context (the security label)
567 The received ancillary data is a null-terminated string containing
568 the security context.
569 The receiver should allocate at least
571 bytes in the data portion of the ancillary message for this data.
573 To receive the security context, the
575 option must be enabled on the socket (see above).
577 When sending ancillary data with
579 only one item of each of the above types may be included in the sent message.
581 At least one byte of real data should be sent when sending ancillary data.
582 On Linux, this is required to successfully send ancillary data over
583 a UNIX domain stream socket.
584 When sending ancillary data over a UNIX domain datagram socket,
585 it is not necessary on Linux to send any accompanying real data.
586 However, portable applications should also include at least one byte
587 of real data when sending ancillary data over a datagram socket.
589 When receiving from a stream socket,
590 ancillary data forms a kind of barrier for the received data.
591 For example, suppose that the sender transmits as follows:
597 of four bytes, with no ancillary data.
600 of one byte, with ancillary data.
603 of four bytes, with no ancillary data.
607 Suppose that the receiver now performs
609 calls each with a buffer size of 20 bytes.
610 The first call will receive five bytes of data,
611 along with the ancillary data sent by the second
614 The next call will receive the remaining four bytes of data.
616 If the space allocated for receiving incoming ancillary data is too small
617 then the ancillary data is truncated to the number of headers
618 that will fit in the supplied buffer (or, in the case of an
620 file descriptor list, the list of file descriptors may be truncated).
621 If no buffer is provided for incoming ancillary data (i.e., the
625 structure supplied to
628 then the incoming ancillary data is discarded.
629 In both of these cases, the
631 flag will be set in the
639 calls return information in
641 The correct syntax is:
646 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
656 sockets, this call returns the number of unread bytes in the receive buffer.
657 The socket must not be in LISTEN state, otherwise an error
662 .IR <linux/sockios.h> .
663 .\" FIXME . https://www.sourceware.org/bugzilla/show_bug.cgi?id=12002,
664 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
666 you can use the synonymous
670 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
671 .\" quite what userland might expect. It seems to return the number
672 .\" of bytes allocated for buffers containing pending output.
673 .\" That number is normally larger than the number of bytes of pending
674 .\" output. Since this info is, from userland's point of view, imprecise,
675 .\" and it may well change, probably best not to document this now.
679 the returned value is the same as
680 for Internet domain datagram sockets;
686 The specified local address is already in use or the filesystem socket
687 object already exists.
690 This error can occur for
692 when sending a file descriptor as ancillary data over
693 a UNIX domain socket (see the description of
695 above), and indicates that the file descriptor number that
696 is being sent is not valid (e.g., it is not an open file descriptor).
699 The remote address specified by
701 was not a listening socket.
702 This error can also occur if the target pathname is not a socket.
705 Remote socket was unexpectedly closed.
708 User memory address was not valid.
711 Invalid argument passed.
712 A common cause is that the value
714 was not specified in the
716 field of passed addresses, or the socket was in an
717 invalid state for the applied operation.
721 called on an already connected socket or a target address was
722 specified on a connected socket.
725 The system-wide limit on the total number of open files has been reached.
728 The pathname in the remote address specified to
736 Socket operation needs a target address, but the socket is not connected.
739 Stream operation called on non-stream oriented socket or tried to
740 use the out-of-band data option.
743 The sender passed invalid credentials in the
747 Remote socket was closed on a stream socket.
751 This can be avoided by passing the
759 Passed protocol is not
763 Remote socket does not match the local socket type
772 While sending an ancillary message containing credentials
773 .RB ( SCM_CREDENTIALS ),
774 the caller specified a PID that does not match any existing process.
777 This error can occur for
779 when sending a file descriptor as ancillary data over
780 a UNIX domain socket (see the description of
783 It occurs if the number of "in-flight" file descriptors exceeds the
785 resource limit and the caller does not have the
788 An in-flight file descriptor is one that has been sent using
790 but has not yet been accepted in the recipient process using
793 This error is diagnosed since mainline Linux 4.5
794 (and in some earlier kernel versions where the fix has been backported).
795 .\" commit 712f4aad406bb1ed67f3f98d04c044191f0ff593
796 In earlier kernel versions,
797 it was possible to place an unlimited number of file descriptors in flight,
798 by sending each file descriptor with
800 and then closing the file descriptor so that it was not accounted against the
804 Other errors can be generated by the generic socket layer or
805 by the filesystem while generating a filesystem socket object.
806 See the appropriate manual pages for more information.
809 and the abstract namespace were introduced with Linux 2.2 and should not
810 be used in portable programs.
811 (Some BSD-derived systems also support credential passing,
812 but the implementation details differ.)
814 Binding to a socket with a filename creates a socket
815 in the filesystem that must be deleted by the caller when it is no
818 The usual UNIX close-behind semantics apply; the socket can be unlinked
819 at any time and will be finally removed from the filesystem when the last
820 reference to it is closed.
822 To pass file descriptors or credentials over a
825 send or receive at least one byte of nonancillary data in the same
831 UNIX domain stream sockets do not support the notion of out-of-band data.
834 When binding a socket to an address,
835 Linux is one of the implementations that appends a null terminator
836 if none is supplied in
838 In most cases this is unproblematic:
839 when the socket address is retrieved,
840 it will be one byte longer than that supplied when the socket was bound.
841 However, there is one case where confusing behavior can result:
842 if 108 non-null bytes are supplied when a socket is bound,
843 then the addition of the null terminator takes the length of
845 .IR sizeof(sun_path) .
846 Consequently, when retrieving the socket address
849 .\" The behavior on Solaris is quite similar.
852 argument for the retrieving call is specified as
853 .IR "sizeof(struct sockaddr_un)" ,
854 then the returned address structure
856 have a null terminator in
859 In addition, some implementations
860 .\" i.e., traditional BSD
861 don't require a null terminator when binding a socket (the
863 argument is used to determine the length of
865 and when the socket address is retrieved on these implementations,
866 there is no null terminator in
869 Applications that retrieve socket addresses can (portably) code
870 to handle the possibility that there is no null terminator in
872 by respecting the fact that the number of valid bytes in the pathname is:
876 strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path))
879 .\" The following patch to amend kernel behavior was rejected:
880 .\" http://thread.gmane.org/gmane.linux.kernel.api/2437
881 .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path
883 .\" And there was a related discussion in the Austin list:
884 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735
885 .\" Subject: Having a sun_path with no null terminator
888 .\" FIXME . Track http://austingroupbugs.net/view.php?id=561
890 Alternatively, an application can retrieve
891 the socket address by allocating a buffer of size
892 .I "sizeof(struct sockaddr_un)+1"
893 that is zeroed out before the retrieval.
894 The retrieving call can specify
897 .IR "sizeof(struct sockaddr_un)" ,
898 and the extra zero byte ensures that there will be
899 a null terminator for the string returned in
906 addrlen = sizeof(struct sockaddr_un);
907 addrp = malloc(addrlen + 1);
910 memset(addrp, 0, addrlen + 1);
912 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1)
915 printf("sun_path = %s\en", ((struct sockaddr_un *) addrp)\->sun_path);
919 This sort of messiness can be avoided if it is guaranteed
920 that the applications that
922 pathname sockets follow the rules outlined above under
923 .IR "Pathname sockets" .
925 The following code demonstrates the use of sequenced-packet
926 sockets for local interprocess communication.
927 It consists of two programs.
928 The server program waits for a connection from the client program.
929 The client sends each of its command-line arguments in separate messages.
930 The server treats the incoming messages as integers and adds them up.
931 The client sends the command string "END".
932 The server sends back a message containing the sum of the client's integers.
933 The client prints the sum and exits.
934 The server waits for the next client to connect.
935 To stop the server, the client is called with the command-line argument "DOWN".
937 The following output was recorded while running the server in the background
938 and repeatedly executing the client.
939 Execution of the server program ends when it receives the "DOWN" command.
947 $ \fB./client 11 \-5\fP
949 $ \fB./client DOWN\fP
962 #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
963 #define BUFFER_SIZE 12
972 #include <sys/socket.h>
975 #include "connection.h"
978 main(int argc, char *argv[])
980 struct sockaddr_un name;
983 int connection_socket;
986 char buffer[BUFFER_SIZE];
988 /* Create local socket. */
990 connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
991 if (connection_socket == \-1) {
997 * For portability clear the whole structure, since some
998 * implementations have additional (nonstandard) fields in
1002 memset(&name, 0, sizeof(name));
1004 /* Bind socket to socket name. */
1006 name.sun_family = AF_UNIX;
1007 strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1);
1009 ret = bind(connection_socket, (const struct sockaddr *) &name,
1017 * Prepare for accepting connections. The backlog size is set
1018 * to 20. So while one request is being processed other requests
1022 ret = listen(connection_socket, 20);
1028 /* This is the main loop for handling connections. */
1032 /* Wait for incoming connection. */
1034 data_socket = accept(connection_socket, NULL, NULL);
1035 if (data_socket == \-1) {
1043 /* Wait for next data packet. */
1045 ret = read(data_socket, buffer, sizeof(buffer));
1051 /* Ensure buffer is 0\-terminated. */
1053 buffer[sizeof(buffer) \- 1] = 0;
1055 /* Handle commands. */
1057 if (!strncmp(buffer, "DOWN", sizeof(buffer))) {
1062 if (!strncmp(buffer, "END", sizeof(buffer))) {
1066 /* Add received summand. */
1068 result += atoi(buffer);
1073 sprintf(buffer, "%d", result);
1074 ret = write(data_socket, buffer, sizeof(buffer));
1084 /* Quit on DOWN command. */
1091 close(connection_socket);
1093 /* Unlink the socket. */
1095 unlink(SOCKET_NAME);
1108 #include <sys/socket.h>
1111 #include "connection.h"
1114 main(int argc, char *argv[])
1116 struct sockaddr_un addr;
1119 char buffer[BUFFER_SIZE];
1121 /* Create local socket. */
1123 data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1124 if (data_socket == \-1) {
1130 * For portability clear the whole structure, since some
1131 * implementations have additional (nonstandard) fields in
1135 memset(&addr, 0, sizeof(addr));
1137 /* Connect socket to socket address. */
1139 addr.sun_family = AF_UNIX;
1140 strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1);
1142 ret = connect(data_socket, (const struct sockaddr *) &addr,
1145 fprintf(stderr, "The server is down.\en");
1149 /* Send arguments. */
1151 for (size_t i = 1; i < argc; ++i) {
1152 ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
1159 /* Request result. */
1161 strcpy(buffer, "END");
1162 ret = write(data_socket, buffer, strlen(buffer) + 1);
1168 /* Receive result. */
1170 ret = read(data_socket, buffer, sizeof(buffer));
1176 /* Ensure buffer is 0\-terminated. */
1178 buffer[sizeof(buffer) \- 1] = 0;
1180 printf("Result = %s\en", buffer);
1190 For examples of the use of
1195 .BR seccomp_unotify (2).
1202 .BR capabilities (7),
1203 .BR credentials (7),