4
0

sam.c 33 KB

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