sam.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * Copyright (c) 2009-2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides a SAM API
  36. */
  37. #include <config.h>
  38. #include <limits.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <errno.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/coroipc_types.h>
  47. #include <corosync/coroipcc.h>
  48. #include <corosync/corodefs.h>
  49. #include <corosync/hdb.h>
  50. #include <corosync/sam.h>
  51. #include "util.h"
  52. #include <stdio.h>
  53. #include <sys/wait.h>
  54. #include <signal.h>
  55. enum sam_internal_status_t {
  56. SAM_INTERNAL_STATUS_NOT_INITIALIZED = 0,
  57. SAM_INTERNAL_STATUS_INITIALIZED,
  58. SAM_INTERNAL_STATUS_REGISTERED,
  59. SAM_INTERNAL_STATUS_STARTED,
  60. SAM_INTERNAL_STATUS_FINALIZED
  61. };
  62. enum sam_command_t {
  63. SAM_COMMAND_START,
  64. SAM_COMMAND_STOP,
  65. SAM_COMMAND_HB,
  66. SAM_COMMAND_DATA_STORE,
  67. };
  68. enum sam_reply_t {
  69. SAM_REPLY_OK,
  70. SAM_REPLY_ERROR,
  71. };
  72. enum sam_parent_action_t {
  73. SAM_PARENT_ACTION_ERROR,
  74. SAM_PARENT_ACTION_RECOVERY,
  75. SAM_PARENT_ACTION_QUIT,
  76. SAM_PARENT_ACTION_CONTINUE
  77. };
  78. static struct {
  79. int time_interval;
  80. sam_recovery_policy_t recovery_policy;
  81. enum sam_internal_status_t internal_status;
  82. unsigned int instance_id;
  83. int child_fd_out;
  84. int child_fd_in;
  85. int term_send;
  86. int warn_signal;
  87. int am_i_child;
  88. sam_hc_callback_t hc_callback;
  89. pthread_t cb_thread;
  90. int cb_rpipe_fd, cb_wpipe_fd;
  91. int cb_registered;
  92. void *user_data;
  93. size_t user_data_size;
  94. size_t user_data_allocated;
  95. } sam_internal_data;
  96. cs_error_t sam_initialize (
  97. int time_interval,
  98. sam_recovery_policy_t recovery_policy)
  99. {
  100. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_NOT_INITIALIZED) {
  101. return (CS_ERR_BAD_HANDLE);
  102. }
  103. if (recovery_policy != SAM_RECOVERY_POLICY_QUIT && recovery_policy != SAM_RECOVERY_POLICY_RESTART) {
  104. return (CS_ERR_INVALID_PARAM);
  105. }
  106. sam_internal_data.recovery_policy = recovery_policy;
  107. sam_internal_data.time_interval = time_interval;
  108. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_INITIALIZED;
  109. sam_internal_data.warn_signal = SIGTERM;
  110. sam_internal_data.am_i_child = 0;
  111. sam_internal_data.user_data = NULL;
  112. sam_internal_data.user_data_size = 0;
  113. sam_internal_data.user_data_allocated = 0;
  114. return (CS_OK);
  115. }
  116. /*
  117. * Wrapper on top of write(2) function. It handles EAGAIN and EINTR states and sends whole buffer if possible.
  118. */
  119. static size_t sam_safe_write (
  120. int d,
  121. const void *buf,
  122. size_t nbyte)
  123. {
  124. ssize_t bytes_write;
  125. ssize_t tmp_bytes_write;
  126. bytes_write = 0;
  127. do {
  128. tmp_bytes_write = write (d, (const char *)buf + bytes_write,
  129. (nbyte - bytes_write > SSIZE_MAX) ? SSIZE_MAX : nbyte - bytes_write);
  130. if (tmp_bytes_write == -1) {
  131. if (!(errno == EAGAIN || errno == EINTR))
  132. return -1;
  133. } else {
  134. bytes_write += tmp_bytes_write;
  135. }
  136. } while (bytes_write != nbyte);
  137. return (bytes_write);
  138. }
  139. /*
  140. * Wrapper on top of read(2) function. It handles EAGAIN and EINTR states and reads whole buffer if possible.
  141. */
  142. static size_t sam_safe_read (
  143. int d,
  144. void *buf,
  145. size_t nbyte)
  146. {
  147. ssize_t bytes_read;
  148. ssize_t tmp_bytes_read;
  149. bytes_read = 0;
  150. do {
  151. tmp_bytes_read = read (d, (char *)buf + bytes_read,
  152. (nbyte - bytes_read > SSIZE_MAX) ? SSIZE_MAX : nbyte - bytes_read);
  153. if (tmp_bytes_read == -1) {
  154. if (!(errno == EAGAIN || errno == EINTR))
  155. return -1;
  156. } else {
  157. bytes_read += tmp_bytes_read;
  158. }
  159. } while (bytes_read != nbyte && tmp_bytes_read != 0);
  160. return (bytes_read);
  161. }
  162. cs_error_t sam_data_getsize (size_t *size)
  163. {
  164. if (size == NULL) {
  165. return (CS_ERR_INVALID_PARAM);
  166. }
  167. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  168. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  169. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  170. return (CS_ERR_BAD_HANDLE);
  171. }
  172. *size = sam_internal_data.user_data_size;
  173. return (CS_OK);
  174. }
  175. cs_error_t sam_data_restore (
  176. void *data,
  177. size_t size)
  178. {
  179. if (data == NULL) {
  180. return (CS_ERR_INVALID_PARAM);
  181. }
  182. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  183. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  184. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  185. return (CS_ERR_BAD_HANDLE);
  186. }
  187. if (sam_internal_data.user_data_size == 0) {
  188. return (CS_OK);
  189. }
  190. if (size < sam_internal_data.user_data_size) {
  191. return (CS_ERR_INVALID_PARAM);
  192. }
  193. memcpy (data, sam_internal_data.user_data, sam_internal_data.user_data_size);
  194. return (CS_OK);
  195. }
  196. cs_error_t sam_data_store (
  197. const void *data,
  198. size_t size)
  199. {
  200. cs_error_t err;
  201. char command;
  202. char *new_data;
  203. char reply;
  204. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  205. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  206. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  207. return (CS_ERR_BAD_HANDLE);
  208. }
  209. if (sam_internal_data.user_data_allocated < size) {
  210. if ((new_data = realloc (sam_internal_data.user_data, size)) == NULL) {
  211. return (CS_ERR_NO_MEMORY);
  212. }
  213. sam_internal_data.user_data_allocated = size;
  214. } else {
  215. new_data = sam_internal_data.user_data;
  216. }
  217. if (data == NULL) {
  218. size = 0;
  219. }
  220. if (sam_internal_data.am_i_child) {
  221. /*
  222. * We are child so we must send data to parent
  223. */
  224. command = SAM_COMMAND_DATA_STORE;
  225. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  226. return (CS_ERR_LIBRARY);
  227. }
  228. if (sam_safe_write (sam_internal_data.child_fd_out, &size, sizeof (size)) != sizeof (size)) {
  229. return (CS_ERR_LIBRARY);
  230. }
  231. if (data != NULL && sam_safe_write (sam_internal_data.child_fd_out, data, size) != size) {
  232. return (CS_ERR_LIBRARY);
  233. }
  234. /*
  235. * And wait for reply
  236. */
  237. if (sam_safe_read (sam_internal_data.child_fd_in, &reply, sizeof (reply)) != sizeof (reply)) {
  238. return (CS_ERR_LIBRARY);
  239. }
  240. switch (reply) {
  241. case SAM_REPLY_ERROR:
  242. /*
  243. * Read error and return that
  244. */
  245. if (sam_safe_read (sam_internal_data.child_fd_in, &err, sizeof (err)) != sizeof (err)) {
  246. return (CS_ERR_LIBRARY);
  247. }
  248. return (err);
  249. break;
  250. case SAM_REPLY_OK:
  251. /*
  252. * Everything correct
  253. */
  254. break;
  255. default:
  256. return (CS_ERR_LIBRARY);
  257. break;
  258. }
  259. }
  260. /*
  261. * We are parent or we received OK reply from parent -> do required action
  262. */
  263. if (data == NULL) {
  264. free (sam_internal_data.user_data);
  265. sam_internal_data.user_data = NULL;
  266. sam_internal_data.user_data_allocated = 0;
  267. sam_internal_data.user_data_size = 0;
  268. } else {
  269. sam_internal_data.user_data = new_data;
  270. sam_internal_data.user_data_size = size;
  271. memcpy (sam_internal_data.user_data, data, size);
  272. }
  273. return (CS_OK);
  274. }
  275. cs_error_t sam_start (void)
  276. {
  277. char command;
  278. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  279. return (CS_ERR_BAD_HANDLE);
  280. }
  281. command = SAM_COMMAND_START;
  282. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  283. return (CS_ERR_LIBRARY);
  284. if (sam_internal_data.hc_callback)
  285. if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
  286. return (CS_ERR_LIBRARY);
  287. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_STARTED;
  288. return (CS_OK);
  289. }
  290. cs_error_t sam_stop (void)
  291. {
  292. char command;
  293. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  294. return (CS_ERR_BAD_HANDLE);
  295. }
  296. command = SAM_COMMAND_STOP;
  297. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  298. return (CS_ERR_LIBRARY);
  299. if (sam_internal_data.hc_callback)
  300. if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
  301. return (CS_ERR_LIBRARY);
  302. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;
  303. return (CS_OK);
  304. }
  305. cs_error_t sam_hc_send (void)
  306. {
  307. char command;
  308. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  309. return (CS_ERR_BAD_HANDLE);
  310. }
  311. command = SAM_COMMAND_HB;
  312. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  313. return (CS_ERR_LIBRARY);
  314. return (CS_OK);
  315. }
  316. cs_error_t sam_finalize (void)
  317. {
  318. cs_error_t error;
  319. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  320. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  321. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  322. return (CS_ERR_BAD_HANDLE);
  323. }
  324. if (sam_internal_data.internal_status == SAM_INTERNAL_STATUS_STARTED) {
  325. error = sam_stop ();
  326. if (error != CS_OK)
  327. goto exit_error;
  328. }
  329. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_FINALIZED;
  330. free (sam_internal_data.user_data);
  331. exit_error:
  332. return (CS_OK);
  333. }
  334. cs_error_t sam_warn_signal_set (int warn_signal)
  335. {
  336. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  337. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  338. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  339. return (CS_ERR_BAD_HANDLE);
  340. }
  341. sam_internal_data.warn_signal = warn_signal;
  342. return (CS_OK);
  343. }
  344. static cs_error_t sam_parent_data_store (
  345. int parent_fd_in,
  346. int parent_fd_out)
  347. {
  348. char reply;
  349. char *user_data;
  350. ssize_t size;
  351. cs_error_t err;
  352. err = CS_OK;
  353. user_data = NULL;
  354. if (sam_safe_read (parent_fd_in, &size, sizeof (size)) != sizeof (size)) {
  355. err = CS_ERR_LIBRARY;
  356. goto error_reply;
  357. }
  358. if (size > 0) {
  359. user_data = malloc (size);
  360. if (user_data == NULL) {
  361. err = CS_ERR_NO_MEMORY;
  362. goto error_reply;
  363. }
  364. if (sam_safe_read (parent_fd_in, user_data, size) != size) {
  365. err = CS_ERR_LIBRARY;
  366. goto free_error_reply;
  367. }
  368. }
  369. err = sam_data_store (user_data, size);
  370. if (err != CS_OK) {
  371. goto free_error_reply;
  372. }
  373. reply = SAM_REPLY_OK;
  374. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  375. err = CS_ERR_LIBRARY;
  376. goto free_error_reply;
  377. }
  378. free (user_data);
  379. return (CS_OK);
  380. free_error_reply:
  381. free (user_data);
  382. error_reply:
  383. reply = SAM_REPLY_ERROR;
  384. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  385. return (CS_ERR_LIBRARY);
  386. }
  387. if (sam_safe_write (parent_fd_out, &err, sizeof (err)) != sizeof (err)) {
  388. return (CS_ERR_LIBRARY);
  389. }
  390. return (err);
  391. }
  392. static enum sam_parent_action_t sam_parent_handler (
  393. int parent_fd_in,
  394. int parent_fd_out,
  395. pid_t child_pid)
  396. {
  397. int poll_error;
  398. int action;
  399. int status;
  400. ssize_t bytes_read;
  401. char command;
  402. int time_interval;
  403. struct pollfd pfds;
  404. status = 0;
  405. action = SAM_PARENT_ACTION_CONTINUE;
  406. while (action == SAM_PARENT_ACTION_CONTINUE) {
  407. pfds.fd = parent_fd_in;
  408. pfds.events = POLLIN;
  409. pfds.revents = 0;
  410. if (status == 1 && sam_internal_data.time_interval != 0) {
  411. time_interval = sam_internal_data.time_interval;
  412. } else {
  413. time_interval = -1;
  414. }
  415. poll_error = poll (&pfds, 1, time_interval);
  416. if (poll_error == -1) {
  417. /*
  418. * Error in poll
  419. * If it is EINTR, continue, otherwise QUIT
  420. */
  421. if (errno != EINTR) {
  422. action = SAM_PARENT_ACTION_ERROR;
  423. }
  424. }
  425. if (poll_error == 0) {
  426. /*
  427. * Time limit expires
  428. */
  429. if (status == 0) {
  430. action = SAM_PARENT_ACTION_QUIT;
  431. } else {
  432. /*
  433. * Kill child process
  434. */
  435. if (!sam_internal_data.term_send) {
  436. /*
  437. * We didn't send warn_signal yet.
  438. */
  439. kill (child_pid, sam_internal_data.warn_signal);
  440. sam_internal_data.term_send = 1;
  441. } else {
  442. /*
  443. * We sent child warning. Now, we will not be so nice
  444. */
  445. kill (child_pid, SIGKILL);
  446. action = SAM_PARENT_ACTION_RECOVERY;
  447. }
  448. }
  449. }
  450. if (poll_error > 0) {
  451. /*
  452. * We have EOF or command in pipe
  453. */
  454. bytes_read = sam_safe_read (parent_fd_in, &command, 1);
  455. if (bytes_read == 0) {
  456. /*
  457. * Handle EOF -> Take recovery action or quit if sam_start wasn't called
  458. */
  459. if (status == 0)
  460. action = SAM_PARENT_ACTION_QUIT;
  461. else
  462. action = SAM_PARENT_ACTION_RECOVERY;
  463. continue;
  464. }
  465. if (bytes_read == -1) {
  466. action = SAM_PARENT_ACTION_ERROR;
  467. goto action_exit;
  468. }
  469. /*
  470. * We have read command
  471. */
  472. switch (command) {
  473. case SAM_COMMAND_START:
  474. if (status == 0) {
  475. /*
  476. * Not started yet
  477. */
  478. status = 1;
  479. }
  480. break;
  481. case SAM_COMMAND_STOP:
  482. if (status == 1) {
  483. /*
  484. * Started
  485. */
  486. status = 0;
  487. }
  488. break;
  489. case SAM_COMMAND_DATA_STORE:
  490. sam_parent_data_store (parent_fd_in, parent_fd_out);
  491. break;
  492. }
  493. } /* select_error > 0 */
  494. } /* action == SAM_PARENT_ACTION_CONTINUE */
  495. action_exit:
  496. return action;
  497. }
  498. cs_error_t sam_register (
  499. unsigned int *instance_id)
  500. {
  501. cs_error_t error;
  502. pid_t pid;
  503. int pipe_error;
  504. int pipe_fd_out[2], pipe_fd_in[2];
  505. enum sam_parent_action_t action;
  506. int child_status;
  507. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED) {
  508. return (CS_ERR_BAD_HANDLE);
  509. }
  510. error = CS_OK;
  511. while (1) {
  512. if ((pipe_error = pipe (pipe_fd_out)) != 0) {
  513. error = CS_ERR_LIBRARY;
  514. goto error_exit;
  515. }
  516. if ((pipe_error = pipe (pipe_fd_in)) != 0) {
  517. close (pipe_fd_out[0]);
  518. close (pipe_fd_out[1]);
  519. error = CS_ERR_LIBRARY;
  520. goto error_exit;
  521. }
  522. sam_internal_data.instance_id++;
  523. sam_internal_data.term_send = 0;
  524. pid = fork ();
  525. if (pid == -1) {
  526. /*
  527. * Fork error
  528. */
  529. sam_internal_data.instance_id--;
  530. error = CS_ERR_LIBRARY;
  531. goto error_exit;
  532. }
  533. if (pid == 0) {
  534. /*
  535. * Child process
  536. */
  537. close (pipe_fd_out[0]);
  538. close (pipe_fd_in[1]);
  539. sam_internal_data.child_fd_out = pipe_fd_out[1];
  540. sam_internal_data.child_fd_in = pipe_fd_in[0];
  541. if (instance_id)
  542. *instance_id = sam_internal_data.instance_id;
  543. sam_internal_data.am_i_child = 1;
  544. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;
  545. goto error_exit;
  546. } else {
  547. /*
  548. * Parent process
  549. */
  550. close (pipe_fd_out[1]);
  551. close (pipe_fd_in[0]);
  552. action = sam_parent_handler (pipe_fd_out[0], pipe_fd_in[1], pid);
  553. close (pipe_fd_out[0]);
  554. close (pipe_fd_in[1]);
  555. if (action == SAM_PARENT_ACTION_ERROR) {
  556. error = CS_ERR_LIBRARY;
  557. goto error_exit;
  558. }
  559. /*
  560. * We really don't like zombies
  561. */
  562. while (waitpid (pid, &child_status, 0) == -1 && errno == EINTR)
  563. ;
  564. if (action == SAM_PARENT_ACTION_RECOVERY) {
  565. if (sam_internal_data.recovery_policy == SAM_RECOVERY_POLICY_QUIT)
  566. action = SAM_PARENT_ACTION_QUIT;
  567. }
  568. if (action == SAM_PARENT_ACTION_QUIT) {
  569. exit (WEXITSTATUS (child_status));
  570. }
  571. }
  572. }
  573. error_exit:
  574. return (error);
  575. }
  576. static void *hc_callback_thread (void *unused_param)
  577. {
  578. int poll_error;
  579. int status;
  580. ssize_t bytes_readed;
  581. char command;
  582. int time_interval, tmp_time_interval;
  583. int counter;
  584. struct pollfd pfds;
  585. status = 0;
  586. counter = 0;
  587. time_interval = sam_internal_data.time_interval >> 2;
  588. while (1) {
  589. pfds.fd = sam_internal_data.cb_rpipe_fd;
  590. pfds.events = POLLIN;
  591. pfds.revents = 0;
  592. if (status == 1) {
  593. tmp_time_interval = time_interval;
  594. } else {
  595. tmp_time_interval = -1;
  596. }
  597. poll_error = poll (&pfds, 1, tmp_time_interval);
  598. if (poll_error == 0) {
  599. sam_hc_send ();
  600. counter++;
  601. if (counter >= 4) {
  602. if (sam_internal_data.hc_callback () != 0) {
  603. status = 3;
  604. }
  605. counter = 0;
  606. }
  607. }
  608. if (poll_error > 0) {
  609. bytes_readed = sam_safe_read (sam_internal_data.cb_rpipe_fd, &command, 1);
  610. if (bytes_readed > 0) {
  611. if (status == 0 && command == SAM_COMMAND_START)
  612. status = 1;
  613. if (status == 1 && command == SAM_COMMAND_STOP)
  614. status = 0;
  615. }
  616. }
  617. }
  618. /*
  619. * This makes compiler happy, it's same as return (NULL);
  620. */
  621. return (unused_param);
  622. }
  623. cs_error_t sam_hc_callback_register (sam_hc_callback_t cb)
  624. {
  625. cs_error_t error = CS_OK;
  626. pthread_attr_t thread_attr;
  627. int pipe_error;
  628. int pipe_fd[2];
  629. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  630. return (CS_ERR_BAD_HANDLE);
  631. }
  632. if (sam_internal_data.time_interval == 0) {
  633. return (CS_ERR_INVALID_PARAM);
  634. }
  635. if (sam_internal_data.cb_registered) {
  636. sam_internal_data.hc_callback = cb;
  637. return (CS_OK);
  638. }
  639. /*
  640. * We know, this is first registration
  641. */
  642. if (cb == NULL) {
  643. return (CS_ERR_INVALID_PARAM);
  644. }
  645. pipe_error = pipe (pipe_fd);
  646. if (pipe_error != 0) {
  647. /*
  648. * Pipe creation error
  649. */
  650. error = CS_ERR_LIBRARY;
  651. goto error_exit;
  652. }
  653. sam_internal_data.cb_rpipe_fd = pipe_fd[0];
  654. sam_internal_data.cb_wpipe_fd = pipe_fd[1];
  655. /*
  656. * Create thread attributes
  657. */
  658. error = pthread_attr_init (&thread_attr);
  659. if (error != 0) {
  660. error = CS_ERR_LIBRARY;
  661. goto error_close_fd_exit;
  662. }
  663. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  664. pthread_attr_setstacksize (&thread_attr, 32768);
  665. /*
  666. * Create thread
  667. */
  668. error = pthread_create (&sam_internal_data.cb_thread, &thread_attr, hc_callback_thread, NULL);
  669. if (error != 0) {
  670. error = CS_ERR_LIBRARY;
  671. goto error_attr_destroy_exit;
  672. }
  673. /*
  674. * Cleanup
  675. */
  676. pthread_attr_destroy(&thread_attr);
  677. sam_internal_data.cb_registered = 1;
  678. sam_internal_data.hc_callback = cb;
  679. return (CS_OK);
  680. error_attr_destroy_exit:
  681. pthread_attr_destroy(&thread_attr);
  682. error_close_fd_exit:
  683. sam_internal_data.cb_rpipe_fd = sam_internal_data.cb_wpipe_fd = 0;
  684. close (pipe_fd[0]);
  685. close (pipe_fd[1]);
  686. error_exit:
  687. return (error);
  688. }