lib/proxy: fix error code propagation

This commit is contained in:
Philippe Gerum 2019-11-13 11:45:34 +01:00
parent d9f26b70b0
commit d04d98df91
4 changed files with 35 additions and 14 deletions

View File

@ -16,9 +16,15 @@ char fmt_buf[1024];
int evl_vprint_proxy(int proxyfd, const char *fmt, va_list ap)
{
ssize_t len = vsnprintf(fmt_buf, sizeof(fmt_buf), fmt, ap);
ssize_t count, ret;
return write(proxyfd, fmt_buf, len);
count = vsnprintf(fmt_buf, sizeof(fmt_buf), fmt, ap);
if (count < 0)
return -errno; /* Assume POSIX beahvior. */
ret = write(proxyfd, fmt_buf, count);
return ret < 0 ? -errno : ret;
}
int evl_print_proxy(int proxyfd, const char *fmt, ...)

View File

@ -9,6 +9,7 @@
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <evl/uapi.h>
@ -25,9 +26,10 @@ int evl_new_proxy(int fd, size_t bufsz, size_t granularity,
}
static inline
ssize_t evl_send_proxy(int proxyfd, const void *buf, size_t len)
ssize_t evl_send_proxy(int proxyfd, const void *buf, size_t count)
{
return write(proxyfd, buf, len);
ssize_t ret = write(proxyfd, buf, count);
return ret < 0 ? -errno : ret;
}
int evl_vprint_proxy(int proxyfd,
@ -36,7 +38,11 @@ int evl_vprint_proxy(int proxyfd,
int evl_print_proxy(int proxyfd,
const char *fmt, ...);
#define evl_printf(__fmt, __args...) printf(__fmt, ##__args)
#define evl_printf(__fmt, __args...) \
({ \
int __ret = printf(__fmt, ##__args); \
__ret < 0 ? -errno : __ret; \
})
#define proxy_outfd fileno(stdout)
#define proxy_errfd fileno(stderr)

View File

@ -21,7 +21,7 @@ int evl_new_proxy(int targetfd, size_t bufsz,
const char *fmt, ...);
ssize_t evl_send_proxy(int proxyfd,
const void *buf, size_t len);
const void *buf, size_t count);
int evl_vprint_proxy(int proxyfd,
const char *fmt, va_list ap);

View File

@ -57,19 +57,26 @@ int evl_new_proxy(int targetfd, size_t bufsz, size_t granularity,
return efd;
}
ssize_t evl_send_proxy(int proxyfd, const void *buf, size_t len)
ssize_t evl_send_proxy(int proxyfd, const void *buf, size_t count)
{
if (evl_is_inband())
return write(proxyfd, buf, len);
ssize_t ret;
return oob_write(proxyfd, buf, len);
if (evl_is_inband())
ret = write(proxyfd, buf, count);
else
ret = oob_write(proxyfd, buf, count);
return ret < 0 ? -errno : ret;
}
int evl_vprint_proxy(int proxyfd, const char *fmt, va_list ap)
{
ssize_t len = vsnprintf(fmt_buf, sizeof(fmt_buf), fmt, ap);
ssize_t count = vsnprintf(fmt_buf, sizeof(fmt_buf), fmt, ap);
return evl_send_proxy(proxyfd, fmt_buf, len);
if (count < 0)
return -errno; /* assume POSIX behavior. */
return (int)evl_send_proxy(proxyfd, fmt_buf, count);
}
int evl_print_proxy(int proxyfd, const char *fmt, ...)
@ -95,9 +102,11 @@ int evl_printf(const char *fmt, ...)
* If evl_init() has not run yet, we can resort to vprintf()
* since latency is not an issue.
*/
if (evl_ctlfd < 0)
if (evl_ctlfd < 0) {
ret = vprintf(fmt, ap);
else
if (ret < 0)
ret = -errno;
} else
ret = evl_vprint_proxy(proxy_outfd, fmt, ap);
va_end(ap);