BINDAT(2)

πŸ’… BINDAT(2) β€” the bind but make it directory-relative page

okay so this is a system calls manual page, from section 2, and it's giving "we made bind(2) but with a file descriptor glow-up". Let's get into it bestie πŸŽ€


✨ NAME ✨

bindat β€” assign a local protocol address to a socket


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

like, that's the one you already link against, no cap.


🌸 SYNOPSIS 🌸

#include <sys/types.h>
#include <sys/socket.h>

#include <fcntl.h>

int
bindat(int fd, int s, const struct sockaddr *addr, socklen_t addrlen);

so the four things you're handing over are fd, s, addr, and addrlen. and yes you need all those headers, don't fight it πŸ’–


πŸ¦‹ DESCRIPTION πŸ¦‹

The bindat() system call assigns the local protocol address to a socket. periodt.

When you pass the special value AT_FDCWD in the fd parameter, the behavior is identical to a call to bind(2). like, literally the same thing. no notes.

Otherwise, bindat() works like the bind(2) system call with exactly two exceptions, and bestie these are the whole personality:

  1. It is limited to sockets in the PF_LOCAL domain. πŸ‘‘ only local-domain girlies allowed.
  2. If the file path stored in the sun_path field of the sockaddr_un structure is a relative path, it is located relative to the directory associated with the file descriptor fd.

so yeah, that's the whole bit β€” relative paths get resolved against fd instead of wherever your process happens to be chillin. iykyk πŸ’…


⭐ RETURN VALUES ⭐

  • The bindat() function returns the value 0 if successful ✨
  • otherwise the value -1 is returned and the global variable errno is set to indicate the error 😭

zero means slay, negative one means check errno. that's it that's the vibe check.


πŸ’€ ERRORS πŸ’€

The bindat() system call may fail with the same errors as the bind(2) system call, or with the following errors:

  • [EBADF] β€” The sun_path field does not specify an absolute path and the fd argument is neither AT_FDCWD nor a valid file descriptor. πŸ™ˆ
  • [ENOTDIR] β€” The sun_path field is not an absolute path and fd is neither AT_FDCWD nor a file descriptor associated with a directory.
ngl both of these are the "you gave me a relative path AND a bad fd" combo special. the path being relative is fine, the fd being trash is not. 🫢

πŸ”₯ SEE ALSO πŸ”₯

go read the friends, they're all iconic in their own way πŸ’œ


πŸŽ€ AUTHORS πŸŽ€

The bindat was developed by Pawel Jakub Dawidek <pawel@dawidek.net> under sponsorship from the FreeBSD Foundation. 🫢 giving patron-energy, honestly.


and that's the whole page bestie!! this one came from Debian, dated February 13, 2013, and it lives in BINDAT(2) β€” section 2, system calls manual. mwah πŸ’‹ go bind something responsibly ✨

πŸ“¦ where this page comes from