My Libmongoose based Server runs fine when on Windows in terminal as well as a Service.
Now, I am porting it to Linux. It works perfect when run in the terminal. Now, I wanted to run it as a Daemon - it works but to my surprise it invokes all the functions twice. I checked the PID only one process is running. It's driving me crazy. The daemonizing code is as follows:
if(CommandArgs.at("-d") == "true")
{
#if __linux
pid_t pid, sid;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if(start_server() == false)
{
exit(EXIT_FAILURE);
}
else
{
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
pause();
}
//exit(EXIT_SUCCESS);
return 0;
#endif
}
Now, after i invoke the start_server()
function -functions inside are invoked twice:
static bool start_server()
{
try{
// invoked once as expected
MyLogger(2, "Info: Starting Server ...");
#ifdef _WIN32
mutex = CreateMutex( NULL, FALSE, NULL);
#else
pthread_mutex_init(&mutex, NULL);
#endif
for(int i = 0; i < serverConf.totalThreads; i++)
{
server[i] = mg_create_server(NULL, event_handler);
if(i==0)
{
const char * error_msg = mg_set_option(server[0], "listening_port", serverConf.port.c_str());
if (error_msg != NULL) {
MyLogger(1,"Error: Cannot bind to the port:",serverConf.port.c_str());
return false;
}
}
else
{
mg_copy_listeners(server[0], server[i]);
}
}
// Now all the logging happening twice
ServerStarted = true;
for(int i = 0; i < serverConf.totalThreads; i++)
{
mg_start_thread(serve, server[i]);
}
// server1 goes to separate thread, server 2 runs in main thread.
// IMPORTANT: NEVER LET DIFFERENT THREADS HANDLE THE SAME SERVER.
MyLogger(2, "Info: Server Started!"); // why was i logged twice
MyLogger(2, "Info: Listening on port:", mg_get_option(server[0], "listening_port")); // why was i logged twice
}
catch(std::exception& e)
{
return false;
}
return true;
}
Now the logs are:
2015-04-27, 23:06:39 Info: Starting Server..
2015-04-27, 23:06:41 Info: Server Started!
2015-04-27, 23:06:41 Info: Server Started!
2015-04-27, 23:06:41 Info: Listening on port: 8091
2015-04-27, 23:06:41 Info: Listening on port: 8091
void MyLogger(int level, const char* msg1)
{
if(level > LogLevel)
{
return;
}
string time = currentDateTime();
std::ofstream out(LogFilePath.c_str(), std::fstream::app);
if(silentMode == false)
{
cout<<"\n"<<time<<" "<<msg1<<"\n";
}
out<<"\n"<<time<<" "<<msg1<<"\n";
}
It seems likely that the messages are being duplicated by function MyLogger()
. In the event that silentMode == false
, it will print the same message to two streams, but they may in fact be the same stream, or else the output from both may end up redirected to the same place.
That could start happening where you say it does if one of the functions mg_create_server()
, mg_set_option()
, or mg_copy_listeners()
modifies out
, sets silentMode
to false
, or otherwise makes a change that would produce such a result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.