sam.c 33 KB

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