summaryrefslogtreecommitdiff
path: root/src/core
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/core
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/core')
-rw-r--r--src/core/automount.c10
-rw-r--r--src/core/machine-id-setup.c15
2 files changed, 10 insertions, 15 deletions
diff --git a/src/core/automount.c b/src/core/automount.c
index 0539fbbc41..cec90cbb31 100644
--- a/src/core/automount.c
+++ b/src/core/automount.c
@@ -725,7 +725,6 @@ static bool automount_check_gc(Unit *u) {
static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
union autofs_v5_packet_union packet;
Automount *a = AUTOMOUNT(userdata);
- ssize_t l;
int r;
assert(a);
@@ -736,12 +735,9 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo
goto fail;
}
- l = loop_read(a->pipe_fd, &packet, sizeof(packet), true);
- if (l != sizeof(packet)) {
- if (l < 0)
- log_unit_error_errno(UNIT(a)->id, l, "Invalid read from pipe: %m");
- else
- log_unit_error(UNIT(a)->id, "Invalid read from pipe: short read");
+ r = loop_read_exact(a->pipe_fd, &packet, sizeof(packet), true);
+ if (r < 0) {
+ log_unit_error_errno(UNIT(a)->id, r, "Invalid read from pipe: %m");
goto fail;
}
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index 063f705ede..623dffdea1 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -64,7 +64,6 @@ static int generate(char id[34], const char *root) {
unsigned char *p;
sd_id128_t buf;
char *q;
- ssize_t k;
const char *vm_id, *dbus_machine_id;
assert(id);
@@ -77,11 +76,10 @@ static int generate(char id[34], const char *root) {
/* First, try reading the D-Bus machine id, unless it is a symlink */
fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
if (fd >= 0) {
- k = loop_read(fd, id, 33, false);
+ r = loop_read_exact(fd, id, 33, false);
safe_close(fd);
- if (k == 33 && id[32] == '\n') {
-
+ if (r >= 0 && id[32] == '\n') {
id[32] = 0;
if (id128_is_valid(id)) {
id[32] = '\n';
@@ -119,14 +117,14 @@ static int generate(char id[34], const char *root) {
r = detect_vm(&vm_id);
if (r > 0 && streq(vm_id, "kvm")) {
- char uuid[37];
+ char uuid[36];
fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
if (fd >= 0) {
- k = loop_read(fd, uuid, 36, false);
+ r = loop_read_exact(fd, uuid, 36, false);
safe_close(fd);
- if (k >= 36) {
+ if (r >= 0) {
r = shorten_uuid(id, uuid);
if (r >= 0) {
log_info("Initializing machine ID from KVM UUID.");
@@ -162,7 +160,8 @@ static int get_valid_machine_id(int fd, char id[34]) {
assert(fd >= 0);
assert(id);
- if (loop_read(fd, id_to_validate, 33, false) == 33 && id_to_validate[32] == '\n') {
+ if (loop_read_exact(fd, id_to_validate, 33, false) >= 0 &&
+ id_to_validate[32] == '\n') {
id_to_validate[32] = 0;
if (id128_is_valid(id_to_validate)) {