]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/socket.2
sched_setattr.2: tfix
[thirdparty/man-pages.git] / man2 / socket.2
CommitLineData
77117f4f
MK
1'\" t
2.\" Copyright (c) 1983, 1991 The Regents of the University of California.
3.\" All rights reserved.
4.\"
a9cd9cb7 5.\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
77117f4f
MK
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
8c9302dc 33.\" %%%LICENSE_END
77117f4f
MK
34.\"
35.\" $Id: socket.2,v 1.4 1999/05/13 11:33:42 freitag Exp $
36.\"
37.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
38.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
39.\" Modified 1998, 1999 by Andi Kleen <ak@muc.de>
40.\" Modified 2002-07-17 by Michael Kerrisk <mtk.manpages@gmail.com>
41.\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
42.\"
9ba01802 43.TH SOCKET 2 2019-03-06 "Linux" "Linux Programmer's Manual"
77117f4f
MK
44.SH NAME
45socket \- create an endpoint for communication
46.SH SYNOPSIS
47.BR "#include <sys/types.h>" " /* See NOTES */"
48.br
49.B #include <sys/socket.h>
68e4db0a 50.PP
77117f4f
MK
51.BI "int socket(int " domain ", int " type ", int " protocol );
52.SH DESCRIPTION
53.BR socket ()
29899888
MK
54creates an endpoint for communication and returns a file descriptor
55that refers to that endpoint.
047013ac
MK
56The file descriptor returned by a successful call will be
57the lowest-numbered file descriptor not currently open for the process.
77117f4f
MK
58.PP
59The
60.I domain
61argument specifies a communication domain; this selects the protocol
62family which will be used for communication.
63These families are defined in
64.IR <sys/socket.h> .
22570de1 65The formats currently understood by the Linux kernel include:
77117f4f
MK
66.TS
67tab(:);
698c7b2f 68l1 lw40 l.
77117f4f
MK
69Name:Purpose:Man page
70T{
e900e16c 71.BR AF_UNIX
77117f4f
MK
72T}:T{
73Local communication
74T}:T{
75.BR unix (7)
76T}
77T{
e900e16c
MK
78.B AF_LOCAL
79T}:T{
80Synonym for
81.B AF_UNIX
82T}:T{
83T}
84T{
d4c8c97c 85.B AF_INET
77117f4f
MK
86T}:IPv4 Internet protocols:T{
87.BR ip (7)
88T}
89T{
5880549a
ES
90.B AF_AX25
91T}:T{
92Amateur radio AX.25 protocol
93T}:T{
94.\" Part of ax25-tools
95.BR ax25 (4)
77117f4f
MK
96T}
97T{
d4c8c97c 98.B AF_IPX
77117f4f
MK
99T}:IPX \- Novell protocols:
100T{
5880549a
ES
101.B AF_APPLETALK
102T}:AppleTalk:T{
103.BR ddp (7)
104T}
105T{
d4c8c97c 106.B AF_X25
77117f4f
MK
107T}:ITU-T X.25 / ISO-8208 protocol:T{
108.BR x25 (7)
109T}
110T{
5880549a
ES
111.B AF_INET6
112T}:IPv6 Internet protocols:T{
113.BR ipv6 (7)
114T}
115T{
5880549a
ES
116.B AF_DECnet
117T}:T{
e900e16c 118DECet protocol sockets
5880549a 119T}
77117f4f 120T{
5880549a
ES
121.B AF_KEY
122T}:T{
698c7b2f 123Key management protocol, originally developed for usage with IPsec
5880549a
ES
124T}
125T{
126.B AF_NETLINK
127T}:T{
128Kernel user interface device
129T}:T{
130.BR netlink (7)
77117f4f
MK
131T}
132T{
d4c8c97c 133.B AF_PACKET
77117f4f 134T}:T{
e900e16c 135Low-level packet interface
77117f4f
MK
136T}:T{
137.BR packet (7)
138T}
5880549a 139T{
5880549a
ES
140.B AF_RDS
141T}:T{
142.\" commit: 639b321b4d8f4e412bfbb2a4a19bfebc1e68ace4
698c7b2f 143Reliable Datagram Sockets (RDS) protocol
5880549a
ES
144T}:T{
145.\" rds-tools: https://github.com/oracle/rds-tools/blob/master/rds.7
146.\" rds-tools: https://github.com/oracle/rds-tools/blob/master/rds-rdma.7
e900e16c
MK
147.BR rds (7)
148.br
8dcb9145 149.BR rds-rdma (7)
5880549a
ES
150T}
151T{
5880549a
ES
152.B AF_PPPOX
153T}:T{
ff085a5e 154Generic PPP transport layer, for setting up L2 tunnels
e900e16c 155(L2TP and PPPoE)
5880549a
ES
156T}
157T{
5880549a
ES
158.B AF_LLC
159T}:T{
160.\" linux-history commit: 34beb106cde7da233d4df35dd3d6cf4fee937caa
698c7b2f 161Logical link control (IEEE 802.2 LLC) protocol
5880549a
ES
162T}
163T{
164.B AF_IB
165T}:T{
166.\" commits: 8d36eb01da5d371f..ce117ffac2e93334
698c7b2f 167InfiniBand native addressing
5880549a
ES
168T}
169T{
170.B AF_MPLS
171T}:T{
172.\" commits: 0189197f441602acdca3f97750d392a895b778fd
698c7b2f 173Multiprotocol Label Switching
5880549a
ES
174T}
175T{
176.B AF_CAN
177T}:T{
178.\" commits: 8dbde28d9711475a..5423dd67bd0108a1
698c7b2f 179Controller Area Network automotive bus protocol
5880549a
ES
180T}
181T{
182.B AF_TIPC
183T}:T{
184.\" commits: b97bf3fd8f6a16966d4f18983b2c40993ff937d4
698c7b2f 185TIPC, "cluster domain sockets" protocol
5880549a
ES
186T}
187T{
188.B AF_BLUETOOTH
189T}:T{
190.\" commits: 8d36eb01da5d371f..ce117ffac2e93334
698c7b2f 191Bluetooth low-level socket protocol
5880549a
ES
192T}
193T{
31d070f8
SM
194.B AF_ALG
195T}:T{
5880549a 196.\" commit: 03c8efc1ffeb6b82a22c1af8dd908af349563314
698c7b2f 197Interface to kernel crypto API
5880549a
ES
198T}
199T{
49c3b226
ES
200.B AF_VSOCK
201T}:T{
202.\" commit: d021c344051af91f42c5ba9fdedc176740cbd238
203VSOCK (originally "VMWare VSockets") protocol
204for hypervisor-guest communication
205T}:T{
206.BR vsock (7)
207T}
208T{
5880549a
ES
209.B AF_KCM
210T}:T{
211.\" commit: 03c8efc1ffeb6b82a22c1af8dd908af349563314
698c7b2f 212KCM (kernel connection multiplexor) interface
5880549a
ES
213T}
214T{
9eadb327
TK
215.B AF_XDP
216T}:T{
5880549a 217.\" commit: c0c77d8fb787cfe0c3fca689c2a30d1dad4eaba7
698c7b2f 218XDP (express data path) interface
9eadb327 219T}
77117f4f
MK
220.TE
221.PP
43c8308e
MK
222Further details of the above address families,
223as well as information on several other address families, can be found in
224.BR address_families (7).
225.PP
77117f4f
MK
226The socket has the indicated
227.IR type ,
228which specifies the communication semantics.
229Currently defined types
230are:
1ee674a4 231.TP 16
77117f4f
MK
232.B SOCK_STREAM
233Provides sequenced, reliable, two-way, connection-based byte streams.
234An out-of-band data transmission mechanism may be supported.
235.TP
236.B SOCK_DGRAM
237Supports datagrams (connectionless, unreliable messages of a fixed
238maximum length).
239.TP
240.B SOCK_SEQPACKET
241Provides a sequenced, reliable, two-way connection-based data
242transmission path for datagrams of fixed maximum length; a consumer is
243required to read an entire packet with each input system call.
244.TP
245.B SOCK_RAW
246Provides raw network protocol access.
247.TP
248.B SOCK_RDM
249Provides a reliable datagram layer that does not guarantee ordering.
250.TP
251.B SOCK_PACKET
252Obsolete and should not be used in new programs;
253see
254.BR packet (7).
255.PP
d332f86f 256Some socket types may not be implemented by all protocol families.
77117f4f 257.PP
af14d9f8
MK
258Since Linux 2.6.27, the
259.I type
260argument serves a second purpose:
261in addition to specifying a socket type,
262it may include the bitwise OR of any of the following values,
263to modify the behavior of
264.BR socket ():
265.TP 16
266.B SOCK_NONBLOCK
267Set the
268.BR O_NONBLOCK
7f11e32c
MK
269file status flag on the open file description (see
270.BR open (2))
271referred to by the new file descriptor.
af14d9f8
MK
272Using this flag saves extra calls to
273.BR fcntl (2)
274to achieve the same result.
275.TP
276.B SOCK_CLOEXEC
277Set the close-on-exec
278.RB ( FD_CLOEXEC )
279flag on the new file descriptor.
c5571b61 280See the description of the
af14d9f8
MK
281.B O_CLOEXEC
282flag in
283.BR open (2)
284for reasons why this may be useful.
285.PP
77117f4f
MK
286The
287.I protocol
288specifies a particular protocol to be used with the socket.
289Normally only a single protocol exists to support a particular
290socket type within a given protocol family, in which case
291.I protocol
292can be specified as 0.
293However, it is possible that many protocols may exist, in
294which case a particular protocol must be specified in this manner.
295The protocol number to use is specific to the \*(lqcommunication domain\*(rq
296in which communication is to take place; see
297.BR protocols (5).
298See
299.BR getprotoent (3)
300on how to map protocol name strings to protocol numbers.
301.PP
302Sockets of type
303.B SOCK_STREAM
f6e34058 304are full-duplex byte streams.
77117f4f
MK
305They do not preserve
306record boundaries.
307A stream socket must be in
308a
309.I connected
310state before any data may be sent or received on it.
311A connection to
312another socket is created with a
313.BR connect (2)
314call.
315Once connected, data may be transferred using
316.BR read (2)
317and
318.BR write (2)
319calls or some variant of the
320.BR send (2)
321and
322.BR recv (2)
323calls.
324When a session has been completed a
325.BR close (2)
326may be performed.
327Out-of-band data may also be transmitted as described in
328.BR send (2)
329and received as described in
330.BR recv (2).
331.PP
332The communications protocols which implement a
333.B SOCK_STREAM
334ensure that data is not lost or duplicated.
335If a piece of data for which
336the peer protocol has buffer space cannot be successfully transmitted
337within a reasonable length of time, then the connection is considered
338to be dead.
339When
340.B SO_KEEPALIVE
341is enabled on the socket the protocol checks in a protocol-specific
342manner if the other end is still alive.
343A
344.B SIGPIPE
345signal is raised if a process sends or receives
346on a broken stream; this causes naive processes,
347which do not handle the signal, to exit.
348.B SOCK_SEQPACKET
349sockets employ the same system calls as
350.B SOCK_STREAM
351sockets.
352The only difference is that
353.BR read (2)
354calls will return only the amount of data requested,
355and any data remaining in the arriving packet will be discarded.
356Also all message boundaries in incoming datagrams are preserved.
357.PP
358.B SOCK_DGRAM
359and
360.B SOCK_RAW
361sockets allow sending of datagrams to correspondents named in
362.BR sendto (2)
363calls.
364Datagrams are generally received with
365.BR recvfrom (2),
366which returns the next datagram along with the address of its sender.
367.PP
368.B SOCK_PACKET
369is an obsolete socket type to receive raw packets directly from the
370device driver.
371Use
372.BR packet (7)
373instead.
374.PP
375An
376.BR fcntl (2)
377.B F_SETOWN
378operation can be used to specify a process or process group to receive a
379.B SIGURG
380signal when the out-of-band data arrives or
381.B SIGPIPE
382signal when a
383.B SOCK_STREAM
384connection breaks unexpectedly.
385This operation may also be used to set the process or process group
386that receives the I/O and asynchronous notification of I/O events via
387.BR SIGIO .
388Using
389.B F_SETOWN
390is equivalent to an
391.BR ioctl (2)
392call with the
393.B FIOSETOWN
394or
395.B SIOCSPGRP
396argument.
397.PP
398When the network signals an error condition to the protocol module (e.g.,
b5fe8515 399using an ICMP message for IP) the pending error flag is set for the socket.
77117f4f
MK
400The next operation on this socket will return the error code of the pending
401error.
402For some protocols it is possible to enable a per-socket error queue
403to retrieve detailed information about the error; see
404.B IP_RECVERR
405in
406.BR ip (7).
407.PP
408The operation of sockets is controlled by socket level
409.IR options .
410These options are defined in
411.IR <sys/socket.h> .
412The functions
413.BR setsockopt (2)
414and
415.BR getsockopt (2)
83a9c27c 416are used to set and get options.
47297adb 417.SH RETURN VALUE
77117f4f
MK
418On success, a file descriptor for the new socket is returned.
419On error, \-1 is returned, and
420.I errno
421is set appropriately.
422.SH ERRORS
423.TP
424.B EACCES
425Permission to create a socket of the specified type and/or protocol
426is denied.
427.TP
428.B EAFNOSUPPORT
429The implementation does not support the specified address family.
430.TP
431.B EINVAL
432Unknown protocol, or protocol family not available.
433.TP
af14d9f8
MK
434.B EINVAL
435.\" Since Linux 2.6.27
436Invalid flags in
437.IR type .
438.TP
77117f4f 439.B EMFILE
26c32fab 440The per-process limit on the number of open file descriptors has been reached.
77117f4f
MK
441.TP
442.B ENFILE
e258766b 443The system-wide limit on the total number of open files has been reached.
77117f4f
MK
444.TP
445.BR ENOBUFS " or " ENOMEM
446Insufficient memory is available.
447The socket cannot be
448created until sufficient resources are freed.
449.TP
450.B EPROTONOSUPPORT
451The protocol type or the specified protocol is not
452supported within this domain.
453.PP
454Other errors may be generated by the underlying protocol modules.
47297adb 455.SH CONFORMING TO
478298bf 456POSIX.1-2001, POSIX.1-2008, 4.4BSD.
efeece04 457.PP
af14d9f8
MK
458The
459.B SOCK_NONBLOCK
460and
461.B SOCK_CLOEXEC
462flags are Linux-specific.
efeece04 463.PP
77117f4f
MK
464.BR socket ()
465appeared in 4.2BSD.
466It is generally portable to/from
467non-BSD systems supporting clones of the BSD socket layer (including
efbfd7ec 468System\ V variants).
77117f4f 469.SH NOTES
478298bf 470POSIX.1 does not require the inclusion of
77117f4f
MK
471.IR <sys/types.h> ,
472and this header file is not required on Linux.
473However, some historical (BSD) implementations required this header
474file, and portable applications are probably wise to include it.
efeece04 475.PP
77117f4f
MK
476The manifest constants used under 4.x BSD for protocol families
477are
478.BR PF_UNIX ,
479.BR PF_INET ,
4a037a4a
MK
480and so on, while
481.BR AF_UNIX ,
1f1fd5ef 482.BR AF_INET ,
4a037a4a 483and so on are used for address
77117f4f
MK
484families.
485However, already the BSD man page promises: "The protocol
486family generally is the same as the address family", and subsequent
487standards use AF_* everywhere.
77117f4f
MK
488.SH EXAMPLE
489An example of the use of
490.BR socket ()
491is shown in
492.BR getaddrinfo (3).
47297adb 493.SH SEE ALSO
77117f4f
MK
494.BR accept (2),
495.BR bind (2),
c1d0454a 496.BR close (2),
77117f4f
MK
497.BR connect (2),
498.BR fcntl (2),
499.BR getpeername (2),
500.BR getsockname (2),
501.BR getsockopt (2),
502.BR ioctl (2),
503.BR listen (2),
504.BR read (2),
505.BR recv (2),
506.BR select (2),
507.BR send (2),
508.BR shutdown (2),
509.BR socketpair (2),
510.BR write (2),
511.BR getprotoent (3),
43c8308e 512.BR address_families (7),
77117f4f
MK
513.BR ip (7),
514.BR socket (7),
515.BR tcp (7),
516.BR udp (7),
517.BR unix (7)
efeece04 518.PP
77117f4f 519\(lqAn Introductory 4.3BSD Interprocess Communication Tutorial\(rq
173fe7e7
DP
520and
521\(lqBSD Interprocess Communication Tutorial\(rq,
522reprinted in
77117f4f 523.I UNIX Programmer's Supplementary Documents Volume 1.