evt.c 61 KB

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