Advertisement
Guest User

Untitled

a guest
Apr 15th, 2011
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.86 KB | None | 0 0
  1. diff --git a/src/mangosd/Main.cpp b/src/mangosd/Main.cpp
  2. index b412929..0cc3051 100644
  3. --- a/src/mangosd/Main.cpp
  4. +++ b/src/mangosd/Main.cpp
  5. @@ -46,6 +46,8 @@ char serviceDescription[] = "Massive Network Game Object Server";
  6.   *  2 - paused
  7.   */
  8.  int m_ServiceStatus = -1;
  9. +#else
  10. +#include "PosixDaemon.h"
  11.  #endif
  12.  
  13.  DatabaseType WorldDatabase;                                 ///< Accessor to the world database
  14. @@ -65,6 +67,10 @@ void usage(const char *prog)
  15.          "    -s run                   run as service\n\r"
  16.          "    -s install               install service\n\r"
  17.          "    -s uninstall             uninstall service\n\r"
  18. +        #else
  19. +        "    Running as daemon functions:\n\r"
  20. +        "    -s run                   run as daemon\n\r"
  21. +        "    -s stop                  stop daemon\n\r"
  22.          #endif
  23.          ,prog);
  24.  }
  25. @@ -80,15 +86,19 @@ extern int main(int argc, char **argv)
  26.      ///- Command line parsing
  27.      char const* cfg_file = _MANGOSD_CONFIG;
  28.  
  29. -#ifdef WIN32
  30. +
  31.      char const *options = ":c:s:";
  32. -#else
  33. -    char const *options = ":c:";
  34. -#endif
  35.  
  36.      ACE_Get_Opt cmd_opts(argc, argv, options);
  37.      cmd_opts.long_option("version", 'v');
  38.  
  39. +    if (!sConfig.SetSource(cfg_file))
  40. +    {
  41. +        sLog.outError("Could not find configuration file %s.", cfg_file);
  42. +        Log::WaitBeforeContinueIfNeed();
  43. +        return 1;
  44. +    }
  45. +
  46.      int option;
  47.      while ((option = cmd_opts()) != EOF)
  48.      {
  49. @@ -100,9 +110,9 @@ extern int main(int argc, char **argv)
  50.              case 'v':
  51.                  printf("%s\n", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID));
  52.                  return 0;
  53. -#ifdef WIN32
  54.              case 's':
  55. -            {
  56. +                        {
  57. +#ifdef WIN32
  58.                  const char *mode = cmd_opts.opt_arg();
  59.  
  60.                  if (!strcmp(mode, "install"))
  61. @@ -127,8 +137,22 @@ extern int main(int argc, char **argv)
  62.                      return 1;
  63.                  }
  64.                  break;
  65. -            }
  66. +#else
  67. +                const char *mode = cmd_opts.opt_arg();
  68. +                if (!strcmp(mode, "run"))
  69. +                    startDaemon(120);
  70. +                else if (!strcmp(mode, "stop"))
  71. +                    stopDaemon();
  72. +                else
  73. +                {
  74. +                    sLog.outError("Runtime-Error: -%c unsupported argument %s", cmd_opts.opt_opt(), mode);
  75. +                    usage(argv[0]);
  76. +                    Log::WaitBeforeContinueIfNeed();
  77. +                    return 1;
  78. +                }
  79.  #endif
  80. +            }
  81. +            break;
  82.              case ':':
  83.                  sLog.outError("Runtime-Error: -%c option requires an input argument", cmd_opts.opt_opt());
  84.                  usage(argv[0]);
  85. @@ -142,13 +166,6 @@ extern int main(int argc, char **argv)
  86.          }
  87.      }
  88.  
  89. -    if (!sConfig.SetSource(cfg_file))
  90. -    {
  91. -        sLog.outError("Could not find configuration file %s.", cfg_file);
  92. -        Log::WaitBeforeContinueIfNeed();
  93. -        return 1;
  94. -    }
  95. -
  96.      sLog.outString( "%s [world-daemon]", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID) );
  97.      sLog.outString( "<Ctrl-C> to stop.\n\n" );
  98.  
  99. diff --git a/src/mangosd/Master.cpp b/src/mangosd/Master.cpp
  100. index bb3b38c..eb9863c 100644
  101. --- a/src/mangosd/Master.cpp
  102. +++ b/src/mangosd/Master.cpp
  103. @@ -20,6 +20,10 @@
  104.      \ingroup mangosd
  105.  */
  106.  
  107. +#ifndef WIN32
  108. +    #include "PosixDaemon.h"
  109. +#endif
  110. +
  111.  #include "WorldSocketMgr.h"
  112.  #include "Common.h"
  113.  #include "Master.h"
  114. @@ -197,6 +201,9 @@ int Master::Run()
  115.      ///- Initialize the World
  116.      sWorld.SetInitialWorldSettings();
  117.  
  118. +    #ifndef WIN32
  119. +    detachDaemon();
  120. +    #endif
  121.      //server loaded successfully => enable async DB requests
  122.      //this is done to forbid any async transactions during server startup!
  123.      CharacterDatabase.AllowAsyncTransactions();
  124. diff --git a/src/realmd/Main.cpp b/src/realmd/Main.cpp
  125. index 1365992..08356db 100644
  126. --- a/src/realmd/Main.cpp
  127. +++ b/src/realmd/Main.cpp
  128. @@ -54,6 +54,8 @@ char serviceDescription[] = "Massive Network Game Object Server";
  129.   *  2 - paused
  130.   */
  131.  int m_ServiceStatus = -1;
  132. +#else
  133. +#include "PosixDaemon.h"
  134.  #endif
  135.  
  136.  bool StartDB();
  137. @@ -75,6 +77,10 @@ void usage(const char *prog)
  138.          "    -s run                   run as service\n\r"
  139.          "    -s install               install service\n\r"
  140.          "    -s uninstall             uninstall service\n\r"
  141. +        #else
  142. +        "    Running as daemon functions:\n\r"
  143. +        "    -s run                   run as daemon\n\r"
  144. +        "    -s stop                  stop daemon\n\r"
  145.          #endif
  146.          ,prog);
  147.  }
  148. @@ -85,16 +91,20 @@ extern int main(int argc, char **argv)
  149.      ///- Command line parsing
  150.      char const* cfg_file = _REALMD_CONFIG;
  151.  
  152. -#ifdef WIN32
  153.      char const *options = ":c:s:";
  154. -#else
  155. -    char const *options = ":c:";
  156. -#endif
  157.  
  158.      ACE_Get_Opt cmd_opts(argc, argv, options);
  159.      cmd_opts.long_option("version", 'v');
  160.  
  161.      int option;
  162. +
  163. +    if (!sConfig.SetSource(cfg_file))
  164. +    {
  165. +        sLog.outError("Could not find configuration file %s.", cfg_file);
  166. +        Log::WaitBeforeContinueIfNeed();
  167. +        return 1;
  168. +    }
  169. +
  170.      while ((option = cmd_opts()) != EOF)
  171.      {
  172.          switch (option)
  173. @@ -105,9 +115,10 @@ extern int main(int argc, char **argv)
  174.              case 'v':
  175.                  printf("%s\n", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID));
  176.                  return 0;
  177. -#ifdef WIN32
  178. +
  179.              case 's':
  180.              {
  181. +#ifdef WIN32
  182.                  const char *mode = cmd_opts.opt_arg();
  183.  
  184.                  if (!strcmp(mode, "install"))
  185. @@ -131,9 +142,22 @@ extern int main(int argc, char **argv)
  186.                      Log::WaitBeforeContinueIfNeed();
  187.                      return 1;
  188.                  }
  189. +#else
  190. +                const char *mode = cmd_opts.opt_arg();
  191. +                if (!strcmp(mode, "run"))
  192. +                    startDaemon();
  193. +                else if (!strcmp(mode, "stop"))
  194. +                    stopDaemon();
  195. +                else
  196. +                {
  197. +                    sLog.outError("Runtime-Error: -%c unsupported argument %s", cmd_opts.opt_opt(), mode);
  198. +                    usage(argv[0]);
  199. +                    Log::WaitBeforeContinueIfNeed();
  200. +                    return 1;
  201. +                }
  202. +#endif
  203.                  break;
  204.              }
  205. -#endif
  206.              case ':':
  207.                  sLog.outError("Runtime-Error: -%c option requires an input argument", cmd_opts.opt_opt());
  208.                  usage(argv[0]);
  209. @@ -147,12 +171,6 @@ extern int main(int argc, char **argv)
  210.          }
  211.      }
  212.  
  213. -    if (!sConfig.SetSource(cfg_file))
  214. -    {
  215. -        sLog.outError("Could not find configuration file %s.", cfg_file);
  216. -        Log::WaitBeforeContinueIfNeed();
  217. -        return 1;
  218. -    }
  219.      sLog.Initialize();
  220.  
  221.      sLog.outString( "%s [realm-daemon]", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID) );
  222. @@ -294,6 +312,9 @@ extern int main(int argc, char **argv)
  223.      uint32 numLoops = (sConfig.GetIntDefault( "MaxPingTime", 30 ) * (MINUTE * 1000000 / 100000));
  224.      uint32 loopCounter = 0;
  225.  
  226. +    #ifndef WIN32
  227. +    detachDaemon();
  228. +    #endif
  229.      ///- Wait for termination signal
  230.      while (!stopEvent)
  231.      {
  232. diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt
  233. index 5b04b1c..6f0ac01 100644
  234. --- a/src/shared/CMakeLists.txt
  235. +++ b/src/shared/CMakeLists.txt
  236. @@ -20,7 +20,12 @@
  237.  file(GLOB_RECURSE shared_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.h)
  238.  
  239.  # Exclude Win32 files
  240. -if(NOT WIN32)
  241. +if(WIN32)
  242. +  list(REMOVE_ITEM shared_SRCS
  243. +    PosixDaemon.h
  244. +    PosixDaemon.cpp
  245. +  )
  246. +else()
  247.    list(REMOVE_ITEM shared_SRCS
  248.      WheatyExceptionReport.cpp
  249.      WheatyExceptionReport.h
  250. diff --git a/src/shared/PosixDaemon.cpp b/src/shared/PosixDaemon.cpp
  251. new file mode 100644
  252. index 0000000..022cb40
  253. --- /dev/null
  254. +++ b/src/shared/PosixDaemon.cpp
  255. @@ -0,0 +1,136 @@
  256. +/*
  257. + * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
  258. + *
  259. + * This program is free software; you can redistribute it and/or
  260. + * modify it under the terms of the GNU General Public License
  261. + * as published by the Free Software Foundation; either version 2
  262. + * of the License, or (at your option) any later version.
  263. + *
  264. + * This program is distributed in the hope that it will be useful,
  265. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  266. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  267. + * GNU General Public License for more details.
  268. + *
  269. + * You should have received a copy of the GNU General Public License
  270. + * along with this program; if not, write to the Free Software
  271. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  272. + */
  273. +
  274. +#include "Config/Config.h"
  275. +#include "PosixDaemon.h"
  276. +#include <cstdio>
  277. +#include <iostream>
  278. +#include <fstream>
  279. +
  280. +pid_t parent_pid = 0, sid = 0;
  281. +
  282. +void daemonSignal(int s)
  283. +{
  284. +
  285. +    if (getpid() != parent_pid)
  286. +    {
  287. +        return;
  288. +    }
  289. +
  290. +    if (s == SIGUSR1)
  291. +    {
  292. +        exit(EXIT_SUCCESS);
  293. +    }
  294. +
  295. +    if (sid) {
  296. +        kill(sid, s);
  297. +    }
  298. +
  299. +    exit(EXIT_FAILURE);
  300. +}
  301. +
  302. +
  303. +void startDaemon(uint32_t timeout)
  304. +{
  305. +    parent_pid = getpid();
  306. +    pid_t pid;
  307. +
  308. +    signal(SIGUSR1, daemonSignal);
  309. +    signal(SIGINT, daemonSignal);
  310. +    signal(SIGTERM, daemonSignal);
  311. +    signal(SIGALRM, daemonSignal);
  312. +
  313. +    sid = pid = fork();
  314. +
  315. +    if (pid < 0) {
  316. +      exit(EXIT_FAILURE);
  317. +    }
  318. +
  319. +    if (pid > 0) {
  320. +        alarm(timeout);
  321. +        pause();
  322. +        exit(EXIT_FAILURE);
  323. +    }
  324. +
  325. +    umask(0);
  326. +
  327. +    sid = setsid();
  328. +
  329. +    if (sid < 0) {
  330. +      exit(EXIT_FAILURE);
  331. +    }
  332. +
  333. +    if ((chdir("/")) < 0) {
  334. +      exit(EXIT_FAILURE);
  335. +    }
  336. +
  337. +    freopen("/dev/null", "rt", stdin);
  338. +    freopen("/dev/null", "wt", stdout);
  339. +    freopen("/dev/null", "wt", stderr);
  340. +}
  341. +
  342. +void stopDaemon()
  343. +{
  344. +    std::string pidfile = sConfig.GetStringDefault("PidFile", "");
  345. +    if(!pidfile.empty())
  346. +    {
  347. +        std::fstream pf(pidfile.c_str(), std::ios::in);
  348. +        uint32_t pid = 0;
  349. +        pf >> pid;
  350. +        if (kill(pid, SIGINT) < 0)
  351. +        {
  352. +            std::cerr << "Unable to stop daemon" << std::endl;
  353. +            exit(EXIT_FAILURE);
  354. +        }
  355. +        pf.close();
  356. +    }
  357. +    else
  358. +    {
  359. +        std::cerr << "No pid file specified" << std::endl;
  360. +    }
  361. +
  362. +    exit(EXIT_SUCCESS);
  363. +}
  364. +
  365. +void detachDaemon()
  366. +{
  367. +    if (parent_pid)
  368. +    {
  369. +        kill(parent_pid, SIGUSR1);
  370. +    }
  371. +}
  372. +
  373. +
  374. +void exitDaemon()
  375. +{
  376. +    if (parent_pid && parent_pid != getpid())
  377. +    {
  378. +        kill(parent_pid, SIGTERM);
  379. +    }
  380. +}
  381. +
  382. +
  383. +struct WatchDog
  384. +{
  385. +    ~WatchDog()
  386. +    {
  387. +        exitDaemon();
  388. +    }
  389. +};
  390. +
  391. +WatchDog dog;
  392. diff --git a/src/shared/PosixDaemon.h b/src/shared/PosixDaemon.h
  393. new file mode 100644
  394. index 0000000..26fb7ad
  395. --- /dev/null
  396. +++ b/src/shared/PosixDaemon.h
  397. @@ -0,0 +1,24 @@
  398. +/*
  399. + * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
  400. + *
  401. + * This program is free software; you can redistribute it and/or
  402. + * modify it under the terms of the GNU General Public License
  403. + * as published by the Free Software Foundation; either version 2
  404. + * of the License, or (at your option) any later version.
  405. + *
  406. + * This program is distributed in the hope that it will be useful,
  407. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  408. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  409. + * GNU General Public License for more details.
  410. + *
  411. + * You should have received a copy of the GNU General Public License
  412. + * along with this program; if not, write to the Free Software
  413. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  414. + */
  415. +
  416. +#include "Common.h"
  417. +#include "Log.h"
  418. +
  419. +void startDaemon(uint32_t timeout = 10);
  420. +void stopDaemon();
  421. +void detachDaemon();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement