1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | /// \addtogroup realmd Realm Daemon |
---|
22 | /// @{ |
---|
23 | /// \file |
---|
24 | |
---|
25 | #include "Common.h" |
---|
26 | #include "Database/DatabaseEnv.h" |
---|
27 | #include "RealmList.h" |
---|
28 | |
---|
29 | #include "Config/ConfigEnv.h" |
---|
30 | #include "Log.h" |
---|
31 | #include "sockets/ListenSocket.h" |
---|
32 | #include "AuthSocket.h" |
---|
33 | #include "SystemConfig.h" |
---|
34 | #include "Util.h" |
---|
35 | |
---|
36 | // Format is YYYYMMDDRR where RR is the change in the conf file |
---|
37 | // for that day. |
---|
38 | #ifndef _REALMDCONFVERSION |
---|
39 | # define _REALMDCONFVERSION 2007062001 |
---|
40 | #endif |
---|
41 | |
---|
42 | #ifndef _TRINITY_REALM_CONFIG |
---|
43 | # define _TRINITY_REALM_CONFIG "trinityrealm.conf" |
---|
44 | #endif //_TRINITY_REALM_CONFIG |
---|
45 | |
---|
46 | #ifdef WIN32 |
---|
47 | #include "ServiceWin32.h" |
---|
48 | char serviceName[] = "realmd"; |
---|
49 | char serviceLongName[] = "Trinity realm service"; |
---|
50 | char serviceDescription[] = "Massive Network Game Object Server"; |
---|
51 | /* |
---|
52 | * -1 - not in service mode |
---|
53 | * 0 - stopped |
---|
54 | * 1 - running |
---|
55 | * 2 - paused |
---|
56 | */ |
---|
57 | int m_ServiceStatus = -1; |
---|
58 | #endif |
---|
59 | |
---|
60 | bool StartDB(std::string &dbstring); |
---|
61 | void UnhookSignals(); |
---|
62 | void HookSignals(); |
---|
63 | |
---|
64 | bool stopEvent = false; ///< Setting it to true stops the server |
---|
65 | RealmList m_realmList; ///< Holds the list of realms for this server |
---|
66 | |
---|
67 | DatabaseType dbRealmServer; ///< Accessor to the realm server database |
---|
68 | |
---|
69 | /// Print out the usage string for this program on the console. |
---|
70 | void usage(const char *prog) |
---|
71 | { |
---|
72 | sLog.outString("Usage: \n %s [<options>]\n" |
---|
73 | " -c config_file use config_file as configuration file\n\r" |
---|
74 | #ifdef WIN32 |
---|
75 | " Running as service functions:\n\r" |
---|
76 | " --service run as service\n\r" |
---|
77 | " -s install install service\n\r" |
---|
78 | " -s uninstall uninstall service\n\r" |
---|
79 | #endif |
---|
80 | ,prog); |
---|
81 | } |
---|
82 | |
---|
83 | /// Launch the realm server |
---|
84 | extern int main(int argc, char **argv) |
---|
85 | { |
---|
86 | ///- Command line parsing to get the configuration file name |
---|
87 | char const* cfg_file = _TRINITY_REALM_CONFIG; |
---|
88 | int c=1; |
---|
89 | while( c < argc ) |
---|
90 | { |
---|
91 | if( strcmp(argv[c],"-c") == 0) |
---|
92 | { |
---|
93 | if( ++c >= argc ) |
---|
94 | { |
---|
95 | sLog.outError("Runtime-Error: -c option requires an input argument"); |
---|
96 | usage(argv[0]); |
---|
97 | return 1; |
---|
98 | } |
---|
99 | else |
---|
100 | cfg_file = argv[c]; |
---|
101 | } |
---|
102 | |
---|
103 | #ifdef WIN32 |
---|
104 | //////////// |
---|
105 | //Services// |
---|
106 | //////////// |
---|
107 | if( strcmp(argv[c],"-s") == 0) |
---|
108 | { |
---|
109 | if( ++c >= argc ) |
---|
110 | { |
---|
111 | sLog.outError("Runtime-Error: -s option requires an input argument"); |
---|
112 | usage(argv[0]); |
---|
113 | return 1; |
---|
114 | } |
---|
115 | if( strcmp(argv[c],"install") == 0) |
---|
116 | { |
---|
117 | if (WinServiceInstall()) |
---|
118 | sLog.outString("Installing service"); |
---|
119 | return 1; |
---|
120 | } |
---|
121 | else if( strcmp(argv[c],"uninstall") == 0) |
---|
122 | { |
---|
123 | if(WinServiceUninstall()) |
---|
124 | sLog.outString("Uninstalling service"); |
---|
125 | return 1; |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | sLog.outError("Runtime-Error: unsupported option %s",argv[c]); |
---|
130 | usage(argv[0]); |
---|
131 | return 1; |
---|
132 | } |
---|
133 | } |
---|
134 | if( strcmp(argv[c],"--service") == 0) |
---|
135 | { |
---|
136 | WinServiceRun(); |
---|
137 | } |
---|
138 | //// |
---|
139 | #endif |
---|
140 | ++c; |
---|
141 | } |
---|
142 | |
---|
143 | if (!sConfig.SetSource(cfg_file)) |
---|
144 | { |
---|
145 | sLog.outError("Could not find configuration file %s.", cfg_file); |
---|
146 | return 1; |
---|
147 | } |
---|
148 | sLog.outString("Using configuration file %s.", cfg_file); |
---|
149 | |
---|
150 | ///- Check the version of the configuration file |
---|
151 | uint32 confVersion = sConfig.GetIntDefault("ConfVersion", 0); |
---|
152 | if (confVersion < _REALMDCONFVERSION) |
---|
153 | { |
---|
154 | sLog.outError("*****************************************************************************"); |
---|
155 | sLog.outError(" WARNING: Your trinityrealm.conf version indicates your conf file is out of date!"); |
---|
156 | sLog.outError(" Please check for updates, as your current default values may cause"); |
---|
157 | sLog.outError(" strange behavior."); |
---|
158 | sLog.outError("*****************************************************************************"); |
---|
159 | clock_t pause = 3000 + clock(); |
---|
160 | |
---|
161 | while (pause > clock()) {} |
---|
162 | } |
---|
163 | |
---|
164 | sLog.outString( "%s (realm-daemon)", _FULLVERSION ); |
---|
165 | sLog.outString( "<Ctrl-C> to stop.\n" ); |
---|
166 | |
---|
167 | /// realmd PID file creation |
---|
168 | std::string pidfile = sConfig.GetStringDefault("PidFile", ""); |
---|
169 | if(!pidfile.empty()) |
---|
170 | { |
---|
171 | uint32 pid = CreatePIDFile(pidfile); |
---|
172 | if( !pid ) |
---|
173 | { |
---|
174 | sLog.outError( "Cannot create PID file %s.\n", pidfile.c_str() ); |
---|
175 | return 1; |
---|
176 | } |
---|
177 | |
---|
178 | sLog.outString( "Daemon PID: %u\n", pid ); |
---|
179 | } |
---|
180 | |
---|
181 | ///- Initialize the database connection |
---|
182 | std::string dbstring; |
---|
183 | if(!StartDB(dbstring)) |
---|
184 | return 1; |
---|
185 | |
---|
186 | ///- Get the list of realms for the server |
---|
187 | m_realmList.Initialize(sConfig.GetIntDefault("RealmsStateUpdateDelay", 20)); |
---|
188 | if (m_realmList.size() == 0) |
---|
189 | { |
---|
190 | sLog.outError("No valid realms specified."); |
---|
191 | return 1; |
---|
192 | } |
---|
193 | |
---|
194 | ///- Launch the listening network socket |
---|
195 | port_t rmport = sConfig.GetIntDefault( "RealmServerPort", DEFAULT_REALMSERVER_PORT ); |
---|
196 | std::string bind_ip = sConfig.GetStringDefault("BindIP", "0.0.0.0"); |
---|
197 | |
---|
198 | SocketHandler h; |
---|
199 | ListenSocket<AuthSocket> authListenSocket(h); |
---|
200 | if ( authListenSocket.Bind(bind_ip.c_str(),rmport)) |
---|
201 | { |
---|
202 | sLog.outError( "Trinity realm can not bind to %s:%d",bind_ip.c_str(), rmport ); |
---|
203 | return 1; |
---|
204 | } |
---|
205 | |
---|
206 | h.Add(&authListenSocket); |
---|
207 | |
---|
208 | ///- Catch termination signals |
---|
209 | HookSignals(); |
---|
210 | |
---|
211 | ///- Handle affinity for multiple processors and process priority on Windows |
---|
212 | #ifdef WIN32 |
---|
213 | { |
---|
214 | HANDLE hProcess = GetCurrentProcess(); |
---|
215 | |
---|
216 | uint32 Aff = sConfig.GetIntDefault("UseProcessors", 0); |
---|
217 | if(Aff > 0) |
---|
218 | { |
---|
219 | ULONG_PTR appAff; |
---|
220 | ULONG_PTR sysAff; |
---|
221 | |
---|
222 | if(GetProcessAffinityMask(hProcess,&appAff,&sysAff)) |
---|
223 | { |
---|
224 | ULONG_PTR curAff = Aff & appAff; // remove non accessible processors |
---|
225 | |
---|
226 | if(!curAff ) |
---|
227 | { |
---|
228 | sLog.outError("Processors marked in UseProcessors bitmask (hex) %x not accessible for realmd. Accessible processors bitmask (hex): %x",Aff,appAff); |
---|
229 | } |
---|
230 | else |
---|
231 | { |
---|
232 | if(SetProcessAffinityMask(hProcess,curAff)) |
---|
233 | sLog.outString("Using processors (bitmask, hex): %x", curAff); |
---|
234 | else |
---|
235 | sLog.outError("Can't set used processors (hex): %x", curAff); |
---|
236 | } |
---|
237 | } |
---|
238 | sLog.outString(); |
---|
239 | } |
---|
240 | |
---|
241 | bool Prio = sConfig.GetBoolDefault("ProcessPriority", false); |
---|
242 | |
---|
243 | if(Prio) |
---|
244 | { |
---|
245 | if(SetPriorityClass(hProcess,HIGH_PRIORITY_CLASS)) |
---|
246 | sLog.outString("TrinityRealm process priority class set to HIGH"); |
---|
247 | else |
---|
248 | sLog.outError("ERROR: Can't set realmd process priority class."); |
---|
249 | sLog.outString(); |
---|
250 | } |
---|
251 | } |
---|
252 | #endif |
---|
253 | |
---|
254 | // maximum counter for next ping |
---|
255 | uint32 numLoops = (sConfig.GetIntDefault( "MaxPingTime", 30 ) * (MINUTE * 1000000 / 100000)); |
---|
256 | uint32 loopCounter = 0; |
---|
257 | |
---|
258 | ///- Wait for termination signal |
---|
259 | while (!stopEvent) |
---|
260 | { |
---|
261 | |
---|
262 | h.Select(0, 100000); |
---|
263 | |
---|
264 | if( (++loopCounter) == numLoops ) |
---|
265 | { |
---|
266 | loopCounter = 0; |
---|
267 | sLog.outDetail("Ping MySQL to keep connection alive"); |
---|
268 | delete dbRealmServer.Query("SELECT 1 FROM realmlist LIMIT 1"); |
---|
269 | } |
---|
270 | #ifdef WIN32 |
---|
271 | if (m_ServiceStatus == 0) stopEvent = true; |
---|
272 | while (m_ServiceStatus == 2) Sleep(1000); |
---|
273 | #endif |
---|
274 | } |
---|
275 | |
---|
276 | ///- Wait for the delay thread to exit |
---|
277 | dbRealmServer.HaltDelayThread(); |
---|
278 | |
---|
279 | ///- Remove signal handling before leaving |
---|
280 | UnhookSignals(); |
---|
281 | |
---|
282 | sLog.outString( "Halting process..." ); |
---|
283 | return 0; |
---|
284 | } |
---|
285 | |
---|
286 | /// Handle termination signals |
---|
287 | /** Put the global variable stopEvent to 'true' if a termination signal is caught **/ |
---|
288 | void OnSignal(int s) |
---|
289 | { |
---|
290 | switch (s) |
---|
291 | { |
---|
292 | case SIGINT: |
---|
293 | case SIGTERM: |
---|
294 | stopEvent = true; |
---|
295 | break; |
---|
296 | #ifdef _WIN32 |
---|
297 | case SIGBREAK: |
---|
298 | stopEvent = true; |
---|
299 | break; |
---|
300 | #endif |
---|
301 | } |
---|
302 | |
---|
303 | signal(s, OnSignal); |
---|
304 | } |
---|
305 | |
---|
306 | /// Initialize connection to the database |
---|
307 | bool StartDB(std::string &dbstring) |
---|
308 | { |
---|
309 | if(!sConfig.GetString("LoginDatabaseInfo", &dbstring)) |
---|
310 | { |
---|
311 | sLog.outError("Database not specified"); |
---|
312 | return false; |
---|
313 | } |
---|
314 | |
---|
315 | sLog.outString("Database: %s", dbstring.c_str() ); |
---|
316 | if(!dbRealmServer.Initialize(dbstring.c_str())) |
---|
317 | { |
---|
318 | sLog.outError("Cannot connect to database"); |
---|
319 | return false; |
---|
320 | } |
---|
321 | |
---|
322 | return true; |
---|
323 | } |
---|
324 | |
---|
325 | /// Define hook 'OnSignal' for all termination signals |
---|
326 | void HookSignals() |
---|
327 | { |
---|
328 | signal(SIGINT, OnSignal); |
---|
329 | signal(SIGTERM, OnSignal); |
---|
330 | #ifdef _WIN32 |
---|
331 | signal(SIGBREAK, OnSignal); |
---|
332 | #endif |
---|
333 | } |
---|
334 | |
---|
335 | /// Unhook the signals before leaving |
---|
336 | void UnhookSignals() |
---|
337 | { |
---|
338 | signal(SIGINT, 0); |
---|
339 | signal(SIGTERM, 0); |
---|
340 | #ifdef _WIN32 |
---|
341 | signal(SIGBREAK, 0); |
---|
342 | #endif |
---|
343 | |
---|
344 | } |
---|
345 | |
---|
346 | /// @} |
---|