getsockopt(2)

๐Ÿ’– getsockopt(2) โ€” the System Calls Manual page, but make it make sense โœจ

okay bestie, gather round ๐Ÿซถ this is the <hotpink>getsockopt(2)</hotpink> page from the System Calls Manual, and honestly? it's giving "two little functions that run your entire network stack's whole personality." let's get into it ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

getsockopt, setsockopt โ€” get and set options on sockets ๐ŸŽ€

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” yeah bb, it's just libc, no extra credit needed โœจ

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <sys/socket.h>

int getsockopt(socklen_t *restrict optlen;
               int sockfd, int level, int optname,
               void optval[_Nullable restrict *optlen],
               socklen_t *restrict optlen);
int setsockopt(socklen_t optlen;
               int sockfd, int level, int optname,
               const void optval[optlen],
               socklen_t optlen);

no cap, that's exactly how it's written up there, weird semicolons and all. we do not editorialize the synopsis. periodt ๐Ÿ’€

๐Ÿ‘‘ DESCRIPTION ๐Ÿ‘‘

okay so, <hotpink>getsockopt()</hotpink> and <hotpink>setsockopt()</hotpink> manipulate options for the socket referred to by the file descriptor sockfd. that's the whole assignment, bestie. ๐Ÿซถ

options can live at multiple protocol levels, and the way that they're always present at the uppermost socket level? iconic behavior honestly. ๐ŸŒˆ

when you're manipulating socket options, you gotta specify two things: the level where the option resides, and the optname. to manipulate options at the sockets API level, level is specified as SOL_SOCKET. to manipulate options at any other level, you supply the protocol number of whatever protocol is controlling the option. for example โ€” and this is the classic โ€” to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP. see getprotoent(3) for that. โœจ

now the fun part:

  • optval and optlen are what you use to access option values for setsockopt().
  • for getsockopt(), those same two identify a buffer where the value for the requested option(s) are to be returned.
  • for getsockopt(), optlen is a value-result argument ๐Ÿ™ˆ โ€” it initially contains the size of the buffer pointed to by optval, and it gets modified on return to indicate the actual size of the value returned. she goes in one way and comes out another, main character energy fr.
  • if no option value is to be supplied or returned, optval may be NULL.

optname and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. the include file <sys/socket.h> contains definitions for socket level options, described below. options at other protocol levels vary in format and name โ€” so go consult the appropriate entries in section 4 of the manual, iykyk ๐Ÿ“–

most socket-level options utilize an int argument for optval. for setsockopt(), the argument should be nonzero to enable a boolean option, or zero if the option is to be disabled. simple as that ๐Ÿ’…

for a description of the available socket options, see socket(7) and the appropriate protocol man pages. ๐Ÿฆ‹

๐ŸŒŸ RETURN VALUE ๐ŸŒŸ

on success, zero is returned for the standard options. on error, -1 is returned, and errno is set to indicate the error. classic. ๐Ÿ˜Œ

but wait โ€” plot twist: Netfilter lets the programmer define custom socket options with associated handlers, and for such options, the return value on success is the value returned by the handler. so don't go assuming it's always zero, bestie, check your handler ๐Ÿซถ

๐Ÿ’€ ERRORS ๐Ÿ’€

here's where things go wrong. and when they do, errno tells the story:

  • EBADF โ€” the argument sockfd is not a valid file descriptor. not a socket, not a vibe, nothing. ๐Ÿ™ˆ
  • EFAULT โ€” the address pointed to by optval is not in a valid part of the process address space. and for getsockopt(), this error may also be returned if optlen is not in a valid part of the process address space. so check both, babe. ๐Ÿ˜ญ
  • EINVAL โ€” optlen invalid in setsockopt(). in some cases this error can also occur for an invalid value in optval (e.g., for the IP_ADD_MEMBERSHIP option described in ip(7)). so yeah, sometimes it's the length, sometimes it's the value, and it will not tell you which. love that for us ๐Ÿ’…
  • ENOPROTOOPT โ€” the option is unknown at the level indicated. wrong level, wrong option, wrong everything. โœจ
  • ENOTSOCK โ€” the file descriptor sockfd does not refer to a socket. she's not a socket, she's just some fd standing there. ๐Ÿ”ฅ

๐Ÿ“œ STANDARDS ๐Ÿ“œ

POSIX.1-2024. certified, standard, official. ๐Ÿ‘‘

๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

POSIX.1-2001, SVr4, 4.4BSD โ€” and it first appeared in 4.2BSD. so this function is old enough to have grandkids and it's still carrying the whole internet. respect your elders ๐ŸŽ€

๐Ÿ› BUGS ๐Ÿ›

Several of the socket options should be handled at lower levels of the system.

that's it, that's the bug note. it's in the original, it's staying, and honestly it's the vaguest drag in the whole manual. ๐Ÿ’€

๐Ÿ”— SEE ALSO ๐Ÿ”—

ioctl(2), socket(2), getprotoent(3), protocols(5), ip(7), packet(7), socket(7), tcp(7), udp(7), unix(7) ๐Ÿฆ‹


and that's the whole page, bestie โ€” this one came out of Linux man-pages 6.20, dated 2025-10-29, and it's the getsockopt(2) page in all its glory ๐ŸŒธ go set some socket options, you absolute legend ๐Ÿ’–โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms