]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/unix.7
unix.7: Note the 108 bytes for sun_path is how things are done on Linux
[thirdparty/man-pages.git] / man7 / unix.7
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" and Copyright (C) 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
5 .\" and Copyright (C) 2008, 2012 Michael Kerrisk <mtk.manpages@gmail.com>
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.
10 .\" %%%LICENSE_END
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 .\"
20 .TH UNIX 7 2015-02-21 "Linux" "Linux Programmer's Manual"
21 .SH NAME
22 unix \- sockets for local interprocess communication
23 .SH SYNOPSIS
24 .B #include <sys/socket.h>
25 .br
26 .B #include <sys/un.h>
27
28 .IB unix_socket " = socket(AF_UNIX, type, 0);"
29 .br
30 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
31 .SH DESCRIPTION
32 The
33 .B AF_UNIX
34 (also known as
35 .BR AF_LOCAL )
36 socket family is used to communicate between processes on the same machine
37 efficiently.
38 Traditionally, UNIX domain sockets can be either unnamed,
39 or bound to a filesystem pathname (marked as being of type socket).
40 Linux also supports an abstract namespace which is independent of the
41 filesystem.
42
43 Valid socket types in the UNIX domain are:
44 .BR SOCK_STREAM ,
45 for a stream-oriented socket;
46 .BR SOCK_DGRAM ,
47 for a datagram-oriented socket that preserves message boundaries
48 (as on most UNIX implementations, UNIX domain datagram
49 sockets are always reliable and don't reorder datagrams);
50 and (since Linux 2.6.4)
51 .BR SOCK_SEQPACKET ,
52 for a connection-oriented socket that preserves message boundaries
53 and delivers messages in the order that they were sent.
54
55 UNIX domain sockets support passing file descriptors or process credentials
56 to other processes using ancillary data.
57 .SS Address format
58 A UNIX domain socket address is represented in the following structure:
59 .in +4n
60 .nf
61
62 .\" #define UNIX_PATH_MAX 108
63 .\"
64 struct sockaddr_un {
65 sa_family_t sun_family; /* AF_UNIX */
66 char sun_path[108]; /* pathname */
67 };
68 .fi
69 .in
70 .PP
71 The
72 .I sun_family
73 field always contains
74 .BR AF_UNIX .
75 On Linux
76 .I sun_path
77 is 108 bytes in size; see also NOTES, below.
78
79 Various systems calls (for example,
80 .BR bind (2),
81 .BR connect (2),
82 and
83 .BR sendto (2))
84 take a
85 .I sockaddr_un
86 argument as input.
87 Some other system calls (for example,
88 .BR getsockname (2),
89 .BR getpeername (2),
90 .BR recvfrom (2),
91 and
92 .BR accept (2))
93 return an argument of this type.
94
95 Three types of address are distinguished in the
96 .I sockaddr_un
97 structure:
98 .IP * 3
99 .IR pathname :
100 a UNIX domain socket can be bound to a null-terminated
101 filesystem pathname using
102 .BR bind (2).
103 When the address of a pathname socket is returned
104 (by one of the system calls noted above),
105 its length is
106
107 offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
108
109 and
110 .I sun_path
111 contains the null-terminated pathname.
112 (On Linux, the above
113 .BR offsetof ()
114 expression equates to the same value as
115 .IR sizeof(sa_family_t) ,
116 but some other implementations include other fields before
117 .IR sun_path ,
118 so the
119 .BR offsetof ()
120 expression more portably describes the size of the address structure.)
121 .IP
122 For further details of pathname sockets, see below.
123 .IP *
124 .IR unnamed :
125 A stream socket that has not been bound to a pathname using
126 .BR bind (2)
127 has no name.
128 Likewise, the two sockets created by
129 .BR socketpair (2)
130 are unnamed.
131 When the address of an unnamed socket is returned,
132 its length is
133 .IR "sizeof(sa_family_t)" ,
134 and
135 .I sun_path
136 should not be inspected.
137 .\" There is quite some variation across implementations: FreeBSD
138 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
139 .IP *
140 .IR abstract :
141 an abstract socket address is distinguished (from a pathname socket)
142 by the fact that
143 .IR sun_path[0]
144 is a null byte (\(aq\\0\(aq).
145 The socket's address in this namespace is given by the additional
146 bytes in
147 .IR sun_path
148 that are covered by the specified length of the address structure.
149 (Null bytes in the name have no special significance.)
150 The name has no connection with filesystem pathnames.
151 When the address of an abstract socket is returned,
152 the returned
153 .I addrlen
154 is greater than
155 .IR "sizeof(sa_family_t)"
156 (i.e., greater than 2), and the name of the socket is contained in
157 the first
158 .IR "(addrlen \- sizeof(sa_family_t))"
159 bytes of
160 .IR sun_path .
161 The abstract socket namespace is a nonportable Linux extension.
162 .SS Pathname sockets
163 When binding a socket to a pathname, a few rules should be observed
164 for maximum portability and ease of coding:
165 .IP * 3
166 The pathname in
167 .I sun_path
168 should be null-terminated.
169 .IP *
170 The length of the pathname, including the terminating null byte,
171 should not exceed the size of
172 .IR sun_path .
173 .IP *
174 The
175 .I addrlen
176 argument that describes the enclosing
177 .I sockaddr_un
178 structure should have a value of at least:
179
180 .nf
181 offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
182 .fi
183 .IP
184 or, more simply,
185 .I addrlen
186 can be specified as
187 .IR "sizeof(struct sockaddr_un)" .
188 .PP
189 There is some variation in how implementations handle UNIX domain
190 socket addresses that do not follow the above rules.
191 For example, some (but not all) implementations
192 .\" Linux does this, including for the case where the supplied path
193 .\" is 108 bytes
194 append a null terminator if none is present in the supplied
195 .IR sun_path .
196
197 When coding portable applications,
198 keep in mind that some implementations
199 .\" HP-UX
200 have
201 .I sun_path
202 as short as 92 bytes.
203 .\" Modern BSDs generally have 104, Tru64 and AIX have 104,
204 .\" Solaris and Irix have 108
205
206 Various system calls
207 .RB ( accept (2),
208 .BR recvfrom (2),
209 .BR getsockname (2),
210 .BR getpeername (2))
211 return socket address structures.
212 When applied to UNIX domain sockets, the value-result
213 .I addrlen
214 argument supplied to the call should be initialized as above.
215 Upon return, the argument is set to indicate the
216 .I actual
217 size of the address structure.
218 The caller should check the value returned in this argument:
219 if the output value exceeds the input value,
220 then there is no guarantee that a null terminator is present in
221 .IR sun_path .
222 (See BUGS.)
223 .SS Socket options
224 For historical reasons, these socket options are specified with a
225 .B SOL_SOCKET
226 type even though they are
227 .B AF_UNIX
228 specific.
229 They can be set with
230 .BR setsockopt (2)
231 and read with
232 .BR getsockopt (2)
233 by specifying
234 .B SOL_SOCKET
235 as the socket family.
236 .TP
237 .B SO_PASSCRED
238 Enables the receiving of the credentials of the sending process in an
239 ancillary message.
240 When this option is set and the socket is not yet connected
241 a unique name in the abstract namespace will be generated automatically.
242 Expects an integer boolean flag.
243 .SS Autobind feature
244 If a
245 .BR bind (2)
246 call specifies
247 .I addrlen
248 as
249 .IR sizeof(sa_family_t) ,
250 .\" i.e., sizeof(short)
251 or the
252 .BR SO_PASSCRED
253 socket option was specified for a socket that was
254 not explicitly bound to an address,
255 then the socket is autobound to an abstract address.
256 The address consists of a null byte
257 followed by 5 bytes in the character set
258 .IR [0-9a-f] .
259 Thus, there is a limit of 2^20 autobind addresses.
260 (From Linux 2.1.15, when the autobind feature was added,
261 8 bytes were used, and the limit was thus 2^32 autobind addresses.
262 The change to 5 bytes came in Linux 2.3.15.)
263 .SS Sockets API
264 The following paragraphs describe domain-specific details and
265 unsupported features of the sockets API for UNIX domain sockets on Linux.
266
267 UNIX domain sockets do not support the transmission of
268 out-of-band data (the
269 .B MSG_OOB
270 flag for
271 .BR send (2)
272 and
273 .BR recv (2)).
274
275 The
276 .BR send (2)
277 .B MSG_MORE
278 flag is not supported by UNIX domain sockets.
279
280 The use of
281 .B MSG_TRUNC
282 in the
283 .I flags
284 argument of
285 .BR recv (2)
286 is not supported by UNIX domain sockets.
287
288 The
289 .B SO_SNDBUF
290 socket option does have an effect for UNIX domain sockets, but the
291 .B SO_RCVBUF
292 option does not.
293 For datagram sockets, the
294 .B SO_SNDBUF
295 value imposes an upper limit on the size of outgoing datagrams.
296 This limit is calculated as the doubled (see
297 .BR socket (7))
298 option value less 32 bytes used for overhead.
299 .SS Ancillary messages
300 Ancillary data is sent and received using
301 .BR sendmsg (2)
302 and
303 .BR recvmsg (2).
304 For historical reasons the ancillary message types listed below
305 are specified with a
306 .B SOL_SOCKET
307 type even though they are
308 .B AF_UNIX
309 specific.
310 To send them set the
311 .I cmsg_level
312 field of the struct
313 .I cmsghdr
314 to
315 .B SOL_SOCKET
316 and the
317 .I cmsg_type
318 field to the type.
319 For more information see
320 .BR cmsg (3).
321 .TP
322 .B SCM_RIGHTS
323 Send or receive a set of open file descriptors from another process.
324 The data portion contains an integer array of the file descriptors.
325 The passed file descriptors behave as though they have been created with
326 .BR dup (2).
327 .TP
328 .B SCM_CREDENTIALS
329 Send or receive UNIX credentials.
330 This can be used for authentication.
331 The credentials are passed as a
332 .I struct ucred
333 ancillary message.
334 Thus structure is defined in
335 .I <sys/socket.h>
336 as follows:
337
338 .in +4n
339 .nf
340 struct ucred {
341 pid_t pid; /* process ID of the sending process */
342 uid_t uid; /* user ID of the sending process */
343 gid_t gid; /* group ID of the sending process */
344 };
345 .fi
346 .in
347
348 Since glibc 2.8, the
349 .B _GNU_SOURCE
350 feature test macro must be defined (before including
351 .I any
352 header files) in order to obtain the definition
353 of this structure.
354
355 The credentials which the sender specifies are checked by the kernel.
356 A process with effective user ID 0 is allowed to specify values that do
357 not match its own.
358 The sender must specify its own process ID (unless it has the capability
359 .BR CAP_SYS_ADMIN ),
360 its user ID, effective user ID, or saved set-user-ID (unless it has
361 .BR CAP_SETUID ),
362 and its group ID, effective group ID, or saved set-group-ID
363 (unless it has
364 .BR CAP_SETGID ).
365 To receive a
366 .I struct ucred
367 message the
368 .B SO_PASSCRED
369 option must be enabled on the socket.
370 .SS Ioctls
371 The following
372 .BR ioctl (2)
373 calls return information in
374 .IR value .
375 The correct syntax is:
376 .PP
377 .RS
378 .nf
379 .BI int " value";
380 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
381 .fi
382 .RE
383 .PP
384 .I ioctl_type
385 can be:
386 .TP
387 .B SIOCINQ
388 For
389 .B SOCK_STREAM
390 socket the function returns the amount of queued unread data in the receive buffer.
391 The socket must not be in LISTEN state, otherwise an error
392 .RB ( EINVAL )
393 is returned.
394 .B SIOCINQ
395 is defined in
396 .IR <linux/sockios.h> .
397 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
398 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
399 Alternatively,
400 you can use the synonymous
401 .BR FIONREAD ,
402 defined in
403 .IR <sys/ioctl.h> .
404 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
405 .\" quite what userland might expect. It seems to return the number
406 .\" of bytes allocated for buffers containing pending output.
407 .\" That number is normally larger than the number of bytes of pending
408 .\" output. Since this info is, from userland's point of view, imprecise,
409 .\" and it may well change, probably best not to document this now.
410 For
411 .B SOCK_DGRAM
412 socket,
413 the returned value is the same as
414 for Internet domain datagram socket;
415 see
416 .BR udp (7).
417 .SH ERRORS
418 .TP
419 .B EADDRINUSE
420 The specified local address is already in use or the filesystem socket
421 object already exists.
422 .TP
423 .B ECONNREFUSED
424 The remote address specified by
425 .BR connect (2)
426 was not a listening socket.
427 This error can also occur if the target pathname is not a socket.
428 .TP
429 .B ECONNRESET
430 Remote socket was unexpectedly closed.
431 .TP
432 .B EFAULT
433 User memory address was not valid.
434 .TP
435 .B EINVAL
436 Invalid argument passed.
437 A common cause is that the value
438 .B AF_UNIX
439 was not specified in the
440 .I sun_type
441 field of passed addresses, or the socket was in an
442 invalid state for the applied operation.
443 .TP
444 .B EISCONN
445 .BR connect (2)
446 called on an already connected socket or a target address was
447 specified on a connected socket.
448 .TP
449 .B ENOENT
450 The pathname in the remote address specified to
451 .BR connect (2)
452 did not exist.
453 .TP
454 .B ENOMEM
455 Out of memory.
456 .TP
457 .B ENOTCONN
458 Socket operation needs a target address, but the socket is not connected.
459 .TP
460 .B EOPNOTSUPP
461 Stream operation called on non-stream oriented socket or tried to
462 use the out-of-band data option.
463 .TP
464 .B EPERM
465 The sender passed invalid credentials in the
466 .IR "struct ucred" .
467 .TP
468 .B EPIPE
469 Remote socket was closed on a stream socket.
470 If enabled, a
471 .B SIGPIPE
472 is sent as well.
473 This can be avoided by passing the
474 .B MSG_NOSIGNAL
475 flag to
476 .BR sendmsg (2)
477 or
478 .BR recvmsg (2).
479 .TP
480 .B EPROTONOSUPPORT
481 Passed protocol is not
482 .BR AF_UNIX .
483 .TP
484 .B EPROTOTYPE
485 Remote socket does not match the local socket type
486 .RB ( SOCK_DGRAM
487 versus
488 .BR SOCK_STREAM )
489 .TP
490 .B ESOCKTNOSUPPORT
491 Unknown socket type.
492 .PP
493 Other errors can be generated by the generic socket layer or
494 by the filesystem while generating a filesystem socket object.
495 See the appropriate manual pages for more information.
496 .SH VERSIONS
497 .B SCM_CREDENTIALS
498 and the abstract namespace were introduced with Linux 2.2 and should not
499 be used in portable programs.
500 (Some BSD-derived systems also support credential passing,
501 but the implementation details differ.)
502 .SH NOTES
503 In the Linux implementation, sockets which are visible in the
504 filesystem honor the permissions of the directory they are in.
505 Their owner, group, and permissions can be changed.
506 Creation of a new socket will fail if the process does not have write and
507 search (execute) permission on the directory the socket is created in.
508 Connecting to the socket object requires read/write permission.
509 This behavior differs from many BSD-derived systems which
510 ignore permissions for UNIX domain sockets.
511 Portable programs should not rely on
512 this feature for security.
513
514 Binding to a socket with a filename creates a socket
515 in the filesystem that must be deleted by the caller when it is no
516 longer needed (using
517 .BR unlink (2)).
518 The usual UNIX close-behind semantics apply; the socket can be unlinked
519 at any time and will be finally removed from the filesystem when the last
520 reference to it is closed.
521
522 To pass file descriptors or credentials over a
523 .BR SOCK_STREAM ,
524 you need
525 to send or receive at least one byte of nonancillary data in the same
526 .BR sendmsg (2)
527 or
528 .BR recvmsg (2)
529 call.
530
531 UNIX domain stream sockets do not support the notion of out-of-band data.
532 .\"
533 .SH BUGS
534 When binding a socket to an address,
535 Linux is one of the implementations that appends a null terminator
536 if none is supplied in
537 .IR sun_path .
538 In most cases this is unproblematic:
539 when the socket address is retrieved,
540 it will be one byte longer than that supplied when the socket was bound.
541 However, there is one case where confusing behavior can result:
542 if 108 non-null bytes are supplied when a socket is bound,
543 then the addition of the null terminator takes the length of
544 the pathname beyond
545 .IR sizeof(sun_path) .
546 Consequently, when retrieving the socket address
547 (for example, via
548 .BR accept (2)),
549 .\" The behavior on Solaris is quite similar.
550 if the input
551 .I addrlen
552 argument for the retrieving call is specified as
553 .IR "sizeof(struct sockaddr_un)" ,
554 then the returned address structure
555 .I won't
556 have a null terminator in
557 .IR sun_path .
558
559 In addition, some implementations
560 .\" i.e., traditional BSD
561 don't require a null terminator when binding a socket (the
562 .I addrlen
563 argument is used to determine the length of
564 .IR sun_path )
565 and when the socket address is retrieved on these implementations,
566 there is no null terminator in
567 .IR sun_path .
568
569 Applications that retrieve socket addresses can (portably) code
570 to handle the possibility that there is no null terminator in
571 .IR sun_path
572 by respecting the fact that the number of valid bytes in the pathname is:
573
574 strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path))
575 .\" The following patch to amend kernel behavior was rejected:
576 .\" http://thread.gmane.org/gmane.linux.kernel.api/2437
577 .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path
578 .\" 2012-04-17
579 .\" And there was a related discussion in the Austin list:
580 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735
581 .\" Subject: Having a sun_path with no null terminator
582 .\" 2012-04-18
583 .\"
584 .\" FIXME . Track http://austingroupbugs.net/view.php?id=561
585
586 Alternatively, an application can retrieve
587 the socket address by allocating a buffer of size
588 .I "sizeof(struct sockaddr_un)+1"
589 that is zeroed out before the retrieval.
590 The retrieving call can specify
591 .I addrlen
592 as
593 .IR "sizeof(struct sockaddr_un)" ,
594 and the extra zero byte ensures that there will be
595 a null terminator for the string returned in
596 .IR sun_path :
597
598 .nf
599 .in +3
600 void *addrp;
601
602 addrlen = sizeof(struct sockaddr_un);
603 addrp = malloc(addrlen + 1);
604 if (addrp == NULL)
605 /* Handle error */ ;
606 memset(addrp, 0, addrlen + 1);
607
608 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1)
609 /* handle error */ ;
610
611 printf("sun_path = %s\\n", ((struct sockaddr_un *) addrp)\->sun_path);
612 .in
613 .fi
614
615 This sort of messiness can be avoided if it is guaranteed
616 that the applications that
617 .I create
618 pathname sockets follow the rules outlined above under
619 .IR "Pathname sockets" .
620 .SH EXAMPLE
621 See
622 .BR bind (2).
623
624 For an example of the use of
625 .BR SCM_RIGHTS
626 see
627 .BR cmsg (3).
628 .SH SEE ALSO
629 .BR recvmsg (2),
630 .BR sendmsg (2),
631 .BR socket (2),
632 .BR socketpair (2),
633 .BR cmsg (3),
634 .BR capabilities (7),
635 .BR credentials (7),
636 .BR socket (7),
637 .BR udp (7)