getsockopt(2) — Linux manual page

NAME | LIBRARY | SYNOPSIS | DESCRIPTION | RETURN VALUE | ERRORS | STANDARDS | HISTORY | BUGS | SEE ALSO | COLOPHON

getsockopt(2)              System Calls Manual             getsockopt(2)

NAME         top

       getsockopt, setsockopt - get and set options on sockets

LIBRARY         top

       Standard C library (libc, -lc)

SYNOPSIS         top

       #include <sys/socket.h>

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

DESCRIPTION         top

       getsockopt() and setsockopt() manipulate options for the socket
       referred to by the file descriptor sockfd.  Options may exist at
       multiple protocol levels; they are always present at the
       uppermost socket level.

       When manipulating socket options, the level at which the option
       resides and the name of the option must be specified.  To
       manipulate options at the sockets API level, level is specified
       as SOL_SOCKET.  To manipulate options at any other level the
       protocol number of the appropriate protocol controlling the
       option is supplied.  For example, 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).

       The arguments optval and optlen are used to access option values
       for setsockopt().  For getsockopt() they identify a buffer in
       which the value for the requested option(s) are to be returned.
       For getsockopt(), optlen is a value-result argument, initially
       containing the size of the buffer pointed to by optval, and
       modified on return to indicate the actual size of the value
       returned.  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; consult the appropriate entries in section 4 of the
       manual.

       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.

       For a description of the available socket options see socket(7)
       and the appropriate protocol man pages.

RETURN VALUE         top

       On success, zero is returned for the standard options.  On error,
       -1 is returned, and errno is set to indicate the error.

       Netfilter allows the programmer to define custom socket options
       with associated handlers; for such options, the return value on
       success is the value returned by the handler.

ERRORS         top

       EBADF  The argument sockfd is not a valid file descriptor.

       EFAULT The address pointed to by optval is not in a valid part of
              the process address space.  For getsockopt(), this error
              may also be returned if optlen is not in a valid part of
              the process address space.

       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)).

       ENOPROTOOPT
              The option is unknown at the level indicated.

       ENOTSOCK
              The file descriptor sockfd does not refer to a socket.

STANDARDS         top

       POSIX.1-2008.

HISTORY         top

       POSIX.1-2001, SVr4, 4.4BSD (first appeared in 4.2BSD).

BUGS         top

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

SEE ALSO         top

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

COLOPHON         top

       This page is part of the man-pages (Linux kernel and C library
       user-space interface documentation) project.  Information about
       the project can be found at 
       ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report
       for this manual page, see
       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.
       This page was obtained from the tarball man-pages-6.9.1.tar.gz
       fetched from
       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on
       2024-06-26.  If you discover any rendering problems in this HTML
       version of the page, or you believe there is a better or more up-
       to-date source for the page, or you have corrections or
       improvements to the information in this COLOPHON (which is not
       part of the original manual page), send a mail to
       [email protected]

Linux man-pages 6.9.1          2024-05-02                  getsockopt(2)

Pages that refer to this page: connect(2)PR_SET_TAGGED_ADDR_CTRL(2const)recv(2)send(2)socket(2)socketcall(2)syscalls(2)if_nameindex(3)io_uring_prep_cmd(3)sctp_connectx(3)sockaddr(3type)bpf-helpers(7)capabilities(7)ip(7)ipv6(7)netlink(7)packet(7)raw(7)sctp(7)signal(7)signal-safety(7)socket(7)tcp(7)udp(7)udplite(7)unix(7)vsock(7)x25(7)