evt.c 58 KB

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