Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


getrandom(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |STANDARDS |HISTORY |NOTES |BUGS |SEE ALSO |COLOPHON

getrandom(2)               System Calls Manualgetrandom(2)

NAME        top

       getrandom - obtain a series of random bytes

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/random.h>ssize_t getrandom(size_t size;voidbuf[size], size_tsize, unsigned intflags);

DESCRIPTION        top

       Thegetrandom() system call fills the buffer pointed to bybuf       with up tosize random bytes.  These bytes can be used to seed       user-space random number generators or for cryptographic purposes.       By default,getrandom() draws entropy from theurandom source       (i.e., the same source as the/dev/urandom device).  This behavior       can be changed via theflags argument.       If theurandom source has been initialized, reads of up to 256       bytes will always return as many bytes as requested and will not       be interrupted by signals.  No such guarantees apply for larger       buffer sizes.  For example, if the call is interrupted by a signal       handler, it may return a partially filled buffer, or fail with the       errorEINTR.       If theurandom source has not yet been initialized, thengetrandom() will block, unlessGRND_NONBLOCKis specified inflags.       Theflags argument is a bit mask that can contain zero or more of       the following values ORed together:GRND_RANDOM              If this bit is set, then random bytes are drawn from therandom source (i.e., the same source as the/dev/random              device) instead of theurandom source.  Therandom source              is limited based on the entropy that can be obtained from              environmental noise.  If the number of available bytes in              therandom source is less than requested insize, the call              returns just the available random bytes.  If no random              bytes are available, the behavior depends on the presence              ofGRND_NONBLOCKin theflags argument.GRND_NONBLOCK              By default, when reading from therandom source,getrandom() blocks if no random bytes are available, and              when reading from theurandom source, it blocks if the              entropy pool has not yet been initialized.  If theGRND_NONBLOCKflag is set, thengetrandom() does not block              in these cases, but instead immediately returns -1 witherrno set toEAGAIN.

RETURN VALUE        top

       On success,getrandom() returns the number of bytes that were       copied to the bufferbuf.  This may be less than the number of       bytes requested viasize if eitherGRND_RANDOMwas specified inflags and insufficient entropy was present in therandom source or       the system call was interrupted by a signal.       On error, -1 is returned, anderrno is set to indicate the error.

ERRORS        top

EAGAINThe requested entropy was not available, andgetrandom()              would have blocked if theGRND_NONBLOCKflag was not set.EFAULTThe address referred to bybuf is outside the accessible              address space.EINTRThe call was interrupted by a signal handler; see the              description of how interruptedread(2) calls on "slow"              devices are handled with and without theSA_RESTARTflag in              thesignal(7) man page.EINVALAn invalid flag was specified inflags.ENOSYSThe glibc wrapper function forgetrandom() determined that              the underlying kernel does not implement this system call.

STANDARDS        top

       Linux.

HISTORY        top

       Linux 3.17, glibc 2.25.

NOTES        top

       For an overview and comparison of the various interfaces that can       be used to obtain randomness, seerandom(7).       Unlike/dev/random and/dev/urandom,getrandom() does not involve       the use of pathnames or file descriptors.  Thus,getrandom() can       be useful in cases wherechroot(2) makes/dev pathnames invisible,       and where an application (e.g., a daemon during start-up) closes a       file descriptor for one of these files that was opened by a       library.Maximum number of bytes returned       As of Linux 3.19 the following limits apply:       •  When reading from theurandom source, a maximum of 32Mi-1 bytes          is returned by a single call togetrandom() on systems whereint has a size of 32 bits.       •  When reading from therandom source, a maximum of 512 bytes is          returned.Interruption by a signal handler       When reading from theurandom source (GRND_RANDOMis not set),getrandom() will block until the entropy pool has been initialized       (unless theGRND_NONBLOCKflag was specified).  If a request is       made to read a large number of bytes (more than 256),getrandom()       will block until those bytes have been generated and transferred       from kernel memory tobuf.  When reading from therandom source       (GRND_RANDOMis set),getrandom() will block until some random       bytes become available (unless theGRND_NONBLOCKflag was       specified).       The behavior when a call togetrandom() that is blocked while       reading from theurandom source is interrupted by a signal handler       depends on the initialization state of the entropy buffer and on       the request size,size.  If the entropy is not yet initialized,       then the call fails with theEINTRerror.  If the entropy pool has       been initialized and the request size is large (size > 256), the       call either succeeds, returning a partially filled buffer, or       fails with the errorEINTR.  If the entropy pool has been       initialized and the request size is small (size <= 256), thengetrandom() will not fail withEINTR.  Instead, it will return all       of the bytes that have been requested.       When reading from therandom source, blocking requests of any size       can be interrupted by a signal handler (the call fails with the       errorEINTR).       Usinggetrandom() to read small buffers (<= 256 bytes) from theurandom source is the preferred mode of usage.       The special treatment of small values ofsize was designed for       compatibility with OpenBSD'sgetentropy(3), which is nowadays       supported by glibc.       The user ofgetrandom()must always check the return value, to       determine whether either an error occurred or fewer bytes than       requested were returned.  In the case whereGRND_RANDOMis not       specified andsize is less than or equal to 256, a return of fewer       bytes than requested should never happen, but the careful       programmer will check for this anyway!

BUGS        top

       As of Linux 3.19, the following bug exists:       •  Depending on CPU load,getrandom() does not react to interrupts          before reading all bytes requested.

SEE ALSO        top

getentropy(3),random(4),urandom(4),random(7),signal(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.16.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2026-01-16.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.16            2025-09-21getrandom(2)

Pages that refer to this page:mcookie(1)syscalls(2)getentropy(3)random(3)sd_id128_randomize(3)uuid_generate(3)random(4)random(7)signal(7)



HTML rendering created 2026-01-16 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2026 Movatter.jp