Retry if open(2) is interrupted; set the CLOEXEC flag as well.

Also retry if read(2) returns EAGAIN. This shouldn't happen in blocking mode,
but it can't hurt either.
This commit is contained in:
Frank Denis
2014-12-25 12:30:14 +01:00
parent 4c4d04bb2a
commit d5ad99fed6
2 changed files with 18 additions and 4 deletions
@@ -94,7 +94,7 @@ safe_read(const int fd, void * const buf_, size_t count)
assert(count > (size_t) 0U);
do {
while ((readnb = read(fd, buf, count)) < (ssize_t) 0 &&
errno == EINTR); /* LCOV_EXCL_LINE */
(errno == EINTR || errno == EAGAIN)); /* LCOV_EXCL_LINE */
if (readnb < (ssize_t) 0) {
return readnb; /* LCOV_EXCL_LINE */
}
@@ -125,15 +125,22 @@ randombytes_salsa20_random_random_dev_open(void)
int fd;
do {
if ((fd = open(*device, O_RDONLY)) != -1) {
fd = open(*device, O_RDONLY);
if (fd != -1) {
if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
# if defined(F_SETFD) && defined(FD_CLOEXEC)
(void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
# endif
return fd;
}
(void) close(fd);
} else if (errno == EINTR) {
continue;
}
device++;
} while (*device != NULL);
errno = EIO;
return -1;
/* LCOV_EXCL_STOP */
}
@@ -83,7 +83,7 @@ safe_read(const int fd, void * const buf_, size_t count)
assert(count > (size_t) 0U);
do {
while ((readnb = read(fd, buf, count)) < (ssize_t) 0 &&
errno == EINTR); /* LCOV_EXCL_LINE */
(errno == EINTR || errno == EAGAIN)); /* LCOV_EXCL_LINE */
if (readnb < (ssize_t) 0) {
return readnb; /* LCOV_EXCL_LINE */
}
@@ -114,15 +114,22 @@ randombytes_sysrandom_random_dev_open(void)
int fd;
do {
if ((fd = open(*device, O_RDONLY)) != -1) {
fd = open(*device, O_RDONLY);
if (fd != -1) {
if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) {
# if defined(F_SETFD) && defined(FD_CLOEXEC)
(void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
# endif
return fd;
}
(void) close(fd);
} else if (errno == EINTR) {
continue;
}
device++;
} while (*device != NULL);
errno = EIO;
return -1;
/* LCOV_EXCL_STOP */
}