BIND(2)
π BIND(2) β bestie edition π
okay so like. this is the manpage for bind(), a system call. it lives in section 2 of the manual, and it's from the System Calls Manual. it's giving β¨foundational networkingβ¨ and honestly? we're obsessed. π«Ά
πΈ LIBRARY πΈ
Standard C Library (libc, -lc). yeah bestie, you get it for free, it's literally right there. π
β¨ SYNOPSIS β¨
#include <sys/socket.h>
int
bind(int s, const struct sockaddr *addr, socklen_t addrlen);
three little args, one big glow-up. let's get into it. π¦
π DESCRIPTION π
so bind() is the system call that assigns the local protocol address to a socket. that's the whole job. that's the vibe.
here's the tea: when you make a socket with socket(2), it exists in an address family space but has no protocol address assigned. it's like a phone with no number. cute, but useless. π₯²
then bind() rolls up and goes "hey, put this addr on this socket." and boom. now it has an address. main character energy achieved. π
π NOTES π
okay important stuff, listen up:
- if you bind an address in the UNIX domain, it literally creates a socket in the file system. and here's the catch: you, the caller, have to delete it when you're done with it, using
unlink(2). no one else is gonna clean that up for you bestie. it will just sit there. forever. judging you. π - the rules for address binding vary between communication domains. so if you need the details, go consult the manual entries in section 4. iykyk.
- for maximum portability, you should always zero the socket address structure before you populate it and pass it to
bind(). like, always. zero it out first. it's the unspoken rule. π
β RETURN VALUES β
- returns the value
0if it worked. slay. β¨ - otherwise it returns
-1, and the global variableerrnogets set to tell you what went wrong. π
π₯ ERRORS π₯
bind() will fail (read: not eat) if any of these happen:
[EAGAIN]β the kernel resources needed to complete the request are temporarily unavailable. give it a sec, it's busy. π[EBADF]β thesargument is not a valid descriptor. that's not a socket bestie, that's a random number. π[EINVAL]β the socket is already bound to an address, and the protocol does not support binding to a new address; or the socket has been shut down. it's over, it's done, move on. β[EINVAL]β theaddrlenargument is not a valid length for the address family. wrong size, wrong vibe. π[ENOTSOCK]β thesargument is not a socket. again: not a socket. not it. π[EADDRNOTAVAIL]β the specified address is not available from the local machine. that address is not from around here. π«[EADDRINUSE]β the specified address is already in use. taken. spoken for. someone else got there first. π€[EAFNOSUPPORT]β addresses in the specified address family cannot be used with this socket. incompatible, no chemistry, it's not gonna work. π[EACCES]β the requested address is protected, and the current user has inadequate permission to access it. permission denied, you don't have the pass. π[EFAULT]β theaddrargument is not in a valid part of the user address space. bad pointer, bad pointer, whatcha gonna do. πΈ
π§ the UNIX-domain-specific ones π§
these errors are specific to binding addresses in the UNIX domain:
[ENOTDIR]β a component of the path prefix is not a directory. it's giving "not a folder, sorry."[ENAMETOOLONG]β a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. bestie your path is so long. 255 per piece, 1023 total. those are the numbers. no cap. π[ENOENT]β a prefix component of the path name does not exist. it's just... not there. π»[ELOOP]β too many symbolic links were encountered in translating the pathname. the symlinks are in a toxic situationship and it's looping. π[EIO]β an I/O error occurred while making the directory entry or allocating the inode. the disk said "no β€οΈ".[EINTEGRITY]β corrupted data was detected while reading from the file system. π¨ corruption alert π¨[EROFS]β the name would reside on a read-only file system. read-only means read-only, bestie. you cannot write there. periodt. π§[EISDIR]β an empty pathname was specified. yeah. empty. nothing. a void. π³οΈ
π«Ά SEE ALSO π«Ά
connect(2), getsockname(2), listen(2), socket(2)
go read those next, they're all in the same friend group. π
π HISTORY π
the bind() system call appeared in 4.2BSD. an icon. a legend. a pioneer. we stan a system call with lore. β¨
and that's the whole page bestie!! ππ
this gorgeous little document came from Debian, dated March 30, 2020, and it's filed under BIND(2) in the System Calls Manual. go bind something (a socket, obviously) and stay slay. π β¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms