Make daemonize() work on windows

This commit is contained in:
Sebastian Messmer 2018-05-19 07:59:24 -07:00
parent f2831c0426
commit 736052b0ee

View File

@ -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,14 +17,9 @@
#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() { void daemonize() {
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) { if (pid < 0) {
@ -50,3 +52,21 @@ namespace cpputils {
close(STDERR_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