12345678910111213141516171819202122232425262728293031323334353637 |
- #define _BSD_SOURCE
- #include <unistd.h>
- #include <sys/random.h>
- #include <pthread.h>
- #include <errno.h>
- int getentropy(void *buffer, size_t len)
- {
- int cs, ret = 0;
- char *pos = buffer;
- if (len > 256) {
- errno = EIO;
- return -1;
- }
- #if defined(__linux__) && !defined(__ANDROID__)
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- #endif
- while (len) {
- ret = getrandom(pos, len, 0);
- if (ret < 0) {
- if (errno == EINTR) continue;
- else break;
- }
- pos += ret;
- len -= ret;
- ret = 0;
- }
- #if defined(__linux__) && !defined(__ANDROID__)
- pthread_setcancelstate(cs, 0);
- #endif
- return ret;
- }
|