sam.c 25 KB

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