summaryrefslogtreecommitdiff
path: root/comm.c
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2025-03-15 01:12:50 +0100
committerSimon Ser <contact@emersion.fr>2025-03-15 01:12:50 +0100
commite0ca5adecd0b0f89f20322c9b410b460e8f73c5b (patch)
tree1a079864dd64297dee9d35f39ad8f59c2624b569 /comm.c
parent285d0292914bf6361a1a32ce1561a6d3fb6ebeca (diff)
Revert "comm: improve robustness"
This reverts commit 285d0292914bf6361a1a32ce1561a6d3fb6ebeca. This got accidentally pushed to master.
Diffstat (limited to 'comm.c')
-rw-r--r--comm.c99
1 files changed, 40 insertions, 59 deletions
diff --git a/comm.c b/comm.c
index c081d87..28e0504 100644
--- a/comm.c
+++ b/comm.c
@@ -1,4 +1,3 @@
-#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -10,68 +9,41 @@
static int comm[2][2] = {{-1, -1}, {-1, -1}};
-static ssize_t read_full(int fd, void *dst, size_t size) {
- char *buf = dst;
- size_t offset = 0;
- while (offset < size) {
- ssize_t n = read(fd, &buf[offset], size - offset);
- if (n < 0) {
- swaylock_log_errno(LOG_ERROR, "read() failed");
- return -1;
- } else if (n == 0) {
- if (offset == 0) {
- return 0;
- }
- swaylock_log(LOG_ERROR, "read() failed: unexpected EOF");
- return -1;
- }
- offset += n;
- }
- return offset;
-}
-
-static bool write_full(int fd, const void *src, size_t size) {
- const char *buf = src;
- size_t offset = 0;
- while (offset < size) {
- ssize_t n = write(fd, &buf[offset], size - offset);
- if (n <= 0) {
- assert(n != 0);
- swaylock_log_errno(LOG_ERROR, "write() failed");
- return false;
- }
- offset += n;
- }
- return true;
-}
-
ssize_t read_comm_request(char **buf_ptr) {
- int fd = comm[0][0];
-
size_t size;
- ssize_t n = read_full(fd, &size, sizeof(size));
- if (n <= 0) {
- return n;
+ ssize_t amt;
+ amt = read(comm[0][0], &size, sizeof(size));
+ if (amt == 0) {
+ return 0;
+ } else if (amt < 0) {
+ swaylock_log_errno(LOG_ERROR, "read pw request");
+ return -1;
}
-
swaylock_log(LOG_DEBUG, "received pw check request");
-
char *buf = password_buffer_create(size);
if (!buf) {
return -1;
}
-
- if (read_full(fd, buf, size) <= 0) {
- swaylock_log_errno(LOG_ERROR, "failed to read pw");
- return -1;
- }
+ size_t offs = 0;
+ do {
+ amt = read(comm[0][0], &buf[offs], size - offs);
+ if (amt <= 0) {
+ swaylock_log_errno(LOG_ERROR, "failed to read pw");
+ return -1;
+ }
+ offs += (size_t)amt;
+ } while (offs < size);
*buf_ptr = buf;
return size;
}
bool write_comm_reply(bool success) {
- return write_full(comm[1][1], &success, sizeof(success));
+ if (write(comm[1][1], &success, sizeof(success)) != sizeof(success)) {
+ swaylock_log_errno(LOG_ERROR, "failed to write pw check result");
+ return false;
+ }
+ return true;
}
bool spawn_comm_child(void) {
@@ -99,18 +71,22 @@ bool spawn_comm_child(void) {
bool write_comm_request(struct swaylock_password *pw) {
bool result = false;
- int fd = comm[0][1];
- size_t size = pw->len + 1;
- if (!write_full(fd, &size, sizeof(size))) {
- swaylock_log_errno(LOG_ERROR, "Failed to write pw size");
+ size_t len = pw->len + 1;
+ size_t offs = 0;
+ if (write(comm[0][1], &len, sizeof(len)) < 0) {
+ swaylock_log_errno(LOG_ERROR, "Failed to request pw check");
goto out;
}
- if (!write_full(fd, pw->buffer, size)) {
- swaylock_log_errno(LOG_ERROR, "Failed to write pw buffer");
- goto out;
- }
+ do {
+ ssize_t amt = write(comm[0][1], &pw->buffer[offs], len - offs);
+ if (amt < 0) {
+ swaylock_log_errno(LOG_ERROR, "Failed to write pw buffer");
+ goto out;
+ }
+ offs += amt;
+ } while (offs < len);
result = true;
@@ -120,8 +96,13 @@ out:
}
bool read_comm_reply(bool *auth_success) {
- if (read_full(comm[1][0], auth_success, sizeof(*auth_success)) <= 0) {
- swaylock_log(LOG_ERROR, "Failed to read pw result");
+ ssize_t n = read(comm[1][0], auth_success, sizeof(*auth_success));
+ if (n != sizeof(*auth_success)) {
+ if (n < 0) {
+ swaylock_log_errno(LOG_ERROR, "Failed to read pw result");
+ } else {
+ swaylock_log(LOG_ERROR, "Failed to read pw result: short read of %zd bytes", n);
+ }
return false;
}
return true;