evt.c 58 KB

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