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 β€” return EAGAIN instead of blocking, if the random(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/random and /dev/urandom are 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] β€” the GRND_NONBLOCK flag was set and the random(4) device was not yet seeded. you said don't wait, and she said bet, error βœ‹
  • [EFAULT] β€” the buf parameter 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 invalid flags was specified. you made up a flag. she noticed πŸ‘€
  • [EINVAL] β€” the requested buflen was larger than IOSIZE_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