evt.c 61 KB

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