@@ -135,24 +135,23 @@ static int randombytes_linux_wait_for_entropy(int device)
135
135
{
136
136
/* We will block on /dev/random, because any increase in the OS' entropy
137
137
* level will unblock the request. I use poll here (as does libsodium),
138
- * because we don't *actually* want to read from the device. */
138
+ * because we don't *actually* want to read from the device.
139
+ */
139
140
enum { IOCTL , PROC } strategy = IOCTL ;
140
141
const int bits = 128 ;
141
- struct pollfd pfd ;
142
- int fd ;
143
142
FILE * proc_file ;
144
- int retcode , retcode_error = 0 ; /* Used as return codes */
143
+ int retcode_error = 0 ; /* Used as return codes */
145
144
int entropy = 0 ;
146
145
147
146
/* If the device has enough entropy already, we will want to return early */
148
- retcode = randombytes_linux_read_entropy_ioctl (device , & entropy );
147
+ int retcode = randombytes_linux_read_entropy_ioctl (device , & entropy );
149
148
if (retcode != 0 && (errno == ENOTTY || errno == ENOSYS )) {
150
149
/* The ioctl call on /dev/urandom has failed due to a
151
150
* - ENOTTY (unsupported action), or
152
- * - ENOSYS (invalid ioctl; this happens on MIPS, see #22 ).
151
+ * - ENOSYS (invalid ioctl; this happens on MIPS).
153
152
*
154
153
* We will fall back to reading from
155
- * ` /proc/sys/kernel/random/entropy_avail`. This less ideal,
154
+ * /proc/sys/kernel/random/entropy_avail . This is less ideal,
156
155
* because it allocates a file descriptor, and it may not work
157
156
* in a chroot. But at this point it seems we have no better
158
157
* options left.
@@ -169,6 +168,7 @@ static int randombytes_linux_wait_for_entropy(int device)
169
168
if (entropy >= bits )
170
169
return 0 ;
171
170
171
+ int fd ;
172
172
do {
173
173
fd = open ("/dev/random" , O_RDONLY );
174
174
} while (fd == -1 && errno == EINTR ); /* EAGAIN will not occur */
@@ -177,8 +177,7 @@ static int randombytes_linux_wait_for_entropy(int device)
177
177
return -1 ;
178
178
}
179
179
180
- pfd .fd = fd ;
181
- pfd .events = POLLIN ;
180
+ struct pollfd pfd = {.fd = fd , .events = POLLIN };
182
181
for (;;) {
183
182
retcode = poll (& pfd , 1 , -1 );
184
183
if (retcode == -1 && (errno == EINTR || errno == EAGAIN )) {
@@ -224,8 +223,6 @@ static int randombytes_linux_wait_for_entropy(int device)
224
223
static int randombytes_linux_randombytes_urandom (void * buf , size_t n )
225
224
{
226
225
int fd ;
227
- size_t offset = 0 , count ;
228
- ssize_t tmp ;
229
226
do {
230
227
fd = open ("/dev/urandom" , O_RDONLY );
231
228
} while (fd == -1 && errno == EINTR );
@@ -236,12 +233,12 @@ static int randombytes_linux_randombytes_urandom(void *buf, size_t n)
236
233
return -1 ;
237
234
#endif
238
235
236
+ size_t offset = 0 ;
239
237
while (n > 0 ) {
240
- count = n <= SSIZE_MAX ? n : SSIZE_MAX ;
241
- tmp = read (fd , (char * ) buf + offset , count );
242
- if (tmp == -1 && (errno == EAGAIN || errno == EINTR )) {
238
+ size_t count = n <= SSIZE_MAX ? n : SSIZE_MAX ;
239
+ ssize_t tmp = read (fd , (char * ) buf + offset , count );
240
+ if (tmp == -1 && (errno == EAGAIN || errno == EINTR ))
243
241
continue ;
244
- }
245
242
if (tmp == -1 )
246
243
return -1 ; /* Unrecoverable IO error */
247
244
offset += tmp ;
0 commit comments