]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/unix.7
unix.7: Fix description of "pathname" sockets
[thirdparty/man-pages.git] / man7 / unix.7
CommitLineData
77117f4f
MK
1.\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2.\" Permission is granted to distribute possibly modified copies
3.\" of this page provided the header is included verbatim,
4.\" and in case of nontrivial modification author and date
5.\" of the modification is added to the header.
6.\"
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.\"
156a0e0d 15.TH UNIX 7 2010-10-10 "Linux" "Linux Programmer's Manual"
77117f4f 16.SH NAME
d4c8c97c 17unix, AF_UNIX, AF_LOCAL \- Sockets for local
77117f4f
MK
18interprocess communication
19.SH SYNOPSIS
20.B #include <sys/socket.h>
21.br
22.B #include <sys/un.h>
23
d4c8c97c 24.IB unix_socket " = socket(AF_UNIX, type, 0);"
77117f4f 25.br
d4c8c97c 26.IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
77117f4f
MK
27.SH DESCRIPTION
28The
d4c8c97c 29.B AF_UNIX
77117f4f 30(also known as
d4c8c97c 31.BR AF_LOCAL )
77117f4f
MK
32socket family is used to communicate between processes on the same machine
33efficiently.
34Traditionally, Unix sockets can be either unnamed,
35or bound to a file system pathname (marked as being of type socket).
36Linux also supports an abstract namespace which is independent of the
37file system.
38
39Valid types are:
40.BR SOCK_STREAM ,
41for a stream-oriented socket and
42.BR SOCK_DGRAM ,
43for a datagram-oriented socket that preserves message boundaries
44(as on most Unix implementations, Unix domain datagram
45sockets are always reliable and don't reorder datagrams);
46and (since Linux 2.6.4)
47.BR SOCK_SEQPACKET ,
48for a connection-oriented socket that preserves message boundaries
49and delivers messages in the order that they were sent.
50
51Unix sockets support passing file descriptors or process credentials
52to other processes using ancillary data.
53.SS Address Format
54A Unix domain socket address is represented in the following structure:
55.in +4n
56.nf
57
58#define UNIX_PATH_MAX 108
59
60struct sockaddr_un {
61 sa_family_t sun_family; /* AF_UNIX */
62 char sun_path[UNIX_PATH_MAX]; /* pathname */
63};
64.fi
65.in
66.PP
67.I sun_family
68always contains
69.BR AF_UNIX .
70
71Three types of address are distinguished in this structure:
72.IP * 3
73.IR pathname :
74a Unix domain socket can be bound to a null-terminated file
75system pathname using
76.BR bind (2).
77When the address of the socket is returned by
78.BR getsockname (2),
79.BR getpeername (2),
80and
81.BR accept (2),
82its length is
6ed236d5 83.IR "offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1" ,
77117f4f
MK
84and
85.I sun_path
86contains the null-terminated pathname.
87.IP *
88.IR unnamed :
89A stream socket that has not been bound to a pathname using
90.BR bind (2)
91has no name.
92Likewise, the two sockets created by
93.BR socketpair (2)
94are unnamed.
95When the address of an unnamed socket is returned by
96.BR getsockname (2),
97.BR getpeername (2),
98and
99.BR accept (2),
100its length is
101.IR "sizeof(sa_family_t)" ,
102and
103.I sun_path
104should not be inspected.
105.\" There is quite some variation across implementations: FreeBSD
106.\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
107.IP *
108.IR abstract :
109an abstract socket address is distinguished by the fact that
110.IR sun_path[0]
111is a null byte ('\\0').
156a0e0d
MK
112The socket's address in this namespace is given by the additional
113bytes in
114.IR sun_path
115that are covered by the specified length of the address structure.
77117f4f
MK
116(Null bytes in the name have no special significance.)
117The name has no connection with file system pathnames.
77117f4f
MK
118When the address of an abstract socket is returned by
119.BR getsockname (2),
120.BR getpeername (2),
121and
122.BR accept (2),
156a0e0d
MK
123the returned
124.I addrlen
125is greater than
126.IR "sizeof(sa_family_t)"
127(i.e., greater than 2), and the name of the socket is contained in
128the first
129.IR "(addrlen \- sizeof(sa_family_t))"
130bytes of
131.IR sun_path .
d603cc27 132The abstract socket namespace is a nonportable Linux extension.
77117f4f
MK
133.SS Socket Options
134For historical reasons these socket options are specified with a
135.B SOL_SOCKET
136type even though they are
d4c8c97c 137.B AF_UNIX
77117f4f
MK
138specific.
139They can be set with
140.BR setsockopt (2)
141and read with
142.BR getsockopt (2)
143by specifying
144.B SOL_SOCKET
145as the socket family.
146.TP
147.B SO_PASSCRED
6074c3e6 148Enables the receiving of the credentials of the sending process in an
77117f4f
MK
149ancillary message.
150When this option is set and the socket is not yet connected
151a unique name in the abstract namespace will be generated automatically.
152Expects an integer boolean flag.
19e19f5f 153.SS Sockets API
77117f4f
MK
154The following paragraphs describe domain-specific details and
155unsupported features of the sockets API for Unix domain sockets on Linux.
156
157Unix domain sockets do not support the transmission of
158out-of-band data (the
159.B MSG_OOB
160flag for
161.BR send (2)
162and
163.BR recv (2)).
164
165The
166.BR send (2)
167.B MSG_MORE
168flag is not supported by Unix domain sockets.
169
77e75b90
MK
170The use of
171.B MSG_TRUNC
172in the
173.I flags
174argument of
175.BR recv (2)
176is not supported by Unix domain sockets.
177
77117f4f
MK
178The
179.B SO_SNDBUF
180socket option does have an effect for Unix domain sockets, but the
181.B SO_RCVBUF
182option does not.
183For datagram sockets, the
184.B SO_SNDBUF
185value imposes an upper limit on the size of outgoing datagrams.
186This limit is calculated as the doubled (see
187.BR socket (7))
188option value less 32 bytes used for overhead.
189.SS Ancillary Messages
190Ancillary data is sent and received using
191.BR sendmsg (2)
192and
193.BR recvmsg (2).
194For historical reasons the ancillary message types listed below
195are specified with a
196.B SOL_SOCKET
197type even though they are
d4c8c97c 198.B AF_UNIX
77117f4f
MK
199specific.
200To send them set the
201.I cmsg_level
202field of the struct
203.I cmsghdr
204to
205.B SOL_SOCKET
206and the
207.I cmsg_type
208field to the type.
209For more information see
210.BR cmsg (3).
211.TP
212.B SCM_RIGHTS
213Send or receive a set of open file descriptors from another process.
214The data portion contains an integer array of the file descriptors.
215The passed file descriptors behave as though they have been created with
216.BR dup (2).
217.TP
218.B SCM_CREDENTIALS
219Send or receive Unix credentials.
220This can be used for authentication.
221The credentials are passed as a
222.I struct ucred
223ancillary message.
b1587ca8
MK
224Thus structure is defined in
225.I <sys/socket.h>
226as follows:
77117f4f
MK
227
228.in +4n
229.nf
230struct ucred {
231 pid_t pid; /* process ID of the sending process */
232 uid_t uid; /* user ID of the sending process */
233 gid_t gid; /* group ID of the sending process */
234};
235.fi
236.in
237
b1587ca8 238Since glibc 2.8, the
1bc510f5 239.B _GNU_SOURCE
e417acb0
MK
240feature test macro must be defined (before including
241.I any
242header files) in order to obtain the definition
b1587ca8
MK
243of this structure.
244
77117f4f
MK
245The credentials which the sender specifies are checked by the kernel.
246A process with effective user ID 0 is allowed to specify values that do
247not match its own.
248The sender must specify its own process ID (unless it has the capability
249.BR CAP_SYS_ADMIN ),
250its user ID, effective user ID, or saved set-user-ID (unless it has
251.BR CAP_SETUID ),
252and its group ID, effective group ID, or saved set-group-ID
253(unless it has
254.BR CAP_SETGID ).
255To receive a
256.I struct ucred
257message the
258.B SO_PASSCRED
259option must be enabled on the socket.
fbea0f81
MK
260.SS Ioctls
261The following
262.BR ioctl (2)
263calls return information in
264.IR value .
265The correct syntax is:
266.PP
267.RS
268.nf
269.BI int " value";
270.IB error " = ioctl(" tcp_socket ", " ioctl_type ", &" value ");"
271.fi
272.RE
273.PP
274.I ioctl_type
275can be:
276.TP
277.B SIOCINQ
278Returns the amount of queued unread data in the receive buffer.
279The socket must not be in LISTEN state, otherwise an error
280.RB ( EINVAL )
281is returned.
282.B SIOCINQ
283is defined in
284.IR <linux/sockios.h> .
285.\" FIXME http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
286.\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
287Alternatively,
288you can use the synonymous
289.BR FIONREAD ,
290defined in
291.IR <sys/ioctl.h> .
7aed61d9 292.\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
fbea0f81
MK
293.\" quite what userland might expect. It seems to return the number
294.\" of bytes allocated for buffers containing pending output.
295.\" That number is normally larger than the number of bytes of pending
296.\" output. Since this info is, from userland's point of view, imprecise,
297.\" and it may well change, probably best not to document this now.
77117f4f
MK
298.SH ERRORS
299.TP
300.B EADDRINUSE
301Selected local address is already taken or file system socket
302object already exists.
303.TP
304.B ECONNREFUSED
305.BR connect (2)
306called with a socket object that isn't listening.
307This can happen when
308the remote socket does not exist or the filename is not a socket.
309.TP
310.B ECONNRESET
311Remote socket was unexpectedly closed.
312.TP
313.B EFAULT
314User memory address was not valid.
315.TP
316.B EINVAL
317Invalid argument passed.
318A common cause is the missing setting of AF_UNIX
319in the
320.I sun_type
321field of passed addresses or the socket being in an
322invalid state for the applied operation.
323.TP
324.B EISCONN
325.BR connect (2)
326called on an already connected socket or a target address was
327specified on a connected socket.
328.TP
329.B ENOMEM
330Out of memory.
331.TP
332.B ENOTCONN
333Socket operation needs a target address, but the socket is not connected.
334.TP
335.B EOPNOTSUPP
336Stream operation called on non-stream oriented socket or tried to
337use the out-of-band data option.
338.TP
339.B EPERM
340The sender passed invalid credentials in the
341.IR "struct ucred" .
342.TP
343.B EPIPE
344Remote socket was closed on a stream socket.
345If enabled, a
346.B SIGPIPE
347is sent as well.
348This can be avoided by passing the
349.B MSG_NOSIGNAL
350flag to
351.BR sendmsg (2)
352or
353.BR recvmsg (2).
354.TP
355.B EPROTONOSUPPORT
d4c8c97c 356Passed protocol is not AF_UNIX.
77117f4f
MK
357.TP
358.B EPROTOTYPE
359Remote socket does not match the local socket type
360.RB ( SOCK_DGRAM
361vs.
362.BR SOCK_STREAM )
363.TP
364.B ESOCKTNOSUPPORT
365Unknown socket type.
366.PP
367Other errors can be generated by the generic socket layer or
368by the file system while generating a file system socket object.
369See the appropriate manual pages for more information.
370.SH VERSIONS
371.B SCM_CREDENTIALS
372and the abstract namespace were introduced with Linux 2.2 and should not
373be used in portable programs.
374(Some BSD-derived systems also support credential passing,
375but the implementation details differ.)
376.SH NOTES
377In the Linux implementation, sockets which are visible in the
378file system honor the permissions of the directory they are in.
379Their owner, group and their permissions can be changed.
380Creation of a new socket will fail if the process does not have write and
381search (execute) permission on the directory the socket is created in.
382Connecting to the socket object requires read/write permission.
383This behavior differs from many BSD-derived systems which
384ignore permissions for Unix sockets.
385Portable programs should not rely on
386this feature for security.
387
388Binding to a socket with a filename creates a socket
389in the file system that must be deleted by the caller when it is no
390longer needed (using
391.BR unlink (2)).
392The usual Unix close-behind semantics apply; the socket can be unlinked
393at any time and will be finally removed from the file system when the last
394reference to it is closed.
395
396To pass file descriptors or credentials over a
397.BR SOCK_STREAM ,
398you need
24b74457 399to send or receive at least one byte of nonancillary data in the same
77117f4f
MK
400.BR sendmsg (2)
401or
402.BR recvmsg (2)
403call.
404
405Unix domain stream sockets do not support the notion of out-of-band data.
406.SH EXAMPLE
407See
408.BR bind (2).
409.SH "SEE ALSO"
410.BR recvmsg (2),
411.BR sendmsg (2),
412.BR socket (2),
413.BR socketpair (2),
414.BR cmsg (3),
415.BR capabilities (7),
416.BR credentials (7),
417.BR socket (7)