From 1842fa90dddd257b8b144a8592a893c8e101d3a1 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Wed, 16 Jul 2014 23:23:03 +0200
Subject: Revise usage of print_{error,fatal}()
Let's limit print_fatal() to unexpected conditions.
Also added exit_fatal() to save a few lines of code.
---
src/common.c | 38 +++++++++++++++-----------------------
src/kike.c | 18 +++++-------------
src/zyklonb.c | 22 ++++++++--------------
3 files changed, 28 insertions(+), 50 deletions(-)
diff --git a/src/common.c b/src/common.c
index eb69b80..73f8e5c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -131,11 +131,19 @@ log_message (int priority, const char *quote, const char *fmt, ...)
va_end (ap);
}
+// `fatal' is reserved for unexpected failures that would harm further operation
+
#define print_fatal(...) log_message (LOG_ERR, "fatal: ", __VA_ARGS__)
#define print_error(...) log_message (LOG_ERR, "error: ", __VA_ARGS__)
#define print_warning(...) log_message (LOG_WARNING, "warning: ", __VA_ARGS__)
#define print_status(...) log_message (LOG_INFO, "-- ", __VA_ARGS__)
+#define exit_fatal(...) \
+ BLOCK_START \
+ print_fatal (__VA_ARGS__); \
+ exit (EXIT_FAILURE); \
+ BLOCK_END
+
// --- Debugging and assertions ------------------------------------------------
// We should check everything that may possibly fail with at least a soft
@@ -191,10 +199,7 @@ xmalloc (size_t n)
{
void *p = malloc (n);
if (!p)
- {
- print_fatal ("malloc: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("malloc: %s", strerror (errno));
return p;
}
@@ -203,10 +208,7 @@ xcalloc (size_t n, size_t m)
{
void *p = calloc (n, m);
if (!p && n && m)
- {
- print_fatal ("calloc: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("calloc: %s", strerror (errno));
return p;
}
@@ -215,10 +217,7 @@ xrealloc (void *o, size_t n)
{
void *p = realloc (o, n);
if (!p && n)
- {
- print_fatal ("realloc: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("realloc: %s", strerror (errno));
return p;
}
@@ -228,8 +227,7 @@ xreallocarray (void *o, size_t n, size_t m)
if (m && n > SIZE_MAX / m)
{
errno = ENOMEM;
- print_fatal ("reallocarray: %s", strerror (errno));
- exit (EXIT_FAILURE);
+ exit_fatal ("reallocarray: %s", strerror (errno));
}
return xrealloc (o, n * m);
}
@@ -1170,10 +1168,7 @@ poller_run (struct poller *self)
while (n_fds == -1 && errno == EINTR);
if (n_fds == -1)
- {
- print_fatal ("%s: %s", "epoll", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "epoll", strerror (errno));
poller_timers_dispatch (&self->timers);
@@ -1312,10 +1307,7 @@ poller_run (struct poller *self)
while (result == -1 && errno == EINTR);
if (result == -1)
- {
- print_fatal ("%s: %s", "poll", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "poll", strerror (errno));
poller_timers_dispatch (&self->timers);
@@ -1931,7 +1923,7 @@ call_write_default_config (const char *hint, const struct config_item *table)
char *filename = write_default_config (hint, prolog, table, &e);
if (!filename)
{
- print_fatal ("%s", e->message);
+ print_error ("%s", e->message);
error_free (e);
exit (EXIT_FAILURE);
}
diff --git a/src/kike.c b/src/kike.c
index 5bcab42..974f80f 100644
--- a/src/kike.c
+++ b/src/kike.c
@@ -65,10 +65,7 @@ static void
setup_signal_handlers (void)
{
if (pipe (g_signal_pipe) == -1)
- {
- print_fatal ("pipe: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "pipe", strerror (errno));
set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]);
@@ -87,10 +84,7 @@ setup_signal_handlers (void)
sa.sa_handler = sigterm_handler;
if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1)
- {
- print_error ("sigaction: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "sigaction", strerror (errno));
}
// --- IRC token validation ----------------------------------------------------
@@ -1011,10 +1005,8 @@ on_irc_client_available (const struct pollfd *pfd, void *user_data)
// TODO: handle resource exhaustion (EMFILE, ENFILE) specially
// (stop accepting new connections and wait until we close some).
- print_fatal ("%s: %s", "accept", strerror (errno));
-
// FIXME: handle this better, bring the server down cleanly.
- exit (EXIT_FAILURE);
+ exit_fatal ("%s: %s", "accept", strerror (errno));
}
char host[NI_MAXHOST] = "unknown", port[NI_MAXSERV] = "unknown";
@@ -1363,7 +1355,7 @@ main (int argc, char *argv[])
call_write_default_config (optarg, g_config_table);
exit (EXIT_SUCCESS);
default:
- print_fatal ("error in options");
+ print_error ("wrong options");
exit (EXIT_FAILURE);
}
}
@@ -1384,7 +1376,7 @@ main (int argc, char *argv[])
struct error *e = NULL;
if (!read_config_file (&ctx.config, &e))
{
- print_fatal ("error loading configuration: %s", e->message);
+ print_error ("error loading configuration: %s", e->message);
error_free (e);
exit (EXIT_FAILURE);
}
diff --git a/src/zyklonb.c b/src/zyklonb.c
index 906df7e..b2584de 100644
--- a/src/zyklonb.c
+++ b/src/zyklonb.c
@@ -420,10 +420,7 @@ static void
setup_signal_handlers (void)
{
if (pipe (g_signal_pipe) == -1)
- {
- print_fatal ("pipe: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "pipe", strerror (errno));
set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]);
@@ -440,17 +437,14 @@ setup_signal_handlers (void)
sigemptyset (&sa.sa_mask);
if (sigaction (SIGCHLD, &sa, NULL) == -1)
- {
- print_fatal ("sigaction: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("sigaction: %s", strerror (errno));
signal (SIGPIPE, SIG_IGN);
sa.sa_handler = sigterm_handler;
if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1)
- print_error ("sigaction: %s", strerror (errno));
+ exit_fatal ("sigaction: %s", strerror (errno));
}
static void
@@ -587,7 +581,7 @@ setup_recovery_handler (struct bot_context *ctx)
bool recover;
if (!set_boolean_if_valid (&recover, recover_str))
{
- print_fatal ("invalid configuration value for `%s'", "recover");
+ print_error ("invalid configuration value for `%s'", "recover");
exit (EXIT_FAILURE);
}
if (!recover)
@@ -968,7 +962,7 @@ plugin_load (struct bot_context *ctx, const char *name, struct error **e)
execve (argv[0], argv, environ);
// We will collect the failure later via SIGCHLD
- print_fatal ("%s: %s: %s",
+ print_error ("%s: %s: %s",
"failed to load the plugin", "exec", strerror (errno));
_exit (EXIT_FAILURE);
}
@@ -1495,7 +1489,7 @@ on_irc_readable (const struct pollfd *fd, struct bot_context *ctx)
if (buf->len >= (1 << 20))
{
- print_fatal ("the IRC server seems to spew out data frantically");
+ print_error ("the IRC server seems to spew out data frantically");
irc_shutdown (ctx);
goto end;
}
@@ -1703,7 +1697,7 @@ main (int argc, char *argv[])
call_write_default_config (optarg, g_config_table);
exit (EXIT_SUCCESS);
default:
- print_fatal ("error in options");
+ print_error ("wrong options");
exit (EXIT_FAILURE);
}
}
@@ -1723,7 +1717,7 @@ main (int argc, char *argv[])
struct error *e = NULL;
if (!read_config_file (&ctx.config, &e))
{
- print_fatal ("error loading configuration: %s", e->message);
+ print_error ("error loading configuration: %s", e->message);
error_free (e);
exit (EXIT_FAILURE);
}
--
cgit v1.2.3-70-g09d2