espp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. StringBuffer sentinel_filename;
  224. splitFilename(argv[0], &sentinel_filename, &sentinel_filename, NULL, NULL);
  225. sentinel_filename.append("esp_sentinel.txt");
  226. Owned<IFile> sentinel_File = createIFile(sentinel_filename.str());
  227. sentinel_File->remove();
  228. Owned<CEspConfig> config;
  229. Owned<CEspServer> server;
  230. try
  231. {
  232. const char* cfgfile = NULL;
  233. const char* procname = NULL;
  234. if(inputs.get())
  235. {
  236. if(inputs->hasProp("config"))
  237. cfgfile = inputs->queryProp("config");
  238. if(inputs->hasProp("process"))
  239. procname = inputs->queryProp("process");
  240. }
  241. if(!cfgfile || !*cfgfile)
  242. cfgfile = "esp.xml";
  243. Owned<IPropertyTree> envpt= createPTreeFromXMLFile(cfgfile, true);
  244. Owned<IPropertyTree> procpt = NULL;
  245. if (envpt)
  246. {
  247. envpt->addProp("@config", cfgfile);
  248. StringBuffer xpath;
  249. if (procname==NULL || strcmp(procname, ".")==0)
  250. xpath.appendf("Software/EspProcess[1]");
  251. else
  252. xpath.appendf("Software/EspProcess[@name=\"%s\"]", procname);
  253. DBGLOG("Using ESP configuration section [%s]", xpath.str());
  254. procpt.set(envpt->queryPropTree(xpath.str()));
  255. if (!procpt)
  256. throw MakeStringException(-1, "Config section [%s] not found", xpath.str());
  257. }
  258. else
  259. throw MakeStringException(-1, "Failed to load config file %s", cfgfile);
  260. StringBuffer logdir;
  261. if(procpt->hasProp("@name"))
  262. {
  263. StringBuffer espNameStr;
  264. procpt->getProp("@name", espNameStr);
  265. if (!getConfigurationDirectory(envpt->queryPropTree("Software/Directories"), "log", "esp", espNameStr.str(), logdir))
  266. {
  267. logdir.clear();
  268. }
  269. }
  270. const char* build_ver = BUILD_TAG;
  271. setBuildVersion(build_ver);
  272. const char* build_level = BUILD_LEVEL;
  273. setBuildLevel(build_level);
  274. if(logdir.length() == 0)
  275. {
  276. if(procpt->hasProp("@logDir"))
  277. procpt->getProp("@logDir", logdir);
  278. }
  279. if(logdir.length() == 0)
  280. logdir.append(".");
  281. if(stricmp(logdir.str(), ".") != 0)
  282. {
  283. recursiveCreateDirectory(logdir.str());
  284. }
  285. if(logdir.charAt(logdir.length() - 1) != PATHSEPCHAR)
  286. logdir.append(PATHSEPCHAR);
  287. openEspLogFile(logdir.str(), procpt.get());
  288. StringBuffer componentfilesDir;
  289. if(procpt->hasProp("@componentfilesDir"))
  290. procpt->getProp("@componentfilesDir", componentfilesDir);
  291. if(componentfilesDir.length() > 0 && strcmp(componentfilesDir.str(), ".") != 0)
  292. {
  293. DBGLOG("componentfiles are under %s", componentfilesDir.str());
  294. setCFD(componentfilesDir.str());
  295. }
  296. StringBuffer sehsetting;
  297. procpt->getProp("@enableSEHMapping", sehsetting);
  298. if(!interactive && sehsetting.length() > 0 && (stricmp(sehsetting.str(), "true") == 0 || stricmp(sehsetting.str(), "1") == 0))
  299. SEHMappingEnabled = true;
  300. if(SEHMappingEnabled)
  301. EnableSEHtoExceptionMapping();
  302. CEspConfig* cfg = new CEspConfig(inputs.getLink(), envpt.getLink(), procpt.getLink(), false);
  303. if(cfg && cfg->isValid())
  304. {
  305. config.setown(cfg);
  306. abortHandler.setConfig(cfg);
  307. }
  308. }
  309. catch(IException* e)
  310. {
  311. StringBuffer description;
  312. ERRLOG("ESP Unhandled IException (%d -- %s)", e->errorCode(), e->errorMessage(description).str());
  313. e->Release();
  314. return -1;
  315. }
  316. catch (...)
  317. {
  318. ERRLOG("ESP Unhandled General Exception.");
  319. return -1;
  320. }
  321. if (config && config->isValid())
  322. {
  323. PROGLOG("Configuring Esp Platform...");
  324. try
  325. {
  326. CEspServer *srv = new CEspServer(config);
  327. if(SEHMappingEnabled)
  328. srv->setSavedSEHHandler(SEHMappingEnabled);
  329. server.setown(srv);
  330. abortHandler.setServer(srv);
  331. setEspContainer(server.get());
  332. config->loadAll();
  333. config->bindServer(*server.get(), *server.get());
  334. }
  335. catch(IException* e)
  336. {
  337. StringBuffer description;
  338. ERRLOG("ESP Unhandled IException (%d -- %s)", e->errorCode(), e->errorMessage(description).str());
  339. e->Release();
  340. return -1;
  341. }
  342. catch (...)
  343. {
  344. ERRLOG("ESP Unhandled General Exception.");
  345. return -1;
  346. }
  347. // if we got here, the initial load is ok, so create the "sentinel file" for re-runs from the script
  348. // put in its own "scope" to force the flush
  349. {
  350. DBGLOG("Creating sentinel file %s for rerun from script", sentinel_filename.str());
  351. Owned<IFileIO> fileIO = sentinel_File->open(IFOcreate);
  352. fileIO->write(0, 5, "rerun");
  353. }
  354. result = work_main(*config, *server.get());
  355. }
  356. else
  357. {
  358. ERRLOG("!!! Unable to load ESP configuration.");
  359. }
  360. return result;
  361. }
  362. //command line arguments:
  363. // [pre] if "work", special init behavior, but removed before init_main
  364. // [1] process name
  365. // [2] config location - local file name or dali address
  366. // [3] config location type - "dali" or ""
  367. int main(int argc, char* argv[])
  368. {
  369. start_init_main(argc, argv, init_main);
  370. stopPerformanceMonitor();
  371. UseSysLogForOperatorMessages(false);
  372. releaseAtoms();
  373. return 0;
  374. }