espp.cpp 12 KB

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