From 9f9cbfb20cb7b78e8a00cec6451e7686343084fe Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 20 Nov 2011 15:14:36 +0100 Subject: [PATCH] Saner error handling in typed_wu() Leave nat_ann, nat_tgm alone and return -1 on all errors. Before, only failed open was handled that way. Failed write and close were logged and ignored. While there, improve the log messages a bit. Note: the return value fix has little effect. It makes tele() log the failure, which is redundant. Everything else goes through wu() and ignores the value. --- src/lib/subs/wu.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/lib/subs/wu.c b/src/lib/subs/wu.c index df6efc6eb..5021cd0c0 100644 --- a/src/lib/subs/wu.c +++ b/src/lib/subs/wu.c @@ -120,7 +120,6 @@ typed_wu(natid from, natid to, char *message, int type) struct iovec iov[2]; int fd; char box[1024]; - int write_ok = 0; int new_tele = 0; struct player *other; @@ -137,9 +136,10 @@ typed_wu(natid from, natid to, char *message, int type) #else if ((fd = open(box, O_WRONLY | O_APPEND | O_BINARY, 0)) < 0) { #endif - logerror("telegram 'open' of %s (#%d) failed", box, to); + logerror("telegram 'open' of %s failed", box); return -1; } + memset(&tel, 0, sizeof(tel)); tel.tel_from = from; (void)time(&tel.tel_date); @@ -151,13 +151,16 @@ typed_wu(natid from, natid to, char *message, int type) iov[1].iov_base = message; iov[1].iov_len = len; if (writev(fd, iov, 2) < (int)(iov[0].iov_len + iov[1].iov_len)) { - logerror("telegram 'write' to #%d failed", to); - } else - write_ok = 1; + logerror("telegram 'write' to %s failed", box); + close(fd); + return -1; + } + if (close(fd) < 0) { + logerror("telegram 'write' to %s failed to close.", box); + return -1; + } - if (close(fd) == -1) { - logerror("telegram 'write' to #%d failed to close.", to); - } else if (write_ok && type == TEL_ANNOUNCE) { + if (type == TEL_ANNOUNCE) { for (to = 0; NULL != (np = getnatp(to)); to++) { if (np->nat_stat < STAT_SANCT) continue; @@ -166,7 +169,7 @@ typed_wu(natid from, natid to, char *message, int type) np->nat_ann++; putnat(np); } - } else if (write_ok) { + } else { new_tele = telegram_is_new(to, &tel); np->nat_tgms += new_tele || np->nat_tgms == 0; putnat(np); -- 2.43.0