unix/{Daemon,PidFile}: throw exception on error

Don't use the bad `FatalError` library.
This commit is contained in:
Max Kellermann 2018-08-01 20:20:29 +02:00
parent 95acc4be07
commit dfda0faac7
3 changed files with 25 additions and 26 deletions

View File

@ -19,9 +19,10 @@
#include "config.h"
#include "Daemon.hxx"
#include "system/FatalError.hxx"
#include "system/Error.hxx"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#include "util/RuntimeError.hxx"
#ifndef _WIN32
#include "PidFile.hxx"
@ -69,18 +70,17 @@ void
daemonize_kill(void)
{
if (pidfile.IsNull())
FatalError("no pid_file specified in the config file");
throw std::runtime_error("no pid_file specified in the config file");
const pid_t pid = ReadPidFile(pidfile);
if (pid < 0) {
const std::string utf8 = pidfile.ToUTF8();
FormatFatalError("unable to read the pid from file \"%s\"",
utf8.c_str());
throw FormatErrno("unable to read the pid from file \"%s\"",
utf8.c_str());
}
if (kill(pid, SIGTERM) < 0)
FormatFatalSystemError("unable to kill process %i",
int(pid));
throw FormatErrno("unable to kill process %i", int(pid));
exit(EXIT_SUCCESS);
}
@ -101,8 +101,7 @@ daemonize_set_user(void)
/* set gid */
if (user_gid != (gid_t)-1 && user_gid != getgid() &&
setgid(user_gid) == -1) {
FormatFatalSystemError("Failed to set group %d",
(int)user_gid);
throw FormatErrno("Failed to set group %d", (int)user_gid);
}
#ifdef HAVE_INITGROUPS
@ -114,17 +113,16 @@ daemonize_set_user(void)
we are already this user */
user_uid != getuid() &&
initgroups(user_name, user_gid) == -1) {
FormatFatalSystemError("Failed to set supplementary groups "
"of user \"%s\"",
user_name);
throw FormatErrno("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) {
FormatFatalSystemError("Failed to set user \"%s\"",
user_name);
throw FormatErrno("Failed to set user \"%s\"", user_name);
}
}
@ -133,7 +131,7 @@ daemonize_begin(bool detach)
{
/* release the current working directory */
if (chdir("/") < 0)
FatalError("problems changing to root directory");
throw MakeErrno("problems changing to root directory");
if (!detach)
/* the rest of this function deals with detaching the
@ -152,13 +150,13 @@ daemonize_begin(bool detach)
int fds[2];
if (pipe(fds) < 0)
FatalSystemError("pipe() failed");
throw MakeErrno("pipe() failed");
/* move to a child process */
pid_t pid = fork();
if (pid < 0)
FatalSystemError("fork() failed");
throw MakeErrno("fork() failed");
if (pid == 0) {
/* in the child process */
@ -193,11 +191,11 @@ daemonize_begin(bool detach)
int status;
pid_t pid2 = waitpid(pid, &status, 0);
if (pid2 < 0)
FatalSystemError("waitpid() failed");
throw MakeErrno("waitpid() failed");
if (WIFSIGNALED(status))
FormatFatalError("MPD died from signal %d%s", WTERMSIG(status),
WCOREDUMP(status) ? " (core dumped)" : "");
throw FormatErrno("MPD died from signal %d%s", WTERMSIG(status),
WCOREDUMP(status) ? " (core dumped)" : "");
exit(WEXITSTATUS(status));
}
@ -223,7 +221,7 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile)
if (user) {
struct passwd *pwd = getpwnam(user);
if (pwd == nullptr)
FormatFatalError("no such user \"%s\"", user);
throw FormatRuntimeError("no such user \"%s\"", user);
user_uid = pwd->pw_uid;
user_gid = pwd->pw_gid;
@ -237,7 +235,8 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile)
if (group) {
struct group *grp = getgrnam(group);
if (grp == nullptr)
FormatFatalError("no such group \"%s\"", group);
throw FormatRuntimeError("no such group \"%s\"",
group);
user_gid = grp->gr_gid;
had_group = true;
}

View File

@ -48,11 +48,11 @@ daemonize_finish()
void
daemonize_kill();
#else
#include "system/FatalError.hxx"
#include <stdexcept>
static inline void
daemonize_kill()
{
FatalError("--kill is not available on WIN32");
throw std::runtime_error("--kill is not available on WIN32");
}
#endif

View File

@ -22,7 +22,7 @@
#include "fs/FileSystem.hxx"
#include "fs/AllocatedPath.hxx"
#include "Log.hxx"
#include "system/Error.hxx"
#include <assert.h>
#include <string.h>
@ -41,8 +41,8 @@ public:
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666).Steal();
if (fd < 0) {
const std::string utf8 = path.ToUTF8();
FormatFatalSystemError("Failed to create pid file \"%s\"",
utf8.c_str());
throw FormatErrno("Failed to create pid file \"%s\"",
utf8.c_str());
}
}