mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-24 07:26:51 +03:00
More robust read loop
This commit is contained in:
parent
f1b44b42a9
commit
30390fa180
35
ent.c
35
ent.c
@ -1,27 +1,38 @@
|
|||||||
#include <ent/ent.h>
|
#include <ent/ent.h>
|
||||||
|
|
||||||
#if defined(ENT_USE_URANDOM)
|
#if defined(ENT_URANDOM)
|
||||||
# include <assert.h>
|
|
||||||
# include <fcntl.h>
|
#include <assert.h>
|
||||||
# include <sys/types.h>
|
#include <fcntl.h>
|
||||||
# include <unistd.h>
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
ent_getentropy(void* buf, size_t buflen)
|
ent_getentropy(void* buf, size_t len)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
assert(buflen <= 256);
|
assert(len <= 256);
|
||||||
if (-1 == (fd = open("/dev/urandom", O_RDONLY))) {
|
if (-1 == (fd = open("/dev/urandom", O_RDONLY))) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (buflen != read(fd, buf, buflen)) {
|
while (len != 0 && (ret = read(fd, buf, len)) != 0) {
|
||||||
(void) close(fd);
|
if (ret == -1) {
|
||||||
return -1;
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
len -= ret;
|
||||||
|
buf += ret;
|
||||||
}
|
}
|
||||||
close(fd);
|
(void) close(fd);
|
||||||
return 0;
|
if (ret == 0) {
|
||||||
|
ret = -1;
|
||||||
|
errno = EIO;
|
||||||
|
}
|
||||||
|
return ret < 0 ? ret : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* ENT_USE_URANDOM */
|
#endif /* ENT_USE_URANDOM */
|
||||||
|
Loading…
Reference in New Issue
Block a user