sam.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. SAM_COMMAND_WARN_SIGNAL_SET,
  68. };
  69. enum sam_reply_t {
  70. SAM_REPLY_OK,
  71. SAM_REPLY_ERROR,
  72. };
  73. enum sam_parent_action_t {
  74. SAM_PARENT_ACTION_ERROR,
  75. SAM_PARENT_ACTION_RECOVERY,
  76. SAM_PARENT_ACTION_QUIT,
  77. SAM_PARENT_ACTION_CONTINUE
  78. };
  79. static struct {
  80. int time_interval;
  81. sam_recovery_policy_t recovery_policy;
  82. enum sam_internal_status_t internal_status;
  83. unsigned int instance_id;
  84. int child_fd_out;
  85. int child_fd_in;
  86. int term_send;
  87. int warn_signal;
  88. int am_i_child;
  89. sam_hc_callback_t hc_callback;
  90. pthread_t cb_thread;
  91. int cb_rpipe_fd, cb_wpipe_fd;
  92. int cb_registered;
  93. void *user_data;
  94. size_t user_data_size;
  95. size_t user_data_allocated;
  96. } sam_internal_data;
  97. cs_error_t sam_initialize (
  98. int time_interval,
  99. sam_recovery_policy_t recovery_policy)
  100. {
  101. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_NOT_INITIALIZED) {
  102. return (CS_ERR_BAD_HANDLE);
  103. }
  104. if (recovery_policy != SAM_RECOVERY_POLICY_QUIT && recovery_policy != SAM_RECOVERY_POLICY_RESTART) {
  105. return (CS_ERR_INVALID_PARAM);
  106. }
  107. sam_internal_data.recovery_policy = recovery_policy;
  108. sam_internal_data.time_interval = time_interval;
  109. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_INITIALIZED;
  110. sam_internal_data.warn_signal = SIGTERM;
  111. sam_internal_data.am_i_child = 0;
  112. sam_internal_data.user_data = NULL;
  113. sam_internal_data.user_data_size = 0;
  114. sam_internal_data.user_data_allocated = 0;
  115. return (CS_OK);
  116. }
  117. /*
  118. * Wrapper on top of write(2) function. It handles EAGAIN and EINTR states and sends whole buffer if possible.
  119. */
  120. static size_t sam_safe_write (
  121. int d,
  122. const void *buf,
  123. size_t nbyte)
  124. {
  125. ssize_t bytes_write;
  126. ssize_t tmp_bytes_write;
  127. bytes_write = 0;
  128. do {
  129. tmp_bytes_write = write (d, (const char *)buf + bytes_write,
  130. (nbyte - bytes_write > SSIZE_MAX) ? SSIZE_MAX : nbyte - bytes_write);
  131. if (tmp_bytes_write == -1) {
  132. if (!(errno == EAGAIN || errno == EINTR))
  133. return -1;
  134. } else {
  135. bytes_write += tmp_bytes_write;
  136. }
  137. } while (bytes_write != nbyte);
  138. return (bytes_write);
  139. }
  140. /*
  141. * Wrapper on top of read(2) function. It handles EAGAIN and EINTR states and reads whole buffer if possible.
  142. */
  143. static size_t sam_safe_read (
  144. int d,
  145. void *buf,
  146. size_t nbyte)
  147. {
  148. ssize_t bytes_read;
  149. ssize_t tmp_bytes_read;
  150. bytes_read = 0;
  151. do {
  152. tmp_bytes_read = read (d, (char *)buf + bytes_read,
  153. (nbyte - bytes_read > SSIZE_MAX) ? SSIZE_MAX : nbyte - bytes_read);
  154. if (tmp_bytes_read == -1) {
  155. if (!(errno == EAGAIN || errno == EINTR))
  156. return -1;
  157. } else {
  158. bytes_read += tmp_bytes_read;
  159. }
  160. } while (bytes_read != nbyte && tmp_bytes_read != 0);
  161. return (bytes_read);
  162. }
  163. static cs_error_t sam_read_reply (
  164. int child_fd_in)
  165. {
  166. char reply;
  167. cs_error_t err;
  168. if (sam_safe_read (sam_internal_data.child_fd_in, &reply, sizeof (reply)) != sizeof (reply)) {
  169. return (CS_ERR_LIBRARY);
  170. }
  171. switch (reply) {
  172. case SAM_REPLY_ERROR:
  173. /*
  174. * Read error and return that
  175. */
  176. if (sam_safe_read (sam_internal_data.child_fd_in, &err, sizeof (err)) != sizeof (err)) {
  177. return (CS_ERR_LIBRARY);
  178. }
  179. return (err);
  180. break;
  181. case SAM_REPLY_OK:
  182. /*
  183. * Everything correct
  184. */
  185. break;
  186. default:
  187. return (CS_ERR_LIBRARY);
  188. break;
  189. }
  190. return (CS_OK);
  191. }
  192. cs_error_t sam_data_getsize (size_t *size)
  193. {
  194. if (size == NULL) {
  195. return (CS_ERR_INVALID_PARAM);
  196. }
  197. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  198. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  199. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  200. return (CS_ERR_BAD_HANDLE);
  201. }
  202. *size = sam_internal_data.user_data_size;
  203. return (CS_OK);
  204. }
  205. cs_error_t sam_data_restore (
  206. void *data,
  207. size_t size)
  208. {
  209. if (data == NULL) {
  210. return (CS_ERR_INVALID_PARAM);
  211. }
  212. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  213. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  214. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  215. return (CS_ERR_BAD_HANDLE);
  216. }
  217. if (sam_internal_data.user_data_size == 0) {
  218. return (CS_OK);
  219. }
  220. if (size < sam_internal_data.user_data_size) {
  221. return (CS_ERR_INVALID_PARAM);
  222. }
  223. memcpy (data, sam_internal_data.user_data, sam_internal_data.user_data_size);
  224. return (CS_OK);
  225. }
  226. cs_error_t sam_data_store (
  227. const void *data,
  228. size_t size)
  229. {
  230. cs_error_t err;
  231. char command;
  232. char *new_data;
  233. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  234. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  235. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  236. return (CS_ERR_BAD_HANDLE);
  237. }
  238. if (sam_internal_data.user_data_allocated < size) {
  239. if ((new_data = realloc (sam_internal_data.user_data, size)) == NULL) {
  240. return (CS_ERR_NO_MEMORY);
  241. }
  242. sam_internal_data.user_data_allocated = size;
  243. } else {
  244. new_data = sam_internal_data.user_data;
  245. }
  246. if (data == NULL) {
  247. size = 0;
  248. }
  249. if (sam_internal_data.am_i_child) {
  250. /*
  251. * We are child so we must send data to parent
  252. */
  253. command = SAM_COMMAND_DATA_STORE;
  254. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  255. return (CS_ERR_LIBRARY);
  256. }
  257. if (sam_safe_write (sam_internal_data.child_fd_out, &size, sizeof (size)) != sizeof (size)) {
  258. return (CS_ERR_LIBRARY);
  259. }
  260. if (data != NULL && sam_safe_write (sam_internal_data.child_fd_out, data, size) != size) {
  261. return (CS_ERR_LIBRARY);
  262. }
  263. /*
  264. * And wait for reply
  265. */
  266. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  267. return (err);
  268. }
  269. }
  270. /*
  271. * We are parent or we received OK reply from parent -> do required action
  272. */
  273. if (data == NULL) {
  274. free (sam_internal_data.user_data);
  275. sam_internal_data.user_data = NULL;
  276. sam_internal_data.user_data_allocated = 0;
  277. sam_internal_data.user_data_size = 0;
  278. } else {
  279. sam_internal_data.user_data = new_data;
  280. sam_internal_data.user_data_size = size;
  281. memcpy (sam_internal_data.user_data, data, size);
  282. }
  283. return (CS_OK);
  284. }
  285. cs_error_t sam_start (void)
  286. {
  287. char command;
  288. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  289. return (CS_ERR_BAD_HANDLE);
  290. }
  291. command = SAM_COMMAND_START;
  292. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  293. return (CS_ERR_LIBRARY);
  294. if (sam_internal_data.hc_callback)
  295. if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
  296. return (CS_ERR_LIBRARY);
  297. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_STARTED;
  298. return (CS_OK);
  299. }
  300. cs_error_t sam_stop (void)
  301. {
  302. char command;
  303. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  304. return (CS_ERR_BAD_HANDLE);
  305. }
  306. command = SAM_COMMAND_STOP;
  307. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  308. return (CS_ERR_LIBRARY);
  309. if (sam_internal_data.hc_callback)
  310. if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
  311. return (CS_ERR_LIBRARY);
  312. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;
  313. return (CS_OK);
  314. }
  315. cs_error_t sam_hc_send (void)
  316. {
  317. char command;
  318. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  319. return (CS_ERR_BAD_HANDLE);
  320. }
  321. command = SAM_COMMAND_HB;
  322. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  323. return (CS_ERR_LIBRARY);
  324. return (CS_OK);
  325. }
  326. cs_error_t sam_finalize (void)
  327. {
  328. cs_error_t error;
  329. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  330. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  331. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  332. return (CS_ERR_BAD_HANDLE);
  333. }
  334. if (sam_internal_data.internal_status == SAM_INTERNAL_STATUS_STARTED) {
  335. error = sam_stop ();
  336. if (error != CS_OK)
  337. goto exit_error;
  338. }
  339. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_FINALIZED;
  340. free (sam_internal_data.user_data);
  341. exit_error:
  342. return (CS_OK);
  343. }
  344. cs_error_t sam_warn_signal_set (int warn_signal)
  345. {
  346. char command;
  347. cs_error_t err;
  348. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  349. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  350. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  351. return (CS_ERR_BAD_HANDLE);
  352. }
  353. if (sam_internal_data.am_i_child) {
  354. /*
  355. * We are child so we must send data to parent
  356. */
  357. command = SAM_COMMAND_WARN_SIGNAL_SET;
  358. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  359. return (CS_ERR_LIBRARY);
  360. }
  361. if (sam_safe_write (sam_internal_data.child_fd_out, &warn_signal, sizeof (warn_signal)) !=
  362. sizeof (warn_signal)) {
  363. return (CS_ERR_LIBRARY);
  364. }
  365. /*
  366. * And wait for reply
  367. */
  368. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  369. return (err);
  370. }
  371. }
  372. /*
  373. * We are parent or we received OK reply from parent -> do required action
  374. */
  375. sam_internal_data.warn_signal = warn_signal;
  376. return (CS_OK);
  377. }
  378. static cs_error_t sam_parent_warn_signal_set (
  379. int parent_fd_in,
  380. int parent_fd_out)
  381. {
  382. char reply;
  383. char *user_data;
  384. int warn_signal;
  385. cs_error_t err;
  386. err = CS_OK;
  387. user_data = NULL;
  388. if (sam_safe_read (parent_fd_in, &warn_signal, sizeof (warn_signal)) != sizeof (warn_signal)) {
  389. err = CS_ERR_LIBRARY;
  390. goto error_reply;
  391. }
  392. err = sam_warn_signal_set (warn_signal);
  393. if (err != CS_OK) {
  394. goto error_reply;
  395. }
  396. reply = SAM_REPLY_OK;
  397. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  398. err = CS_ERR_LIBRARY;
  399. goto error_reply;
  400. }
  401. return (CS_OK);
  402. error_reply:
  403. reply = SAM_REPLY_ERROR;
  404. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  405. return (CS_ERR_LIBRARY);
  406. }
  407. if (sam_safe_write (parent_fd_out, &err, sizeof (err)) != sizeof (err)) {
  408. return (CS_ERR_LIBRARY);
  409. }
  410. return (err);
  411. }
  412. static cs_error_t sam_parent_data_store (
  413. int parent_fd_in,
  414. int parent_fd_out)
  415. {
  416. char reply;
  417. char *user_data;
  418. ssize_t size;
  419. cs_error_t err;
  420. err = CS_OK;
  421. user_data = NULL;
  422. if (sam_safe_read (parent_fd_in, &size, sizeof (size)) != sizeof (size)) {
  423. err = CS_ERR_LIBRARY;
  424. goto error_reply;
  425. }
  426. if (size > 0) {
  427. user_data = malloc (size);
  428. if (user_data == NULL) {
  429. err = CS_ERR_NO_MEMORY;
  430. goto error_reply;
  431. }
  432. if (sam_safe_read (parent_fd_in, user_data, size) != size) {
  433. err = CS_ERR_LIBRARY;
  434. goto free_error_reply;
  435. }
  436. }
  437. err = sam_data_store (user_data, size);
  438. if (err != CS_OK) {
  439. goto free_error_reply;
  440. }
  441. reply = SAM_REPLY_OK;
  442. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  443. err = CS_ERR_LIBRARY;
  444. goto free_error_reply;
  445. }
  446. free (user_data);
  447. return (CS_OK);
  448. free_error_reply:
  449. free (user_data);
  450. error_reply:
  451. reply = SAM_REPLY_ERROR;
  452. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  453. return (CS_ERR_LIBRARY);
  454. }
  455. if (sam_safe_write (parent_fd_out, &err, sizeof (err)) != sizeof (err)) {
  456. return (CS_ERR_LIBRARY);
  457. }
  458. return (err);
  459. }
  460. static enum sam_parent_action_t sam_parent_handler (
  461. int parent_fd_in,
  462. int parent_fd_out,
  463. pid_t child_pid)
  464. {
  465. int poll_error;
  466. int action;
  467. int status;
  468. ssize_t bytes_read;
  469. char command;
  470. int time_interval;
  471. struct pollfd pfds;
  472. status = 0;
  473. action = SAM_PARENT_ACTION_CONTINUE;
  474. while (action == SAM_PARENT_ACTION_CONTINUE) {
  475. pfds.fd = parent_fd_in;
  476. pfds.events = POLLIN;
  477. pfds.revents = 0;
  478. if (status == 1 && sam_internal_data.time_interval != 0) {
  479. time_interval = sam_internal_data.time_interval;
  480. } else {
  481. time_interval = -1;
  482. }
  483. poll_error = poll (&pfds, 1, time_interval);
  484. if (poll_error == -1) {
  485. /*
  486. * Error in poll
  487. * If it is EINTR, continue, otherwise QUIT
  488. */
  489. if (errno != EINTR) {
  490. action = SAM_PARENT_ACTION_ERROR;
  491. }
  492. }
  493. if (poll_error == 0) {
  494. /*
  495. * Time limit expires
  496. */
  497. if (status == 0) {
  498. action = SAM_PARENT_ACTION_QUIT;
  499. } else {
  500. /*
  501. * Kill child process
  502. */
  503. if (!sam_internal_data.term_send) {
  504. /*
  505. * We didn't send warn_signal yet.
  506. */
  507. kill (child_pid, sam_internal_data.warn_signal);
  508. sam_internal_data.term_send = 1;
  509. } else {
  510. /*
  511. * We sent child warning. Now, we will not be so nice
  512. */
  513. kill (child_pid, SIGKILL);
  514. action = SAM_PARENT_ACTION_RECOVERY;
  515. }
  516. }
  517. }
  518. if (poll_error > 0) {
  519. /*
  520. * We have EOF or command in pipe
  521. */
  522. bytes_read = sam_safe_read (parent_fd_in, &command, 1);
  523. if (bytes_read == 0) {
  524. /*
  525. * Handle EOF -> Take recovery action or quit if sam_start wasn't called
  526. */
  527. if (status == 0)
  528. action = SAM_PARENT_ACTION_QUIT;
  529. else
  530. action = SAM_PARENT_ACTION_RECOVERY;
  531. continue;
  532. }
  533. if (bytes_read == -1) {
  534. action = SAM_PARENT_ACTION_ERROR;
  535. goto action_exit;
  536. }
  537. /*
  538. * We have read command
  539. */
  540. switch (command) {
  541. case SAM_COMMAND_START:
  542. if (status == 0) {
  543. /*
  544. * Not started yet
  545. */
  546. status = 1;
  547. }
  548. break;
  549. case SAM_COMMAND_STOP:
  550. if (status == 1) {
  551. /*
  552. * Started
  553. */
  554. status = 0;
  555. }
  556. break;
  557. case SAM_COMMAND_DATA_STORE:
  558. sam_parent_data_store (parent_fd_in, parent_fd_out);
  559. break;
  560. case SAM_COMMAND_WARN_SIGNAL_SET:
  561. sam_parent_warn_signal_set (parent_fd_in, parent_fd_out);
  562. break;
  563. }
  564. } /* select_error > 0 */
  565. } /* action == SAM_PARENT_ACTION_CONTINUE */
  566. action_exit:
  567. return action;
  568. }
  569. cs_error_t sam_register (
  570. unsigned int *instance_id)
  571. {
  572. cs_error_t error;
  573. pid_t pid;
  574. int pipe_error;
  575. int pipe_fd_out[2], pipe_fd_in[2];
  576. enum sam_parent_action_t action;
  577. int child_status;
  578. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED) {
  579. return (CS_ERR_BAD_HANDLE);
  580. }
  581. error = CS_OK;
  582. while (1) {
  583. if ((pipe_error = pipe (pipe_fd_out)) != 0) {
  584. error = CS_ERR_LIBRARY;
  585. goto error_exit;
  586. }
  587. if ((pipe_error = pipe (pipe_fd_in)) != 0) {
  588. close (pipe_fd_out[0]);
  589. close (pipe_fd_out[1]);
  590. error = CS_ERR_LIBRARY;
  591. goto error_exit;
  592. }
  593. sam_internal_data.instance_id++;
  594. sam_internal_data.term_send = 0;
  595. pid = fork ();
  596. if (pid == -1) {
  597. /*
  598. * Fork error
  599. */
  600. sam_internal_data.instance_id--;
  601. error = CS_ERR_LIBRARY;
  602. goto error_exit;
  603. }
  604. if (pid == 0) {
  605. /*
  606. * Child process
  607. */
  608. close (pipe_fd_out[0]);
  609. close (pipe_fd_in[1]);
  610. sam_internal_data.child_fd_out = pipe_fd_out[1];
  611. sam_internal_data.child_fd_in = pipe_fd_in[0];
  612. if (instance_id)
  613. *instance_id = sam_internal_data.instance_id;
  614. sam_internal_data.am_i_child = 1;
  615. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;
  616. goto error_exit;
  617. } else {
  618. /*
  619. * Parent process
  620. */
  621. close (pipe_fd_out[1]);
  622. close (pipe_fd_in[0]);
  623. action = sam_parent_handler (pipe_fd_out[0], pipe_fd_in[1], pid);
  624. close (pipe_fd_out[0]);
  625. close (pipe_fd_in[1]);
  626. if (action == SAM_PARENT_ACTION_ERROR) {
  627. error = CS_ERR_LIBRARY;
  628. goto error_exit;
  629. }
  630. /*
  631. * We really don't like zombies
  632. */
  633. while (waitpid (pid, &child_status, 0) == -1 && errno == EINTR)
  634. ;
  635. if (action == SAM_PARENT_ACTION_RECOVERY) {
  636. if (sam_internal_data.recovery_policy == SAM_RECOVERY_POLICY_QUIT)
  637. action = SAM_PARENT_ACTION_QUIT;
  638. }
  639. if (action == SAM_PARENT_ACTION_QUIT) {
  640. exit (WEXITSTATUS (child_status));
  641. }
  642. }
  643. }
  644. error_exit:
  645. return (error);
  646. }
  647. static void *hc_callback_thread (void *unused_param)
  648. {
  649. int poll_error;
  650. int status;
  651. ssize_t bytes_readed;
  652. char command;
  653. int time_interval, tmp_time_interval;
  654. int counter;
  655. struct pollfd pfds;
  656. status = 0;
  657. counter = 0;
  658. time_interval = sam_internal_data.time_interval >> 2;
  659. while (1) {
  660. pfds.fd = sam_internal_data.cb_rpipe_fd;
  661. pfds.events = POLLIN;
  662. pfds.revents = 0;
  663. if (status == 1) {
  664. tmp_time_interval = time_interval;
  665. } else {
  666. tmp_time_interval = -1;
  667. }
  668. poll_error = poll (&pfds, 1, tmp_time_interval);
  669. if (poll_error == 0) {
  670. sam_hc_send ();
  671. counter++;
  672. if (counter >= 4) {
  673. if (sam_internal_data.hc_callback () != 0) {
  674. status = 3;
  675. }
  676. counter = 0;
  677. }
  678. }
  679. if (poll_error > 0) {
  680. bytes_readed = sam_safe_read (sam_internal_data.cb_rpipe_fd, &command, 1);
  681. if (bytes_readed > 0) {
  682. if (status == 0 && command == SAM_COMMAND_START)
  683. status = 1;
  684. if (status == 1 && command == SAM_COMMAND_STOP)
  685. status = 0;
  686. }
  687. }
  688. }
  689. /*
  690. * This makes compiler happy, it's same as return (NULL);
  691. */
  692. return (unused_param);
  693. }
  694. cs_error_t sam_hc_callback_register (sam_hc_callback_t cb)
  695. {
  696. cs_error_t error = CS_OK;
  697. pthread_attr_t thread_attr;
  698. int pipe_error;
  699. int pipe_fd[2];
  700. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  701. return (CS_ERR_BAD_HANDLE);
  702. }
  703. if (sam_internal_data.time_interval == 0) {
  704. return (CS_ERR_INVALID_PARAM);
  705. }
  706. if (sam_internal_data.cb_registered) {
  707. sam_internal_data.hc_callback = cb;
  708. return (CS_OK);
  709. }
  710. /*
  711. * We know, this is first registration
  712. */
  713. if (cb == NULL) {
  714. return (CS_ERR_INVALID_PARAM);
  715. }
  716. pipe_error = pipe (pipe_fd);
  717. if (pipe_error != 0) {
  718. /*
  719. * Pipe creation error
  720. */
  721. error = CS_ERR_LIBRARY;
  722. goto error_exit;
  723. }
  724. sam_internal_data.cb_rpipe_fd = pipe_fd[0];
  725. sam_internal_data.cb_wpipe_fd = pipe_fd[1];
  726. /*
  727. * Create thread attributes
  728. */
  729. error = pthread_attr_init (&thread_attr);
  730. if (error != 0) {
  731. error = CS_ERR_LIBRARY;
  732. goto error_close_fd_exit;
  733. }
  734. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  735. pthread_attr_setstacksize (&thread_attr, 32768);
  736. /*
  737. * Create thread
  738. */
  739. error = pthread_create (&sam_internal_data.cb_thread, &thread_attr, hc_callback_thread, NULL);
  740. if (error != 0) {
  741. error = CS_ERR_LIBRARY;
  742. goto error_attr_destroy_exit;
  743. }
  744. /*
  745. * Cleanup
  746. */
  747. pthread_attr_destroy(&thread_attr);
  748. sam_internal_data.cb_registered = 1;
  749. sam_internal_data.hc_callback = cb;
  750. return (CS_OK);
  751. error_attr_destroy_exit:
  752. pthread_attr_destroy(&thread_attr);
  753. error_close_fd_exit:
  754. sam_internal_data.cb_rpipe_fd = sam_internal_data.cb_wpipe_fd = 0;
  755. close (pipe_fd[0]);
  756. close (pipe_fd[1]);
  757. error_exit:
  758. return (error);
  759. }