evt.c 52 KB

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