evt.c 60 KB

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