evt.c 43 KB

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