evt.c 58 KB

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