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