evt.c 61 KB

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