]>
Commit | Line | Data |
---|---|---|
7c4ddad1 AC |
1 | .\" SPDX-License-Identifier: Linux-man-pages-1-para |
2 | .\" | |
15545eb6 HS |
3 | .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>, |
4 | .\" Copyright (C) 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>, | |
5 | .\" and Copyright (C) 2016, Heinrich Schuchardt <xypron.glpk@gmx.de> | |
2297bf0e | 6 | .\" |
77117f4f MK |
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 | .\" | |
ab47278f | 15 | .TH UNIX 7 (date) "Linux man-pages (unreleased)" |
77117f4f | 16 | .SH NAME |
f68512e9 | 17 | unix \- sockets for local interprocess communication |
77117f4f | 18 | .SH SYNOPSIS |
c7db92b9 | 19 | .nf |
77117f4f | 20 | .B #include <sys/socket.h> |
77117f4f | 21 | .B #include <sys/un.h> |
f90f031e | 22 | .PP |
d4c8c97c | 23 | .IB unix_socket " = socket(AF_UNIX, type, 0);" |
d4c8c97c | 24 | .IB error " = socketpair(AF_UNIX, type, 0, int *" sv ");" |
c7db92b9 | 25 | .fi |
77117f4f MK |
26 | .SH DESCRIPTION |
27 | The | |
d4c8c97c | 28 | .B AF_UNIX |
77117f4f | 29 | (also known as |
d4c8c97c | 30 | .BR AF_LOCAL ) |
77117f4f MK |
31 | socket family is used to communicate between processes on the same machine |
32 | efficiently. | |
4891f52a | 33 | Traditionally, UNIX domain sockets can be either unnamed, |
9ee4a2b6 | 34 | or bound to a filesystem pathname (marked as being of type socket). |
77117f4f | 35 | Linux also supports an abstract namespace which is independent of the |
9ee4a2b6 | 36 | filesystem. |
5711c04f | 37 | .PP |
d02879f7 | 38 | Valid socket types in the UNIX domain are: |
77117f4f | 39 | .BR SOCK_STREAM , |
d02879f7 | 40 | for a stream-oriented socket; |
77117f4f MK |
41 | .BR SOCK_DGRAM , |
42 | for a datagram-oriented socket that preserves message boundaries | |
008f1ecc | 43 | (as on most UNIX implementations, UNIX domain datagram |
77117f4f MK |
44 | sockets are always reliable and don't reorder datagrams); |
45 | and (since Linux 2.6.4) | |
46 | .BR SOCK_SEQPACKET , | |
0d7e8d59 MK |
47 | for a sequenced-packet socket that is connection-oriented, |
48 | preserves message boundaries, | |
77117f4f | 49 | and delivers messages in the order that they were sent. |
5711c04f | 50 | .PP |
4891f52a | 51 | UNIX domain sockets support passing file descriptors or process credentials |
77117f4f | 52 | to other processes using ancillary data. |
c634028a | 53 | .SS Address format |
008f1ecc | 54 | A UNIX domain socket address is represented in the following structure: |
e646a1ba | 55 | .PP |
77117f4f | 56 | .in +4n |
e646a1ba | 57 | .EX |
63bc262c | 58 | .\" #define UNIX_PATH_MAX 108 |
5ffdc2fd | 59 | .\" |
77117f4f MK |
60 | struct sockaddr_un { |
61 | sa_family_t sun_family; /* AF_UNIX */ | |
b65f4c69 | 62 | char sun_path[108]; /* Pathname */ |
77117f4f | 63 | }; |
b8302363 | 64 | .EE |
77117f4f MK |
65 | .in |
66 | .PP | |
d02879f7 | 67 | The |
77117f4f | 68 | .I sun_family |
d02879f7 | 69 | field always contains |
77117f4f | 70 | .BR AF_UNIX . |
05bf3361 | 71 | On Linux, |
840aa3c7 | 72 | .I sun_path |
7903a735 | 73 | is 108 bytes in size; see also BUGS, below. |
5711c04f | 74 | .PP |
d02879f7 MK |
75 | Various systems calls (for example, |
76 | .BR bind (2), | |
77 | .BR connect (2), | |
78 | and | |
79 | .BR sendto (2)) | |
80 | take a | |
b8017cf5 | 81 | .I sockaddr_un |
d02879f7 MK |
82 | argument as input. |
83 | Some other system calls (for example, | |
84 | .BR getsockname (2), | |
85 | .BR getpeername (2), | |
86 | .BR recvfrom (2), | |
87 | and | |
88 | .BR accept (2)) | |
89 | return an argument of this type. | |
5711c04f | 90 | .PP |
d02879f7 MK |
91 | Three types of address are distinguished in the |
92 | .I sockaddr_un | |
93 | structure: | |
4279e42d AC |
94 | .TP |
95 | pathname | |
1c7b2458 MK |
96 | a UNIX domain socket can be bound to a null-terminated |
97 | filesystem pathname using | |
77117f4f | 98 | .BR bind (2). |
d02879f7 MK |
99 | When the address of a pathname socket is returned |
100 | (by one of the system calls noted above), | |
77117f4f | 101 | its length is |
5711c04f | 102 | .IP |
1ae6b2c7 AC |
103 | .in +4n |
104 | .EX | |
105 | offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1 | |
106 | .EE | |
107 | .in | |
5711c04f | 108 | .IP |
77117f4f MK |
109 | and |
110 | .I sun_path | |
111 | contains the null-terminated pathname. | |
d02879f7 MK |
112 | (On Linux, the above |
113 | .BR offsetof () | |
114 | expression equates to the same value as | |
115 | .IR sizeof(sa_family_t) , | |
116 | but some other implementations include other fields before | |
117 | .IR sun_path , | |
118 | so the | |
119 | .BR offsetof () | |
120 | expression more portably describes the size of the address structure.) | |
121 | .IP | |
122 | For further details of pathname sockets, see below. | |
4279e42d AC |
123 | .TP |
124 | unnamed | |
77117f4f MK |
125 | A stream socket that has not been bound to a pathname using |
126 | .BR bind (2) | |
127 | has no name. | |
128 | Likewise, the two sockets created by | |
129 | .BR socketpair (2) | |
130 | are unnamed. | |
d02879f7 | 131 | When the address of an unnamed socket is returned, |
77117f4f MK |
132 | its length is |
133 | .IR "sizeof(sa_family_t)" , | |
134 | and | |
135 | .I sun_path | |
136 | should not be inspected. | |
137 | .\" There is quite some variation across implementations: FreeBSD | |
138 | .\" says the length is 16 bytes, HP-UX 11 says it's zero bytes. | |
4279e42d AC |
139 | .TP |
140 | abstract | |
d02879f7 MK |
141 | an abstract socket address is distinguished (from a pathname socket) |
142 | by the fact that | |
1ae6b2c7 | 143 | .I sun_path[0] |
b957f81f | 144 | is a null byte (\[aq]\e0\[aq]). |
156a0e0d MK |
145 | The socket's address in this namespace is given by the additional |
146 | bytes in | |
1ae6b2c7 | 147 | .I sun_path |
156a0e0d | 148 | that are covered by the specified length of the address structure. |
77117f4f | 149 | (Null bytes in the name have no special significance.) |
9ee4a2b6 | 150 | The name has no connection with filesystem pathnames. |
d02879f7 | 151 | When the address of an abstract socket is returned, |
156a0e0d MK |
152 | the returned |
153 | .I addrlen | |
154 | is greater than | |
1ae6b2c7 | 155 | .I sizeof(sa_family_t) |
156a0e0d MK |
156 | (i.e., greater than 2), and the name of the socket is contained in |
157 | the first | |
1ae6b2c7 | 158 | .I (addrlen \- sizeof(sa_family_t)) |
156a0e0d MK |
159 | bytes of |
160 | .IR sun_path . | |
d02879f7 MK |
161 | .SS Pathname sockets |
162 | When binding a socket to a pathname, a few rules should be observed | |
163 | for maximum portability and ease of coding: | |
cdede5cd | 164 | .IP \[bu] 3 |
d02879f7 MK |
165 | The pathname in |
166 | .I sun_path | |
167 | should be null-terminated. | |
cdede5cd | 168 | .IP \[bu] |
d02879f7 MK |
169 | The length of the pathname, including the terminating null byte, |
170 | should not exceed the size of | |
171 | .IR sun_path . | |
cdede5cd | 172 | .IP \[bu] |
d02879f7 MK |
173 | The |
174 | .I addrlen | |
175 | argument that describes the enclosing | |
176 | .I sockaddr_un | |
177 | structure should have a value of at least: | |
5711c04f | 178 | .IP |
1ae6b2c7 AC |
179 | .in +4n |
180 | .EX | |
181 | offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1 | |
182 | .EE | |
183 | .in | |
d02879f7 | 184 | .IP |
b8017cf5 | 185 | or, more simply, |
d02879f7 MK |
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 . | |
5711c04f | 197 | .PP |
d02879f7 MK |
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 | |
5711c04f | 206 | .PP |
d02879f7 MK |
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.) | |
9f213833 MK |
224 | .\" |
225 | .SS Pathname socket ownership and permissions | |
226 | In the Linux implementation, | |
227 | pathname sockets honor the permissions of the directory they are in. | |
a23d8efa | 228 | Creation of a new socket fails if the process does not have write and |
9f213833 | 229 | search (execute) permission on the directory in which the socket is created. |
5711c04f | 230 | .PP |
9f213833 MK |
231 | On Linux, |
232 | connecting to a stream socket object requires write permission on that socket; | |
233 | sending a datagram to a datagram socket likewise | |
234 | requires write permission on that socket. | |
235 | POSIX does not make any statement about the effect of the permissions | |
7f98a239 | 236 | on a socket file, and on some systems (e.g., older BSDs), |
9f213833 MK |
237 | the socket permissions are ignored. |
238 | Portable programs should not rely on | |
239 | this feature for security. | |
5711c04f | 240 | .PP |
9f213833 MK |
241 | When creating a new socket, the owner and group of the socket file |
242 | are set according to the usual rules. | |
243 | The socket file has all permissions enabled, | |
244 | other than those that are turned off by the process | |
245 | .BR umask (2). | |
5711c04f | 246 | .PP |
9f213833 MK |
247 | The owner, group, and permissions of a pathname socket can be changed (using |
248 | .BR chown (2) | |
249 | and | |
250 | .BR chmod (2)). | |
251 | .\" However, fchown() and fchmod() do not seem to have an effect | |
252 | .\" | |
d1875c13 | 253 | .SS Abstract sockets |
44cca454 MK |
254 | Socket permissions have no meaning for abstract sockets: |
255 | the process | |
256 | .BR umask (2) | |
257 | has no effect when binding an abstract socket, | |
258 | and changing the ownership and permissions of the object (via | |
259 | .BR fchown (2) | |
260 | and | |
261 | .BR fchmod (2)) | |
262 | has no effect on the accessibility of the socket. | |
5711c04f | 263 | .PP |
d1875c13 MK |
264 | Abstract sockets automatically disappear when all open references |
265 | to the socket are closed. | |
5711c04f | 266 | .PP |
d1875c13 MK |
267 | The abstract socket namespace is a nonportable Linux extension. |
268 | .\" | |
c634028a | 269 | .SS Socket options |
464b254b | 270 | For historical reasons, these socket options are specified with a |
77117f4f MK |
271 | .B SOL_SOCKET |
272 | type even though they are | |
d4c8c97c | 273 | .B AF_UNIX |
77117f4f MK |
274 | specific. |
275 | They can be set with | |
276 | .BR setsockopt (2) | |
277 | and read with | |
278 | .BR getsockopt (2) | |
279 | by specifying | |
280 | .B SOL_SOCKET | |
281 | as the socket family. | |
282 | .TP | |
283 | .B SO_PASSCRED | |
928c3e7c | 284 | Enabling this socket option causes receipt of the credentials of |
421d34c6 MK |
285 | the sending process in an |
286 | .B SCM_CREDENTIALS ancillary | |
928c3e7c | 287 | message in each subsequently received message. |
421d34c6 MK |
288 | The returned credentials are those specified by the sender using |
289 | .BR SCM_CREDENTIALS , | |
290 | or a default that includes the sender's PID, real user ID, and real group ID, | |
291 | if the sender did not specify | |
292 | .B SCM_CREDENTIALS | |
293 | ancillary data. | |
c02ed554 MK |
294 | .IP |
295 | When this option is set and the socket is not yet connected, | |
77117f4f | 296 | a unique name in the abstract namespace will be generated automatically. |
744c8fa8 MK |
297 | .IP |
298 | The value given as an argument to | |
299 | .BR setsockopt (2) | |
300 | and returned as the result of | |
301 | .BR getsockopt (2) | |
302 | is an integer boolean flag. | |
366a9bff MK |
303 | .TP |
304 | .B SO_PASSSEC | |
ffad6a01 MK |
305 | Enables receiving of the SELinux security label of the peer socket |
306 | in an ancillary message of type | |
1ae6b2c7 | 307 | .B SCM_SECURITY |
ffad6a01 | 308 | (see below). |
744c8fa8 MK |
309 | .IP |
310 | The value given as an argument to | |
311 | .BR setsockopt (2) | |
312 | and returned as the result of | |
313 | .BR getsockopt (2) | |
314 | is an integer boolean flag. | |
315 | .IP | |
316 | The | |
317 | .B SO_PASSSEC | |
318 | option is supported for UNIX domain datagram sockets | |
366a9bff MK |
319 | .\" commit 877ce7c1b3afd69a9b1caeb1b9964c992641f52a |
320 | since Linux 2.6.18; | |
321 | support for UNIX domain stream sockets was added | |
322 | .\" commit 37a9a8df8ce9de6ea73349c9ac8bdf6ba4ec4f70 | |
323 | in Linux 4.2. | |
ffab8460 | 324 | .TP |
1ae6b2c7 | 325 | .B SO_PEEK_OFF |
ffab8460 MK |
326 | See |
327 | .BR socket (7). | |
94950b9a MK |
328 | .TP |
329 | .B SO_PEERCRED | |
3d3cddde MK |
330 | This read-only socket option returns the |
331 | credentials of the peer process connected to this socket. | |
94950b9a MK |
332 | The returned credentials are those that were in effect at the time |
333 | of the call to | |
334 | .BR connect (2) | |
335 | or | |
336 | .BR socketpair (2). | |
3d3cddde MK |
337 | .IP |
338 | The argument to | |
339 | .BR getsockopt (2) | |
340 | is a pointer to a | |
94950b9a MK |
341 | .I ucred |
342 | structure; define the | |
343 | .B _GNU_SOURCE | |
344 | feature test macro to obtain the definition of that structure from | |
345 | .IR <sys/socket.h> . | |
3d3cddde MK |
346 | .IP |
347 | The use of this option is possible only for connected | |
348 | .B AF_UNIX | |
349 | stream sockets and for | |
350 | .B AF_UNIX | |
351 | stream and datagram socket pairs created using | |
352 | .BR socketpair (2). | |
e6f90c3f SS |
353 | .TP |
354 | .B SO_PEERSEC | |
355 | This read-only socket option returns the | |
356 | security context of the peer socket connected to this socket. | |
357 | By default, this will be the same as the security context of | |
358 | the process that created the peer socket unless overridden | |
359 | by the policy or by a process with the required permissions. | |
360 | .IP | |
361 | The argument to | |
362 | .BR getsockopt (2) | |
14c11c82 | 363 | is a pointer to a buffer of the specified length in bytes |
e6f90c3f SS |
364 | into which the security context string will be copied. |
365 | If the buffer length is less than the length of the security | |
366 | context string, then | |
367 | .BR getsockopt (2) | |
29494dfe | 368 | returns \-1, sets |
e6f90c3f SS |
369 | .I errno |
370 | to | |
29494dfe MK |
371 | .BR ERANGE , |
372 | and returns the required length via | |
373 | .IR optlen . | |
e6f90c3f | 374 | The caller should allocate at least |
1ae6b2c7 | 375 | .B NAME_MAX |
29494dfe | 376 | bytes for the buffer initially, although this is not guaranteed |
14c11c82 MK |
377 | to be sufficient. |
378 | Resizing the buffer to the returned length | |
e6f90c3f SS |
379 | and retrying may be necessary. |
380 | .IP | |
381 | The security context string may include a terminating null character | |
382 | in the returned length, but is not guaranteed to do so: a security | |
383 | context "foo" might be represented as either {'f','o','o'} of length 3 | |
384 | or {'f','o','o','\\0'} of length 4, which are considered to be | |
14c11c82 | 385 | interchangeable. |
29494dfe MK |
386 | The string is printable, does not contain non-terminating null characters, |
387 | and is in an unspecified encoding (in particular, it | |
e6f90c3f SS |
388 | is not guaranteed to be ASCII or UTF-8). |
389 | .IP | |
390 | The use of this option for sockets in the | |
391 | .B AF_UNIX | |
29494dfe MK |
392 | address family is supported since Linux 2.6.2 for connected stream sockets, |
393 | and since Linux 4.18 | |
e6f90c3f | 394 | .\" commit 0b811db2cb2aabc910e53d34ebb95a15997c33e7 |
14c11c82 | 395 | also for stream and datagram socket pairs created using |
e6f90c3f | 396 | .BR socketpair (2). |
366a9bff | 397 | .\" |
c634028a | 398 | .SS Autobind feature |
0cf2caa4 | 399 | If a |
0b80cf56 | 400 | .BR bind (2) |
0cf2caa4 MK |
401 | call specifies |
402 | .I addrlen | |
403 | as | |
404 | .IR sizeof(sa_family_t) , | |
449dd4e2 | 405 | .\" i.e., sizeof(short) |
0cf2caa4 | 406 | or the |
1ae6b2c7 | 407 | .B SO_PASSCRED |
0cf2caa4 MK |
408 | socket option was specified for a socket that was |
409 | not explicitly bound to an address, | |
410 | then the socket is autobound to an abstract address. | |
411 | The address consists of a null byte | |
412 | followed by 5 bytes in the character set | |
9bc87ed0 | 413 | .IR [0\-9a\-f] . |
e4b01162 | 414 | Thus, there is a limit of 2\[ha]20 autobind addresses. |
1e4e3bad | 415 | (From Linux 2.1.15, when the autobind feature was added, |
e4b01162 | 416 | 8 bytes were used, and the limit was thus 2\[ha]32 autobind addresses. |
1e4e3bad | 417 | The change to 5 bytes came in Linux 2.3.15.) |
19e19f5f | 418 | .SS Sockets API |
77117f4f | 419 | The following paragraphs describe domain-specific details and |
008f1ecc | 420 | unsupported features of the sockets API for UNIX domain sockets on Linux. |
5711c04f | 421 | .PP |
008f1ecc | 422 | UNIX domain sockets do not support the transmission of |
77117f4f MK |
423 | out-of-band data (the |
424 | .B MSG_OOB | |
425 | flag for | |
426 | .BR send (2) | |
427 | and | |
428 | .BR recv (2)). | |
5711c04f | 429 | .PP |
77117f4f MK |
430 | The |
431 | .BR send (2) | |
432 | .B MSG_MORE | |
008f1ecc | 433 | flag is not supported by UNIX domain sockets. |
5711c04f | 434 | .PP |
c9a39fea MK |
435 | Before Linux 3.4, |
436 | .\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f | |
437 | the use of | |
77e75b90 MK |
438 | .B MSG_TRUNC |
439 | in the | |
440 | .I flags | |
441 | argument of | |
442 | .BR recv (2) | |
c9a39fea | 443 | was not supported by UNIX domain sockets. |
5711c04f | 444 | .PP |
77117f4f MK |
445 | The |
446 | .B SO_SNDBUF | |
008f1ecc | 447 | socket option does have an effect for UNIX domain sockets, but the |
77117f4f MK |
448 | .B SO_RCVBUF |
449 | option does not. | |
450 | For datagram sockets, the | |
451 | .B SO_SNDBUF | |
452 | value imposes an upper limit on the size of outgoing datagrams. | |
453 | This limit is calculated as the doubled (see | |
454 | .BR socket (7)) | |
455 | option value less 32 bytes used for overhead. | |
c634028a | 456 | .SS Ancillary messages |
77117f4f MK |
457 | Ancillary data is sent and received using |
458 | .BR sendmsg (2) | |
459 | and | |
460 | .BR recvmsg (2). | |
05bf3361 | 461 | For historical reasons, the ancillary message types listed below |
77117f4f MK |
462 | are specified with a |
463 | .B SOL_SOCKET | |
464 | type even though they are | |
d4c8c97c | 465 | .B AF_UNIX |
77117f4f | 466 | specific. |
05bf3361 | 467 | To send them, set the |
77117f4f MK |
468 | .I cmsg_level |
469 | field of the struct | |
470 | .I cmsghdr | |
471 | to | |
472 | .B SOL_SOCKET | |
473 | and the | |
474 | .I cmsg_type | |
475 | field to the type. | |
05bf3361 | 476 | For more information, see |
77117f4f MK |
477 | .BR cmsg (3). |
478 | .TP | |
479 | .B SCM_RIGHTS | |
480 | Send or receive a set of open file descriptors from another process. | |
481 | The data portion contains an integer array of the file descriptors. | |
87996200 | 482 | .IP |
13600496 MK |
483 | Commonly, this operation is referred to as "passing a file descriptor" |
484 | to another process. | |
485 | However, more accurately, | |
486 | what is being passed is a reference to an open file description (see | |
487 | .BR open (2)), | |
488 | and in the receiving process it is likely that a different | |
489 | file descriptor number will be used. | |
490 | Semantically, this operation is equivalent to duplicating | |
491 | .RB ( dup (2)) | |
492 | a file descriptor into the file descriptor table of another process. | |
8bdcf4bf | 493 | .IP |
4564dd1f MK |
494 | If the buffer used to receive the ancillary data containing |
495 | file descriptors is too small (or is absent), | |
496 | then the ancillary data is truncated (or discarded) | |
497 | and the excess file descriptors are automatically closed | |
498 | in the receiving process. | |
499 | .IP | |
e2340cf7 MK |
500 | If the number of file descriptors received in the ancillary data would |
501 | cause the process to exceed its | |
502 | .B RLIMIT_NOFILE | |
503 | resource limit (see | |
504 | .BR getrlimit (2)), | |
505 | the excess file descriptors are automatically closed | |
506 | in the receiving process. | |
507 | .IP | |
8bdcf4bf | 508 | The kernel constant |
1ae6b2c7 | 509 | .B SCM_MAX_FD |
8bdcf4bf | 510 | defines a limit on the number of file descriptors in the array. |
1221abb6 | 511 | Attempting to send an array larger than this limit causes |
8bdcf4bf MK |
512 | .BR sendmsg (2) |
513 | to fail with the error | |
514 | .BR EINVAL . | |
1ae6b2c7 | 515 | .B SCM_MAX_FD |
8bdcf4bf | 516 | has the value 253 |
8bdcf4bf | 517 | .\" commit bba14de98753cb6599a2dae0e520714b2153522d |
b324e17d | 518 | (or 255 before Linux 2.6.38). |
77117f4f MK |
519 | .TP |
520 | .B SCM_CREDENTIALS | |
008f1ecc | 521 | Send or receive UNIX credentials. |
77117f4f MK |
522 | This can be used for authentication. |
523 | The credentials are passed as a | |
524 | .I struct ucred | |
525 | ancillary message. | |
6a141329 | 526 | This structure is defined in |
b1587ca8 MK |
527 | .I <sys/socket.h> |
528 | as follows: | |
5711c04f | 529 | .IP |
77117f4f | 530 | .in +4n |
b8302363 | 531 | .EX |
77117f4f | 532 | struct ucred { |
b65f4c69 MK |
533 | pid_t pid; /* Process ID of the sending process */ |
534 | uid_t uid; /* User ID of the sending process */ | |
535 | gid_t gid; /* Group ID of the sending process */ | |
77117f4f | 536 | }; |
b8302363 | 537 | .EE |
77117f4f | 538 | .in |
5711c04f | 539 | .IP |
b1587ca8 | 540 | Since glibc 2.8, the |
1bc510f5 | 541 | .B _GNU_SOURCE |
e417acb0 MK |
542 | feature test macro must be defined (before including |
543 | .I any | |
544 | header files) in order to obtain the definition | |
b1587ca8 | 545 | of this structure. |
5711c04f | 546 | .IP |
77117f4f | 547 | The credentials which the sender specifies are checked by the kernel. |
f1081bdc | 548 | A privileged process is allowed to specify values that do not match its own. |
77117f4f | 549 | The sender must specify its own process ID (unless it has the capability |
863d6b7d MK |
550 | .BR CAP_SYS_ADMIN , |
551 | in which case the PID of any existing process may be specified), | |
06b8a13b | 552 | its real user ID, effective user ID, or saved set-user-ID (unless it has |
77117f4f | 553 | .BR CAP_SETUID ), |
06b8a13b | 554 | and its real group ID, effective group ID, or saved set-group-ID |
77117f4f MK |
555 | (unless it has |
556 | .BR CAP_SETGID ). | |
bdef8021 | 557 | .IP |
77117f4f MK |
558 | To receive a |
559 | .I struct ucred | |
b66d5714 | 560 | message, the |
77117f4f MK |
561 | .B SO_PASSCRED |
562 | option must be enabled on the socket. | |
ffad6a01 MK |
563 | .TP |
564 | .B SCM_SECURITY | |
565 | Receive the SELinux security context (the security label) | |
566 | of the peer socket. | |
567 | The received ancillary data is a null-terminated string containing | |
568 | the security context. | |
569 | The receiver should allocate at least | |
1ae6b2c7 | 570 | .B NAME_MAX |
ffad6a01 MK |
571 | bytes in the data portion of the ancillary message for this data. |
572 | .IP | |
573 | To receive the security context, the | |
574 | .B SO_PASSSEC | |
575 | option must be enabled on the socket (see above). | |
c8772146 | 576 | .PP |
5b5cb195 MK |
577 | When sending ancillary data with |
578 | .BR sendmsg (2), | |
579 | only one item of each of the above types may be included in the sent message. | |
580 | .PP | |
5219daec MK |
581 | At least one byte of real data should be sent when sending ancillary data. |
582 | On Linux, this is required to successfully send ancillary data over | |
583 | a UNIX domain stream socket. | |
584 | When sending ancillary data over a UNIX domain datagram socket, | |
585 | it is not necessary on Linux to send any accompanying real data. | |
586 | However, portable applications should also include at least one byte | |
587 | of real data when sending ancillary data over a datagram socket. | |
588 | .PP | |
5af0f223 MK |
589 | When receiving from a stream socket, |
590 | ancillary data forms a kind of barrier for the received data. | |
591 | For example, suppose that the sender transmits as follows: | |
592 | .PP | |
593 | .RS | |
594 | .PD 0 | |
22356d97 | 595 | .IP (1) 5 |
5af0f223 MK |
596 | .BR sendmsg (2) |
597 | of four bytes, with no ancillary data. | |
22356d97 | 598 | .IP (2) |
5af0f223 MK |
599 | .BR sendmsg (2) |
600 | of one byte, with ancillary data. | |
22356d97 | 601 | .IP (3) |
5af0f223 MK |
602 | .BR sendmsg (2) |
603 | of four bytes, with no ancillary data. | |
604 | .PD | |
605 | .RE | |
606 | .PP | |
607 | Suppose that the receiver now performs | |
608 | .BR recvmsg (2) | |
609 | calls each with a buffer size of 20 bytes. | |
610 | The first call will receive five bytes of data, | |
611 | along with the ancillary data sent by the second | |
612 | .BR sendmsg (2) | |
613 | call. | |
897367f9 | 614 | The next call will receive the remaining four bytes of data. |
5af0f223 | 615 | .PP |
c0e56ed6 | 616 | If the space allocated for receiving incoming ancillary data is too small |
c8772146 MK |
617 | then the ancillary data is truncated to the number of headers |
618 | that will fit in the supplied buffer (or, in the case of an | |
1ae6b2c7 | 619 | .B SCM_RIGHTS |
c8772146 | 620 | file descriptor list, the list of file descriptors may be truncated). |
c0e56ed6 MK |
621 | If no buffer is provided for incoming ancillary data (i.e., the |
622 | .I msg_control | |
623 | field of the | |
624 | .I msghdr | |
625 | structure supplied to | |
626 | .BR recvmsg (2) | |
627 | is NULL), | |
628 | then the incoming ancillary data is discarded. | |
629 | In both of these cases, the | |
1ae6b2c7 | 630 | .B MSG_CTRUNC |
c8772146 MK |
631 | flag will be set in the |
632 | .I msg.msg_flags | |
633 | value returned by | |
634 | .BR recvmsg (2). | |
635 | .\" | |
fbea0f81 MK |
636 | .SS Ioctls |
637 | The following | |
638 | .BR ioctl (2) | |
639 | calls return information in | |
640 | .IR value . | |
641 | The correct syntax is: | |
642 | .PP | |
643 | .RS | |
644 | .nf | |
645 | .BI int " value"; | |
f0d77d97 | 646 | .IB error " = ioctl(" unix_socket ", " ioctl_type ", &" value ");" |
fbea0f81 MK |
647 | .fi |
648 | .RE | |
649 | .PP | |
650 | .I ioctl_type | |
651 | can be: | |
652 | .TP | |
653 | .B SIOCINQ | |
170e5f0d JC |
654 | For |
655 | .B SOCK_STREAM | |
311bf2f6 | 656 | sockets, this call returns the number of unread bytes in the receive buffer. |
fbea0f81 MK |
657 | The socket must not be in LISTEN state, otherwise an error |
658 | .RB ( EINVAL ) | |
659 | is returned. | |
660 | .B SIOCINQ | |
661 | is defined in | |
662 | .IR <linux/sockios.h> . | |
fd00f831 | 663 | .\" FIXME . https://www.sourceware.org/bugzilla/show_bug.cgi?id=12002, |
fbea0f81 MK |
664 | .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers |
665 | Alternatively, | |
666 | you can use the synonymous | |
667 | .BR FIONREAD , | |
668 | defined in | |
669 | .IR <sys/ioctl.h> . | |
7aed61d9 | 670 | .\" SIOCOUTQ also has an effect for UNIX domain sockets, but not |
fbea0f81 MK |
671 | .\" quite what userland might expect. It seems to return the number |
672 | .\" of bytes allocated for buffers containing pending output. | |
673 | .\" That number is normally larger than the number of bytes of pending | |
674 | .\" output. Since this info is, from userland's point of view, imprecise, | |
675 | .\" and it may well change, probably best not to document this now. | |
170e5f0d JC |
676 | For |
677 | .B SOCK_DGRAM | |
311bf2f6 | 678 | sockets, |
170e5f0d | 679 | the returned value is the same as |
311bf2f6 | 680 | for Internet domain datagram sockets; |
170e5f0d JC |
681 | see |
682 | .BR udp (7). | |
77117f4f MK |
683 | .SH ERRORS |
684 | .TP | |
685 | .B EADDRINUSE | |
9ee4a2b6 | 686 | The specified local address is already in use or the filesystem socket |
77117f4f MK |
687 | object already exists. |
688 | .TP | |
7934bcdf MK |
689 | .B EBADF |
690 | This error can occur for | |
691 | .BR sendmsg (2) | |
692 | when sending a file descriptor as ancillary data over | |
693 | a UNIX domain socket (see the description of | |
694 | .BR SCM_RIGHTS , | |
695 | above), and indicates that the file descriptor number that | |
3eb078c5 | 696 | is being sent is not valid (e.g., it is not an open file descriptor). |
7934bcdf | 697 | .TP |
77117f4f | 698 | .B ECONNREFUSED |
1fe284ab | 699 | The remote address specified by |
77117f4f | 700 | .BR connect (2) |
1fe284ab | 701 | was not a listening socket. |
d02879f7 | 702 | This error can also occur if the target pathname is not a socket. |
77117f4f MK |
703 | .TP |
704 | .B ECONNRESET | |
705 | Remote socket was unexpectedly closed. | |
706 | .TP | |
707 | .B EFAULT | |
708 | User memory address was not valid. | |
709 | .TP | |
710 | .B EINVAL | |
711 | Invalid argument passed. | |
1fe284ab | 712 | A common cause is that the value |
40656bc7 | 713 | .B AF_UNIX |
1fe284ab | 714 | was not specified in the |
77117f4f | 715 | .I sun_type |
1fe284ab | 716 | field of passed addresses, or the socket was in an |
77117f4f MK |
717 | invalid state for the applied operation. |
718 | .TP | |
719 | .B EISCONN | |
720 | .BR connect (2) | |
721 | called on an already connected socket or a target address was | |
722 | specified on a connected socket. | |
723 | .TP | |
0e6dbf30 KI |
724 | .B ENFILE |
725 | The system-wide limit on the total number of open files has been reached. | |
726 | .TP | |
ec55a2b6 MK |
727 | .B ENOENT |
728 | The pathname in the remote address specified to | |
9470f355 | 729 | .BR connect (2) |
ec55a2b6 MK |
730 | did not exist. |
731 | .TP | |
77117f4f MK |
732 | .B ENOMEM |
733 | Out of memory. | |
734 | .TP | |
735 | .B ENOTCONN | |
736 | Socket operation needs a target address, but the socket is not connected. | |
737 | .TP | |
738 | .B EOPNOTSUPP | |
739 | Stream operation called on non-stream oriented socket or tried to | |
740 | use the out-of-band data option. | |
741 | .TP | |
742 | .B EPERM | |
743 | The sender passed invalid credentials in the | |
744 | .IR "struct ucred" . | |
745 | .TP | |
746 | .B EPIPE | |
747 | Remote socket was closed on a stream socket. | |
748 | If enabled, a | |
749 | .B SIGPIPE | |
750 | is sent as well. | |
751 | This can be avoided by passing the | |
752 | .B MSG_NOSIGNAL | |
753 | flag to | |
110039c1 | 754 | .BR send (2) |
77117f4f | 755 | or |
110039c1 | 756 | .BR sendmsg (2). |
77117f4f MK |
757 | .TP |
758 | .B EPROTONOSUPPORT | |
cd0221ea MK |
759 | Passed protocol is not |
760 | .BR AF_UNIX . | |
77117f4f MK |
761 | .TP |
762 | .B EPROTOTYPE | |
763 | Remote socket does not match the local socket type | |
764 | .RB ( SOCK_DGRAM | |
d1c9ea80 | 765 | versus |
9ca2e0c1 | 766 | .BR SOCK_STREAM ). |
77117f4f MK |
767 | .TP |
768 | .B ESOCKTNOSUPPORT | |
769 | Unknown socket type. | |
dc4eea68 | 770 | .TP |
863d6b7d MK |
771 | .B ESRCH |
772 | While sending an ancillary message containing credentials | |
773 | .RB ( SCM_CREDENTIALS ), | |
774 | the caller specified a PID that does not match any existing process. | |
775 | .TP | |
dc4eea68 MK |
776 | .B ETOOMANYREFS |
777 | This error can occur for | |
778 | .BR sendmsg (2) | |
4529d4e5 | 779 | when sending a file descriptor as ancillary data over |
dc4eea68 MK |
780 | a UNIX domain socket (see the description of |
781 | .BR SCM_RIGHTS , | |
782 | above). | |
783 | It occurs if the number of "in-flight" file descriptors exceeds the | |
784 | .B RLIMIT_NOFILE | |
785 | resource limit and the caller does not have the | |
1ae6b2c7 | 786 | .B CAP_SYS_RESOURCE |
dc4eea68 MK |
787 | capability. |
788 | An in-flight file descriptor is one that has been sent using | |
789 | .BR sendmsg (2) | |
790 | but has not yet been accepted in the recipient process using | |
791 | .BR recvmsg (2). | |
5711c04f | 792 | .IP |
70fdcbc2 MK |
793 | This error is diagnosed since mainline Linux 4.5 |
794 | (and in some earlier kernel versions where the fix has been backported). | |
dc4eea68 MK |
795 | .\" commit 712f4aad406bb1ed67f3f98d04c044191f0ff593 |
796 | In earlier kernel versions, | |
797 | it was possible to place an unlimited number of file descriptors in flight, | |
798 | by sending each file descriptor with | |
799 | .BR sendmsg (2) | |
800 | and then closing the file descriptor so that it was not accounted against the | |
801 | .B RLIMIT_NOFILE | |
802 | resource limit. | |
77117f4f MK |
803 | .PP |
804 | Other errors can be generated by the generic socket layer or | |
9ee4a2b6 | 805 | by the filesystem while generating a filesystem socket object. |
77117f4f MK |
806 | See the appropriate manual pages for more information. |
807 | .SH VERSIONS | |
808 | .B SCM_CREDENTIALS | |
809 | and the abstract namespace were introduced with Linux 2.2 and should not | |
810 | be used in portable programs. | |
811 | (Some BSD-derived systems also support credential passing, | |
812 | but the implementation details differ.) | |
813 | .SH NOTES | |
00b78c5f MK |
814 | Binding to a socket with a filename creates a socket |
815 | in the filesystem that must be deleted by the caller when it is no | |
816 | longer needed (using | |
817 | .BR unlink (2)). | |
818 | The usual UNIX close-behind semantics apply; the socket can be unlinked | |
819 | at any time and will be finally removed from the filesystem when the last | |
820 | reference to it is closed. | |
5711c04f | 821 | .PP |
00b78c5f | 822 | To pass file descriptors or credentials over a |
1ae6b2c7 | 823 | .B SOCK_STREAM |
d3e7786d | 824 | socket, you must |
dbba2b26 | 825 | send or receive at least one byte of nonancillary data in the same |
00b78c5f MK |
826 | .BR sendmsg (2) |
827 | or | |
828 | .BR recvmsg (2) | |
829 | call. | |
5711c04f | 830 | .PP |
00b78c5f MK |
831 | UNIX domain stream sockets do not support the notion of out-of-band data. |
832 | .\" | |
d02879f7 MK |
833 | .SH BUGS |
834 | When binding a socket to an address, | |
835 | Linux is one of the implementations that appends a null terminator | |
836 | if none is supplied in | |
837 | .IR sun_path . | |
838 | In most cases this is unproblematic: | |
839 | when the socket address is retrieved, | |
840 | it will be one byte longer than that supplied when the socket was bound. | |
841 | However, there is one case where confusing behavior can result: | |
842 | if 108 non-null bytes are supplied when a socket is bound, | |
843 | then the addition of the null terminator takes the length of | |
844 | the pathname beyond | |
845 | .IR sizeof(sun_path) . | |
846 | Consequently, when retrieving the socket address | |
847 | (for example, via | |
848 | .BR accept (2)), | |
849 | .\" The behavior on Solaris is quite similar. | |
850 | if the input | |
851 | .I addrlen | |
852 | argument for the retrieving call is specified as | |
853 | .IR "sizeof(struct sockaddr_un)" , | |
854 | then the returned address structure | |
855 | .I won't | |
856 | have a null terminator in | |
857 | .IR sun_path . | |
5711c04f | 858 | .PP |
d02879f7 MK |
859 | In addition, some implementations |
860 | .\" i.e., traditional BSD | |
861 | don't require a null terminator when binding a socket (the | |
862 | .I addrlen | |
863 | argument is used to determine the length of | |
864 | .IR sun_path ) | |
865 | and when the socket address is retrieved on these implementations, | |
866 | there is no null terminator in | |
867 | .IR sun_path . | |
5711c04f | 868 | .PP |
b8017cf5 | 869 | Applications that retrieve socket addresses can (portably) code |
d02879f7 | 870 | to handle the possibility that there is no null terminator in |
1ae6b2c7 | 871 | .I sun_path |
d02879f7 | 872 | by respecting the fact that the number of valid bytes in the pathname is: |
5711c04f | 873 | .PP |
1ae6b2c7 AC |
874 | .in +4n |
875 | .EX | |
876 | strnlen(addr.sun_path, addrlen \- offsetof(sockaddr_un, sun_path)) | |
877 | .EE | |
878 | .in | |
d02879f7 MK |
879 | .\" The following patch to amend kernel behavior was rejected: |
880 | .\" http://thread.gmane.org/gmane.linux.kernel.api/2437 | |
881 | .\" Subject: [patch] Fix handling of overlength pathname in AF_UNIX sun_path | |
882 | .\" 2012-04-17 | |
883 | .\" And there was a related discussion in the Austin list: | |
884 | .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/5735 | |
885 | .\" Subject: Having a sun_path with no null terminator | |
886 | .\" 2012-04-18 | |
887 | .\" | |
888 | .\" FIXME . Track http://austingroupbugs.net/view.php?id=561 | |
5711c04f | 889 | .PP |
d02879f7 MK |
890 | Alternatively, an application can retrieve |
891 | the socket address by allocating a buffer of size | |
892 | .I "sizeof(struct sockaddr_un)+1" | |
893 | that is zeroed out before the retrieval. | |
894 | The retrieving call can specify | |
895 | .I addrlen | |
896 | as | |
897 | .IR "sizeof(struct sockaddr_un)" , | |
898 | and the extra zero byte ensures that there will be | |
899 | a null terminator for the string returned in | |
900 | .IR sun_path : | |
5711c04f | 901 | .PP |
a2b7a144 MK |
902 | .in +4n |
903 | .EX | |
d02879f7 | 904 | void *addrp; |
fe5dba13 | 905 | \& |
d02879f7 MK |
906 | addrlen = sizeof(struct sockaddr_un); |
907 | addrp = malloc(addrlen + 1); | |
908 | if (addrp == NULL) | |
909 | /* Handle error */ ; | |
910 | memset(addrp, 0, addrlen + 1); | |
fe5dba13 | 911 | \& |
3e35b19b | 912 | if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1) |
d02879f7 | 913 | /* handle error */ ; |
fe5dba13 | 914 | \& |
d1a71985 | 915 | printf("sun_path = %s\en", ((struct sockaddr_un *) addrp)\->sun_path); |
b8302363 | 916 | .EE |
e646a1ba | 917 | .in |
5711c04f | 918 | .PP |
d02879f7 MK |
919 | This sort of messiness can be avoided if it is guaranteed |
920 | that the applications that | |
921 | .I create | |
922 | pathname sockets follow the rules outlined above under | |
923 | .IR "Pathname sockets" . | |
a14af333 | 924 | .SH EXAMPLES |
84c8cae2 MK |
925 | The following code demonstrates the use of sequenced-packet |
926 | sockets for local interprocess communication. | |
eb73e8ad | 927 | It consists of two programs. |
15545eb6 | 928 | The server program waits for a connection from the client program. |
84c8cae2 MK |
929 | The client sends each of its command-line arguments in separate messages. |
930 | The server treats the incoming messages as integers and adds them up. | |
eb73e8ad | 931 | The client sends the command string "END". |
84c8cae2 MK |
932 | The server sends back a message containing the sum of the client's integers. |
933 | The client prints the sum and exits. | |
15545eb6 | 934 | The server waits for the next client to connect. |
84c8cae2 | 935 | To stop the server, the client is called with the command-line argument "DOWN". |
15545eb6 HS |
936 | .PP |
937 | The following output was recorded while running the server in the background | |
84c8cae2 MK |
938 | and repeatedly executing the client. |
939 | Execution of the server program ends when it receives the "DOWN" command. | |
15545eb6 HS |
940 | .SS Example output |
941 | .in +4n | |
b8302363 | 942 | .EX |
eb73e8ad | 943 | $ \fB./server &\fP |
15545eb6 | 944 | [1] 25887 |
eb73e8ad | 945 | $ \fB./client 3 4\fP |
15545eb6 | 946 | Result = 7 |
eb73e8ad | 947 | $ \fB./client 11 \-5\fP |
15545eb6 | 948 | Result = 6 |
eb73e8ad | 949 | $ \fB./client DOWN\fP |
15545eb6 HS |
950 | Result = 0 |
951 | [1]+ Done ./server | |
952 | $ | |
b8302363 | 953 | .EE |
15545eb6 HS |
954 | .in |
955 | .SS Program source | |
c7885256 | 956 | \& |
e7d0bb47 | 957 | .EX |
15545eb6 HS |
958 | /* |
959 | * File connection.h | |
960 | */ | |
fe5dba13 | 961 | \& |
15545eb6 HS |
962 | #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket" |
963 | #define BUFFER_SIZE 12 | |
fe5dba13 | 964 | \& |
15545eb6 HS |
965 | /* |
966 | * File server.c | |
967 | */ | |
fe5dba13 | 968 | \& |
15545eb6 HS |
969 | #include <stdio.h> |
970 | #include <stdlib.h> | |
971 | #include <string.h> | |
972 | #include <sys/socket.h> | |
973 | #include <sys/un.h> | |
974 | #include <unistd.h> | |
975 | #include "connection.h" | |
fe5dba13 | 976 | \& |
15545eb6 HS |
977 | int |
978 | main(int argc, char *argv[]) | |
979 | { | |
980 | struct sockaddr_un name; | |
981 | int down_flag = 0; | |
982 | int ret; | |
983 | int connection_socket; | |
984 | int data_socket; | |
985 | int result; | |
986 | char buffer[BUFFER_SIZE]; | |
fe5dba13 | 987 | \& |
46b20ca1 | 988 | /* Create local socket. */ |
fe5dba13 | 989 | \& |
15545eb6 HS |
990 | connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
991 | if (connection_socket == \-1) { | |
992 | perror("socket"); | |
993 | exit(EXIT_FAILURE); | |
994 | } | |
fe5dba13 | 995 | \& |
15545eb6 | 996 | /* |
eb73e8ad MK |
997 | * For portability clear the whole structure, since some |
998 | * implementations have additional (nonstandard) fields in | |
999 | * the structure. | |
15545eb6 | 1000 | */ |
fe5dba13 | 1001 | \& |
012b86a0 | 1002 | memset(&name, 0, sizeof(name)); |
fe5dba13 | 1003 | \& |
46b20ca1 | 1004 | /* Bind socket to socket name. */ |
fe5dba13 | 1005 | \& |
15545eb6 HS |
1006 | name.sun_family = AF_UNIX; |
1007 | strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1); | |
fe5dba13 | 1008 | \& |
15545eb6 | 1009 | ret = bind(connection_socket, (const struct sockaddr *) &name, |
012b86a0 | 1010 | sizeof(name)); |
15545eb6 HS |
1011 | if (ret == \-1) { |
1012 | perror("bind"); | |
1013 | exit(EXIT_FAILURE); | |
1014 | } | |
fe5dba13 | 1015 | \& |
15545eb6 | 1016 | /* |
eb73e8ad MK |
1017 | * Prepare for accepting connections. The backlog size is set |
1018 | * to 20. So while one request is being processed other requests | |
1019 | * can be waiting. | |
15545eb6 | 1020 | */ |
fe5dba13 | 1021 | \& |
15545eb6 HS |
1022 | ret = listen(connection_socket, 20); |
1023 | if (ret == \-1) { | |
1024 | perror("listen"); | |
1025 | exit(EXIT_FAILURE); | |
1026 | } | |
fe5dba13 | 1027 | \& |
46b20ca1 | 1028 | /* This is the main loop for handling connections. */ |
fe5dba13 | 1029 | \& |
15545eb6 | 1030 | for (;;) { |
fe5dba13 | 1031 | \& |
46b20ca1 | 1032 | /* Wait for incoming connection. */ |
fe5dba13 | 1033 | \& |
15545eb6 | 1034 | data_socket = accept(connection_socket, NULL, NULL); |
3cb43b95 | 1035 | if (data_socket == \-1) { |
15545eb6 HS |
1036 | perror("accept"); |
1037 | exit(EXIT_FAILURE); | |
1038 | } | |
fe5dba13 | 1039 | \& |
15545eb6 | 1040 | result = 0; |
52900faa | 1041 | for (;;) { |
fe5dba13 | 1042 | \& |
46b20ca1 | 1043 | /* Wait for next data packet. */ |
fe5dba13 | 1044 | \& |
b9bf9029 | 1045 | ret = read(data_socket, buffer, sizeof(buffer)); |
15545eb6 | 1046 | if (ret == \-1) { |
eb73e8ad | 1047 | perror("read"); |
15545eb6 HS |
1048 | exit(EXIT_FAILURE); |
1049 | } | |
fe5dba13 | 1050 | \& |
46b20ca1 | 1051 | /* Ensure buffer is 0\-terminated. */ |
fe5dba13 | 1052 | \& |
b9bf9029 | 1053 | buffer[sizeof(buffer) \- 1] = 0; |
fe5dba13 | 1054 | \& |
46b20ca1 | 1055 | /* Handle commands. */ |
fe5dba13 | 1056 | \& |
b9bf9029 | 1057 | if (!strncmp(buffer, "DOWN", sizeof(buffer))) { |
15545eb6 HS |
1058 | down_flag = 1; |
1059 | break; | |
1060 | } | |
fe5dba13 | 1061 | \& |
b9bf9029 | 1062 | if (!strncmp(buffer, "END", sizeof(buffer))) { |
15545eb6 HS |
1063 | break; |
1064 | } | |
fe5dba13 | 1065 | \& |
46b20ca1 | 1066 | /* Add received summand. */ |
fe5dba13 | 1067 | \& |
15545eb6 HS |
1068 | result += atoi(buffer); |
1069 | } | |
fe5dba13 | 1070 | \& |
46b20ca1 | 1071 | /* Send result. */ |
fe5dba13 | 1072 | \& |
15545eb6 | 1073 | sprintf(buffer, "%d", result); |
b9bf9029 | 1074 | ret = write(data_socket, buffer, sizeof(buffer)); |
15545eb6 | 1075 | if (ret == \-1) { |
eb73e8ad | 1076 | perror("write"); |
15545eb6 HS |
1077 | exit(EXIT_FAILURE); |
1078 | } | |
fe5dba13 | 1079 | \& |
46b20ca1 | 1080 | /* Close socket. */ |
fe5dba13 | 1081 | \& |
15545eb6 | 1082 | close(data_socket); |
fe5dba13 | 1083 | \& |
46b20ca1 | 1084 | /* Quit on DOWN command. */ |
fe5dba13 | 1085 | \& |
15545eb6 HS |
1086 | if (down_flag) { |
1087 | break; | |
1088 | } | |
1089 | } | |
fe5dba13 | 1090 | \& |
15545eb6 | 1091 | close(connection_socket); |
fe5dba13 | 1092 | \& |
46b20ca1 | 1093 | /* Unlink the socket. */ |
fe5dba13 | 1094 | \& |
15545eb6 | 1095 | unlink(SOCKET_NAME); |
fe5dba13 | 1096 | \& |
15545eb6 HS |
1097 | exit(EXIT_SUCCESS); |
1098 | } | |
fe5dba13 | 1099 | \& |
15545eb6 HS |
1100 | /* |
1101 | * File client.c | |
1102 | */ | |
fe5dba13 | 1103 | \& |
15545eb6 HS |
1104 | #include <errno.h> |
1105 | #include <stdio.h> | |
1106 | #include <stdlib.h> | |
1107 | #include <string.h> | |
1108 | #include <sys/socket.h> | |
1109 | #include <sys/un.h> | |
1110 | #include <unistd.h> | |
1111 | #include "connection.h" | |
fe5dba13 | 1112 | \& |
15545eb6 HS |
1113 | int |
1114 | main(int argc, char *argv[]) | |
1115 | { | |
24a31d63 | 1116 | struct sockaddr_un addr; |
15545eb6 HS |
1117 | int ret; |
1118 | int data_socket; | |
1119 | char buffer[BUFFER_SIZE]; | |
fe5dba13 | 1120 | \& |
46b20ca1 | 1121 | /* Create local socket. */ |
fe5dba13 | 1122 | \& |
15545eb6 HS |
1123 | data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
1124 | if (data_socket == \-1) { | |
1125 | perror("socket"); | |
1126 | exit(EXIT_FAILURE); | |
1127 | } | |
fe5dba13 | 1128 | \& |
15545eb6 | 1129 | /* |
eb73e8ad MK |
1130 | * For portability clear the whole structure, since some |
1131 | * implementations have additional (nonstandard) fields in | |
1132 | * the structure. | |
15545eb6 | 1133 | */ |
fe5dba13 | 1134 | \& |
012b86a0 | 1135 | memset(&addr, 0, sizeof(addr)); |
fe5dba13 | 1136 | \& |
46b20ca1 | 1137 | /* Connect socket to socket address. */ |
fe5dba13 | 1138 | \& |
24a31d63 MK |
1139 | addr.sun_family = AF_UNIX; |
1140 | strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1); | |
fe5dba13 | 1141 | \& |
7551caea | 1142 | ret = connect(data_socket, (const struct sockaddr *) &addr, |
012b86a0 | 1143 | sizeof(addr)); |
15545eb6 | 1144 | if (ret == \-1) { |
d1a71985 | 1145 | fprintf(stderr, "The server is down.\en"); |
15545eb6 HS |
1146 | exit(EXIT_FAILURE); |
1147 | } | |
fe5dba13 | 1148 | \& |
46b20ca1 | 1149 | /* Send arguments. */ |
fe5dba13 | 1150 | \& |
b42296e4 | 1151 | for (size_t i = 1; i < argc; ++i) { |
15545eb6 HS |
1152 | ret = write(data_socket, argv[i], strlen(argv[i]) + 1); |
1153 | if (ret == \-1) { | |
eb73e8ad | 1154 | perror("write"); |
15545eb6 HS |
1155 | break; |
1156 | } | |
1157 | } | |
fe5dba13 | 1158 | \& |
46b20ca1 | 1159 | /* Request result. */ |
fe5dba13 | 1160 | \& |
7551caea | 1161 | strcpy(buffer, "END"); |
15545eb6 HS |
1162 | ret = write(data_socket, buffer, strlen(buffer) + 1); |
1163 | if (ret == \-1) { | |
eb73e8ad | 1164 | perror("write"); |
15545eb6 HS |
1165 | exit(EXIT_FAILURE); |
1166 | } | |
fe5dba13 | 1167 | \& |
46b20ca1 | 1168 | /* Receive result. */ |
fe5dba13 | 1169 | \& |
b9bf9029 | 1170 | ret = read(data_socket, buffer, sizeof(buffer)); |
15545eb6 | 1171 | if (ret == \-1) { |
eb73e8ad | 1172 | perror("read"); |
15545eb6 HS |
1173 | exit(EXIT_FAILURE); |
1174 | } | |
fe5dba13 | 1175 | \& |
46b20ca1 | 1176 | /* Ensure buffer is 0\-terminated. */ |
fe5dba13 | 1177 | \& |
b9bf9029 | 1178 | buffer[sizeof(buffer) \- 1] = 0; |
fe5dba13 | 1179 | \& |
d1a71985 | 1180 | printf("Result = %s\en", buffer); |
fe5dba13 | 1181 | \& |
46b20ca1 | 1182 | /* Close socket. */ |
fe5dba13 | 1183 | \& |
15545eb6 | 1184 | close(data_socket); |
fe5dba13 | 1185 | \& |
15545eb6 HS |
1186 | exit(EXIT_SUCCESS); |
1187 | } | |
e7d0bb47 | 1188 | .EE |
15545eb6 | 1189 | .PP |
7a275383 MK |
1190 | For examples of the use of |
1191 | .BR SCM_RIGHTS , | |
c751683c | 1192 | see |
7a275383 MK |
1193 | .BR cmsg (3) |
1194 | and | |
1195 | .BR seccomp_unotify (2). | |
47297adb | 1196 | .SH SEE ALSO |
77117f4f MK |
1197 | .BR recvmsg (2), |
1198 | .BR sendmsg (2), | |
1199 | .BR socket (2), | |
1200 | .BR socketpair (2), | |
1201 | .BR cmsg (3), | |
1202 | .BR capabilities (7), | |
1203 | .BR credentials (7), | |
170e5f0d JC |
1204 | .BR socket (7), |
1205 | .BR udp (7) |