daemon: Add ability to write a PID file

The PID file will only be written if the daemon is called with the
`--detach` command line argument and a `--pidfile /some/file/path`
argument.
This commit is contained in:
Erik de Castro Lopo 2017-07-19 21:45:00 +10:00
parent ab594cfee9
commit 51efb21713
3 changed files with 58 additions and 11 deletions

View File

@ -43,6 +43,10 @@ namespace daemonizer
"detach" "detach"
, "Run as daemon" , "Run as daemon"
}; };
const command_line::arg_descriptor<std::string> arg_pidfile = {
"pidfile"
, "File path to write the daemon's PID to (optional, requires --detach)"
};
const command_line::arg_descriptor<bool> arg_non_interactive = { const command_line::arg_descriptor<bool> arg_non_interactive = {
"non-interactive" "non-interactive"
, "Run non-interactive" , "Run non-interactive"
@ -55,6 +59,7 @@ namespace daemonizer
) )
{ {
command_line::add_arg(normal_options, arg_detach); command_line::add_arg(normal_options, arg_detach);
command_line::add_arg(normal_options, arg_pidfile);
command_line::add_arg(normal_options, arg_non_interactive); command_line::add_arg(normal_options, arg_non_interactive);
} }
@ -80,7 +85,12 @@ namespace daemonizer
if (command_line::has_arg(vm, arg_detach)) if (command_line::has_arg(vm, arg_detach))
{ {
tools::success_msg_writer() << "Forking to background..."; tools::success_msg_writer() << "Forking to background...";
posix::fork(); std::string pidfile;
if (command_line::has_arg(vm, arg_pidfile))
{
pidfile = command_line::get_arg(vm, arg_pidfile);
}
posix::fork(pidfile);
auto daemon = executor.create_daemon(vm); auto daemon = executor.create_daemon(vm);
return daemon.run(); return daemon.run();
} }

View File

@ -21,15 +21,43 @@
namespace posix { namespace posix {
namespace { namespace {
void quit(std::string const & message) void quit(const std::string & message)
{ {
LOG_ERROR(message); LOG_ERROR(message);
throw std::runtime_error(message); throw std::runtime_error(message);
} }
} }
void fork() void fork(const std::string & pidfile)
{ {
// If a PID file is specified, we open the file here, because
// we can't report errors after the fork operation.
// When we fork, we close thise file in each of the parent
// processes.
// Only in the final child process do we write the PID to the
// file (and close it).
std::ofstream pidofs;
if (! pidfile.empty ())
{
int oldpid;
std::ifstream pidrifs;
pidrifs.open(pidfile, std::fstream::in);
if (! pidrifs.fail())
{
// Read the PID and send signal 0 to see if the process exists.
if (pidrifs >> oldpid && oldpid > 1 && kill(oldpid, 0) == 0)
{
quit("PID file " + pidfile + " already exists and the PID therein is valid");
}
pidrifs.close();
}
pidofs.open(pidfile, std::fstream::out | std::fstream::trunc);
if (pidofs.fail())
{
quit("Failed to open specified PID file for writing");
}
}
// Fork the process and have the parent exit. If the process was started // Fork the process and have the parent exit. If the process was started
// from a shell, this returns control to the user. Forking a new process is // from a shell, this returns control to the user. Forking a new process is
// also a prerequisite for the subsequent call to setsid(). // also a prerequisite for the subsequent call to setsid().
@ -38,7 +66,7 @@ void fork()
if (pid > 0) if (pid > 0)
{ {
// We're in the parent process and need to exit. // We're in the parent process and need to exit.
// pidofs.close();
// When the exit() function is used, the program terminates without // When the exit() function is used, the program terminates without
// invoking local variables' destructors. Only global variables are // invoking local variables' destructors. Only global variables are
// destroyed. // destroyed.
@ -59,6 +87,7 @@ void fork()
{ {
if (pid > 0) if (pid > 0)
{ {
pidofs.close();
exit(0); exit(0);
} }
else else
@ -67,6 +96,13 @@ void fork()
} }
} }
if (! pidofs.fail())
{
int pid = ::getpid();
pidofs << pid << std::endl;
pidofs.close();
}
// Close the standard streams. This decouples the daemon from the terminal // Close the standard streams. This decouples the daemon from the terminal
// that started it. // that started it.
close(0); close(0);

View File

@ -27,12 +27,13 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once #pragma once
#include <string>
#ifndef WIN32 #ifndef WIN32
namespace posix { namespace posix {
void fork(); void fork(const std::string & pidfile);
} }