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