Make program operate as a demon. Assumes UNIX type OS. Call very early on the the program. Has built in code to prevent multiple execution Arguments: None Returns: None
| Return Type | Function name | Arguments |
|---|---|---|
| void | Demonize | (void) |
Declared in file: hzProcess.h
Defined in file : hzProcess.cpp
Function Logic:
Function body:
void Demonize (void)
{
// Category: Process
//
// Make program operate as a demon.
// Assumes UNIX type OS. Call very early on the the program. Has built in code to prevent multiple execution
//
// Arguments: None
// Returns: None
static bool bBeenHere = false ; // Already called marker
if (bBeenHere)
{
std::cerr << "Attempt to call Demonize more than once" << std::endl ;
return ;
}
bBeenHere = true ;
pid_t pid ; // Our process ID
pid_t sid ; // Our session ID
// Fork off the parent process
pid = fork();
if (pid < 0)
{
std::cerr << "Parent [" << getpid() << "] begets crap PID [" << pid << "] - exiting" << std::endl ;
exit(EXIT_FAILURE);
}
// If we got a good PID, then we can exit the parent process.
if (pid > 0)
{
std::cout << "Exiting parent [" << getpid() << "] starting [" << pid << "]" << std::endl ;
exit(EXIT_SUCCESS);
}
// Change the file mode mask
umask(0);
// Create a new SID for the child process
sid = setsid();
if (sid < 0)
{
std::cout << "Parent [" << getpid() << "] begets bad SID of [" << sid << "]" << std::endl ;
exit(EXIT_FAILURE);
}
// Close out the standard file descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}