]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/getsockopt.2
bind.2, chmod.2, chown.2, chroot.2, clock_getres.2, clone.2, connect.2, dup.2, falloc...
[thirdparty/man-pages.git] / man2 / getsockopt.2
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\" $Id: getsockopt.2,v 1.1 1999/05/24 14:57:04 freitag Exp $
35 .\"
36 .\" Modified Sat Jul 24 16:19:32 1993 by Rik Faith (faith@cs.unc.edu)
37 .\" Modified Mon Apr 22 02:29:06 1996 by Martin Schulze (joey@infodrom.north.de)
38 .\" Modified Tue Aug 27 10:52:51 1996 by Andries Brouwer (aeb@cwi.nl)
39 .\" Modified Thu Jan 23 13:29:34 1997 by Andries Brouwer (aeb@cwi.nl)
40 .\" Modified Sun Mar 28 21:26:46 1999 by Andries Brouwer (aeb@cwi.nl)
41 .\" Modified 1999 by Andi Kleen <ak@muc.de>.
42 .\" Removed most stuff because it is in socket.7 now.
43 .\"
44 .TH GETSOCKOPT 2 2016-12-12 "Linux" "Linux Programmer's Manual"
45 .SH NAME
46 getsockopt, setsockopt \- get and set options on sockets
47 .SH SYNOPSIS
48 .nf
49 .BR "#include <sys/types.h>" " /* See NOTES */"
50 .br
51 .B #include <sys/socket.h>
52 .sp
53 .BI "int getsockopt(int " sockfd ", int " level ", int " optname ,
54 .BI " void *" optval ", socklen_t *" optlen );
55 .BI "int setsockopt(int " sockfd ", int " level ", int " optname ,
56 .BI " const void *" optval ", socklen_t " optlen );
57 .fi
58 .SH DESCRIPTION
59 .BR getsockopt ()
60 and
61 .BR setsockopt ()
62 manipulate options for the socket referred to by the file descriptor
63 .IR sockfd .
64 Options may exist at multiple
65 protocol levels; they are always present at the uppermost
66 socket level.
67
68 When manipulating socket options, the level at which the
69 option resides and the name of the option must be specified.
70 To manipulate options at the sockets API level,
71 .I level
72 is specified as
73 .BR SOL_SOCKET .
74 To manipulate options at any
75 other level the protocol number of the appropriate protocol
76 controlling the option is supplied.
77 For example,
78 to indicate that an option is to be interpreted by the
79 .B TCP
80 protocol,
81 .I level
82 should be set to the protocol number of
83 .BR TCP ;
84 see
85 .BR getprotoent (3).
86
87 The arguments
88 .I optval
89 and
90 .I optlen
91 are used to access option values for
92 .BR setsockopt ().
93 For
94 .BR getsockopt ()
95 they identify a buffer in which the value for the
96 requested option(s) are to be returned.
97 For
98 .BR getsockopt (),
99 .I optlen
100 is a value-result argument, initially containing the
101 size of the buffer pointed to by
102 .IR optval ,
103 and modified on return to indicate the actual size of
104 the value returned.
105 If no option value is to be supplied or returned,
106 .I optval
107 may be NULL.
108
109 .I Optname
110 and any specified options are passed uninterpreted to the appropriate
111 protocol module for interpretation.
112 The include file
113 .I <sys/socket.h>
114 contains definitions for socket level options, described below.
115 Options at
116 other protocol levels vary in format and name; consult the appropriate
117 entries in section 4 of the manual.
118
119 Most socket-level options utilize an
120 .I int
121 argument for
122 .IR optval .
123 For
124 .BR setsockopt (),
125 the argument should be nonzero to enable a boolean option, or zero if the
126 option is to be disabled.
127 .PP
128 For a description of the available socket options see
129 .BR socket (7)
130 and the appropriate protocol man pages.
131 .SH RETURN VALUE
132 On success, zero is returned for the standard options.
133 On error, \-1 is returned, and
134 .I errno
135 is set appropriately.
136
137 Netfilter allows the programmer
138 to define custom socket options with associated handlers; for such
139 options, the return value on success is the value returned by the handler.
140 .SH ERRORS
141 .TP 10
142 .B EBADF
143 The argument
144 .I sockfd
145 is not a valid file descriptor.
146 .TP
147 .B EFAULT
148 The address pointed to by
149 .I optval
150 is not in a valid part of the process address space.
151 For
152 .BR getsockopt (),
153 this error may also be returned if
154 .I optlen
155 is not in a valid part of the process address space.
156 .TP
157 .B EINVAL
158 .I optlen
159 invalid in
160 .BR setsockopt ().
161 In some cases this error can also occur for an invalid value in
162 .IR optval
163 (e.g., for the
164 .B IP_ADD_MEMBERSHIP
165 option described in
166 .BR ip (7)).
167 .TP
168 .B ENOPROTOOPT
169 The option is unknown at the level indicated.
170 .TP
171 .B ENOTSOCK
172 The file descriptor
173 .I sockfd
174 does not refer to a socket.
175 .SH CONFORMING TO
176 POSIX.1-2001, POSIX.1-2008,
177 SVr4, 4.4BSD (these system calls first appeared in 4.2BSD).
178 .\" SVr4 documents additional ENOMEM and ENOSR error codes, but does
179 .\" not document the
180 .\" .BR SO_SNDLOWAT ", " SO_RCVLOWAT ", " SO_SNDTIMEO ", " SO_RCVTIMEO
181 .\" options
182 .SH NOTES
183 POSIX.1 does not require the inclusion of
184 .IR <sys/types.h> ,
185 and this header file is not required on Linux.
186 However, some historical (BSD) implementations required this header
187 file, and portable applications are probably wise to include it.
188
189 For background on the
190 .I socklen_t
191 type, see
192 .BR accept (2).
193 .SH BUGS
194 Several of the socket options should be handled at lower levels of the
195 system.
196 .SH SEE ALSO
197 .BR ioctl (2),
198 .BR socket (2),
199 .BR getprotoent (3),
200 .BR protocols (5),
201 .BR ip (7),
202 .BR packet (7),
203 .BR socket (7),
204 .BR tcp (7),
205 .BR udp (7),
206 .BR unix (7)