evt.c 57 KB

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