system/FmtError: new library

Replaces the Format*() functions in system/Error.hxx.
This commit is contained in:
Max Kellermann
2022-11-28 18:49:35 +01:00
parent 124e75c286
commit 96ae659fdf
25 changed files with 305 additions and 186 deletions

View File

@@ -19,7 +19,8 @@
#include "config.h"
#include "Daemon.hxx"
#include "system/Error.hxx"
#include "lib/fmt/PathFormatter.hxx"
#include "system/FmtError.hxx"
#include "fs/AllocatedPath.hxx"
#include "util/RuntimeError.hxx"
@@ -70,14 +71,12 @@ daemonize_kill()
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();
throw FormatErrno("unable to read the pid from file \"%s\"",
utf8.c_str());
}
if (pid < 0)
throw FmtErrno("unable to read the pid from file \"{}\"",
pidfile);
if (kill(pid, SIGTERM) < 0)
throw FormatErrno("unable to kill process %i", int(pid));
throw FmtErrno("unable to kill process {}", pid);
std::exit(EXIT_SUCCESS);
}
@@ -98,7 +97,7 @@ daemonize_set_user()
/* set gid */
if (user_gid != (gid_t)-1 && user_gid != getgid() &&
setgid(user_gid) == -1) {
throw FormatErrno("Failed to set group %d", (int)user_gid);
throw FmtErrno("Failed to set group {}", user_gid);
}
#ifdef HAVE_INITGROUPS
@@ -110,16 +109,16 @@ daemonize_set_user()
we are already this user */
user_uid != getuid() &&
initgroups(user_name, user_gid) == -1) {
throw FormatErrno("Failed to set supplementary groups "
"of user \"%s\"",
user_name);
throw FmtErrno("Failed to set supplementary groups "
"of user \"{}\"",
user_name);
}
#endif
/* set uid */
if (user_uid != (uid_t)-1 && user_uid != getuid() &&
setuid(user_uid) == -1) {
throw FormatErrno("Failed to set user \"%s\"", user_name);
throw FmtErrno("Failed to set user \"{}\"", user_name);
}
}
@@ -191,8 +190,8 @@ daemonize_begin(bool detach)
throw MakeErrno("waitpid() failed");
if (WIFSIGNALED(status))
throw FormatErrno("MPD died from signal %d%s", WTERMSIG(status),
WCOREDUMP(status) ? " (core dumped)" : "");
throw FmtErrno("MPD died from signal {}{}", WTERMSIG(status),
WCOREDUMP(status) ? " (core dumped)" : "");
std::exit(WEXITSTATUS(status));
}

View File

@@ -20,9 +20,10 @@
#ifndef MPD_PID_FILE_HXX
#define MPD_PID_FILE_HXX
#include "lib/fmt/PathFormatter.hxx"
#include "fs/FileSystem.hxx"
#include "fs/AllocatedPath.hxx"
#include "system/Error.hxx"
#include "system/FmtError.hxx"
#include <cassert>
@@ -40,11 +41,9 @@ public:
return;
fd = OpenFile(path, O_WRONLY|O_CREAT|O_TRUNC, 0666).Steal();
if (fd < 0) {
const std::string utf8 = path.ToUTF8();
throw FormatErrno("Failed to create pid file \"%s\"",
utf8.c_str());
}
if (fd < 0)
throw FmtErrno("Failed to create pid file \"{}\"",
path);
}
PidFile(const PidFile &) = delete;