evt.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /*
  2. * Copyright (c) 2004-2005 Mark Haverkamp
  3. * Copyright (c) 2004-2005 Open Source Development Lab
  4. *
  5. * All rights reserved.
  6. *
  7. * This software licensed under BSD license, the text of which follows:
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the Open Source Developement Lab nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/types.h>
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <sys/time.h>
  40. #include <sys/socket.h>
  41. #include "../include/ipc_evt.h"
  42. #include "util.h"
  43. #include "../exec/totempg.h"
  44. static void evtHandleInstanceDestructor(void *instance);
  45. static void chanHandleInstanceDestructor(void *instance);
  46. static void eventHandleInstanceDestructor(void *instance);
  47. /*
  48. * Versions of the SAF AIS specification supported by this library
  49. */
  50. static SaVersionT supported_versions[] = {
  51. {'B', 0x01, 0x01},
  52. {'b', 0x01, 0x01}
  53. };
  54. static struct saVersionDatabase evt_version_database = {
  55. sizeof(supported_versions) / sizeof(SaVersionT),
  56. supported_versions
  57. };
  58. /*
  59. * Event instance data
  60. */
  61. struct saHandleDatabase evt_instance_handle_db = {
  62. .handleCount = 0,
  63. .handles = 0,
  64. .mutex = PTHREAD_MUTEX_INITIALIZER,
  65. .handleInstanceDestructor = evtHandleInstanceDestructor
  66. };
  67. /*
  68. * Channel instance data
  69. */
  70. struct saHandleDatabase channel_handle_db = {
  71. .handleCount = 0,
  72. .handles = 0,
  73. .mutex = PTHREAD_MUTEX_INITIALIZER,
  74. .handleInstanceDestructor = chanHandleInstanceDestructor
  75. };
  76. /*
  77. * Event instance data
  78. */
  79. struct saHandleDatabase event_handle_db = {
  80. .handleCount = 0,
  81. .handles = 0,
  82. .mutex = PTHREAD_MUTEX_INITIALIZER,
  83. .handleInstanceDestructor = eventHandleInstanceDestructor
  84. };
  85. struct message_overlay {
  86. struct res_header header;
  87. char data[0];
  88. };
  89. /*
  90. * data required to support events for a given initialization
  91. *
  92. * ei_fd: fd received from the evtInitialize call.
  93. * ei_callback: callback function.
  94. * ei_version: version sent to the evtInitialize call.
  95. * ei_node_id: our node id.
  96. * ei_node_name: our node name.
  97. * ei_finalize: instance in finalize flag
  98. * ei_queue: queue for async messages while doing sync commands
  99. * ei_mutex: instance mutex
  100. *
  101. */
  102. struct event_instance {
  103. int ei_fd;
  104. SaEvtCallbacksT ei_callback;
  105. SaVersionT ei_version;
  106. SaClmNodeIdT ei_node_id;
  107. SaNameT ei_node_name;
  108. int ei_finalize;
  109. struct queue ei_inq;
  110. char ei_message[MESSAGE_SIZE_MAX];
  111. pthread_mutex_t ei_mutex;
  112. };
  113. /*
  114. * Data associated with an opened channel
  115. *
  116. * eci_channel_name: name of channel
  117. * eci_open_flags: channel open flags
  118. * eci_svr_channel_handle: channel handle returned from server
  119. * eci_closing: channel in process of being closed
  120. * eci_mutex: channel mutex
  121. *
  122. */
  123. struct event_channel_instance {
  124. SaNameT eci_channel_name;
  125. SaEvtChannelOpenFlagsT eci_open_flags;
  126. uint32_t eci_svr_channel_handle;
  127. SaEvtHandleT eci_instance_handle;
  128. int eci_closing;
  129. pthread_mutex_t eci_mutex;
  130. };
  131. /*
  132. * Event data.
  133. *
  134. * Store event data from saEvtEventAllocate function.
  135. * Store event data from received events.
  136. *
  137. * edi_channel_handle: handle (local) of assocated channel
  138. * edi_patterns: event patterns
  139. * edi_priority: event priority
  140. * edi_retention_time: event's retention time
  141. * edi_pub_name: event's publisher name
  142. * edi_pub_node: event's publisher node
  143. * edi_pub_time: event's publish time
  144. * edi_event_id: event's Id
  145. * edi_event_data: event's data
  146. * edi_event_data_size: size of edi_event_data
  147. * edi_freeing: event is being freed
  148. * edi_mutex: event data mutex
  149. *
  150. */
  151. struct event_data_instance {
  152. SaEvtChannelHandleT edi_channel_handle;
  153. SaEvtEventPatternArrayT edi_patterns;
  154. SaUint8T edi_priority;
  155. SaTimeT edi_retention_time;
  156. SaNameT edi_pub_name;
  157. SaClmNodeIdT edi_pub_node;
  158. SaTimeT edi_pub_time;
  159. SaEvtEventIdT edi_event_id;
  160. void *edi_event_data;
  161. SaSizeT edi_event_data_size;
  162. int edi_freeing;
  163. pthread_mutex_t edi_mutex;
  164. };
  165. #define min(a,b) ((a) < (b) ? (a) : (b))
  166. /*
  167. * Clean up function for an evt instance (saEvtInitialize) handle
  168. */
  169. static void evtHandleInstanceDestructor(void *instance)
  170. {
  171. struct event_instance *evti = instance;
  172. void **msg;
  173. int empty;
  174. /*
  175. * Empty out the queue if there are any pending messages
  176. */
  177. while ((saQueueIsEmpty(&evti->ei_inq, &empty) == SA_AIS_OK) && !empty) {
  178. saQueueItemGet(&evti->ei_inq, (void *)&msg);
  179. saQueueItemRemove(&evti->ei_inq);
  180. free(*msg);
  181. }
  182. /*
  183. * clean up the queue itself
  184. */
  185. if (evti->ei_inq.items) {
  186. free(evti->ei_inq.items);
  187. }
  188. /*
  189. * Disconnect from the server
  190. */
  191. if (evti->ei_fd != -1) {
  192. shutdown(evti->ei_fd, 0);
  193. close(evti->ei_fd);
  194. }
  195. }
  196. /*
  197. * Clean up function for an open channel handle
  198. */
  199. static void chanHandleInstanceDestructor(void *instance)
  200. {
  201. }
  202. /*
  203. * Clean up function for an event handle
  204. */
  205. static void eventHandleInstanceDestructor(void *instance)
  206. {
  207. struct event_data_instance *edi = instance;
  208. int i;
  209. for (i = 0; i < edi->edi_patterns.patternsNumber; i++) {
  210. free(edi->edi_patterns.patterns[i].pattern);
  211. }
  212. if (edi->edi_patterns.patterns) {
  213. free(edi->edi_patterns.patterns);
  214. }
  215. if (edi->edi_event_data) {
  216. free(edi->edi_event_data);
  217. }
  218. }
  219. /*
  220. * The saEvtInitialize() function initializes the Event Service for the
  221. * invoking process. A user of the Event Service must invoke this function
  222. * before it invokes any other function of the Event Service API. Each
  223. * initialization returns a different callback handle that the process
  224. * can use to communicate with that library instance.
  225. */
  226. SaAisErrorT
  227. saEvtInitialize(
  228. SaEvtHandleT *evtHandle,
  229. const SaEvtCallbacksT *callbacks,
  230. SaVersionT *version)
  231. {
  232. SaAisErrorT error = SA_AIS_OK;
  233. struct event_instance *evti;
  234. /*
  235. * validate the requested version with what we support
  236. */
  237. error = saVersionVerify(&evt_version_database, version);
  238. if (error != SA_AIS_OK) {
  239. goto error_nofree;
  240. }
  241. /*
  242. * Allocate instance data, allocate unique handle for instance,
  243. * assign instance data to unique handle
  244. */
  245. error = saHandleCreate(&evt_instance_handle_db, sizeof(*evti),
  246. (void*)evtHandle);
  247. if (error != SA_AIS_OK) {
  248. goto error_nofree;
  249. }
  250. error = saHandleInstanceGet(&evt_instance_handle_db, *evtHandle,
  251. (void*)&evti);
  252. if (error != SA_AIS_OK) {
  253. goto error_handle_free;
  254. }
  255. memset(evti, 0, sizeof(*evti));
  256. /*
  257. * Save the version so we can check with the event server
  258. * and see if it supports this version.
  259. */
  260. evti->ei_version = *version;
  261. /*
  262. * An inq is needed to store async messages while waiting for a
  263. * sync response
  264. */
  265. error = saQueueInit(&evti->ei_inq, 1024, sizeof(void *));
  266. if (error != SA_AIS_OK) {
  267. goto error_handle_put;
  268. }
  269. /*
  270. * Set up communication with the event server
  271. */
  272. error = saServiceConnect(&evti->ei_fd, MESSAGE_REQ_EVT_INIT);
  273. if (error != SA_AIS_OK) {
  274. goto error_handle_put;
  275. }
  276. /*
  277. * The callback function is saved in the event instance for
  278. * saEvtDispatch() to use.
  279. */
  280. if (callbacks) {
  281. memcpy(&evti->ei_callback, callbacks,
  282. sizeof(evti->ei_callback));
  283. }
  284. pthread_mutex_init(&evti->ei_mutex, NULL);
  285. saHandleInstancePut(&evt_instance_handle_db, *evtHandle);
  286. return SA_AIS_OK;
  287. error_handle_put:
  288. saHandleInstancePut(&evt_instance_handle_db, *evtHandle);
  289. error_handle_free:
  290. (void)saHandleDestroy(&evt_instance_handle_db, *evtHandle);
  291. error_nofree:
  292. return error;
  293. }
  294. /*
  295. * The saEvtSelectionObjectGet() function returns the operating system
  296. * handle selectionObject, associated with the handle evtHandle, allowing
  297. * the invoking process to ascertain when callbacks are pending. This
  298. * function allows a process to avoid repeated invoking saEvtDispatch() to
  299. * see if there is a new event, thus, needlessly consuming CPU time. In a
  300. * POSIX environment the system handle could be a file descriptor that is
  301. * used with the poll() or select() system calls to detect incoming callbacks.
  302. */
  303. SaAisErrorT
  304. saEvtSelectionObjectGet(
  305. SaEvtHandleT evtHandle,
  306. SaSelectionObjectT *selectionObject)
  307. {
  308. struct event_instance *evti;
  309. SaAisErrorT error;
  310. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  311. (void *)&evti);
  312. if (error != SA_AIS_OK) {
  313. return error;
  314. }
  315. *selectionObject = evti->ei_fd;
  316. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  317. return SA_AIS_OK;
  318. }
  319. /*
  320. * Alocate an event data structure and associated handle to be
  321. * used to supply event data to a call back function.
  322. */
  323. static SaAisErrorT make_event(SaEvtEventHandleT *event_handle,
  324. struct lib_event_data *evt)
  325. {
  326. struct event_data_instance *edi;
  327. SaEvtEventPatternT *pat;
  328. SaUint8T *str;
  329. SaAisErrorT error;
  330. int i;
  331. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  332. (void*)event_handle);
  333. if (error != SA_AIS_OK) {
  334. goto make_evt_done;
  335. }
  336. error = saHandleInstanceGet(&event_handle_db, *event_handle,
  337. (void*)&edi);
  338. if (error != SA_AIS_OK) {
  339. goto make_evt_done;
  340. }
  341. memset(edi, 0, sizeof(*edi));
  342. pthread_mutex_init(&edi->edi_mutex, NULL);
  343. edi->edi_freeing = 0;
  344. edi->edi_channel_handle = evt->led_lib_channel_handle;
  345. edi->edi_priority = evt->led_priority;
  346. edi->edi_retention_time = evt->led_retention_time;
  347. edi->edi_pub_node = evt->led_publisher_node_id;
  348. edi->edi_pub_time = evt->led_publish_time;
  349. edi->edi_event_data_size = evt->led_user_data_size;
  350. edi->edi_event_id = evt->led_event_id;
  351. edi->edi_pub_name = evt->led_publisher_name;
  352. if (edi->edi_event_data_size) {
  353. edi->edi_event_data = malloc(edi->edi_event_data_size);
  354. memcpy(edi->edi_event_data,
  355. evt->led_body + evt->led_user_data_offset,
  356. edi->edi_event_data_size);
  357. }
  358. /*
  359. * Move the pattern bits into the SaEvtEventPatternArrayT
  360. */
  361. edi->edi_patterns.patternsNumber = evt->led_patterns_number;
  362. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  363. edi->edi_patterns.patternsNumber);
  364. pat = (SaEvtEventPatternT *)evt->led_body;
  365. str = evt->led_body + sizeof(SaEvtEventPatternT) *
  366. edi->edi_patterns.patternsNumber;
  367. for (i = 0; i < evt->led_patterns_number; i++) {
  368. edi->edi_patterns.patterns[i].patternSize = pat->patternSize;
  369. edi->edi_patterns.patterns[i].pattern = malloc(pat->patternSize);
  370. if (!edi->edi_patterns.patterns[i].pattern) {
  371. printf("make_event: couldn't alloc %d bytes\n", pat->patternSize);
  372. error = SA_AIS_ERR_NO_MEMORY;
  373. break;
  374. }
  375. memcpy(edi->edi_patterns.patterns[i].pattern,
  376. str, pat->patternSize);
  377. str += pat->patternSize;
  378. pat++;
  379. }
  380. saHandleInstancePut (&event_handle_db, *event_handle);
  381. make_evt_done:
  382. return error;
  383. }
  384. /*
  385. * The saEvtDispatch() function invokes, in the context of the calling
  386. * thread, one or all of the pending callbacks for the handle evtHandle.
  387. */
  388. SaAisErrorT
  389. saEvtDispatch(
  390. SaEvtHandleT evtHandle,
  391. SaDispatchFlagsT dispatchFlags)
  392. {
  393. struct pollfd ufds;
  394. int timeout = -1;
  395. SaAisErrorT error;
  396. int dispatch_avail;
  397. struct event_instance *evti;
  398. SaEvtEventHandleT event_handle;
  399. SaEvtCallbacksT callbacks;
  400. struct res_header **queue_msg;
  401. struct res_header *msg = 0;
  402. int empty;
  403. int ignore_dispatch = 0;
  404. int cont = 1; /* always continue do loop except when set to 0 */
  405. int poll_fd;
  406. struct message_overlay *dispatch_data;
  407. struct lib_event_data *evt;
  408. struct res_evt_event_data res;
  409. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  410. (void *)&evti);
  411. if (error != SA_AIS_OK) {
  412. return error;
  413. }
  414. /*
  415. * Timeout instantly for SA_DISPATCH_ALL
  416. */
  417. if (dispatchFlags == SA_DISPATCH_ALL) {
  418. timeout = 0;
  419. }
  420. do {
  421. poll_fd = evti->ei_fd;
  422. ufds.fd = poll_fd;
  423. ufds.events = POLLIN;
  424. ufds.revents = 0;
  425. pthread_mutex_lock(&evti->ei_mutex);
  426. saQueueIsEmpty(&evti->ei_inq, &empty);
  427. /*
  428. * Read from the socket if there is nothing in
  429. * our queue.
  430. */
  431. if (empty == 1) {
  432. pthread_mutex_unlock(&evti->ei_mutex);
  433. error = saPollRetry(&ufds, 1, timeout);
  434. if (error != SA_AIS_OK) {
  435. goto error_nounlock;
  436. }
  437. pthread_mutex_lock(&evti->ei_mutex);
  438. }
  439. /*
  440. * Handle has been finalized in another thread
  441. */
  442. if (evti->ei_finalize == 1) {
  443. error = SA_AIS_OK;
  444. pthread_mutex_unlock(&evti->ei_mutex);
  445. goto error_unlock;
  446. }
  447. dispatch_avail = (ufds.revents & POLLIN) | (empty == 0);
  448. if (dispatch_avail == 0 && dispatchFlags == SA_DISPATCH_ALL) {
  449. pthread_mutex_unlock(&evti->ei_mutex);
  450. break; /* exit do while cont is 1 loop */
  451. } else
  452. if (dispatch_avail == 0) {
  453. pthread_mutex_unlock(&evti->ei_mutex);
  454. continue; /* next poll */
  455. }
  456. saQueueIsEmpty(&evti->ei_inq, &empty);
  457. if (empty == 0) {
  458. /*
  459. * Queue is not empty, read data from queue
  460. */
  461. saQueueItemGet(&evti->ei_inq, (void *)&queue_msg);
  462. msg = *queue_msg;
  463. dispatch_data = (struct message_overlay *)msg;
  464. saQueueItemRemove(&evti->ei_inq);
  465. } else {
  466. /*
  467. * Queue empty, read response from socket
  468. */
  469. dispatch_data = (struct message_overlay *)&evti->ei_message;
  470. error = saRecvRetry(evti->ei_fd, &dispatch_data->header,
  471. sizeof(struct res_header), MSG_WAITALL | MSG_NOSIGNAL);
  472. if (error != SA_AIS_OK) {
  473. pthread_mutex_unlock(&evti->ei_mutex);
  474. goto error_unlock;
  475. }
  476. if (dispatch_data->header.size > sizeof(struct res_header)) {
  477. error = saRecvRetry(evti->ei_fd, dispatch_data->data,
  478. dispatch_data->header.size - sizeof(struct res_header),
  479. MSG_WAITALL | MSG_NOSIGNAL);
  480. if (error != SA_AIS_OK) {
  481. pthread_mutex_unlock(&evti->ei_mutex);
  482. goto error_unlock;
  483. }
  484. }
  485. }
  486. /*
  487. * Make copy of callbacks, message data, unlock instance,
  488. * and call callback. A risk of this dispatch method is that
  489. * the callback routines may operate at the same time that
  490. * EvtFinalize has been called in another thread.
  491. */
  492. memcpy(&callbacks, &evti->ei_callback, sizeof(evti->ei_callback));
  493. pthread_mutex_unlock(&evti->ei_mutex);
  494. /*
  495. * Dispatch incoming response
  496. */
  497. switch (dispatch_data->header.id) {
  498. case MESSAGE_RES_LIB_ACTIVATEPOLL:
  499. /*
  500. * This is a do nothing message which the node
  501. * executive sends to activate the file evtHandle
  502. * in poll when the library has queued a message into
  503. * evti->ei_inq. The dispatch is ignored for the
  504. * following two cases:
  505. * 1) setting of timeout to zero for the
  506. * DISPATCH_ALL case
  507. * 2) expiration of the do loop for the
  508. * DISPATCH_ONE case
  509. */
  510. ignore_dispatch = 1;
  511. break;
  512. case MESSAGE_RES_EVT_AVAILABLE:
  513. /*
  514. * There are events available. Send a request for one and then
  515. * dispatch it.
  516. */
  517. evt = (struct lib_event_data *)&evti->ei_message;
  518. res.evd_head.id = MESSAGE_REQ_EVT_EVENT_DATA;
  519. res.evd_head.size = sizeof(res);
  520. error = saSendRetry(evti->ei_fd, &res, sizeof(res), MSG_NOSIGNAL);
  521. if (error != SA_AIS_OK) {
  522. printf("MESSAGE_RES_EVT_AVAILABLE: send failed: %d\n", error);
  523. break;
  524. }
  525. error = saRecvQueue(evti->ei_fd, evt, &evti->ei_inq,
  526. MESSAGE_RES_EVT_EVENT_DATA);
  527. if (error != SA_AIS_OK) {
  528. printf("MESSAGE_RES_EVT_AVAILABLE: receive failed: %d\n",
  529. error);
  530. break;
  531. }
  532. /*
  533. * No data available. This is OK.
  534. */
  535. if (evt->led_head.error == SA_AIS_ERR_NOT_EXIST) {
  536. // printf("MESSAGE_RES_EVT_AVAILABLE: No event data\n");
  537. error = SA_AIS_OK;
  538. break;
  539. }
  540. if (evt->led_head.error != SA_AIS_OK) {
  541. error = evt->led_head.error;
  542. printf("MESSAGE_RES_EVT_AVAILABLE: Error returned: %d\n",
  543. error);
  544. break;
  545. }
  546. error = make_event(&event_handle, evt);
  547. if (error != SA_AIS_OK) {
  548. break;
  549. }
  550. /*
  551. * Only call if there was a function registered
  552. */
  553. if (callbacks.saEvtEventDeliverCallback) {
  554. callbacks.saEvtEventDeliverCallback(evt->led_sub_id,
  555. event_handle, evt->led_user_data_size);
  556. }
  557. break;
  558. case MESSAGE_RES_EVT_CHAN_OPEN_CALLBACK:
  559. {
  560. struct res_evt_open_chan_async *resa =
  561. (struct res_evt_open_chan_async *)dispatch_data;
  562. struct event_channel_instance *eci;
  563. /*
  564. * Check for errors. If there are none, then
  565. * look up the local channel via the handle that we
  566. * got from the callback request. All we need to do
  567. * is place in the handle from the server side and then
  568. * we can call the callback.
  569. */
  570. error = resa->ica_head.error;
  571. if (error == SA_AIS_OK) {
  572. error = saHandleInstanceGet(&channel_handle_db,
  573. resa->ica_c_handle, (void*)&eci);
  574. if (error == SA_AIS_OK) {
  575. eci->eci_svr_channel_handle = resa->ica_channel_handle;
  576. saHandleInstancePut (&channel_handle_db,
  577. resa->ica_c_handle);
  578. }
  579. }
  580. /*
  581. * Only call if there was a function registered
  582. */
  583. if (callbacks.saEvtChannelOpenCallback) {
  584. callbacks.saEvtChannelOpenCallback(resa->ica_invocation,
  585. resa->ica_c_handle, error);
  586. }
  587. }
  588. break;
  589. default:
  590. printf("Dispatch: Bad message type 0x%x\n",
  591. dispatch_data->header.id);
  592. error = SA_AIS_ERR_LIBRARY;
  593. goto error_nounlock;
  594. break;
  595. }
  596. /*
  597. * If empty is zero it means the we got the
  598. * message from the queue and we are responsible
  599. * for freeing it.
  600. */
  601. if (empty == 0) {
  602. free(msg);
  603. }
  604. /*
  605. * Determine if more messages should be processed
  606. */
  607. switch (dispatchFlags) {
  608. case SA_DISPATCH_ONE:
  609. if (ignore_dispatch) {
  610. ignore_dispatch = 0;
  611. } else {
  612. cont = 0;
  613. }
  614. break;
  615. case SA_DISPATCH_ALL:
  616. if (ignore_dispatch) {
  617. ignore_dispatch = 0;
  618. }
  619. break;
  620. case SA_DISPATCH_BLOCKING:
  621. break;
  622. }
  623. } while (cont);
  624. error_unlock:
  625. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  626. error_nounlock:
  627. return error;
  628. }
  629. /*
  630. * The saEvtFinalize() function closes the association, represented by the
  631. * evtHandle parameter, between the process and the Event Service. It may
  632. * free up resources.
  633. * This function cannot be invoked before the process has invoked the
  634. * corresponding saEvtInitialize() function for the Event Service. After
  635. * this function is invoked, the selection object is no longer valid.
  636. * Moreover, the Event Service is unavailable for further use unless it is
  637. * reinitialized using the saEvtInitialize() function.
  638. */
  639. SaAisErrorT
  640. saEvtFinalize(SaEvtHandleT evtHandle)
  641. {
  642. struct event_instance *evti;
  643. SaAisErrorT error;
  644. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  645. (void *)&evti);
  646. if (error != SA_AIS_OK) {
  647. return error;
  648. }
  649. pthread_mutex_lock(&evti->ei_mutex);
  650. /*
  651. * Another thread has already started finalizing
  652. */
  653. if (evti->ei_finalize) {
  654. pthread_mutex_unlock(&evti->ei_mutex);
  655. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  656. return SA_AIS_ERR_BAD_HANDLE;
  657. }
  658. evti->ei_finalize = 1;
  659. saActivatePoll(evti->ei_fd);
  660. pthread_mutex_unlock(&evti->ei_mutex);
  661. saHandleDestroy(&evt_instance_handle_db, evtHandle);
  662. saHandleInstancePut(&evt_instance_handle_db, evtHandle);
  663. return error;
  664. }
  665. /*
  666. * The saEvtChannelOpen() function creates a new event channel or open an
  667. * existing channel. The saEvtChannelOpen() function is a blocking operation
  668. * and returns a new event channel handle. An event channel may be opened
  669. * multiple times by the same or different processes for publishing, and
  670. * subscribing to, events. If a process opens an event channel multiple
  671. * times, it is possible to receive the same event multiple times. However,
  672. * a process shall never receive an event more than once on a particular
  673. * event channel handle. If a process opens a channel twice and an event is
  674. * matched on both open channels, the Event Service performs two
  675. * callbacks -- one for each opened channel.
  676. */
  677. SaAisErrorT
  678. saEvtChannelOpen(
  679. SaEvtHandleT evtHandle,
  680. const SaNameT *channelName,
  681. SaEvtChannelOpenFlagsT channelOpenFlags,
  682. SaTimeT timeout,
  683. SaEvtChannelHandleT *channelHandle)
  684. {
  685. struct event_instance *evti;
  686. struct req_evt_channel_open req;
  687. struct res_evt_channel_open res;
  688. struct event_channel_instance *eci;
  689. SaAisErrorT error;
  690. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  691. (void*)&evti);
  692. if (error != SA_AIS_OK) {
  693. goto chan_open_done;
  694. }
  695. /*
  696. * create a handle for this open channel
  697. */
  698. error = saHandleCreate(&channel_handle_db, sizeof(*eci),
  699. (void*)channelHandle);
  700. if (error != SA_AIS_OK) {
  701. goto chan_open_put;
  702. }
  703. error = saHandleInstanceGet(&channel_handle_db, *channelHandle,
  704. (void*)&eci);
  705. if (error != SA_AIS_OK) {
  706. saHandleDestroy(&channel_handle_db, *channelHandle);
  707. goto chan_open_put;
  708. }
  709. /*
  710. * Send the request to the server and wait for a response
  711. */
  712. req.ico_head.size = sizeof(req);
  713. req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL;
  714. req.ico_c_handle = *channelHandle;
  715. req.ico_timeout = timeout;
  716. req.ico_open_flag = channelOpenFlags;
  717. req.ico_channel_name = *channelName;
  718. pthread_mutex_lock(&evti->ei_mutex);
  719. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  720. if (error != SA_AIS_OK) {
  721. pthread_mutex_unlock (&evti->ei_mutex);
  722. goto chan_open_free;
  723. }
  724. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  725. MESSAGE_RES_EVT_OPEN_CHANNEL);
  726. pthread_mutex_unlock (&evti->ei_mutex);
  727. if (error != SA_AIS_OK) {
  728. goto chan_open_free;
  729. }
  730. error = res.ico_head.error;
  731. if (error != SA_AIS_OK) {
  732. goto chan_open_free;
  733. }
  734. eci->eci_svr_channel_handle = res.ico_channel_handle;
  735. eci->eci_channel_name = *channelName;
  736. eci->eci_open_flags = channelOpenFlags;
  737. eci->eci_instance_handle = evtHandle;
  738. eci->eci_closing = 0;
  739. pthread_mutex_init(&eci->eci_mutex, NULL);
  740. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  741. saHandleInstancePut (&channel_handle_db, *channelHandle);
  742. return SA_AIS_OK;
  743. chan_open_free:
  744. saHandleDestroy(&channel_handle_db, *channelHandle);
  745. saHandleInstancePut (&channel_handle_db, *channelHandle);
  746. chan_open_put:
  747. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  748. chan_open_done:
  749. return error;
  750. }
  751. /*
  752. * The saEvtChannelClose() function closes an event channel and frees
  753. * resources allocated for that event channel in the invoking process.
  754. */
  755. SaAisErrorT
  756. saEvtChannelClose(SaEvtChannelHandleT channelHandle)
  757. {
  758. SaAisErrorT error;
  759. struct event_instance *evti;
  760. struct event_channel_instance *eci;
  761. struct req_evt_channel_close req;
  762. struct res_evt_channel_close res;
  763. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  764. (void*)&eci);
  765. if (error != SA_AIS_OK) {
  766. goto chan_close_done;
  767. }
  768. /*
  769. * get the evt handle for the fd
  770. */
  771. error = saHandleInstanceGet(&evt_instance_handle_db,
  772. eci->eci_instance_handle, (void*)&evti);
  773. if (error != SA_AIS_OK) {
  774. goto chan_close_put1;
  775. }
  776. /*
  777. * Make sure that the channel isn't being closed elsewhere
  778. */
  779. pthread_mutex_lock(&eci->eci_mutex);
  780. if (eci->eci_closing) {
  781. pthread_mutex_unlock(&eci->eci_mutex);
  782. saHandleInstancePut(&channel_handle_db, channelHandle);
  783. return SA_AIS_ERR_BAD_HANDLE;
  784. }
  785. eci->eci_closing = 1;
  786. saActivatePoll(evti->ei_fd);
  787. pthread_mutex_unlock(&eci->eci_mutex);
  788. /*
  789. * Send the request to the server and wait for a response
  790. */
  791. req.icc_head.size = sizeof(req);
  792. req.icc_head.id = MESSAGE_REQ_EVT_CLOSE_CHANNEL;
  793. req.icc_channel_handle = eci->eci_svr_channel_handle;
  794. pthread_mutex_lock(&evti->ei_mutex);
  795. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  796. if (error != SA_AIS_OK) {
  797. pthread_mutex_unlock(&evti->ei_mutex);
  798. eci->eci_closing = 0;
  799. goto chan_close_put2;
  800. }
  801. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  802. MESSAGE_RES_EVT_CLOSE_CHANNEL);
  803. pthread_mutex_unlock(&evti->ei_mutex);
  804. if (error != SA_AIS_OK) {
  805. eci->eci_closing = 0;
  806. goto chan_close_put2;
  807. }
  808. error = res.icc_head.error;
  809. saHandleInstancePut(&evt_instance_handle_db,
  810. eci->eci_instance_handle);
  811. saHandleDestroy(&channel_handle_db, channelHandle);
  812. saHandleInstancePut(&channel_handle_db, channelHandle);
  813. return error;
  814. chan_close_put2:
  815. saHandleInstancePut(&evt_instance_handle_db,
  816. eci->eci_instance_handle);
  817. chan_close_put1:
  818. saHandleInstancePut(&channel_handle_db, channelHandle);
  819. chan_close_done:
  820. return error;
  821. }
  822. /*
  823. * The saEvtChannelOpenAsync() function creates a new event channel or open an
  824. * existing channel. The saEvtChannelOpenAsync() function is a non-blocking
  825. * operation. A new event channel handle is returned in the channel open
  826. * callback function (SaEvtChannelOpenCallbackT).
  827. */
  828. SaAisErrorT
  829. saEvtChannelOpenAsync(SaEvtHandleT evtHandle,
  830. SaInvocationT invocation,
  831. const SaNameT *channelName,
  832. SaEvtChannelOpenFlagsT channelOpenFlags)
  833. {
  834. struct event_instance *evti;
  835. struct req_evt_channel_open req;
  836. struct res_evt_channel_open res;
  837. struct event_channel_instance *eci;
  838. SaEvtChannelHandleT channel_handle;
  839. SaAisErrorT error;
  840. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  841. (void*)&evti);
  842. if (error != SA_AIS_OK) {
  843. goto chan_open_done;
  844. }
  845. /*
  846. * Make sure that an open channel callback has been
  847. * registered before allowing the open to continue.
  848. */
  849. if (!evti->ei_callback.saEvtChannelOpenCallback) {
  850. error = SA_AIS_ERR_INIT;
  851. goto chan_open_put;
  852. }
  853. /*
  854. * create a handle for this open channel
  855. */
  856. error = saHandleCreate(&channel_handle_db, sizeof(*eci),
  857. &channel_handle);
  858. if (error != SA_AIS_OK) {
  859. goto chan_open_put;
  860. }
  861. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  862. (void*)&eci);
  863. if (error != SA_AIS_OK) {
  864. saHandleDestroy(&channel_handle_db, channel_handle);
  865. goto chan_open_put;
  866. }
  867. /*
  868. * Send the request to the server. The response isn't the open channel,
  869. * just an ack. The open channel will be returned when the channel open
  870. * callback is called.
  871. */
  872. req.ico_head.size = sizeof(req);
  873. req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL_ASYNC;
  874. req.ico_c_handle = channel_handle;
  875. req.ico_timeout = 0;
  876. req.ico_invocation = invocation;
  877. req.ico_open_flag = channelOpenFlags;
  878. req.ico_channel_name = *channelName;
  879. pthread_mutex_lock(&evti->ei_mutex);
  880. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  881. if (error != SA_AIS_OK) {
  882. pthread_mutex_unlock (&evti->ei_mutex);
  883. goto chan_open_free;
  884. }
  885. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  886. MESSAGE_RES_EVT_OPEN_CHANNEL);
  887. pthread_mutex_unlock (&evti->ei_mutex);
  888. if (error != SA_AIS_OK) {
  889. goto chan_open_free;
  890. }
  891. error = res.ico_head.error;
  892. if (error != SA_AIS_OK) {
  893. goto chan_open_free;
  894. }
  895. eci->eci_svr_channel_handle = 0; /* filled in by callback */
  896. eci->eci_channel_name = *channelName;
  897. eci->eci_open_flags = channelOpenFlags;
  898. eci->eci_instance_handle = evtHandle;
  899. eci->eci_closing = 0;
  900. pthread_mutex_init(&eci->eci_mutex, NULL);
  901. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  902. saHandleInstancePut (&channel_handle_db, channel_handle);
  903. return SA_AIS_OK;
  904. chan_open_free:
  905. saHandleDestroy(&channel_handle_db, channel_handle);
  906. saHandleInstancePut (&channel_handle_db, channel_handle);
  907. chan_open_put:
  908. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  909. chan_open_done:
  910. return error;
  911. }
  912. /*
  913. * The SaEvtChannelUnlink function deletes an existing event channel
  914. * from the cluster.
  915. *
  916. * After completion of the invocation:
  917. * - An open of the channel name without a create will fail.
  918. * - The channel remains available to any already opened instances.
  919. * - If an open/create is performed on this channel name a new instance
  920. * is created.
  921. * - The ulinked channel's resources will be deleted when the last open
  922. * instance is closed.
  923. *
  924. * Note that an event channel is only deleted from the cluster
  925. * namespace when saEvtChannelUnlink() is invoked on it. The deletion
  926. * of an event channel frees all resources allocated by the Event
  927. * Service for it, such as published events with non-zero retention
  928. * time.
  929. */
  930. SaAisErrorT
  931. saEvtChannelUnlink(
  932. SaEvtHandleT evtHandle,
  933. const SaNameT *channelName)
  934. {
  935. struct event_instance *evti;
  936. struct req_evt_channel_unlink req;
  937. struct res_evt_channel_unlink res;
  938. SaAisErrorT error;
  939. error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
  940. (void*)&evti);
  941. if (error != SA_AIS_OK) {
  942. goto chan_unlink_done;
  943. }
  944. /*
  945. * Send the request to the server and wait for a response
  946. */
  947. req.iuc_head.size = sizeof(req);
  948. req.iuc_head.id = MESSAGE_REQ_EVT_UNLINK_CHANNEL;
  949. req.iuc_channel_name = *channelName;
  950. pthread_mutex_lock(&evti->ei_mutex);
  951. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  952. if (error != SA_AIS_OK) {
  953. pthread_mutex_unlock (&evti->ei_mutex);
  954. goto chan_unlink_put;
  955. }
  956. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  957. MESSAGE_RES_EVT_UNLINK_CHANNEL);
  958. pthread_mutex_unlock (&evti->ei_mutex);
  959. if (error != SA_AIS_OK) {
  960. goto chan_unlink_put;
  961. }
  962. error = res.iuc_head.error;
  963. chan_unlink_put:
  964. saHandleInstancePut (&evt_instance_handle_db, evtHandle);
  965. chan_unlink_done:
  966. return error;
  967. }
  968. /*
  969. * The saEvtEventAllocate() function allocates memory for the event, but not
  970. * for the eventHandle, and initializes all event attributes to default values.
  971. * The event allocated by saEvtEventAllocate() is freed by invoking
  972. * saEvtEventFree().
  973. * The memory associated with the eventHandle is not deallocated by the
  974. * saEvtEventAllocate() function or the saEvtEventFree() function. It is the
  975. * responsibility of the invoking process to deallocate the memory for the
  976. * eventHandle when the process has published the event and has freed the
  977. * event by invoking saEvtEventFree().
  978. */
  979. SaAisErrorT
  980. saEvtEventAllocate(
  981. const SaEvtChannelHandleT channelHandle,
  982. SaEvtEventHandleT *eventHandle)
  983. {
  984. SaAisErrorT error;
  985. struct event_data_instance *edi;
  986. struct event_instance *evti;
  987. struct event_channel_instance *eci;
  988. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  989. (void*)&eci);
  990. if (error != SA_AIS_OK) {
  991. goto alloc_done;
  992. }
  993. error = saHandleInstanceGet(&evt_instance_handle_db,
  994. eci->eci_instance_handle, (void*)&evti);
  995. if (error != SA_AIS_OK) {
  996. goto alloc_put1;
  997. }
  998. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  999. (void*)eventHandle);
  1000. if (error != SA_AIS_OK) {
  1001. goto alloc_put2;
  1002. }
  1003. error = saHandleInstanceGet(&event_handle_db, *eventHandle,
  1004. (void*)&edi);
  1005. if (error != SA_AIS_OK) {
  1006. goto alloc_put2;
  1007. }
  1008. memset(edi, 0, sizeof(*edi));
  1009. pthread_mutex_init(&edi->edi_mutex, NULL);
  1010. edi->edi_freeing = 0;
  1011. edi->edi_channel_handle = channelHandle;
  1012. edi->edi_pub_node = evti->ei_node_id;
  1013. edi->edi_priority = SA_EVT_LOWEST_PRIORITY;
  1014. edi->edi_event_id = SA_EVT_EVENTID_NONE;
  1015. saHandleInstancePut (&event_handle_db, *eventHandle);
  1016. alloc_put2:
  1017. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  1018. alloc_put1:
  1019. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  1020. alloc_done:
  1021. return error;
  1022. }
  1023. /*
  1024. * The saEvtEventFree() function gives the Event Service premission to
  1025. * deallocate the memory that contains the attributes of the event that is
  1026. * associated with eventHandle. The function is used to free events allocated
  1027. * by saEvtEventAllocate() and by saEvtEventDeliverCallback().
  1028. */
  1029. SaAisErrorT
  1030. saEvtEventFree(SaEvtEventHandleT eventHandle)
  1031. {
  1032. SaAisErrorT error;
  1033. struct event_data_instance *edi;
  1034. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1035. (void*)&edi);
  1036. if (error != SA_AIS_OK) {
  1037. goto evt_free_done;
  1038. }
  1039. /*
  1040. * Make sure that the event isn't being freed elsewhere
  1041. */
  1042. pthread_mutex_lock(&edi->edi_mutex);
  1043. if (edi->edi_freeing) {
  1044. pthread_mutex_unlock(&edi->edi_mutex);
  1045. saHandleInstancePut(&event_handle_db, eventHandle);
  1046. return SA_AIS_ERR_BAD_HANDLE;
  1047. }
  1048. edi->edi_freeing = 1;
  1049. pthread_mutex_unlock(&edi->edi_mutex);
  1050. saHandleDestroy(&event_handle_db, eventHandle);
  1051. saHandleInstancePut(&event_handle_db, eventHandle);
  1052. evt_free_done:
  1053. return error;
  1054. }
  1055. /*
  1056. * This function may be used to assign writeable event attributes. It takes
  1057. * as arguments an event handle eventHandle and the attribute to be set in
  1058. * the event structure of the event with that handle. Note: The only
  1059. * attributes that a process can set are the patternArray, priority,
  1060. * retentionTime and publisherName attributes.
  1061. */
  1062. SaAisErrorT
  1063. saEvtEventAttributesSet(
  1064. const SaEvtEventHandleT eventHandle,
  1065. const SaEvtEventPatternArrayT *patternArray,
  1066. SaEvtEventPriorityT priority,
  1067. SaTimeT retentionTime,
  1068. const SaNameT *publisherName)
  1069. {
  1070. SaEvtEventPatternT *oldpatterns;
  1071. SaSizeT oldnumber;
  1072. SaAisErrorT error;
  1073. struct event_data_instance *edi;
  1074. int i;
  1075. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1076. (void*)&edi);
  1077. if (error != SA_AIS_OK) {
  1078. goto attr_set_done;
  1079. }
  1080. pthread_mutex_lock(&edi->edi_mutex);
  1081. edi->edi_priority = priority;
  1082. edi->edi_retention_time = retentionTime;
  1083. /*
  1084. * publisherName or patternArray not allowed to be NULL
  1085. */
  1086. if (!publisherName || !patternArray) {
  1087. error = SA_AIS_ERR_INVALID_PARAM;
  1088. goto attr_set_unlock;
  1089. }
  1090. edi->edi_pub_name = *publisherName;
  1091. oldpatterns = edi->edi_patterns.patterns;
  1092. oldnumber = edi->edi_patterns.patternsNumber;
  1093. edi->edi_patterns.patterns = 0;
  1094. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  1095. patternArray->patternsNumber);
  1096. if (!edi->edi_patterns.patterns) {
  1097. error = SA_AIS_ERR_NO_MEMORY;
  1098. goto attr_set_done_reset;
  1099. }
  1100. edi->edi_patterns.patternsNumber = patternArray->patternsNumber;
  1101. /*
  1102. * copy the patterns from the caller. allocating memory for
  1103. * of all the strings.
  1104. */
  1105. for (i = 0; i < patternArray->patternsNumber; i++) {
  1106. edi->edi_patterns.patterns[i].pattern =
  1107. malloc(patternArray->patterns[i].patternSize);
  1108. if (!edi->edi_patterns.patterns[i].pattern) {
  1109. int j;
  1110. for (j = 0; j < i; j++) {
  1111. free(edi->edi_patterns.patterns[j].pattern);
  1112. }
  1113. free(edi->edi_patterns.patterns);
  1114. error = SA_AIS_ERR_NO_MEMORY;
  1115. goto attr_set_done_reset;
  1116. }
  1117. memcpy(edi->edi_patterns.patterns[i].pattern,
  1118. patternArray->patterns[i].pattern,
  1119. patternArray->patterns[i].patternSize);
  1120. edi->edi_patterns.patterns[i].patternSize =
  1121. patternArray->patterns[i].patternSize;
  1122. }
  1123. /*
  1124. * free up the old pattern memory
  1125. */
  1126. if (oldpatterns) {
  1127. for (i = 0; i < oldnumber; i++) {
  1128. if (oldpatterns[i].pattern) {
  1129. free(oldpatterns[i].pattern);
  1130. }
  1131. }
  1132. free (oldpatterns);
  1133. }
  1134. goto attr_set_unlock;
  1135. attr_set_done_reset:
  1136. edi->edi_patterns.patterns = oldpatterns;
  1137. edi->edi_patterns.patternsNumber = oldnumber;
  1138. attr_set_unlock:
  1139. pthread_mutex_unlock(&edi->edi_mutex);
  1140. saHandleInstancePut(&event_handle_db, eventHandle);
  1141. attr_set_done:
  1142. return error;
  1143. }
  1144. /*
  1145. * This function takes as parameters an event handle eventHandle and the
  1146. * attributes of the event with that handle. The function retrieves the
  1147. * value of the attributes for the event and stores them at the address
  1148. * provided for the out parameters.
  1149. * It is the responsibility of the invoking process to allocate memory for
  1150. * the out parameters before it invokes this function. The Event Service
  1151. * assigns the out values into that memory. For each of the out, or in/out,
  1152. * parameters, if the invoking process provides a NULL reference, the
  1153. * Availability Management Framework does not return the out value.
  1154. * Similarly, it is the responsibility of the invoking process to allocate
  1155. * memory for the patternArray.
  1156. */
  1157. SaAisErrorT
  1158. saEvtEventAttributesGet(
  1159. SaEvtEventHandleT eventHandle,
  1160. SaEvtEventPatternArrayT *patternArray,
  1161. SaEvtEventPriorityT *priority,
  1162. SaTimeT *retentionTime,
  1163. SaNameT *publisherName,
  1164. SaTimeT *publishTime,
  1165. SaEvtEventIdT *eventId)
  1166. {
  1167. SaAisErrorT error;
  1168. struct event_data_instance *edi;
  1169. SaSizeT npats;
  1170. int i;
  1171. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1172. (void*)&edi);
  1173. if (error != SA_AIS_OK) {
  1174. goto attr_get_done;
  1175. }
  1176. pthread_mutex_lock(&edi->edi_mutex);
  1177. /*
  1178. * Go through the args and send back information if the pointer
  1179. * isn't NULL
  1180. */
  1181. if (eventId) {
  1182. *eventId = edi->edi_event_id;
  1183. }
  1184. if (publishTime) {
  1185. *publishTime = edi->edi_pub_time;
  1186. }
  1187. if (publisherName) {
  1188. *publisherName = edi->edi_pub_name;
  1189. }
  1190. if (retentionTime) {
  1191. *retentionTime = edi->edi_retention_time;
  1192. }
  1193. if (priority) {
  1194. *priority = edi->edi_priority;
  1195. }
  1196. if (!patternArray) {
  1197. goto attr_get_unlock;
  1198. }
  1199. npats = min(patternArray->patternsNumber,
  1200. edi->edi_patterns.patternsNumber);
  1201. /*
  1202. * We set the returned number of patterns to the actual
  1203. * pattern count of the event. This way the caller can tell
  1204. * if it got all the possible patterns. If the returned number
  1205. * is more that the number supplied, then some available patterns
  1206. * were not returned.
  1207. *
  1208. * The same thing happens when copying the pattern strings.
  1209. */
  1210. patternArray->patternsNumber = edi->edi_patterns.patternsNumber;
  1211. for (i = 0; i < npats; i++) {
  1212. memcpy(patternArray->patterns[i].pattern,
  1213. edi->edi_patterns.patterns[i].pattern,
  1214. min(patternArray->patterns[i].patternSize,
  1215. edi->edi_patterns.patterns[i].patternSize));
  1216. patternArray->patterns[i].patternSize =
  1217. edi->edi_patterns.patterns[i].patternSize;
  1218. }
  1219. attr_get_unlock:
  1220. pthread_mutex_unlock(&edi->edi_mutex);
  1221. saHandleInstancePut(&event_handle_db, eventHandle);
  1222. attr_get_done:
  1223. return error;
  1224. }
  1225. /*
  1226. * The saEvtEventDataGet() function allows a process to retrieve the data
  1227. * associated with an event previously delivered by
  1228. * saEvtEventDeliverCallback().
  1229. */
  1230. SaAisErrorT
  1231. saEvtEventDataGet(
  1232. const SaEvtEventHandleT eventHandle,
  1233. void *eventData,
  1234. SaSizeT *eventDataSize)
  1235. {
  1236. SaAisErrorT error = SA_AIS_OK;
  1237. struct event_data_instance *edi;
  1238. SaSizeT xfsize;
  1239. if (!eventData || !eventDataSize) {
  1240. goto data_get_done;
  1241. }
  1242. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1243. (void*)&edi);
  1244. if (error != SA_AIS_OK) {
  1245. goto data_get_done;
  1246. }
  1247. pthread_mutex_lock(&edi->edi_mutex);
  1248. if (edi->edi_event_data && edi->edi_event_data_size) {
  1249. xfsize = min(*eventDataSize, edi->edi_event_data_size);
  1250. *eventDataSize = edi->edi_event_data_size;
  1251. memcpy(eventData, edi->edi_event_data, xfsize);
  1252. } else {
  1253. *eventDataSize = 0;
  1254. }
  1255. pthread_mutex_unlock(&edi->edi_mutex);
  1256. saHandleInstancePut(&event_handle_db, eventHandle);
  1257. data_get_done:
  1258. return error;
  1259. }
  1260. /*
  1261. * Calculate the size in bytes for patterns
  1262. */
  1263. static size_t patt_size(const SaEvtEventPatternArrayT *patterns)
  1264. {
  1265. int i;
  1266. size_t size = sizeof(SaEvtEventPatternArrayT);
  1267. for (i = 0; i < patterns->patternsNumber; i++) {
  1268. size += sizeof(SaEvtEventPatternT);
  1269. size += patterns->patterns[i].patternSize;
  1270. }
  1271. return size;
  1272. }
  1273. /*
  1274. * copy patterns to a form for sending to the server
  1275. */
  1276. static uint32_t aispatt_to_evt_patt(const SaEvtEventPatternArrayT *patterns,
  1277. void *data)
  1278. {
  1279. int i;
  1280. SaEvtEventPatternT *pats = data;
  1281. SaUint8T *str = (SaUint8T *)pats +
  1282. (patterns->patternsNumber * sizeof(*pats));
  1283. /*
  1284. * Pointers are replaced with offsets into the data array. These
  1285. * will be later converted back into pointers when received as events.
  1286. */
  1287. for (i = 0; i < patterns->patternsNumber; i++) {
  1288. memcpy(str, patterns->patterns[i].pattern,
  1289. patterns->patterns[i].patternSize);
  1290. pats->patternSize = patterns->patterns[i].patternSize;
  1291. pats->pattern = (SaUint8T *)((void *)str - data);
  1292. str += patterns->patterns[i].patternSize;
  1293. pats++;
  1294. }
  1295. return patterns->patternsNumber;
  1296. }
  1297. /*
  1298. * Calculate the size in bytes for filters
  1299. */
  1300. static size_t filt_size(const SaEvtEventFilterArrayT *filters)
  1301. {
  1302. int i;
  1303. size_t size = sizeof(SaEvtEventFilterArrayT);
  1304. for (i = 0; i < filters->filtersNumber; i++) {
  1305. size += sizeof(SaEvtEventFilterT);
  1306. size += filters->filters[i].filter.patternSize;
  1307. }
  1308. return size;
  1309. }
  1310. /*
  1311. * Convert the Sa filters to a form that can be sent over the network
  1312. * i.e. replace pointers with offsets. The pointers will be reconstituted
  1313. * by the receiver.
  1314. */
  1315. static uint32_t aisfilt_to_evt_filt(const SaEvtEventFilterArrayT *filters,
  1316. void *data)
  1317. {
  1318. int i;
  1319. SaEvtEventFilterArrayT *filta = data;
  1320. SaEvtEventFilterT *filts = data + sizeof(SaEvtEventFilterArrayT);
  1321. SaUint8T *str = (SaUint8T *)filts +
  1322. (filters->filtersNumber * sizeof(*filts));
  1323. /*
  1324. * Pointers are replaced with offsets into the data array. These
  1325. * will be later converted back into pointers by the evt server.
  1326. */
  1327. filta->filters = (SaEvtEventFilterT *)((void *)filts - data);
  1328. filta->filtersNumber = filters->filtersNumber;
  1329. for (i = 0; i < filters->filtersNumber; i++) {
  1330. filts->filterType = filters->filters[i].filterType;
  1331. filts->filter.patternSize =
  1332. filters->filters[i].filter.patternSize;
  1333. memcpy(str,
  1334. filters->filters[i].filter.pattern,
  1335. filters->filters[i].filter.patternSize);
  1336. filts->filter.pattern = (SaUint8T *)((void *)str - data);
  1337. str += filters->filters[i].filter.patternSize;
  1338. filts++;
  1339. }
  1340. return filters->filtersNumber;
  1341. }
  1342. /*
  1343. * The saEvtEventPublish() function publishes an event on the associated
  1344. * channel. The event to be published consists of a
  1345. * standard set of attributes (the event header) and an optional data part.
  1346. * Before an event can be published, the publisher process must invoke the
  1347. * saEvtEventPatternArraySet() function to set the event patterns. The event
  1348. * is delivered to subscribers whose subscription filter matches the event
  1349. * patterns.
  1350. * When the Event Service publishes an event, it automatically sets
  1351. * the following readonly event attributes:
  1352. * - Event attribute time
  1353. * - Event publisher identifier
  1354. * - Event publisher node identifier
  1355. * - Event identifier
  1356. * In addition to the event attributes, a process can supply values for the
  1357. * eventData and eventDataSize parameters for publication as part of the
  1358. * event. The data portion of the event may be at most SA_EVT_DATA_MAX_LEN
  1359. * bytes in length.
  1360. * The process may assume that the invocation of saEvtEventPublish() copies
  1361. * all pertinent parameters, including the memory associated with the
  1362. * eventHandle and eventData parameters, to its own local memory. However,
  1363. * the invocation does not automatically deallocate memory associated with
  1364. * the eventHandle and eventData parameters. It is the responsibility of the
  1365. * invoking process to deallocate the memory for those parameters after
  1366. * saEvtEventPublish() returns.
  1367. */
  1368. SaAisErrorT
  1369. saEvtEventPublish(
  1370. const SaEvtEventHandleT eventHandle,
  1371. const void *eventData,
  1372. SaSizeT eventDataSize,
  1373. SaEvtEventIdT *eventId)
  1374. {
  1375. SaAisErrorT error;
  1376. struct event_data_instance *edi;
  1377. struct event_instance *evti;
  1378. struct event_channel_instance *eci;
  1379. struct lib_event_data *req;
  1380. struct res_evt_event_publish res;
  1381. size_t pattern_size;
  1382. struct event_pattern *patterns;
  1383. void *data_start;
  1384. if (eventDataSize > SA_EVT_DATA_MAX_LEN) {
  1385. error = SA_AIS_ERR_INVALID_PARAM;
  1386. goto pub_done;
  1387. }
  1388. error = saHandleInstanceGet(&event_handle_db, eventHandle,
  1389. (void*)&edi);
  1390. if (error != SA_AIS_OK) {
  1391. goto pub_done;
  1392. }
  1393. pthread_mutex_lock(&edi->edi_mutex);
  1394. /*
  1395. * See if patterns have been set for this event. If not, we
  1396. * can't publish.
  1397. */
  1398. if (!edi->edi_patterns.patterns) {
  1399. error = SA_AIS_ERR_INVALID_PARAM;
  1400. goto pub_put1;
  1401. }
  1402. error = saHandleInstanceGet(&channel_handle_db, edi->edi_channel_handle,
  1403. (void*)&eci);
  1404. if (error != SA_AIS_OK) {
  1405. goto pub_put1;
  1406. }
  1407. /*
  1408. * See if we can publish to this channel
  1409. */
  1410. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_PUBLISHER)) {
  1411. error = SA_AIS_ERR_ACCESS;
  1412. goto pub_put2;
  1413. }
  1414. error = saHandleInstanceGet(&evt_instance_handle_db,
  1415. eci->eci_instance_handle, (void*)&evti);
  1416. if (error != SA_AIS_OK) {
  1417. goto pub_put2;
  1418. }
  1419. /*
  1420. * Figure out how much memory we need for the patterns and data
  1421. */
  1422. pattern_size = patt_size(&edi->edi_patterns);
  1423. req = malloc(sizeof(*req) + eventDataSize + pattern_size);
  1424. patterns = (struct event_pattern *)req->led_body;
  1425. data_start = (void *)req->led_body + pattern_size;
  1426. if (!req) {
  1427. error = SA_AIS_ERR_NO_MEMORY;
  1428. goto pub_put3;
  1429. }
  1430. /*
  1431. * copy everything to the request structure
  1432. */
  1433. aispatt_to_evt_patt(&edi->edi_patterns, patterns);
  1434. req->led_patterns_number = edi->edi_patterns.patternsNumber;
  1435. req->led_user_data_offset = pattern_size;
  1436. if (eventData && eventDataSize) {
  1437. memcpy(data_start, eventData, eventDataSize);
  1438. req->led_user_data_size = eventDataSize;
  1439. } else {
  1440. req->led_user_data_size = 0;
  1441. }
  1442. req->led_head.id = MESSAGE_REQ_EVT_PUBLISH;
  1443. req->led_head.size = sizeof(*req) + pattern_size + eventDataSize;
  1444. req->led_svr_channel_handle = eci->eci_svr_channel_handle;
  1445. req->led_retention_time = edi->edi_retention_time;
  1446. req->led_publish_time = clustTimeNow();
  1447. req->led_priority = edi->edi_priority;
  1448. req->led_publisher_name = edi->edi_pub_name;
  1449. pthread_mutex_lock(&evti->ei_mutex);
  1450. error = saSendRetry(evti->ei_fd, req, req->led_head.size, MSG_NOSIGNAL);
  1451. free(req);
  1452. if (error != SA_AIS_OK) {
  1453. pthread_mutex_unlock (&evti->ei_mutex);
  1454. goto pub_put3;
  1455. }
  1456. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1457. MESSAGE_RES_EVT_PUBLISH);
  1458. pthread_mutex_unlock (&evti->ei_mutex);
  1459. if (error != SA_AIS_OK) {
  1460. goto pub_put3;
  1461. }
  1462. error = res.iep_head.error;
  1463. if (error == SA_AIS_OK) {
  1464. *eventId = res.iep_event_id;
  1465. }
  1466. pub_put3:
  1467. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  1468. pub_put2:
  1469. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  1470. pub_put1:
  1471. pthread_mutex_unlock(&edi->edi_mutex);
  1472. saHandleInstancePut(&event_handle_db, eventHandle);
  1473. pub_done:
  1474. return error;
  1475. }
  1476. /*
  1477. * The saEvtEventSubscribe() function enables a process to subscribe for
  1478. * events on an event channel by registering one or more filters on that
  1479. * event channel. The process must have opened the event channel, designated
  1480. * by channelHandle, with the SA_EVT_CHANNEL_SUBSCRIBER flag set for an
  1481. * invocation of this function to be successful.
  1482. * The memory associated with the filters is not deallocated by the
  1483. * saEvtEventSubscribe() function. It is the responsibility of the invoking
  1484. * process to deallocate the memory when the saEvtEventSubscribe() function
  1485. * returns.
  1486. * For a given subscription, the filters parameter cannot be modified. To
  1487. * change the filters parameter without losing events, a process must
  1488. * establish a new subscription with the new filters parameter. After the new
  1489. * subscription is established, the old subscription can be removed by
  1490. * invoking the saEvtEventUnsubscribe() function.
  1491. */
  1492. SaAisErrorT
  1493. saEvtEventSubscribe(
  1494. const SaEvtChannelHandleT channelHandle,
  1495. const SaEvtEventFilterArrayT *filters,
  1496. SaEvtSubscriptionIdT subscriptionId)
  1497. {
  1498. SaAisErrorT error;
  1499. struct event_instance *evti;
  1500. struct event_channel_instance *eci;
  1501. struct req_evt_event_subscribe *req;
  1502. struct res_evt_event_subscribe res;
  1503. int sz;
  1504. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1505. (void*)&eci);
  1506. if (error != SA_AIS_OK) {
  1507. goto subscribe_done;
  1508. }
  1509. /*
  1510. * get the evt handle for the fd
  1511. */
  1512. error = saHandleInstanceGet(&evt_instance_handle_db,
  1513. eci->eci_instance_handle, (void*)&evti);
  1514. if (error != SA_AIS_OK) {
  1515. goto subscribe_put1;
  1516. }
  1517. /*
  1518. * Make sure that a deliver callback has been
  1519. * registered before allowing the subscribe to continue.
  1520. */
  1521. if (!evti->ei_callback.saEvtEventDeliverCallback) {
  1522. error = SA_AIS_ERR_INIT;
  1523. goto subscribe_put2;
  1524. }
  1525. /*
  1526. * See if we can subscribe to this channel
  1527. */
  1528. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1529. error = SA_AIS_ERR_INVALID_PARAM;
  1530. goto subscribe_put2;
  1531. }
  1532. /*
  1533. * calculate size needed to store the filters
  1534. */
  1535. sz = filt_size(filters);
  1536. req = malloc(sizeof(*req) + sz);
  1537. if (!req) {
  1538. error = SA_AIS_ERR_NO_MEMORY;
  1539. goto subscribe_put2;
  1540. }
  1541. /*
  1542. * Copy the supplied filters to the request
  1543. */
  1544. req->ics_filter_count = aisfilt_to_evt_filt(filters,
  1545. req->ics_filter_data);
  1546. req->ics_head.id = MESSAGE_REQ_EVT_SUBSCRIBE;
  1547. req->ics_head.size = sizeof(*req) + sz;
  1548. req->ics_channel_handle = eci->eci_svr_channel_handle;
  1549. req->ics_sub_id = subscriptionId;
  1550. req->ics_filter_size = sz;
  1551. pthread_mutex_lock(&evti->ei_mutex);
  1552. error = saSendRetry(evti->ei_fd, req, req->ics_head.size, MSG_NOSIGNAL);
  1553. free(req);
  1554. if (error != SA_AIS_OK) {
  1555. pthread_mutex_unlock (&evti->ei_mutex);
  1556. goto subscribe_put2;
  1557. }
  1558. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1559. MESSAGE_RES_EVT_SUBSCRIBE);
  1560. pthread_mutex_unlock (&evti->ei_mutex);
  1561. if (error != SA_AIS_OK) {
  1562. goto subscribe_put2;
  1563. }
  1564. error = res.ics_head.error;
  1565. subscribe_put2:
  1566. saHandleInstancePut(&evt_instance_handle_db,
  1567. eci->eci_instance_handle);
  1568. subscribe_put1:
  1569. saHandleInstancePut(&channel_handle_db, channelHandle);
  1570. subscribe_done:
  1571. return error;
  1572. }
  1573. /*
  1574. * The saEvtEventUnsubscribe() function allows a process to stop receiving
  1575. * events for a particular subscription on an event channel by removing the
  1576. * subscription. The saEvtEventUnsubscribe() operation is successful if the
  1577. * subscriptionId parameter matches a previously registered subscription.
  1578. * Pending events that no longer match any subscription, because the
  1579. * saEvtEventUnsubscribe() operation had been invoked, are purged. a process
  1580. * that wishes to modify a subscription without losing any events must
  1581. * establish the new subscription before removing the existing subscription.
  1582. */
  1583. SaAisErrorT
  1584. saEvtEventUnsubscribe(
  1585. const SaEvtChannelHandleT channelHandle,
  1586. SaEvtSubscriptionIdT subscriptionId)
  1587. {
  1588. SaAisErrorT error;
  1589. struct event_instance *evti;
  1590. struct event_channel_instance *eci;
  1591. struct req_evt_event_unsubscribe req;
  1592. struct res_evt_event_unsubscribe res;
  1593. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1594. (void*)&eci);
  1595. if (error != SA_AIS_OK) {
  1596. goto unsubscribe_done;
  1597. }
  1598. error = saHandleInstanceGet(&evt_instance_handle_db,
  1599. eci->eci_instance_handle, (void*)&evti);
  1600. if (error != SA_AIS_OK) {
  1601. goto unsubscribe_put1;
  1602. }
  1603. req.icu_head.id = MESSAGE_REQ_EVT_UNSUBSCRIBE;
  1604. req.icu_head.size = sizeof(req);
  1605. req.icu_channel_handle = eci->eci_svr_channel_handle;
  1606. req.icu_sub_id = subscriptionId;
  1607. pthread_mutex_lock(&evti->ei_mutex);
  1608. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  1609. if (error != SA_AIS_OK) {
  1610. pthread_mutex_unlock (&evti->ei_mutex);
  1611. goto unsubscribe_put2;
  1612. }
  1613. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1614. MESSAGE_RES_EVT_UNSUBSCRIBE);
  1615. pthread_mutex_unlock (&evti->ei_mutex);
  1616. if (error != SA_AIS_OK) {
  1617. goto unsubscribe_put2;
  1618. }
  1619. error = res.icu_head.error;
  1620. unsubscribe_put2:
  1621. saHandleInstancePut(&evt_instance_handle_db,
  1622. eci->eci_instance_handle);
  1623. unsubscribe_put1:
  1624. saHandleInstancePut(&channel_handle_db, channelHandle);
  1625. unsubscribe_done:
  1626. return error;
  1627. }
  1628. /*
  1629. * The saEvtEventRetentionTimeClear() function is used to clear the retention
  1630. * time of a published event. It indicates to the Event Service that it does
  1631. * not need to keep the event any longer for potential new subscribers. Once
  1632. * the value of the retention time is reset to 0, the event is no longer
  1633. * available for new subscribers. The event is held until all old subscribers
  1634. * in the system process the event and free the event using saEvtEventFree().
  1635. */
  1636. SaAisErrorT
  1637. saEvtEventRetentionTimeClear(
  1638. const SaEvtChannelHandleT channelHandle,
  1639. const SaEvtEventIdT eventId)
  1640. {
  1641. SaAisErrorT error;
  1642. struct event_instance *evti;
  1643. struct event_channel_instance *eci;
  1644. struct req_evt_event_clear_retentiontime req;
  1645. struct res_evt_event_clear_retentiontime res;
  1646. error = saHandleInstanceGet(&channel_handle_db, channelHandle,
  1647. (void*)&eci);
  1648. if (error != SA_AIS_OK) {
  1649. goto ret_time_done;
  1650. }
  1651. error = saHandleInstanceGet(&evt_instance_handle_db,
  1652. eci->eci_instance_handle, (void*)&evti);
  1653. if (error != SA_AIS_OK) {
  1654. goto ret_time_put1;
  1655. }
  1656. req.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  1657. req.iec_head.size = sizeof(req);
  1658. req.iec_channel_handle = eci->eci_svr_channel_handle;
  1659. req.iec_event_id = eventId;
  1660. pthread_mutex_lock(&evti->ei_mutex);
  1661. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  1662. if (error != SA_AIS_OK) {
  1663. pthread_mutex_unlock (&evti->ei_mutex);
  1664. goto ret_time_put2;
  1665. }
  1666. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1667. MESSAGE_RES_EVT_CLEAR_RETENTIONTIME);
  1668. pthread_mutex_unlock (&evti->ei_mutex);
  1669. if (error != SA_AIS_OK) {
  1670. goto ret_time_put2;
  1671. }
  1672. error = res.iec_head.error;
  1673. ret_time_put2:
  1674. saHandleInstancePut(&evt_instance_handle_db,
  1675. eci->eci_instance_handle);
  1676. ret_time_put1:
  1677. saHandleInstancePut(&channel_handle_db, channelHandle);
  1678. ret_time_done:
  1679. return error;
  1680. }
  1681. /*
  1682. * vi: set autoindent tabstop=4 shiftwidth=4 :
  1683. */