1UNIX(7) Linux Programmer's Manual UNIX(7)
2
3
4
6 unix - sockets for local interprocess communication
7
9 #include <sys/socket.h>
10 #include <sys/un.h>
11
12 unix_socket = socket(AF_UNIX, type, 0);
13 error = socketpair(AF_UNIX, type, 0, int *sv);
14
16 The AF_UNIX (also known as AF_LOCAL) socket family is used to communi‐
17 cate between processes on the same machine efficiently. Traditionally,
18 UNIX domain sockets can be either unnamed, or bound to a filesystem
19 pathname (marked as being of type socket). Linux also supports an
20 abstract namespace which is independent of the filesystem.
21
22 Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-
23 oriented socket; SOCK_DGRAM, for a datagram-oriented socket that pre‐
24 serves message boundaries (as on most UNIX implementations, UNIX domain
25 datagram sockets are always reliable and don't reorder datagrams); and
26 (since Linux 2.6.4) SOCK_SEQPACKET, for a sequenced-packet socket that
27 is connection-oriented, preserves message boundaries, and delivers mes‐
28 sages in the order that they were sent.
29
30 UNIX domain sockets support passing file descriptors or process creden‐
31 tials to other processes using ancillary data.
32
33 Address format
34 A UNIX domain socket address is represented in the following structure:
35
36 struct sockaddr_un {
37 sa_family_t sun_family; /* AF_UNIX */
38 char sun_path[108]; /* pathname */
39 };
40
41 The sun_family field always contains AF_UNIX. On Linux sun_path is 108
42 bytes in size; see also NOTES, below.
43
44 Various systems calls (for example, bind(2), connect(2), and sendto(2))
45 take a sockaddr_un argument as input. Some other system calls (for
46 example, getsockname(2), getpeername(2), recvfrom(2), and accept(2))
47 return an argument of this type.
48
49 Three types of address are distinguished in the sockaddr_un structure:
50
51 * pathname: a UNIX domain socket can be bound to a null-terminated
52 filesystem pathname using bind(2). When the address of a pathname
53 socket is returned (by one of the system calls noted above), its
54 length is
55
56 offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
57
58 and sun_path contains the null-terminated pathname. (On Linux, the
59 above offsetof() expression equates to the same value as
60 sizeof(sa_family_t), but some other implementations include other
61 fields before sun_path, so the offsetof() expression more portably
62 describes the size of the address structure.)
63
64 For further details of pathname sockets, see below.
65
66 * unnamed: A stream socket that has not been bound to a pathname using
67 bind(2) has no name. Likewise, the two sockets created by socket‐
68 pair(2) are unnamed. When the address of an unnamed socket is
69 returned, its length is sizeof(sa_family_t), and sun_path should not
70 be inspected.
71
72 * abstract: an abstract socket address is distinguished (from a path‐
73 name socket) by the fact that sun_path[0] is a null byte ('\0').
74 The socket's address in this namespace is given by the additional
75 bytes in sun_path that are covered by the specified length of the
76 address structure. (Null bytes in the name have no special signifi‐
77 cance.) The name has no connection with filesystem pathnames. When
78 the address of an abstract socket is returned, the returned addrlen
79 is greater than sizeof(sa_family_t) (i.e., greater than 2), and the
80 name of the socket is contained in the first (addrlen -
81 sizeof(sa_family_t)) bytes of sun_path.
82
83 Pathname sockets
84 When binding a socket to a pathname, a few rules should be observed for
85 maximum portability and ease of coding:
86
87 * The pathname in sun_path should be null-terminated.
88
89 * The length of the pathname, including the terminating null byte,
90 should not exceed the size of sun_path.
91
92 * The addrlen argument that describes the enclosing sockaddr_un struc‐
93 ture should have a value of at least:
94
95 offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
96
97 or, more simply, addrlen can be specified as sizeof(struct sock‐
98 addr_un).
99
100 There is some variation in how implementations handle UNIX domain
101 socket addresses that do not follow the above rules. For example, some
102 (but not all) implementations append a null terminator if none is
103 present in the supplied sun_path.
104
105 When coding portable applications, keep in mind that some implementa‐
106 tions have sun_path as short as 92 bytes.
107
108 Various system calls (accept(2), recvfrom(2), getsockname(2), getpeer‐
109 name(2)) return socket address structures. When applied to UNIX domain
110 sockets, the value-result addrlen argument supplied to the call should
111 be initialized as above. Upon return, the argument is set to indicate
112 the actual size of the address structure. The caller should check the
113 value returned in this argument: if the output value exceeds the input
114 value, then there is no guarantee that a null terminator is present in
115 sun_path. (See BUGS.)
116
117 Pathname socket ownership and permissions
118 In the Linux implementation, pathname sockets honor the permissions of
119 the directory they are in. Creation of a new socket fails if the
120 process does not have write and search (execute) permission on the
121 directory in which the socket is created.
122
123 On Linux, connecting to a stream socket object requires write permis‐
124 sion on that socket; sending a datagram to a datagram socket likewise
125 requires write permission on that socket. POSIX does not make any
126 statement about the effect of the permissions on a socket file, and on
127 some systems (e.g., older BSDs), the socket permissions are ignored.
128 Portable programs should not rely on this feature for security.
129
130 When creating a new socket, the owner and group of the socket file are
131 set according to the usual rules. The socket file has all permissions
132 enabled, other than those that are turned off by the process umask(2).
133
134 The owner, group, and permissions of a pathname socket can be changed
135 (using chown(2) and chmod(2)).
136
137 Abstract sockets
138 Socket permissions have no meaning for abstract sockets: the process
139 umask(2) has no effect when binding an abstract socket, and changing
140 the ownership and permissions of the object (via fchown(2) and fch‐
141 mod(2)) has no effect on the accessibility of the socket.
142
143 Abstract sockets automatically disappear when all open references to
144 the socket are closed.
145
146 The abstract socket namespace is a nonportable Linux extension.
147
148 Socket options
149 For historical reasons, these socket options are specified with a
150 SOL_SOCKET type even though they are AF_UNIX specific. They can be set
151 with setsockopt(2) and read with getsockopt(2) by specifying SOL_SOCKET
152 as the socket family.
153
154 SO_PASSCRED
155 Enables the receiving of the credentials of the sending process
156 in an ancillary message. When this option is set and the socket
157 is not yet connected a unique name in the abstract namespace
158 will be generated automatically. Expects an integer boolean
159 flag.
160
161 Autobind feature
162 If a bind(2) call specifies addrlen as sizeof(sa_family_t), or the
163 SO_PASSCRED socket option was specified for a socket that was not
164 explicitly bound to an address, then the socket is autobound to an
165 abstract address. The address consists of a null byte followed by 5
166 bytes in the character set [0-9a-f]. Thus, there is a limit of 2^20
167 autobind addresses. (From Linux 2.1.15, when the autobind feature was
168 added, 8 bytes were used, and the limit was thus 2^32 autobind
169 addresses. The change to 5 bytes came in Linux 2.3.15.)
170
171 Sockets API
172 The following paragraphs describe domain-specific details and unsup‐
173 ported features of the sockets API for UNIX domain sockets on Linux.
174
175 UNIX domain sockets do not support the transmission of out-of-band data
176 (the MSG_OOB flag for send(2) and recv(2)).
177
178 The send(2) MSG_MORE flag is not supported by UNIX domain sockets.
179
180 Before Linux 3.4, the use of MSG_TRUNC in the flags argument of recv(2)
181 was not supported by UNIX domain sockets.
182
183 The SO_SNDBUF socket option does have an effect for UNIX domain sock‐
184 ets, but the SO_RCVBUF option does not. For datagram sockets, the
185 SO_SNDBUF value imposes an upper limit on the size of outgoing data‐
186 grams. This limit is calculated as the doubled (see socket(7)) option
187 value less 32 bytes used for overhead.
188
189 Ancillary messages
190 Ancillary data is sent and received using sendmsg(2) and recvmsg(2).
191 For historical reasons the ancillary message types listed below are
192 specified with a SOL_SOCKET type even though they are AF_UNIX specific.
193 To send them set the cmsg_level field of the struct cmsghdr to
194 SOL_SOCKET and the cmsg_type field to the type. For more information
195 see cmsg(3).
196
197 SCM_RIGHTS
198 Send or receive a set of open file descriptors from another
199 process. The data portion contains an integer array of the file
200 descriptors. The passed file descriptors behave as though they
201 have been created with dup(2).
202
203 SCM_CREDENTIALS
204 Send or receive UNIX credentials. This can be used for authen‐
205 tication. The credentials are passed as a struct ucred ancil‐
206 lary message. Thus structure is defined in <sys/socket.h> as
207 follows:
208
209 struct ucred {
210 pid_t pid; /* process ID of the sending process */
211 uid_t uid; /* user ID of the sending process */
212 gid_t gid; /* group ID of the sending process */
213 };
214
215 Since glibc 2.8, the _GNU_SOURCE feature test macro must be
216 defined (before including any header files) in order to obtain
217 the definition of this structure.
218
219 The credentials which the sender specifies are checked by the
220 kernel. A process with effective user ID 0 is allowed to spec‐
221 ify values that do not match its own. The sender must specify
222 its own process ID (unless it has the capability CAP_SYS_ADMIN),
223 its real user ID, effective user ID, or saved set-user-ID
224 (unless it has CAP_SETUID), and its real group ID, effective
225 group ID, or saved set-group-ID (unless it has CAP_SETGID). To
226 receive a struct ucred message the SO_PASSCRED option must be
227 enabled on the socket.
228
229 Ioctls
230 The following ioctl(2) calls return information in value. The correct
231 syntax is:
232
233 int value;
234 error = ioctl(unix_socket, ioctl_type, &value);
235
236 ioctl_type can be:
237
238 SIOCINQ
239 For SOCK_STREAM socket the function returns the amount of queued
240 unread data in the receive buffer. The socket must not be in
241 LISTEN state, otherwise an error (EINVAL) is returned. SIOCINQ
242 is defined in <linux/sockios.h>. Alternatively, you can use the
243 synonymous FIONREAD, defined in <sys/ioctl.h>. For SOCK_DGRAM
244 socket, the returned value is the same as for Internet domain
245 datagram socket; see udp(7).
246
248 EADDRINUSE
249 The specified local address is already in use or the filesystem
250 socket object already exists.
251
252 EBADF This error can occur for sendmsg(2) when sending a file descrip‐
253 tor as ancillary data over a UNIX domain socket (see the
254 description of SCM_RIGHTS, above), and indicates that the file
255 descriptor number that is being sent is not valid (e.g., it is
256 not an open file descriptor).
257
258 ECONNREFUSED
259 The remote address specified by connect(2) was not a listening
260 socket. This error can also occur if the target pathname is not
261 a socket.
262
263 ECONNRESET
264 Remote socket was unexpectedly closed.
265
266 EFAULT User memory address was not valid.
267
268 EINVAL Invalid argument passed. A common cause is that the value
269 AF_UNIX was not specified in the sun_type field of passed
270 addresses, or the socket was in an invalid state for the applied
271 operation.
272
273 EISCONN
274 connect(2) called on an already connected socket or a target
275 address was specified on a connected socket.
276
277 ENOENT The pathname in the remote address specified to connect(2) did
278 not exist.
279
280 ENOMEM Out of memory.
281
282 ENOTCONN
283 Socket operation needs a target address, but the socket is not
284 connected.
285
286 EOPNOTSUPP
287 Stream operation called on non-stream oriented socket or tried
288 to use the out-of-band data option.
289
290 EPERM The sender passed invalid credentials in the struct ucred.
291
292 EPIPE Remote socket was closed on a stream socket. If enabled, a SIG‐
293 PIPE is sent as well. This can be avoided by passing the
294 MSG_NOSIGNAL flag to send(2) or sendmsg(2).
295
296 EPROTONOSUPPORT
297 Passed protocol is not AF_UNIX.
298
299 EPROTOTYPE
300 Remote socket does not match the local socket type (SOCK_DGRAM
301 versus SOCK_STREAM).
302
303 ESOCKTNOSUPPORT
304 Unknown socket type.
305
306 ETOOMANYREFS
307 This error can occur for sendmsg(2) when sending a file descrip‐
308 tor as ancillary data over a UNIX domain socket (see the
309 description of SCM_RIGHTS, above). It occurs if the number of
310 "in-flight" file descriptors exceeds the RLIMIT_NOFILE resource
311 limit and the caller does not have the CAP_SYS_RESOURCE capabil‐
312 ity. An in-flight file descriptor is one that has been sent
313 using sendmsg(2) but has not yet been accepted in the recipient
314 process using recvmsg(2).
315
316 This error is diagnosed since mainline Linux 4.5 (and in some
317 earlier kernel versions where the fix has been backported). In
318 earlier kernel versions, it was possible to place an unlimited
319 number of file descriptors in flight, by sending each file
320 descriptor with sendmsg(2) and then closing the file descriptor
321 so that it was not accounted against the RLIMIT_NOFILE resource
322 limit.
323
324 Other errors can be generated by the generic socket layer or by the
325 filesystem while generating a filesystem socket object. See the appro‐
326 priate manual pages for more information.
327
329 SCM_CREDENTIALS and the abstract namespace were introduced with Linux
330 2.2 and should not be used in portable programs. (Some BSD-derived
331 systems also support credential passing, but the implementation details
332 differ.)
333
335 Binding to a socket with a filename creates a socket in the filesystem
336 that must be deleted by the caller when it is no longer needed (using
337 unlink(2)). The usual UNIX close-behind semantics apply; the socket
338 can be unlinked at any time and will be finally removed from the
339 filesystem when the last reference to it is closed.
340
341 To pass file descriptors or credentials over a SOCK_STREAM socket, you
342 must to send or receive at least one byte of nonancillary data in the
343 same sendmsg(2) or recvmsg(2) call.
344
345 UNIX domain stream sockets do not support the notion of out-of-band
346 data.
347
349 When binding a socket to an address, Linux is one of the implementa‐
350 tions that appends a null terminator if none is supplied in sun_path.
351 In most cases this is unproblematic: when the socket address is
352 retrieved, it will be one byte longer than that supplied when the
353 socket was bound. However, there is one case where confusing behavior
354 can result: if 108 non-null bytes are supplied when a socket is bound,
355 then the addition of the null terminator takes the length of the path‐
356 name beyond sizeof(sun_path). Consequently, when retrieving the socket
357 address (for example, via accept(2)), if the input addrlen argument for
358 the retrieving call is specified as sizeof(struct sockaddr_un), then
359 the returned address structure won't have a null terminator in
360 sun_path.
361
362 In addition, some implementations don't require a null terminator when
363 binding a socket (the addrlen argument is used to determine the length
364 of sun_path) and when the socket address is retrieved on these imple‐
365 mentations, there is no null terminator in sun_path.
366
367 Applications that retrieve socket addresses can (portably) code to han‐
368 dle the possibility that there is no null terminator in sun_path by
369 respecting the fact that the number of valid bytes in the pathname is:
370
371 strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path))
372
373 Alternatively, an application can retrieve the socket address by allo‐
374 cating a buffer of size sizeof(struct sockaddr_un)+1 that is zeroed out
375 before the retrieval. The retrieving call can specify addrlen as
376 sizeof(struct sockaddr_un), and the extra zero byte ensures that there
377 will be a null terminator for the string returned in sun_path:
378
379 void *addrp;
380
381 addrlen = sizeof(struct sockaddr_un);
382 addrp = malloc(addrlen + 1);
383 if (addrp == NULL)
384 /* Handle error */ ;
385 memset(addrp, 0, addrlen + 1);
386
387 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == -1)
388 /* handle error */ ;
389
390 printf("sun_path = %s\n", ((struct sockaddr_un *) addrp)->sun_path);
391
392 This sort of messiness can be avoided if it is guaranteed that the
393 applications that create pathname sockets follow the rules outlined
394 above under Pathname sockets.
395
397 The following code demonstrates the use of sequenced-packet sockets for
398 local interprocess communication. It consists of two programs. The
399 server program waits for a connection from the client program. The
400 client sends each of its command-line arguments in separate messages.
401 The server treats the incoming messages as integers and adds them up.
402 The client sends the command string "END". The server sends back a
403 message containing the sum of the client's integers. The client prints
404 the sum and exits. The server waits for the next client to connect.
405 To stop the server, the client is called with the command-line argument
406 "DOWN".
407
408 The following output was recorded while running the server in the back‐
409 ground and repeatedly executing the client. Execution of the server
410 program ends when it receives the "DOWN" command.
411
412 Example output
413 $ ./server &
414 [1] 25887
415 $ ./client 3 4
416 Result = 7
417 $ ./client 11 -5
418 Result = 6
419 $ ./client DOWN
420 Result = 0
421 [1]+ Done ./server
422 $
423
424 Program source
425
426 /*
427 * File connection.h
428 */
429
430 #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
431 #define BUFFER_SIZE 12
432
433 /*
434 * File server.c
435 */
436
437 #include <stdio.h>
438 #include <stdlib.h>
439 #include <string.h>
440 #include <sys/socket.h>
441 #include <sys/un.h>
442 #include <unistd.h>
443 #include "connection.h"
444
445 int
446 main(int argc, char *argv[])
447 {
448 struct sockaddr_un name;
449 int down_flag = 0;
450 int ret;
451 int connection_socket;
452 int data_socket;
453 int result;
454 char buffer[BUFFER_SIZE];
455
456 /*
457 * In case the program exited inadvertently on the last run,
458 * remove the socket.
459 */
460
461 unlink(SOCKET_NAME);
462
463 /* Create local socket. */
464
465 connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
466 if (connection_socket == -1) {
467 perror("socket");
468 exit(EXIT_FAILURE);
469 }
470
471 /*
472 * For portability clear the whole structure, since some
473 * implementations have additional (nonstandard) fields in
474 * the structure.
475 */
476
477 memset(&name, 0, sizeof(struct sockaddr_un));
478
479 /* Bind socket to socket name. */
480
481 name.sun_family = AF_UNIX;
482 strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1);
483
484 ret = bind(connection_socket, (const struct sockaddr *) &name,
485 sizeof(struct sockaddr_un));
486 if (ret == -1) {
487 perror("bind");
488 exit(EXIT_FAILURE);
489 }
490
491 /*
492 * Prepare for accepting connections. The backlog size is set
493 * to 20. So while one request is being processed other requests
494 * can be waiting.
495 */
496
497 ret = listen(connection_socket, 20);
498 if (ret == -1) {
499 perror("listen");
500 exit(EXIT_FAILURE);
501 }
502
503 /* This is the main loop for handling connections. */
504
505 for (;;) {
506
507 /* Wait for incoming connection. */
508
509 data_socket = accept(connection_socket, NULL, NULL);
510 if (data_socket == -1) {
511 perror("accept");
512 exit(EXIT_FAILURE);
513 }
514
515 result = 0;
516 for(;;) {
517
518 /* Wait for next data packet. */
519
520 ret = read(data_socket, buffer, BUFFER_SIZE);
521 if (ret == -1) {
522 perror("read");
523 exit(EXIT_FAILURE);
524 }
525
526 /* Ensure buffer is 0-terminated. */
527
528 buffer[BUFFER_SIZE - 1] = 0;
529
530 /* Handle commands. */
531
532 if (!strncmp(buffer, "DOWN", BUFFER_SIZE)) {
533 down_flag = 1;
534 break;
535 }
536
537 if (!strncmp(buffer, "END", BUFFER_SIZE)) {
538 break;
539 }
540
541 /* Add received summand. */
542
543 result += atoi(buffer);
544 }
545
546 /* Send result. */
547
548 sprintf(buffer, "%d", result);
549 ret = write(data_socket, buffer, BUFFER_SIZE);
550
551 if (ret == -1) {
552 perror("write");
553 exit(EXIT_FAILURE);
554 }
555
556 /* Close socket. */
557
558 close(data_socket);
559
560 /* Quit on DOWN command. */
561
562 if (down_flag) {
563 break;
564 }
565 }
566
567 close(connection_socket);
568
569 /* Unlink the socket. */
570
571 unlink(SOCKET_NAME);
572
573 exit(EXIT_SUCCESS);
574 }
575
576 /*
577 * File client.c
578 */
579
580 #include <errno.h>
581 #include <stdio.h>
582 #include <stdlib.h>
583 #include <string.h>
584 #include <sys/socket.h>
585 #include <sys/un.h>
586 #include <unistd.h>
587 #include "connection.h"
588
589 int
590 main(int argc, char *argv[])
591 {
592 struct sockaddr_un addr;
593 int i;
594 int ret;
595 int data_socket;
596 char buffer[BUFFER_SIZE];
597
598 /* Create local socket. */
599
600 data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
601 if (data_socket == -1) {
602 perror("socket");
603 exit(EXIT_FAILURE);
604 }
605
606 /*
607 * For portability clear the whole structure, since some
608 * implementations have additional (nonstandard) fields in
609 * the structure.
610 */
611
612 memset(&addr, 0, sizeof(struct sockaddr_un));
613
614 /* Connect socket to socket address */
615
616 addr.sun_family = AF_UNIX;
617 strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1);
618
619 ret = connect (data_socket, (const struct sockaddr *) &addr,
620 sizeof(struct sockaddr_un));
621 if (ret == -1) {
622 fprintf(stderr, "The server is down.\n");
623 exit(EXIT_FAILURE);
624 }
625
626 /* Send arguments. */
627
628 for (i = 1; i < argc; ++i) {
629 ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
630 if (ret == -1) {
631 perror("write");
632 break;
633 }
634 }
635
636 /* Request result. */
637
638 strcpy (buffer, "END");
639 ret = write(data_socket, buffer, strlen(buffer) + 1);
640 if (ret == -1) {
641 perror("write");
642 exit(EXIT_FAILURE);
643 }
644
645 /* Receive result. */
646
647 ret = read(data_socket, buffer, BUFFER_SIZE);
648 if (ret == -1) {
649 perror("read");
650 exit(EXIT_FAILURE);
651 }
652
653 /* Ensure buffer is 0-terminated. */
654
655 buffer[BUFFER_SIZE - 1] = 0;
656
657 printf("Result = %s\n", buffer);
658
659 /* Close socket. */
660
661 close(data_socket);
662
663 exit(EXIT_SUCCESS);
664 }
665
666 For an example of the use of SCM_RIGHTS see cmsg(3).
667
669 recvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3), capabili‐
670 ties(7), credentials(7), socket(7), udp(7)
671
673 This page is part of release 4.16 of the Linux man-pages project. A
674 description of the project, information about reporting bugs, and the
675 latest version of this page, can be found at
676 https://www.kernel.org/doc/man-pages/.
677
678
679
680Linux 2018-04-30 UNIX(7)