evt.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755
  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. pthread_mutex_unlock(&evti->ei_mutex);
  478. goto error_unlock;
  479. }
  480. if (dispatch_data->header.size > sizeof(struct res_header)) {
  481. error = saRecvRetry(evti->ei_fd, dispatch_data->data,
  482. dispatch_data->header.size - sizeof(struct res_header),
  483. MSG_WAITALL | MSG_NOSIGNAL);
  484. if (error != SA_OK) {
  485. pthread_mutex_unlock(&evti->ei_mutex);
  486. goto error_unlock;
  487. }
  488. }
  489. }
  490. /*
  491. * Make copy of callbacks, message data, unlock instance,
  492. * and call callback. A risk of this dispatch method is that
  493. * the callback routines may operate at the same time that
  494. * EvtFinalize has been called in another thread.
  495. */
  496. memcpy(&callbacks, &evti->ei_callback, sizeof(evti->ei_callback));
  497. pthread_mutex_unlock(&evti->ei_mutex);
  498. /*
  499. * Dispatch incoming response
  500. */
  501. switch (dispatch_data->header.id) {
  502. case MESSAGE_RES_LIB_ACTIVATEPOLL:
  503. /*
  504. * This is a do nothing message which the node
  505. * executive sends to activate the file evt_handle
  506. * in poll when the library has queued a message into
  507. * evti->ei_inq. The dispatch is ignored for the
  508. * following two cases:
  509. * 1) setting of timeout to zero for the
  510. * DISPATCH_ALL case
  511. * 2) expiration of the do loop for the
  512. * DISPATCH_ONE case
  513. */
  514. ignore_dispatch = 1;
  515. break;
  516. case MESSAGE_RES_EVT_AVAILABLE:
  517. /*
  518. * There are events available. Send a request for one and then
  519. * dispatch it.
  520. */
  521. evt = (struct lib_event_data *)&evti->ei_message;
  522. res.evd_head.id = MESSAGE_REQ_EVT_EVENT_DATA;
  523. res.evd_head.size = sizeof(res);
  524. error = saSendRetry(evti->ei_fd, &res, sizeof(res), MSG_NOSIGNAL);
  525. if (error != SA_OK) {
  526. printf("MESSAGE_RES_EVT_AVAILABLE: send failed: %d\n", error);
  527. break;
  528. }
  529. error = saRecvQueue(evti->ei_fd, evt, &evti->ei_inq,
  530. MESSAGE_RES_EVT_EVENT_DATA);
  531. if (error != SA_OK) {
  532. printf("MESSAGE_RES_EVT_AVAILABLE: receive failed: %d\n",
  533. error);
  534. break;
  535. }
  536. /*
  537. * No data available. This is OK.
  538. */
  539. if (evt->led_head.error == SA_ERR_NOT_EXIST) {
  540. // printf("MESSAGE_RES_EVT_AVAILABLE: No event data\n");
  541. error = SA_OK;
  542. break;
  543. }
  544. if (evt->led_head.error != SA_OK) {
  545. error = evt->led_head.error;
  546. printf("MESSAGE_RES_EVT_AVAILABLE: Error returned: %d\n",
  547. error);
  548. break;
  549. }
  550. error = make_event(&event_handle, evt);
  551. if (error != SA_OK) {
  552. break;
  553. }
  554. callbacks.saEvtEventDeliverCallback(evt->led_sub_id, event_handle,
  555. evt->led_user_data_size);
  556. break;
  557. case MESSAGE_RES_EVT_CHAN_OPEN_CALLBACK:
  558. /*
  559. * TODO: do something here
  560. */
  561. printf("Dispatch: Open callback\n");
  562. break;
  563. default:
  564. printf("Dispatch: Bad message type 0x%x\n",
  565. dispatch_data->header.id);
  566. error = SA_ERR_LIBRARY;
  567. goto error_nounlock;
  568. break;
  569. }
  570. /*
  571. * If empty is zero it means the we got the
  572. * message from the queue and we are responsible
  573. * for freeing it.
  574. */
  575. if (empty == 0) {
  576. free(msg);
  577. }
  578. /*
  579. * Determine if more messages should be processed
  580. */
  581. switch (dispatch_flags) {
  582. case SA_DISPATCH_ONE:
  583. if (ignore_dispatch) {
  584. ignore_dispatch = 0;
  585. } else {
  586. cont = 0;
  587. }
  588. break;
  589. case SA_DISPATCH_ALL:
  590. if (ignore_dispatch) {
  591. ignore_dispatch = 0;
  592. }
  593. break;
  594. case SA_DISPATCH_BLOCKING:
  595. break;
  596. }
  597. } while (cont);
  598. error_unlock:
  599. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  600. error_nounlock:
  601. return error;
  602. }
  603. /*
  604. * The saEvtFinalize() function closes the association, represented by the
  605. * evt_handle parameter, between the process and the Event Service. It may
  606. * free up resources.
  607. * This function cannot be invoked before the process has invoked the
  608. * corresponding saEvtInitialize() function for the Event Service. After
  609. * this function is invoked, the selection object is no longer valid.
  610. * Moreover, the Event Service is unavailable for further use unless it is
  611. * reinitialized using the saEvtInitialize() function.
  612. */
  613. SaErrorT
  614. saEvtFinalize(SaEvtHandleT evt_handle)
  615. {
  616. struct event_instance *evti;
  617. SaErrorT error;
  618. error = saHandleInstanceGet(&evt_instance_handle_db, evt_handle,
  619. (void *)&evti);
  620. if (error != SA_OK) {
  621. return error;
  622. }
  623. pthread_mutex_lock(&evti->ei_mutex);
  624. /*
  625. * Another thread has already started finalizing
  626. */
  627. if (evti->ei_finalize) {
  628. pthread_mutex_unlock(&evti->ei_mutex);
  629. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  630. return SA_ERR_BAD_HANDLE;
  631. }
  632. evti->ei_finalize = 1;
  633. saActivatePoll(evti->ei_fd);
  634. pthread_mutex_unlock(&evti->ei_mutex);
  635. saHandleDestroy(&evt_instance_handle_db, evt_handle);
  636. saHandleInstancePut(&evt_instance_handle_db, evt_handle);
  637. return error;
  638. }
  639. /*
  640. * The saEvtChannelOpen() function creates a new event channel or open an
  641. * existing channel. The saEvtChannelOpen() function is a blocking operation
  642. * and returns a new event channel handle. An event channel may be opened
  643. * multiple times by the same or different processes for publishing, and
  644. * subscribing to, events. If a process opens an event channel multiple
  645. * times, it is possible to receive the same event multiple times. However,
  646. * a process shall never receive an event more than once on a particular
  647. * event channel handle. If a process opens a channel twice and an event is
  648. * matched on both open channels, the Event Service performs two
  649. * callbacks -- one for each opened channel.
  650. */
  651. SaErrorT
  652. saEvtChannelOpen(
  653. SaEvtHandleT evt_handle,
  654. const SaNameT *channel_name,
  655. SaEvtChannelOpenFlagsT channel_open_flags,
  656. SaTimeT timeout,
  657. SaEvtChannelHandleT *channel_handle)
  658. {
  659. struct event_instance *evti;
  660. struct req_evt_channel_open req;
  661. struct res_evt_channel_open res;
  662. struct event_channel_instance *eci;
  663. SaErrorT error;
  664. error = saHandleInstanceGet(&evt_instance_handle_db, evt_handle,
  665. (void*)&evti);
  666. if (error != SA_OK) {
  667. goto chan_open_done;
  668. }
  669. /*
  670. * create a handle for this open channel
  671. */
  672. error = saHandleCreate(&channel_handle_db, sizeof(*eci),
  673. (void*)channel_handle);
  674. if (error != SA_OK) {
  675. goto chan_open_put;
  676. }
  677. error = saHandleInstanceGet(&channel_handle_db, *channel_handle,
  678. (void*)&eci);
  679. if (error != SA_OK) {
  680. saHandleDestroy(&channel_handle_db, *channel_handle);
  681. goto chan_open_put;
  682. }
  683. /*
  684. * Send the request to the server and wait for a response
  685. */
  686. req.ico_head.size = sizeof(req);
  687. req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL;
  688. req.ico_c_handle = *channel_handle;
  689. req.ico_timeout = timeout;
  690. req.ico_open_flag = channel_open_flags;
  691. req.ico_channel_name = *channel_name;
  692. pthread_mutex_lock(&evti->ei_mutex);
  693. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  694. if (error != SA_OK) {
  695. goto chan_open_free;
  696. }
  697. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  698. MESSAGE_RES_EVT_OPEN_CHANNEL);
  699. pthread_mutex_unlock (&evti->ei_mutex);
  700. if (error != SA_OK) {
  701. goto chan_open_free;
  702. }
  703. error = res.ico_head.error;
  704. if (error != SA_OK) {
  705. goto chan_open_free;
  706. }
  707. eci->eci_svr_channel_handle = res.ico_channel_handle;
  708. eci->eci_channel_name = *channel_name;
  709. eci->eci_open_flags = channel_open_flags;
  710. eci->eci_instance_handle = evt_handle;
  711. eci->eci_closing = 0;
  712. pthread_mutex_init(&eci->eci_mutex, NULL);
  713. saHandleInstancePut (&evt_instance_handle_db, evt_handle);
  714. saHandleInstancePut (&channel_handle_db, *channel_handle);
  715. return SA_OK;
  716. chan_open_free:
  717. saHandleDestroy(&channel_handle_db, *channel_handle);
  718. saHandleInstancePut (&channel_handle_db, *channel_handle);
  719. chan_open_put:
  720. saHandleInstancePut (&evt_instance_handle_db, evt_handle);
  721. chan_open_done:
  722. return error;
  723. }
  724. /*
  725. * The saEvtChannelClose() function closes an event channel and frees
  726. * resources allo-cated for that event channel in the invoking process. If
  727. * the event channel is not refer-enced by any process and does not hold
  728. * any events with non-zero retention time, the Event Service automatically
  729. * deletes the event channel from the cluster namespace.
  730. */
  731. SaErrorT
  732. saEvtChannelClose(SaEvtChannelHandleT channel_handle)
  733. {
  734. SaErrorT error;
  735. struct event_instance *evti;
  736. struct event_channel_instance *eci;
  737. struct req_evt_channel_close req;
  738. struct res_evt_channel_close res;
  739. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  740. (void*)&eci);
  741. if (error != SA_OK) {
  742. goto chan_close_done;
  743. }
  744. /*
  745. * get the evt handle for the fd
  746. */
  747. error = saHandleInstanceGet(&evt_instance_handle_db,
  748. eci->eci_instance_handle, (void*)&evti);
  749. if (error != SA_OK) {
  750. goto chan_close_put1;
  751. }
  752. /*
  753. * Make sure that the channel isn't being closed elsewhere
  754. */
  755. pthread_mutex_lock(&eci->eci_mutex);
  756. if (eci->eci_closing) {
  757. pthread_mutex_unlock(&eci->eci_mutex);
  758. saHandleInstancePut(&channel_handle_db, channel_handle);
  759. return SA_ERR_BAD_HANDLE;
  760. }
  761. eci->eci_closing = 1;
  762. saActivatePoll(evti->ei_fd);
  763. pthread_mutex_unlock(&eci->eci_mutex);
  764. /*
  765. * Send the request to the server and wait for a response
  766. */
  767. req.icc_head.size = sizeof(req);
  768. req.icc_head.id = MESSAGE_REQ_EVT_CLOSE_CHANNEL;
  769. req.icc_channel_handle = eci->eci_svr_channel_handle;
  770. pthread_mutex_lock(&evti->ei_mutex);
  771. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  772. if (error != SA_OK) {
  773. pthread_mutex_unlock(&evti->ei_mutex);
  774. eci->eci_closing = 0;
  775. goto chan_close_put2;
  776. }
  777. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  778. MESSAGE_RES_EVT_CLOSE_CHANNEL);
  779. pthread_mutex_unlock(&evti->ei_mutex);
  780. if (error != SA_OK) {
  781. eci->eci_closing = 0;
  782. goto chan_close_put2;
  783. }
  784. error = res.icc_head.error;
  785. saHandleInstancePut(&evt_instance_handle_db,
  786. eci->eci_instance_handle);
  787. saHandleDestroy(&channel_handle_db, channel_handle);
  788. saHandleInstancePut(&channel_handle_db, channel_handle);
  789. return error;
  790. chan_close_put2:
  791. saHandleInstancePut(&evt_instance_handle_db,
  792. eci->eci_instance_handle);
  793. chan_close_put1:
  794. saHandleInstancePut(&channel_handle_db, channel_handle);
  795. chan_close_done:
  796. return error;
  797. }
  798. SaErrorT
  799. saEvtChannelOpenAsync(SaEvtHandleT evt_handle,
  800. SaInvocationT invocation,
  801. const SaNameT *channel_name,
  802. SaEvtChannelOpenFlagsT channel_open_flags)
  803. {
  804. /*
  805. * TODO: Fill in code
  806. */
  807. return SA_ERR_LIBRARY;
  808. }
  809. SaErrorT
  810. SaEvtChannelUnlink(
  811. SaEvtHandleT evtHandle,
  812. const SaNameT *channelName)
  813. {
  814. /*
  815. * TODO: Fill in code
  816. */
  817. return SA_ERR_LIBRARY;
  818. }
  819. /*
  820. * The saEvtEventAllocate() function allocates memory for the event, but not
  821. * for the eventHandle, and initializes all event attributes to default values.
  822. * The event allocated by saEvtEventAllocate() is freed by invoking
  823. * saEvtEventFree().
  824. * The memory associated with the eventHandle is not deallocated by the
  825. * saEvtEventAllocate() function or the saEvtEventFree() function. It is the
  826. * responsibility of the invoking process to deallocate the memory for the
  827. * eventHandle when the process has published the event and has freed the
  828. * event by invoking saEvtEventFree().
  829. */
  830. SaErrorT
  831. saEvtEventAllocate(
  832. const SaEvtChannelHandleT channel_handle,
  833. SaEvtEventHandleT *event_handle)
  834. {
  835. SaErrorT error;
  836. struct event_data_instance *edi;
  837. struct event_instance *evti;
  838. struct event_channel_instance *eci;
  839. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  840. (void*)&eci);
  841. if (error != SA_OK) {
  842. goto alloc_done;
  843. }
  844. error = saHandleInstanceGet(&evt_instance_handle_db,
  845. eci->eci_instance_handle, (void*)&evti);
  846. if (error != SA_OK) {
  847. goto alloc_put1;
  848. }
  849. error = saHandleCreate(&event_handle_db, sizeof(*edi),
  850. (void*)event_handle);
  851. if (error != SA_OK) {
  852. goto alloc_put2;
  853. }
  854. error = saHandleInstanceGet(&event_handle_db, *event_handle,
  855. (void*)&edi);
  856. if (error != SA_OK) {
  857. goto alloc_put2;
  858. }
  859. memset(edi, 0, sizeof(*edi));
  860. pthread_mutex_init(&edi->edi_mutex, NULL);
  861. edi->edi_freeing = 0;
  862. edi->edi_channel_handle = channel_handle;
  863. edi->edi_pub_node = evti->ei_node_id;
  864. edi->edi_priority = SA_EVT_LOWEST_PRIORITY;
  865. saHandleInstancePut (&event_handle_db, *event_handle);
  866. alloc_put2:
  867. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  868. alloc_put1:
  869. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  870. alloc_done:
  871. return error;
  872. }
  873. /*
  874. * The saEvtEventFree() function gives the Event Service premission to
  875. * deallocate the memory that contains the attributes of the event that is
  876. * associated with eventHandle. The function is used to free events allocated
  877. * by saEvtEventAllocate() and by saEvtEventDeliverCallback().
  878. */
  879. SaErrorT
  880. saEvtEventFree(SaEvtEventHandleT event_handle)
  881. {
  882. SaErrorT error;
  883. struct event_data_instance *edi;
  884. error = saHandleInstanceGet(&event_handle_db, event_handle,
  885. (void*)&edi);
  886. if (error != SA_OK) {
  887. goto evt_free_done;
  888. }
  889. /*
  890. * Make sure that the event isn't being freed elsewhere
  891. */
  892. pthread_mutex_lock(&edi->edi_mutex);
  893. if (edi->edi_freeing) {
  894. pthread_mutex_unlock(&edi->edi_mutex);
  895. saHandleInstancePut(&event_handle_db, event_handle);
  896. return SA_ERR_BAD_HANDLE;
  897. }
  898. edi->edi_freeing = 1;
  899. pthread_mutex_unlock(&edi->edi_mutex);
  900. saHandleDestroy(&event_handle_db, event_handle);
  901. saHandleInstancePut(&event_handle_db, event_handle);
  902. evt_free_done:
  903. return error;
  904. }
  905. /*
  906. * This function may be used to assign writeable event attributes. It takes
  907. * as arguments an event handle event_handle and the attribute to be set in
  908. * the event structure of the event with that handle. Note: The only
  909. * attributes that a process can set are the pattern_array, priority,
  910. * retention_time and publisher_name attributes.
  911. */
  912. SaErrorT
  913. saEvtEventAttributesSet(
  914. const SaEvtEventHandleT event_handle,
  915. const SaEvtEventPatternArrayT *pattern_array,
  916. SaEvtEventPriorityT priority,
  917. SaTimeT retention_time,
  918. const SaNameT *publisher_name)
  919. {
  920. SaEvtEventPatternT *oldpatterns;
  921. SaSizeT oldnumber;
  922. SaErrorT error;
  923. struct event_data_instance *edi;
  924. int i;
  925. error = saHandleInstanceGet(&event_handle_db, event_handle,
  926. (void*)&edi);
  927. if (error != SA_OK) {
  928. goto attr_set_done;
  929. }
  930. pthread_mutex_lock(&edi->edi_mutex);
  931. edi->edi_priority = priority;
  932. edi->edi_retention_time = retention_time;
  933. /*
  934. * publisher_name or pattern_array not allowed to be NULL
  935. */
  936. if (!publisher_name || !pattern_array) {
  937. error = SA_ERR_INVALID_PARAM;
  938. goto attr_set_unlock;
  939. }
  940. edi->edi_pub_name = *publisher_name;
  941. oldpatterns = edi->edi_patterns.patterns;
  942. oldnumber = edi->edi_patterns.patternsNumber;
  943. edi->edi_patterns.patterns = 0;
  944. edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
  945. pattern_array->patternsNumber);
  946. if (!edi->edi_patterns.patterns) {
  947. error = SA_ERR_NO_MEMORY;
  948. goto attr_set_done_reset;
  949. }
  950. edi->edi_patterns.patternsNumber = pattern_array->patternsNumber;
  951. /*
  952. * copy the patterns from the caller. allocating memory for
  953. * of all the strings.
  954. */
  955. for (i = 0; i < pattern_array->patternsNumber; i++) {
  956. edi->edi_patterns.patterns[i].pattern =
  957. malloc(pattern_array->patterns[i].patternSize);
  958. if (!edi->edi_patterns.patterns[i].pattern) {
  959. int j;
  960. for (j = 0; j < i; j++) {
  961. free(edi->edi_patterns.patterns[j].pattern);
  962. }
  963. free(edi->edi_patterns.patterns);
  964. error = SA_ERR_NO_MEMORY;
  965. goto attr_set_done_reset;
  966. }
  967. memcpy(edi->edi_patterns.patterns[i].pattern,
  968. pattern_array->patterns[i].pattern,
  969. pattern_array->patterns[i].patternSize);
  970. edi->edi_patterns.patterns[i].patternSize =
  971. pattern_array->patterns[i].patternSize;
  972. }
  973. /*
  974. * free up the old pattern memory
  975. */
  976. if (oldpatterns) {
  977. for (i = 0; i < oldnumber; i++) {
  978. if (oldpatterns[i].pattern) {
  979. free(oldpatterns[i].pattern);
  980. }
  981. }
  982. free (oldpatterns);
  983. }
  984. goto attr_set_unlock;
  985. attr_set_done_reset:
  986. edi->edi_patterns.patterns = oldpatterns;
  987. edi->edi_patterns.patternsNumber = oldnumber;
  988. attr_set_unlock:
  989. pthread_mutex_unlock(&edi->edi_mutex);
  990. saHandleInstancePut(&event_handle_db, event_handle);
  991. attr_set_done:
  992. return error;
  993. }
  994. /*
  995. * This function takes as parameters an event handle eventHandle and the
  996. * attributes of the event with that handle. The function retrieves the
  997. * value of the attributes for the event and stores them at the address
  998. * provided for the out parameters.
  999. * It is the responsibility of the invoking process to allocate memory for
  1000. * the out parameters before it invokes this function. The Event Service
  1001. * assigns the out values into that memory. For each of the out, or in/out,
  1002. * parameters, if the invoking process provides a NULL reference, the
  1003. * Availability Management Framework does not return the out value.
  1004. * Similarly, it is the responsibility of the invoking process to allocate
  1005. * memory for the pattern_array.
  1006. */
  1007. SaErrorT
  1008. saEvtEventAttributesGet(
  1009. SaEvtEventHandleT event_handle,
  1010. SaEvtEventPatternArrayT *pattern_array,
  1011. SaEvtEventPriorityT *priority,
  1012. SaTimeT *retention_time,
  1013. SaNameT *publisher_name,
  1014. SaTimeT *publish_time,
  1015. SaEvtEventIdT *event_id)
  1016. {
  1017. SaErrorT error;
  1018. struct event_data_instance *edi;
  1019. SaSizeT npats;
  1020. int i;
  1021. error = saHandleInstanceGet(&event_handle_db, event_handle,
  1022. (void*)&edi);
  1023. if (error != SA_OK) {
  1024. goto attr_get_done;
  1025. }
  1026. pthread_mutex_lock(&edi->edi_mutex);
  1027. /*
  1028. * Go through the args and send back information if the pointer
  1029. * isn't NULL
  1030. */
  1031. if (event_id) {
  1032. *event_id = edi->edi_event_id;
  1033. }
  1034. if (publish_time) {
  1035. *publish_time = edi->edi_pub_time;
  1036. }
  1037. if (publisher_name) {
  1038. *publisher_name = edi->edi_pub_name;
  1039. }
  1040. if (retention_time) {
  1041. *retention_time = edi->edi_retention_time;
  1042. }
  1043. if (priority) {
  1044. *priority = edi->edi_priority;
  1045. }
  1046. if (!pattern_array) {
  1047. goto attr_get_unlock;
  1048. }
  1049. npats = min(pattern_array->patternsNumber,
  1050. edi->edi_patterns.patternsNumber);
  1051. /*
  1052. * We set the returned number of patterns to the actual
  1053. * pattern count of the event. This way the caller can tell
  1054. * if it got all the possible patterns. If the returned number
  1055. * is more that the number supplied, then some available patterns
  1056. * were not returned.
  1057. *
  1058. * The same thing happens when copying the pattern strings.
  1059. */
  1060. pattern_array->patternsNumber = edi->edi_patterns.patternsNumber;
  1061. for (i = 0; i < npats; i++) {
  1062. memcpy(pattern_array->patterns[i].pattern,
  1063. edi->edi_patterns.patterns[i].pattern,
  1064. min(pattern_array->patterns[i].patternSize,
  1065. edi->edi_patterns.patterns[i].patternSize));
  1066. pattern_array->patterns[i].patternSize =
  1067. edi->edi_patterns.patterns[i].patternSize;
  1068. }
  1069. attr_get_unlock:
  1070. pthread_mutex_unlock(&edi->edi_mutex);
  1071. saHandleInstancePut(&event_handle_db, event_handle);
  1072. attr_get_done:
  1073. return error;
  1074. }
  1075. /*
  1076. * The saEvtEventDataGet() function allows a process to retrieve the data
  1077. * associated with an event previously delivered by
  1078. * saEvtEventDeliverCallback().
  1079. */
  1080. SaErrorT
  1081. saEvtEventDataGet(
  1082. const SaEvtEventHandleT event_handle,
  1083. void *event_data,
  1084. SaSizeT *event_data_size)
  1085. {
  1086. SaErrorT error = SA_OK;
  1087. struct event_data_instance *edi;
  1088. SaSizeT xfsize;
  1089. if (!event_data || !event_data_size) {
  1090. goto data_get_done;
  1091. }
  1092. error = saHandleInstanceGet(&event_handle_db, event_handle,
  1093. (void*)&edi);
  1094. if (error != SA_OK) {
  1095. goto data_get_done;
  1096. }
  1097. pthread_mutex_lock(&edi->edi_mutex);
  1098. if (edi->edi_event_data && edi->edi_event_data_size) {
  1099. xfsize = min(*event_data_size, edi->edi_event_data_size);
  1100. *event_data_size = edi->edi_event_data_size;
  1101. memcpy(event_data, edi->edi_event_data, xfsize);
  1102. } else {
  1103. *event_data_size = 0;
  1104. }
  1105. pthread_mutex_unlock(&edi->edi_mutex);
  1106. saHandleInstancePut(&event_handle_db, event_handle);
  1107. data_get_done:
  1108. return error;
  1109. }
  1110. /*
  1111. * Calculate the size in bytes for patterns
  1112. */
  1113. static size_t patt_size(const SaEvtEventPatternArrayT *patterns)
  1114. {
  1115. int i;
  1116. size_t size = sizeof(SaEvtEventPatternArrayT);
  1117. for (i = 0; i < patterns->patternsNumber; i++) {
  1118. size += sizeof(SaEvtEventPatternT);
  1119. size += patterns->patterns[i].patternSize;
  1120. }
  1121. return size;
  1122. }
  1123. /*
  1124. * copy patterns to a form for sending to the server
  1125. */
  1126. static uint32_t aispatt_to_evt_patt(const SaEvtEventPatternArrayT *patterns,
  1127. void *data)
  1128. {
  1129. int i;
  1130. SaEvtEventPatternT *pats = data;
  1131. SaUint8T *str = (SaUint8T *)pats +
  1132. (patterns->patternsNumber * sizeof(*pats));
  1133. /*
  1134. * Pointers are replaced with offsets into the data array. These
  1135. * will be later converted back into pointers when received as events.
  1136. */
  1137. for (i = 0; i < patterns->patternsNumber; i++) {
  1138. memcpy(str, patterns->patterns[i].pattern,
  1139. patterns->patterns[i].patternSize);
  1140. pats->patternSize = patterns->patterns[i].patternSize;
  1141. pats->pattern = (SaUint8T *)((void *)str - data);
  1142. str += patterns->patterns[i].patternSize;
  1143. pats++;
  1144. }
  1145. return patterns->patternsNumber;
  1146. }
  1147. /*
  1148. * Calculate the size in bytes for filters
  1149. */
  1150. static size_t filt_size(const SaEvtEventFilterArrayT *filters)
  1151. {
  1152. int i;
  1153. size_t size = sizeof(SaEvtEventFilterArrayT);
  1154. for (i = 0; i < filters->filtersNumber; i++) {
  1155. size += sizeof(SaEvtEventFilterT);
  1156. size += filters->filters[i].filter.patternSize;
  1157. }
  1158. return size;
  1159. }
  1160. /*
  1161. * Convert the Sa filters to a form that can be sent over the network
  1162. * i.e. replace pointers with offsets. The pointers will be reconstituted
  1163. * by the receiver.
  1164. */
  1165. static uint32_t aisfilt_to_evt_filt(const SaEvtEventFilterArrayT *filters,
  1166. void *data)
  1167. {
  1168. int i;
  1169. SaEvtEventFilterArrayT *filta = data;
  1170. SaEvtEventFilterT *filts = data + sizeof(SaEvtEventFilterArrayT);
  1171. SaUint8T *str = (SaUint8T *)filts +
  1172. (filters->filtersNumber * sizeof(*filts));
  1173. /*
  1174. * Pointers are replaced with offsets into the data array. These
  1175. * will be later converted back into pointers by the evt server.
  1176. */
  1177. filta->filters = (SaEvtEventFilterT *)((void *)filts - data);
  1178. filta->filtersNumber = filters->filtersNumber;
  1179. for (i = 0; i < filters->filtersNumber; i++) {
  1180. filts->filterType = filters->filters[i].filterType;
  1181. filts->filter.patternSize =
  1182. filters->filters[i].filter.patternSize;
  1183. memcpy(str,
  1184. filters->filters[i].filter.pattern,
  1185. filters->filters[i].filter.patternSize);
  1186. filts->filter.pattern = (SaUint8T *)((void *)str - data);
  1187. str += filters->filters[i].filter.patternSize;
  1188. filts++;
  1189. }
  1190. return filters->filtersNumber;
  1191. }
  1192. /*
  1193. * The saEvtEventPublish() function publishes an event on the channel
  1194. * designated by channel_handle. The event to be published consists of a
  1195. * standard set of attributes (the event header) and an optional data part.
  1196. * Before an event can be published, the publisher process must invoke the
  1197. * saEvtEventPatternArraySet() function to set the event patterns. The event
  1198. * is delivered to subscribers whose subscription filter matches the event
  1199. * patterns.
  1200. * When the Event Service publishes an event, it automatically sets
  1201. * the following readonly event attributes:
  1202. * - Event attribute time
  1203. * - Event publisher identifier
  1204. * - Event publisher node identifier
  1205. * - Event identifier
  1206. * In addition to the event attributes, a process can supply values for the
  1207. * eventData and eventDataSize parameters for publication as part of the
  1208. * event. The data portion of the event may be at most SA_EVT_DATA_MAX_LEN
  1209. * bytes in length.
  1210. * The process may assume that the invocation of saEvtEventPublish() copies
  1211. * all pertinent parameters, including the memory associated with the
  1212. * eventHandle and eventData parameters, to its own local memory. However,
  1213. * the invocation does not automatically deallocate memory associated with
  1214. * the eventHandle and eventData parameters. It is the responsibility of the
  1215. * invoking process to deallocate the memory for those parameters after
  1216. * saEvtEventPublish() returns.
  1217. */
  1218. SaErrorT
  1219. saEvtEventPublish(
  1220. const SaEvtEventHandleT event_handle,
  1221. const void *event_data,
  1222. SaSizeT event_data_size,
  1223. SaEvtEventIdT *eventid)
  1224. {
  1225. SaErrorT error;
  1226. struct event_data_instance *edi;
  1227. struct event_instance *evti;
  1228. struct event_channel_instance *eci;
  1229. struct lib_event_data *req;
  1230. struct res_evt_event_publish res;
  1231. size_t pattern_size;
  1232. struct event_pattern *patterns;
  1233. void *data_start;
  1234. int pub_sleep_trys = PUB_SLEEP_TRYS;
  1235. if (event_data_size > SA_EVT_DATA_MAX_LEN) {
  1236. error = SA_ERR_INVALID_PARAM;
  1237. goto pub_done;
  1238. }
  1239. error = saHandleInstanceGet(&event_handle_db, event_handle,
  1240. (void*)&edi);
  1241. if (error != SA_OK) {
  1242. goto pub_done;
  1243. }
  1244. pthread_mutex_lock(&edi->edi_mutex);
  1245. /*
  1246. * See if patterns have been set for this event. If not, we
  1247. * can't publish.
  1248. */
  1249. if (!edi->edi_patterns.patterns) {
  1250. error = SA_ERR_INVALID_PARAM;
  1251. goto pub_put1;
  1252. }
  1253. error = saHandleInstanceGet(&channel_handle_db, edi->edi_channel_handle,
  1254. (void*)&eci);
  1255. if (error != SA_OK) {
  1256. goto pub_put1;
  1257. }
  1258. /*
  1259. * See if we can publish to this channel
  1260. */
  1261. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_PUBLISHER)) {
  1262. error = SA_ERR_ACCESS;
  1263. goto pub_put2;
  1264. }
  1265. error = saHandleInstanceGet(&evt_instance_handle_db,
  1266. eci->eci_instance_handle, (void*)&evti);
  1267. if (error != SA_OK) {
  1268. goto pub_put2;
  1269. }
  1270. /*
  1271. * Figure out how much memory we need for the patterns and data
  1272. */
  1273. pattern_size = patt_size(&edi->edi_patterns);
  1274. req = malloc(sizeof(*req) + event_data_size + pattern_size);
  1275. patterns = (struct event_pattern *)req->led_body;
  1276. data_start = (void *)req->led_body + pattern_size;
  1277. if (!req) {
  1278. error = SA_ERR_NO_MEMORY;
  1279. goto pub_put3;
  1280. }
  1281. /*
  1282. * copy everything to the request structure
  1283. */
  1284. aispatt_to_evt_patt(&edi->edi_patterns, patterns);
  1285. req->led_patterns_number = edi->edi_patterns.patternsNumber;
  1286. req->led_user_data_offset = pattern_size;
  1287. if (event_data && event_data_size) {
  1288. memcpy(data_start, event_data, event_data_size);
  1289. req->led_user_data_size = event_data_size;
  1290. } else {
  1291. req->led_user_data_size = 0;
  1292. }
  1293. req->led_head.id = MESSAGE_REQ_EVT_PUBLISH;
  1294. req->led_head.size = sizeof(*req) + pattern_size + event_data_size;
  1295. req->led_svr_channel_handle = eci->eci_svr_channel_handle;
  1296. req->led_retention_time = edi->edi_retention_time;
  1297. req->led_publish_time = clustTimeNow();
  1298. req->led_priority = edi->edi_priority;
  1299. req->led_publisher_name = edi->edi_pub_name;
  1300. while (--pub_sleep_trys) {
  1301. pthread_mutex_lock(&evti->ei_mutex);
  1302. error = saSendRetry(evti->ei_fd, req, req->led_head.size, MSG_NOSIGNAL);
  1303. if (error != SA_OK) {
  1304. pthread_mutex_unlock (&evti->ei_mutex);
  1305. goto pub_put3_free;
  1306. }
  1307. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1308. MESSAGE_RES_EVT_PUBLISH);
  1309. pthread_mutex_unlock (&evti->ei_mutex);
  1310. if (error != SA_OK) {
  1311. goto pub_put3_free;
  1312. }
  1313. error = res.iep_head.error;
  1314. if (error == SA_ERR_TRY_AGAIN) {
  1315. struct timespec ts;
  1316. struct timespec rem;
  1317. ts.tv_sec = PUB_SLEEP_SEC;
  1318. ts.tv_nsec = PUB_SLEEP_NSEC;
  1319. pub_sleep:
  1320. if (nanosleep(&ts, &rem) < 0) {
  1321. if (errno == EINTR) {
  1322. ts = rem;
  1323. goto pub_sleep;
  1324. }
  1325. error = SA_ERR_TIMEOUT;
  1326. goto pub_put3_free;
  1327. }
  1328. continue;
  1329. }
  1330. *eventid = res.iep_event_id;
  1331. break;
  1332. }
  1333. if (error == SA_ERR_TRY_AGAIN) {
  1334. error = SA_ERR_TIMEOUT;
  1335. }
  1336. pub_put3_free:
  1337. free(req);
  1338. pub_put3:
  1339. saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
  1340. pub_put2:
  1341. saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
  1342. pub_put1:
  1343. pthread_mutex_unlock(&edi->edi_mutex);
  1344. saHandleInstancePut(&event_handle_db, event_handle);
  1345. pub_done:
  1346. return error;
  1347. }
  1348. /*
  1349. * The saEvtEventSubscribe() function enables a process to subscribe for
  1350. * events on an event channel by registering one or more filters on that
  1351. * event channel. The process must have opened the event channel, designated
  1352. * by channel_handle, with the SA_EVT_CHANNEL_SUBSCRIBER flag set for an
  1353. * invocation of this function to be successful.
  1354. * The memory associated with the filters is not deallocated by the
  1355. * saEvtEventSubscribe() function. It is the responsibility of the invoking
  1356. * process to deallocate the memory when the saEvtEventSubscribe() function
  1357. * returns.
  1358. * For a given subscription, the filters parameter cannot be modified. To
  1359. * change the filters parameter without losing events, a process must
  1360. * establish a new subscription with the new filters parameter. After the new
  1361. * subscription is established, the old subscription can be removed by
  1362. * invoking the saEvtEventUnsubscribe() function.
  1363. */
  1364. SaErrorT
  1365. saEvtEventSubscribe(
  1366. const SaEvtChannelHandleT channel_handle,
  1367. const SaEvtEventFilterArrayT *filters,
  1368. SaEvtSubscriptionIdT subscription_id)
  1369. {
  1370. SaErrorT error;
  1371. struct event_instance *evti;
  1372. struct event_channel_instance *eci;
  1373. struct req_evt_event_subscribe *req;
  1374. struct res_evt_event_subscribe res;
  1375. int sz;
  1376. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1377. (void*)&eci);
  1378. if (error != SA_OK) {
  1379. goto subscribe_done;
  1380. }
  1381. /*
  1382. * get the evt handle for the fd
  1383. */
  1384. error = saHandleInstanceGet(&evt_instance_handle_db,
  1385. eci->eci_instance_handle, (void*)&evti);
  1386. if (error != SA_OK) {
  1387. goto subscribe_put1;
  1388. }
  1389. /*
  1390. * See if we can subscribe to this channel
  1391. */
  1392. if (!(eci->eci_open_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
  1393. error = SA_ERR_INVALID_PARAM;
  1394. goto subscribe_put2;
  1395. }
  1396. /*
  1397. * calculate size needed to store the filters
  1398. */
  1399. sz = filt_size(filters);
  1400. req = malloc(sizeof(*req) + sz);
  1401. if (!req) {
  1402. error = SA_ERR_NO_MEMORY;
  1403. goto subscribe_put2;
  1404. }
  1405. /*
  1406. * Copy the supplied filters to the request
  1407. */
  1408. req->ics_filter_count = aisfilt_to_evt_filt(filters,
  1409. req->ics_filter_data);
  1410. req->ics_head.id = MESSAGE_REQ_EVT_SUBSCRIBE;
  1411. req->ics_head.size = sizeof(*req) + sz;
  1412. req->ics_channel_handle = eci->eci_svr_channel_handle;
  1413. req->ics_sub_id = subscription_id;
  1414. req->ics_filter_size = sz;
  1415. pthread_mutex_lock(&evti->ei_mutex);
  1416. error = saSendRetry(evti->ei_fd, req, req->ics_head.size, MSG_NOSIGNAL);
  1417. free(req);
  1418. if (error != SA_OK) {
  1419. pthread_mutex_unlock (&evti->ei_mutex);
  1420. goto subscribe_put2;
  1421. }
  1422. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1423. MESSAGE_RES_EVT_SUBSCRIBE);
  1424. pthread_mutex_unlock (&evti->ei_mutex);
  1425. if (error != SA_OK) {
  1426. goto subscribe_put2;
  1427. }
  1428. error = res.ics_head.error;
  1429. subscribe_put2:
  1430. saHandleInstancePut(&evt_instance_handle_db,
  1431. eci->eci_instance_handle);
  1432. subscribe_put1:
  1433. saHandleInstancePut(&channel_handle_db, channel_handle);
  1434. subscribe_done:
  1435. return error;
  1436. }
  1437. /*
  1438. * The saEvtEventUnsubscribe() function allows a process to stop receiving
  1439. * events for a particular subscription on an event channel by removing the
  1440. * subscription. The saEvtEventUnsubscribe() operation is successful if the
  1441. * subscriptionId parameter matches a previously registered subscription.
  1442. * Pending events that no longer match any subscription, because the
  1443. * saEvtEventUnsubscribe() operation had been invoked, are purged. a process
  1444. * that wishes to modify a subscription without losing any events must
  1445. * establish the new subscription before removing the existing subscription.
  1446. */
  1447. SaErrorT
  1448. saEvtEventUnsubscribe(
  1449. const SaEvtChannelHandleT channel_handle,
  1450. SaEvtSubscriptionIdT subscription_id)
  1451. {
  1452. SaErrorT error;
  1453. struct event_instance *evti;
  1454. struct event_channel_instance *eci;
  1455. struct req_evt_event_unsubscribe req;
  1456. struct res_evt_event_unsubscribe res;
  1457. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1458. (void*)&eci);
  1459. if (error != SA_OK) {
  1460. goto unsubscribe_done;
  1461. }
  1462. error = saHandleInstanceGet(&evt_instance_handle_db,
  1463. eci->eci_instance_handle, (void*)&evti);
  1464. if (error != SA_OK) {
  1465. goto unsubscribe_put1;
  1466. }
  1467. req.icu_head.id = MESSAGE_REQ_EVT_UNSUBSCRIBE;
  1468. req.icu_head.size = sizeof(req);
  1469. req.icu_channel_handle = eci->eci_svr_channel_handle;
  1470. req.icu_sub_id = subscription_id;
  1471. pthread_mutex_lock(&evti->ei_mutex);
  1472. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  1473. if (error != SA_OK) {
  1474. pthread_mutex_unlock (&evti->ei_mutex);
  1475. goto unsubscribe_put2;
  1476. }
  1477. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1478. MESSAGE_RES_EVT_UNSUBSCRIBE);
  1479. pthread_mutex_unlock (&evti->ei_mutex);
  1480. if (error != SA_OK) {
  1481. goto unsubscribe_put2;
  1482. }
  1483. error = res.icu_head.error;
  1484. unsubscribe_put2:
  1485. saHandleInstancePut(&evt_instance_handle_db,
  1486. eci->eci_instance_handle);
  1487. unsubscribe_put1:
  1488. saHandleInstancePut(&channel_handle_db, channel_handle);
  1489. unsubscribe_done:
  1490. return error;
  1491. }
  1492. /*
  1493. * The saEvtEventRetentionTimeClear() function is used to clear the retention
  1494. * time of a published event. It indicates to the Event Service that it does
  1495. * not need to keep the event any longer for potential new subscribers. Once
  1496. * the value of the retention time is reset to 0, the event is no longer
  1497. * available for new subscribers. The event is held until all old subscribers
  1498. * in the system process the event and free the event using saEvtEventFree().
  1499. */
  1500. SaErrorT
  1501. saEvtEventRetentionTimeClear(
  1502. const SaEvtChannelHandleT channel_handle,
  1503. const SaEvtEventIdT event_id)
  1504. {
  1505. SaErrorT error;
  1506. struct event_instance *evti;
  1507. struct event_channel_instance *eci;
  1508. struct req_evt_event_clear_retentiontime req;
  1509. struct res_evt_event_clear_retentiontime res;
  1510. error = saHandleInstanceGet(&channel_handle_db, channel_handle,
  1511. (void*)&eci);
  1512. if (error != SA_OK) {
  1513. goto ret_time_done;
  1514. }
  1515. error = saHandleInstanceGet(&evt_instance_handle_db,
  1516. eci->eci_instance_handle, (void*)&evti);
  1517. if (error != SA_OK) {
  1518. goto ret_time_put1;
  1519. }
  1520. req.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
  1521. req.iec_head.size = sizeof(req);
  1522. req.iec_channel_handle = eci->eci_svr_channel_handle;
  1523. req.iec_event_id = event_id;
  1524. pthread_mutex_lock(&evti->ei_mutex);
  1525. error = saSendRetry(evti->ei_fd, &req, sizeof(req), MSG_NOSIGNAL);
  1526. if (error != SA_OK) {
  1527. pthread_mutex_unlock (&evti->ei_mutex);
  1528. goto ret_time_put2;
  1529. }
  1530. error = saRecvQueue(evti->ei_fd, &res, &evti->ei_inq,
  1531. MESSAGE_RES_EVT_CLEAR_RETENTIONTIME);
  1532. pthread_mutex_unlock (&evti->ei_mutex);
  1533. if (error != SA_OK) {
  1534. goto ret_time_put2;
  1535. }
  1536. error = res.iec_head.error;
  1537. ret_time_put2:
  1538. saHandleInstancePut(&evt_instance_handle_db,
  1539. eci->eci_instance_handle);
  1540. ret_time_put1:
  1541. saHandleInstancePut(&channel_handle_db, channel_handle);
  1542. ret_time_done:
  1543. return error;
  1544. }