summaryrefslogtreecommitdiff
path: root/usr/src/man/man2/getrandom.2
blob: 9ced3a3cb718a37ba88b25acac717ea15415da91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
.\"
.\" This file and its contents are supplied under the terms of the
.\" Common Development and Distribution License ("CDDL"), version 1.0.
.\" You may only use this file in accordance with the terms of version
.\" 1.0 of the CDDL.
.\"
.\" A full copy of the text of the CDDL should have accompanied this
.\" source.  A copy of the CDDL is also available via the Internet at
.\" http://www.illumos.org/license/CDDL.
.\"
.\"
.\" Copyright 2018 Joyent, Inc.
.\"
.Dd "November 6, 2018"
.Dt GETRANDOM 2
.Os
.Sh NAME
.Nm getrandom
.Nd get random numbers
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/random.h
.Ft ssize_t
.Fo getrandom
.Fa "void *bufp"
.Fa "size_t buflen"
.Fa "unsigned int flags"
.Fc
.Sh DESCRIPTION
The
.Fn getrandom
function is used to retrieve random and pseudo-random numbers from the
operating system.
.Pp
By default, the
.Fn getrandom
function will read up to
.Fa buflen
bytes of pseudo-random data into
.Fa bufp .
Pseudo-random data will be retrieved from the same source that provides
data to
.Pa /dev/urandom .
The
.Fn getrandom
function may return less data than was requested in
.Fa buflen .
This can happen because of interrupts from signals, availability of
data, or because the request was too large.
Callers must always check to see how much data was actually returned.
.Pp
The following values may be bitwise-ORed together in the
.Fa flags
argument to modify the behavior of the function:
.Bl -tag -width Dv
.It Dv GRND_NONBLOCK
Instead of blocking, return immediately if data is not available.
If no data was obtained,
.Er EAGAIN
will be set in
.Va errno .
Otherwise, less data will be returned than requested.
.It Dv GRND_RANDOM
Use the same source of random data as reading from
.Pa /dev/random ,
instead of
.Pa /dev/urandom .
.El
.Pp
The
.Fn getrandom
function is intended to eliminate the need to explicitly call
.Xr open 2
and
.Xr read 2
on
.Pa /dev/random
or
.Pa /dev/urandom .
This eliminates the need to have the character devices available or
cases where a program may not have an available file descriptor.
For other uses,
.Xr arc4random 3C
may be a better interface.
.Sh RETURN VALUES
Upon successful completion, the
.Fn getrandom
function returns the number of bytes written into
.Fa bufp .
Otherwise,
.Sy -1
is returned and
.Va errno
is set to indicate the error.
.Sh ERRORS
The
.Fn getrandom
function will fail if:
.Bl -tag -width Er
.It Er EAGAIN
The
.Fn getrandom
function would have blocked and
.Dv GRND_NONBLOCK
flag was set.
.It Er EFAULT
The
.Fa bufp
argument points to an illegal address.
.It Er EINAVL
An invalid value was passed in
.Fa flags .
.It Er EINTR
A signal was caught during the operation and no data was transferred.
.It Er EIO
An internal error occurred with the corresponding
.Xr random 7D
device.
.El
.Sh INTERFACE STABILITY
.Sy Committed
.Sh MT-LEVEL
.Sy MT-Safe
.Sh SEE ALSO
.Xr open 2 ,
.Xr read 2 ,
.Xr arc4random 3C ,
.Xr random 7D
.Sh STANDARDS
The
.Fn getrandom
function is non-standard.
It originally appeared in Linux.