evt.c 58 KB

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