evt.c 47 KB

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