diff options
author | Mattias Andrée <maandree@kth.se> | 2021-12-10 21:04:15 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-12-10 21:04:15 +0100 |
commit | c660f6d13f4febd59a5ba5ad4f008d0dd78e0ecd (patch) | |
tree | bde9e10c2fa6e6c6d26e7a69b3657cade09091bc | |
parent | Add lseek (diff) | |
download | liberror-libc-c660f6d13f4febd59a5ba5ad4f008d0dd78e0ecd.tar.gz liberror-libc-c660f6d13f4febd59a5ba5ad4f008d0dd78e0ecd.tar.bz2 liberror-libc-c660f6d13f4febd59a5ba5ad4f008d0dd78e0ecd.tar.xz |
Add liberror_send_require and liberror_send_short
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | liberror-libc.h | 3 | ||||
-rw-r--r-- | send.c | 39 |
2 files changed, 42 insertions, 0 deletions
diff --git a/liberror-libc.h b/liberror-libc.h index ffdc127..8de6025 100644 --- a/liberror-libc.h +++ b/liberror-libc.h @@ -9,6 +9,7 @@ #define LIBERROR_LIBC_ERRROR_SHORT_READ -1 #define LIBERROR_LIBC_ERRROR_SHORT_SEEK -2 +#define LIBERROR_LIBC_ERRROR_SHORT_WRITE -3 typedef int64_t _liberror_off_t; @@ -49,7 +50,9 @@ void liberror_recv_failed(int, void *, size_t, int, const char *); void liberror_recv_short(int, void *, size_t, int, size_t min, size_t max, ssize_t returned, const char *); /* void *liberror_posix_memalign(void **, size_t, size_t); TODO */ ssize_t liberror_send(int, const void *, size_t, int, const char *); +ssize_t liberror_send_require(int, const void *, size_t, int, size_t min, size_t max, const char *); void liberror_send_failed(int, const void *, size_t, int, const char *); +void liberror_send_short(int, const void *, size_t, int, size_t min, size_t max, ssize_t returned, const char *); int liberror_setenv(const char *, const char *, int); int liberror_shutdown(int, int, const char *); void liberror_shutdown_failed(int, int, const char *); @@ -145,6 +145,30 @@ liberror_send_failed(int fd, const void *buf, size_t n, int flags, const char *f } +void +liberror_send_short(int fd, const void *buf, size_t n, int flags, size_t min, size_t max, ssize_t returned, const char *fname) +{ + const char *desc; + struct liberror_error *error; + int saved_errno = errno; + liberror_save_backtrace(NULL); + if (returned < 0 || (size_t)returned < min) { + desc = "Sent message was shorter than expected"; + } else if (returned > max) { + desc = "Sent message was larger than expected"; + } else { + desc = "Sent message was of an unexpected size"; + } + liberror_set_error(desc, "send", "liberror-libc", LIBERROR_LIBC_ERRROR_SHORT_WRITE); + error = liberror_get_error(); + error->details_type = LIBERROR_DETAILS_ONE_FILE; + error->details.one_file.fd = fd; + error->details.one_file.name = fname ? strdup(fname) : NULL; + error->details.one_file.role = "Socket file"; + errno = saved_errno; +} + + ssize_t liberror_send(int fd, const void *buf, size_t n, int flags, const char *fname) { @@ -155,3 +179,18 @@ liberror_send(int fd, const void *buf, size_t n, int flags, const char *fname) liberror_send_failed(fd, buf, n, flags, fname); return -1; } + + +ssize_t +liberror_send_require(int fd, const void *buf, size_t n, int flags, size_t min, size_t max, const char *fname) +{ + ssize_t r = send(fd, buf, n, flags); + if (r >= 0 && (size_t)r >= min && (size_t)r <= max) + return r; + liberror_save_backtrace(NULL); + if (r < 0) + liberror_send_failed(fd, buf, n, flags, fname); + else + liberror_send_short(fd, buf, n, flags, min, max, r, fname); + return -1; +} |