Make daemonize() work on windows
This commit is contained in:
parent
f2831c0426
commit
736052b0ee
@ -1,4 +1,11 @@
|
|||||||
#include "daemonize.h"
|
#include "daemonize.h"
|
||||||
|
#include "../logging/logging.h"
|
||||||
|
|
||||||
|
using namespace cpputils::logging;
|
||||||
|
|
||||||
|
//TODO Test daemonize()
|
||||||
|
|
||||||
|
#if !defined(_MSC_VER)
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
@ -10,43 +17,56 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../logging/logging.h"
|
|
||||||
|
|
||||||
using namespace cpputils::logging;
|
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
|
|
||||||
//TODO Test daemonize()
|
void daemonize() {
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid < 0) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (pid > 0) {
|
||||||
|
//We're the parent process. Exit.
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
void daemonize() {
|
// We're the child process.
|
||||||
pid_t pid = fork();
|
umask(0);
|
||||||
if (pid < 0) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (pid > 0) {
|
|
||||||
//We're the parent process. Exit.
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
// We're the child process.
|
// Create a new SID for the child process
|
||||||
umask(0);
|
pid_t sid = setsid();
|
||||||
|
if (sid < 0) {
|
||||||
|
LOG(ERR, "Failed to get SID for daemon process");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new SID for the child process
|
// Change the current working directory to a directory that's always existin
|
||||||
pid_t sid = setsid();
|
if ((chdir("/")) < 0) {
|
||||||
if (sid < 0) {
|
LOG(ERR, "Failed to change working directory for daemon process");
|
||||||
LOG(ERR, "Failed to get SID for daemon process");
|
exit(EXIT_FAILURE);
|
||||||
exit(EXIT_FAILURE);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Change the current working directory to a directory that's always existin
|
// Close out the standard file descriptors. The process can't use them anyhow.
|
||||||
if ((chdir("/")) < 0) {
|
close(STDIN_FILENO);
|
||||||
LOG(ERR, "Failed to change working directory for daemon process");
|
close(STDOUT_FILENO);
|
||||||
exit(EXIT_FAILURE);
|
close(STDERR_FILENO);
|
||||||
}
|
};
|
||||||
|
|
||||||
// Close out the standard file descriptors. The process can't use them anyhow.
|
|
||||||
close(STDIN_FILENO);
|
|
||||||
close(STDOUT_FILENO);
|
|
||||||
close(STDERR_FILENO);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
|
||||||
|
namespace cpputils {
|
||||||
|
|
||||||
|
void daemonize() {
|
||||||
|
LOG(INFO, "Process started in the background. You can close this console window now.");
|
||||||
|
if (!FreeConsole()) {
|
||||||
|
LOG(ERR, "Failed to call FreeConsole()");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user