OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man7 / socket.7
1 '\" t
2 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
3 .\" and copyright (c) 1999 Matthew Wilcox.
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 .\" 2002-10-30, Michael Kerrisk, <mtk.manpages@gmail.com>
13 .\"     Added description of SO_ACCEPTCONN
14 .\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text.
15 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
16 .\"     Added notes on capability requirements
17 .\"     A few small grammar fixes
18 .\" 2010-06-13 Jan Engelhardt <jengelh@medozas.de>
19 .\"     Documented SO_DOMAIN and SO_PROTOCOL.
20 .\" FIXME
21 .\" The following are not yet documented:
22 .\"     SO_PEERNAME (2.4?)
23 .\"             get only
24 .\"             Seems to do something similar to getpeername(), but then
25 .\"             why is it necessary / how does it differ?
26 .\"     SO_TIMESTAMPNS (2.6.22)
27 .\"             Documentation/networking/timestamping.txt
28 .\"             commit 92f37fd2ee805aa77925c1e64fd56088b46094fc
29 .\"             Author: Eric Dumazet <dada1@cosmosbay.com>
30 .\"     SO_TIMESTAMPING (2.6.30)
31 .\"             Documentation/networking/timestamping.txt
32 .\"             commit cb9eff097831007afb30d64373f29d99825d0068
33 .\"             Author: Patrick Ohly <patrick.ohly@intel.com>
34 .\"     SO_WIFI_STATUS (3.3)
35 .\"             commit 6e3e939f3b1bf8534b32ad09ff199d88800835a0
36 .\"             Author: Johannes Berg <johannes.berg@intel.com>
37 .\"             Also: SCM_WIFI_STATUS
38 .\"     SO_NOFCS (3.4)
39 .\"             commit 3bdc0eba0b8b47797f4a76e377dd8360f317450f
40 .\"             Author: Ben Greear <greearb@candelatech.com>
41 .\"     SO_GET_FILTER (3.8)
42 .\"             commit a8fc92778080c845eaadc369a0ecf5699a03bef0
43 .\"             Author: Pavel Emelyanov <xemul@parallels.com>
44 .\"     SO_REUSEPORT (3.9)
45 .\"             commit c617f398edd4db2b8567a28e899a88f8f574798d
46 .\"             https://lwn.net/Articles/542629/
47 .\"     SO_LOCK_FILTER (3.9)
48 .\"             commit d59577b6ffd313d0ab3be39cb1ab47e29bdc9182
49 .\"             Author: Vincent Bernat <bernat@luffy.cx>
50 .\"     SO_SELECT_ERR_QUEUE (3.10)
51 .\"             commit 7d4c04fc170087119727119074e72445f2bb192b
52 .\"             Author: Keller, Jacob E <jacob.e.keller@intel.com>
53 .\"     SO_MAX_PACING_RATE (3.13)
54 .\"             commit 62748f32d501f5d3712a7c372bbb92abc7c62bc7
55 .\"
56 .TH SOCKET 7 2014-02-21 Linux "Linux Programmer's Manual"
57 .SH NAME
58 socket \- Linux socket interface
59 .SH SYNOPSIS
60 .B #include <sys/socket.h>
61 .sp
62 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
63 .SH DESCRIPTION
64 This manual page describes the Linux networking socket layer user
65 interface.
66 The BSD compatible sockets
67 are the uniform interface
68 between the user process and the network protocol stacks in the kernel.
69 The protocol modules are grouped into
70 .I protocol families
71 such as
72 .BR AF_INET ", " AF_IPX ", and " AF_PACKET ,
73 and
74 .I socket types
75 such as
76 .B SOCK_STREAM
77 or
78 .BR SOCK_DGRAM .
79 See
80 .BR socket (2)
81 for more information on families and types.
82 .SS Socket-layer functions
83 These functions are used by the user process to send or receive packets
84 and to do other socket operations.
85 For more information see their respective manual pages.
86
87 .BR socket (2)
88 creates a socket,
89 .BR connect (2)
90 connects a socket to a remote socket address,
91 the
92 .BR bind (2)
93 function binds a socket to a local socket address,
94 .BR listen (2)
95 tells the socket that new connections shall be accepted, and
96 .BR accept (2)
97 is used to get a new socket with a new incoming connection.
98 .BR socketpair (2)
99 returns two connected anonymous sockets (implemented only for a few
100 local families like
101 .BR AF_UNIX )
102 .PP
103 .BR send (2),
104 .BR sendto (2),
105 and
106 .BR sendmsg (2)
107 send data over a socket, and
108 .BR recv (2),
109 .BR recvfrom (2),
110 .BR recvmsg (2)
111 receive data from a socket.
112 .BR poll (2)
113 and
114 .BR select (2)
115 wait for arriving data or a readiness to send data.
116 In addition, the standard I/O operations like
117 .BR write (2),
118 .BR writev (2),
119 .BR sendfile (2),
120 .BR read (2),
121 and
122 .BR readv (2)
123 can be used to read and write data.
124 .PP
125 .BR getsockname (2)
126 returns the local socket address and
127 .BR getpeername (2)
128 returns the remote socket address.
129 .BR getsockopt (2)
130 and
131 .BR setsockopt (2)
132 are used to set or get socket layer or protocol options.
133 .BR ioctl (2)
134 can be used to set or read some other options.
135 .PP
136 .BR close (2)
137 is used to close a socket.
138 .BR shutdown (2)
139 closes parts of a full-duplex socket connection.
140 .PP
141 Seeking, or calling
142 .BR pread (2)
143 or
144 .BR pwrite (2)
145 with a nonzero position is not supported on sockets.
146 .PP
147 It is possible to do nonblocking I/O on sockets by setting the
148 .B O_NONBLOCK
149 flag on a socket file descriptor using
150 .BR fcntl (2).
151 Then all operations that would block will (usually)
152 return with
153 .B EAGAIN
154 (operation should be retried later);
155 .BR connect (2)
156 will return
157 .B EINPROGRESS
158 error.
159 The user can then wait for various events via
160 .BR poll (2)
161 or
162 .BR select (2).
163 .TS
164 tab(:) allbox;
165 c s s
166 l l l.
167 I/O events
168 Event:Poll flag:Occurrence
169 Read:POLLIN:T{
170 New data arrived.
171 T}
172 Read:POLLIN:T{
173 A connection setup has been completed
174 (for connection-oriented sockets)
175 T}
176 Read:POLLHUP:T{
177 A disconnection request has been initiated by the other end.
178 T}
179 Read:POLLHUP:T{
180 A connection is broken (only for connection-oriented protocols).
181 When the socket is written
182 .B SIGPIPE
183 is also sent.
184 T}
185 Write:POLLOUT:T{
186 Socket has enough send buffer space for writing new data.
187 T}
188 Read/Write:T{
189 POLLIN|
190 .br
191 POLLOUT
192 T}:T{
193 An outgoing
194 .BR connect (2)
195 finished.
196 T}
197 Read/Write:POLLERR:An asynchronous error occurred.
198 Read/Write:POLLHUP:The other end has shut down one direction.
199 Exception:POLLPRI:T{
200 Urgent data arrived.
201 .B SIGURG
202 is sent then.
203 T}
204 .\" FIXME . The following is not true currently:
205 .\" It is no I/O event when the connection
206 .\" is broken from the local end using
207 .\" .BR shutdown (2)
208 .\" or
209 .\" .BR close (2).
210 .TE
211 .PP
212 An alternative to
213 .BR poll (2)
214 and
215 .BR select (2)
216 is to let the kernel inform the application about events
217 via a
218 .B SIGIO
219 signal.
220 For that the
221 .B O_ASYNC
222 flag must be set on a socket file descriptor via
223 .BR fcntl (2)
224 and a valid signal handler for
225 .B SIGIO
226 must be installed via
227 .BR sigaction (2).
228 See the
229 .I Signals
230 discussion below.
231 .SS Socket address structures
232 Each socket domain has its own format for socket addresses,
233 with a domain-specific address structure.
234 Each of these structures begins with an
235 integer "family" field (typed as
236 .IR sa_family_t )
237 that indicates the type of the address structure.
238 This allows
239 the various system calls (e.g.,
240 .BR connect (2),
241 .BR bind (2),
242 .BR accept (2),
243 .BR getsockname (2),
244 .BR getpeername (2)),
245 which are generic to all socket domains,
246 to determine the domain of a particular socket address.
247
248 To allow any type of socket address to be passed to
249 interfaces in the sockets API,
250 the type
251 .IR "struct sockaddr"
252 is defined.
253 The purpose of this type is purely to allow casting of
254 domain-specific socket address types to a "generic" type,
255 so as to avoid compiler warnings about type mismatches in
256 calls to the sockets API.
257
258 In addition, the sockets API provides the data type
259 .IR "struct sockaddr_storage".
260 This type
261 is suitable to accommodate all supported domain-specific socket
262 address structures; it is large enough and is aligned properly.
263 (In particular, it is large enough to hold
264 IPv6 socket addresses.)
265 The structure includes the following field, which can be used to identify
266 the type of socket address actually stored in the structure:
267
268 .in +4n
269 .nf
270     sa_family_t ss_family;
271 .fi
272 .in
273
274 The
275 .I sockaddr_storage
276 structure is useful in programs that must handle socket addresses
277 in a generic way
278 (e.g., programs that must deal with both IPv4 and IPv6 socket addresses).
279 .SS Socket options
280 The socket options listed below can be set by using
281 .BR setsockopt (2)
282 and read with
283 .BR getsockopt (2)
284 with the socket level set to
285 .B SOL_SOCKET
286 for all sockets.
287 Unless otherwise noted,
288 .I optval
289 is a pointer to an
290 .IR int .
291 .\" FIXME
292 .\" In the list below, the text used to describe argument types
293 .\" for each socket option should be more consistent
294 .\"
295 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
296 .\" W R Stevens, UNPv1
297 .TP
298 .B SO_ACCEPTCONN
299 Returns a value indicating whether or not this socket has been marked
300 to accept connections with
301 .BR listen (2).
302 The value 0 indicates that this is not a listening socket,
303 the value 1 indicates that this is a listening socket.
304 This socket option is read-only.
305 .TP
306 .B SO_BINDTODEVICE
307 Bind this socket to a particular device like \(lqeth0\(rq,
308 as specified in the passed interface name.
309 If the
310 name is an empty string or the option length is zero, the socket device
311 binding is removed.
312 The passed option is a variable-length null-terminated
313 interface name string with the maximum size of
314 .BR IFNAMSIZ .
315 If a socket is bound to an interface,
316 only packets received from that particular interface are processed by the
317 socket.
318 Note that this works only for some socket types, particularly
319 .B AF_INET
320 sockets.
321 It is not supported for packet sockets (use normal
322 .BR bind (2)
323 there).
324
325 Before Linux 3.8,
326 this socket option could be set, but could not retrieved with
327 .BR getsockopt (2).
328 Since Linux 3.8, it is readable.
329 The
330 .I optlen
331 argument should contain the buffer size available
332 to receive the device name and is recommended to be
333 .BR IFNAMSZ
334 bytes.
335 The real device name length is reported back in the
336 .I optlen
337 argument.
338 .TP
339 .B SO_BROADCAST
340 Set or get the broadcast flag.
341 When enabled, datagram sockets are allowed to send
342 packets to a broadcast address.
343 This option has no effect on stream-oriented sockets.
344 .TP
345 .B SO_BSDCOMPAT
346 Enable BSD bug-to-bug compatibility.
347 This is used by the UDP protocol module in Linux 2.0 and 2.2.
348 If enabled, ICMP errors received for a UDP socket will not be passed
349 to the user program.
350 In later kernel versions, support for this option has been phased out:
351 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
352 (printk()) if a program uses this option.
353 Linux 2.0 also enabled BSD bug-to-bug compatibility
354 options (random header changing, skipping of the broadcast flag) for raw
355 sockets with this option, but that was removed in Linux 2.2.
356 .TP
357 .B SO_DEBUG
358 Enable socket debugging.
359 Only allowed for processes with the
360 .B CAP_NET_ADMIN
361 capability or an effective user ID of 0.
362 .TP
363 .BR SO_DOMAIN " (since Linux 2.6.32)"
364 Retrieves the socket domain as an integer, returning a value such as
365 .BR AF_INET6 .
366 See
367 .BR socket (2)
368 for details.
369 This socket option is read-only.
370 .TP
371 .B SO_ERROR
372 Get and clear the pending socket error.
373 This socket option is read-only.
374 Expects an integer.
375 .TP
376 .B SO_DONTROUTE
377 Don't send via a gateway, send only to directly connected hosts.
378 The same effect can be achieved by setting the
379 .B MSG_DONTROUTE
380 flag on a socket
381 .BR send (2)
382 operation.
383 Expects an integer boolean flag.
384 .TP
385 .B SO_KEEPALIVE
386 Enable sending of keep-alive messages on connection-oriented sockets.
387 Expects an integer boolean flag.
388 .TP
389 .B SO_LINGER
390 Sets or gets the
391 .B SO_LINGER
392 option.
393 The argument is a
394 .I linger
395 structure.
396 .sp
397 .in +4n
398 .nf
399 struct linger {
400     int l_onoff;    /* linger active */
401     int l_linger;   /* how many seconds to linger for */
402 };
403 .fi
404 .in
405 .IP
406 When enabled, a
407 .BR close (2)
408 or
409 .BR shutdown (2)
410 will not return until all queued messages for the socket have been
411 successfully sent or the linger timeout has been reached.
412 Otherwise,
413 the call returns immediately and the closing is done in the background.
414 When the socket is closed as part of
415 .BR exit (2),
416 it always lingers in the background.
417 .TP
418 .BR SO_MARK " (since Linux 2.6.25)"
419 .\" commit 4a19ec5800fc3bb64e2d87c4d9fdd9e636086fe0
420 .\" and    914a9ab386a288d0f22252fc268ecbc048cdcbd5
421 Set the mark for each packet sent through this socket
422 (similar to the netfilter MARK target but socket-based).
423 Changing the mark can be used for mark-based
424 routing without netfilter or for packet filtering.
425 Setting this option requires the
426 .B CAP_NET_ADMIN
427 capability.
428 .TP
429 .B SO_OOBINLINE
430 If this option is enabled,
431 out-of-band data is directly placed into the receive data stream.
432 Otherwise out-of-band data is passed only when the
433 .B MSG_OOB
434 flag is set during receiving.
435 .\" don't document it because it can do too much harm.
436 .\".B SO_NO_CHECK
437 .TP
438 .B SO_PASSCRED
439 Enable or disable the receiving of the
440 .B SCM_CREDENTIALS
441 control message.
442 For more information see
443 .BR unix (7).
444 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
445 .\" in the 2.6.18 ChangeLog
446 .TP
447 .BR SO_PEEK_OFF " (since Linux 3.4)"
448 .\" commit ef64a54f6e558155b4f149bb10666b9e914b6c54
449 This option, which is currently supported only for
450 .BR unix (7)
451 sockets, sets the value of the "peek offset" for the
452 .BR recv (2)
453 system call when used with
454 .BR MSG_PEEK
455 flag.
456
457 When this option is set to a negative value
458 (it is set to \-1 for all new sockets),
459 traditional behavior is provided:
460 .BR recv (2)
461 with the
462 .BR MSG_PEEK
463 flag will peek data from the front of the queue.
464
465 When the option is set to a value greater than or equal to zero,
466 then the next peek at data queued in the socket will occur at
467 the byte offset specified by the option value.
468 At the same time, the "peek offset" will be
469 incremented by the number of bytes that were peeked from the queue,
470 so that a subsequent peek will return the next data in the queue.
471
472 If data is removed from the front of the queue via a call to
473 .BR recv (2)
474 (or similar) without the
475 .BR MSG_PEEK
476 flag, the "peek offset" will be decreased by the number of bytes removed.
477 In other words, receiving data without the
478 .B MSG_PEEK
479 flag will cause the "peek offset" to be adjusted to maintain
480 the correct relative position in the queued data,
481 so that a subsequent peek will retrieve the data that would have been
482 retrieved had the data not been removed.
483
484 For datagram sockets, if the "peek offset" points to the middle of a packet,
485 the data returned will be marked with the
486 .BR MSG_TRUNC
487 flag.
488
489 The following example serves to illustrate the use of
490 .BR SO_PEEK_OFF .
491 Suppose a stream socket has the following queued input data:
492
493     aabbccddeeff
494
495 .IP
496 The following sequence of
497 .BR recv (2)
498 calls would have the effect noted in the comments:
499
500 .in +4n
501 .nf
502 int ov = 4;                  // Set peek offset to 4
503 setsockopt(fd, SOL_SOCKET, SO_PEEK_OFF, &ov, sizeof(ov));
504
505 recv(fd, buf, 2, MSG_PEEK);  // Peeks "cc"; offset set to 6
506 recv(fd, buf, 2, MSG_PEEK);  // Peeks "dd"; offset set to 8
507 recv(fd, buf, 2, 0);         // Reads "aa"; offset set to 6
508 recv(fd, buf, 2, MSG_PEEK);  // Peeks "ee"; offset set to 8
509 .fi
510 .in
511 .TP
512 .B SO_PEERCRED
513 Return the credentials of the foreign process connected to this socket.
514 This is possible only for connected
515 .B AF_UNIX
516 stream sockets and
517 .B AF_UNIX
518 stream and datagram socket pairs created using
519 .BR socketpair (2);
520 see
521 .BR unix (7).
522 The returned credentials are those that were in effect at the time
523 of the call to
524 .BR connect (2)
525 or
526 .BR socketpair (2).
527 The argument is a
528 .I ucred
529 structure; define the
530 .B _GNU_SOURCE
531 feature test macro to obtain the definition of that structure from
532 .IR <sys/socket.h> .
533 This socket option is read-only.
534 .TP
535 .B SO_PRIORITY
536 Set the protocol-defined priority for all packets to be sent on
537 this socket.
538 Linux uses this value to order the networking queues:
539 packets with a higher priority may be processed first depending
540 on the selected device queueing discipline.
541 For
542 .BR ip (7),
543 this also sets the IP type-of-service (TOS) field for outgoing packets.
544 Setting a priority outside the range 0 to 6 requires the
545 .B CAP_NET_ADMIN
546 capability.
547 .TP
548 .BR SO_PROTOCOL " (since Linux 2.6.32)"
549 Retrieves the socket protocol as an integer, returning a value such as
550 .BR IPPROTO_SCTP .
551 See
552 .BR socket (2)
553 for details.
554 This socket option is read-only.
555 .TP
556 .B SO_RCVBUF
557 Sets or gets the maximum socket receive buffer in bytes.
558 The kernel doubles this value (to allow space for bookkeeping overhead)
559 when it is set using
560 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
561 .BR setsockopt (2),
562 and this doubled value is returned by
563 .BR getsockopt (2).
564 .\" The following thread on LMKL is quite informative:
565 .\" getsockopt/setsockopt with SO_RCVBUF and SO_SNDBUF "non-standard" behaviour
566 .\" 17 July 2012
567 .\" http://thread.gmane.org/gmane.linux.kernel/1328935
568 The default value is set by the
569 .I /proc/sys/net/core/rmem_default
570 file, and the maximum allowed value is set by the
571 .I /proc/sys/net/core/rmem_max
572 file.
573 The minimum (doubled) value for this option is 256.
574 .TP
575 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
576 Using this socket option, a privileged
577 .RB ( CAP_NET_ADMIN )
578 process can perform the same task as
579 .BR SO_RCVBUF ,
580 but the
581 .I rmem_max
582 limit can be overridden.
583 .TP
584 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
585 Specify the minimum number of bytes in the buffer until the socket layer
586 will pass the data to the protocol
587 .RB ( SO_SNDLOWAT )
588 or the user on receiving
589 .RB ( SO_RCVLOWAT ).
590 These two values are initialized to 1.
591 .B SO_SNDLOWAT
592 is not changeable on Linux
593 .RB ( setsockopt (2)
594 fails with the error
595 .BR ENOPROTOOPT ).
596 .B SO_RCVLOWAT
597 is changeable
598 only since Linux 2.4.
599 The
600 .BR select (2)
601 and
602 .BR poll (2)
603 system calls currently do not respect the
604 .B SO_RCVLOWAT
605 setting on Linux,
606 and mark a socket readable when even a single byte of data is available.
607 A subsequent read from the socket will block until
608 .B SO_RCVLOWAT
609 bytes are available.
610 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
611 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
612 .TP
613 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
614 .\" Not implemented in 2.0.
615 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
616 .\" Implemented in 2.3.41 for setsockopt, and actually used.
617 Specify the receiving or sending timeouts until reporting an error.
618 The argument is a
619 .IR "struct timeval" .
620 If an input or output function blocks for this period of time, and
621 data has been sent or received, the return value of that function
622 will be the amount of data transferred; if no data has been transferred
623 and the timeout has been reached, then \-1 is returned with
624 .I errno
625 set to
626 .BR EAGAIN
627 or
628 .BR EWOULDBLOCK ,
629 .\" in fact to EAGAIN
630 or
631 .B EINPROGRESS
632 (for
633 .BR connect (2))
634 just as if the socket was specified to be nonblocking.
635 If the timeout is set to zero (the default),
636 then the operation will never timeout.
637 Timeouts only have effect for system calls that perform socket I/O (e.g.,
638 .BR read (2),
639 .BR recvmsg (2),
640 .BR send (2),
641 .BR sendmsg (2));
642 timeouts have no effect for
643 .BR select (2),
644 .BR poll (2),
645 .BR epoll_wait (2),
646 and so on.
647 .TP
648 .B SO_REUSEADDR
649 Indicates that the rules used in validating addresses supplied in a
650 .BR bind (2)
651 call should allow reuse of local addresses.
652 For
653 .B AF_INET
654 sockets this
655 means that a socket may bind, except when there
656 is an active listening socket bound to the address.
657 When the listening socket is bound to
658 .B INADDR_ANY
659 with a specific port then it is not possible
660 to bind to this port for any local address.
661 Argument is an integer boolean flag.
662 .TP
663 .BR SO_RXQ_OVFL " (since Linux 2.6.33)"
664 .\" commit 3b885787ea4112eaa80945999ea0901bf742707f
665 Indicates that an unsigned 32-bit value ancilliary msg (cmsg)
666 should be attached to received skbs indicating
667 the number of packets dropped by the socket between
668 the last received packet and this received packet
669 .TP
670 .B SO_SNDBUF
671 Sets or gets the maximum socket send buffer in bytes.
672 The kernel doubles this value (to allow space for bookkeeping overhead)
673 when it is set using
674 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
675 .\" See also the comment to SO_RCVBUF (17 Jul 2012 LKML mail)
676 .BR setsockopt (2),
677 and this doubled value is returned by
678 .BR getsockopt (2).
679 The default value is set by the
680 .I /proc/sys/net/core/wmem_default
681 file and the maximum allowed value is set by the
682 .I /proc/sys/net/core/wmem_max
683 file.
684 The minimum (doubled) value for this option is 2048.
685 .TP
686 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
687 Using this socket option, a privileged
688 .RB ( CAP_NET_ADMIN )
689 process can perform the same task as
690 .BR SO_SNDBUF ,
691 but the
692 .I wmem_max
693 limit can be overridden.
694 .TP
695 .B SO_TIMESTAMP
696 Enable or disable the receiving of the
697 .B SO_TIMESTAMP
698 control message.
699 The timestamp control message is sent with level
700 .B SOL_SOCKET
701 and the
702 .I cmsg_data
703 field is a
704 .I "struct timeval"
705 indicating the
706 reception time of the last packet passed to the user in this call.
707 See
708 .BR cmsg (3)
709 for details on control messages.
710 .TP
711 .B SO_TYPE
712 Gets the socket type as an integer (e.g.,
713 .BR SOCK_STREAM ).
714 This socket option is read-only.
715 .TP
716 .BR SO_BUSY_POLL " (since Linux 3.11)"
717 Sets the approximate time in microseconds to busy poll on a blocking receive
718 when there is no data.
719 Increasing this value requires
720 .BR CAP_NET_ADMIN .
721 The default for this option is controlled by the
722 .I /proc/sys/net/core/busy_read
723 file.
724
725 The value in the
726 .I /proc/sys/net/core/busy_poll
727 file determines how long
728 .BR select (2)
729 and
730 .BR poll (2)
731 will busy poll when they operate on sockets with
732 .BR SO_BUSY_POLL
733 set and no events to report are found.
734
735 In both cases,
736 busy polling will only be done when the socket last received data
737 from a network device that supports this option.
738
739 While busy polling may improve latency of some applications,
740 care must be taken when using it since this will increase
741 both CPU utilization and power usage.
742 .SS Signals
743 When writing onto a connection-oriented socket that has been shut down
744 (by the local or the remote end)
745 .B SIGPIPE
746 is sent to the writing process and
747 .B EPIPE
748 is returned.
749 The signal is not sent when the write call
750 specified the
751 .B MSG_NOSIGNAL
752 flag.
753 .PP
754 When requested with the
755 .B FIOSETOWN
756 .BR fcntl (2)
757 or
758 .B SIOCSPGRP
759 .BR ioctl (2),
760 .B SIGIO
761 is sent when an I/O event occurs.
762 It is possible to use
763 .BR poll (2)
764 or
765 .BR select (2)
766 in the signal handler to find out which socket the event occurred on.
767 An alternative (in Linux 2.2) is to set a real-time signal using the
768 .B F_SETSIG
769 .BR fcntl (2);
770 the handler of the real time signal will be called with
771 the file descriptor in the
772 .I si_fd
773 field of its
774 .IR siginfo_t .
775 See
776 .BR fcntl (2)
777 for more information.
778 .PP
779 Under some circumstances (e.g., multiple processes accessing a
780 single socket), the condition that caused the
781 .B SIGIO
782 may have already disappeared when the process reacts to the signal.
783 If this happens, the process should wait again because Linux
784 will resend the signal later.
785 .\" .SS Ancillary messages
786 .SS /proc interfaces
787 The core socket networking parameters can be accessed
788 via files in the directory
789 .IR /proc/sys/net/core/ .
790 .TP
791 .I rmem_default
792 contains the default setting in bytes of the socket receive buffer.
793 .TP
794 .I rmem_max
795 contains the maximum socket receive buffer size in bytes which a user may
796 set by using the
797 .B SO_RCVBUF
798 socket option.
799 .TP
800 .I wmem_default
801 contains the default setting in bytes of the socket send buffer.
802 .TP
803 .I wmem_max
804 contains the maximum socket send buffer size in bytes which a user may
805 set by using the
806 .B SO_SNDBUF
807 socket option.
808 .TP
809 .IR message_cost " and " message_burst
810 configure the token bucket filter used to load limit warning messages
811 caused by external network events.
812 .TP
813 .I netdev_max_backlog
814 Maximum number of packets in the global input queue.
815 .TP
816 .I optmem_max
817 Maximum length of ancillary data and user control data like the iovecs
818 per socket.
819 .\" netdev_fastroute is not documented because it is experimental
820 .SS Ioctls
821 These operations can be accessed using
822 .BR ioctl (2):
823
824 .in +4n
825 .nf
826 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
827 .fi
828 .in
829 .TP
830 .B SIOCGSTAMP
831 Return a
832 .I struct timeval
833 with the receive timestamp of the last packet passed to the user.
834 This is useful for accurate round trip time measurements.
835 See
836 .BR setitimer (2)
837 for a description of
838 .IR "struct timeval" .
839 .\"
840 This ioctl should be used only if the socket option
841 .B SO_TIMESTAMP
842 is not set on the socket.
843 Otherwise, it returns the timestamp of the
844 last packet that was received while
845 .B SO_TIMESTAMP
846 was not set, or it fails if no such packet has been received,
847 (i.e.,
848 .BR ioctl (2)
849 returns \-1 with
850 .I errno
851 set to
852 .BR ENOENT ).
853 .TP
854 .B SIOCSPGRP
855 Set the process or process group to send
856 .B SIGIO
857 or
858 .B SIGURG
859 signals
860 to when an
861 asynchronous I/O operation has finished or urgent data is available.
862 The argument is a pointer to a
863 .IR pid_t .
864 If the argument is positive, send the signals to that process.
865 If the
866 argument is negative, send the signals to the process group with the ID
867 of the absolute value of the argument.
868 The process may only choose itself or its own process group to receive
869 signals unless it has the
870 .B CAP_KILL
871 capability or an effective UID of 0.
872 .TP
873 .B FIOASYNC
874 Change the
875 .B O_ASYNC
876 flag to enable or disable asynchronous I/O mode of the socket.
877 Asynchronous I/O mode means that the
878 .B SIGIO
879 signal or the signal set with
880 .B F_SETSIG
881 is raised when a new I/O event occurs.
882 .IP
883 Argument is an integer boolean flag.
884 (This operation is synonymous with the use of
885 .BR fcntl (2)
886 to set the
887 .B O_ASYNC
888 flag.)
889 .\"
890 .TP
891 .B SIOCGPGRP
892 Get the current process or process group that receives
893 .B SIGIO
894 or
895 .B SIGURG
896 signals,
897 or 0
898 when none is set.
899 .PP
900 Valid
901 .BR fcntl (2)
902 operations:
903 .TP
904 .B FIOGETOWN
905 The same as the
906 .B SIOCGPGRP
907 .BR ioctl (2).
908 .TP
909 .B FIOSETOWN
910 The same as the
911 .B SIOCSPGRP
912 .BR ioctl (2).
913 .SH VERSIONS
914 .B SO_BINDTODEVICE
915 was introduced in Linux 2.0.30.
916 .B SO_PASSCRED
917 is new in Linux 2.2.
918 The
919 .I /proc
920 interfaces was introduced in Linux 2.2.
921 .B SO_RCVTIMEO
922 and
923 .B SO_SNDTIMEO
924 are supported since Linux 2.3.41.
925 Earlier, timeouts were fixed to
926 a protocol-specific setting, and could not be read or written.
927 .SH NOTES
928 Linux assumes that half of the send/receive buffer is used for internal
929 kernel structures; thus the values in the corresponding
930 .I /proc
931 files are twice what can be observed on the wire.
932
933 Linux will only allow port reuse with the
934 .B SO_REUSEADDR
935 option
936 when this option was set both in the previous program that performed a
937 .BR bind (2)
938 to the port and in the program that wants to reuse the port.
939 This differs from some implementations (e.g., FreeBSD)
940 where only the later program needs to set the
941 .B SO_REUSEADDR
942 option.
943 Typically this difference is invisible, since, for example, a server
944 program is designed to always set this option.
945 .SH BUGS
946 The
947 .B CONFIG_FILTER
948 socket options
949 .B SO_ATTACH_FILTER
950 and
951 .B SO_DETACH_FILTER
952 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
953 are not documented.
954 The suggested interface to use them is via the libpcap
955 library.
956 .\" .SH AUTHORS
957 .\" This man page was written by Andi Kleen.
958 .SH SEE ALSO
959 .BR connect (2),
960 .BR getsockopt (2),
961 .BR setsockopt (2),
962 .BR socket (2),
963 .BR capabilities (7),
964 .BR ddp (7),
965 .BR ip (7),
966 .BR packet (7),
967 .BR tcp (7),
968 .BR udp (7),
969 .BR unix (7)
970 .SH COLOPHON
971 This page is part of release 3.65 of the Linux
972 .I man-pages
973 project.
974 A description of the project,
975 and information about reporting bugs,
976 can be found at
977 \%http://www.kernel.org/doc/man\-pages/.