Improve error handling on logging calls

pull/13/head
Daniel Roethlisberger 10 years ago
parent 98520c8091
commit 65f56f634d

49
log.c

@ -223,15 +223,15 @@ log_connect_close(void)
* This is guaranteed by the current pxythr architecture.
*/
#define PREPFLAG_OUTBOUND 1
#define PREPFLAG_REQUEST 1
struct log_content_ctx {
unsigned int open : 1;
int fd;
union {
struct {
char *header_in;
char *header_out;
char *header_req;
char *header_resp;
} file;
struct {
char *filename;
@ -448,13 +448,13 @@ log_content_open(log_content_ctx_t **pctx, opts_t *opts,
} else {
/* single-file content log (-L) */
ctx->fd = content_fd;
if (asprintf(&ctx->u.file.header_in, "%s -> %s",
if (asprintf(&ctx->u.file.header_req, "%s -> %s",
srcaddr, dstaddr) < 0) {
goto errout;
}
if (asprintf(&ctx->u.file.header_out, "%s -> %s",
if (asprintf(&ctx->u.file.header_resp, "%s -> %s",
dstaddr, srcaddr) < 0) {
free(ctx->u.file.header_in);
free(ctx->u.file.header_req);
goto errout;
}
}
@ -470,28 +470,33 @@ errout:
return -1;
}
void
log_content_submit(log_content_ctx_t *ctx, logbuf_t *lb, int is_outbound)
int
log_content_submit(log_content_ctx_t *ctx, logbuf_t *lb, int is_request)
{
unsigned long prepflags = 0;
if (!ctx->open) {
log_err_printf("log_content_submit called on closed ctx\n");
return;
return -1;
}
if (is_outbound)
prepflags |= PREPFLAG_OUTBOUND;
logger_submit(content_log, ctx, prepflags, lb);
if (is_request)
prepflags |= PREPFLAG_REQUEST;
return logger_submit(content_log, ctx, prepflags, lb);
}
void
int
log_content_close(log_content_ctx_t **pctx)
{
int rv = 0;
if (!(*pctx)->open)
return;
logger_close(content_log, *pctx);
return -1;
if (logger_close(content_log, *pctx) == -1) {
rv = -1;
}
*pctx = NULL;
return rv;
}
/*
@ -599,11 +604,11 @@ log_content_file_closecb(void *fh)
{
log_content_ctx_t *ctx = fh;
if (ctx->u.file.header_in) {
free(ctx->u.file.header_in);
if (ctx->u.file.header_req) {
free(ctx->u.file.header_req);
}
if (ctx->u.file.header_out) {
free(ctx->u.file.header_out);
if (ctx->u.file.header_resp) {
free(ctx->u.file.header_resp);
}
free(ctx);
@ -613,14 +618,14 @@ static logbuf_t *
log_content_file_prepcb(void *fh, unsigned long prepflags, logbuf_t *lb)
{
log_content_ctx_t *ctx = fh;
int is_outbound = !!(prepflags & PREPFLAG_OUTBOUND);
int is_request = !!(prepflags & PREPFLAG_REQUEST);
logbuf_t *head;
time_t epoch;
struct tm *utc;
char *header;
if (!(header = is_outbound ? ctx->u.file.header_out
: ctx->u.file.header_in))
if (!(header = is_request ? ctx->u.file.header_req
: ctx->u.file.header_resp))
goto out;
/* prepend size tag and newline */

@ -60,8 +60,9 @@ extern logger_t *connect_log;
typedef struct log_content_ctx log_content_ctx_t;
int log_content_open(log_content_ctx_t **, opts_t *, char *, char *,
char *, char *, char *) NONNULL(1,2,3) WUNRES;
void log_content_submit(log_content_ctx_t *, logbuf_t *, int) NONNULL(1,2);
void log_content_close(log_content_ctx_t **) NONNULL(1);
int log_content_submit(log_content_ctx_t *, logbuf_t *, int)
NONNULL(1,2) WUNRES;
int log_content_close(log_content_ctx_t **) NONNULL(1) WUNRES;
int log_preinit(opts_t *) NONNULL(1) WUNRES;
int log_init(opts_t *) NONNULL(1) WUNRES;

@ -47,22 +47,22 @@ logger_t * logger_new(logger_open_func_t, logger_close_func_t,
void logger_free(logger_t *) NONNULL(1);
int logger_start(logger_t *) NONNULL(1) WUNRES;
void logger_leave(logger_t *) NONNULL(1);
int logger_join(logger_t *) NONNULL(1) /*WUNRES*/;
int logger_stop(logger_t *) NONNULL(1) /*WUNRES*/;
int logger_join(logger_t *) NONNULL(1);
int logger_stop(logger_t *) NONNULL(1) WUNRES;
int logger_open(logger_t *, void *) NONNULL(1,2) WUNRES;
int logger_close(logger_t *, void *) NONNULL(1,2) /*WUNRES*/;
int logger_close(logger_t *, void *) NONNULL(1,2) WUNRES;
int logger_submit(logger_t *, void *, unsigned long,
logbuf_t *) NONNULL(1,4) /*WUNRES*/;
logbuf_t *) NONNULL(1,4) WUNRES;
int logger_printf(logger_t *, void *, unsigned long,
const char *, ...) PRINTF(4,5) NONNULL(1,4) /*WUNRES*/;
const char *, ...) PRINTF(4,5) NONNULL(1,4) WUNRES;
int logger_print(logger_t *, void *, unsigned long,
const char *) NONNULL(1,4) /*WUNRES*/;
const char *) NONNULL(1,4) WUNRES;
int logger_write(logger_t *, void *, unsigned long,
const void *, size_t) NONNULL(1,4) /*WUNRES*/;
const void *, size_t) NONNULL(1,4) WUNRES;
int logger_print_freebuf(logger_t *, void *, unsigned long,
char *) NONNULL(1,4) /*WUNRES*/;
char *) NONNULL(1,4) WUNRES;
int logger_write_freebuf(logger_t *, void *, unsigned long,
void *, size_t) NONNULL(1,4) /*WUNRES*/;
void *, size_t) NONNULL(1,4) WUNRES;
#endif /* !LOGGER_H */

@ -272,7 +272,9 @@ pxy_conn_ctx_free(pxy_conn_ctx_t *ctx)
free(ctx->sni);
}
if (WANT_CONTENT_LOG(ctx)) {
log_content_close(&ctx->logctx);
if (log_content_close(&ctx->logctx) == -1) {
log_err_printf("Warning: Content log close failed\n");
}
}
free(ctx);
}
@ -397,7 +399,10 @@ pxy_log_connect_nonhttp(pxy_conn_ctx_t *ctx)
log_err_printf("%s", msg);
}
if (ctx->opts->connectlog) {
log_connect_print_free(msg);
if (log_connect_print_free(msg) == -1) {
free(msg);
log_err_printf("Warning: Connection logging failed\n");
}
} else {
free(msg);
}
@ -492,7 +497,10 @@ pxy_log_connect_http(pxy_conn_ctx_t *ctx)
log_err_printf("%s", msg);
}
if (ctx->opts->connectlog) {
log_connect_print_free(msg);
if (log_connect_print_free(msg) == -1) {
free(msg);
log_err_printf("Warning: Connection logging failed\n");
}
} else {
free(msg);
}
@ -1380,7 +1388,12 @@ deny:
NULL, NULL);
if (lb &&
(evbuffer_copyout(inbuf, lb->buf, lb->sz) != -1)) {
log_content_submit(ctx->logctx, lb, 0);
if (log_content_submit(ctx->logctx, lb,
1/*req*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
}
evbuffer_drain(inbuf, evbuffer_get_length(inbuf));
@ -1394,7 +1407,12 @@ deny:
lb = logbuf_new_copy(ocspresp, sizeof(ocspresp) - 1,
NULL, NULL);
if (lb) {
log_content_submit(ctx->logctx, lb, 1);
if (log_content_submit(ctx->logctx, lb,
0/*resp*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
}
}
@ -1484,7 +1502,12 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
}
}
if (lb && WANT_CONTENT_LOG(ctx)) {
log_content_submit(ctx->logctx, lb, 0);
if (log_content_submit(ctx->logctx, lb,
1/*req*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
if (!ctx->seen_req_header)
return;
@ -1527,7 +1550,12 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
}
}
if (lb && WANT_CONTENT_LOG(ctx)) {
log_content_submit(ctx->logctx, lb, 0);
if (log_content_submit(ctx->logctx, lb,
0/*resp*/) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
if (!ctx->seen_resp_header)
return;
@ -1547,8 +1575,12 @@ pxy_bev_readcb(struct bufferevent *bev, void *arg)
logbuf_t *lb;
lb = logbuf_new_alloc(evbuffer_get_length(inbuf), NULL, NULL);
if (lb && (evbuffer_copyout(inbuf, lb->buf, lb->sz) != -1)) {
log_content_submit(ctx->logctx, lb,
(bev != ctx->src.bev));
if (log_content_submit(ctx->logctx, lb,
(bev == ctx->src.bev)) == -1) {
logbuf_free(lb);
log_err_printf("Warning: Content log "
"submission failed\n");
}
}
}
evbuffer_add_buffer(outbuf, inbuf);

Loading…
Cancel
Save