| 1 |
#define _BSD_SOURCE |
| 2 |
#include <unistd.h> |
| 3 |
#include <sys/random.h> |
| 4 |
#include <pthread.h> |
| 5 |
#include <errno.h> |
| 6 |
|
| 7 |
int getentropy(void *buffer, size_t len) |
| 8 |
{ |
| 9 |
int cs, ret = 0; |
| 10 |
char *pos = buffer; |
| 11 |
|
| 12 |
if (len > 256) { |
| 13 |
errno = EIO; |
| 14 |
return -1; |
| 15 |
} |
| 16 |
|
| 17 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
| 18 |
|
| 19 |
while (len) { |
| 20 |
ret = getrandom(pos, len, 0); |
| 21 |
if (ret < 0) { |
| 22 |
if (errno == EINTR) continue; |
| 23 |
else break; |
| 24 |
} |
| 25 |
pos += ret; |
| 26 |
len -= ret; |
| 27 |
ret = 0; |
| 28 |
} |
| 29 |
|
| 30 |
pthread_setcancelstate(cs, 0); |
| 31 |
|
| 32 |
return ret; |
| 33 |
} |