summaryrefslogtreecommitdiffstats
path: root/hunt
diff options
context:
space:
mode:
authordholland <dholland@NetBSD.org>2014-03-29 22:29:55 +0000
committerdholland <dholland@NetBSD.org>2014-03-29 22:29:55 +0000
commit01c156137af771211dcc8c7bedbe4121c12f22b8 (patch)
treea35c47e5d06a59a4ece1e325c567ade712583388 /hunt
parent18db53a64319b86ed429e171bc3a52f4b3bc74f2 (diff)
downloadbsdgames-darwin-01c156137af771211dcc8c7bedbe4121c12f22b8.tar.gz
bsdgames-darwin-01c156137af771211dcc8c7bedbe4121c12f22b8.tar.zst
bsdgames-darwin-01c156137af771211dcc8c7bedbe4121c12f22b8.zip
Reduce ifdefs by making a common function to talk to either syslogd or
stderr.
Diffstat (limited to 'hunt')
-rw-r--r--hunt/huntd/Makefile4
-rw-r--r--hunt/huntd/answer.c10
-rw-r--r--hunt/huntd/driver.c58
-rw-r--r--hunt/huntd/hunt.h8
-rw-r--r--hunt/huntd/support.c60
5 files changed, 80 insertions, 60 deletions
diff --git a/hunt/huntd/Makefile b/hunt/huntd/Makefile
index aff53079..0981256f 100644
--- a/hunt/huntd/Makefile
+++ b/hunt/huntd/Makefile
@@ -1,8 +1,8 @@
-# $NetBSD: Makefile,v 1.5 2014/03/29 21:27:40 dholland Exp $
+# $NetBSD: Makefile,v 1.6 2014/03/29 22:29:55 dholland Exp $
PROG= huntd
SRCS= answer.c draw.c driver.c execute.c expl.c \
- extern.c makemaze.c shots.c terminal.c
+ extern.c makemaze.c shots.c support.c terminal.c
MAN= huntd.6
CPPFLAGS+=-I${.CURDIR}/../include
diff --git a/hunt/huntd/answer.c b/hunt/huntd/answer.c
index da040f19..df3dd045 100644
--- a/hunt/huntd/answer.c
+++ b/hunt/huntd/answer.c
@@ -1,4 +1,4 @@
-/* $NetBSD: answer.c,v 1.17 2014/03/29 19:41:10 dholland Exp $ */
+/* $NetBSD: answer.c,v 1.18 2014/03/29 22:29:55 dholland Exp $ */
/*
* Copyright (c) 1983-2003, Regents of the University of California.
* All rights reserved.
@@ -32,7 +32,7 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: answer.c,v 1.17 2014/03/29 19:41:10 dholland Exp $");
+__RCSID("$NetBSD: answer.c,v 1.18 2014/03/29 22:29:55 dholland Exp $");
#endif /* not lint */
#include <ctype.h>
@@ -79,11 +79,7 @@ answer(void)
{
if (errno == EINTR)
return false;
-#ifdef LOG
- syslog(LOG_ERR, "accept: %m");
-#else
- perror("accept");
-#endif
+ complain(LOG_ERR, "accept");
cleanup(1);
}
diff --git a/hunt/huntd/driver.c b/hunt/huntd/driver.c
index 88b497fb..c46fcfee 100644
--- a/hunt/huntd/driver.c
+++ b/hunt/huntd/driver.c
@@ -1,4 +1,4 @@
-/* $NetBSD: driver.c,v 1.31 2014/03/29 22:11:19 dholland Exp $ */
+/* $NetBSD: driver.c,v 1.32 2014/03/29 22:29:55 dholland Exp $ */
/*
* Copyright (c) 1983-2003, Regents of the University of California.
* All rights reserved.
@@ -32,7 +32,7 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: driver.c,v 1.31 2014/03/29 22:11:19 dholland Exp $");
+__RCSID("$NetBSD: driver.c,v 1.32 2014/03/29 22:29:55 dholland Exp $");
#endif /* not lint */
#include <sys/ioctl.h>
@@ -130,11 +130,7 @@ again:
while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0)
{
if (errno != EINTR)
-#ifdef LOG
- syslog(LOG_WARNING, "poll: %m");
-#else
- warn("poll");
-#endif
+ complain(LOG_WARNING, "poll");
errno = 0;
}
#ifdef INTERNET
@@ -294,11 +290,7 @@ init(void)
if (errno == EADDRINUSE)
exit(0);
else {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
+ complain(LOG_ERR, "bind");
cleanup(1);
}
}
@@ -307,11 +299,7 @@ init(void)
#ifdef INTERNET
len = sizeof (SOCKET);
if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0) {
-#ifdef LOG
- syslog(LOG_ERR, "getsockname: %m");
-#else
- warn("getsockname");
-#endif
+ complain(LOG_ERR, "getsockname");
exit(1);
}
stat_port = ntohs(Daemon.sin_port);
@@ -333,21 +321,13 @@ init(void)
#if defined(INTERNET)
msg = 1;
if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
-#ifdef LOG
- syslog(LOG_WARNING, "setsockopt loopback %m");
-#else
- warn("setsockopt loopback");
-#endif
+ complain(LOG_WARNING, "setsockopt loopback");
#endif
if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
if (errno == EADDRINUSE)
exit(0);
else {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
+ complain(LOG_ERR, "bind");
cleanup(1);
}
}
@@ -356,11 +336,7 @@ init(void)
#ifdef INTERNET
len = sizeof (SOCKET);
if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0) {
-#ifdef LOG
- syslog(LOG_ERR, "getsockname: %m");
-#else
- warn("getsockname");
-#endif
+ complain(LOG_ERR, "getsockname");
exit(1);
}
sock_port = ntohs(Daemon.sin_port);
@@ -391,11 +367,7 @@ init(void)
Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
if (bind(Test_socket, (struct sockaddr *) &test_port,
DAEMON_SIZE) < 0) {
-#ifdef LOG
- syslog(LOG_ERR, "bind: %m");
-#else
- warn("bind");
-#endif
+ complain(LOG_ERR, "bind");
exit(1);
}
(void) listen(Test_socket, 5);
@@ -891,20 +863,12 @@ send_stats(void)
if (s < 0) {
if (errno == EINTR)
return;
-#ifdef LOG
- syslog(LOG_WARNING, "accept: %m");
-#else
- warn("accept");
-#endif
+ complain(LOG_WARNING, "accept");
return;
}
fp = fdopen(s, "w");
if (fp == NULL) {
-#ifdef LOG
- syslog(LOG_WARNING, "fdopen: %m");
-#else
- warn("fdopen");
-#endif
+ complain(LOG_WARNING, "fdopen");
(void) close(s);
return;
}
diff --git a/hunt/huntd/hunt.h b/hunt/huntd/hunt.h
index 71c33adb..b94cdc20 100644
--- a/hunt/huntd/hunt.h
+++ b/hunt/huntd/hunt.h
@@ -1,4 +1,4 @@
-/* $NetBSD: hunt.h,v 1.26 2014/03/29 21:55:59 dholland Exp $ */
+/* $NetBSD: hunt.h,v 1.27 2014/03/29 22:29:55 dholland Exp $ */
/*
* Copyright (c) 1983-2003, Regents of the University of California.
@@ -34,10 +34,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
-
-#ifdef LOG
#include <syslog.h>
-#endif
#include <sys/ioctl.h>
#include <sys/types.h>
@@ -209,6 +206,9 @@ bool opposite(int, char);
BULLET *is_bullet(int, int);
void fixshots(int, int, char);
+/* in support.c */
+__printflike(2, 3) void complain(int level, const char *fmt, ...);
+
/* in terminal.c */
void cgoto(PLAYER *, int, int);
void outch(PLAYER *, int);
diff --git a/hunt/huntd/support.c b/hunt/huntd/support.c
new file mode 100644
index 00000000..a40ab162
--- /dev/null
+++ b/hunt/huntd/support.c
@@ -0,0 +1,60 @@
+/* $NetBSD: support.c,v 1.1 2014/03/29 22:29:55 dholland Exp $ */
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by David A. Holland.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <syslog.h>
+#include <errno.h>
+#include <err.h>
+
+#include "hunt.h"
+
+__RCSID("$NetBSD: support.c,v 1.1 2014/03/29 22:29:55 dholland Exp $");
+
+void
+complain(int level, const char *fmt, ...)
+{
+ va_list ap;
+#ifdef LOG
+ char buf[4096];
+ int serrno = errno;
+#endif
+
+ va_start(ap, fmt);
+#ifdef LOG
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ snprintf(buf, sizeof(buf) - strlen(buf), ": %s", strerror(serrno));
+ syslog(level, "%s", buf);
+#else
+ (void)level;
+ vwarn("accept");
+#endif
+ va_end(ap);
+}