evt.c 54 KB

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