unix/{Daemon,PidFile}: throw exception on error
Don't use the bad `FatalError` library.
This commit is contained in:
@@ -19,9 +19,10 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "Daemon.hxx"
|
#include "Daemon.hxx"
|
||||||
#include "system/FatalError.hxx"
|
#include "system/Error.hxx"
|
||||||
#include "fs/AllocatedPath.hxx"
|
#include "fs/AllocatedPath.hxx"
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/FileSystem.hxx"
|
||||||
|
#include "util/RuntimeError.hxx"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include "PidFile.hxx"
|
#include "PidFile.hxx"
|
||||||
@@ -69,18 +70,17 @@ void
|
|||||||
daemonize_kill(void)
|
daemonize_kill(void)
|
||||||
{
|
{
|
||||||
if (pidfile.IsNull())
|
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);
|
const pid_t pid = ReadPidFile(pidfile);
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
const std::string utf8 = pidfile.ToUTF8();
|
const std::string utf8 = pidfile.ToUTF8();
|
||||||
FormatFatalError("unable to read the pid from file \"%s\"",
|
throw FormatErrno("unable to read the pid from file \"%s\"",
|
||||||
utf8.c_str());
|
utf8.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kill(pid, SIGTERM) < 0)
|
if (kill(pid, SIGTERM) < 0)
|
||||||
FormatFatalSystemError("unable to kill process %i",
|
throw FormatErrno("unable to kill process %i", int(pid));
|
||||||
int(pid));
|
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
@@ -101,8 +101,7 @@ daemonize_set_user(void)
|
|||||||
/* set gid */
|
/* set gid */
|
||||||
if (user_gid != (gid_t)-1 && user_gid != getgid() &&
|
if (user_gid != (gid_t)-1 && user_gid != getgid() &&
|
||||||
setgid(user_gid) == -1) {
|
setgid(user_gid) == -1) {
|
||||||
FormatFatalSystemError("Failed to set group %d",
|
throw FormatErrno("Failed to set group %d", (int)user_gid);
|
||||||
(int)user_gid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_INITGROUPS
|
#ifdef HAVE_INITGROUPS
|
||||||
@@ -114,17 +113,16 @@ daemonize_set_user(void)
|
|||||||
we are already this user */
|
we are already this user */
|
||||||
user_uid != getuid() &&
|
user_uid != getuid() &&
|
||||||
initgroups(user_name, user_gid) == -1) {
|
initgroups(user_name, user_gid) == -1) {
|
||||||
FormatFatalSystemError("Failed to set supplementary groups "
|
throw FormatErrno("Failed to set supplementary groups "
|
||||||
"of user \"%s\"",
|
"of user \"%s\"",
|
||||||
user_name);
|
user_name);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* set uid */
|
/* set uid */
|
||||||
if (user_uid != (uid_t)-1 && user_uid != getuid() &&
|
if (user_uid != (uid_t)-1 && user_uid != getuid() &&
|
||||||
setuid(user_uid) == -1) {
|
setuid(user_uid) == -1) {
|
||||||
FormatFatalSystemError("Failed to set user \"%s\"",
|
throw FormatErrno("Failed to set user \"%s\"", user_name);
|
||||||
user_name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +131,7 @@ daemonize_begin(bool detach)
|
|||||||
{
|
{
|
||||||
/* release the current working directory */
|
/* release the current working directory */
|
||||||
if (chdir("/") < 0)
|
if (chdir("/") < 0)
|
||||||
FatalError("problems changing to root directory");
|
throw MakeErrno("problems changing to root directory");
|
||||||
|
|
||||||
if (!detach)
|
if (!detach)
|
||||||
/* the rest of this function deals with detaching the
|
/* the rest of this function deals with detaching the
|
||||||
@@ -152,13 +150,13 @@ daemonize_begin(bool detach)
|
|||||||
|
|
||||||
int fds[2];
|
int fds[2];
|
||||||
if (pipe(fds) < 0)
|
if (pipe(fds) < 0)
|
||||||
FatalSystemError("pipe() failed");
|
throw MakeErrno("pipe() failed");
|
||||||
|
|
||||||
/* move to a child process */
|
/* move to a child process */
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid < 0)
|
if (pid < 0)
|
||||||
FatalSystemError("fork() failed");
|
throw MakeErrno("fork() failed");
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
/* in the child process */
|
/* in the child process */
|
||||||
@@ -193,11 +191,11 @@ daemonize_begin(bool detach)
|
|||||||
int status;
|
int status;
|
||||||
pid_t pid2 = waitpid(pid, &status, 0);
|
pid_t pid2 = waitpid(pid, &status, 0);
|
||||||
if (pid2 < 0)
|
if (pid2 < 0)
|
||||||
FatalSystemError("waitpid() failed");
|
throw MakeErrno("waitpid() failed");
|
||||||
|
|
||||||
if (WIFSIGNALED(status))
|
if (WIFSIGNALED(status))
|
||||||
FormatFatalError("MPD died from signal %d%s", WTERMSIG(status),
|
throw FormatErrno("MPD died from signal %d%s", WTERMSIG(status),
|
||||||
WCOREDUMP(status) ? " (core dumped)" : "");
|
WCOREDUMP(status) ? " (core dumped)" : "");
|
||||||
|
|
||||||
exit(WEXITSTATUS(status));
|
exit(WEXITSTATUS(status));
|
||||||
}
|
}
|
||||||
@@ -223,7 +221,7 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile)
|
|||||||
if (user) {
|
if (user) {
|
||||||
struct passwd *pwd = getpwnam(user);
|
struct passwd *pwd = getpwnam(user);
|
||||||
if (pwd == nullptr)
|
if (pwd == nullptr)
|
||||||
FormatFatalError("no such user \"%s\"", user);
|
throw FormatRuntimeError("no such user \"%s\"", user);
|
||||||
|
|
||||||
user_uid = pwd->pw_uid;
|
user_uid = pwd->pw_uid;
|
||||||
user_gid = pwd->pw_gid;
|
user_gid = pwd->pw_gid;
|
||||||
@@ -237,7 +235,8 @@ daemonize_init(const char *user, const char *group, AllocatedPath &&_pidfile)
|
|||||||
if (group) {
|
if (group) {
|
||||||
struct group *grp = getgrnam(group);
|
struct group *grp = getgrnam(group);
|
||||||
if (grp == nullptr)
|
if (grp == nullptr)
|
||||||
FormatFatalError("no such group \"%s\"", group);
|
throw FormatRuntimeError("no such group \"%s\"",
|
||||||
|
group);
|
||||||
user_gid = grp->gr_gid;
|
user_gid = grp->gr_gid;
|
||||||
had_group = true;
|
had_group = true;
|
||||||
}
|
}
|
||||||
|
@@ -48,11 +48,11 @@ daemonize_finish()
|
|||||||
void
|
void
|
||||||
daemonize_kill();
|
daemonize_kill();
|
||||||
#else
|
#else
|
||||||
#include "system/FatalError.hxx"
|
#include <stdexcept>
|
||||||
static inline void
|
static inline void
|
||||||
daemonize_kill()
|
daemonize_kill()
|
||||||
{
|
{
|
||||||
FatalError("--kill is not available on WIN32");
|
throw std::runtime_error("--kill is not available on WIN32");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/FileSystem.hxx"
|
||||||
#include "fs/AllocatedPath.hxx"
|
#include "fs/AllocatedPath.hxx"
|
||||||
#include "Log.hxx"
|
#include "system/Error.hxx"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -41,8 +41,8 @@ public:
|
|||||||
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666).Steal();
|
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666).Steal();
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
const std::string utf8 = path.ToUTF8();
|
const std::string utf8 = path.ToUTF8();
|
||||||
FormatFatalSystemError("Failed to create pid file \"%s\"",
|
throw FormatErrno("Failed to create pid file \"%s\"",
|
||||||
utf8.c_str());
|
utf8.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user