jmisc.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. //#define MINIMALTRACE // a more readable version of the trace file (IMO)
  14. //#define LOGCLOCK
  15. #include "platform.h"
  16. #include "stdio.h"
  17. #include <time.h>
  18. #include "jmisc.hpp"
  19. #include "jmutex.hpp"
  20. #include "jexcept.hpp"
  21. #include "jstring.hpp"
  22. #include "jdebug.hpp"
  23. #ifdef _WIN32
  24. #include <direct.h>
  25. #else
  26. #include <sys/wait.h>
  27. #include <pwd.h>
  28. #endif
  29. #ifdef LOGCLOCK
  30. #define MSGFIELD_PRINTLOG MSGFIELD_timeDate | MSGFIELD_msgID | MSGFIELD_process | MSGFIELD_thread | MSGFIELD_code | MSGFIELD_milliTime
  31. #else
  32. #define MSGFIELD_PRINTLOG MSGFIELD_timeDate | MSGFIELD_msgID | MSGFIELD_process | MSGFIELD_thread | MSGFIELD_code
  33. #endif
  34. #define FPRINTF(s) { fprintf(stdlog, "%s", s); \
  35. if (logFile) fprintf(logFile, "%s", s); }
  36. void _rev(size32_t len, void * _ptr)
  37. {
  38. byte * ptr = (byte *)_ptr;
  39. while (len > 1)
  40. {
  41. byte t = *ptr;
  42. *ptr = ptr[--len];
  43. ptr[len] = t;
  44. len--;
  45. ptr++;
  46. }
  47. }
  48. Mutex printMutex;
  49. FILE *logFile;
  50. FILE *stdlog = stderr;
  51. HiresTimer logTimer;
  52. class CStdLogIntercept: public ILogIntercept
  53. {
  54. bool nl;
  55. #ifdef MINIMALTRACE
  56. time_t lastt;
  57. unsigned lasttpid;
  58. #endif
  59. unsigned detail;
  60. public:
  61. CStdLogIntercept()
  62. {
  63. #ifdef MINIMALTRACE
  64. lasttpid = 0;
  65. lastt = 0;
  66. #endif
  67. nl = true;
  68. detail = _LOG_TIME | _LOG_PID | _LOG_TID;
  69. #ifdef LOGCLOCK
  70. detail |= _LOG_CLOCK;
  71. #endif
  72. queryStderrLogMsgHandler()->setMessageFields(detail);
  73. }
  74. unsigned setLogDetail(unsigned i)
  75. {
  76. unsigned ret = detail;
  77. detail = i;
  78. return ret;
  79. }
  80. void print(const char *str)
  81. {
  82. if (nl) {
  83. char timeStamp[32];
  84. time_t tNow;
  85. time(&tNow);
  86. #ifdef _WIN32
  87. struct tm ltNow = *localtime(&tNow);
  88. #else
  89. struct tm ltNow;
  90. localtime_r(&tNow, &ltNow);
  91. #endif
  92. #ifdef MINIMALTRACE
  93. unsigned tpid = GetCurrentThreadId();
  94. if (((detail & _LOG_TID) && tpid!=lasttpid)||((detail & _LOG_TIME) && memcmp(&tNow,&lastt,sizeof(tNow))!=0))
  95. {
  96. lasttpid = tpid;
  97. lastt = tNow;
  98. FPRINTF("[");
  99. if (detail & _LOG_TIME)
  100. {
  101. strftime(timeStamp, 32, "%H:%M:%S ", &ltNow);
  102. FPRINTF(timeStamp);
  103. }
  104. if (detail & _LOG_TID)
  105. {
  106. fprintf(stdlog, "TID=%d ", tpid);
  107. if (logFile)
  108. fprintf(logFile, "TID=%d ", tpid);
  109. }
  110. FPRINTF("]\n");
  111. }
  112. #else
  113. if (detail & _LOG_TIME)
  114. {
  115. strftime(timeStamp, 32, "%m/%d/%y %H:%M:%S ", &ltNow);
  116. FPRINTF(timeStamp);
  117. }
  118. if (detail & _LOG_CLOCK)
  119. {
  120. unsigned t=msTick();
  121. fprintf(stdlog, "%u ", t);
  122. if (logFile)
  123. fprintf(logFile, "%u ", t);
  124. }
  125. if (detail & _LOG_HIRESCLOCK)
  126. {
  127. unsigned clock = usTick();
  128. fprintf(stdlog, " TICK=%u ", clock);
  129. if (logFile)
  130. fprintf(logFile, " TICK=%u ", clock);
  131. }
  132. #if defined(_WIN32)
  133. if (detail & _LOG_TID)
  134. {
  135. fprintf(stdlog, "TID=%d ", GetCurrentThreadId());
  136. if (logFile)
  137. fprintf(logFile, "TID=%d ", GetCurrentThreadId());
  138. }
  139. if (detail & _LOG_PID)
  140. {
  141. fprintf(stdlog, "PID=%d ", GetCurrentProcessId());
  142. if (logFile)
  143. fprintf(logFile, "PID=%d ", GetCurrentProcessId());
  144. }
  145. #else
  146. if (detail & _LOG_PID)
  147. {
  148. fprintf(stdlog, "PID=%d ", getpid());
  149. if (logFile)
  150. fprintf(logFile, "PID=%d ", getpid());
  151. }
  152. #endif
  153. if (detail & (_LOG_PID|_LOG_TID|_LOG_TIME))
  154. {
  155. fprintf(stdlog, "- ");
  156. if (logFile)
  157. fprintf(logFile, "- ");
  158. }
  159. #endif
  160. }
  161. if (str && *str)
  162. {
  163. nl = str[strlen(str)-1]=='\n';
  164. FPRINTF(str);
  165. }
  166. else
  167. nl = false;
  168. fflush(stdlog);
  169. if (logFile)
  170. {
  171. fflush(logFile);
  172. }
  173. }
  174. void close()
  175. {
  176. if (logFile) {
  177. fclose(logFile);
  178. logFile = 0;
  179. }
  180. }
  181. };
  182. static ILogIntercept *logintercept = NULL;
  183. jlib_decl ILogIntercept* interceptLog(ILogIntercept *intercept)
  184. {
  185. ILogIntercept *old=logintercept;
  186. logintercept = intercept;
  187. return old;
  188. }
  189. jlib_decl void openLogFile(StringBuffer & resolvedFS, const char *filename, unsigned detail, bool enterQueueMode, bool append)
  190. {
  191. if(enterQueueMode)
  192. queryLogMsgManager()->enterQueueingMode();
  193. Owned<IComponentLogFileCreator> lf = createComponentLogFileCreator(".", 0);
  194. lf->setCreateAliasFile(false);
  195. lf->setLocal(true);
  196. lf->setRolling(false);
  197. lf->setAppend(append);
  198. lf->setCompleteFilespec(filename);//user specified log filespec
  199. lf->setMaxDetail(detail ? detail : DefaultDetail);
  200. lf->setMsgFields(MSGFIELD_timeDate | MSGFIELD_msgID | MSGFIELD_process | MSGFIELD_thread | MSGFIELD_code);
  201. lf->beginLogging();
  202. resolvedFS.set(lf->queryLogFileSpec());
  203. }
  204. jlib_decl void PrintLogDirect(const char *msg)
  205. {
  206. LOG(MClegacy, unknownJob, "%s", msg);
  207. }
  208. jlib_decl int PrintLog(const char *fmt, ...)
  209. {
  210. va_list args;
  211. va_start(args, fmt);
  212. VALOG(MClegacy, unknownJob, fmt, args);
  213. va_end(args);
  214. return 0;
  215. }
  216. jlib_decl void SPrintLog(const char *fmt, ...)
  217. {
  218. va_list args;
  219. va_start(args, fmt);
  220. VALOG(MClegacy, unknownJob, fmt, args);
  221. va_end(args);
  222. }
  223. StringBuffer &addFileTimestamp(StringBuffer &fname, bool daily)
  224. {
  225. time_t tNow;
  226. time(&tNow);
  227. char timeStamp[32];
  228. #ifdef _WIN32
  229. struct tm *ltNow;
  230. ltNow = localtime(&tNow);
  231. strftime(timeStamp, 32, daily ? ".%Y_%m_%d" : ".%Y_%m_%d_%H_%M_%S", ltNow);
  232. #else
  233. struct tm ltNow;
  234. localtime_r(&tNow, &ltNow);
  235. strftime(timeStamp, 32, daily ? ".%Y_%m_%d" : ".%Y_%m_%d_%H_%M_%S", &ltNow);
  236. #endif
  237. return fname.append(timeStamp);
  238. }
  239. #define MAX_TRY_SIZE 0x8000000 // 256 MB
  240. jlib_decl void PrintMemoryStatusLog()
  241. {
  242. #ifdef _WIN32
  243. MEMORYSTATUS mS;
  244. GlobalMemoryStatus(&mS);
  245. LOG(MCdebugInfo, unknownJob, "Available Physical Memory = %dK", (unsigned)(mS.dwAvailPhys/1024));
  246. #ifdef FRAGMENTATION_CHECK
  247. // see if fragmented
  248. size32_t sz = MAX_TRY_SIZE;
  249. while (sz) {
  250. if (sz<mS.dwAvailPhys/4) {
  251. void *p=malloc(sz);
  252. if (p) {
  253. free(p);
  254. break;
  255. }
  256. }
  257. sz /= 2;
  258. }
  259. sz *= 2;
  260. if ((sz<MAX_TRY_SIZE)&&(sz<mS.dwAvailPhys/4)) {
  261. LOG(MCdebugInfo, unknownJob, "WARNING: Could not allocate block size %d", sz);
  262. _HEAPINFO hinfo;
  263. int heapstatus;
  264. hinfo._pentry = NULL;
  265. size32_t max=0;
  266. unsigned fragments=0;
  267. while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK ) {
  268. if (hinfo._useflag != _USEDENTRY) {
  269. if (hinfo._size>max)
  270. max = hinfo._size;
  271. fragments++;
  272. }
  273. }
  274. LOG(MCdebugInfo, unknownJob, "Largest unused fragment = %d", max);
  275. LOG(MCdebugInfo, unknownJob, "Number of fragments = %d", fragments);
  276. }
  277. #endif
  278. #endif
  279. }
  280. static class _init_jmisc_class
  281. {
  282. public:
  283. ~_init_jmisc_class()
  284. {
  285. if (logFile)
  286. fclose(logFile);
  287. }
  288. } _init_jmisc;
  289. FILE *xfopen(const char *path, const char *mode)
  290. {
  291. char *s, *e, *p;
  292. p = s = strdup(path);
  293. e = s+strlen(path);
  294. bool alt=false;
  295. for (; p<e; p++)
  296. {
  297. #ifdef _WIN32
  298. if (*p == '/')
  299. #else
  300. if (*p == '\\')
  301. #endif
  302. {
  303. alt = true;
  304. *p = PATHSEPCHAR;
  305. }
  306. }
  307. if (alt)
  308. printf("XFOPEN ALTERED FILENAME FROM:%s TO %s\n", path, s);
  309. FILE *file = ::fopen(s, mode);
  310. free(s);
  311. return file;
  312. }
  313. const char * queryCcLogName()
  314. {
  315. const char* logFileName = getenv("ECL_CCLOG");
  316. if (!logFileName)
  317. {
  318. logFileName = "cclog.txt";
  319. }
  320. return logFileName;
  321. //More does output get redirected here?
  322. }
  323. StringBuffer& queryCcLogName(const char* wuid, StringBuffer& logname)
  324. {
  325. if(!wuid || !*wuid)
  326. logname.append(queryCcLogName());
  327. else
  328. {
  329. const char* presetName = getenv("ECL_CCLOG");
  330. if (presetName && *presetName)
  331. logname.append(presetName);
  332. else
  333. logname.append(wuid).trim().append(".cclog.txt");
  334. }
  335. return logname;
  336. }
  337. jlib_decl char* readarg(char*& curptr)
  338. {
  339. char *cur = curptr;
  340. if(cur == NULL || *cur == 0)
  341. return NULL;
  342. while(*cur == ' ' || *cur == '\t') {
  343. cur++;
  344. if (!*cur) {
  345. curptr = cur;
  346. return NULL;
  347. }
  348. }
  349. char quote = 0;
  350. if(*cur == '\'' || *cur == '"')
  351. {
  352. quote = *cur;
  353. cur++;
  354. }
  355. char *ret = cur;
  356. if (quote)
  357. {
  358. while (*cur && *cur!=quote)
  359. cur++;
  360. }
  361. else
  362. {
  363. do {
  364. cur++;
  365. } while(*cur && *cur != ' ' && *cur != '\t');
  366. }
  367. if(*cur != 0)
  368. {
  369. *cur = 0;
  370. cur++;
  371. }
  372. curptr = cur;
  373. return ret;
  374. }
  375. #ifdef _WIN32
  376. bool invoke_program(const char *command_line, DWORD &runcode, bool wait, const char *outfile, HANDLE *rethandle, bool throwException, bool newProcessGroup)
  377. {
  378. runcode = 0;
  379. if (rethandle)
  380. *rethandle = 0;
  381. if(!command_line || !*command_line)
  382. return false;
  383. STARTUPINFO StartupInfo;
  384. SECURITY_ATTRIBUTES security;
  385. _clear(security);
  386. security.nLength = sizeof(security);
  387. security.bInheritHandle = TRUE;
  388. _clear(StartupInfo);
  389. StartupInfo.cb = sizeof(StartupInfo);
  390. HANDLE outh = NULL;
  391. if (outfile&&*outfile) {
  392. outh = CreateFile(outfile,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,&security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  393. if (outh == INVALID_HANDLE_VALUE)
  394. {
  395. ERRLOG("Cannot create file '%s' error code %d",outfile,(int)GetLastError());
  396. return false;
  397. }
  398. }
  399. if (outh) {
  400. if (!DuplicateHandle(GetCurrentProcess(),outh,GetCurrentProcess(),&StartupInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS) ||
  401. !DuplicateHandle(GetCurrentProcess(),outh,GetCurrentProcess(),&StartupInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS))
  402. {
  403. ERRLOG("Execution of \"%s\" failed, DuplicateHandle error = %d",command_line, (int)GetLastError());
  404. CloseHandle(outh);
  405. return false;
  406. }
  407. StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  408. StartupInfo.dwFlags = STARTF_USESTDHANDLES;
  409. CloseHandle(outh);
  410. }
  411. PROCESS_INFORMATION ProcessInformation;
  412. bool ok = CreateProcess(NULL,(char *)command_line,NULL,NULL,TRUE,0,NULL,NULL,&StartupInfo,&ProcessInformation)!=0;
  413. if (ok) {
  414. if (wait)
  415. {
  416. WaitForSingleObject(ProcessInformation.hProcess,INFINITE);
  417. GetExitCodeProcess(ProcessInformation.hProcess, &runcode);
  418. }
  419. CloseHandle(ProcessInformation.hThread);
  420. if (rethandle) {
  421. *rethandle = (HANDLE)ProcessInformation.hProcess;
  422. if (wait)
  423. CloseHandle(ProcessInformation.hProcess);
  424. }
  425. else
  426. CloseHandle(ProcessInformation.hProcess);
  427. }
  428. else
  429. {
  430. int lastError = (int)GetLastError(); // for debugging
  431. //print out why create process failed
  432. ERRLOG("Execution of \"%s\" failed, error = %d",command_line,lastError);
  433. }
  434. if (outh)
  435. {
  436. CloseHandle(StartupInfo.hStdOutput);
  437. CloseHandle(StartupInfo.hStdError);
  438. }
  439. return ok;
  440. }
  441. bool wait_program(HANDLE handle,DWORD &runcode,bool block)
  442. {
  443. runcode = (DWORD)-1;
  444. if ((handle==NULL)||(handle==(HANDLE)-1))
  445. return true; // actually it failed
  446. int ret = WaitForSingleObject(handle,block?INFINITE:0);
  447. int err = GetLastError();
  448. if (ret==WAIT_OBJECT_0) {
  449. GetExitCodeProcess(handle, &runcode);
  450. CloseHandle(handle);
  451. return true;
  452. }
  453. return false;
  454. }
  455. jlib_decl bool interrupt_program(HANDLE handle, bool stopChildren, int signum)
  456. {
  457. if (signum==0)
  458. return TerminateProcess(handle,1)!=FALSE;
  459. ERRLOG("interrupt_program signal %d not supported in windows",signum);
  460. return false;
  461. }
  462. void close_program(HANDLE handle)
  463. {
  464. CloseHandle(handle);
  465. }
  466. #else
  467. bool invoke_program(const char *command_line, DWORD &runcode, bool wait, const char *outfile, HANDLE *rethandle, bool throwException, bool newProcessGroup)
  468. {
  469. runcode = 0;
  470. if (rethandle)
  471. *rethandle = 0;
  472. if(!command_line || !*command_line)
  473. return false;
  474. pid_t pid = fork();
  475. if (pid == 0)
  476. {
  477. //Force the child process into its own process group, so we can terminate it and its children.
  478. if (newProcessGroup)
  479. setpgid(0,0);
  480. if (outfile&&*outfile) {
  481. int outh = open(outfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR);
  482. if(outh >= 0)
  483. {
  484. dup2(outh, STDOUT_FILENO);
  485. dup2(outh, STDERR_FILENO);
  486. close(outh);
  487. }
  488. else {
  489. ERRLOG("invoke_program: could not open %s",outfile);
  490. return false;
  491. }
  492. }
  493. int size = 10;
  494. char **args;
  495. char **argptr = args = (char **) malloc(sizeof(args) * size);
  496. char **over = argptr+size;
  497. char *cl = strdup(command_line);
  498. char *curptr = cl;
  499. *argptr = readarg(curptr);
  500. while (*argptr != NULL)
  501. {
  502. argptr++;
  503. if (argptr==over)
  504. {
  505. args = (char **) realloc(args, sizeof(args) * size * 2);
  506. argptr = args+size;
  507. size *= 2;
  508. over = args+size;
  509. }
  510. *argptr = readarg(curptr);
  511. }
  512. // JCSMORE - do I not need to free args, if successful exec?
  513. if (execvp(*args, args))
  514. {
  515. //print out why create process failed
  516. int err = errno;
  517. ERRLOG("invoke_program(%s) failed with error(%d): %s",command_line,err,strerror(err));
  518. }
  519. assertex(0); // don't expect to get here!
  520. _exit(0);
  521. }
  522. else if (pid < 0)
  523. {
  524. StringBuffer s("fork of \"");
  525. s.append(command_line).append("\" failed. fork() returned:");
  526. if (errno == EAGAIN)
  527. s.append("EAGAIN");
  528. else if (errno == ENOMEM)
  529. s.append("ENOMEM");
  530. else
  531. s.append(errno);
  532. ERRLOG("%s",s.str());
  533. if(throwException)
  534. throw MakeStringExceptionDirect(-1, s.str());
  535. return false;
  536. }
  537. if (rethandle)
  538. *rethandle = (HANDLE)pid;
  539. if (wait)
  540. {
  541. int retv;
  542. if (waitpid(pid, &retv, 0) != pid) {
  543. ERRLOG("invoke_program(%s): wait failed (%d)",command_line,(int)pid);
  544. return false;
  545. }
  546. if (!WIFEXITED(retv)) //did not exit normally
  547. {
  548. int err = errno;
  549. ERRLOG("invoke_program(%s): failed.",command_line);
  550. ERRLOG("The process was killed by signal %d%s.",(int)WTERMSIG(retv),WCOREDUMP(retv)?" - core dumped":"");
  551. ERRLOG("Last system error is %s",strerror(err));
  552. }
  553. runcode = WEXITSTATUS(retv);
  554. }
  555. return true; // it did run even if signalled
  556. }
  557. bool wait_program(HANDLE handle,DWORD &runcode,bool block)
  558. {
  559. pid_t pid = (pid_t)handle;
  560. runcode = (DWORD)-1;
  561. if ((int)pid<=0)
  562. return true; // actually it failed
  563. int retv;
  564. pid_t ret = waitpid(pid, &retv, block?0:WNOHANG);
  565. if (ret == (pid_t)-1) {
  566. ERRLOG("wait_program: wait failed (%d)",errno);
  567. return true; // actually it failed but assume finished
  568. }
  569. else if (ret==0)
  570. return false;
  571. runcode = WEXITSTATUS(retv);
  572. return true;
  573. }
  574. bool interrupt_program(HANDLE handle, bool stopChildren, int signum)
  575. {
  576. if (signum == 0)
  577. signum = SIGINT;
  578. pid_t pid = (pid_t)handle;
  579. if ((int)pid<=0)
  580. return false;
  581. //If we need to also stop child processes then kill the process group (same as the pid)
  582. //Note: This will not apply to grand-children started by the children by calling invoke_program()
  583. //since they will have a different process group
  584. if (stopChildren)
  585. pid = -pid;
  586. return (kill(pid, signum)==0);
  587. }
  588. void close_program(HANDLE handle)
  589. {
  590. // not needed in linux
  591. }
  592. #endif
  593. #ifndef _WIN32
  594. bool CopyFile(const char *file, const char *newfile, bool fail)
  595. {
  596. struct stat s;
  597. if ((fail) && (0 == stat((char *)newfile, &s))) return false;
  598. FILE *in=fopen(file,"rb"), *out=fopen(newfile,"wb");
  599. try
  600. {
  601. if (!in)
  602. throw MakeStringException(-1, "failed to open %s for copy",file);
  603. if (!out)
  604. throw MakeStringException(-1, "failed to create %s",newfile);
  605. char b[1024];
  606. while (true)
  607. {
  608. int c=fread(b,1,sizeof(b),in);
  609. if (!c) break;
  610. if (!fwrite(b,c,1,out)) throw MakeStringException(-1, "failed to copy file %s to %s",file,newfile);
  611. }
  612. fclose(in);
  613. fclose(out);
  614. stat((char *)file, &s);
  615. chmod(newfile, s.st_mode);
  616. } catch (...)
  617. {
  618. if (in) fclose(in);
  619. if (out) fclose(out);
  620. return false;
  621. }
  622. return true;
  623. }
  624. #endif
  625. //========================================================================================================================
  626. static bool hadAbortSignal = false;
  627. static bool handlerInstalled = false;
  628. CriticalSection abortCrit;
  629. class AbortHandlerInfo : public CInterface
  630. {
  631. public:
  632. ThreadId installer;
  633. AbortHandler handler;
  634. SimpleAbortHandler shandler;
  635. IAbortHandler *ihandler;
  636. AbortHandlerInfo(AbortHandler _handler)
  637. {
  638. handler = _handler;
  639. shandler = NULL;
  640. ihandler = NULL;
  641. installer = GetCurrentThreadId();
  642. }
  643. AbortHandlerInfo(SimpleAbortHandler _handler)
  644. {
  645. handler = NULL;
  646. shandler = _handler;
  647. ihandler = NULL;
  648. installer = GetCurrentThreadId();
  649. }
  650. AbortHandlerInfo(IAbortHandler *_ihandler)
  651. {
  652. handler = NULL;
  653. shandler = NULL;
  654. ihandler = _ihandler;
  655. installer = GetCurrentThreadId();
  656. }
  657. bool handle(ahType type)
  658. {
  659. #ifndef _WIN32
  660. if (installer == GetCurrentThreadId())
  661. #endif
  662. {
  663. // DBGLOG("handle abort %x", GetCurrentThreadId());
  664. if (handler)
  665. return handler(type);
  666. else if (shandler)
  667. return shandler();
  668. else
  669. return ihandler->onAbort();
  670. }
  671. #ifndef _WIN32
  672. else
  673. return false;
  674. #endif
  675. }
  676. };
  677. CIArrayOf<AbortHandlerInfo> handlers;
  678. bool notifyOnAbort(ahType type)
  679. {
  680. // DBGLOG("notifyOnAbort %x", GetCurrentThreadId());
  681. // CriticalBlock c(abortCrit); You would think that this was needed, but it locks up.
  682. // If it needs to be threadsafe, have to clone the list or something
  683. bool doExit = false;
  684. ForEachItemInRev(idx, handlers)
  685. {
  686. if (handlers.item(idx).handle(type))
  687. doExit = true;
  688. }
  689. // DBGLOG("notifyOnAbort returning %d", (int) doExit);
  690. return doExit;
  691. }
  692. #ifdef _WIN32
  693. BOOL WINAPI WindowsAbortHandler ( DWORD dwCtrlType )
  694. {
  695. switch( dwCtrlType )
  696. {
  697. case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate
  698. case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode
  699. case CTRL_CLOSE_EVENT:
  700. {
  701. hadAbortSignal = true;
  702. bool doExit = notifyOnAbort(ahInterrupt);
  703. return !doExit;
  704. }
  705. case CTRL_LOGOFF_EVENT:
  706. case CTRL_SHUTDOWN_EVENT:
  707. hadAbortSignal = true;
  708. notifyOnAbort(ahTerminate);
  709. return FALSE;
  710. }
  711. return FALSE;
  712. }
  713. BOOL WINAPI ModuleExitHandler ( DWORD dwCtrlType )
  714. {
  715. switch( dwCtrlType )
  716. {
  717. case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate
  718. case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode
  719. case CTRL_CLOSE_EVENT:
  720. case CTRL_LOGOFF_EVENT:
  721. case CTRL_SHUTDOWN_EVENT:
  722. ExitModuleObjects();
  723. }
  724. return FALSE;
  725. }
  726. #elif defined(__linux__)
  727. static void UnixAbortHandler(int signo)
  728. {
  729. ahType type = ahInterrupt;
  730. if (SIGTERM == signo)
  731. type = ahTerminate;
  732. hadAbortSignal = true;
  733. if (handlers.length()==0 || notifyOnAbort(type))
  734. {
  735. _exit(0);
  736. }
  737. }
  738. #endif
  739. void queryInstallAbortHandler()
  740. {
  741. if (handlerInstalled)
  742. return;
  743. #if defined(_WIN32)
  744. SetConsoleCtrlHandler( WindowsAbortHandler, TRUE );
  745. #elif defined(__linux__)
  746. struct sigaction action;
  747. sigemptyset(&action.sa_mask);
  748. action.sa_flags = SA_RESTART;
  749. action.sa_handler = (void(*)(int))UnixAbortHandler;
  750. if (sigaction(SIGINT, &action, NULL) == -1 ||
  751. sigaction(SIGQUIT, &action, NULL) == -1 ||
  752. sigaction(SIGTERM, &action, NULL) == -1)
  753. {
  754. perror("sigaction in queryInstallAbortHandler failed");
  755. }
  756. #endif
  757. handlerInstalled = true;
  758. }
  759. void queryUninstallAbortHandler()
  760. {
  761. if (handlers.ordinality())
  762. return;
  763. #if defined(_WIN32)
  764. if (handlerInstalled)
  765. {
  766. SetConsoleCtrlHandler( WindowsAbortHandler, FALSE);
  767. handlerInstalled = false;
  768. }
  769. #else
  770. // Don't uninstall - we always want one for the module exit support
  771. #endif
  772. }
  773. MODULE_INIT(INIT_PRIORITY_JMISC2)
  774. {
  775. #if defined(_WIN32)
  776. // NOTE: handlers are called in LIFO order and hence any handler that returns false
  777. // (e.g CTRL-C not wanting to abort)) will stop this handler being called also (correctly).
  778. SetConsoleCtrlHandler( ModuleExitHandler, TRUE);
  779. #elif defined(__linux__)
  780. queryInstallAbortHandler();
  781. #endif
  782. return true;
  783. }
  784. void addAbortHandler(AbortHandler handler)
  785. {
  786. CriticalBlock c(abortCrit);
  787. queryInstallAbortHandler();
  788. handlers.append(*new AbortHandlerInfo(handler));
  789. }
  790. void addAbortHandler(SimpleAbortHandler handler)
  791. {
  792. CriticalBlock c(abortCrit);
  793. queryInstallAbortHandler();
  794. handlers.append(*new AbortHandlerInfo(handler));
  795. }
  796. void addAbortHandler(IAbortHandler & handler)
  797. {
  798. CriticalBlock c(abortCrit);
  799. queryInstallAbortHandler();
  800. handlers.append(*new AbortHandlerInfo(&handler));
  801. }
  802. void removeAbortHandler(AbortHandler handler)
  803. {
  804. CriticalBlock c(abortCrit);
  805. ForEachItemInRev(idx, handlers)
  806. {
  807. if (handlers.item(idx).handler == handler)
  808. {
  809. handlers.remove(idx);
  810. break;
  811. }
  812. }
  813. queryUninstallAbortHandler();
  814. }
  815. void removeAbortHandler(SimpleAbortHandler handler)
  816. {
  817. CriticalBlock c(abortCrit);
  818. ForEachItemInRev(idx, handlers)
  819. {
  820. if (handlers.item(idx).shandler == handler)
  821. {
  822. handlers.remove(idx);
  823. break;
  824. }
  825. }
  826. queryUninstallAbortHandler();
  827. }
  828. void removeAbortHandler(IAbortHandler & handler)
  829. {
  830. CriticalBlock c(abortCrit);
  831. ForEachItemInRev(idx, handlers)
  832. {
  833. if (handlers.item(idx).ihandler == &handler)
  834. {
  835. handlers.remove(idx);
  836. break;
  837. }
  838. }
  839. queryUninstallAbortHandler();
  840. }
  841. bool isAborting()
  842. {
  843. return hadAbortSignal;
  844. }
  845. void throwAbortException()
  846. {
  847. throw MakeStringException(JLIBERR_UserAbort, "Operation aborted by user");
  848. }
  849. void throwExceptionIfAborting()
  850. {
  851. if (isAborting())
  852. throwAbortException();
  853. }
  854. //========================================================================================================================
  855. StringBuffer & hexdump2string(byte const * in, size32_t inSize, StringBuffer & out)
  856. {
  857. out.append("[");
  858. byte last = 0;
  859. unsigned seq = 1;
  860. for(unsigned i=0; i<inSize; ++i)
  861. {
  862. if((i>0) && (in[i]==last))
  863. {
  864. ++seq;
  865. }
  866. else
  867. {
  868. if(seq>1)
  869. {
  870. if(seq==2)
  871. out.appendf(" %02X", last);
  872. else
  873. out.appendf("x%u", seq);
  874. seq = 1;
  875. }
  876. out.appendf(" %02X", in[i]);
  877. last = in[i];
  878. }
  879. }
  880. if(seq>1)
  881. out.appendf("x%u", seq);
  882. out.append(" ]");
  883. return out;
  884. }
  885. jlib_decl bool getHomeDir(StringBuffer & homepath)
  886. {
  887. #ifdef _WIN32
  888. const char *home = getenv("APPDATA");
  889. // Not the 'official' way - which changes with every windows version
  890. // but should work well enough for us (and avoids sorting out windows include mess)
  891. #else
  892. const char *home = getenv("HOME");
  893. if (!home)
  894. {
  895. struct passwd *pw = getpwuid(getuid());
  896. home = pw->pw_dir;
  897. }
  898. #endif
  899. if (!home)
  900. return false;
  901. homepath.append(home);
  902. return true;
  903. }
  904. #ifdef _WIN32
  905. char *mkdtemp(char *_template)
  906. {
  907. if (!_template || strlen(_template) < 6 || !streq(_template+strlen(_template)-6, "XXXXXX"))
  908. {
  909. errno = EINVAL;
  910. return nullptr;
  911. }
  912. char * tail = _template + strlen(_template) - 6;
  913. for (int i = 0; i < 100; i++)
  914. {
  915. snprintf(tail, 7, "%06d", fastRand());
  916. if (!_mkdir(_template))
  917. return _template;
  918. if (errno != EEXIST)
  919. return nullptr;
  920. }
  921. errno = EINVAL;
  922. return nullptr;
  923. }
  924. #endif