OSDN Git Service

(split) LDP: Update original to LDP v3.40.
[linuxjm/LDP_man-pages.git] / original / man7 / socket.7
1 '\" t
2 .\" Don't change the first line, it tells man that we need tbl.
3 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
4 .\" and copyright (c) 1999 Matthew Wilcox.
5 .\" Permission is granted to distribute possibly modified copies
6 .\" of this page provided the header is included verbatim,
7 .\" and in case of nontrivial modification author and date
8 .\" of the modification is added to the header.
9 .\"
10 .\" 2002-10-30, Michael Kerrisk, <mtk.manpages@gmail.com>
11 .\"     Added description of SO_ACCEPTCONN
12 .\" 2004-05-20, aeb, added SO_RCVTIMEO/SO_SNDTIMEO text.
13 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
14 .\"     Added notes on capability requirements
15 .\"     A few small grammar fixes
16 .\" 2010-06-13 Jan Engelhardt <jengelh@medozas.de>
17 .\"     Documented SO_DOMAIN and SO_PROTOCOL.
18 .\" FIXME
19 .\" The following are not yet documented:
20 .\"     SO_PEERNAME
21 .\"     SO_TIMESTAMPNS
22 .\"     SO_MARK (see https://bugzilla.kernel.org/show_bug.cgi?id=16461)
23 .\"     SO_TIMESTAMPING (2.6.30)
24 .\"     SO_RXQ_OVFL (2.6.33)
25 .\"
26 .TH SOCKET 7 2012-04-23 Linux "Linux Programmer's Manual"
27 .SH NAME
28 socket \- Linux socket interface
29 .SH SYNOPSIS
30 .B #include <sys/socket.h>
31 .sp
32 .IB sockfd " = socket(int " socket_family ", int " socket_type ", int " protocol );
33 .SH DESCRIPTION
34 This manual page describes the Linux networking socket layer user
35 interface.
36 The BSD compatible sockets
37 are the uniform interface
38 between the user process and the network protocol stacks in the kernel.
39 The protocol modules are grouped into
40 .I protocol families
41 like
42 .BR AF_INET ", " AF_IPX ", " AF_PACKET
43 and
44 .I socket types
45 like
46 .B SOCK_STREAM
47 or
48 .BR SOCK_DGRAM .
49 See
50 .BR socket (2)
51 for more information on families and types.
52 .SS Socket Layer Functions
53 These functions are used by the user process to send or receive packets
54 and to do other socket operations.
55 For more information see their respective manual pages.
56
57 .BR socket (2)
58 creates a socket,
59 .BR connect (2)
60 connects a socket to a remote socket address,
61 the
62 .BR bind (2)
63 function binds a socket to a local socket address,
64 .BR listen (2)
65 tells the socket that new connections shall be accepted, and
66 .BR accept (2)
67 is used to get a new socket with a new incoming connection.
68 .BR socketpair (2)
69 returns two connected anonymous sockets (only implemented for a few
70 local families like
71 .BR AF_UNIX )
72 .PP
73 .BR send (2),
74 .BR sendto (2),
75 and
76 .BR sendmsg (2)
77 send data over a socket, and
78 .BR recv (2),
79 .BR recvfrom (2),
80 .BR recvmsg (2)
81 receive data from a socket.
82 .BR poll (2)
83 and
84 .BR select (2)
85 wait for arriving data or a readiness to send data.
86 In addition, the standard I/O operations like
87 .BR write (2),
88 .BR writev (2),
89 .BR sendfile (2),
90 .BR read (2),
91 and
92 .BR readv (2)
93 can be used to read and write data.
94 .PP
95 .BR getsockname (2)
96 returns the local socket address and
97 .BR getpeername (2)
98 returns the remote socket address.
99 .BR getsockopt (2)
100 and
101 .BR setsockopt (2)
102 are used to set or get socket layer or protocol options.
103 .BR ioctl (2)
104 can be used to set or read some other options.
105 .PP
106 .BR close (2)
107 is used to close a socket.
108 .BR shutdown (2)
109 closes parts of a full-duplex socket connection.
110 .PP
111 Seeking, or calling
112 .BR pread (2)
113 or
114 .BR pwrite (2)
115 with a nonzero position is not supported on sockets.
116 .PP
117 It is possible to do nonblocking I/O on sockets by setting the
118 .B O_NONBLOCK
119 flag on a socket file descriptor using
120 .BR fcntl (2).
121 Then all operations that would block will (usually)
122 return with
123 .B EAGAIN
124 (operation should be retried later);
125 .BR connect (2)
126 will return
127 .B EINPROGRESS
128 error.
129 The user can then wait for various events via
130 .BR poll (2)
131 or
132 .BR select (2).
133 .TS
134 tab(:) allbox;
135 c s s
136 l l l.
137 I/O events
138 Event:Poll flag:Occurrence
139 Read:POLLIN:T{
140 New data arrived.
141 T}
142 Read:POLLIN:T{
143 A connection setup has been completed
144 (for connection-oriented sockets)
145 T}
146 Read:POLLHUP:T{
147 A disconnection request has been initiated by the other end.
148 T}
149 Read:POLLHUP:T{
150 A connection is broken (only for connection-oriented protocols).
151 When the socket is written
152 .B SIGPIPE
153 is also sent.
154 T}
155 Write:POLLOUT:T{
156 Socket has enough send buffer space for writing new data.
157 T}
158 Read/Write:T{
159 POLLIN|
160 .br
161 POLLOUT
162 T}:T{
163 An outgoing
164 .BR connect (2)
165 finished.
166 T}
167 Read/Write:POLLERR:An asynchronous error occurred.
168 Read/Write:POLLHUP:The other end has shut down one direction.
169 Exception:POLLPRI:T{
170 Urgent data arrived.
171 .B SIGURG
172 is sent then.
173 T}
174 .\" FIXME . The following is not true currently:
175 .\" It is no I/O event when the connection
176 .\" is broken from the local end using
177 .\" .BR shutdown (2)
178 .\" or
179 .\" .BR close (2).
180 .TE
181
182 .PP
183 An alternative to
184 .BR poll (2)
185 and
186 .BR select (2)
187 is to let the kernel inform the application about events
188 via a
189 .B SIGIO
190 signal.
191 For that the
192 .B O_ASYNC
193 flag must be set on a socket file descriptor via
194 .BR fcntl (2)
195 and a valid signal handler for
196 .B SIGIO
197 must be installed via
198 .BR sigaction (2).
199 See the
200 .I Signals
201 discussion below.
202 .SS Socket Options
203 These socket options can be set by using
204 .BR setsockopt (2)
205 and read with
206 .BR getsockopt (2)
207 with the socket level set to
208 .B SOL_SOCKET
209 for all sockets:
210 .\" FIXME
211 .\" In the list below, the text used to describe argument types
212 .\" for each socket option should be more consistent
213 .\"
214 .\" SO_ACCEPTCONN is in POSIX.1-2001, and its origin is explained in
215 .\" W R Stevens, UNPv1
216 .TP
217 .B SO_ACCEPTCONN
218 Returns a value indicating whether or not this socket has been marked
219 to accept connections with
220 .BR listen (2).
221 The value 0 indicates that this is not a listening socket,
222 the value 1 indicates that this is a listening socket.
223 This socket option is read-only.
224 .TP
225 .B SO_BINDTODEVICE
226 Bind this socket to a particular device like \(lqeth0\(rq,
227 as specified in the passed interface name.
228 If the
229 name is an empty string or the option length is zero, the socket device
230 binding is removed.
231 The passed option is a variable-length null-terminated
232 interface name string with the maximum size of
233 .BR IFNAMSIZ .
234 If a socket is bound to an interface,
235 only packets received from that particular interface are processed by the
236 socket.
237 Note that this only works for some socket types, particularly
238 .B AF_INET
239 sockets.
240 It is not supported for packet sockets (use normal
241 .BR bind (2)
242 there).
243 .TP
244 .B SO_BROADCAST
245 Set or get the broadcast flag.
246 When enabled, datagram sockets are allowed to send
247 packets to a broadcast address.
248 This option has no effect on stream-oriented sockets.
249 .TP
250 .B SO_BSDCOMPAT
251 Enable BSD bug-to-bug compatibility.
252 This is used by the UDP protocol module in Linux 2.0 and 2.2.
253 If enabled ICMP errors received for a UDP socket will not be passed
254 to the user program.
255 In later kernel versions, support for this option has been phased out:
256 Linux 2.4 silently ignores it, and Linux 2.6 generates a kernel warning
257 (printk()) if a program uses this option.
258 Linux 2.0 also enabled BSD bug-to-bug compatibility
259 options (random header changing, skipping of the broadcast flag) for raw
260 sockets with this option, but that was removed in Linux 2.2.
261 .TP
262 .B SO_DEBUG
263 Enable socket debugging.
264 Only allowed for processes with the
265 .B CAP_NET_ADMIN
266 capability or an effective user ID of 0.
267 .TP
268 .BR SO_DOMAIN " (since Linux 2.6.32)"
269 Retrieves the socket domain as an integer, returning a value such as
270 .BR AF_INET6 .
271 See
272 .BR socket (2)
273 for details.
274 This socket option is read-only.
275 .TP
276 .B SO_ERROR
277 Get and clear the pending socket error.
278 This socket option is read-only.
279 Expects an integer.
280 .TP
281 .B SO_DONTROUTE
282 Don't send via a gateway, only send to directly connected hosts.
283 The same effect can be achieved by setting the
284 .B MSG_DONTROUTE
285 flag on a socket
286 .BR send (2)
287 operation.
288 Expects an integer boolean flag.
289 .TP
290 .B SO_KEEPALIVE
291 Enable sending of keep-alive messages on connection-oriented sockets.
292 Expects an integer boolean flag.
293 .TP
294 .B SO_LINGER
295 Sets or gets the
296 .B SO_LINGER
297 option.
298 The argument is a
299 .I linger
300 structure.
301 .sp
302 .in +4n
303 .nf
304 struct linger {
305     int l_onoff;    /* linger active */
306     int l_linger;   /* how many seconds to linger for */
307 };
308 .fi
309 .in
310 .IP
311 When enabled, a
312 .BR close (2)
313 or
314 .BR shutdown (2)
315 will not return until all queued messages for the socket have been
316 successfully sent or the linger timeout has been reached.
317 Otherwise,
318 the call returns immediately and the closing is done in the background.
319 When the socket is closed as part of
320 .BR exit (2),
321 it always lingers in the background.
322 .TP
323 .B SO_OOBINLINE
324 If this option is enabled,
325 out-of-band data is directly placed into the receive data stream.
326 Otherwise out-of-band data is only passed when the
327 .B MSG_OOB
328 flag is set during receiving.
329 .\" don't document it because it can do too much harm.
330 .\".B SO_NO_CHECK
331 .TP
332 .B SO_PASSCRED
333 Enable or disable the receiving of the
334 .B SCM_CREDENTIALS
335 control message.
336 For more information see
337 .BR unix (7).
338 .\" FIXME Document SO_PASSSEC, added in 2.6.18; there is some info
339 .\" in the 2.6.18 ChangeLog
340 .TP
341 .B SO_PEERCRED
342 Return the credentials of the foreign process connected to this socket.
343 This is only possible for connected
344 .B AF_UNIX
345 stream sockets and
346 .B AF_UNIX
347 stream and datagram socket pairs created using
348 .BR socketpair (2);
349 see
350 .BR unix (7).
351 The returned credentials are those that were in effect at the time
352 of the call to
353 .BR connect (2)
354 or
355 .BR socketpair (2).
356 Argument is a
357 .I ucred
358 structure.
359 This socket option is read-only.
360 .TP
361 .B SO_PRIORITY
362 Set the protocol-defined priority for all packets to be sent on
363 this socket.
364 Linux uses this value to order the networking queues:
365 packets with a higher priority may be processed first depending
366 on the selected device queueing discipline.
367 For
368 .BR ip (7),
369 this also sets the IP type-of-service (TOS) field for outgoing packets.
370 Setting a priority outside the range 0 to 6 requires the
371 .B CAP_NET_ADMIN
372 capability.
373 .TP
374 .BR SO_PROTOCOL " (since Linux 2.6.32)"
375 Retrieves the socket protocol as an integer, returning a value such as
376 .BR IPPROTO_SCTP .
377 See
378 .BR socket (2)
379 for details.
380 This socket option is read-only.
381 .TP
382 .B SO_RCVBUF
383 Sets or gets the maximum socket receive buffer in bytes.
384 The kernel doubles this value (to allow space for bookkeeping overhead)
385 when it is set using
386 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
387 .BR setsockopt (2),
388 and this doubled value is returned by
389 .BR getsockopt (2).
390 The default value is set by the
391 .I /proc/sys/net/core/rmem_default
392 file, and the maximum allowed value is set by the
393 .I /proc/sys/net/core/rmem_max
394 file.
395 The minimum (doubled) value for this option is 256.
396 .TP
397 .BR SO_RCVBUFFORCE " (since Linux 2.6.14)"
398 Using this socket option, a privileged
399 .RB ( CAP_NET_ADMIN )
400 process can perform the same task as
401 .BR SO_RCVBUF ,
402 but the
403 .I rmem_max
404 limit can be overridden.
405 .TP
406 .BR SO_RCVLOWAT " and " SO_SNDLOWAT
407 Specify the minimum number of bytes in the buffer until the socket layer
408 will pass the data to the protocol
409 .RB ( SO_SNDLOWAT )
410 or the user on receiving
411 .RB ( SO_RCVLOWAT ).
412 These two values are initialized to 1.
413 .B SO_SNDLOWAT
414 is not changeable on Linux
415 .RB ( setsockopt (2)
416 fails with the error
417 .BR ENOPROTOOPT ).
418 .B SO_RCVLOWAT
419 is changeable
420 only since Linux 2.4.
421 The
422 .BR select (2)
423 and
424 .BR poll (2)
425 system calls currently do not respect the
426 .B SO_RCVLOWAT
427 setting on Linux,
428 and mark a socket readable when even a single byte of data is available.
429 A subsequent read from the socket will block until
430 .B SO_RCVLOWAT
431 bytes are available.
432 .\" See http://marc.theaimsgroup.com/?l=linux-kernel&m=111049368106984&w=2
433 .\" Tested on kernel 2.6.14 -- mtk, 30 Nov 05
434 .TP
435 .BR SO_RCVTIMEO " and " SO_SNDTIMEO
436 .\" Not implemented in 2.0.
437 .\" Implemented in 2.1.11 for getsockopt: always return a zero struct.
438 .\" Implemented in 2.3.41 for setsockopt, and actually used.
439 Specify the receiving or sending timeouts until reporting an error.
440 The argument is a
441 .IR "struct timeval" .
442 If an input or output function blocks for this period of time, and
443 data has been sent or received, the return value of that function
444 will be the amount of data transferred; if no data has been transferred
445 and the timeout has been reached then \-1 is returned with
446 .I errno
447 set to
448 .B EAGAIN
449 or
450 .B EWOULDBLOCK
451 .\" in fact to EAGAIN
452 just as if the socket was specified to be nonblocking.
453 If the timeout is set to zero (the default)
454 then the operation will never timeout.
455 Timeouts only have effect for system calls that perform socket I/O (e.g.,
456 .BR read (2),
457 .BR recvmsg (2),
458 .BR send (2),
459 .BR sendmsg (2));
460 timeouts have no effect for
461 .BR select (2),
462 .BR poll (2),
463 .BR epoll_wait (2),
464 etc.
465 .TP
466 .B SO_REUSEADDR
467 Indicates that the rules used in validating addresses supplied in a
468 .BR bind (2)
469 call should allow reuse of local addresses.
470 For
471 .B AF_INET
472 sockets this
473 means that a socket may bind, except when there
474 is an active listening socket bound to the address.
475 When the listening socket is bound to
476 .B INADDR_ANY
477 with a specific port then it is not possible
478 to bind to this port for any local address.
479 Argument is an integer boolean flag.
480 .TP
481 .B SO_SNDBUF
482 Sets or gets the maximum socket send buffer in bytes.
483 The kernel doubles this value (to allow space for bookkeeping overhead)
484 when it is set using
485 .\" Most (all?) other implementations do not do this -- MTK, Dec 05
486 .BR setsockopt (2),
487 and this doubled value is returned by
488 .BR getsockopt (2).
489 The default value is set by the
490 .I /proc/sys/net/core/wmem_default
491 file and the maximum allowed value is set by the
492 .I /proc/sys/net/core/wmem_max
493 file.
494 The minimum (doubled) value for this option is 2048.
495 .TP
496 .BR SO_SNDBUFFORCE " (since Linux 2.6.14)"
497 Using this socket option, a privileged
498 .RB ( CAP_NET_ADMIN )
499 process can perform the same task as
500 .BR SO_SNDBUF ,
501 but the
502 .I wmem_max
503 limit can be overridden.
504 .TP
505 .B SO_TIMESTAMP
506 Enable or disable the receiving of the
507 .B SO_TIMESTAMP
508 control message.
509 The timestamp control message is sent with level
510 .B SOL_SOCKET
511 and the
512 .I cmsg_data
513 field is a
514 .I "struct timeval"
515 indicating the
516 reception time of the last packet passed to the user in this call.
517 See
518 .BR cmsg (3)
519 for details on control messages.
520 .TP
521 .B SO_TYPE
522 Gets the socket type as an integer (e.g.,
523 .BR SOCK_STREAM ).
524 This socket option is read-only.
525 .SS Signals
526 When writing onto a connection-oriented socket that has been shut down
527 (by the local or the remote end)
528 .B SIGPIPE
529 is sent to the writing process and
530 .B EPIPE
531 is returned.
532 The signal is not sent when the write call
533 specified the
534 .B MSG_NOSIGNAL
535 flag.
536 .PP
537 When requested with the
538 .B FIOSETOWN
539 .BR fcntl (2)
540 or
541 .B SIOCSPGRP
542 .BR ioctl (2),
543 .B SIGIO
544 is sent when an I/O event occurs.
545 It is possible to use
546 .BR poll (2)
547 or
548 .BR select (2)
549 in the signal handler to find out which socket the event occurred on.
550 An alternative (in Linux 2.2) is to set a real-time signal using the
551 .B F_SETSIG
552 .BR fcntl (2);
553 the handler of the real time signal will be called with
554 the file descriptor in the
555 .I si_fd
556 field of its
557 .IR siginfo_t .
558 See
559 .BR fcntl (2)
560 for more information.
561 .PP
562 Under some circumstances (e.g., multiple processes accessing a
563 single socket), the condition that caused the
564 .B SIGIO
565 may have already disappeared when the process reacts to the signal.
566 If this happens, the process should wait again because Linux
567 will resend the signal later.
568 .\" .SS Ancillary Messages
569 .SS /proc interfaces
570 The core socket networking parameters can be accessed
571 via files in the directory
572 .IR /proc/sys/net/core/ .
573 .TP
574 .I rmem_default
575 contains the default setting in bytes of the socket receive buffer.
576 .TP
577 .I rmem_max
578 contains the maximum socket receive buffer size in bytes which a user may
579 set by using the
580 .B SO_RCVBUF
581 socket option.
582 .TP
583 .I wmem_default
584 contains the default setting in bytes of the socket send buffer.
585 .TP
586 .I wmem_max
587 contains the maximum socket send buffer size in bytes which a user may
588 set by using the
589 .B SO_SNDBUF
590 socket option.
591 .TP
592 .IR message_cost " and " message_burst
593 configure the token bucket filter used to load limit warning messages
594 caused by external network events.
595 .TP
596 .I netdev_max_backlog
597 Maximum number of packets in the global input queue.
598 .TP
599 .I optmem_max
600 Maximum length of ancillary data and user control data like the iovecs
601 per socket.
602 .\" netdev_fastroute is not documented because it is experimental
603 .SS Ioctls
604 These operations can be accessed using
605 .BR ioctl (2):
606
607 .in +4n
608 .nf
609 .IB error " = ioctl(" ip_socket ", " ioctl_type ", " &value_result ");"
610 .fi
611 .in
612 .TP
613 .B SIOCGSTAMP
614 Return a
615 .I struct timeval
616 with the receive timestamp of the last packet passed to the user.
617 This is useful for accurate round trip time measurements.
618 See
619 .BR setitimer (2)
620 for a description of
621 .IR "struct timeval" .
622 .\"
623 This ioctl should only be used if the socket option
624 .B SO_TIMESTAMP
625 is not set on the socket.
626 Otherwise, it returns the timestamp of the
627 last packet that was received while
628 .B SO_TIMESTAMP
629 was not set, or it fails if no such packet has been received,
630 (i.e.,
631 .BR ioctl (2)
632 returns \-1 with
633 .I errno
634 set to
635 .BR ENOENT ).
636 .TP
637 .B SIOCSPGRP
638 Set the process or process group to send
639 .B SIGIO
640 or
641 .B SIGURG
642 signals
643 to when an
644 asynchronous I/O operation has finished or urgent data is available.
645 The argument is a pointer to a
646 .IR pid_t .
647 If the argument is positive, send the signals to that process.
648 If the
649 argument is negative, send the signals to the process group with the ID
650 of the absolute value of the argument.
651 The process may only choose itself or its own process group to receive
652 signals unless it has the
653 .B CAP_KILL
654 capability or an effective UID of 0.
655 .TP
656 .B FIOASYNC
657 Change the
658 .B O_ASYNC
659 flag to enable or disable asynchronous I/O mode of the socket.
660 Asynchronous I/O mode means that the
661 .B SIGIO
662 signal or the signal set with
663 .B F_SETSIG
664 is raised when a new I/O event occurs.
665 .IP
666 Argument is an integer boolean flag.
667 (This operation is synonymous with the use of
668 .BR fcntl (2)
669 to set the
670 .B O_ASYNC
671 flag.)
672 .\"
673 .TP
674 .B SIOCGPGRP
675 Get the current process or process group that receives
676 .B SIGIO
677 or
678 .B SIGURG
679 signals,
680 or 0
681 when none is set.
682 .PP
683 Valid
684 .BR fcntl (2)
685 operations:
686 .TP
687 .B FIOGETOWN
688 The same as the
689 .B SIOCGPGRP
690 .BR ioctl (2).
691 .TP
692 .B FIOSETOWN
693 The same as the
694 .B SIOCSPGRP
695 .BR ioctl (2).
696 .SH VERSIONS
697 .B SO_BINDTODEVICE
698 was introduced in Linux 2.0.30.
699 .B SO_PASSCRED
700 is new in Linux 2.2.
701 The
702 .I /proc
703 interfaces was introduced in Linux 2.2.
704 .B SO_RCVTIMEO
705 and
706 .B SO_SNDTIMEO
707 are supported since Linux 2.3.41.
708 Earlier, timeouts were fixed to
709 a protocol-specific setting, and could not be read or written.
710 .SH NOTES
711 Linux assumes that half of the send/receive buffer is used for internal
712 kernel structures; thus the values in the corresponding
713 .I /proc
714 files are twice what can be observed on the wire.
715
716 Linux will only allow port reuse with the
717 .B SO_REUSEADDR
718 option
719 when this option was set both in the previous program that performed a
720 .BR bind (2)
721 to the port and in the program that wants to reuse the port.
722 This differs from some implementations (e.g., FreeBSD)
723 where only the later program needs to set the
724 .B SO_REUSEADDR
725 option.
726 Typically this difference is invisible, since, for example, a server
727 program is designed to always set this option.
728 .SH BUGS
729 The
730 .B CONFIG_FILTER
731 socket options
732 .B SO_ATTACH_FILTER
733 and
734 .B SO_DETACH_FILTER
735 .\" FIXME Document SO_ATTACH_FILTER and SO_DETACH_FILTER
736 are not documented.
737 The suggested interface to use them is via the libpcap
738 library.
739 .\" .SH AUTHORS
740 .\" This man page was written by Andi Kleen.
741 .SH "SEE ALSO"
742 .BR getsockopt (2),
743 .BR setsockopt (2),
744 .BR socket (2),
745 .BR capabilities (7),
746 .BR ddp (7),
747 .BR ip (7),
748 .BR packet (7),
749 .BR tcp (7),
750 .BR udp (7),
751 .BR unix (7)