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 ECONNREFUSED
253 The remote address specified by connect(2) was not a listening
254 socket. This error can also occur if the target pathname is not
255 a socket.
256
257 ECONNRESET
258 Remote socket was unexpectedly closed.
259
260 EFAULT User memory address was not valid.
261
262 EINVAL Invalid argument passed. A common cause is that the value
263 AF_UNIX was not specified in the sun_type field of passed
264 addresses, or the socket was in an invalid state for the applied
265 operation.
266
267 EISCONN
268 connect(2) called on an already connected socket or a target
269 address was specified on a connected socket.
270
271 ENOENT The pathname in the remote address specified to connect(2) did
272 not exist.
273
274 ENOMEM Out of memory.
275
276 ENOTCONN
277 Socket operation needs a target address, but the socket is not
278 connected.
279
280 EOPNOTSUPP
281 Stream operation called on non-stream oriented socket or tried
282 to use the out-of-band data option.
283
284 EPERM The sender passed invalid credentials in the struct ucred.
285
286 EPIPE Remote socket was closed on a stream socket. If enabled, a SIG‐
287 PIPE is sent as well. This can be avoided by passing the
288 MSG_NOSIGNAL flag to send(2) or sendmsg(2).
289
290 EPROTONOSUPPORT
291 Passed protocol is not AF_UNIX.
292
293 EPROTOTYPE
294 Remote socket does not match the local socket type (SOCK_DGRAM
295 versus SOCK_STREAM).
296
297 ESOCKTNOSUPPORT
298 Unknown socket type.
299
300 ETOOMANYREFS
301 This error can occur for sendmsg(2) when sending a file descrip‐
302 tor as ancillary data over a UNIX domain socket (see the
303 description of SCM_RIGHTS, above). It occurs if the number of
304 "in-flight" file descriptors exceeds the RLIMIT_NOFILE resource
305 limit and the caller does not have the CAP_SYS_RESOURCE capabil‐
306 ity. An in-flight file descriptor is one that has been sent
307 using sendmsg(2) but has not yet been accepted in the recipient
308 process using recvmsg(2).
309
310 This error is diagnosed since mainline Linux 4.5 (and in some
311 earlier kernel versions where the fix has been backported). In
312 earlier kernel versions, it was possible to place an unlimited
313 number of file descriptors in flight, by sending each file
314 descriptor with sendmsg(2) and then closing the file descriptor
315 so that it was not accounted against the RLIMIT_NOFILE resource
316 limit.
317
318 Other errors can be generated by the generic socket layer or by the
319 filesystem while generating a filesystem socket object. See the appro‐
320 priate manual pages for more information.
321
323 SCM_CREDENTIALS and the abstract namespace were introduced with Linux
324 2.2 and should not be used in portable programs. (Some BSD-derived
325 systems also support credential passing, but the implementation details
326 differ.)
327
329 Binding to a socket with a filename creates a socket in the filesystem
330 that must be deleted by the caller when it is no longer needed (using
331 unlink(2)). The usual UNIX close-behind semantics apply; the socket
332 can be unlinked at any time and will be finally removed from the
333 filesystem when the last reference to it is closed.
334
335 To pass file descriptors or credentials over a SOCK_STREAM, you need to
336 send or receive at least one byte of nonancillary data in the same
337 sendmsg(2) or recvmsg(2) call.
338
339 UNIX domain stream sockets do not support the notion of out-of-band
340 data.
341
343 When binding a socket to an address, Linux is one of the implementa‐
344 tions that appends a null terminator if none is supplied in sun_path.
345 In most cases this is unproblematic: when the socket address is
346 retrieved, it will be one byte longer than that supplied when the
347 socket was bound. However, there is one case where confusing behavior
348 can result: if 108 non-null bytes are supplied when a socket is bound,
349 then the addition of the null terminator takes the length of the path‐
350 name beyond sizeof(sun_path). Consequently, when retrieving the socket
351 address (for example, via accept(2)), if the input addrlen argument for
352 the retrieving call is specified as sizeof(struct sockaddr_un), then
353 the returned address structure won't have a null terminator in
354 sun_path.
355
356 In addition, some implementations don't require a null terminator when
357 binding a socket (the addrlen argument is used to determine the length
358 of sun_path) and when the socket address is retrieved on these imple‐
359 mentations, there is no null terminator in sun_path.
360
361 Applications that retrieve socket addresses can (portably) code to han‐
362 dle the possibility that there is no null terminator in sun_path by
363 respecting the fact that the number of valid bytes in the pathname is:
364
365 strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path))
366
367 Alternatively, an application can retrieve the socket address by allo‐
368 cating a buffer of size sizeof(struct sockaddr_un)+1 that is zeroed out
369 before the retrieval. The retrieving call can specify addrlen as
370 sizeof(struct sockaddr_un), and the extra zero byte ensures that there
371 will be a null terminator for the string returned in sun_path:
372
373 void *addrp;
374
375 addrlen = sizeof(struct sockaddr_un);
376 addrp = malloc(addrlen + 1);
377 if (addrp == NULL)
378 /* Handle error */ ;
379 memset(addrp, 0, addrlen + 1);
380
381 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == -1)
382 /* handle error */ ;
383
384 printf("sun_path = %s\n", ((struct sockaddr_un *) addrp)->sun_path);
385
386 This sort of messiness can be avoided if it is guaranteed that the
387 applications that create pathname sockets follow the rules outlined
388 above under Pathname sockets.
389
391 The following code demonstrates the use of sequenced-packet sockets for
392 local interprocess communication. It consists of two programs. The
393 server program waits for a connection from the client program. The
394 client sends each of its command-line arguments in separate messages.
395 The server treats the incoming messages as integers and adds them up.
396 The client sends the command string "END". The server sends back a
397 message containing the sum of the client's integers. The client prints
398 the sum and exits. The server waits for the next client to connect.
399 To stop the server, the client is called with the command-line argument
400 "DOWN".
401
402 The following output was recorded while running the server in the back‐
403 ground and repeatedly executing the client. Execution of the server
404 program ends when it receives the "DOWN" command.
405
406 Example output
407 $ ./server &
408 [1] 25887
409 $ ./client 3 4
410 Result = 7
411 $ ./client 11 -5
412 Result = 6
413 $ ./client DOWN
414 Result = 0
415 [1]+ Done ./server
416 $
417
418 Program source
419
420 /*
421 * File connection.h
422 */
423
424 #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
425 #define BUFFER_SIZE 12
426
427 /*
428 * File server.c
429 */
430
431 #include <stdio.h>
432 #include <stdlib.h>
433 #include <string.h>
434 #include <sys/socket.h>
435 #include <sys/un.h>
436 #include <unistd.h>
437 #include "connection.h"
438
439 int
440 main(int argc, char *argv[])
441 {
442 struct sockaddr_un name;
443 int down_flag = 0;
444 int ret;
445 int connection_socket;
446 int data_socket;
447 int result;
448 char buffer[BUFFER_SIZE];
449
450 /*
451 * In case the program exited inadvertently on the last run,
452 * remove the socket.
453 */
454
455 unlink(SOCKET_NAME);
456
457 /* Create local socket. */
458
459 connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
460 if (connection_socket == -1) {
461 perror("socket");
462 exit(EXIT_FAILURE);
463 }
464
465 /*
466 * For portability clear the whole structure, since some
467 * implementations have additional (nonstandard) fields in
468 * the structure.
469 */
470
471 memset(&name, 0, sizeof(struct sockaddr_un));
472
473 /* Bind socket to socket name. */
474
475 name.sun_family = AF_UNIX;
476 strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1);
477
478 ret = bind(connection_socket, (const struct sockaddr *) &name,
479 sizeof(struct sockaddr_un));
480 if (ret == -1) {
481 perror("bind");
482 exit(EXIT_FAILURE);
483 }
484
485 /*
486 * Prepare for accepting connections. The backlog size is set
487 * to 20. So while one request is being processed other requests
488 * can be waiting.
489 */
490
491 ret = listen(connection_socket, 20);
492 if (ret == -1) {
493 perror("listen");
494 exit(EXIT_FAILURE);
495 }
496
497 /* This is the main loop for handling connections. */
498
499 for (;;) {
500
501 /* Wait for incoming connection. */
502
503 data_socket = accept(connection_socket, NULL, NULL);
504 if (data_socket == -1) {
505 perror("accept");
506 exit(EXIT_FAILURE);
507 }
508
509 result = 0;
510 for(;;) {
511
512 /* Wait for next data packet. */
513
514 ret = read(data_socket, buffer, BUFFER_SIZE);
515 if (ret == -1) {
516 perror("read");
517 exit(EXIT_FAILURE);
518 }
519
520 /* Ensure buffer is 0-terminated. */
521
522 buffer[BUFFER_SIZE - 1] = 0;
523
524 /* Handle commands. */
525
526 if (!strncmp(buffer, "DOWN", BUFFER_SIZE)) {
527 down_flag = 1;
528 break;
529 }
530
531 if (!strncmp(buffer, "END", BUFFER_SIZE)) {
532 break;
533 }
534
535 /* Add received summand. */
536
537 result += atoi(buffer);
538 }
539
540 /* Send result. */
541
542 sprintf(buffer, "%d", result);
543 ret = write(data_socket, buffer, BUFFER_SIZE);
544
545 if (ret == -1) {
546 perror("write");
547 exit(EXIT_FAILURE);
548 }
549
550 /* Close socket. */
551
552 close(data_socket);
553
554 /* Quit on DOWN command. */
555
556 if (down_flag) {
557 break;
558 }
559 }
560
561 close(connection_socket);
562
563 /* Unlink the socket. */
564
565 unlink(SOCKET_NAME);
566
567 exit(EXIT_SUCCESS);
568 }
569
570 /*
571 * File client.c
572 */
573
574 #include <errno.h>
575 #include <stdio.h>
576 #include <stdlib.h>
577 #include <string.h>
578 #include <sys/socket.h>
579 #include <sys/un.h>
580 #include <unistd.h>
581 #include "connection.h"
582
583 int
584 main(int argc, char *argv[])
585 {
586 struct sockaddr_un addr;
587 int i;
588 int ret;
589 int data_socket;
590 char buffer[BUFFER_SIZE];
591
592 /* Create local socket. */
593
594 data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
595 if (data_socket == -1) {
596 perror("socket");
597 exit(EXIT_FAILURE);
598 }
599
600 /*
601 * For portability clear the whole structure, since some
602 * implementations have additional (nonstandard) fields in
603 * the structure.
604 */
605
606 memset(&addr, 0, sizeof(struct sockaddr_un));
607
608 /* Connect socket to socket address */
609
610 addr.sun_family = AF_UNIX;
611 strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1);
612
613 ret = connect (data_socket, (const struct sockaddr *) &addr,
614 sizeof(struct sockaddr_un));
615 if (ret == -1) {
616 fprintf(stderr, "The server is down.\n");
617 exit(EXIT_FAILURE);
618 }
619
620 /* Send arguments. */
621
622 for (i = 1; i < argc; ++i) {
623 ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
624 if (ret == -1) {
625 perror("write");
626 break;
627 }
628 }
629
630 /* Request result. */
631
632 strcpy (buffer, "END");
633 ret = write(data_socket, buffer, strlen(buffer) + 1);
634 if (ret == -1) {
635 perror("write");
636 exit(EXIT_FAILURE);
637 }
638
639 /* Receive result. */
640
641 ret = read(data_socket, buffer, BUFFER_SIZE);
642 if (ret == -1) {
643 perror("read");
644 exit(EXIT_FAILURE);
645 }
646
647 /* Ensure buffer is 0-terminated. */
648
649 buffer[BUFFER_SIZE - 1] = 0;
650
651 printf("Result = %s\n", buffer);
652
653 /* Close socket. */
654
655 close(data_socket);
656
657 exit(EXIT_SUCCESS);
658 }
659
660 For an example of the use of SCM_RIGHTS see cmsg(3).
661
663 recvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3), capabili‐
664 ties(7), credentials(7), socket(7), udp(7)
665
667 This page is part of release 4.15 of the Linux man-pages project. A
668 description of the project, information about reporting bugs, and the
669 latest version of this page, can be found at
670 https://www.kernel.org/doc/man-pages/.
671
672
673
674Linux 2017-09-15 UNIX(7)