sam.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /*
  2. * Copyright (c) 2009-2011 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/time.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <errno.h>
  46. #include <poll.h>
  47. #include <corosync/corotypes.h>
  48. #include <qb/qbipcc.h>
  49. #include <corosync/corodefs.h>
  50. #include <corosync/cmap.h>
  51. #include <corosync/hdb.h>
  52. #include <corosync/quorum.h>
  53. #include <corosync/sam.h>
  54. #include "util.h"
  55. #include <stdio.h>
  56. #include <sys/wait.h>
  57. #include <signal.h>
  58. #define SAM_CMAP_S_FAILED "failed"
  59. #define SAM_CMAP_S_REGISTERED "stopped"
  60. #define SAM_CMAP_S_STARTED "running"
  61. #define SAM_CMAP_S_Q_WAIT "waiting for quorum"
  62. #define SAM_RP_MASK_Q(pol) (pol & (~SAM_RECOVERY_POLICY_QUORUM))
  63. #define SAM_RP_MASK_C(pol) (pol & (~SAM_RECOVERY_POLICY_CMAP))
  64. #define SAM_RP_MASK(pol) (pol & (~(SAM_RECOVERY_POLICY_QUORUM | SAM_RECOVERY_POLICY_CMAP)))
  65. enum sam_internal_status_t {
  66. SAM_INTERNAL_STATUS_NOT_INITIALIZED = 0,
  67. SAM_INTERNAL_STATUS_INITIALIZED,
  68. SAM_INTERNAL_STATUS_REGISTERED,
  69. SAM_INTERNAL_STATUS_STARTED,
  70. SAM_INTERNAL_STATUS_FINALIZED
  71. };
  72. enum sam_command_t {
  73. SAM_COMMAND_START,
  74. SAM_COMMAND_STOP,
  75. SAM_COMMAND_HB,
  76. SAM_COMMAND_DATA_STORE,
  77. SAM_COMMAND_WARN_SIGNAL_SET,
  78. SAM_COMMAND_MARK_FAILED,
  79. };
  80. enum sam_reply_t {
  81. SAM_REPLY_OK,
  82. SAM_REPLY_ERROR,
  83. };
  84. enum sam_parent_action_t {
  85. SAM_PARENT_ACTION_ERROR,
  86. SAM_PARENT_ACTION_RECOVERY,
  87. SAM_PARENT_ACTION_QUIT,
  88. SAM_PARENT_ACTION_CONTINUE
  89. };
  90. enum sam_cmap_key_t {
  91. SAM_CMAP_KEY_RECOVERY,
  92. SAM_CMAP_KEY_HC_PERIOD,
  93. SAM_CMAP_KEY_LAST_HC,
  94. SAM_CMAP_KEY_STATE,
  95. };
  96. static struct {
  97. int time_interval;
  98. sam_recovery_policy_t recovery_policy;
  99. enum sam_internal_status_t internal_status;
  100. unsigned int instance_id;
  101. int child_fd_out;
  102. int child_fd_in;
  103. int term_send;
  104. int warn_signal;
  105. int am_i_child;
  106. sam_hc_callback_t hc_callback;
  107. pthread_t cb_thread;
  108. int cb_rpipe_fd, cb_wpipe_fd;
  109. int cb_registered;
  110. void *user_data;
  111. size_t user_data_size;
  112. size_t user_data_allocated;
  113. pthread_mutex_t lock;
  114. quorum_handle_t quorum_handle;
  115. uint32_t quorate;
  116. int quorum_fd;
  117. cmap_handle_t cmap_handle;
  118. char cmap_pid_path[CMAP_KEYNAME_MAXLEN];
  119. } sam_internal_data;
  120. extern const char *__progname;
  121. static cs_error_t sam_cmap_update_key (enum sam_cmap_key_t key, const char *value)
  122. {
  123. cs_error_t err;
  124. const char *svalue;
  125. uint64_t hc_period, last_hc;
  126. const char *ssvalue[] = { [SAM_RECOVERY_POLICY_QUIT] = "quit", [SAM_RECOVERY_POLICY_RESTART] = "restart" };
  127. char key_name[CMAP_KEYNAME_MAXLEN];
  128. switch (key) {
  129. case SAM_CMAP_KEY_RECOVERY:
  130. svalue = ssvalue[SAM_RP_MASK (sam_internal_data.recovery_policy)];
  131. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "%s%s", sam_internal_data.cmap_pid_path,
  132. "recovery");
  133. if ((err = cmap_set_string(sam_internal_data.cmap_handle, key_name, svalue)) != CS_OK) {
  134. goto exit_error;
  135. }
  136. break;
  137. case SAM_CMAP_KEY_HC_PERIOD:
  138. hc_period = sam_internal_data.time_interval;
  139. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "%s%s", sam_internal_data.cmap_pid_path,
  140. "poll_period");
  141. if ((err = cmap_set_uint64(sam_internal_data.cmap_handle, key_name, hc_period)) != CS_OK) {
  142. goto exit_error;
  143. }
  144. break;
  145. case SAM_CMAP_KEY_LAST_HC:
  146. last_hc = cs_timestamp_get();
  147. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "%s%s", sam_internal_data.cmap_pid_path,
  148. "last_updated");
  149. if ((err = cmap_set_uint64(sam_internal_data.cmap_handle, key_name, last_hc)) != CS_OK) {
  150. goto exit_error;
  151. }
  152. break;
  153. case SAM_CMAP_KEY_STATE:
  154. svalue = value;
  155. snprintf(key_name, CMAP_KEYNAME_MAXLEN, "%s%s", sam_internal_data.cmap_pid_path,
  156. "state");
  157. if ((err = cmap_set_string(sam_internal_data.cmap_handle, key_name, svalue)) != CS_OK) {
  158. goto exit_error;
  159. }
  160. break;
  161. }
  162. return (CS_OK);
  163. exit_error:
  164. return (err);
  165. }
  166. static cs_error_t sam_cmap_destroy_pid_path (void)
  167. {
  168. cmap_iter_handle_t iter;
  169. cs_error_t err;
  170. char key_name[CMAP_KEYNAME_MAXLEN];
  171. err = cmap_iter_init(sam_internal_data.cmap_handle, sam_internal_data.cmap_pid_path, &iter);
  172. if (err != CS_OK) {
  173. goto error_exit;
  174. }
  175. while ((err = cmap_iter_next(sam_internal_data.cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  176. cmap_delete(sam_internal_data.cmap_handle, key_name);
  177. }
  178. err = cmap_iter_finalize(sam_internal_data.cmap_handle, iter);
  179. error_exit:
  180. return (err);
  181. }
  182. static cs_error_t sam_cmap_register (void)
  183. {
  184. cs_error_t err;
  185. cmap_handle_t cmap_handle;
  186. if ((err = cmap_initialize (&cmap_handle)) != CS_OK) {
  187. return (err);
  188. }
  189. snprintf(sam_internal_data.cmap_pid_path, CMAP_KEYNAME_MAXLEN, "resources.process.%d.", getpid());
  190. sam_internal_data.cmap_handle = cmap_handle;
  191. if ((err = sam_cmap_update_key (SAM_CMAP_KEY_RECOVERY, NULL)) != CS_OK) {
  192. goto destroy_finalize_error;
  193. }
  194. if ((err = sam_cmap_update_key (SAM_CMAP_KEY_HC_PERIOD, NULL)) != CS_OK) {
  195. goto destroy_finalize_error;
  196. }
  197. return (CS_OK);
  198. destroy_finalize_error:
  199. sam_cmap_destroy_pid_path ();
  200. cmap_finalize (cmap_handle);
  201. return (err);
  202. }
  203. static void quorum_notification_fn (
  204. quorum_handle_t handle,
  205. uint32_t quorate,
  206. uint64_t ring_id,
  207. uint32_t view_list_entries,
  208. uint32_t *view_list)
  209. {
  210. sam_internal_data.quorate = quorate;
  211. }
  212. cs_error_t sam_initialize (
  213. int time_interval,
  214. sam_recovery_policy_t recovery_policy)
  215. {
  216. quorum_callbacks_t quorum_callbacks;
  217. uint32_t quorum_type;
  218. cs_error_t err;
  219. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_NOT_INITIALIZED) {
  220. return (CS_ERR_BAD_HANDLE);
  221. }
  222. if (SAM_RP_MASK (recovery_policy) != SAM_RECOVERY_POLICY_QUIT &&
  223. SAM_RP_MASK (recovery_policy) != SAM_RECOVERY_POLICY_RESTART) {
  224. return (CS_ERR_INVALID_PARAM);
  225. }
  226. if (recovery_policy & SAM_RECOVERY_POLICY_QUORUM) {
  227. /*
  228. * Initialize quorum
  229. */
  230. quorum_callbacks.quorum_notify_fn = quorum_notification_fn;
  231. if ((err = quorum_initialize (&sam_internal_data.quorum_handle, &quorum_callbacks, &quorum_type)) != CS_OK) {
  232. goto exit_error;
  233. }
  234. if ((err = quorum_trackstart (sam_internal_data.quorum_handle, CS_TRACK_CHANGES)) != CS_OK) {
  235. goto exit_error_quorum;
  236. }
  237. if ((err = quorum_fd_get (sam_internal_data.quorum_handle, &sam_internal_data.quorum_fd)) != CS_OK) {
  238. goto exit_error_quorum;
  239. }
  240. /*
  241. * Dispatch initial quorate state
  242. */
  243. if ((err = quorum_dispatch (sam_internal_data.quorum_handle, CS_DISPATCH_ONE)) != CS_OK) {
  244. goto exit_error_quorum;
  245. }
  246. }
  247. sam_internal_data.recovery_policy = recovery_policy;
  248. sam_internal_data.time_interval = time_interval;
  249. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_INITIALIZED;
  250. sam_internal_data.warn_signal = SIGTERM;
  251. sam_internal_data.am_i_child = 0;
  252. sam_internal_data.user_data = NULL;
  253. sam_internal_data.user_data_size = 0;
  254. sam_internal_data.user_data_allocated = 0;
  255. pthread_mutex_init (&sam_internal_data.lock, NULL);
  256. return (CS_OK);
  257. exit_error_quorum:
  258. quorum_finalize (sam_internal_data.quorum_handle);
  259. exit_error:
  260. return (err);
  261. }
  262. /*
  263. * Wrapper on top of write(2) function. It handles EAGAIN and EINTR states and sends whole buffer if possible.
  264. */
  265. static size_t sam_safe_write (
  266. int d,
  267. const void *buf,
  268. size_t nbyte)
  269. {
  270. ssize_t bytes_write;
  271. ssize_t tmp_bytes_write;
  272. bytes_write = 0;
  273. do {
  274. tmp_bytes_write = write (d, (const char *)buf + bytes_write,
  275. (nbyte - bytes_write > SSIZE_MAX) ? SSIZE_MAX : nbyte - bytes_write);
  276. if (tmp_bytes_write == -1) {
  277. if (!(errno == EAGAIN || errno == EINTR))
  278. return -1;
  279. } else {
  280. bytes_write += tmp_bytes_write;
  281. }
  282. } while (bytes_write != nbyte);
  283. return (bytes_write);
  284. }
  285. /*
  286. * Wrapper on top of read(2) function. It handles EAGAIN and EINTR states and reads whole buffer if possible.
  287. */
  288. static size_t sam_safe_read (
  289. int d,
  290. void *buf,
  291. size_t nbyte)
  292. {
  293. ssize_t bytes_read;
  294. ssize_t tmp_bytes_read;
  295. bytes_read = 0;
  296. do {
  297. tmp_bytes_read = read (d, (char *)buf + bytes_read,
  298. (nbyte - bytes_read > SSIZE_MAX) ? SSIZE_MAX : nbyte - bytes_read);
  299. if (tmp_bytes_read == -1) {
  300. if (!(errno == EAGAIN || errno == EINTR))
  301. return -1;
  302. } else {
  303. bytes_read += tmp_bytes_read;
  304. }
  305. } while (bytes_read != nbyte && tmp_bytes_read != 0);
  306. return (bytes_read);
  307. }
  308. static cs_error_t sam_read_reply (
  309. int child_fd_in)
  310. {
  311. char reply;
  312. cs_error_t err;
  313. if (sam_safe_read (sam_internal_data.child_fd_in, &reply, sizeof (reply)) != sizeof (reply)) {
  314. return (CS_ERR_LIBRARY);
  315. }
  316. switch (reply) {
  317. case SAM_REPLY_ERROR:
  318. /*
  319. * Read error and return that
  320. */
  321. if (sam_safe_read (sam_internal_data.child_fd_in, &err, sizeof (err)) != sizeof (err)) {
  322. return (CS_ERR_LIBRARY);
  323. }
  324. return (err);
  325. break;
  326. case SAM_REPLY_OK:
  327. /*
  328. * Everything correct
  329. */
  330. break;
  331. default:
  332. return (CS_ERR_LIBRARY);
  333. break;
  334. }
  335. return (CS_OK);
  336. }
  337. cs_error_t sam_data_getsize (size_t *size)
  338. {
  339. if (size == NULL) {
  340. return (CS_ERR_INVALID_PARAM);
  341. }
  342. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  343. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  344. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  345. return (CS_ERR_BAD_HANDLE);
  346. }
  347. pthread_mutex_lock (&sam_internal_data.lock);
  348. *size = sam_internal_data.user_data_size;
  349. pthread_mutex_unlock (&sam_internal_data.lock);
  350. return (CS_OK);
  351. }
  352. cs_error_t sam_data_restore (
  353. void *data,
  354. size_t size)
  355. {
  356. cs_error_t err;
  357. err = CS_OK;
  358. if (data == NULL) {
  359. return (CS_ERR_INVALID_PARAM);
  360. }
  361. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  362. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  363. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  364. return (CS_ERR_BAD_HANDLE);
  365. }
  366. pthread_mutex_lock (&sam_internal_data.lock);
  367. if (sam_internal_data.user_data_size == 0) {
  368. err = CS_OK;
  369. goto error_unlock;
  370. }
  371. if (size < sam_internal_data.user_data_size) {
  372. err = CS_ERR_INVALID_PARAM;
  373. goto error_unlock;
  374. }
  375. memcpy (data, sam_internal_data.user_data, sam_internal_data.user_data_size);
  376. pthread_mutex_unlock (&sam_internal_data.lock);
  377. return (CS_OK);
  378. error_unlock:
  379. pthread_mutex_unlock (&sam_internal_data.lock);
  380. return (err);
  381. }
  382. cs_error_t sam_data_store (
  383. const void *data,
  384. size_t size)
  385. {
  386. cs_error_t err;
  387. char command;
  388. char *new_data;
  389. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  390. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  391. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  392. return (CS_ERR_BAD_HANDLE);
  393. }
  394. if (data == NULL) {
  395. size = 0;
  396. }
  397. pthread_mutex_lock (&sam_internal_data.lock);
  398. if (sam_internal_data.am_i_child) {
  399. /*
  400. * We are child so we must send data to parent
  401. */
  402. command = SAM_COMMAND_DATA_STORE;
  403. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  404. err = CS_ERR_LIBRARY;
  405. goto error_unlock;
  406. }
  407. if (sam_safe_write (sam_internal_data.child_fd_out, &size, sizeof (size)) != sizeof (size)) {
  408. err = CS_ERR_LIBRARY;
  409. goto error_unlock;
  410. }
  411. if (data != NULL && sam_safe_write (sam_internal_data.child_fd_out, data, size) != size) {
  412. err = CS_ERR_LIBRARY;
  413. goto error_unlock;
  414. }
  415. /*
  416. * And wait for reply
  417. */
  418. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  419. goto error_unlock;
  420. }
  421. }
  422. /*
  423. * We are parent or we received OK reply from parent -> do required action
  424. */
  425. if (data == NULL) {
  426. free (sam_internal_data.user_data);
  427. sam_internal_data.user_data = NULL;
  428. sam_internal_data.user_data_allocated = 0;
  429. sam_internal_data.user_data_size = 0;
  430. } else {
  431. if (sam_internal_data.user_data_allocated < size) {
  432. if ((new_data = realloc (sam_internal_data.user_data, size)) == NULL) {
  433. err = CS_ERR_NO_MEMORY;
  434. goto error_unlock;
  435. }
  436. sam_internal_data.user_data_allocated = size;
  437. } else {
  438. new_data = sam_internal_data.user_data;
  439. }
  440. sam_internal_data.user_data = new_data;
  441. sam_internal_data.user_data_size = size;
  442. memcpy (sam_internal_data.user_data, data, size);
  443. }
  444. pthread_mutex_unlock (&sam_internal_data.lock);
  445. return (CS_OK);
  446. error_unlock:
  447. pthread_mutex_unlock (&sam_internal_data.lock);
  448. return (err);
  449. }
  450. cs_error_t sam_start (void)
  451. {
  452. char command;
  453. cs_error_t err;
  454. sam_recovery_policy_t recpol;
  455. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  456. return (CS_ERR_BAD_HANDLE);
  457. }
  458. recpol = sam_internal_data.recovery_policy;
  459. if (recpol & SAM_RECOVERY_POLICY_QUORUM || recpol & SAM_RECOVERY_POLICY_CMAP) {
  460. pthread_mutex_lock (&sam_internal_data.lock);
  461. }
  462. command = SAM_COMMAND_START;
  463. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  464. if (recpol & SAM_RECOVERY_POLICY_QUORUM || recpol & SAM_RECOVERY_POLICY_CMAP) {
  465. pthread_mutex_unlock (&sam_internal_data.lock);
  466. }
  467. return (CS_ERR_LIBRARY);
  468. }
  469. if (recpol & SAM_RECOVERY_POLICY_QUORUM || recpol & SAM_RECOVERY_POLICY_CMAP) {
  470. /*
  471. * Wait for parent reply
  472. */
  473. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  474. pthread_mutex_unlock (&sam_internal_data.lock);
  475. return (err);
  476. }
  477. pthread_mutex_unlock (&sam_internal_data.lock);
  478. }
  479. if (sam_internal_data.hc_callback)
  480. if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
  481. return (CS_ERR_LIBRARY);
  482. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_STARTED;
  483. return (CS_OK);
  484. }
  485. cs_error_t sam_stop (void)
  486. {
  487. char command;
  488. cs_error_t err;
  489. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  490. return (CS_ERR_BAD_HANDLE);
  491. }
  492. command = SAM_COMMAND_STOP;
  493. if (sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP) {
  494. pthread_mutex_lock (&sam_internal_data.lock);
  495. }
  496. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  497. if (sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP) {
  498. pthread_mutex_unlock (&sam_internal_data.lock);
  499. }
  500. return (CS_ERR_LIBRARY);
  501. }
  502. if (sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP) {
  503. /*
  504. * Wait for parent reply
  505. */
  506. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  507. pthread_mutex_unlock (&sam_internal_data.lock);
  508. return (err);
  509. }
  510. pthread_mutex_unlock (&sam_internal_data.lock);
  511. }
  512. if (sam_internal_data.hc_callback)
  513. if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
  514. return (CS_ERR_LIBRARY);
  515. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;
  516. return (CS_OK);
  517. }
  518. cs_error_t sam_hc_send (void)
  519. {
  520. char command;
  521. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  522. return (CS_ERR_BAD_HANDLE);
  523. }
  524. command = SAM_COMMAND_HB;
  525. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  526. return (CS_ERR_LIBRARY);
  527. return (CS_OK);
  528. }
  529. cs_error_t sam_finalize (void)
  530. {
  531. cs_error_t error;
  532. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  533. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  534. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  535. return (CS_ERR_BAD_HANDLE);
  536. }
  537. if (sam_internal_data.internal_status == SAM_INTERNAL_STATUS_STARTED) {
  538. error = sam_stop ();
  539. if (error != CS_OK)
  540. goto exit_error;
  541. }
  542. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_FINALIZED;
  543. free (sam_internal_data.user_data);
  544. exit_error:
  545. return (CS_OK);
  546. }
  547. cs_error_t sam_mark_failed (void)
  548. {
  549. char command;
  550. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED &&
  551. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  552. return (CS_ERR_BAD_HANDLE);
  553. }
  554. if (!(sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP)) {
  555. return (CS_ERR_INVALID_PARAM);
  556. }
  557. command = SAM_COMMAND_MARK_FAILED;
  558. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
  559. return (CS_ERR_LIBRARY);
  560. return (CS_OK);
  561. }
  562. cs_error_t sam_warn_signal_set (int warn_signal)
  563. {
  564. char command;
  565. cs_error_t err;
  566. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
  567. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
  568. sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
  569. return (CS_ERR_BAD_HANDLE);
  570. }
  571. pthread_mutex_lock (&sam_internal_data.lock);
  572. if (sam_internal_data.am_i_child) {
  573. /*
  574. * We are child so we must send data to parent
  575. */
  576. command = SAM_COMMAND_WARN_SIGNAL_SET;
  577. if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
  578. err = CS_ERR_LIBRARY;
  579. goto error_unlock;
  580. }
  581. if (sam_safe_write (sam_internal_data.child_fd_out, &warn_signal, sizeof (warn_signal)) !=
  582. sizeof (warn_signal)) {
  583. err = CS_ERR_LIBRARY;
  584. goto error_unlock;
  585. }
  586. /*
  587. * And wait for reply
  588. */
  589. if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
  590. goto error_unlock;
  591. }
  592. }
  593. /*
  594. * We are parent or we received OK reply from parent -> do required action
  595. */
  596. sam_internal_data.warn_signal = warn_signal;
  597. pthread_mutex_unlock (&sam_internal_data.lock);
  598. return (CS_OK);
  599. error_unlock:
  600. pthread_mutex_unlock (&sam_internal_data.lock);
  601. return (err);
  602. }
  603. static cs_error_t sam_parent_reply_send (
  604. cs_error_t err,
  605. int parent_fd_in,
  606. int parent_fd_out)
  607. {
  608. char reply;
  609. if (err == CS_OK) {
  610. reply = SAM_REPLY_OK;
  611. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  612. err = CS_ERR_LIBRARY;
  613. goto error_reply;
  614. }
  615. return (CS_OK);
  616. }
  617. error_reply:
  618. reply = SAM_REPLY_ERROR;
  619. if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
  620. return (CS_ERR_LIBRARY);
  621. }
  622. if (sam_safe_write (parent_fd_out, &err, sizeof (err)) != sizeof (err)) {
  623. return (CS_ERR_LIBRARY);
  624. }
  625. return (err);
  626. }
  627. static cs_error_t sam_parent_warn_signal_set (
  628. int parent_fd_in,
  629. int parent_fd_out)
  630. {
  631. int warn_signal;
  632. cs_error_t err;
  633. err = CS_OK;
  634. if (sam_safe_read (parent_fd_in, &warn_signal, sizeof (warn_signal)) != sizeof (warn_signal)) {
  635. err = CS_ERR_LIBRARY;
  636. goto error_reply;
  637. }
  638. err = sam_warn_signal_set (warn_signal);
  639. if (err != CS_OK) {
  640. goto error_reply;
  641. }
  642. return (sam_parent_reply_send (CS_OK, parent_fd_in, parent_fd_out));
  643. error_reply:
  644. return (sam_parent_reply_send (err, parent_fd_in, parent_fd_out));
  645. }
  646. static cs_error_t sam_parent_wait_for_quorum (
  647. int parent_fd_in,
  648. int parent_fd_out)
  649. {
  650. cs_error_t err;
  651. struct pollfd pfds[2];
  652. int poll_err;
  653. if (sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP) {
  654. if ((err = sam_cmap_update_key (SAM_CMAP_KEY_STATE, SAM_CMAP_S_Q_WAIT)) != CS_OK) {
  655. goto error_reply;
  656. }
  657. }
  658. /*
  659. * Update current quorum
  660. */
  661. if ((err = quorum_dispatch (sam_internal_data.quorum_handle, CS_DISPATCH_ALL)) != CS_OK) {
  662. goto error_reply;
  663. }
  664. /*
  665. * Wait for quorum
  666. */
  667. while (!sam_internal_data.quorate) {
  668. pfds[0].fd = parent_fd_in;
  669. pfds[0].events = 0;
  670. pfds[0].revents = 0;
  671. pfds[1].fd = sam_internal_data.quorum_fd;
  672. pfds[1].events = POLLIN;
  673. pfds[1].revents = 0;
  674. poll_err = poll (pfds, 2, -1);
  675. if (poll_err == -1) {
  676. /*
  677. * Error in poll
  678. * If it is EINTR, continue, otherwise QUIT
  679. */
  680. if (errno != EINTR) {
  681. err = CS_ERR_LIBRARY;
  682. goto error_reply;
  683. }
  684. }
  685. if (pfds[0].revents != 0) {
  686. if (pfds[0].revents == POLLERR || pfds[0].revents == POLLHUP ||pfds[0].revents == POLLNVAL) {
  687. /*
  688. * Child has exited
  689. */
  690. return (CS_OK);
  691. }
  692. }
  693. if (pfds[1].revents != 0) {
  694. if ((err = quorum_dispatch (sam_internal_data.quorum_handle, CS_DISPATCH_ONE)) != CS_OK) {
  695. goto error_reply;
  696. }
  697. }
  698. }
  699. if (sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP) {
  700. if ((err = sam_cmap_update_key (SAM_CMAP_KEY_STATE, SAM_CMAP_S_STARTED)) != CS_OK) {
  701. goto error_reply;
  702. }
  703. }
  704. return (sam_parent_reply_send (CS_OK, parent_fd_in, parent_fd_out));
  705. error_reply:
  706. if (sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CMAP) {
  707. sam_cmap_update_key (SAM_CMAP_KEY_STATE, SAM_CMAP_S_REGISTERED);
  708. }
  709. return (sam_parent_reply_send (err, parent_fd_in, parent_fd_out));
  710. }
  711. static cs_error_t sam_parent_cmap_state_set (
  712. int parent_fd_in,
  713. int parent_fd_out,
  714. int state)
  715. {
  716. cs_error_t err;
  717. const char *state_s;
  718. if (state == 1) {
  719. state_s = SAM_CMAP_S_STARTED;
  720. } else {
  721. state_s = SAM_CMAP_S_REGISTERED;
  722. }
  723. if ((err = sam_cmap_update_key (SAM_CMAP_KEY_STATE, state_s)) != CS_OK) {
  724. goto error_reply;
  725. }
  726. return (sam_parent_reply_send (CS_OK, parent_fd_in, parent_fd_out));
  727. error_reply:
  728. return (sam_parent_reply_send (err, parent_fd_in, parent_fd_out));
  729. }
  730. static cs_error_t sam_parent_kill_child (
  731. int *action,
  732. pid_t child_pid)
  733. {
  734. /*
  735. * Kill child process
  736. */
  737. if (!sam_internal_data.term_send) {
  738. /*
  739. * We didn't send warn_signal yet.
  740. */
  741. kill (child_pid, sam_internal_data.warn_signal);
  742. sam_internal_data.term_send = 1;
  743. } else {
  744. /*
  745. * We sent child warning. Now, we will not be so nice
  746. */
  747. kill (child_pid, SIGKILL);
  748. *action = SAM_PARENT_ACTION_RECOVERY;
  749. }
  750. return (CS_OK);
  751. }
  752. static cs_error_t sam_parent_mark_child_failed (
  753. int *action,
  754. pid_t child_pid)
  755. {
  756. sam_recovery_policy_t recpol;
  757. recpol = sam_internal_data.recovery_policy;
  758. sam_internal_data.term_send = 1;
  759. sam_internal_data.recovery_policy = SAM_RECOVERY_POLICY_QUIT |
  760. (SAM_RP_MASK_C (recpol) ? SAM_RECOVERY_POLICY_CMAP : 0) |
  761. (SAM_RP_MASK_Q (recpol) ? SAM_RECOVERY_POLICY_QUORUM : 0);
  762. return (sam_parent_kill_child (action, child_pid));
  763. }
  764. static cs_error_t sam_parent_data_store (
  765. int parent_fd_in,
  766. int parent_fd_out)
  767. {
  768. char *user_data;
  769. ssize_t size;
  770. cs_error_t err;
  771. err = CS_OK;
  772. user_data = NULL;
  773. if (sam_safe_read (parent_fd_in, &size, sizeof (size)) != sizeof (size)) {
  774. err = CS_ERR_LIBRARY;
  775. goto error_reply;
  776. }
  777. if (size > 0) {
  778. user_data = malloc (size);
  779. if (user_data == NULL) {
  780. err = CS_ERR_NO_MEMORY;
  781. goto error_reply;
  782. }
  783. if (sam_safe_read (parent_fd_in, user_data, size) != size) {
  784. err = CS_ERR_LIBRARY;
  785. goto free_error_reply;
  786. }
  787. }
  788. err = sam_data_store (user_data, size);
  789. if (err != CS_OK) {
  790. goto free_error_reply;
  791. }
  792. free (user_data);
  793. return (sam_parent_reply_send (CS_OK, parent_fd_in, parent_fd_out));
  794. free_error_reply:
  795. free (user_data);
  796. error_reply:
  797. return (sam_parent_reply_send (err, parent_fd_in, parent_fd_out));
  798. }
  799. static enum sam_parent_action_t sam_parent_handler (
  800. int parent_fd_in,
  801. int parent_fd_out,
  802. pid_t child_pid)
  803. {
  804. int poll_error;
  805. int action;
  806. int status;
  807. ssize_t bytes_read;
  808. char command;
  809. int time_interval;
  810. struct pollfd pfds[2];
  811. nfds_t nfds;
  812. cs_error_t err;
  813. sam_recovery_policy_t recpol;
  814. status = 0;
  815. action = SAM_PARENT_ACTION_CONTINUE;
  816. recpol = sam_internal_data.recovery_policy;
  817. while (action == SAM_PARENT_ACTION_CONTINUE) {
  818. pfds[0].fd = parent_fd_in;
  819. pfds[0].events = POLLIN;
  820. pfds[0].revents = 0;
  821. nfds = 1;
  822. if (status == 1 && sam_internal_data.time_interval != 0) {
  823. time_interval = sam_internal_data.time_interval;
  824. } else {
  825. time_interval = -1;
  826. }
  827. if (recpol & SAM_RECOVERY_POLICY_QUORUM) {
  828. pfds[nfds].fd = sam_internal_data.quorum_fd;
  829. pfds[nfds].events = POLLIN;
  830. pfds[nfds].revents = 0;
  831. nfds++;
  832. }
  833. poll_error = poll (pfds, nfds, time_interval);
  834. if (poll_error == -1) {
  835. /*
  836. * Error in poll
  837. * If it is EINTR, continue, otherwise QUIT
  838. */
  839. if (errno != EINTR) {
  840. action = SAM_PARENT_ACTION_ERROR;
  841. }
  842. }
  843. if (poll_error == 0) {
  844. /*
  845. * Time limit expires
  846. */
  847. if (status == 0) {
  848. action = SAM_PARENT_ACTION_QUIT;
  849. } else {
  850. sam_parent_kill_child (&action, child_pid);
  851. }
  852. }
  853. if (poll_error > 0) {
  854. if (pfds[0].revents != 0) {
  855. /*
  856. * We have EOF or command in pipe
  857. */
  858. bytes_read = sam_safe_read (parent_fd_in, &command, 1);
  859. if (bytes_read == 0) {
  860. /*
  861. * Handle EOF -> Take recovery action or quit if sam_start wasn't called
  862. */
  863. if (status == 0)
  864. action = SAM_PARENT_ACTION_QUIT;
  865. else
  866. action = SAM_PARENT_ACTION_RECOVERY;
  867. continue;
  868. }
  869. if (bytes_read == -1) {
  870. action = SAM_PARENT_ACTION_ERROR;
  871. goto action_exit;
  872. }
  873. if (recpol & SAM_RECOVERY_POLICY_CMAP) {
  874. sam_cmap_update_key (SAM_CMAP_KEY_LAST_HC, NULL);
  875. }
  876. /*
  877. * We have read command
  878. */
  879. switch (command) {
  880. case SAM_COMMAND_START:
  881. if (status == 0) {
  882. /*
  883. * Not started yet
  884. */
  885. if (recpol & SAM_RECOVERY_POLICY_QUORUM) {
  886. if (sam_parent_wait_for_quorum (parent_fd_in,
  887. parent_fd_out) != CS_OK) {
  888. continue;
  889. }
  890. }
  891. if (recpol & SAM_RECOVERY_POLICY_CMAP) {
  892. if (sam_parent_cmap_state_set (parent_fd_in,
  893. parent_fd_out, 1) != CS_OK) {
  894. continue;
  895. }
  896. }
  897. status = 1;
  898. }
  899. break;
  900. case SAM_COMMAND_STOP:
  901. if (status == 1) {
  902. /*
  903. * Started
  904. */
  905. if (recpol & SAM_RECOVERY_POLICY_CMAP) {
  906. if (sam_parent_cmap_state_set (parent_fd_in,
  907. parent_fd_out, 0) != CS_OK) {
  908. continue;
  909. }
  910. }
  911. status = 0;
  912. }
  913. break;
  914. case SAM_COMMAND_DATA_STORE:
  915. sam_parent_data_store (parent_fd_in, parent_fd_out);
  916. break;
  917. case SAM_COMMAND_WARN_SIGNAL_SET:
  918. sam_parent_warn_signal_set (parent_fd_in, parent_fd_out);
  919. break;
  920. case SAM_COMMAND_MARK_FAILED:
  921. status = 1;
  922. sam_parent_mark_child_failed (&action, child_pid);
  923. break;
  924. }
  925. } /* if (pfds[0].revents != 0) */
  926. if ((sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_QUORUM) &&
  927. pfds[1].revents != 0) {
  928. /*
  929. * Handle quorum change
  930. */
  931. err = quorum_dispatch (sam_internal_data.quorum_handle, CS_DISPATCH_ALL);
  932. if (status == 1 &&
  933. (!sam_internal_data.quorate || (err != CS_ERR_TRY_AGAIN && err != CS_OK))) {
  934. sam_parent_kill_child (&action, child_pid);
  935. }
  936. }
  937. } /* select_error > 0 */
  938. } /* action == SAM_PARENT_ACTION_CONTINUE */
  939. action_exit:
  940. return action;
  941. }
  942. cs_error_t sam_register (
  943. unsigned int *instance_id)
  944. {
  945. cs_error_t error;
  946. pid_t pid;
  947. int pipe_error;
  948. int pipe_fd_out[2], pipe_fd_in[2];
  949. enum sam_parent_action_t action, old_action;
  950. int child_status;
  951. sam_recovery_policy_t recpol;
  952. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED) {
  953. return (CS_ERR_BAD_HANDLE);
  954. }
  955. recpol = sam_internal_data.recovery_policy;
  956. if (recpol & SAM_RECOVERY_POLICY_CMAP) {
  957. /*
  958. * Register to cmap
  959. */
  960. if ((error = sam_cmap_register ()) != CS_OK) {
  961. goto error_exit;
  962. }
  963. }
  964. error = CS_OK;
  965. while (1) {
  966. if ((pipe_error = pipe (pipe_fd_out)) != 0) {
  967. error = CS_ERR_LIBRARY;
  968. goto error_exit;
  969. }
  970. if ((pipe_error = pipe (pipe_fd_in)) != 0) {
  971. close (pipe_fd_out[0]);
  972. close (pipe_fd_out[1]);
  973. error = CS_ERR_LIBRARY;
  974. goto error_exit;
  975. }
  976. if (recpol & SAM_RECOVERY_POLICY_CMAP) {
  977. if ((error = sam_cmap_update_key (SAM_CMAP_KEY_STATE, SAM_CMAP_S_REGISTERED)) != CS_OK) {
  978. goto error_exit;
  979. }
  980. }
  981. sam_internal_data.instance_id++;
  982. sam_internal_data.term_send = 0;
  983. pid = fork ();
  984. if (pid == -1) {
  985. /*
  986. * Fork error
  987. */
  988. sam_internal_data.instance_id--;
  989. error = CS_ERR_LIBRARY;
  990. goto error_exit;
  991. }
  992. if (pid == 0) {
  993. /*
  994. * Child process
  995. */
  996. close (pipe_fd_out[0]);
  997. close (pipe_fd_in[1]);
  998. sam_internal_data.child_fd_out = pipe_fd_out[1];
  999. sam_internal_data.child_fd_in = pipe_fd_in[0];
  1000. if (instance_id)
  1001. *instance_id = sam_internal_data.instance_id;
  1002. sam_internal_data.am_i_child = 1;
  1003. sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;
  1004. pthread_mutex_init (&sam_internal_data.lock, NULL);
  1005. goto error_exit;
  1006. } else {
  1007. /*
  1008. * Parent process
  1009. */
  1010. close (pipe_fd_out[1]);
  1011. close (pipe_fd_in[0]);
  1012. action = sam_parent_handler (pipe_fd_out[0], pipe_fd_in[1], pid);
  1013. close (pipe_fd_out[0]);
  1014. close (pipe_fd_in[1]);
  1015. if (action == SAM_PARENT_ACTION_ERROR) {
  1016. error = CS_ERR_LIBRARY;
  1017. goto error_exit;
  1018. }
  1019. /*
  1020. * We really don't like zombies
  1021. */
  1022. while (waitpid (pid, &child_status, 0) == -1 && errno == EINTR)
  1023. ;
  1024. old_action = action;
  1025. if (action == SAM_PARENT_ACTION_RECOVERY) {
  1026. if (SAM_RP_MASK (sam_internal_data.recovery_policy) == SAM_RECOVERY_POLICY_QUIT)
  1027. action = SAM_PARENT_ACTION_QUIT;
  1028. }
  1029. if (action == SAM_PARENT_ACTION_QUIT) {
  1030. if (recpol & SAM_RECOVERY_POLICY_QUORUM) {
  1031. quorum_finalize (sam_internal_data.quorum_handle);
  1032. }
  1033. if (recpol & SAM_RECOVERY_POLICY_CMAP) {
  1034. if (old_action == SAM_PARENT_ACTION_RECOVERY) {
  1035. /*
  1036. * Mark as failed
  1037. */
  1038. sam_cmap_update_key (SAM_CMAP_KEY_STATE, SAM_CMAP_S_FAILED);
  1039. } else {
  1040. sam_cmap_destroy_pid_path ();
  1041. }
  1042. }
  1043. exit (WEXITSTATUS (child_status));
  1044. }
  1045. }
  1046. }
  1047. error_exit:
  1048. return (error);
  1049. }
  1050. static void *hc_callback_thread (void *unused_param)
  1051. {
  1052. int poll_error;
  1053. int status;
  1054. ssize_t bytes_readed;
  1055. char command;
  1056. int time_interval, tmp_time_interval;
  1057. int counter;
  1058. struct pollfd pfds;
  1059. status = 0;
  1060. counter = 0;
  1061. time_interval = sam_internal_data.time_interval >> 2;
  1062. while (1) {
  1063. pfds.fd = sam_internal_data.cb_rpipe_fd;
  1064. pfds.events = POLLIN;
  1065. pfds.revents = 0;
  1066. if (status == 1) {
  1067. tmp_time_interval = time_interval;
  1068. } else {
  1069. tmp_time_interval = -1;
  1070. }
  1071. poll_error = poll (&pfds, 1, tmp_time_interval);
  1072. if (poll_error == 0) {
  1073. if (sam_hc_send () == CS_OK) {
  1074. counter++;
  1075. }
  1076. if (counter >= 4) {
  1077. if (sam_internal_data.hc_callback () != 0) {
  1078. status = 3;
  1079. }
  1080. counter = 0;
  1081. }
  1082. }
  1083. if (poll_error > 0) {
  1084. bytes_readed = sam_safe_read (sam_internal_data.cb_rpipe_fd, &command, 1);
  1085. if (bytes_readed > 0) {
  1086. if (status == 0 && command == SAM_COMMAND_START)
  1087. status = 1;
  1088. if (status == 1 && command == SAM_COMMAND_STOP)
  1089. status = 0;
  1090. }
  1091. }
  1092. }
  1093. /*
  1094. * This makes compiler happy, it's same as return (NULL);
  1095. */
  1096. return (unused_param);
  1097. }
  1098. cs_error_t sam_hc_callback_register (sam_hc_callback_t cb)
  1099. {
  1100. cs_error_t error = CS_OK;
  1101. pthread_attr_t thread_attr;
  1102. int pipe_error;
  1103. int pipe_fd[2];
  1104. if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
  1105. return (CS_ERR_BAD_HANDLE);
  1106. }
  1107. if (sam_internal_data.time_interval == 0) {
  1108. return (CS_ERR_INVALID_PARAM);
  1109. }
  1110. if (sam_internal_data.cb_registered) {
  1111. sam_internal_data.hc_callback = cb;
  1112. return (CS_OK);
  1113. }
  1114. /*
  1115. * We know, this is first registration
  1116. */
  1117. if (cb == NULL) {
  1118. return (CS_ERR_INVALID_PARAM);
  1119. }
  1120. pipe_error = pipe (pipe_fd);
  1121. if (pipe_error != 0) {
  1122. /*
  1123. * Pipe creation error
  1124. */
  1125. error = CS_ERR_LIBRARY;
  1126. goto error_exit;
  1127. }
  1128. sam_internal_data.cb_rpipe_fd = pipe_fd[0];
  1129. sam_internal_data.cb_wpipe_fd = pipe_fd[1];
  1130. /*
  1131. * Create thread attributes
  1132. */
  1133. error = pthread_attr_init (&thread_attr);
  1134. if (error != 0) {
  1135. error = CS_ERR_LIBRARY;
  1136. goto error_close_fd_exit;
  1137. }
  1138. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  1139. pthread_attr_setstacksize (&thread_attr, 32768);
  1140. /*
  1141. * Create thread
  1142. */
  1143. error = pthread_create (&sam_internal_data.cb_thread, &thread_attr, hc_callback_thread, NULL);
  1144. if (error != 0) {
  1145. error = CS_ERR_LIBRARY;
  1146. goto error_attr_destroy_exit;
  1147. }
  1148. /*
  1149. * Cleanup
  1150. */
  1151. pthread_attr_destroy(&thread_attr);
  1152. sam_internal_data.cb_registered = 1;
  1153. sam_internal_data.hc_callback = cb;
  1154. return (CS_OK);
  1155. error_attr_destroy_exit:
  1156. pthread_attr_destroy(&thread_attr);
  1157. error_close_fd_exit:
  1158. sam_internal_data.cb_rpipe_fd = sam_internal_data.cb_wpipe_fd = 0;
  1159. close (pipe_fd[0]);
  1160. close (pipe_fd[1]);
  1161. error_exit:
  1162. return (error);
  1163. }