FatalError: new library to replace mpd_error.h

This commit is contained in:
Max Kellermann 2013-08-07 09:35:30 +02:00
parent 67e44b0f2c
commit a27d105dcd
15 changed files with 222 additions and 76 deletions

View File

@ -91,6 +91,7 @@ src_mpd_SOURCES = \
src/thread/PosixCond.hxx \
src/thread/WindowsCond.hxx \
src/thread/GLibCond.hxx \
src/FatalError.cxx src/FatalError.hxx \
src/clock.c src/clock.h \
src/notify.cxx src/notify.hxx \
src/AudioConfig.cxx src/AudioConfig.hxx \
@ -1360,6 +1361,7 @@ endif
if ENABLE_INOTIFY
noinst_PROGRAMS += test/run_inotify
test_run_inotify_SOURCES = test/run_inotify.cxx \
src/FatalError.cxx \
src/fd_util.c \
src/InotifySource.cxx
test_run_inotify_LDADD = \

View File

@ -19,13 +19,13 @@
#include "config.h"
#include "Daemon.hxx"
#include "FatalError.hxx"
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -65,23 +65,23 @@ daemonize_kill(void)
int pid, ret;
if (pidfile == nullptr)
MPD_ERROR("no pid_file specified in the config file");
FatalError("no pid_file specified in the config file");
fp = fopen(pidfile, "r");
if (fp == nullptr)
MPD_ERROR("unable to open pid file \"%s\": %s",
pidfile, g_strerror(errno));
FormatFatalSystemError("Unable to open pid file \"%s\"",
pidfile);
if (fscanf(fp, "%i", &pid) != 1) {
MPD_ERROR("unable to read the pid from file \"%s\"",
FormatFatalError("unable to read the pid from file \"%s\"",
pidfile);
}
fclose(fp);
ret = kill(pid, SIGTERM);
if (ret < 0)
MPD_ERROR("unable to kill process %i: %s",
pid, g_strerror(errno));
FormatFatalSystemError("unable to kill process %i",
int(pid));
exit(EXIT_SUCCESS);
}
@ -102,8 +102,8 @@ daemonize_set_user(void)
/* set gid */
if (user_gid != (gid_t)-1 && user_gid != getgid()) {
if (setgid(user_gid) == -1) {
MPD_ERROR("cannot setgid to %d: %s",
(int)user_gid, g_strerror(errno));
FormatFatalSystemError("Failed to set group %d",
(int)user_gid);
}
}
@ -112,17 +112,17 @@ daemonize_set_user(void)
* (must be done before we change our uid)
*/
if (!had_group && initgroups(user_name, user_gid) == -1) {
g_warning("cannot init supplementary groups "
"of user \"%s\": %s",
user_name, g_strerror(errno));
FormatFatalSystemError("Failed to set supplementary groups "
"of user \"%s\"",
user_name);
}
#endif
/* set uid */
if (user_uid != (uid_t)-1 && user_uid != getuid() &&
setuid(user_uid) == -1) {
MPD_ERROR("cannot change to uid of user \"%s\": %s",
user_name, g_strerror(errno));
FormatFatalSystemError("Failed to set user \"%s\"",
user_name);
}
}
@ -136,7 +136,7 @@ daemonize_detach(void)
#ifdef HAVE_DAEMON
if (daemon(0, 1))
MPD_ERROR("daemon() failed: %s", g_strerror(errno));
FatalSystemError("daemon() failed");
#elif defined(HAVE_FORK)
@ -144,7 +144,7 @@ daemonize_detach(void)
switch (fork()) {
case -1:
MPD_ERROR("fork() failed: %s", g_strerror(errno));
FatalSystemError("fork() failed");
case 0:
break;
default:
@ -155,14 +155,14 @@ daemonize_detach(void)
/* release the current working directory */
if (chdir("/") < 0)
MPD_ERROR("problems changing to root directory");
FatalError("problems changing to root directory");
/* detach from the current session */
setsid();
#else
MPD_ERROR("no support for daemonizing");
FatalError("no support for daemonizing");
#endif
g_debug("daemonized!");
@ -179,8 +179,8 @@ daemonize(bool detach)
g_debug("opening pid file");
fp = fopen(pidfile, "w+");
if (!fp) {
MPD_ERROR("could not create pid file \"%s\": %s",
pidfile, g_strerror(errno));
FormatFatalSystemError("Failed to create pid file \"%s\"",
pidfile);
}
}
@ -200,7 +200,7 @@ daemonize_init(const char *user, const char *group, const char *_pidfile)
if (user) {
struct passwd *pwd = getpwnam(user);
if (pwd == nullptr)
MPD_ERROR("no such user \"%s\"", user);
FormatFatalError("no such user \"%s\"", user);
user_uid = pwd->pw_uid;
user_gid = pwd->pw_gid;
@ -214,7 +214,7 @@ daemonize_init(const char *user, const char *group, const char *_pidfile)
if (group) {
struct group *grp = getgrnam(group);
if (grp == nullptr)
MPD_ERROR("no such group \"%s\"", group);
FormatFatalError("no such group \"%s\"", group);
user_gid = grp->gr_gid;
had_group = true;
}

View File

@ -20,8 +20,6 @@
#ifndef MPD_DAEMON_HXX
#define MPD_DAEMON_HXX
#include "mpd_error.h"
#ifndef WIN32
void
daemonize_init(const char *user, const char *group, const char *pidfile);
@ -48,10 +46,12 @@ daemonize_finish(void)
void
daemonize_kill(void);
#else
#include <glib.h>
#include "FatalError.hxx"
static inline void
daemonize_kill(void)
{ MPD_ERROR("--kill is not available on WIN32"); }
{
FatalError("--kill is not available on WIN32");
}
#endif
/**

View File

@ -24,7 +24,7 @@
#include "DecoderError.hxx"
#include "DecoderPlugin.hxx"
#include "Song.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include "Mapper.hxx"
#include "fs/Path.hxx"
#include "DecoderAPI.hxx"
@ -501,6 +501,6 @@ decoder_thread_start(struct decoder_control *dc)
GError *e = NULL;
dc->thread = g_thread_create(decoder_task, dc, true, &e);
if (dc->thread == NULL)
MPD_ERROR("Failed to spawn decoder task: %s", e->message);
FatalError("Failed to spawn decoder task", e);
#endif
}

91
src/FatalError.cxx Normal file
View File

@ -0,0 +1,91 @@
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "FatalError.hxx"
#include <glib.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <errno.h>
#endif
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "FatalError"
void
FatalError(const char *msg)
{
g_critical("%s", msg);
exit(EXIT_FAILURE);
}
void
FormatFatalError(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
g_logv(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void
FatalError(GError *error)
{
FatalError(error->message);
}
void
FatalError(const char *msg, GError *error)
{
FormatFatalError("%s: %s", msg, error->message);
}
void
FatalSystemError(const char *msg)
{
const char *system_error;
#ifdef WIN32
system_error = g_win32_error_message(GetLastError());
#else
system_error = g_strerror(errno);
#endif
g_critical("%s: %s", msg, system_error);
exit(EXIT_FAILURE);
}
void
FormatFatalSystemError(const char *fmt, ...)
{
char buffer[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
FatalSystemError(buffer);
}

58
src/FatalError.hxx Normal file
View File

@ -0,0 +1,58 @@
/*
* Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef MPD_FATAL_ERROR_HXX
#define MPD_FATAL_ERROR_HXX
#include "gerror.h"
#include "gcc.h"
/**
* Log the specified message and abort the process.
*/
gcc_noreturn
void
FatalError(const char *msg);
gcc_noreturn
void
FormatFatalError(const char *fmt, ...);
gcc_noreturn
void
FatalError(GError *error);
gcc_noreturn
void
FatalError(const char *msg, GError *error);
/**
* Call this after a system call has failed that is not supposed to
* fail. Prints the given message, the system error message (from
* errno or GetLastError()) and abort the process.
*/
gcc_noreturn
void
FatalSystemError(const char *msg);
gcc_noreturn
void
FormatFatalSystemError(const char *fmt, ...);
#endif

View File

@ -21,7 +21,7 @@
#include "InotifySource.hxx"
#include "util/fifo_buffer.h"
#include "fd_util.h"
#include "mpd_error.h"
#include "FatalError.hxx"
#include <glib.h>
@ -50,14 +50,13 @@ InotifySource::OnSocketReady(gcc_unused unsigned flags)
dest = fifo_buffer_write(buffer, &length);
if (dest == NULL)
MPD_ERROR("buffer full");
FatalError("buffer full");
nbytes = read(Get(), dest, length);
if (nbytes < 0)
MPD_ERROR("failed to read from inotify: %s",
g_strerror(errno));
FatalSystemError("Failed to read from inotify");
if (nbytes == 0)
MPD_ERROR("end of file from inotify");
FatalError("end of file from inotify");
fifo_buffer_append(buffer, nbytes);

View File

@ -21,6 +21,7 @@
#include "Log.hxx"
#include "conf.h"
#include "fd_util.h"
#include "FatalError.hxx"
#include "mpd_error.h"
#include <assert.h>
@ -60,9 +61,9 @@ static void redirect_logs(int fd)
{
assert(fd >= 0);
if (dup2(fd, STDOUT_FILENO) < 0)
MPD_ERROR("problems dup2 stdout : %s\n", strerror(errno));
FatalSystemError("Failed to dup2 stdout");
if (dup2(fd, STDERR_FILENO) < 0)
MPD_ERROR("problems dup2 stderr : %s\n", strerror(errno));
FatalSystemError("Failed to dup2 stderr");
}
static const char *log_date(void)

View File

@ -55,13 +55,12 @@
#include "AudioConfig.hxx"
#include "pcm/PcmResample.hxx"
#include "Daemon.hxx"
#include "FatalError.hxx"
extern "C" {
#include "stats.h"
}
#include "mpd_error.h"
#ifdef ENABLE_INOTIFY
#include "InotifyUpdate.hxx"
#endif
@ -194,13 +193,13 @@ glue_db_init_and_load(void)
}
if (!DatabaseGlobalInit(*param, &error))
MPD_ERROR("%s", error->message);
FatalError(error);
delete allocated;
ret = DatabaseGlobalOpen(&error);
if (!ret)
MPD_ERROR("%s", error->message);
FatalError(error);
/* run database update after daemonization? */
return !db_is_simple() || db_exists();
@ -216,10 +215,10 @@ glue_sticker_init(void)
GError *error = NULL;
char *sticker_file = config_dup_path(CONF_STICKER_FILE, &error);
if (sticker_file == NULL && error != NULL)
MPD_ERROR("%s", error->message);
FatalError(error);
if (!sticker_global_init(sticker_file, &error))
MPD_ERROR("%s", error->message);
FatalError(error);
g_free(sticker_file);
#endif
@ -268,16 +267,12 @@ static void winsock_init(void)
retval = WSAStartup(MAKEWORD(2, 2), &sockinfo);
if(retval != 0)
{
MPD_ERROR("Attempt to open Winsock2 failed; error code %d\n",
FormatFatalError("Attempt to open Winsock2 failed; error code %d",
retval);
}
if (LOBYTE(sockinfo.wVersion) != 2)
{
MPD_ERROR("We use Winsock2 but your version is either too new "
"or old; please install Winsock 2.x\n");
}
FatalError("We use Winsock2 but your version is either too new "
"or old; please install Winsock 2.x");
#endif
}
@ -298,8 +293,9 @@ initialize_decoder_and_player(void)
if (param != NULL) {
long tmp = strtol(param->value, &test, 10);
if (*test != '\0' || tmp <= 0 || tmp == LONG_MAX)
MPD_ERROR("buffer size \"%s\" is not a positive integer, "
"line %i\n", param->value, param->line);
FormatFatalError("buffer size \"%s\" is not a "
"positive integer, line %i",
param->value, param->line);
buffer_size = tmp;
} else
buffer_size = DEFAULT_BUFFER_SIZE;
@ -309,14 +305,16 @@ initialize_decoder_and_player(void)
buffered_chunks = buffer_size / CHUNK_SIZE;
if (buffered_chunks >= 1 << 15)
MPD_ERROR("buffer size \"%li\" is too big\n", (long)buffer_size);
FormatFatalError("buffer size \"%lu\" is too big",
(unsigned long)buffer_size);
param = config_get_param(CONF_BUFFER_BEFORE_PLAY);
if (param != NULL) {
perc = strtod(param->value, &test);
if (*test != '%' || perc < 0 || perc > 100) {
MPD_ERROR("buffered before play \"%s\" is not a positive "
"percentage and less than 100 percent, line %i",
FormatFatalError("buffered before play \"%s\" is not "
"a positive percentage and less "
"than 100 percent, line %i",
param->value, param->line);
}
} else
@ -505,7 +503,7 @@ int mpd_main(int argc, char *argv[])
database */
unsigned job = update_enqueue(NULL, true);
if (job == 0)
MPD_ERROR("directory update failed");
FatalError("directory update failed");
}
if (!glue_state_file_init(&error)) {

View File

@ -22,7 +22,7 @@
#include "MusicChunk.hxx"
#include "thread/Mutex.hxx"
#include "util/SliceBuffer.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include <assert.h>
@ -33,7 +33,7 @@ struct music_buffer : public SliceBuffer<music_chunk> {
music_buffer(unsigned num_chunks)
:SliceBuffer(num_chunks) {
if (IsOOM())
MPD_ERROR("Failed to allocate buffer");
FatalError("Failed to allocate buffer");
}
};

View File

@ -26,7 +26,7 @@
#include "MusicBuffer.hxx"
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include "conf.h"
#include "notify.hxx"
@ -128,10 +128,10 @@ audio_output_all_init(struct player_control *pc)
struct audio_output *output = audio_output_new(*param, pc, &error);
if (output == NULL) {
if (param != NULL)
MPD_ERROR("line %i: %s",
FormatFatalError("line %i: %s",
param->line, error->message);
else
MPD_ERROR("%s", error->message);
FatalError(error);
}
audio_outputs[i] = output;
@ -139,8 +139,8 @@ audio_output_all_init(struct player_control *pc)
/* require output names to be unique: */
for (j = 0; j < i; j++) {
if (!strcmp(output->name, audio_outputs[j]->name)) {
MPD_ERROR("output devices with identical "
"names: %s\n", output->name);
FormatFatalError("output devices with identical "
"names: %s", output->name);
}
}
}

View File

@ -29,8 +29,7 @@
#include "PlayerControl.hxx"
#include "MusicPipe.hxx"
#include "MusicChunk.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include "gcc.h"
#include <glib.h>
@ -676,6 +675,6 @@ void audio_output_thread_start(struct audio_output *ao)
#else
GError *e = nullptr;
if (!(ao->thread = g_thread_create(audio_output_task, ao, true, &e)))
MPD_ERROR("Failed to spawn output task: %s\n", e->message);
FatalError("Failed to spawn output task", e);
#endif
}

View File

@ -26,7 +26,7 @@
#include "MusicChunk.hxx"
#include "Song.hxx"
#include "Main.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include "CrossFade.hxx"
#include "PlayerControl.hxx"
#include "OutputAll.hxx"
@ -1207,6 +1207,6 @@ player_create(struct player_control *pc)
GError *e = NULL;
pc->thread = g_thread_create(player_task, pc, true, &e);
if (pc->thread == NULL)
MPD_ERROR("Failed to spawn player task: %s", e->message);
FatalError("Failed to spawn player task", e);
#endif
}

View File

@ -26,13 +26,11 @@
#include "Main.hxx"
#include "event/Loop.hxx"
#include "GlobalEvents.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include <glib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
static void exit_signal_handler(gcc_unused int signum)
{
@ -48,7 +46,7 @@ static void
x_sigaction(int signum, const struct sigaction *act)
{
if (sigaction(signum, act, NULL) < 0)
MPD_ERROR("sigaction() failed: %s", strerror(errno));
FatalSystemError("sigaction() failed");
}
static void

View File

@ -33,7 +33,7 @@ extern "C" {
#include "Main.hxx"
#include "Instance.hxx"
#include "mpd_error.h"
#include "FatalError.hxx"
#include <glib.h>
@ -110,7 +110,7 @@ spawn_update_task(const char *path)
GError *e = NULL;
update_thr = g_thread_create(update_task, g_strdup(path), TRUE, &e);
if (update_thr == NULL)
MPD_ERROR("Failed to spawn update task: %s", e->message);
FatalError("Failed to spawn update task", e);
#endif
if (++update_task_id > update_task_id_max)