sam.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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 (data == NULL) {
  280. size = 0;
  281. }
  282. if (sam_internal_data.am_i_child) {
  283. /*
  284. * We are child so we must send data to parent
  285. */
  286. command = SAM_COMMAND_DATA_STORE;
  287. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  288. return (CS_ERR_LIBRARY);
  289. }
  290. if (sam_safe_write (sam_internal_data.child_fd_out, &size, sizeof (size)) != sizeof (size)) {
  291. return (CS_ERR_LIBRARY);
  292. }
  293. if (data != NULL && sam_safe_write (sam_internal_data.child_fd_out, data, size) != size) {
  294. return (CS_ERR_LIBRARY);
  295. }
  296. /*
  297. * And wait for reply
  298. */
  299. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  300. return (err);
  301. }
  302. }
  303. /*
  304. * We are parent or we received OK reply from parent -> do required action
  305. */
  306. if (data == NULL) {
  307. free (sam_internal_data.user_data);
  308. sam_internal_data.user_data = NULL;
  309. sam_internal_data.user_data_allocated = 0;
  310. sam_internal_data.user_data_size = 0;
  311. } else {
  312. if (sam_internal_data.user_data_allocated < size) {
  313. if ((new_data = realloc (sam_internal_data.user_data, size)) == NULL) {
  314. return (CS_ERR_NO_MEMORY);
  315. }
  316. sam_internal_data.user_data_allocated = size;
  317. } else {
  318. new_data = sam_internal_data.user_data;
  319. }
  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. if (sam_hc_send () == CS_OK) {
  825. counter++;
  826. }
  827. if (counter >= 4) {
  828. if (sam_internal_data.hc_callback () != 0) {
  829. status = 3;
  830. }
  831. counter = 0;
  832. }
  833. }
  834. if (poll_error > 0) {
  835. bytes_readed = sam_safe_read (sam_internal_data.cb_rpipe_fd, &command, 1);
  836. if (bytes_readed > 0) {
  837. if (status == 0 && command == SAM_COMMAND_START)
  838. status = 1;
  839. if (status == 1 && command == SAM_COMMAND_STOP)
  840. status = 0;
  841. }
  842. }
  843. }
  844. /*
  845. * This makes compiler happy, it's same as return (NULL);
  846. */
  847. return (unused_param);
  848. }
  849. cs_error_t sam_hc_callback_register (sam_hc_callback_t cb)
  850. {
  851. cs_error_t error = CS_OK;
  852. pthread_attr_t thread_attr;
  853. int pipe_error;
  854. int pipe_fd[2];
  855. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  856. return (CS_ERR_BAD_HANDLE);
  857. }
  858. if (sam_internal_data.time_interval == 0) {
  859. return (CS_ERR_INVALID_PARAM);
  860. }
  861. if (sam_internal_data.cb_registered) {
  862. sam_internal_data.hc_callback = cb;
  863. return (CS_OK);
  864. }
  865. /*
  866. * We know, this is first registration
  867. */
  868. if (cb == NULL) {
  869. return (CS_ERR_INVALID_PARAM);
  870. }
  871. pipe_error = pipe (pipe_fd);
  872. if (pipe_error != 0) {
  873. /*
  874. * Pipe creation error
  875. */
  876. error = CS_ERR_LIBRARY;
  877. goto error_exit;
  878. }
  879. sam_internal_data.cb_rpipe_fd = pipe_fd[0];
  880. sam_internal_data.cb_wpipe_fd = pipe_fd[1];
  881. /*
  882. * Create thread attributes
  883. */
  884. error = pthread_attr_init (&thread_attr);
  885. if (error != 0) {
  886. error = CS_ERR_LIBRARY;
  887. goto error_close_fd_exit;
  888. }
  889. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  890. pthread_attr_setstacksize (&thread_attr, 32768);
  891. /*
  892. * Create thread
  893. */
  894. error = pthread_create (&sam_internal_data.cb_thread, &thread_attr, hc_callback_thread, NULL);
  895. if (error != 0) {
  896. error = CS_ERR_LIBRARY;
  897. goto error_attr_destroy_exit;
  898. }
  899. /*
  900. * Cleanup
  901. */
  902. pthread_attr_destroy(&thread_attr);
  903. sam_internal_data.cb_registered = 1;
  904. sam_internal_data.hc_callback = cb;
  905. return (CS_OK);
  906. error_attr_destroy_exit:
  907. pthread_attr_destroy(&thread_attr);
  908. error_close_fd_exit:
  909. sam_internal_data.cb_rpipe_fd = sam_internal_data.cb_wpipe_fd = 0;
  910. close (pipe_fd[0]);
  911. close (pipe_fd[1]);
  912. error_exit:
  913. return (error);
  914. }