lscriu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: George Wang <gwang@litespeedtech.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /*
  19. Copyright (c) 2002-2018, Lite Speed Technologies Inc.
  20. All rights reserved.
  21. Redistribution and use in source and binary forms, with or without
  22. modification, are permitted provided that the following conditions are
  23. met:
  24. * Redistributions of source code must retain the above copyright
  25. notice, this list of conditions and the following disclaimer.
  26. * Redistributions in binary form must reproduce the above
  27. copyright notice, this list of conditions and the following
  28. disclaimer in the documentation and/or other materials provided
  29. with the distribution.
  30. * Neither the name of the Lite Speed Technologies Inc nor the
  31. names of its contributors may be used to endorse or promote
  32. products derived from this software without specific prior
  33. written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. #define HAVE_MSGHDR_MSG_CONTROL
  47. #include "lsapilib.h"
  48. #include <stdio.h>
  49. #if HAVE_STDLIB_H
  50. #include <stdlib.h>
  51. #endif
  52. #if HAVE_UNISTD_H
  53. #include <unistd.h>
  54. #endif
  55. #include <sys/wait.h>
  56. #include <sys/stat.h>
  57. #if HAVE_SYS_TYPES_H
  58. #include <sys/types.h>
  59. #endif
  60. #include <sys/types.h>
  61. #include <sys/socket.h>
  62. #include <sys/un.h>
  63. #include <arpa/inet.h>
  64. #include <netinet/in.h>
  65. #include <semaphore.h>
  66. #include <sys/mman.h>
  67. #include <fcntl.h>
  68. #include <dlfcn.h>
  69. #include <stdlib.h>
  70. #include <errno.h>
  71. #include <string.h>
  72. #include <stdarg.h>
  73. #include <signal.h>
  74. #include <time.h>
  75. #include <sys/timeb.h>
  76. #include <unistd.h>
  77. #include "lscriu.h"
  78. #define LSCRIU_PATH 256
  79. // Begin CRIU inclusion
  80. //CRIU inclusion
  81. static int s_initial_start_reqs = 0;
  82. static int s_requests_count = 0;
  83. static int s_restored = 0;
  84. static int (*s_lscapi_dump_me)() = NULL;
  85. static int (*s_lscapi_prepare_me)() = NULL;
  86. static int s_native = 0;
  87. static int s_tried_checkpoint = 0;
  88. static int s_criu_debug = 0;
  89. static int s_fd_native = -1;
  90. static char *s_criu_image_path = NULL;
  91. static int s_pid = 0;
  92. typedef enum
  93. {
  94. CRIU_GCOUNTER_SHM,
  95. CRIU_GCOUNTER_SIG,
  96. CRIU_GCOUNTER_PIPE
  97. } GlobalCounterType_t;
  98. static GlobalCounterType_t s_global_counter_type = CRIU_GCOUNTER_SHM;
  99. #ifndef sighandler_t
  100. typedef void (*sighandler_t)(int);
  101. #endif
  102. void lsapi_perror( const char * pMessage, int err_no );
  103. void LSAPI_reset_server_state( void );
  104. int LSAPI_Get_ppid();
  105. #ifdef LSAPILIB_DEBUG_CRIU
  106. #define lscriu_dbg(...) \
  107. do { if (s_criu_debug) fprintf(stderr, __VA_ARGS__); } while(0)
  108. #else
  109. #define lscriu_dbg(...)
  110. #endif
  111. #define lscriu_err(...) fprintf(stderr, __VA_ARGS__)
  112. #define SUN_PATH_MAX (sizeof(((struct sockaddr_un *)NULL)->sun_path))
  113. typedef struct
  114. {
  115. pid_t m_iPidToDump;
  116. char m_chImageDirectory[1024];
  117. char m_chSocketDir[SUN_PATH_MAX];
  118. char m_chServiceAddress[SUN_PATH_MAX];
  119. } criu_native_dump_t;
  120. typedef struct
  121. {
  122. int m_iDumpResult;
  123. char m_chDumpResponseMessage[1024];
  124. } criu_native_dump_response_t;
  125. typedef sem_t * (*psem_open_t) (const char *__name, int __oflag, ...);
  126. typedef int (*psem_post_t) (sem_t *__sem);
  127. typedef int (*psem_close_t) (sem_t *__sem);
  128. psem_open_t psem_open = NULL;
  129. psem_post_t psem_post = NULL;
  130. psem_close_t psem_close = NULL;
  131. static void lsapi_criu_signal(int signo, sighandler_t handler)
  132. {
  133. struct sigaction sa;
  134. sigaction(signo, NULL, &sa);
  135. if (sa.sa_handler == SIG_DFL) {
  136. sigemptyset(&sa.sa_mask);
  137. sa.sa_flags = 0;
  138. sa.sa_handler = handler;
  139. sigaction(signo, &sa, NULL);
  140. }
  141. }
  142. static void lsapi_siguser2(int sig)
  143. {
  144. // child requests counter for master process
  145. ++s_requests_count;
  146. }
  147. static void LSCRIU_Set_Initial_Start_Reqs(int reqs)
  148. {
  149. s_initial_start_reqs = reqs;
  150. }
  151. static void LSCRIU_Set_Global_Counter_Type(GlobalCounterType_t tp)
  152. {
  153. if ((tp == CRIU_GCOUNTER_SHM) || (tp == CRIU_GCOUNTER_SIG)
  154. || (tp == CRIU_GCOUNTER_PIPE)) {
  155. s_global_counter_type = tp;
  156. } else {
  157. s_global_counter_type = CRIU_GCOUNTER_SHM;
  158. }
  159. }
  160. static int LSCRIU_Get_Global_Counter_Type(void)
  161. {
  162. return s_global_counter_type;
  163. }
  164. static int LSCRIU_Init_Global_Counter(int value)
  165. {
  166. if (LSCRIU_Get_Global_Counter_Type() != CRIU_GCOUNTER_SHM
  167. || !s_initial_start_reqs) {
  168. return 0;
  169. }
  170. return 0;
  171. }
  172. static void LSCRIU_Increase_Global_Counter(void)
  173. {
  174. if (LSCRIU_Get_Global_Counter_Type() != CRIU_GCOUNTER_SHM
  175. || !s_initial_start_reqs) {
  176. return;
  177. }
  178. s_requests_count = LSAPI_Inc_Req_Processed(1);
  179. }
  180. static void LSCRIU_Get_Global_Counter(void)
  181. {
  182. if (!s_initial_start_reqs) {
  183. return;
  184. }
  185. s_requests_count = LSAPI_Inc_Req_Processed(0);
  186. }
  187. static int LSCRIU_need_checkpoint(void)
  188. {
  189. if (!s_initial_start_reqs) {
  190. return 0;
  191. }
  192. if (LSCRIU_Get_Global_Counter_Type() == CRIU_GCOUNTER_SHM
  193. && s_requests_count <= s_initial_start_reqs) {
  194. LSCRIU_Get_Global_Counter();
  195. }
  196. if (s_initial_start_reqs > 0
  197. && s_requests_count >= s_initial_start_reqs) {
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. static int LSCRIU_load_liblscapi(void)
  203. {
  204. void *lib_handle = NULL;
  205. void *pthread_lib_handle = NULL;
  206. if (s_native)
  207. return 0;
  208. // Numerical signals indicates Apache
  209. int error = 1;
  210. char *last;
  211. if (!(lib_handle = dlopen(last = "liblscapi.so", RTLD_LAZY)) /*||
  212. !(pthread_lib_handle = dlopen(last = "libpthread.so", RTLD_LAZY))*/)
  213. fprintf(stderr, "LSCRIU (%d): failed to dlopen %s: %s - ignore CRIU\n",
  214. s_pid, last, dlerror());
  215. else if (!(s_lscapi_dump_me = dlsym(lib_handle, last = "lscapi_dump_me")) ||
  216. !(s_lscapi_prepare_me = dlsym(lib_handle, last = "lscapi_prepare_me")) ||
  217. !(psem_open = dlsym(pthread_lib_handle, last = "sem_open")) ||
  218. !(psem_post = dlsym(pthread_lib_handle, last = "sem_post")) ||
  219. !(psem_close = dlsym(pthread_lib_handle, last = "sem_close")))
  220. fprintf(stderr, "LSCRIU (%d): failed to dlsym %s: %s - ignore CRIU\n",
  221. s_pid, last, dlerror());
  222. else
  223. error = 0;
  224. if (error) {
  225. // close the dll handles so we release the resources
  226. if (lib_handle)
  227. dlclose(lib_handle);
  228. if (pthread_lib_handle)
  229. dlclose(pthread_lib_handle);
  230. return -1;
  231. }
  232. return 0;
  233. }
  234. static void LSCRIU_Wink_Server_is_Ready(void)
  235. {
  236. char sem_name[60];
  237. if (s_native) {
  238. // Not used for native
  239. return;
  240. }
  241. if (getenv("LSAPI_UNIQE"))
  242. snprintf(sem_name, sizeof sem_name - 1, "lsphp[hash=%s].is_ready",
  243. getenv("LSAPI_UNIQE"));
  244. else
  245. snprintf(sem_name, sizeof sem_name - 1, "lsphp[euid=0x%x].is_ready",
  246. geteuid());
  247. sem_t *is_ready_sem = psem_open(sem_name, O_RDWR);
  248. if (is_ready_sem) {
  249. if (psem_post(is_ready_sem) < 0)
  250. lsapi_perror(sem_name, errno);
  251. if (psem_close(is_ready_sem) < 0)
  252. lsapi_perror(sem_name, errno);
  253. }
  254. else if (errno != ENOENT)
  255. lsapi_perror(sem_name, errno);
  256. }
  257. static char *LSCRIU_Error_File_Name(char *pchFile, int max_len)
  258. {
  259. const char *pchDefaultSocketPath = "/tmp/";
  260. const char *pchDefaultLogFileName = "lsws_error.log";
  261. snprintf(pchFile, max_len, "%s%s", pchDefaultSocketPath,
  262. pchDefaultLogFileName);
  263. return pchFile;
  264. }
  265. #ifdef LSAPILIB_DEBUG_CRIU
  266. static void LSCRIU_Debugging(void) {
  267. char *pchCRIUDebug;
  268. pchCRIUDebug = getenv("LSAPI_CRIU_DEBUG");
  269. if (!pchCRIUDebug)
  270. pchCRIUDebug = getenv("LSCAPI_CRIU_DEBUG");
  271. //fprintf(stderr,"(%d) LSCRIU: CRIU debug environment variable: %s\n",
  272. // s_pid, pchCRIUDebug);
  273. // I've made it easy to turn on debugging. CloudLinux Apache sets
  274. // LSCAPI_CRIU_DEBUG to nothing to indicate it's on. Sigh.
  275. if ((!pchCRIUDebug) ||
  276. ((!*pchCRIUDebug) ||
  277. (*pchCRIUDebug == '0') ||
  278. (*pchCRIUDebug == 'f') ||
  279. (*pchCRIUDebug == 'F') ||
  280. (((*pchCRIUDebug == 'O') ||
  281. (*pchCRIUDebug == 'o')) &&
  282. ((*(pchCRIUDebug + 1)) &&
  283. ((*(pchCRIUDebug + 1) == 'F') || (*(pchCRIUDebug + 1) == 'f'))))))
  284. {
  285. lscriu_dbg("LSCRIU (%d): CRIU Debugging disabled by environment\n", s_pid);
  286. s_criu_debug = 0;
  287. }
  288. else {
  289. s_criu_debug = 1;
  290. lscriu_dbg("LSCRIU (%d): CRIU Debugging enabled by environment\n", s_pid);
  291. fprintf(stderr,"LSCRIU (%d): CRIU debug environment variable: %s\n",
  292. s_pid, pchCRIUDebug);
  293. }
  294. }
  295. static void LSCRIU_Restored_Error(int iFatal, char *format, ...) {
  296. // This routine deals with the awful situation of trying to get info while the stderr handle is closed on purpose.
  297. int iOldUMask;
  298. int iFd = -1;
  299. char chFile[1024];
  300. if (!iFatal) {
  301. // LSCRIU_Debugging();
  302. if (!s_criu_debug) {
  303. // Debugging message and debugging is off
  304. return;
  305. }
  306. }
  307. if (!LSCRIU_Error_File_Name(chFile, sizeof(chFile))) {
  308. // We're done here...nowhere to write
  309. return;
  310. }
  311. iOldUMask = umask(0);
  312. iFd = open( chFile, O_WRONLY | O_APPEND | O_CREAT,
  313. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  314. umask(iOldUMask);
  315. if (iFd >= 0) {
  316. char chFullMessage[0x1000];
  317. struct timeb sTimeb;
  318. struct tm sTm;
  319. ftime(&sTimeb);
  320. localtime_r(&sTimeb.time,&sTm);
  321. va_list ap;
  322. va_start(ap, format);
  323. char buf[0x1000];
  324. vsnprintf(buf, sizeof(buf), format, ap);
  325. va_end(ap);
  326. int n = snprintf(chFullMessage, sizeof(chFullMessage),
  327. "%04d-%02d-%02d %02d:%02d:%02d.%03d: LSCRIU (%d): %s %s\n",
  328. sTm.tm_year + 1900,
  329. sTm.tm_mon + 1,
  330. sTm.tm_mday,
  331. sTm.tm_hour,
  332. sTm.tm_min,
  333. sTm.tm_sec,
  334. sTimeb.millitm,
  335. s_pid,
  336. iFatal ? "FATAL! " : "(debug) ",
  337. buf);
  338. if (n > (int)sizeof(chFullMessage))
  339. n = sizeof(chFullMessage);
  340. write(iFd, chFullMessage, n);
  341. close(iFd);
  342. }
  343. }
  344. #else // no debugging
  345. static void inline LSCRIU_Debugging(void) {}
  346. static void inline LSCRIU_Restored_Error(int iFatal, char *format, ...) {}
  347. #endif
  348. static int LSCRIU_Native_Dump(pid_t iPid,
  349. char *pchImagePath,
  350. int iFdNative) {
  351. criu_native_dump_t criu_native_dump;
  352. char *pchLastSlash;
  353. criu_native_dump_response_t criu_native_dump_response;
  354. memset(&criu_native_dump, 0, sizeof(criu_native_dump));
  355. criu_native_dump.m_iPidToDump = iPid;
  356. strncpy(criu_native_dump.m_chImageDirectory, pchImagePath,
  357. sizeof(criu_native_dump.m_chImageDirectory));
  358. pchLastSlash = strrchr(criu_native_dump.m_chSocketDir,'/');
  359. if (pchLastSlash) {
  360. pchLastSlash++;
  361. (*pchLastSlash) = 0;
  362. }
  363. lscriu_dbg("LSCRIU (%d): Sent the dump request to the listener\n", s_pid);
  364. if (write(iFdNative,
  365. &criu_native_dump,
  366. sizeof(criu_native_dump)) == -1) {
  367. lscriu_err("LSCRIU (%d): Error sending dump request to the listener: %s\n",
  368. s_pid, strerror(errno));
  369. return(-1);
  370. }
  371. return 0;
  372. /* do not wait response.
  373. //while (sleep(7200));
  374. if (read(iFdNative,
  375. &criu_native_dump_response,
  376. sizeof(criu_native_dump_response)) == -1) {
  377. // The test will actually fail it!
  378. //LSCRIU_Restored_Error(1, "Error reading dump socket #%d from parent: %s",
  379. // iFdNative, strerror(errno));
  380. //return(-1);
  381. }
  382. return(-1);
  383. */
  384. }
  385. static void LSCRIU_CloudLinux_Checkpoint(void)
  386. {
  387. int iRet;
  388. if ((!s_native) && (!s_lscapi_dump_me)) {
  389. lscriu_dbg("LSCRIU (%d): Not native and unable to dump - abandon one-time "
  390. "dump\n", s_pid);
  391. return;
  392. }
  393. iRet = s_lscapi_dump_me();
  394. if (iRet < 0) {
  395. lscriu_err("LSCRIU: CloudLinux dump of PID: %d, error: %s\n",
  396. s_pid, strerror(errno));
  397. }
  398. if (iRet == 0) {
  399. // Dumped. To continue the child must send us the handles back
  400. lscriu_err("LSCRIU: Successful CloudLinux dump of PID: %d\n", s_pid);
  401. }
  402. else {
  403. s_restored = 1;
  404. LSAPI_reset_server_state();
  405. /*
  406. Here we have restored the php process, so we should to tell (via
  407. semaphore) mod_lsapi that we are started and ready to receive data.
  408. */
  409. LSCRIU_Wink_Server_is_Ready();
  410. lscriu_err("LSCRIU: Successful CloudLinux restore of PID: %d, parent: %d.\n",
  411. getpid(), getppid());
  412. }
  413. LSCRIU_Set_Initial_Start_Reqs(0);
  414. }
  415. static void LSCRIU_Wait_Dump_Finsh_Or_Restored(int pid_parent)
  416. {
  417. // Now get restored. We know if we're restored if the ppid changes!
  418. // If we're dumped, we're killed (no use worrying about that!).
  419. time_t iTimeStart = 0;
  420. time_t iTimeNow;
  421. int iRestored = 0;
  422. do {
  423. usleep(1000);
  424. time(&iTimeNow);
  425. if (!iTimeStart) {
  426. iTimeStart = iTimeNow;
  427. }
  428. else if ((pid_parent != getppid()) ||
  429. (iTimeNow - iTimeStart > 10)) {
  430. iRestored = 1;
  431. }
  432. else if (iTimeNow - iTimeStart > 5) {
  433. LSCRIU_Restored_Error(1, "Timed out waiting to be dumped");
  434. exit(1);
  435. }
  436. } while (!iRestored);
  437. }
  438. static void LSCRIU_try_checkpoint(int *forked_pid)
  439. {
  440. int iRet;
  441. pid_t iPid;
  442. pid_t iPidDump = getpid();
  443. if (s_tried_checkpoint) {
  444. lscriu_dbg("LSCRIU (%d): Already tried checkpoint - one time per customer\n",
  445. iPidDump);
  446. return;
  447. }
  448. lscriu_dbg("LSCRIU (%d): Trying checkpoint\n", iPidDump);
  449. s_tried_checkpoint = 1;
  450. if (!s_native) {
  451. LSCRIU_CloudLinux_Checkpoint();
  452. return;
  453. }
  454. lscriu_dbg("LSCRIU (%d): fork!\n", iPidDump);
  455. iPid = fork();
  456. if (iPid < 0) {
  457. lscriu_err("LSCRIU (%d): Can't checkpoint due to a fork error: %s\n",
  458. iPidDump, strerror(errno));
  459. return;
  460. }
  461. if (iPid == 0) {
  462. pid_t iPidParent = getppid();
  463. setsid();
  464. iRet = LSCRIU_Native_Dump(iPidDump,
  465. s_criu_image_path,
  466. s_fd_native);
  467. close(s_fd_native);
  468. LSCRIU_Wait_Dump_Finsh_Or_Restored(iPidParent);
  469. LSCRIU_Restored_Error(0, "Restored!");
  470. LSAPI_reset_server_state();
  471. s_restored = 1;
  472. }
  473. else {
  474. if (forked_pid)
  475. *forked_pid = iPid;
  476. }
  477. LSCRIU_Set_Initial_Start_Reqs(0);
  478. }
  479. static int init_native_env()
  480. {
  481. char *pchFd;
  482. pchFd = getenv("LSAPI_CRIU_SYNC_FD");
  483. if (!pchFd)
  484. pchFd = getenv("LSCAPI_CRIU_SYNC_FD");
  485. const char *image_path;
  486. image_path = getenv("LSAPI_CRIU_IMAGE_PATH");
  487. if (!pchFd) {
  488. lscriu_err("LSCRIU (%d): LSAPI_CRIU_SYNC_FD internal environment "
  489. "variable not set - contact Litespeed tech support\n", getpid());
  490. return -1;
  491. }
  492. if (!image_path) {
  493. lscriu_err("LSCRIU (%d): LSAPI_CRIU_IMAGE_PATH internal environment "
  494. "variable not set - contact Litespeed tech support\n", getpid());
  495. return -1;
  496. }
  497. lscriu_dbg("LSCRIU (%d): Checkpoint dump. ImagePath: %s\n",
  498. getpid(), image_path);
  499. s_fd_native = atoi(pchFd);
  500. lscriu_dbg("LSCRIU (%d): Native checkpoint. Use filepointer %d (%s) to send "
  501. "pid %d\n", getpid(), s_fd_native, pchFd, iPidDump);
  502. s_criu_image_path = strdup(image_path);
  503. return 0;
  504. }
  505. static int LSCRIU_Init_Env_Parameters(void)
  506. {
  507. const char *p;
  508. int n;
  509. p = getenv("LSAPI_INITIAL_START");
  510. if (!p)
  511. p = getenv("LSAPI_BACKEND_INITIAL_START");
  512. if (p) {
  513. n = atoi(p);
  514. if (n > 0) {
  515. lscriu_dbg("LSCRIU (%d): Set start requests based on environment (%d)\n",
  516. getpid(), n);
  517. LSCRIU_Set_Initial_Start_Reqs(n);
  518. } else {
  519. lscriu_dbg("LSCRIU (%d): LSAPI_INITIAL_START set to 0 disabled\n",
  520. getpid());
  521. return 0;
  522. }
  523. } else {
  524. lscriu_dbg("LSCRIU (%d): LSAPI_INITIAL_START NOT set - disabled\n",
  525. getpid());
  526. return 0;
  527. }
  528. if (LSAPI_Is_Listen()) {
  529. lscriu_dbg("LSCRIU (%d): Listening...\n", getpid());
  530. GlobalCounterType_t gc_type = CRIU_GCOUNTER_SHM;
  531. char *env;
  532. if ((env = getenv("LSAPI_CRIU_USE_SHM"))) {
  533. // CloudLinux doc: Off (shared memory) or Signals.
  534. // Litespeed doc: On (shared memory) or Signals
  535. // So just check the first character for an 'S' and if not, then
  536. // use shared memory. Pipe support is lost (sigh).
  537. if ((*env == 'S') || (*env == 's'))
  538. gc_type = CRIU_GCOUNTER_SIG; // Just assume the rest is signals
  539. // else use the default of shared memory
  540. }
  541. else if ((env = getenv("LSAPI_SIGNALS"))) {
  542. if ((*env == '1') ||
  543. (*env == 'Y') ||
  544. (*env == 'y') ||
  545. (*env == 'T') ||
  546. (*env == 't') ||
  547. (((*env == 'O') || (*env == 'o')) &&
  548. ((*(env + 1) == 'N') || (*(env + 1) == 'n'))))
  549. gc_type = CRIU_GCOUNTER_SIG;
  550. else if (*env == 2)
  551. gc_type = CRIU_GCOUNTER_PIPE; // The only case for pipe
  552. //else use the default of shared memory
  553. }
  554. if (gc_type != CRIU_GCOUNTER_SHM) {
  555. lscriu_dbg("LSCRIU (%d): Use %s\n", getpid(),
  556. gc_type == CRIU_GCOUNTER_SIG ? "signals" : "pipe");
  557. lsapi_criu_signal(SIGUSR2, lsapi_siguser2);
  558. }
  559. else
  560. lscriu_dbg("LSCRIU (%d): Use shared memory\n", getpid());
  561. LSCRIU_Set_Global_Counter_Type(gc_type);
  562. }
  563. else
  564. lscriu_dbg("LSCRIU (%d): NOT Listening\n", getpid());
  565. char *criu_mode = NULL;
  566. criu_mode = getenv("LSAPI_CRIU");
  567. // 0 disabled
  568. // 1 cloudlinux
  569. // 2 native
  570. if (criu_mode) {
  571. if (*criu_mode == '0') {
  572. lscriu_dbg("LSCRIU (%d): Disabled by environment.\n", getpid());
  573. LSCRIU_Set_Initial_Start_Reqs(0);
  574. }
  575. else if (*criu_mode == '2') {
  576. lscriu_dbg("LSCRIU (%d): Disabled by environment.\n", getpid());
  577. s_native = 1;
  578. }
  579. }
  580. if (s_native && init_native_env() == -1)
  581. LSCRIU_Set_Initial_Start_Reqs(0);
  582. //unset_lsapi_envs();
  583. return 0;
  584. }
  585. void LSCRIU_inc_req_procssed()
  586. {
  587. if (!LSCRIU_Get_Global_Counter_Type()) {
  588. ++s_requests_count;
  589. }
  590. lscriu_dbg("LSCRIU (%d): s_requests_count %d counter %d\n", getpid(),
  591. s_requests_count, s_initial_start_reqs);
  592. if (s_initial_start_reqs > 0 && s_requests_count <= s_initial_start_reqs) {
  593. if (LSCRIU_Get_Global_Counter_Type() == CRIU_GCOUNTER_SHM) {
  594. LSCRIU_Increase_Global_Counter();
  595. if (s_requests_count >= s_initial_start_reqs) {
  596. //Maybe this time we can stop to send signal and wait for
  597. //1 second of select timeout
  598. //kill( LSCRIU_Get_ppid(), SIGUSR2 );
  599. lscriu_dbg("LSCRIU (%d): Time to dump main process with semaphore\n",
  600. getpid());
  601. }
  602. } else {
  603. kill(LSAPI_Get_ppid(), SIGUSR2);
  604. lscriu_dbg("LSCRIU (%d): Send kill to main process with signals\n",
  605. getpid());
  606. }
  607. }
  608. }
  609. static void LSCRIU_on_timer(int *forked_pid)
  610. {
  611. lscriu_dbg("LSCRIU (%d): LSCRIU_on_timer\n", getpid());
  612. if (LSCRIU_need_checkpoint()) {
  613. LSCRIU_try_checkpoint(forked_pid);
  614. }
  615. }
  616. int LSCRIU_Init(void)
  617. {
  618. s_pid = getpid();
  619. LSCRIU_Debugging();
  620. LSCRIU_Init_Env_Parameters();
  621. if (s_initial_start_reqs && !s_native) {
  622. if (LSCRIU_load_liblscapi() == -1)
  623. s_initial_start_reqs = 0;
  624. }
  625. if (s_initial_start_reqs) {
  626. LSCRIU_Wink_Server_is_Ready();
  627. lscriu_dbg("LSCRIU (%d): LSAPI_Register_Pgrp_Timer_Callback\n", s_pid);
  628. LSAPI_Register_Pgrp_Timer_Callback(LSCRIU_on_timer);
  629. LSCRIU_Init_Global_Counter(0);
  630. }
  631. return s_initial_start_reqs > 0;
  632. }