1GETRANDOM(2)               Linux Programmer's Manual              GETRANDOM(2)
2
3
4

NAME

6       getrandom - obtain a series of random bytes
7

SYNOPSIS

9       #include <sys/random.h>
10
11       ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
12

DESCRIPTION

14       The  getrandom() system call fills the buffer pointed to by buf with up
15       to buflen random bytes.  These bytes can be  used  to  seed  user-space
16       random number generators or for cryptographic purposes.
17
18       By  default,  getrandom()  draws entropy from the urandom source (i.e.,
19       the same source as the /dev/urandom  device).   This  behavior  can  be
20       changed via the flags argument.
21
22       If  the  urandom  source has been initialized, reads of up to 256 bytes
23       will always return as many bytes as requested and will  not  be  inter‐
24       rupted  by  signals.  No such guarantees apply for larger buffer sizes.
25       For example, if the call is interrupted by a  signal  handler,  it  may
26       return a partially filled buffer, or fail with the error EINTR.
27
28       If  the  urandom  source has not yet been initialized, then getrandom()
29       will block, unless GRND_NONBLOCK is specified in flags.
30
31       The flags argument is a bit mask that can contain zero or more  of  the
32       following values ORed together:
33
34       GRND_RANDOM
35              If  this bit is set, then random bytes are drawn from the random
36              source (i.e., the same source as the /dev/random device) instead
37              of  the  urandom  source.  The random source is limited based on
38              the entropy that can be obtained from environmental  noise.   If
39              the  number of available bytes in the random source is less than
40              requested in buflen, the call returns just the available  random
41              bytes.   If  no random bytes are available, the behavior depends
42              on the presence of GRND_NONBLOCK in the flags argument.
43
44       GRND_NONBLOCK
45              By default, when reading from  the  random  source,  getrandom()
46              blocks  if  no random bytes are available, and when reading from
47              the urandom source, it blocks if the entropy pool  has  not  yet
48              been  initialized.   If  the  GRND_NONBLOCK  flag  is  set, then
49              getrandom() does not block in these cases, but  instead  immedi‐
50              ately returns -1 with errno set to EAGAIN.
51

RETURN VALUE

53       On success, getrandom() returns the number of bytes that were copied to
54       the buffer buf.  This may be less than the number  of  bytes  requested
55       via  buflen  if  either GRND_RANDOM was specified in flags and insuffi‐
56       cient entropy was present in the random source or the system  call  was
57       interrupted by a signal.
58
59       On error, -1 is returned, and errno is set appropriately.
60

ERRORS

62       EAGAIN The  requested  entropy was not available, and getrandom() would
63              have blocked if the GRND_NONBLOCK flag was not set.
64
65       EFAULT The address referred to by buf is outside the accessible address
66              space.
67
68       EINTR  The  call  was interrupted by a signal handler; see the descrip‐
69              tion of how interrupted read(2) calls on "slow" devices are han‐
70              dled  with  and without the SA_RESTART flag in the signal(7) man
71              page.
72
73       EINVAL An invalid flag was specified in flags.
74
75       ENOSYS The glibc wrapper function for getrandom() determined  that  the
76              underlying kernel does not implement this system call.
77

VERSIONS

79       getrandom()  was  introduced in version 3.17 of the Linux kernel.  Sup‐
80       port was added to glibc in version 2.25.
81

CONFORMING TO

83       This system call is Linux-specific.
84

NOTES

86       For an overview and comparison of the various interfaces  that  can  be
87       used to obtain randomness, see random(7).
88
89       Unlike  /dev/random  and /dev/urandom, getrandom() does not involve the
90       use of pathnames or file descriptors.  Thus, getrandom() can be  useful
91       in  cases  where chroot(2) makes /dev pathnames invisible, and where an
92       application (e.g., a daemon during start-up) closes a  file  descriptor
93       for one of these files that was opened by a library.
94
95   Maximum number of bytes returned
96       As of Linux 3.19 the following limits apply:
97
98       *  When reading from the urandom source, a maximum of 33554431 bytes is
99          returned by a single call to getrandom() on systems where int has  a
100          size of 32 bits.
101
102       *  When  reading  from  the  random  source,  a maximum of 512 bytes is
103          returned.
104
105   Interruption by a signal handler
106       When reading from the urandom source (GRND_RANDOM is not set),  getran‐
107       dom()  will  block  until the entropy pool has been initialized (unless
108       the GRND_NONBLOCK flag was specified).  If a request is made to read  a
109       large  number  of  bytes  (more than 256), getrandom() will block until
110       those bytes have been generated and transferred from kernel  memory  to
111       buf.  When reading from the random source (GRND_RANDOM is set), getran‐
112       dom() will block until some random bytes become available  (unless  the
113       GRND_NONBLOCK flag was specified).
114
115       The  behavior  when a call to getrandom() that is blocked while reading
116       from the urandom source is interrupted by a signal handler  depends  on
117       the initialization state of the entropy buffer and on the request size,
118       buflen.  If the entropy is not yet initialized,  then  the  call  fails
119       with the EINTR error.  If the entropy pool has been initialized and the
120       request size is large (buflen > 256), the call either succeeds, return‐
121       ing  a  partially filled buffer, or fails with the error EINTR.  If the
122       entropy pool has  been  initialized  and  the  request  size  is  small
123       (buflen <= 256),  then  getrandom() will not fail with EINTR.  Instead,
124       it will return all of the bytes that have been requested.
125
126       When reading from the random source, blocking requests of any size  can
127       be  interrupted  by  a  signal  handler  (the call fails with the error
128       EINTR).
129
130       Using getrandom() to read small buffers (<= 256 bytes) from the urandom
131       source is the preferred mode of usage.
132
133       The  special  treatment of small values of buflen was designed for com‐
134       patibility with OpenBSD's getentropy(3), which is nowadays supported by
135       glibc.
136
137       The  user  of getrandom() must always check the return value, to deter‐
138       mine whether either an error occurred or  fewer  bytes  than  requested
139       were  returned.   In  the  case  where GRND_RANDOM is not specified and
140       buflen is less than or equal to 256,  a  return  of  fewer  bytes  than
141       requested  should  never  happen, but the careful programmer will check
142       for this anyway!
143

BUGS

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

SEE ALSO

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

COLOPHON

154       This  page  is  part of release 4.16 of the Linux man-pages project.  A
155       description of the project, information about reporting bugs,  and  the
156       latest     version     of     this    page,    can    be    found    at
157       https://www.kernel.org/doc/man-pages/.
158
159
160
161Linux                             2017-09-15                      GETRANDOM(2)
Impressum