4
0

sam.c 33 KB

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