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