]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/unix.7
unix.7: Minor wording fix
[thirdparty/man-pages.git] / man7 / unix.7
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>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM_ONE_PARA)
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 2016-03-15 "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 sequenced-packet socket that is connection-oriented,
53 preserves message boundaries,
54 and delivers messages in the order that they were sent.
55
56 UNIX domain sockets support passing file descriptors or process credentials
57 to other processes using ancillary data.
58 .SS Address format
59 A UNIX domain socket address is represented in the following structure:
60 .in +4n
61 .nf
62
63 .\" #define UNIX_PATH_MAX 108
64 .\"
65 struct sockaddr_un {
66 sa_family_t sun_family; /* AF_UNIX */
67 char sun_path[108]; /* pathname */
68 };
69 .fi
70 .in
71 .PP
72 The
73 .I sun_family
74 field always contains
75 .BR AF_UNIX .
76 On Linux
77 .I sun_path
78 is 108 bytes in size; see also NOTES, below.
79
80 Various systems calls (for example,
81 .BR bind (2),
82 .BR connect (2),
83 and
84 .BR sendto (2))
85 take a
86 .I sockaddr_un
87 argument as input.
88 Some other system calls (for example,
89 .BR getsockname (2),
90 .BR getpeername (2),
91 .BR recvfrom (2),
92 and
93 .BR accept (2))
94 return an argument of this type.
95
96 Three types of address are distinguished in the
97 .I sockaddr_un
98 structure:
99 .IP * 3
100 .IR pathname :
101 a UNIX domain socket can be bound to a null-terminated
102 filesystem pathname using
103 .BR bind (2).
104 When the address of a pathname socket is returned
105 (by one of the system calls noted above),
106 its length is
107
108 offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
109
110 and
111 .I sun_path
112 contains the null-terminated pathname.
113 (On Linux, the above
114 .BR offsetof ()
115 expression equates to the same value as
116 .IR sizeof(sa_family_t) ,
117 but some other implementations include other fields before
118 .IR sun_path ,
119 so the
120 .BR offsetof ()
121 expression more portably describes the size of the address structure.)
122 .IP
123 For further details of pathname sockets, see below.
124 .IP *
125 .IR unnamed :
126 A stream socket that has not been bound to a pathname using
127 .BR bind (2)
128 has no name.
129 Likewise, the two sockets created by
130 .BR socketpair (2)
131 are unnamed.
132 When the address of an unnamed socket is returned,
133 its length is
134 .IR "sizeof(sa_family_t)" ,
135 and
136 .I sun_path
137 should 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 :
142 an abstract socket address is distinguished (from a pathname socket)
143 by the fact that
144 .IR sun_path[0]
145 is a null byte (\(aq\\0\(aq).
146 The socket's address in this namespace is given by the additional
147 bytes in
148 .IR sun_path
149 that are covered by the specified length of the address structure.
150 (Null bytes in the name have no special significance.)
151 The name has no connection with filesystem pathnames.
152 When the address of an abstract socket is returned,
153 the returned
154 .I addrlen
155 is greater than
156 .IR "sizeof(sa_family_t)"
157 (i.e., greater than 2), and the name of the socket is contained in
158 the first
159 .IR "(addrlen \- sizeof(sa_family_t))"
160 bytes of
161 .IR sun_path .
162 The abstract socket namespace is a nonportable Linux extension.
163 .SS Pathname sockets
164 When binding a socket to a pathname, a few rules should be observed
165 for maximum portability and ease of coding:
166 .IP * 3
167 The pathname in
168 .I sun_path
169 should be null-terminated.
170 .IP *
171 The length of the pathname, including the terminating null byte,
172 should not exceed the size of
173 .IR sun_path .
174 .IP *
175 The
176 .I addrlen
177 argument that describes the enclosing
178 .I sockaddr_un
179 structure should have a value of at least:
180
181 .nf
182 offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
183 .fi
184 .IP
185 or, more simply,
186 .I addrlen
187 can be specified as
188 .IR "sizeof(struct sockaddr_un)" .
189 .PP
190 There is some variation in how implementations handle UNIX domain
191 socket addresses that do not follow the above rules.
192 For example, some (but not all) implementations
193 .\" Linux does this, including for the case where the supplied path
194 .\" is 108 bytes
195 append a null terminator if none is present in the supplied
196 .IR sun_path .
197
198 When coding portable applications,
199 keep in mind that some implementations
200 .\" HP-UX
201 have
202 .I sun_path
203 as short as 92 bytes.
204 .\" Modern BSDs generally have 104, Tru64 and AIX have 104,
205 .\" Solaris and Irix have 108
206
207 Various system calls
208 .RB ( accept (2),
209 .BR recvfrom (2),
210 .BR getsockname (2),
211 .BR getpeername (2))
212 return socket address structures.
213 When applied to UNIX domain sockets, the value-result
214 .I addrlen
215 argument supplied to the call should be initialized as above.
216 Upon return, the argument is set to indicate the
217 .I actual
218 size of the address structure.
219 The caller should check the value returned in this argument:
220 if the output value exceeds the input value,
221 then there is no guarantee that a null terminator is present in
222 .IR sun_path .
223 (See BUGS.)
224 .SS Socket options
225 For historical reasons, these socket options are specified with a
226 .B SOL_SOCKET
227 type even though they are
228 .B AF_UNIX
229 specific.
230 They can be set with
231 .BR setsockopt (2)
232 and read with
233 .BR getsockopt (2)
234 by specifying
235 .B SOL_SOCKET
236 as the socket family.
237 .TP
238 .B SO_PASSCRED
239 Enables the receiving of the credentials of the sending process in an
240 ancillary message.
241 When this option is set and the socket is not yet connected
242 a unique name in the abstract namespace will be generated automatically.
243 Expects an integer boolean flag.
244 .SS Autobind feature
245 If a
246 .BR bind (2)
247 call specifies
248 .I addrlen
249 as
250 .IR sizeof(sa_family_t) ,
251 .\" i.e., sizeof(short)
252 or the
253 .BR SO_PASSCRED
254 socket option was specified for a socket that was
255 not explicitly bound to an address,
256 then the socket is autobound to an abstract address.
257 The address consists of a null byte
258 followed by 5 bytes in the character set
259 .IR [0-9a-f] .
260 Thus, there is a limit of 2^20 autobind addresses.
261 (From Linux 2.1.15, when the autobind feature was added,
262 8 bytes were used, and the limit was thus 2^32 autobind addresses.
263 The change to 5 bytes came in Linux 2.3.15.)
264 .SS Sockets API
265 The following paragraphs describe domain-specific details and
266 unsupported features of the sockets API for UNIX domain sockets on Linux.
267
268 UNIX domain sockets do not support the transmission of
269 out-of-band data (the
270 .B MSG_OOB
271 flag for
272 .BR send (2)
273 and
274 .BR recv (2)).
275
276 The
277 .BR send (2)
278 .B MSG_MORE
279 flag is not supported by UNIX domain sockets.
280
281 The use of
282 .B MSG_TRUNC
283 in the
284 .I flags
285 argument of
286 .BR recv (2)
287 is not supported by UNIX domain sockets.
288
289 The
290 .B SO_SNDBUF
291 socket option does have an effect for UNIX domain sockets, but the
292 .B SO_RCVBUF
293 option does not.
294 For datagram sockets, the
295 .B SO_SNDBUF
296 value imposes an upper limit on the size of outgoing datagrams.
297 This limit is calculated as the doubled (see
298 .BR socket (7))
299 option value less 32 bytes used for overhead.
300 .SS Ancillary messages
301 Ancillary data is sent and received using
302 .BR sendmsg (2)
303 and
304 .BR recvmsg (2).
305 For historical reasons the ancillary message types listed below
306 are specified with a
307 .B SOL_SOCKET
308 type even though they are
309 .B AF_UNIX
310 specific.
311 To send them set the
312 .I cmsg_level
313 field of the struct
314 .I cmsghdr
315 to
316 .B SOL_SOCKET
317 and the
318 .I cmsg_type
319 field to the type.
320 For more information see
321 .BR cmsg (3).
322 .TP
323 .B SCM_RIGHTS
324 Send or receive a set of open file descriptors from another process.
325 The data portion contains an integer array of the file descriptors.
326 The passed file descriptors behave as though they have been created with
327 .BR dup (2).
328 .TP
329 .B SCM_CREDENTIALS
330 Send or receive UNIX credentials.
331 This can be used for authentication.
332 The credentials are passed as a
333 .I struct ucred
334 ancillary message.
335 Thus structure is defined in
336 .I <sys/socket.h>
337 as follows:
338
339 .in +4n
340 .nf
341 struct ucred {
342 pid_t pid; /* process ID of the sending process */
343 uid_t uid; /* user ID of the sending process */
344 gid_t gid; /* group ID of the sending process */
345 };
346 .fi
347 .in
348
349 Since glibc 2.8, the
350 .B _GNU_SOURCE
351 feature test macro must be defined (before including
352 .I any
353 header files) in order to obtain the definition
354 of this structure.
355
356 The credentials which the sender specifies are checked by the kernel.
357 A process with effective user ID 0 is allowed to specify values that do
358 not match its own.
359 The sender must specify its own process ID (unless it has the capability
360 .BR CAP_SYS_ADMIN ),
361 its user ID, effective user ID, or saved set-user-ID (unless it has
362 .BR CAP_SETUID ),
363 and its group ID, effective group ID, or saved set-group-ID
364 (unless it has
365 .BR CAP_SETGID ).
366 To receive a
367 .I struct ucred
368 message the
369 .B SO_PASSCRED
370 option must be enabled on the socket.
371 .SS Ioctls
372 The following
373 .BR ioctl (2)
374 calls return information in
375 .IR value .
376 The correct syntax is:
377 .PP
378 .RS
379 .nf
380 .BI int " value";
381 .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");"
382 .fi
383 .RE
384 .PP
385 .I ioctl_type
386 can be:
387 .TP
388 .B SIOCINQ
389 For
390 .B SOCK_STREAM
391 socket the function returns the amount of queued unread data in the receive buffer.
392 The socket must not be in LISTEN state, otherwise an error
393 .RB ( EINVAL )
394 is returned.
395 .B SIOCINQ
396 is defined in
397 .IR <linux/sockios.h> .
398 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002,
399 .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers
400 Alternatively,
401 you can use the synonymous
402 .BR FIONREAD ,
403 defined in
404 .IR <sys/ioctl.h> .
405 .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not
406 .\" quite what userland might expect. It seems to return the number
407 .\" of bytes allocated for buffers containing pending output.
408 .\" That number is normally larger than the number of bytes of pending
409 .\" output. Since this info is, from userland's point of view, imprecise,
410 .\" and it may well change, probably best not to document this now.
411 For
412 .B SOCK_DGRAM
413 socket,
414 the returned value is the same as
415 for Internet domain datagram socket;
416 see
417 .BR udp (7).
418 .SH ERRORS
419 .TP
420 .B EADDRINUSE
421 The specified local address is already in use or the filesystem socket
422 object already exists.
423 .TP
424 .B ECONNREFUSED
425 The remote address specified by
426 .BR connect (2)
427 was not a listening socket.
428 This error can also occur if the target pathname is not a socket.
429 .TP
430 .B ECONNRESET
431 Remote socket was unexpectedly closed.
432 .TP
433 .B EFAULT
434 User memory address was not valid.
435 .TP
436 .B EINVAL
437 Invalid argument passed.
438 A common cause is that the value
439 .B AF_UNIX
440 was not specified in the
441 .I sun_type
442 field of passed addresses, or the socket was in an
443 invalid state for the applied operation.
444 .TP
445 .B EISCONN
446 .BR connect (2)
447 called on an already connected socket or a target address was
448 specified on a connected socket.
449 .TP
450 .B ENOENT
451 The pathname in the remote address specified to
452 .BR connect (2)
453 did not exist.
454 .TP
455 .B ENOMEM
456 Out of memory.
457 .TP
458 .B ENOTCONN
459 Socket operation needs a target address, but the socket is not connected.
460 .TP
461 .B EOPNOTSUPP
462 Stream operation called on non-stream oriented socket or tried to
463 use the out-of-band data option.
464 .TP
465 .B EPERM
466 The sender passed invalid credentials in the
467 .IR "struct ucred" .
468 .TP
469 .B EPIPE
470 Remote socket was closed on a stream socket.
471 If enabled, a
472 .B SIGPIPE
473 is sent as well.
474 This can be avoided by passing the
475 .B MSG_NOSIGNAL
476 flag to
477 .BR sendmsg (2)
478 or
479 .BR recvmsg (2).
480 .TP
481 .B EPROTONOSUPPORT
482 Passed protocol is not
483 .BR AF_UNIX .
484 .TP
485 .B EPROTOTYPE
486 Remote socket does not match the local socket type
487 .RB ( SOCK_DGRAM
488 versus
489 .BR SOCK_STREAM )
490 .TP
491 .B ESOCKTNOSUPPORT
492 Unknown socket type.
493 .PP
494 Other errors can be generated by the generic socket layer or
495 by the filesystem while generating a filesystem socket object.
496 See the appropriate manual pages for more information.
497 .SH VERSIONS
498 .B SCM_CREDENTIALS
499 and the abstract namespace were introduced with Linux 2.2 and should not
500 be used in portable programs.
501 (Some BSD-derived systems also support credential passing,
502 but the implementation details differ.)
503 .SH NOTES
504 Binding to a socket with a filename creates a socket
505 in the filesystem that must be deleted by the caller when it is no
506 longer needed (using
507 .BR unlink (2)).
508 The usual UNIX close-behind semantics apply; the socket can be unlinked
509 at any time and will be finally removed from the filesystem when the last
510 reference to it is closed.
511
512 To pass file descriptors or credentials over a
513 .BR SOCK_STREAM ,
514 you need
515 to send or receive at least one byte of nonancillary data in the same
516 .BR sendmsg (2)
517 or
518 .BR recvmsg (2)
519 call.
520
521 UNIX domain stream sockets do not support the notion of out-of-band data.
522 .\"
523 .SS Socket ownership and permissions
524 In the Linux implementation, sockets which are visible in the
525 filesystem honor the permissions of the directory they are in.
526 Creation of a new socket will fail if the process does not have write and
527 search (execute) permission on the directory the socket is created in.
528
529 On Linux,
530 connecting to a stream socket object requires write permission on that socket;
531 sending a datagram to a datagram socket likewise
532 requires write permission on that socket.
533 POSIX does not make any statement about the effect of the permissions
534 on a socket file, and on many systems (e.g., several BSD derivatives),
535 the socket permissions are ignored.
536 Portable programs should not rely on
537 this feature for security.
538
539 When creating a new socket, the owner and group of the socket file
540 are set according to the usual rules.
541 The socket file has all permissions enabled,
542 other than those that are turned off by the process
543 .BR umask (2).
544
545 For a socket that is visible in the filesystem,
546 the owner, group, and permissions can be changed (using
547 .BR chown (2)
548 and
549 .BR chmod (2)).
550 .\" However, fchown() and fchmod() do not seem to have an effect
551
552 .\"
553 .SH BUGS
554 When binding a socket to an address,
555 Linux is one of the implementations that appends a null terminator
556 if none is supplied in
557 .IR sun_path .
558 In most cases this is unproblematic:
559 when the socket address is retrieved,
560 it will be one byte longer than that supplied when the socket was bound.
561 However, there is one case where confusing behavior can result:
562 if 108 non-null bytes are supplied when a socket is bound,
563 then the addition of the null terminator takes the length of
564 the pathname beyond
565 .IR sizeof(sun_path) .
566 Consequently, when retrieving the socket address
567 (for example, via
568 .BR accept (2)),
569 .\" The behavior on Solaris is quite similar.
570 if the input
571 .I addrlen
572 argument for the retrieving call is specified as
573 .IR "sizeof(struct sockaddr_un)" ,
574 then the returned address structure
575 .I won't
576 have a null terminator in
577 .IR sun_path .
578
579 In addition, some implementations
580 .\" i.e., traditional BSD
581 don't require a null terminator when binding a socket (the
582 .I addrlen
583 argument is used to determine the length of
584 .IR sun_path )
585 and when the socket address is retrieved on these implementations,
586 there is no null terminator in
587 .IR sun_path .
588
589 Applications that retrieve socket addresses can (portably) code
590 to handle the possibility that there is no null terminator in
591 .IR sun_path
592 by respecting the fact that the number of valid bytes in the pathname is:
593
594 strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path))
595 .\" The following patch to amend kernel behavior was rejected:
596 .\" http://thread.gmane.org/gmane.linux.kernel.api/2437
597 .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path
598 .\" 2012-04-17
599 .\" And there was a related discussion in the Austin list:
600 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735
601 .\" Subject: Having a sun_path with no null terminator
602 .\" 2012-04-18
603 .\"
604 .\" FIXME . Track http://austingroupbugs.net/view.php?id=561
605
606 Alternatively, an application can retrieve
607 the socket address by allocating a buffer of size
608 .I "sizeof(struct sockaddr_un)+1"
609 that is zeroed out before the retrieval.
610 The retrieving call can specify
611 .I addrlen
612 as
613 .IR "sizeof(struct sockaddr_un)" ,
614 and the extra zero byte ensures that there will be
615 a null terminator for the string returned in
616 .IR sun_path :
617
618 .nf
619 .in +3
620 void *addrp;
621
622 addrlen = sizeof(struct sockaddr_un);
623 addrp = malloc(addrlen + 1);
624 if (addrp == NULL)
625 /* Handle error */ ;
626 memset(addrp, 0, addrlen + 1);
627
628 if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1)
629 /* handle error */ ;
630
631 printf("sun_path = %s\\n", ((struct sockaddr_un *) addrp)\->sun_path);
632 .in
633 .fi
634
635 This sort of messiness can be avoided if it is guaranteed
636 that the applications that
637 .I create
638 pathname sockets follow the rules outlined above under
639 .IR "Pathname sockets" .
640 .SH EXAMPLE
641 The following code demonstrates the use of sequenced-packet
642 sockets for local interprocess communication.
643 It consists of two programs.
644 The server program waits for a connection from the client program.
645 The client sends each of its command-line arguments in separate messages.
646 The server treats the incoming messages as integers and adds them up.
647 The client sends the command string "END".
648 The server sends back a message containing the sum of the client's integers.
649 The client prints the sum and exits.
650 The server waits for the next client to connect.
651 To stop the server, the client is called with the command-line argument "DOWN".
652 .PP
653 The following output was recorded while running the server in the background
654 and repeatedly executing the client.
655 Execution of the server program ends when it receives the "DOWN" command.
656 .SS Example output
657 .in +4n
658 .nf
659 $ \fB./server &\fP
660 [1] 25887
661 $ \fB./client 3 4\fP
662 Result = 7
663 $ \fB./client 11 \-5\fP
664 Result = 6
665 $ \fB./client DOWN\fP
666 Result = 0
667 [1]+ Done ./server
668 $
669 .fi
670 .in
671 .SS Program source
672 .nf
673 /*
674 * File connection.h
675 */
676
677 #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
678 #define BUFFER_SIZE 12
679
680 /*
681 * File server.c
682 */
683
684 #include <stdio.h>
685 #include <stdlib.h>
686 #include <string.h>
687 #include <sys/socket.h>
688 #include <sys/un.h>
689 #include <unistd.h>
690 #include "connection.h"
691
692 int
693 main(int argc, char *argv[])
694 {
695 struct sockaddr_un name;
696 int down_flag = 0;
697 int ret;
698 int connection_socket;
699 int data_socket;
700 int result;
701 char buffer[BUFFER_SIZE];
702
703 /*
704 * In case the program exited inadvertently on the last run,
705 * remove the socket.
706 */
707
708 unlink(SOCKET_NAME);
709
710 /* Create local socket. */
711
712 connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
713 if (connection_socket == \-1) {
714 perror("socket");
715 exit(EXIT_FAILURE);
716 }
717
718 /*
719 * For portability clear the whole structure, since some
720 * implementations have additional (nonstandard) fields in
721 * the structure.
722 */
723
724 memset(&name, 0, sizeof(struct sockaddr_un));
725
726 /* Bind socket to socket name. */
727
728 name.sun_family = AF_UNIX;
729 strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1);
730
731 ret = bind(connection_socket, (const struct sockaddr *) &name,
732 sizeof(struct sockaddr_un));
733 if (ret == \-1) {
734 perror("bind");
735 exit(EXIT_FAILURE);
736 }
737
738 /*
739 * Prepare for accepting connections. The backlog size is set
740 * to 20. So while one request is being processed other requests
741 * can be waiting.
742 */
743
744 ret = listen(connection_socket, 20);
745 if (ret == \-1) {
746 perror("listen");
747 exit(EXIT_FAILURE);
748 }
749
750 /* This is the main loop for handling connections. */
751
752 for (;;) {
753
754 /* Wait for incoming connection. */
755
756 data_socket = accept(connection_socket, NULL, NULL);
757 if (data_socket == \-1) {
758 perror("accept");
759 exit(EXIT_FAILURE);
760 }
761
762 result = 0;
763 for(;;) {
764
765 /* Wait for next data packet. */
766
767 ret = read(data_socket, buffer, BUFFER_SIZE);
768 if (ret == \-1) {
769 perror("read");
770 exit(EXIT_FAILURE);
771 }
772
773 /* Ensure buffer is 0\-terminated. */
774
775 buffer[BUFFER_SIZE \- 1] = 0;
776
777 /* Handle commands. */
778
779 if (!strncmp(buffer, "DOWN", BUFFER_SIZE)) {
780 down_flag = 1;
781 break;
782 }
783
784 if (!strncmp(buffer, "END", BUFFER_SIZE)) {
785 break;
786 }
787
788 /* Add received summand. */
789
790 result += atoi(buffer);
791 }
792
793 /* Send result. */
794
795 sprintf(buffer, "%d", result);
796 ret = write(data_socket, buffer, BUFFER_SIZE);
797
798 if (ret == \-1) {
799 perror("write");
800 exit(EXIT_FAILURE);
801 }
802
803 /* Close socket. */
804
805 close(data_socket);
806
807 /* Quit on DOWN command. */
808
809 if (down_flag) {
810 break;
811 }
812 }
813
814 close(connection_socket);
815
816 /* Unlink the socket. */
817
818 unlink(SOCKET_NAME);
819
820 exit(EXIT_SUCCESS);
821 }
822
823 /*
824 * File client.c
825 */
826
827 #include <errno.h>
828 #include <stdio.h>
829 #include <stdlib.h>
830 #include <string.h>
831 #include <sys/socket.h>
832 #include <sys/un.h>
833 #include <unistd.h>
834 #include "connection.h"
835
836 int
837 main(int argc, char *argv[])
838 {
839 struct sockaddr_un addr;
840 int i;
841 int ret;
842 int data_socket;
843 char buffer[BUFFER_SIZE];
844
845 /* Create local socket. */
846
847 data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
848 if (data_socket == \-1) {
849 perror("socket");
850 exit(EXIT_FAILURE);
851 }
852
853 /*
854 * For portability clear the whole structure, since some
855 * implementations have additional (nonstandard) fields in
856 * the structure.
857 */
858
859 memset(&addr, 0, sizeof(struct sockaddr_un));
860
861 /* Connect socket to socket address */
862
863 addr.sun_family = AF_UNIX;
864 strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1);
865
866 ret = connect (data_socket, (const struct sockaddr *) &addr,
867 sizeof(struct sockaddr_un));
868 if (ret == \-1) {
869 fprintf(stderr, "The server is down.\\n");
870 exit(EXIT_FAILURE);
871 }
872
873 /* Send arguments. */
874
875 for (i = 1; i < argc; ++i) {
876 ret = write(data_socket, argv[i], strlen(argv[i]) + 1);
877 if (ret == \-1) {
878 perror("write");
879 break;
880 }
881 }
882
883 /* Request result. */
884
885 strcpy (buffer, "END");
886 ret = write(data_socket, buffer, strlen(buffer) + 1);
887 if (ret == \-1) {
888 perror("write");
889 exit(EXIT_FAILURE);
890 }
891
892 /* Receive result. */
893
894 ret = read(data_socket, buffer, BUFFER_SIZE);
895 if (ret == \-1) {
896 perror("read");
897 exit(EXIT_FAILURE);
898 }
899
900 /* Ensure buffer is 0\-terminated. */
901
902 buffer[BUFFER_SIZE \- 1] = 0;
903
904 printf("Result = %s\\n", buffer);
905
906 /* Close socket. */
907
908 close(data_socket);
909
910 exit(EXIT_SUCCESS);
911 }
912 .fi
913 .PP
914 For an example of the use of
915 .BR SCM_RIGHTS
916 see
917 .BR cmsg (3).
918 .SH SEE ALSO
919 .BR recvmsg (2),
920 .BR sendmsg (2),
921 .BR socket (2),
922 .BR socketpair (2),
923 .BR cmsg (3),
924 .BR capabilities (7),
925 .BR credentials (7),
926 .BR socket (7),
927 .BR udp (7)