OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man7 / unix.7
1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" Permission is granted to distribute possibly modified copies
3 .\" of this page provided the header is included verbatim,
4 .\" and in case of nontrivial modification author and date
5 .\" of the modification is added to the header.
6 .\"
7 .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com>
8 .\" Modified, 2003-09-23, Adam Langley
9 .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com>
10 .\"     Added SOCK_SEQPACKET
11 .\" 2008-05-27, mtk, Provide a clear description of the three types of
12 .\"     address that can appear in the sockaddr_un structure: pathname,
13 .\"     unnamed, and abstract.
14 .\"
15 .TH UNIX  7 2008-12-01 "Linux" "Linux Programmer's Manual"
16 .SH NAME
17 unix, AF_UNIX, AF_LOCAL \- Sockets for local
18 interprocess communication
19 .SH SYNOPSIS
20 .B #include <sys/socket.h>
21 .br
22 .B #include <sys/un.h>
23
24 .IB unix_socket " = socket(AF_UNIX, type, 0);"
25 .br
26 .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");"
27 .SH DESCRIPTION
28 The
29 .B AF_UNIX
30 (also known as
31 .BR AF_LOCAL )
32 socket family is used to communicate between processes on the same machine
33 efficiently.
34 Traditionally, Unix sockets can be either unnamed,
35 or bound to a file system pathname (marked as being of type socket).
36 Linux also supports an abstract namespace which is independent of the
37 file system.
38
39 Valid types are:
40 .BR SOCK_STREAM ,
41 for a stream-oriented socket and
42 .BR SOCK_DGRAM ,
43 for a datagram-oriented socket that preserves message boundaries
44 (as on most Unix implementations, Unix domain datagram
45 sockets are always reliable and don't reorder datagrams);
46 and (since Linux 2.6.4)
47 .BR SOCK_SEQPACKET ,
48 for a connection-oriented socket that preserves message boundaries
49 and delivers messages in the order that they were sent.
50
51 Unix sockets support passing file descriptors or process credentials
52 to other processes using ancillary data.
53 .SS Address Format
54 A Unix domain socket address is represented in the following structure:
55 .in +4n
56 .nf
57
58 #define UNIX_PATH_MAX    108
59
60 struct sockaddr_un {
61     sa_family_t sun_family;               /* AF_UNIX */
62     char        sun_path[UNIX_PATH_MAX];  /* pathname */
63 };
64 .fi
65 .in
66 .PP
67 .I sun_family
68 always contains
69 .BR AF_UNIX .
70
71 Three types of address are distinguished in this structure:
72 .IP * 3
73 .IR pathname :
74 a Unix domain socket can be bound to a null-terminated file
75 system pathname using
76 .BR bind (2).
77 When the address of the socket is returned by
78 .BR getsockname (2),
79 .BR getpeername (2),
80 and
81 .BR accept (2),
82 its length is
83 .IR "sizeof(sa_family_t) + strlen(sun_path) + 1" ,
84 and
85 .I sun_path
86 contains the null-terminated pathname.
87 .IP *
88 .IR unnamed :
89 A stream socket that has not been bound to a pathname using
90 .BR bind (2)
91 has no name.
92 Likewise, the two sockets created by
93 .BR socketpair (2)
94 are unnamed.
95 When the address of an unnamed socket is returned by
96 .BR getsockname (2),
97 .BR getpeername (2),
98 and
99 .BR accept (2),
100 its length is
101 .IR "sizeof(sa_family_t)" ,
102 and
103 .I sun_path
104 should not be inspected.
105 .\" There is quite some variation across implementations: FreeBSD
106 .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes.
107 .IP *
108 .IR abstract :
109 an abstract socket address is distinguished by the fact that
110 .IR sun_path[0]
111 is a null byte ('\\0').
112 All of the remaining bytes in
113 .I sun_path
114 define the "name" of the socket.
115 (Null bytes in the name have no special significance.)
116 The name has no connection with file system pathnames.
117 The socket's address in this namespace is given by the rest of the
118 bytes in
119 .IR sun_path .
120 When the address of an abstract socket is returned by
121 .BR getsockname (2),
122 .BR getpeername (2),
123 and
124 .BR accept (2),
125 its length is
126 .IR "sizeof(struct sockaddr_un)" ,
127 and
128 .I sun_path
129 contains the abstract name.
130 The abstract socket namespace is a nonportable Linux extension.
131 .SS Socket Options
132 For historical reasons these socket options are specified with a
133 .B SOL_SOCKET
134 type even though they are
135 .B AF_UNIX
136 specific.
137 They can be set with
138 .BR setsockopt (2)
139 and read with
140 .BR getsockopt (2)
141 by specifying
142 .B SOL_SOCKET
143 as the socket family.
144 .TP
145 .B SO_PASSCRED
146 Enables the receiving of the credentials of the sending process
147 ancillary message.
148 When this option is set and the socket is not yet connected
149 a unique name in the abstract namespace will be generated automatically.
150 Expects an integer boolean flag.
151 .SS Sockets API
152 The following paragraphs describe domain-specific details and
153 unsupported features of the sockets API for Unix domain sockets on Linux.
154
155 Unix domain sockets do not support the transmission of
156 out-of-band data (the
157 .B MSG_OOB
158 flag for
159 .BR send (2)
160 and
161 .BR recv (2)).
162
163 The
164 .BR send (2)
165 .B MSG_MORE
166 flag is not supported by Unix domain sockets.
167
168 The use of
169 .B MSG_TRUNC
170 in the
171 .I flags
172 argument of
173 .BR recv (2)
174 is not supported by Unix domain sockets.
175
176 The
177 .B SO_SNDBUF
178 socket option does have an effect for Unix domain sockets, but the
179 .B SO_RCVBUF
180 option does not.
181 For datagram sockets, the
182 .B SO_SNDBUF
183 value imposes an upper limit on the size of outgoing datagrams.
184 This limit is calculated as the doubled (see
185 .BR socket (7))
186 option value less 32 bytes used for overhead.
187 .SS Ancillary Messages
188 Ancillary data is sent and received using
189 .BR sendmsg (2)
190 and
191 .BR recvmsg (2).
192 For historical reasons the ancillary message types listed below
193 are specified with a
194 .B SOL_SOCKET
195 type even though they are
196 .B AF_UNIX
197 specific.
198 To send them set the
199 .I cmsg_level
200 field of the struct
201 .I cmsghdr
202 to
203 .B SOL_SOCKET
204 and the
205 .I cmsg_type
206 field to the type.
207 For more information see
208 .BR cmsg (3).
209 .TP
210 .B SCM_RIGHTS
211 Send or receive a set of open file descriptors from another process.
212 The data portion contains an integer array of the file descriptors.
213 The passed file descriptors behave as though they have been created with
214 .BR dup (2).
215 .TP
216 .B SCM_CREDENTIALS
217 Send or receive Unix credentials.
218 This can be used for authentication.
219 The credentials are passed as a
220 .I struct ucred
221 ancillary message.
222 Thus structure is defined in
223 .I <sys/socket.h>
224 as follows:
225
226 .in +4n
227 .nf
228 struct ucred {
229     pid_t pid;    /* process ID of the sending process */
230     uid_t uid;    /* user ID of the sending process */
231     gid_t gid;    /* group ID of the sending process */
232 };
233 .fi
234 .in
235
236 Since glibc 2.8, the
237 .B _GNU_SOURCE
238 feature test macro must be defined in order to obtain the definition
239 of this structure.
240
241 The credentials which the sender specifies are checked by the kernel.
242 A process with effective user ID 0 is allowed to specify values that do
243 not match its own.
244 The sender must specify its own process ID (unless it has the capability
245 .BR CAP_SYS_ADMIN ),
246 its user ID, effective user ID, or saved set-user-ID (unless it has
247 .BR CAP_SETUID ),
248 and its group ID, effective group ID, or saved set-group-ID
249 (unless it has
250 .BR CAP_SETGID ).
251 To receive a
252 .I struct ucred
253 message the
254 .B SO_PASSCRED
255 option must be enabled on the socket.
256 .SH ERRORS
257 .TP
258 .B EADDRINUSE
259 Selected local address is already taken or file system socket
260 object already exists.
261 .TP
262 .B ECONNREFUSED
263 .BR connect (2)
264 called with a socket object that isn't listening.
265 This can happen when
266 the remote socket does not exist or the filename is not a socket.
267 .TP
268 .B ECONNRESET
269 Remote socket was unexpectedly closed.
270 .TP
271 .B EFAULT
272 User memory address was not valid.
273 .TP
274 .B EINVAL
275 Invalid argument passed.
276 A common cause is the missing setting of AF_UNIX
277 in the
278 .I sun_type
279 field of passed addresses or the socket being in an
280 invalid state for the applied operation.
281 .TP
282 .B EISCONN
283 .BR connect (2)
284 called on an already connected socket or a target address was
285 specified on a connected socket.
286 .TP
287 .B ENOMEM
288 Out of memory.
289 .TP
290 .B ENOTCONN
291 Socket operation needs a target address, but the socket is not connected.
292 .TP
293 .B EOPNOTSUPP
294 Stream operation called on non-stream oriented socket or tried to
295 use the out-of-band data option.
296 .TP
297 .B EPERM
298 The sender passed invalid credentials in the
299 .IR "struct ucred" .
300 .TP
301 .B EPIPE
302 Remote socket was closed on a stream socket.
303 If enabled, a
304 .B SIGPIPE
305 is sent as well.
306 This can be avoided by passing the
307 .B MSG_NOSIGNAL
308 flag to
309 .BR sendmsg (2)
310 or
311 .BR recvmsg (2).
312 .TP
313 .B EPROTONOSUPPORT
314 Passed protocol is not AF_UNIX.
315 .TP
316 .B EPROTOTYPE
317 Remote socket does not match the local socket type
318 .RB ( SOCK_DGRAM
319 vs.
320 .BR SOCK_STREAM )
321 .TP
322 .B ESOCKTNOSUPPORT
323 Unknown socket type.
324 .PP
325 Other errors can be generated by the generic socket layer or
326 by the file system while generating a file system socket object.
327 See the appropriate manual pages for more information.
328 .SH VERSIONS
329 .B SCM_CREDENTIALS
330 and the abstract namespace were introduced with Linux 2.2 and should not
331 be used in portable programs.
332 (Some BSD-derived systems also support credential passing,
333 but the implementation details differ.)
334 .SH NOTES
335 In the Linux implementation, sockets which are visible in the
336 file system honor the permissions of the directory they are in.
337 Their owner, group and their permissions can be changed.
338 Creation of a new socket will fail if the process does not have write and
339 search (execute) permission on the directory the socket is created in.
340 Connecting to the socket object requires read/write permission.
341 This behavior differs from many BSD-derived systems which
342 ignore permissions for Unix sockets.
343 Portable programs should not rely on
344 this feature for security.
345
346 Binding to a socket with a filename creates a socket
347 in the file system that must be deleted by the caller when it is no
348 longer needed (using
349 .BR unlink (2)).
350 The usual Unix close-behind semantics apply; the socket can be unlinked
351 at any time and will be finally removed from the file system when the last
352 reference to it is closed.
353
354 To pass file descriptors or credentials over a
355 .BR SOCK_STREAM ,
356 you need
357 to send or receive at least one byte of nonancillary data in the same
358 .BR sendmsg (2)
359 or
360 .BR recvmsg (2)
361 call.
362
363 Unix domain stream sockets do not support the notion of out-of-band data.
364 .SH EXAMPLE
365 See
366 .BR bind (2).
367 .SH "SEE ALSO"
368 .BR recvmsg (2),
369 .BR sendmsg (2),
370 .BR socket (2),
371 .BR socketpair (2),
372 .BR cmsg (3),
373 .BR capabilities (7),
374 .BR credentials (7),
375 .BR socket (7)