unix/Daemon: move code to ReadPidFile()
This commit is contained in:
parent
28a0c46ca7
commit
567bf445bf
@ -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",
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user