summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-03-09 21:23:53 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2015-03-09 22:10:54 -0400
commita6dcc7e5924f9c27d3e9c6560a448b02ec28b65f (patch)
tree694aa3c7b5b2f4e61fb8c25d5bd8d1f023cf511e /src/shared
parentad7bcf526d5ec54838bc9411a0e09a293845a015 (diff)
Introduce loop_read_exact helper
Usually when using loop_read(), we want to read the full buffer. Add a helper that mirrors loop_write(), and returns 0 when full buffer was read, and an error otherwise. Use -ENODATA for the short read, to distinguish it from a read error.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/logs-show.c4
-rw-r--r--src/shared/util.c28
-rw-r--r--src/shared/util.h1
3 files changed, 19 insertions, 14 deletions
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index 944d6856cd..d3ee139880 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -1163,9 +1163,9 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
if (fd < 0)
_exit(EXIT_FAILURE);
- k = loop_read(fd, buf, 36, false);
+ r = loop_read_exact(fd, buf, 36, false);
safe_close(fd);
- if (k != 36)
+ if (k < 0)
_exit(EXIT_FAILURE);
k = send(pair[1], buf, 36, MSG_NOSIGNAL);
diff --git a/src/shared/util.c b/src/shared/util.c
index 74f29949c9..a13819e79a 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2326,6 +2326,17 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
return n;
}
+int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
+ ssize_t n;
+
+ n = loop_read(fd, buf, nbytes, do_poll);
+ if (n < 0)
+ return n;
+ if ((size_t) n != nbytes)
+ return -EIO;
+ return 0;
+}
+
int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
const uint8_t *p = buf;
@@ -2580,8 +2591,9 @@ char* dirname_malloc(const char *path) {
int dev_urandom(void *p, size_t n) {
static int have_syscall = -1;
- int r, fd;
- ssize_t k;
+
+ _cleanup_close_ fd = -1;
+ int r;
/* Gathers some randomness from the kernel. This call will
* never block, and will always return some data from the
@@ -2616,22 +2628,14 @@ int dev_urandom(void *p, size_t n) {
return -errno;
} else
/* too short read? */
- return -EIO;
+ return -ENODATA;
}
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return errno == ENOENT ? -ENOSYS : -errno;
- k = loop_read(fd, p, n, true);
- safe_close(fd);
-
- if (k < 0)
- return (int) k;
- if ((size_t) k != n)
- return -EIO;
-
- return 0;
+ return loop_read_exact(fd, p, n, true);
}
void initialize_srand(void) {
diff --git a/src/shared/util.h b/src/shared/util.h
index 2de654f4cc..d2da3e2895 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -432,6 +432,7 @@ int sigaction_many(const struct sigaction *sa, ...);
int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
+int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll);
int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
bool is_device_path(const char *path);