GETRANDOM(2)
π getrandom(2) β the glow-up edition π
okay bestie, so you want random data? say less. this is getrandom from the System Calls Manual, and it's literally the moment your program gets to vibe with pure chaos. no cap.
β¨ NAME β¨
getrandom β get random data π«
π«Ά LIBRARY π«Ά
Standard C Library (libc, -lc) β she's built in, you don't have to go looking for her π
πΈ SYNOPSIS πΈ
#include <sys/random.h>
ssize_t
getrandom(void *buf, size_t buflen, unsigned int flags);
π¦ DESCRIPTION π¦
so basically getrandom() fills buf with up to buflen bytes of random data. that's it. that's the whole job. iconic behaviour honestly β¨
the flags argument may include zero or more of the following, and yes you can mix and match:
GRND_NONBLOCKβ returnEAGAINinstead of blocking, if therandom(4)device has not yet been seeded. by default (no flag, no drama)getrandom()will just sit there and block until the device is seeded. like she's waiting for her iced coffee. patient queen βGRND_RANDOMβ bestie. this flag does nothing on FreeBSD./dev/randomand/dev/urandomare identical. it's giving decorative. it's giving participation trophy. it's giving π
key deets: if the random(4) device has been seeded, reads of up to 256 bytes will always return as many bytes as requested and will not be interrupted by signals. 256 bytes or less = chef's kiss, fully delivered, signal-proof π«Ά
π RETURN VALUES π
upon successful completion, the number of bytes which were actually read is returned. so cute, so simple.
but watch out: for requests larger than 256 bytes, this can be fewer bytes than were requested. so if you asked for a big chunk, don't be shocked if she gives you less β she's not being rude, that's just how she is π
otherwise, -1 is returned and the global variable errno is set to indicate the error. the classic. the -1 jumpscare π
π ERRORS π
the getrandom() operation returns the following errors:
[EAGAIN]β theGRND_NONBLOCKflag was set and therandom(4)device was not yet seeded. you said don't wait, and she said bet, error β[EFAULT]β thebufparameter points to an invalid address. bad address bestie, fix your pointer π«[EINTR]β the sleep was interrupted by a signal. rude of that signal fr fr π€[EINVAL]β an invalidflagswas specified. you made up a flag. she noticed π[EINVAL]β the requestedbuflenwas larger thanIOSIZE_MAX. that's too much. she has limits. slay responsibly π
π SEE ALSO π
arc4random(3), getentropy(3), random(4)
go read those too, they're all part of the random-verse iykyk π
β STANDARDS β
getentropy() is non-standard. It is present in Linux. so like, it's not officially official, but Linux said "we have it at home" and honestly? good for her π
π HISTORY π
The getrandom() system call first appeared in FreeBSD 12.0. a legend was born β¨
Debian π
February 24, 2018 π
GETRANDOM(2)
and that's the whole page bestie!! that's the footer, that's the date, that's the distro, that's the section β we're done here. go forth and generate randomness responsibly. mwah π«Άπβ¨
π¦ 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) 2018 Conrad Meyer <cem@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms