aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2015-08-15 16:39:52 +0200
committerMax Kellermann <max@duempel.org>2015-08-15 16:41:22 +0200
commit567bf445bf188ae60056e779927da52dcd7ed35d (patch)
tree564862278fc77871478e7192f10a5f875c21d067 /src
parent28a0c46ca7dd06d4e57bec2593a86c32b3b31667 (diff)
downloadmpd-567bf445bf188ae60056e779927da52dcd7ed35d.tar.gz
mpd-567bf445bf188ae60056e779927da52dcd7ed35d.tar.xz
mpd-567bf445bf188ae60056e779927da52dcd7ed35d.zip
unix/Daemon: move code to ReadPidFile()
Diffstat (limited to '')
-rw-r--r--src/unix/Daemon.cxx14
-rw-r--r--src/unix/PidFile.hxx16
2 files changed, 18 insertions, 12 deletions
diff --git a/src/unix/Daemon.cxx b/src/unix/Daemon.cxx
index 4bb11aeac..d16de8928 100644
--- a/src/unix/Daemon.cxx
+++ b/src/unix/Daemon.cxx
@@ -65,25 +65,15 @@ static int detach_fd = -1;
void
daemonize_kill(void)
{
- FILE *fp;
- int pid;
-
if (pidfile.IsNull())
FatalError("no pid_file specified in the config file");
- fp = FOpen(pidfile, PATH_LITERAL("r"));
- if (fp == nullptr) {
- const std::string utf8 = pidfile.ToUTF8();
- FormatFatalSystemError("Unable to open pid file \"%s\"",
- utf8.c_str());
- }
-
- if (fscanf(fp, "%i", &pid) != 1) {
+ 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());
}
- fclose(fp);
if (kill(pid, SIGTERM) < 0)
FormatFatalSystemError("unable to kill process %i",
diff --git a/src/unix/PidFile.hxx b/src/unix/PidFile.hxx
index 3c3d5ca21..a8af1e890 100644
--- a/src/unix/PidFile.hxx
+++ b/src/unix/PidFile.hxx
@@ -82,4 +82,20 @@ public:
}
};
+gcc_pure
+static inline pid_t
+ReadPidFile(Path path)
+{
+ FILE *fp = FOpen(path, PATH_LITERAL("r"));
+ if (fp == nullptr)
+ return -1;
+
+ int pid;
+ if (fscanf(fp, "%i", &pid) != 1)
+ pid = -1;
+
+ fclose(fp);
+ return pid;
+}
+
#endif