evt.c 61 KB

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