evt.c 54 KB

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