Make wxApp as a daemon in Linux

Thanks Devin Watson.

#include <wx/app.h>

class testwxdApp : public wxAppConsole
{
public:
virtual bool OnInit();

private:
void MakeDaemon();
};

IMPLEMENT_APP(testwxdApp);

bool testwxdApp::OnInit()
{
// Make a daemon at very first
MakeDaemon();

// Daemon-specific initialization goes here

return true;
}

void testwxdApp::MakeDaemon()
{
// Our process ID and Session ID
pid_t pid, sid;

// Fork off the parent process
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
// If we got a good PID, then we can exit the parent process
if (pid > 0) {
exit(EXIT_SUCCESS);
}

// Change the file mode mask
umask(0);

// Some logs may be opened here

// Create a new SID for the child process
sid = setsid();
if (sid < 0) {
// Log the failure
exit(EXIT_FAILURE);
}

// Change the current working directory
if ((chdir("/")) < 0) {
// Log the failure
exit(EXIT_FAILURE);
}

// Close out the standard file descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}

posted @ 2011-06-22 17:31  ALLI Look for Lost Idylls  阅读(287)  评论(0编辑  收藏  举报