4
0

evt.c 54 KB

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