getpeereid(3bsd)
π getpeereid β the libbsd glow-up β¨
okay bestie, so you know how Unix-domain sockets are like. a whole thing? well getpeereid is the function from 3bsd that lets you peek at who's actually on the other end. it's giving "who dis" but for sockets, and it's reliable. no cap.
π LIBRARY
Utility functions from BSD systems β that's libbsd, and you link it with -lbsd. yes queen, add the flag. π
β¨ SYNOPSIS β¨
#include <sys/types.h>
#include <unistd.h>
(See libbsd(7) for include usage.)
int
getpeereid(int s, uid_t *euid, gid_t *egid);
π‘ friendly reminder: check libbsd(7) for how the include situation works, it's got the deets.
π¦ DESCRIPTION π¦
so. getpeereid() hands you the effective user ID and the effective group ID of the peer that's connected to a Unix-domain socket. literally just. the credentials. of your socket bestie.
the argument s has to be a Unix-domain socket (unix(4)) of type SOCK_STREAM β and like, connect(2) or listen(2) must have already been called on it. no skipping steps babe. β
the effective user ID lands in euid, and the effective group ID lands in egid. cute little out-params. π«Ά
now here's the part that's actually iconic:
- if you're the
listen(2)caller, the credentials you get back are your peer's credentials at the time it calledconnect(2). - if you're the
connect(2)caller, the credentials you get back are your peer's credentials at the time it calledlisten(2).
and the way this is reliable?? there is genuinely no way for either side to influence the credentials returned to its peer β the only way to change them is to call the appropriate system call (so, either connect(2) or listen(2)) under different effective credentials. that's it. that's the whole loophole. there isn't one. π
one super common use of this routine is a Unix-domain server verifying the credentials of its client. and likewise? the client can verify the credentials of the server. mutual verification. it's giving healthy relationship. π
π§ IMPLEMENTATION NOTES π§
on FreeBSD, getpeereid() is implemented in terms of the LOCAL_PEERCRED unix(4) socket option. so that's the tea. β
π RETURN VALUES π
getpeereid()returns the value0when it's successful. slay. β- otherwise it returns the value
-1, and the global variableerrnogets set to indicate the error. π
π ERRORS π
getpeereid() fails if:
[EBADF]β the argumentsis not a valid descriptor. bestie that's not even a real fd. π[ENOTSOCK]β the argumentsis a file, not a socket. wrong vibe entirely, it's giving regular file energy. π[ENOTCONN]β the argumentsdoes not refer to a socket on whichconnect(2)orlisten(2)have been called. you gotta commit first, babe. π[EINVAL]β the argumentsdoes not refer to a socket of typeSOCK_STREAM, or the kernel returned invalid data. either wrong socket type or the kernel is being weird, and honestly both are a moment. π
π SEE ALSO π
connect(2), getpeername(2), getsockname(2), getsockopt(2), listen(2), unix(4)
like. go read them. iykyk. πβ¨
πΈ HISTORY πΈ
getpeereid() appeared in FreeBSD 4.6, NetBSD 5.0 and OpenBSD 3.0. a whole trilogy. main character energy across three BSDs. π
and that's the page bestie!! π this little beauty came out of Debian, dated July 15, 2001 β yes it's older than you, it's a vintage icon and it still ate. π β¨π¦
π¦ where this page comes from
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 2001 Dima Dorfman
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms