GETSOCKOPT(3POSIX)

πŸ’… GETSOCKOPT β€” get the socket options πŸ’…

okay bestie, gather round, because this is the POSIX Programmer's Manual page for getsockopt, and she is lowkey a legend. GETSOCKOPT(3POSIX), periodt.


🚨 PROLOG (read this first, fr fr)

This manual page is part of the POSIX Programmer's Manual. Here's the tea:

  • The Linux implementation of this interface may differ β€” so consult the corresponding Linux manual page for the actual Linux behavior, okay?
  • Or honestly, the interface may not even be implemented on Linux at all. πŸ™ˆ
So like, no cap: check your local docs before you trust this blindly. ✨

✨ NAME ✨

getsockopt β€” get the socket options πŸ’–


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/socket.h>

int getsockopt(int socket, int level, int option_name,
    void *restrict option_value, socklen_t *restrict option_len);

Like. She's five arguments and she's serving. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

The getsockopt() function manipulates options associated with a socket. Main character energy, honestly.

The getsockopt() function shall retrieve the value for the option specified by the option_name argument for the socket specified by the socket argument. And here's the tricky part, so pay attention:

  • If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. (yes, silently. she just does it. no warning. rude but iconic πŸ’€)
  • Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value.

The level argument specifies the protocol level at which the option resides. So:

  • To retrieve options at the socket level, specify the level argument as SOL_SOCKET.
  • To retrieve options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicate that an option is interpreted by the TCP (Transmission Control Protocol), set level to IPPROTO_TCP as defined in the <netinet/in.h> header. 🌸

The socket in use may require the process to have appropriate privileges to use the getsockopt() function. iykyk.

The option_name argument specifies a single option to be retrieved. It can be one of the socket-level options defined in <sys_socket.h> and described in Section 2.10.16, Use of Options.


πŸ”₯ RETURN VALUE πŸ”₯

  • Upon successful completion, getsockopt() shall return 0. slay. πŸ’–
  • Otherwise, -1 shall be returned and errno set to indicate the error. 😭

πŸ’€ ERRORS πŸ’€

The getsockopt() function shall fail if:

  • EBADF β€” The socket argument is not a valid file descriptor.
  • EINVAL β€” The specified option is invalid at the specified socket level.
  • ENOPROTOOPT β€” The option is not supported by the protocol.
  • ENOTSOCK β€” The socket argument does not refer to a socket.

The getsockopt() function may fail if:

  • EACCES β€” The calling process does not have appropriate privileges.
  • EINVAL β€” The socket has been shut down.
  • ENOBUFS β€” Insufficient resources are available in the system to complete the function.
Not the "shall" versus "may" distinction doing the absolute most. 😭

The following sections are informative. (translation: the real spec is above, everything below is vibes 🫢)


🌈 EXAMPLES 🌈

None.


🧚 APPLICATION USAGE 🧚

None.


⭐ RATIONALE ⭐

None.


πŸ‘‘ FUTURE DIRECTIONS πŸ‘‘

None.


πŸ’œ SEE ALSO πŸ’œ

  • Section 2.10.16, Use of Options
  • bind()
  • close()
  • endprotoent()
  • setsockopt()
  • socket()

The Base Definitions volume of POSIX.1‐2017, <sys_socket.h>, <netinet_in.h>


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie!! brought to you by IEEE/The Open Group, 2017 β€” the original GETSOCKOPT(3POSIX) moment. go get those socket options. πŸ’…βœ¨πŸŽ€