espp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #pragma warning (disable : 4786)
  15. #if defined(_WIN32) && defined(_DEBUG)
  16. //#include <vld.h>
  17. #endif
  18. //Jlib
  19. #include "jliball.hpp"
  20. //CRT / OS
  21. #ifndef _WIN32
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <sys/resource.h>
  25. #include <sys/time.h>
  26. #endif
  27. //SCM Interfaces
  28. #include "esp.hpp"
  29. // We can use "work" as the first parameter when debugging.
  30. #define ESP_SINGLE_PROCESS
  31. //ESP Core
  32. #include "espp.hpp"
  33. #include "espcfg.ipp"
  34. #include "esplog.hpp"
  35. #include "espcontext.hpp"
  36. #include "build-config.h"
  37. #ifdef _WIN32
  38. /*******************************************
  39. _WIN32
  40. *******************************************/
  41. #ifdef ESP_SINGLE_PROCESS
  42. int start_init_main(int argc, char** argv, int (*init_main_func)(int,char**))
  43. {
  44. return init_main_func(argc, argv);
  45. }
  46. #define START_WORK_MAIN(config, server, result) result = work_main((config), (server));
  47. #define SET_ESP_SIGNAL_HANDLER(sig, handler)
  48. #define RESET_ESP_SIGNAL_HANDLER(sig, handler)
  49. #else //!ESP_SINGLE_PROCESS
  50. #define SET_ESP_SIGNAL_HANDLER(sig, handler)
  51. #define RESET_ESP_SIGNAL_HANDLER(sig, handler)
  52. int start_init_main(int argc, char** argv, int (*init_main_func)(int, char**))
  53. {
  54. if(argc > 1 && !strcmp(argv[1], "work"))
  55. {
  56. char** newargv = new char*[argc - 1];
  57. newargv[0] = argv[0];
  58. for(int i = 2; i < argc; i++)
  59. {
  60. newargv[i - 1] = argv[i];
  61. }
  62. int rtcode = init_main_func(argc - 1, newargv);
  63. delete[] newargv;
  64. return rtcode;
  65. }
  66. else
  67. {
  68. StringBuffer command;
  69. command.append(argv[0]);
  70. command.append(" work ");
  71. for(int i = 1; i < argc; i++)
  72. {
  73. command.append(argv[i]);
  74. command.append(" ");
  75. }
  76. DWORD exitcode = 0;
  77. while(true)
  78. {
  79. PROGLOG("Starting working process: %s", command.str());
  80. PROCESS_INFORMATION process;
  81. STARTUPINFO si;
  82. GetStartupInfo(&si);
  83. if(!CreateProcess(NULL, (char*)command.str(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &process))
  84. {
  85. ERRLOG("Process failed: %d\r\n",GetLastError());
  86. exit(-1);
  87. }
  88. WaitForSingleObject(process.hProcess,INFINITE);
  89. GetExitCodeProcess(process.hProcess, &exitcode);
  90. PROGLOG("Working process exited, exitcode=%d", exitcode);
  91. if(exitcode == TERMINATE_EXITCODE)
  92. {
  93. DBGLOG("This is telling the monitoring process to exit too. Exiting once and for all....");
  94. exit(exitcode);
  95. }
  96. CloseHandle(process.hProcess);
  97. CloseHandle(process.hThread);
  98. Sleep(1000);
  99. }
  100. }
  101. }
  102. #define START_WORK_MAIN(config, server, result) result = work_main((config), (server));
  103. #endif //!ESP_SINGLE_PROCESS
  104. #else
  105. /*****************************************************
  106. LINUX
  107. ****************************************************/
  108. #define SET_ESP_SIGNAL_HANDLER(sig, handler) signal(sig, handler)
  109. #define RESET_ESP_SIGNAL_HANDLER(sig, handler) signal(sig, handler)
  110. int start_init_main(int argc, char** argv, int (*init_main_func)(int,char**))
  111. {
  112. return init_main_func(argc, argv);
  113. }
  114. int work_main(CEspConfig& config, CEspServer& server);
  115. int do_work_main(CEspConfig& config, CEspServer& server)
  116. {
  117. int result;
  118. int numchildren = 0;
  119. pid_t childpid=0;
  120. createworker:
  121. childpid = fork();
  122. if(childpid < 0)
  123. {
  124. ERRLOG("Unable to create new process");
  125. result = -1;
  126. }
  127. else if(childpid == 0)
  128. {
  129. result = work_main(config, server);
  130. }
  131. else
  132. {
  133. DBGLOG("New process generated, pid=%d", childpid);
  134. numchildren++;
  135. if(numchildren < MAX_CHILDREN)
  136. goto createworker;
  137. int status;
  138. childpid = wait3(&status, 0, NULL);
  139. DBGLOG("Attention: child process exited, pid = %d", childpid);
  140. numchildren--;
  141. DBGLOG("Bringing up a new process...");
  142. sleep(1);
  143. goto createworker;
  144. }
  145. return result;
  146. }
  147. #define START_WORK_MAIN(config, server, result) result = do_work_main(config, server)
  148. #endif
  149. void brokenpipe_handler(int sig)
  150. {
  151. //Reset the signal first
  152. RESET_ESP_SIGNAL_HANDLER(SIGPIPE, brokenpipe_handler);
  153. DBGLOG("Broken Pipe - remote side closed the socket");
  154. }
  155. int work_main(CEspConfig& config, CEspServer& server)
  156. {
  157. server.start();
  158. DBGLOG("ESP server started.");
  159. server.waitForExit(config);
  160. server.stop(true);
  161. config.stopping();
  162. config.clear();
  163. return 0;
  164. }
  165. void openEspLogFile(const char* logdir, IPropertyTree* globals)
  166. {
  167. StringBuffer logbase;
  168. StringBuffer alias;
  169. if(logdir && *logdir)
  170. {
  171. logbase.append(logdir);
  172. alias.append(logdir);
  173. }
  174. logbase.append("esp_main");
  175. alias.append("esp.log");
  176. queryLogMsgManager()->addMonitorOwn(getRollingFileLogMsgHandler(logbase.str(), ".log", MSGFIELD_STANDARD, false, true, NULL, alias.str()), getCategoryLogMsgFilter(MSGAUD_all, MSGCLS_all, DefaultDetail));
  177. if (globals->getPropBool("@enableSysLog", false))
  178. UseSysLogForOperatorMessages();
  179. DBGLOG("Esp starting %s", BUILD_TAG);
  180. }
  181. static void usage()
  182. {
  183. puts("ESP - Enterprise Service Platform server. (C) 2001-2011, HPCC Systems.");
  184. puts("Usage:");
  185. puts(" esp [options]");
  186. puts("Options:");
  187. puts(" -?/-h: show this help page");
  188. puts(" interactive: start in interactive mode (pop up error dialog when exception occurs)");
  189. puts(" config=<file>: specify the config file name [default: esp.xml]");
  190. puts(" process=<name>: specify the process name in the config [default: the 1st process]");
  191. exit(1);
  192. }
  193. int init_main(int argc, char* argv[])
  194. {
  195. InitModuleObjects();
  196. Owned<IProperties> inputs = createProperties(true);
  197. bool interactive = false;
  198. for (int i = 1; i < argc; i++)
  199. {
  200. if (stricmp(argv[i], "-?")==0 || stricmp(argv[i], "-h")==0 || stricmp(argv[i], "-help")==0
  201. || stricmp(argv[i], "/?")==0 || stricmp(argv[i], "/h")==0)
  202. usage();
  203. else if(stricmp(argv[i], "interactive") == 0)
  204. interactive = true;
  205. else if (strchr(argv[i],'='))
  206. {
  207. inputs->loadProp(argv[i]);
  208. }
  209. else
  210. {
  211. fprintf(stderr, "Unknown option: %s", argv[i]);
  212. return 0;
  213. }
  214. }
  215. int result = -1;
  216. #ifdef _WIN32
  217. if (!interactive)
  218. ::SetErrorMode(SEM_NOGPFAULTERRORBOX|SEM_FAILCRITICALERRORS);
  219. #endif
  220. SET_ESP_SIGNAL_HANDLER(SIGPIPE, brokenpipe_handler);
  221. bool SEHMappingEnabled = false;
  222. CEspAbortHandler abortHandler;
  223. Owned<IFile> sentinelFile = createSentinelTarget();
  224. removeSentinelFile(sentinelFile);
  225. Owned<CEspConfig> config;
  226. Owned<CEspServer> server;
  227. try
  228. {
  229. const char* cfgfile = NULL;
  230. const char* procname = NULL;
  231. if(inputs.get())
  232. {
  233. if(inputs->hasProp("config"))
  234. cfgfile = inputs->queryProp("config");
  235. if(inputs->hasProp("process"))
  236. procname = inputs->queryProp("process");
  237. }
  238. if(!cfgfile || !*cfgfile)
  239. cfgfile = "esp.xml";
  240. Owned<IPropertyTree> envpt= createPTreeFromXMLFile(cfgfile, ipt_caseInsensitive);
  241. Owned<IPropertyTree> procpt = NULL;
  242. if (envpt)
  243. {
  244. envpt->addProp("@config", cfgfile);
  245. StringBuffer xpath;
  246. if (procname==NULL || strcmp(procname, ".")==0)
  247. xpath.appendf("Software/EspProcess[1]");
  248. else
  249. xpath.appendf("Software/EspProcess[@name=\"%s\"]", procname);
  250. DBGLOG("Using ESP configuration section [%s]", xpath.str());
  251. procpt.set(envpt->queryPropTree(xpath.str()));
  252. if (!procpt)
  253. throw MakeStringException(-1, "Config section [%s] not found", xpath.str());
  254. }
  255. else
  256. throw MakeStringException(-1, "Failed to load config file %s", cfgfile);
  257. StringBuffer logdir;
  258. if(procpt->hasProp("@name"))
  259. {
  260. StringBuffer espNameStr;
  261. procpt->getProp("@name", espNameStr);
  262. if (!getConfigurationDirectory(envpt->queryPropTree("Software/Directories"), "log", "esp", espNameStr.str(), logdir))
  263. {
  264. logdir.clear();
  265. }
  266. }
  267. const char* build_ver = BUILD_TAG;
  268. setBuildVersion(build_ver);
  269. const char* build_level = BUILD_LEVEL;
  270. setBuildLevel(build_level);
  271. if(logdir.length() == 0)
  272. {
  273. if(procpt->hasProp("@logDir"))
  274. procpt->getProp("@logDir", logdir);
  275. }
  276. if(logdir.length() == 0)
  277. logdir.append(".");
  278. if(stricmp(logdir.str(), ".") != 0)
  279. {
  280. recursiveCreateDirectory(logdir.str());
  281. }
  282. if(logdir.charAt(logdir.length() - 1) != PATHSEPCHAR)
  283. logdir.append(PATHSEPCHAR);
  284. openEspLogFile(logdir.str(), procpt.get());
  285. StringBuffer componentfilesDir;
  286. if(procpt->hasProp("@componentfilesDir"))
  287. procpt->getProp("@componentfilesDir", componentfilesDir);
  288. if(componentfilesDir.length() > 0 && strcmp(componentfilesDir.str(), ".") != 0)
  289. {
  290. DBGLOG("componentfiles are under %s", componentfilesDir.str());
  291. setCFD(componentfilesDir.str());
  292. }
  293. StringBuffer sehsetting;
  294. procpt->getProp("@enableSEHMapping", sehsetting);
  295. if(!interactive && sehsetting.length() > 0 && (stricmp(sehsetting.str(), "true") == 0 || stricmp(sehsetting.str(), "1") == 0))
  296. SEHMappingEnabled = true;
  297. if(SEHMappingEnabled)
  298. EnableSEHtoExceptionMapping();
  299. CEspConfig* cfg = new CEspConfig(inputs.getLink(), envpt.getLink(), procpt.getLink(), false);
  300. if(cfg && cfg->isValid())
  301. {
  302. config.setown(cfg);
  303. abortHandler.setConfig(cfg);
  304. }
  305. }
  306. catch(IException* e)
  307. {
  308. StringBuffer description;
  309. ERRLOG("ESP Unhandled IException (%d -- %s)", e->errorCode(), e->errorMessage(description).str());
  310. e->Release();
  311. return -1;
  312. }
  313. catch (...)
  314. {
  315. ERRLOG("ESP Unhandled General Exception.");
  316. return -1;
  317. }
  318. if (config && config->isValid())
  319. {
  320. PROGLOG("Configuring Esp Platform...");
  321. try
  322. {
  323. CEspServer *srv = new CEspServer(config);
  324. if(SEHMappingEnabled)
  325. srv->setSavedSEHHandler(SEHMappingEnabled);
  326. server.setown(srv);
  327. abortHandler.setServer(srv);
  328. setEspContainer(server.get());
  329. config->loadAll();
  330. config->bindServer(*server.get(), *server.get());
  331. }
  332. catch(IException* e)
  333. {
  334. StringBuffer description;
  335. ERRLOG("ESP Unhandled IException (%d -- %s)", e->errorCode(), e->errorMessage(description).str());
  336. e->Release();
  337. return -1;
  338. }
  339. catch (...)
  340. {
  341. ERRLOG("ESP Unhandled General Exception.");
  342. return -1;
  343. }
  344. writeSentinelFile(sentinelFile);
  345. result = work_main(*config, *server.get());
  346. }
  347. else
  348. {
  349. ERRLOG("!!! Unable to load ESP configuration.");
  350. }
  351. return result;
  352. }
  353. //command line arguments:
  354. // [pre] if "work", special init behavior, but removed before init_main
  355. // [1] process name
  356. // [2] config location - local file name or dali address
  357. // [3] config location type - "dali" or ""
  358. int main(int argc, char* argv[])
  359. {
  360. start_init_main(argc, argv, init_main);
  361. stopPerformanceMonitor();
  362. UseSysLogForOperatorMessages(false);
  363. releaseAtoms();
  364. return 0;
  365. }