2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /// \addtogroup realmd Realm Daemon
24 #include "Database/DatabaseEnv.h"
25 #include "RealmList.h"
27 #include "Config/ConfigEnv.h"
29 #include "sockets/ListenSocket.h"
30 #include "AuthSocket.h"
31 #include "SystemConfig.h"
33 #include "revision_nr.h"
34 #include "revision_sql.h"
36 #include <openssl/opensslv.h>
37 #include <openssl/crypto.h>
40 #include "ServiceWin32.h"
41 char serviceName
[] = "realmd";
42 char serviceLongName
[] = "MaNGOS realmd service";
43 char serviceDescription
[] = "Massive Network Game Object Server";
45 * -1 - not in service mode
50 int m_ServiceStatus
= -1;
57 bool stopEvent
= false; ///< Setting it to true stops the server
59 DatabaseType loginDatabase
; ///< Accessor to the realm server database
61 /// Print out the usage string for this program on the console.
62 void usage(const char *prog
)
64 sLog
.outString("Usage: \n %s [<options>]\n"
65 " --version print version and exist\n\r"
66 " -c config_file use config_file as configuration file\n\r"
68 " Running as service functions:\n\r"
69 " --service run as service\n\r"
70 " -s install install service\n\r"
71 " -s uninstall uninstall service\n\r"
76 /// Launch the realm server
77 extern int main(int argc
, char **argv
)
79 ///- Command line parsing to get the configuration file name
80 char const* cfg_file
= _REALMD_CONFIG
;
84 if( strcmp(argv
[c
],"-c") == 0)
88 sLog
.outError("Runtime-Error: -c option requires an input argument");
96 if( strcmp(argv
[c
],"--version") == 0)
98 printf("%s\n", _FULLVERSION(REVISION_DATE
,REVISION_TIME
,REVISION_NR
,REVISION_ID
));
106 if( strcmp(argv
[c
],"-s") == 0)
110 sLog
.outError("Runtime-Error: -s option requires an input argument");
114 if( strcmp(argv
[c
],"install") == 0)
116 if (WinServiceInstall())
117 sLog
.outString("Installing service");
120 else if( strcmp(argv
[c
],"uninstall") == 0)
122 if(WinServiceUninstall())
123 sLog
.outString("Uninstalling service");
128 sLog
.outError("Runtime-Error: unsupported option %s",argv
[c
]);
133 if( strcmp(argv
[c
],"--service") == 0)
142 if (!sConfig
.SetSource(cfg_file
))
144 sLog
.outError("Could not find configuration file %s.", cfg_file
);
149 sLog
.outString( "%s [realm-daemon]", _FULLVERSION(REVISION_DATE
,REVISION_TIME
,REVISION_NR
,REVISION_ID
) );
150 sLog
.outString( "<Ctrl-C> to stop.\n" );
151 sLog
.outString("Using configuration file %s.", cfg_file
);
153 ///- Check the version of the configuration file
154 uint32 confVersion
= sConfig
.GetIntDefault("ConfVersion", 0);
155 if (confVersion
< _REALMDCONFVERSION
)
157 sLog
.outError("*****************************************************************************");
158 sLog
.outError(" WARNING: Your realmd.conf version indicates your conf file is out of date!");
159 sLog
.outError(" Please check for updates, as your current default values may cause");
160 sLog
.outError(" strange behavior.");
161 sLog
.outError("*****************************************************************************");
162 clock_t pause
= 3000 + clock();
164 while (pause
> clock()) {}
167 sLog
.outDetail("%s (Library: %s)", OPENSSL_VERSION_TEXT
, SSLeay_version(SSLEAY_VERSION
));
168 if (SSLeay() < 0x009080bfL
)
170 sLog
.outDetail("WARNING: Outdated version of OpenSSL lib. Logins to server may not work!");
171 sLog
.outDetail("WARNING: Minimal required version [OpenSSL 0.9.8k]");
174 /// realmd PID file creation
175 std::string pidfile
= sConfig
.GetStringDefault("PidFile", "");
178 uint32 pid
= CreatePIDFile(pidfile
);
181 sLog
.outError( "Cannot create PID file %s.\n", pidfile
.c_str() );
185 sLog
.outString( "Daemon PID: %u\n", pid
);
188 ///- Initialize the database connection
192 ///- Get the list of realms for the server
193 sRealmList
.Initialize(sConfig
.GetIntDefault("RealmsStateUpdateDelay", 20));
194 if (sRealmList
.size() == 0)
196 sLog
.outError("No valid realms specified.");
200 ///- Launch the listening network socket
201 port_t rmport
= sConfig
.GetIntDefault( "RealmServerPort", DEFAULT_REALMSERVER_PORT
);
202 std::string bind_ip
= sConfig
.GetStringDefault("BindIP", "0.0.0.0");
205 ListenSocket
<AuthSocket
> authListenSocket(h
);
206 if ( authListenSocket
.Bind(bind_ip
.c_str(),rmport
))
208 sLog
.outError( "MaNGOS realmd can not bind to %s:%d",bind_ip
.c_str(), rmport
);
213 //set expired bans to inactive
214 loginDatabase
.Execute("UPDATE account_banned SET active = 0 WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
215 loginDatabase
.Execute("DELETE FROM ip_banned WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
217 h
.Add(&authListenSocket
);
219 ///- Catch termination signals
222 ///- Handle affinity for multiple processors and process priority on Windows
225 HANDLE hProcess
= GetCurrentProcess();
227 uint32 Aff
= sConfig
.GetIntDefault("UseProcessors", 0);
233 if(GetProcessAffinityMask(hProcess
,&appAff
,&sysAff
))
235 ULONG_PTR curAff
= Aff
& appAff
; // remove non accessible processors
239 sLog
.outError("Processors marked in UseProcessors bitmask (hex) %x not accessible for realmd. Accessible processors bitmask (hex): %x",Aff
,appAff
);
243 if(SetProcessAffinityMask(hProcess
,curAff
))
244 sLog
.outString("Using processors (bitmask, hex): %x", curAff
);
246 sLog
.outError("Can't set used processors (hex): %x", curAff
);
252 bool Prio
= sConfig
.GetBoolDefault("ProcessPriority", false);
256 if(SetPriorityClass(hProcess
,HIGH_PRIORITY_CLASS
))
257 sLog
.outString("realmd process priority class set to HIGH");
259 sLog
.outError("ERROR: Can't set realmd process priority class.");
265 // maximum counter for next ping
266 uint32 numLoops
= (sConfig
.GetIntDefault( "MaxPingTime", 30 ) * (MINUTE
* 1000000 / 100000));
267 uint32 loopCounter
= 0;
269 ///- Wait for termination signal
275 if( (++loopCounter
) == numLoops
)
278 sLog
.outDetail("Ping MySQL to keep connection alive");
279 delete loginDatabase
.Query("SELECT 1 FROM realmlist LIMIT 1");
282 if (m_ServiceStatus
== 0) stopEvent
= true;
283 while (m_ServiceStatus
== 2) Sleep(1000);
287 ///- Wait for the delay thread to exit
288 loginDatabase
.HaltDelayThread();
290 ///- Remove signal handling before leaving
293 sLog
.outString( "Halting process..." );
297 /// Handle termination signals
298 /** Put the global variable stopEvent to 'true' if a termination signal is caught **/
317 /// Initialize connection to the database
320 std::string dbstring
= sConfig
.GetStringDefault("LoginDatabaseInfo", "");
323 sLog
.outError("Database not specified");
327 sLog
.outString("Database: %s", dbstring
.c_str() );
328 if(!loginDatabase
.Initialize(dbstring
.c_str()))
330 sLog
.outError("Cannot connect to database");
334 if(!loginDatabase
.CheckRequiredField("realmd_db_version",REVISION_DB_REALMD
))
336 ///- Wait for already started DB delay threads to end
337 loginDatabase
.HaltDelayThread();
344 /// Define hook 'OnSignal' for all termination signals
347 signal(SIGINT
, OnSignal
);
348 signal(SIGTERM
, OnSignal
);
350 signal(SIGBREAK
, OnSignal
);
354 /// Unhook the signals before leaving