evt.c 52 KB

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