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.
This commit is contained in:
Přemysl Eric Janouch 2014-07-16 23:23:03 +02:00
parent e00d2079b5
commit 1842fa90dd
3 changed files with 28 additions and 50 deletions

View File

@ -131,11 +131,19 @@ log_message (int priority, const char *quote, const char *fmt, ...)
va_end (ap); 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_fatal(...) log_message (LOG_ERR, "fatal: ", __VA_ARGS__)
#define print_error(...) log_message (LOG_ERR, "error: ", __VA_ARGS__) #define print_error(...) log_message (LOG_ERR, "error: ", __VA_ARGS__)
#define print_warning(...) log_message (LOG_WARNING, "warning: ", __VA_ARGS__) #define print_warning(...) log_message (LOG_WARNING, "warning: ", __VA_ARGS__)
#define print_status(...) log_message (LOG_INFO, "-- ", __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 ------------------------------------------------ // --- Debugging and assertions ------------------------------------------------
// We should check everything that may possibly fail with at least a soft // 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); void *p = malloc (n);
if (!p) if (!p)
{ exit_fatal ("malloc: %s", strerror (errno));
print_fatal ("malloc: %s", strerror (errno));
exit (EXIT_FAILURE);
}
return p; return p;
} }
@ -203,10 +208,7 @@ xcalloc (size_t n, size_t m)
{ {
void *p = calloc (n, m); void *p = calloc (n, m);
if (!p && n && m) if (!p && n && m)
{ exit_fatal ("calloc: %s", strerror (errno));
print_fatal ("calloc: %s", strerror (errno));
exit (EXIT_FAILURE);
}
return p; return p;
} }
@ -215,10 +217,7 @@ xrealloc (void *o, size_t n)
{ {
void *p = realloc (o, n); void *p = realloc (o, n);
if (!p && n) if (!p && n)
{ exit_fatal ("realloc: %s", strerror (errno));
print_fatal ("realloc: %s", strerror (errno));
exit (EXIT_FAILURE);
}
return p; return p;
} }
@ -228,8 +227,7 @@ xreallocarray (void *o, size_t n, size_t m)
if (m && n > SIZE_MAX / m) if (m && n > SIZE_MAX / m)
{ {
errno = ENOMEM; errno = ENOMEM;
print_fatal ("reallocarray: %s", strerror (errno)); exit_fatal ("reallocarray: %s", strerror (errno));
exit (EXIT_FAILURE);
} }
return xrealloc (o, n * m); return xrealloc (o, n * m);
} }
@ -1170,10 +1168,7 @@ poller_run (struct poller *self)
while (n_fds == -1 && errno == EINTR); while (n_fds == -1 && errno == EINTR);
if (n_fds == -1) if (n_fds == -1)
{ exit_fatal ("%s: %s", "epoll", strerror (errno));
print_fatal ("%s: %s", "epoll", strerror (errno));
exit (EXIT_FAILURE);
}
poller_timers_dispatch (&self->timers); poller_timers_dispatch (&self->timers);
@ -1312,10 +1307,7 @@ poller_run (struct poller *self)
while (result == -1 && errno == EINTR); while (result == -1 && errno == EINTR);
if (result == -1) if (result == -1)
{ exit_fatal ("%s: %s", "poll", strerror (errno));
print_fatal ("%s: %s", "poll", strerror (errno));
exit (EXIT_FAILURE);
}
poller_timers_dispatch (&self->timers); 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); char *filename = write_default_config (hint, prolog, table, &e);
if (!filename) if (!filename)
{ {
print_fatal ("%s", e->message); print_error ("%s", e->message);
error_free (e); error_free (e);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }

View File

@ -65,10 +65,7 @@ static void
setup_signal_handlers (void) setup_signal_handlers (void)
{ {
if (pipe (g_signal_pipe) == -1) if (pipe (g_signal_pipe) == -1)
{ exit_fatal ("%s: %s", "pipe", strerror (errno));
print_fatal ("pipe: %s", strerror (errno));
exit (EXIT_FAILURE);
}
set_cloexec (g_signal_pipe[0]); set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]); set_cloexec (g_signal_pipe[1]);
@ -87,10 +84,7 @@ setup_signal_handlers (void)
sa.sa_handler = sigterm_handler; sa.sa_handler = sigterm_handler;
if (sigaction (SIGINT, &sa, NULL) == -1 if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1) || sigaction (SIGTERM, &sa, NULL) == -1)
{ exit_fatal ("%s: %s", "sigaction", strerror (errno));
print_error ("sigaction: %s", strerror (errno));
exit (EXIT_FAILURE);
}
} }
// --- IRC token validation ---------------------------------------------------- // --- 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 // TODO: handle resource exhaustion (EMFILE, ENFILE) specially
// (stop accepting new connections and wait until we close some). // (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. // 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"; 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); call_write_default_config (optarg, g_config_table);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
default: default:
print_fatal ("error in options"); print_error ("wrong options");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
} }
@ -1384,7 +1376,7 @@ main (int argc, char *argv[])
struct error *e = NULL; struct error *e = NULL;
if (!read_config_file (&ctx.config, &e)) 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); error_free (e);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }

View File

@ -420,10 +420,7 @@ static void
setup_signal_handlers (void) setup_signal_handlers (void)
{ {
if (pipe (g_signal_pipe) == -1) if (pipe (g_signal_pipe) == -1)
{ exit_fatal ("%s: %s", "pipe", strerror (errno));
print_fatal ("pipe: %s", strerror (errno));
exit (EXIT_FAILURE);
}
set_cloexec (g_signal_pipe[0]); set_cloexec (g_signal_pipe[0]);
set_cloexec (g_signal_pipe[1]); set_cloexec (g_signal_pipe[1]);
@ -440,17 +437,14 @@ setup_signal_handlers (void)
sigemptyset (&sa.sa_mask); sigemptyset (&sa.sa_mask);
if (sigaction (SIGCHLD, &sa, NULL) == -1) if (sigaction (SIGCHLD, &sa, NULL) == -1)
{ exit_fatal ("sigaction: %s", strerror (errno));
print_fatal ("sigaction: %s", strerror (errno));
exit (EXIT_FAILURE);
}
signal (SIGPIPE, SIG_IGN); signal (SIGPIPE, SIG_IGN);
sa.sa_handler = sigterm_handler; sa.sa_handler = sigterm_handler;
if (sigaction (SIGINT, &sa, NULL) == -1 if (sigaction (SIGINT, &sa, NULL) == -1
|| sigaction (SIGTERM, &sa, NULL) == -1) || sigaction (SIGTERM, &sa, NULL) == -1)
print_error ("sigaction: %s", strerror (errno)); exit_fatal ("sigaction: %s", strerror (errno));
} }
static void static void
@ -587,7 +581,7 @@ setup_recovery_handler (struct bot_context *ctx)
bool recover; bool recover;
if (!set_boolean_if_valid (&recover, recover_str)) 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); exit (EXIT_FAILURE);
} }
if (!recover) if (!recover)
@ -968,7 +962,7 @@ plugin_load (struct bot_context *ctx, const char *name, struct error **e)
execve (argv[0], argv, environ); execve (argv[0], argv, environ);
// We will collect the failure later via SIGCHLD // 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)); "failed to load the plugin", "exec", strerror (errno));
_exit (EXIT_FAILURE); _exit (EXIT_FAILURE);
} }
@ -1495,7 +1489,7 @@ on_irc_readable (const struct pollfd *fd, struct bot_context *ctx)
if (buf->len >= (1 << 20)) 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); irc_shutdown (ctx);
goto end; goto end;
} }
@ -1703,7 +1697,7 @@ main (int argc, char *argv[])
call_write_default_config (optarg, g_config_table); call_write_default_config (optarg, g_config_table);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
default: default:
print_fatal ("error in options"); print_error ("wrong options");
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }
} }
@ -1723,7 +1717,7 @@ main (int argc, char *argv[])
struct error *e = NULL; struct error *e = NULL;
if (!read_config_file (&ctx.config, &e)) 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); error_free (e);
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
} }