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