| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271 |
- /*
- * Copyright (c) 2004-2005 Mark Haverkamp
- * Copyright (c) 2004-2005 Open Source Development Lab
- *
- * All rights reserved.
- *
- * This software licensed under BSD license, the text of which follows:
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * - Neither the name of the Open Source Developement Lab nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <sys/types.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include "../include/ipc_evt.h"
- #include "util.h"
- #include "../exec/totem.h"
- #include "../include/list.h"
- static void evtHandleInstanceDestructor(void *instance);
- static void chanHandleInstanceDestructor(void *instance);
- static void eventHandleInstanceDestructor(void *instance);
- /*
- * Versions of the SAF AIS specification supported by this library
- */
- static SaVersionT supported_versions[] = {
- {'B', 0x01, 0x01}
- };
- static struct saVersionDatabase evt_version_database = {
- sizeof(supported_versions) / sizeof(SaVersionT),
- supported_versions
- };
- /*
- * Event instance data
- */
- struct saHandleDatabase evt_instance_handle_db = {
- .handleCount = 0,
- .handles = 0,
- .mutex = PTHREAD_MUTEX_INITIALIZER,
- .handleInstanceDestructor = evtHandleInstanceDestructor
- };
- /*
- * Channel instance data
- */
- struct saHandleDatabase channel_handle_db = {
- .handleCount = 0,
- .handles = 0,
- .mutex = PTHREAD_MUTEX_INITIALIZER,
- .handleInstanceDestructor = chanHandleInstanceDestructor
- };
- /*
- * Event instance data
- */
- struct saHandleDatabase event_handle_db = {
- .handleCount = 0,
- .handles = 0,
- .mutex = PTHREAD_MUTEX_INITIALIZER,
- .handleInstanceDestructor = eventHandleInstanceDestructor
- };
- struct res_overlay {
- mar_res_header_t header __attribute__((aligned(8)));
- char data[MESSAGE_SIZE_MAX];
- };
- struct handle_list {
- SaUint64T hl_handle;
- struct list_head hl_entry;
- };
- /*
- * data required to support events for a given initialization
- *
- * ei_dispatch_fd: fd used for getting callback data e.g. async event data
- * ei_response_fd: fd used for everything else (i.e. evt sync api commands).
- * ei_callback: callback function.
- * ei_version: version sent to the evtInitialize call.
- * ei_node_id: our node id.
- * ei_node_name: our node name.
- * ei_finalize: instance in finalize flag
- * ei_dispatch_mutex: mutex for dispatch fd
- * ei_response_mutex: mutex for response fd
- * ei_channel_list: list of associated channels (struct handle_list)
- * ei_data_available: Indicates that there is a pending event message though
- * there may not be a poll event. This can happen
- * when we get a SA_AIS_ERR_TRY_AGAIN when asking for an
- * event.
- *
- */
- struct event_instance {
- int ei_dispatch_fd;
- int ei_response_fd;
- SaEvtCallbacksT ei_callback;
- SaVersionT ei_version;
- SaClmNodeIdT ei_node_id;
- SaNameT ei_node_name;
- int ei_finalize;
- pthread_mutex_t ei_dispatch_mutex;
- pthread_mutex_t ei_response_mutex;
- struct list_head ei_channel_list;
- int ei_data_available;
- };
- /*
- * Data associated with an opened channel
- *
- * eci_channel_name: name of channel
- * eci_open_flags: channel open flags
- * eci_svr_channel_handle: channel handle returned from server
- * eci_closing: channel in process of being closed
- * eci_mutex: channel mutex
- * eci_event_list: events associated with this
- * channel (struct handle_list)
- * eci_hl: pointer to event instance handle struct
- * for this channel.
- */
- struct event_channel_instance {
- SaNameT eci_channel_name;
- SaEvtChannelOpenFlagsT eci_open_flags;
- uint32_t eci_svr_channel_handle;
- SaEvtHandleT eci_instance_handle;
- int eci_closing;
- pthread_mutex_t eci_mutex;
- struct list_head eci_event_list;
- struct handle_list *eci_hl;
-
- };
- /*
- * Event data.
- *
- * Store event data from saEvtEventAllocate function.
- * Store event data from received events.
- *
- * edi_channel_handle: handle (local) of assocated channel
- * edi_patterns: event patterns
- * edi_priority: event priority
- * edi_retention_time: event's retention time
- * edi_pub_name: event's publisher name
- * edi_pub_node: event's publisher node
- * edi_pub_time: event's publish time
- * edi_event_id: event's Id
- * edi_event_data: event's data
- * edi_event_data_size: size of edi_event_data
- * edi_freeing: event is being freed
- * edi_mutex: event data mutex
- * edi_hl: pointer to channel's handle
- * struct for this event.
- * edi_ro: read only flag
- */
- struct event_data_instance {
- SaEvtChannelHandleT edi_channel_handle;
- SaEvtEventPatternArrayT edi_patterns;
- SaUint8T edi_priority;
- SaTimeT edi_retention_time;
- SaNameT edi_pub_name;
- SaClmNodeIdT edi_pub_node;
- SaTimeT edi_pub_time;
- SaEvtEventIdT edi_event_id;
- void *edi_event_data;
- SaSizeT edi_event_data_size;
- int edi_freeing;
- pthread_mutex_t edi_mutex;
- struct handle_list *edi_hl;
- int edi_ro;
- };
- #define min(a,b) ((a) < (b) ? (a) : (b))
- static inline int is_valid_event_id(SaEvtEventIdT evt_id)
- {
- if (evt_id > 1000) {
- return 1;
- } else {
- return 0;
- }
- }
- /*
- * Clean up function for an evt instance (saEvtInitialize) handle
- * Not to be confused with event data.
- */
- static void evtHandleInstanceDestructor(void *instance)
- {
- struct event_instance *evti = instance;
- struct event_channel_instance *eci;
- struct handle_list *hl;
- struct list_head *l, *nxt;
- uint64_t handle;
- SaAisErrorT error;
- /*
- * Free up any channel data
- */
- for (l = evti->ei_channel_list.next;
- l != &evti->ei_channel_list; l = nxt) {
- nxt = l->next;
- hl = list_entry(l, struct handle_list, hl_entry);
- handle = hl->hl_handle;
- error = saHandleInstanceGet(&channel_handle_db, hl->hl_handle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- /*
- * already gone
- */
- continue;
- }
- saHandleDestroy(&channel_handle_db, handle);
- saHandleInstancePut(&channel_handle_db, handle);
- }
- }
- /*
- * Clean up function for an open channel handle
- */
- static void chanHandleInstanceDestructor(void *instance)
- {
- struct event_channel_instance *eci = instance;
- struct list_head *l, *nxt;
- struct handle_list *hl;
- uint64_t handle;
- if (eci->eci_hl) {
- list_del(&eci->eci_hl->hl_entry);
- free(eci->eci_hl);
- eci->eci_hl = 0;
- }
- /*
- * Free up any channel associated events
- */
- for (l = eci->eci_event_list.next; l != &eci->eci_event_list; l = nxt) {
- nxt = l->next;
- hl = list_entry(l, struct handle_list, hl_entry);
- handle = hl->hl_handle;
- saEvtEventFree(handle);
- }
- }
- /*
- * Clean up function for an event handle
- */
- static void eventHandleInstanceDestructor(void *instance)
- {
- struct event_data_instance *edi = instance;
- int i;
- if (edi->edi_hl) {
- list_del(&edi->edi_hl->hl_entry);
- free(edi->edi_hl);
- edi->edi_hl = 0;
- }
- if (edi->edi_patterns.patterns) {
- for (i = 0; i < edi->edi_patterns.patternsNumber; i++) {
- free(edi->edi_patterns.patterns[i].pattern);
- }
- free(edi->edi_patterns.patterns);
- }
- if (edi->edi_event_data) {
- free(edi->edi_event_data);
- }
- }
- static SaAisErrorT evt_recv_event(int fd, struct lib_event_data **msg)
- {
- SaAisErrorT error;
- mar_res_header_t hdr;
- void *data;
- error = saRecvRetry(fd, &hdr, sizeof(hdr));
- if (error != SA_AIS_OK) {
- goto msg_out;
- }
- *msg = malloc(hdr.size);
- if (!*msg) {
- error = SA_AIS_ERR_LIBRARY;
- goto msg_out;
- }
- data = (void *)((unsigned long)*msg) + sizeof(hdr);
- memcpy(*msg, &hdr, sizeof(hdr));
- if (hdr.size > sizeof(hdr)) {
- error = saRecvRetry(fd, data, hdr.size - sizeof(hdr));
- if (error != SA_AIS_OK) {
- goto msg_out;
- }
- }
- msg_out:
- return error;
- }
- /*
- * The saEvtInitialize() function initializes the Event Service for the
- * invoking process. A user of the Event Service must invoke this function
- * before it invokes any other function of the Event Service API. Each
- * initialization returns a different callback handle that the process
- * can use to communicate with that library instance.
- */
- SaAisErrorT
- saEvtInitialize(
- SaEvtHandleT *evtHandle,
- const SaEvtCallbacksT *callbacks,
- SaVersionT *version)
- {
- SaAisErrorT error = SA_AIS_OK;
- struct event_instance *evti;
- if (!version || !evtHandle) {
- error = SA_AIS_ERR_INVALID_PARAM;
- goto error_nofree;
- }
- /*
- * validate the requested version with what we support
- */
- error = saVersionVerify(&evt_version_database, version);
- if (error != SA_AIS_OK) {
- goto error_nofree;
- }
- /*
- * Allocate instance data, allocate unique handle for instance,
- * assign instance data to unique handle
- */
- error = saHandleCreate(&evt_instance_handle_db, sizeof(*evti),
- evtHandle);
- if (error != SA_AIS_OK) {
- goto error_nofree;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db, *evtHandle,
- (void*)&evti);
- if (error != SA_AIS_OK) {
- if (error == SA_AIS_ERR_BAD_HANDLE) {
- error = SA_AIS_ERR_LIBRARY;
- }
- goto error_handle_free;
- }
- memset(evti, 0, sizeof(*evti));
-
- list_init(&evti->ei_channel_list);
- /*
- * Save the version so we can check with the event server
- * and see if it supports this version.
- */
- evti->ei_version = *version;
- /*
- * Set up communication with the event server
- */
- error = saServiceConnect(&evti->ei_response_fd,
- &evti->ei_dispatch_fd, EVT_SERVICE);
- if (error != SA_AIS_OK) {
- goto error_handle_put;
- }
- /*
- * The callback function is saved in the event instance for
- * saEvtDispatch() to use.
- */
- if (callbacks) {
- memcpy(&evti->ei_callback, callbacks,
- sizeof(evti->ei_callback));
- }
- pthread_mutex_init(&evti->ei_dispatch_mutex, NULL);
- pthread_mutex_init(&evti->ei_response_mutex, NULL);
- saHandleInstancePut(&evt_instance_handle_db, *evtHandle);
- return SA_AIS_OK;
- error_handle_put:
- saHandleInstancePut(&evt_instance_handle_db, *evtHandle);
- error_handle_free:
- (void)saHandleDestroy(&evt_instance_handle_db, *evtHandle);
- error_nofree:
- return error;
- }
- /*
- * The saEvtSelectionObjectGet() function returns the operating system
- * handle selectionObject, associated with the handle evtHandle, allowing
- * the invoking process to ascertain when callbacks are pending. This
- * function allows a process to avoid repeated invoking saEvtDispatch() to
- * see if there is a new event, thus, needlessly consuming CPU time. In a
- * POSIX environment the system handle could be a file descriptor that is
- * used with the poll() or select() system calls to detect incoming callbacks.
- */
- SaAisErrorT
- saEvtSelectionObjectGet(
- SaEvtHandleT evtHandle,
- SaSelectionObjectT *selectionObject)
- {
- struct event_instance *evti;
- SaAisErrorT error;
- if (!selectionObject) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
- (void *)&evti);
- if (error != SA_AIS_OK) {
- return error;
- }
- *selectionObject = evti->ei_dispatch_fd;
- saHandleInstancePut(&evt_instance_handle_db, evtHandle);
- return SA_AIS_OK;
- }
- /*
- * Alocate an event data structure and associated handle to be
- * used to supply event data to a call back function.
- */
- static SaAisErrorT make_event(SaEvtEventHandleT *event_handle,
- struct lib_event_data *evt)
- {
- struct event_data_instance *edi;
- struct event_channel_instance *eci;
- SaEvtEventPatternT *pat;
- SaUint8T *str;
- SaAisErrorT error;
- struct handle_list *hl;
- int i;
- error = saHandleCreate(&event_handle_db, sizeof(*edi),
- event_handle);
- if (error != SA_AIS_OK) {
- if (error == SA_AIS_ERR_NO_MEMORY) {
- error = SA_AIS_ERR_LIBRARY;
- }
- goto make_evt_done;
- }
- error = saHandleInstanceGet(&event_handle_db, *event_handle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- saHandleDestroy(&event_handle_db, *event_handle);
- goto make_evt_done;
- }
- error = saHandleInstanceGet(&channel_handle_db,
- evt->led_lib_channel_handle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- saHandleDestroy(&event_handle_db, *event_handle);
- goto make_evt_done_put;
- }
- pthread_mutex_init(&edi->edi_mutex, NULL);
- edi->edi_ro = 1;
- edi->edi_freeing = 0;
- edi->edi_channel_handle = evt->led_lib_channel_handle;
- edi->edi_priority = evt->led_priority;
- edi->edi_retention_time = evt->led_retention_time;
- edi->edi_pub_node = evt->led_publisher_node_id;
- edi->edi_pub_time = evt->led_publish_time;
- edi->edi_event_data_size = evt->led_user_data_size;
- edi->edi_event_id = evt->led_event_id;
- edi->edi_pub_name = evt->led_publisher_name;
- if (edi->edi_event_data_size) {
- edi->edi_event_data = malloc(edi->edi_event_data_size);
- if (!edi->edi_event_data) {
- saHandleDestroy(&event_handle_db, *event_handle);
- /*
- * saEvtDispatch doesn't return SA_AIS_ERR_NO_MEMORY
- */
- error = SA_AIS_ERR_LIBRARY;
- goto make_evt_done_put2;
- }
- memcpy(edi->edi_event_data,
- evt->led_body + evt->led_user_data_offset,
- edi->edi_event_data_size);
- }
- /*
- * Move the pattern bits into the SaEvtEventPatternArrayT
- */
- edi->edi_patterns.patternsNumber = evt->led_patterns_number;
- edi->edi_patterns.allocatedNumber = evt->led_patterns_number;
- edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
- edi->edi_patterns.patternsNumber);
- if (!edi->edi_patterns.patterns) {
- /*
- * The destructor will take care of freeing event data already
- * allocated.
- */
- edi->edi_patterns.patternsNumber = 0;
- saHandleDestroy(&event_handle_db, *event_handle);
- /*
- * saEvtDispatch doesn't return SA_AIS_ERR_NO_MEMORY
- */
- error = SA_AIS_ERR_LIBRARY;
- goto make_evt_done_put2;
- }
- memset(edi->edi_patterns.patterns, 0, sizeof(SaEvtEventPatternT) *
- edi->edi_patterns.patternsNumber);
- pat = (SaEvtEventPatternT *)evt->led_body;
- str = evt->led_body + sizeof(SaEvtEventPatternT) *
- edi->edi_patterns.patternsNumber;
- for (i = 0; i < evt->led_patterns_number; i++) {
- edi->edi_patterns.patterns[i].patternSize = pat->patternSize;
- edi->edi_patterns.patterns[i].allocatedSize = pat->patternSize;
- edi->edi_patterns.patterns[i].pattern = malloc(pat->patternSize);
- if (!edi->edi_patterns.patterns[i].pattern) {
- DPRINT (("make_event: couldn't alloc %llu bytes\n",
- (unsigned long long)pat->patternSize));
- saHandleDestroy(&event_handle_db, *event_handle);
- error = SA_AIS_ERR_LIBRARY;
- goto make_evt_done_put2;
- }
- memcpy(edi->edi_patterns.patterns[i].pattern,
- str, pat->patternSize);
- str += pat->patternSize;
- pat++;
- }
- hl = malloc(sizeof(*hl));
- if (!hl) {
- saHandleDestroy(&event_handle_db, *event_handle);
- error = SA_AIS_ERR_LIBRARY;
- } else {
- edi->edi_hl = hl;
- hl->hl_handle = *event_handle;
- list_init(&hl->hl_entry);
- list_add(&hl->hl_entry, &eci->eci_event_list);
- }
- make_evt_done_put2:
- saHandleInstancePut (&channel_handle_db, evt->led_lib_channel_handle);
- make_evt_done_put:
- saHandleInstancePut (&event_handle_db, *event_handle);
- make_evt_done:
- return error;
- }
- /*
- * The saEvtDispatch() function invokes, in the context of the calling
- * thread, one or all of the pending callbacks for the handle evtHandle.
- */
- SaAisErrorT
- saEvtDispatch(
- SaEvtHandleT evtHandle,
- SaDispatchFlagsT dispatchFlags)
- {
- struct pollfd ufds;
- int timeout = -1;
- SaAisErrorT error;
- int dispatch_avail;
- struct event_instance *evti;
- SaEvtEventHandleT event_handle;
- SaEvtCallbacksT callbacks;
- int ignore_dispatch = 0;
- int cont = 1; /* always continue do loop except when set to 0 */
- int poll_fd;
- struct res_overlay dispatch_data;
- struct lib_event_data *evt = 0;
- struct res_evt_event_data res;
- if (dispatchFlags < SA_DISPATCH_ONE ||
- dispatchFlags > SA_DISPATCH_BLOCKING) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
- (void *)&evti);
- if (error != SA_AIS_OK) {
- return error;
- }
- /*
- * Timeout instantly for SA_DISPATCH_ALL
- */
- if (dispatchFlags == SA_DISPATCH_ALL || dispatchFlags == SA_DISPATCH_ONE) {
- timeout = 0;
- }
- do {
- poll_fd = evti->ei_dispatch_fd;
- ufds.fd = poll_fd;
- ufds.events = POLLIN;
- ufds.revents = 0;
- error = saPollRetry(&ufds, 1, timeout);
- if (error != SA_AIS_OK) {
- goto dispatch_put;
- }
- pthread_mutex_lock(&evti->ei_dispatch_mutex);
- /*
- * Handle has been finalized in another thread
- */
- if (evti->ei_finalize == 1) {
- error = SA_AIS_OK;
- goto dispatch_unlock;
- }
- /*
- * If we know that we have an event waiting, we can skip the
- * polling and just ask for it.
- */
- if (!evti->ei_data_available) {
- /*
- * Check the poll data in case the fd status has changed
- * since taking the lock
- */
- error = saPollRetry(&ufds, 1, 0);
- if (error != SA_AIS_OK) {
- goto dispatch_unlock;
- }
- if ((ufds.revents & (POLLERR|POLLHUP|POLLNVAL)) != 0) {
- error = SA_AIS_ERR_BAD_HANDLE;
- goto dispatch_unlock;
- }
- dispatch_avail = ufds.revents & POLLIN;
- if (dispatch_avail == 0 &&
- (dispatchFlags == SA_DISPATCH_ALL ||
- dispatchFlags == SA_DISPATCH_ONE)) {
- pthread_mutex_unlock(&evti->ei_dispatch_mutex);
- break; /* exit do while cont is 1 loop */
- } else if (dispatch_avail == 0) {
- pthread_mutex_unlock(&evti->ei_dispatch_mutex);
- continue; /* next poll */
- }
- if (ufds.revents & POLLIN) {
- error = saRecvRetry (evti->ei_dispatch_fd, &dispatch_data.header,
- sizeof (mar_res_header_t));
- if (error != SA_AIS_OK) {
- goto dispatch_unlock;
- }
- if (dispatch_data.header.size > sizeof (mar_res_header_t)) {
- error = saRecvRetry (evti->ei_dispatch_fd, &dispatch_data.data,
- dispatch_data.header.size - sizeof (mar_res_header_t));
- if (error != SA_AIS_OK) {
- goto dispatch_unlock;
- }
- }
- } else {
- pthread_mutex_unlock(&evti->ei_dispatch_mutex);
- continue;
- }
- } else {
- /*
- * We know that we have an event available from before.
- * Fake up a header message and the switch statement will
- * take care of the rest.
- */
- dispatch_data.header.id = MESSAGE_RES_EVT_AVAILABLE;
- }
- /*
- * Make copy of callbacks, message data, unlock instance,
- * and call callback. A risk of this dispatch method is that
- * the callback routines may operate at the same time that
- * EvtFinalize has been called in another thread.
- */
- memcpy(&callbacks, &evti->ei_callback, sizeof(evti->ei_callback));
- pthread_mutex_unlock(&evti->ei_dispatch_mutex);
- /*
- * Dispatch incoming response
- */
- switch (dispatch_data.header.id) {
- case MESSAGE_RES_EVT_AVAILABLE:
- evti->ei_data_available = 0;
- /*
- * There are events available. Send a request for one and then
- * dispatch it.
- */
- res.evd_head.id = MESSAGE_REQ_EVT_EVENT_DATA;
- res.evd_head.size = sizeof(res);
-
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendRetry(evti->ei_response_fd, &res, sizeof(res));
-
- if (error != SA_AIS_OK) {
- DPRINT (("MESSAGE_RES_EVT_AVAILABLE: send failed: %d\n", error));
- pthread_mutex_unlock(&evti->ei_response_mutex);
- break;
- }
- error = evt_recv_event(evti->ei_response_fd, &evt);
- pthread_mutex_unlock(&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- DPRINT (("MESSAGE_RES_EVT_AVAILABLE: receive failed: %d\n", error));
- break;
- }
- /*
- * No data available. This is OK, another thread may have
- * grabbed it.
- */
- if (evt->led_head.error == SA_AIS_ERR_NOT_EXIST) {
- error = SA_AIS_OK;
- break;
- }
- if (evt->led_head.error != SA_AIS_OK) {
- error = evt->led_head.error;
- /*
- * If we get a try again response, we've lost the poll event
- * so we have a data available flag so that we know that there
- * really is an event waiting the next time dispatch gets
- * called.
- */
- if (error == SA_AIS_ERR_TRY_AGAIN) {
- evti->ei_data_available = 1;
- } else {
- DPRINT (("MESSAGE_RES_EVT_AVAILABLE: Error returned: %d\n", error));
- }
- break;
- }
- error = make_event(&event_handle, evt);
- if (error != SA_AIS_OK) {
- break;
- }
- /*
- * Only call if there was a function registered
- */
- if (callbacks.saEvtEventDeliverCallback) {
- callbacks.saEvtEventDeliverCallback(evt->led_sub_id,
- event_handle, evt->led_user_data_size);
- }
- break;
- case MESSAGE_RES_EVT_CHAN_OPEN_CALLBACK:
- {
- struct res_evt_open_chan_async *resa =
- (struct res_evt_open_chan_async *)&dispatch_data;
- struct event_channel_instance *eci;
- /*
- * Check for errors. If there are none, then
- * look up the local channel via the handle that we
- * got from the callback request. All we need to do
- * is place in the handle from the server side and then
- * we can call the callback.
- */
- error = resa->ica_head.error;
- if (error == SA_AIS_OK) {
- error = saHandleInstanceGet(&channel_handle_db,
- resa->ica_c_handle, (void*)&eci);
- if (error == SA_AIS_OK) {
- eci->eci_svr_channel_handle = resa->ica_channel_handle;
- saHandleInstancePut (&channel_handle_db,
- resa->ica_c_handle);
- }
- }
- /*
- * Only call if there was a function registered
- */
- if (callbacks.saEvtChannelOpenCallback) {
- callbacks.saEvtChannelOpenCallback(resa->ica_invocation,
- resa->ica_c_handle, error);
- }
- }
- break;
- default:
- DPRINT (("Dispatch: Bad message type 0x%x\n", dispatch_data.header.id));
- error = SA_AIS_ERR_LIBRARY;
- goto dispatch_put;
- }
- /*
- * If empty is zero it means the we got the
- * message from the queue and we are responsible
- * for freeing it.
- */
- if (evt) {
- free(evt);
- evt = 0;
- }
- /*
- * Determine if more messages should be processed
- */
- switch (dispatchFlags) {
- case SA_DISPATCH_ONE:
- if (ignore_dispatch) {
- ignore_dispatch = 0;
- } else {
- cont = 0;
- }
- break;
- case SA_DISPATCH_ALL:
- if (ignore_dispatch) {
- ignore_dispatch = 0;
- }
- break;
- case SA_DISPATCH_BLOCKING:
- break;
- }
- } while (cont);
- goto dispatch_put;
- dispatch_unlock:
- pthread_mutex_unlock(&evti->ei_dispatch_mutex);
- dispatch_put:
- saHandleInstancePut(&evt_instance_handle_db, evtHandle);
- return error;
- }
- /*
- * The saEvtFinalize() function closes the association, represented by the
- * evtHandle parameter, between the process and the Event Service. It may
- * free up resources.
- * This function cannot be invoked before the process has invoked the
- * corresponding saEvtInitialize() function for the Event Service. After
- * this function is invoked, the selection object is no longer valid.
- * Moreover, the Event Service is unavailable for further use unless it is
- * reinitialized using the saEvtInitialize() function.
- */
- SaAisErrorT
- saEvtFinalize(SaEvtHandleT evtHandle)
- {
- struct event_instance *evti;
- SaAisErrorT error;
- error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
- (void *)&evti);
- if (error != SA_AIS_OK) {
- return error;
- }
- pthread_mutex_lock(&evti->ei_response_mutex);
- /*
- * Another thread has already started finalizing
- */
- if (evti->ei_finalize) {
- pthread_mutex_unlock(&evti->ei_response_mutex);
- saHandleInstancePut(&evt_instance_handle_db, evtHandle);
- return SA_AIS_ERR_BAD_HANDLE;
- }
- evti->ei_finalize = 1;
- pthread_mutex_unlock(&evti->ei_response_mutex);
- saHandleDestroy(&evt_instance_handle_db, evtHandle);
- /*
- * Disconnect from the server
- */
- if (evti->ei_response_fd != -1) {
- shutdown(evti->ei_response_fd, 0);
- close(evti->ei_response_fd);
- }
- if (evti->ei_dispatch_fd != -1) {
- shutdown(evti->ei_dispatch_fd, 0);
- close(evti->ei_dispatch_fd);
- }
- saHandleInstancePut(&evt_instance_handle_db, evtHandle);
- return error;
- }
- /*
- * The saEvtChannelOpen() function creates a new event channel or open an
- * existing channel. The saEvtChannelOpen() function is a blocking operation
- * and returns a new event channel handle. An event channel may be opened
- * multiple times by the same or different processes for publishing, and
- * subscribing to, events. If a process opens an event channel multiple
- * times, it is possible to receive the same event multiple times. However,
- * a process shall never receive an event more than once on a particular
- * event channel handle. If a process opens a channel twice and an event is
- * matched on both open channels, the Event Service performs two
- * callbacks -- one for each opened channel.
- */
- SaAisErrorT
- saEvtChannelOpen(
- SaEvtHandleT evtHandle,
- const SaNameT *channelName,
- SaEvtChannelOpenFlagsT channelOpenFlags,
- SaTimeT timeout,
- SaEvtChannelHandleT *channelHandle)
- {
- struct event_instance *evti;
- struct req_evt_channel_open req;
- struct res_evt_channel_open res;
- struct event_channel_instance *eci;
- struct handle_list *hl;
- SaAisErrorT error;
- struct iovec iov;
- if (!channelHandle || !channelName) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- if ((channelOpenFlags & ~(SA_EVT_CHANNEL_CREATE|SA_EVT_CHANNEL_PUBLISHER|
- SA_EVT_CHANNEL_SUBSCRIBER)) != 0) {
- return SA_AIS_ERR_BAD_FLAGS;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
- (void*)&evti);
-
- if (error != SA_AIS_OK) {
- goto chan_open_done;
- }
- /*
- * create a handle for this open channel
- */
- error = saHandleCreate(&channel_handle_db, sizeof(*eci),
- channelHandle);
- if (error != SA_AIS_OK) {
- goto chan_open_put;
- }
- error = saHandleInstanceGet(&channel_handle_db, *channelHandle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- saHandleDestroy(&channel_handle_db, *channelHandle);
- goto chan_open_put;
- }
- list_init(&eci->eci_event_list);
- /*
- * Send the request to the server and wait for a response
- */
- req.ico_head.size = sizeof(req);
- req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL;
- req.ico_c_handle = *channelHandle;
- req.ico_timeout = timeout;
- req.ico_open_flag = channelOpenFlags;
- req.ico_channel_name = *channelName;
- iov.iov_base = &req;
- iov.iov_len = sizeof(req);
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
- &res, sizeof(res));
- pthread_mutex_unlock (&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- goto chan_open_free;
- }
- if (res.ico_head.id != MESSAGE_RES_EVT_OPEN_CHANNEL) {
- error = SA_AIS_ERR_LIBRARY;
- goto chan_open_free;
- }
- error = res.ico_head.error;
- if (error != SA_AIS_OK) {
- goto chan_open_free;
- }
- eci->eci_svr_channel_handle = res.ico_channel_handle;
- eci->eci_channel_name = *channelName;
- eci->eci_open_flags = channelOpenFlags;
- eci->eci_instance_handle = evtHandle;
- eci->eci_closing = 0;
- hl = malloc(sizeof(*hl));
- if (!hl) {
- error = SA_AIS_ERR_NO_MEMORY;
- goto chan_open_free;
- }
- eci->eci_hl = hl;
- hl->hl_handle = *channelHandle;
- list_init(&hl->hl_entry);
- list_add(&hl->hl_entry, &evti->ei_channel_list);
- pthread_mutex_init(&eci->eci_mutex, NULL);
- saHandleInstancePut (&evt_instance_handle_db, evtHandle);
- saHandleInstancePut (&channel_handle_db, *channelHandle);
- return SA_AIS_OK;
- chan_open_free:
- saHandleDestroy(&channel_handle_db, *channelHandle);
- saHandleInstancePut (&channel_handle_db, *channelHandle);
- chan_open_put:
- saHandleInstancePut (&evt_instance_handle_db, evtHandle);
- chan_open_done:
- return error;
- }
- /*
- * The saEvtChannelClose() function closes an event channel and frees
- * resources allocated for that event channel in the invoking process.
- */
- SaAisErrorT
- saEvtChannelClose(SaEvtChannelHandleT channelHandle)
- {
- SaAisErrorT error;
- struct event_instance *evti;
- struct event_channel_instance *eci;
- struct req_evt_channel_close req;
- struct res_evt_channel_close res;
- struct iovec iov;
- error = saHandleInstanceGet(&channel_handle_db, channelHandle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- goto chan_close_done;
- }
- /*
- * get the evt handle for the fd
- */
- error = saHandleInstanceGet(&evt_instance_handle_db,
- eci->eci_instance_handle, (void*)&evti);
- if (error != SA_AIS_OK) {
- goto chan_close_put1;
- }
- /*
- * Make sure that the channel isn't being closed elsewhere
- */
- pthread_mutex_lock(&eci->eci_mutex);
- if (eci->eci_closing) {
- pthread_mutex_unlock(&eci->eci_mutex);
- saHandleInstancePut(&channel_handle_db, channelHandle);
- return SA_AIS_ERR_BAD_HANDLE;
- }
- eci->eci_closing = 1;
- pthread_mutex_unlock(&eci->eci_mutex);
-
- /*
- * Send the request to the server and wait for a response
- */
- req.icc_head.size = sizeof(req);
- req.icc_head.id = MESSAGE_REQ_EVT_CLOSE_CHANNEL;
- req.icc_channel_handle = eci->eci_svr_channel_handle;
- iov.iov_base = &req;
- iov.iov_len = sizeof (req);
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply (evti->ei_response_fd, &iov, 1,
- &res, sizeof (res));
- pthread_mutex_unlock(&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- eci->eci_closing = 0;
- goto chan_close_put2;
- }
- if (res.icc_head.id != MESSAGE_RES_EVT_CLOSE_CHANNEL) {
- error = SA_AIS_ERR_LIBRARY;
- eci->eci_closing = 0;
- goto chan_close_put2;
- }
- error = res.icc_head.error;
- if (error == SA_AIS_ERR_TRY_AGAIN) {
- pthread_mutex_lock(&eci->eci_mutex);
- eci->eci_closing = 0;
- pthread_mutex_unlock(&eci->eci_mutex);
- goto chan_close_put2;
- }
- saHandleInstancePut(&evt_instance_handle_db,
- eci->eci_instance_handle);
- saHandleDestroy(&channel_handle_db, channelHandle);
- saHandleInstancePut(&channel_handle_db, channelHandle);
- return error;
- chan_close_put2:
- saHandleInstancePut(&evt_instance_handle_db,
- eci->eci_instance_handle);
- chan_close_put1:
- saHandleInstancePut(&channel_handle_db, channelHandle);
- chan_close_done:
- return error;
- }
- /*
- * The saEvtChannelOpenAsync() function creates a new event channel or open an
- * existing channel. The saEvtChannelOpenAsync() function is a non-blocking
- * operation. A new event channel handle is returned in the channel open
- * callback function (SaEvtChannelOpenCallbackT).
- */
- SaAisErrorT
- saEvtChannelOpenAsync(SaEvtHandleT evtHandle,
- SaInvocationT invocation,
- const SaNameT *channelName,
- SaEvtChannelOpenFlagsT channelOpenFlags)
- {
- struct event_instance *evti;
- struct req_evt_channel_open req;
- struct res_evt_channel_open res;
- struct event_channel_instance *eci;
- SaEvtChannelHandleT channel_handle;
- SaAisErrorT error;
- struct handle_list *hl;
- struct iovec iov;
- if (!channelName) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- if ((channelOpenFlags & ~(SA_EVT_CHANNEL_CREATE|SA_EVT_CHANNEL_PUBLISHER|
- SA_EVT_CHANNEL_SUBSCRIBER)) != 0) {
- return SA_AIS_ERR_BAD_FLAGS;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
- (void*)&evti);
-
- if (error != SA_AIS_OK) {
- goto chan_open_done;
- }
- /*
- * Make sure that an open channel callback has been
- * registered before allowing the open to continue.
- */
- if (!evti->ei_callback.saEvtChannelOpenCallback) {
- error = SA_AIS_ERR_INIT;
- goto chan_open_put;
- }
- /*
- * create a handle for this open channel
- */
- error = saHandleCreate(&channel_handle_db, sizeof(*eci),
- &channel_handle);
- if (error != SA_AIS_OK) {
- goto chan_open_put;
- }
- error = saHandleInstanceGet(&channel_handle_db, channel_handle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- saHandleDestroy(&channel_handle_db, channel_handle);
- goto chan_open_put;
- }
- list_init(&eci->eci_event_list);
- /*
- * Send the request to the server. The response isn't the open channel,
- * just an ack. The open channel will be returned when the channel open
- * callback is called.
- */
- req.ico_head.size = sizeof(req);
- req.ico_head.id = MESSAGE_REQ_EVT_OPEN_CHANNEL_ASYNC;
- req.ico_c_handle = channel_handle;
- req.ico_timeout = 0;
- req.ico_invocation = invocation;
- req.ico_open_flag = channelOpenFlags;
- req.ico_channel_name = *channelName;
- iov.iov_base = &req;
- iov.iov_len = sizeof(req);
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply (evti->ei_response_fd, &iov, 1,
- &res, sizeof (res));
- pthread_mutex_unlock(&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- goto chan_open_free;
- }
- if (res.ico_head.id != MESSAGE_RES_EVT_OPEN_CHANNEL) {
- error = SA_AIS_ERR_LIBRARY;
- goto chan_open_free;
- }
- error = res.ico_head.error;
- if (error != SA_AIS_OK) {
- goto chan_open_free;
- }
- eci->eci_svr_channel_handle = 0; /* filled in by callback */
- eci->eci_channel_name = *channelName;
- eci->eci_open_flags = channelOpenFlags;
- eci->eci_instance_handle = evtHandle;
- eci->eci_closing = 0;
- list_init(&eci->eci_event_list);
- hl = malloc(sizeof(*hl));
- if (!hl) {
- error = SA_AIS_ERR_NO_MEMORY;
- goto chan_open_free;
- }
- eci->eci_hl = hl;
- hl->hl_handle = channel_handle;
- list_init(&hl->hl_entry);
- list_add(&hl->hl_entry, &evti->ei_channel_list);
- pthread_mutex_init(&eci->eci_mutex, NULL);
- saHandleInstancePut (&evt_instance_handle_db, evtHandle);
- saHandleInstancePut (&channel_handle_db, channel_handle);
- return SA_AIS_OK;
- chan_open_free:
- saHandleDestroy(&channel_handle_db, channel_handle);
- saHandleInstancePut (&channel_handle_db, channel_handle);
- chan_open_put:
- saHandleInstancePut (&evt_instance_handle_db, evtHandle);
- chan_open_done:
- return error;
- }
- /*
- * The SaEvtChannelUnlink function deletes an existing event channel
- * from the cluster.
- *
- * After completion of the invocation:
- * - An open of the channel name without a create will fail.
- * - The channel remains available to any already opened instances.
- * - If an open/create is performed on this channel name a new instance
- * is created.
- * - The ulinked channel's resources will be deleted when the last open
- * instance is closed.
- *
- * Note that an event channel is only deleted from the cluster
- * namespace when saEvtChannelUnlink() is invoked on it. The deletion
- * of an event channel frees all resources allocated by the Event
- * Service for it, such as published events with non-zero retention
- * time.
- */
- SaAisErrorT
- saEvtChannelUnlink(
- SaEvtHandleT evtHandle,
- const SaNameT *channelName)
- {
- struct event_instance *evti;
- struct req_evt_channel_unlink req;
- struct res_evt_channel_unlink res;
- struct iovec iov;
- SaAisErrorT error;
- if (!channelName) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db, evtHandle,
- (void*)&evti);
-
- if (error != SA_AIS_OK) {
- goto chan_unlink_done;
- }
- /*
- * Send the request to the server and wait for a response
- */
- req.iuc_head.size = sizeof(req);
- req.iuc_head.id = MESSAGE_REQ_EVT_UNLINK_CHANNEL;
- req.iuc_channel_name = *channelName;
- iov.iov_base = &req;
- iov.iov_len = sizeof(req);
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply (evti->ei_response_fd, &iov, 1,
- &res, sizeof (res));
- pthread_mutex_unlock(&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- goto chan_unlink_put;
- }
- if (res.iuc_head.id != MESSAGE_RES_EVT_UNLINK_CHANNEL) {
- error = SA_AIS_ERR_LIBRARY;
- goto chan_unlink_put;
- }
- error = res.iuc_head.error;
- chan_unlink_put:
- saHandleInstancePut (&evt_instance_handle_db, evtHandle);
- chan_unlink_done:
- return error;
- }
- /*
- * The saEvtEventAllocate() function allocates memory for the event, but not
- * for the eventHandle, and initializes all event attributes to default values.
- * The event allocated by saEvtEventAllocate() is freed by invoking
- * saEvtEventFree().
- * The memory associated with the eventHandle is not deallocated by the
- * saEvtEventAllocate() function or the saEvtEventFree() function. It is the
- * responsibility of the invoking process to deallocate the memory for the
- * eventHandle when the process has published the event and has freed the
- * event by invoking saEvtEventFree().
- */
- SaAisErrorT
- saEvtEventAllocate(
- const SaEvtChannelHandleT channelHandle,
- SaEvtEventHandleT *eventHandle)
- {
- SaAisErrorT error;
- struct event_data_instance *edi;
- struct event_instance *evti;
- struct event_channel_instance *eci;
- struct handle_list *hl;
- if (!eventHandle) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&channel_handle_db, channelHandle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- goto alloc_done;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db,
- eci->eci_instance_handle, (void*)&evti);
- if (error != SA_AIS_OK) {
- goto alloc_put1;
- }
- error = saHandleCreate(&event_handle_db, sizeof(*edi),
- eventHandle);
- if (error != SA_AIS_OK) {
- goto alloc_put2;
- }
- error = saHandleInstanceGet(&event_handle_db, *eventHandle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- saHandleDestroy(&event_handle_db, *eventHandle);
- goto alloc_put2;
- }
- pthread_mutex_init(&edi->edi_mutex, NULL);
- edi->edi_ro = 0;
- edi->edi_freeing = 0;
- edi->edi_channel_handle = channelHandle;
- edi->edi_pub_node = evti->ei_node_id;
- edi->edi_priority = SA_EVT_LOWEST_PRIORITY;
- edi->edi_event_id = SA_EVT_EVENTID_NONE;
- edi->edi_pub_time = SA_TIME_UNKNOWN;
- edi->edi_retention_time = 0;
- hl = malloc(sizeof(*hl));
- if (!hl) {
- saHandleDestroy(&event_handle_db, *eventHandle);
- error = SA_AIS_ERR_NO_MEMORY;
- goto alloc_put2;
- }
- edi->edi_hl = hl;
- hl->hl_handle = *eventHandle;
- list_init(&hl->hl_entry);
- list_add(&hl->hl_entry, &eci->eci_event_list);
- saHandleInstancePut (&event_handle_db, *eventHandle);
- alloc_put2:
- saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
- alloc_put1:
- saHandleInstancePut (&channel_handle_db, channelHandle);
- alloc_done:
- return error;
- }
- /*
- * The saEvtEventFree() function gives the Event Service premission to
- * deallocate the memory that contains the attributes of the event that is
- * associated with eventHandle. The function is used to free events allocated
- * by saEvtEventAllocate() and by saEvtEventDeliverCallback().
- */
- SaAisErrorT
- saEvtEventFree(SaEvtEventHandleT eventHandle)
- {
- SaAisErrorT error;
- struct event_data_instance *edi;
- error = saHandleInstanceGet(&event_handle_db, eventHandle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- goto evt_free_done;
- }
- /*
- * Make sure that the event isn't being freed elsewhere
- */
- pthread_mutex_lock(&edi->edi_mutex);
- if (edi->edi_freeing) {
- pthread_mutex_unlock(&edi->edi_mutex);
- saHandleInstancePut(&event_handle_db, eventHandle);
- return SA_AIS_ERR_BAD_HANDLE;
- }
- edi->edi_freeing = 1;
- pthread_mutex_unlock(&edi->edi_mutex);
- saHandleDestroy(&event_handle_db, eventHandle);
- saHandleInstancePut(&event_handle_db, eventHandle);
- evt_free_done:
- return error;
- }
- /*
- * This function may be used to assign writeable event attributes. It takes
- * as arguments an event handle eventHandle and the attribute to be set in
- * the event structure of the event with that handle. Note: The only
- * attributes that a process can set are the patternArray, priority,
- * retentionTime and publisherName attributes.
- */
- SaAisErrorT
- saEvtEventAttributesSet(
- const SaEvtEventHandleT eventHandle,
- const SaEvtEventPatternArrayT *patternArray,
- SaEvtEventPriorityT priority,
- SaTimeT retentionTime,
- const SaNameT *publisherName)
- {
- SaEvtEventPatternT *oldpatterns;
- SaSizeT oldnumber;
- SaAisErrorT error;
- struct event_data_instance *edi;
- int i;
- if (priority > SA_EVT_LOWEST_PRIORITY) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&event_handle_db, eventHandle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- goto attr_set_done;
- }
- pthread_mutex_lock(&edi->edi_mutex);
- /*
- * Cannot modify an event returned via callback.
- */
- if (edi->edi_ro) {
- error = SA_AIS_ERR_ACCESS;
- goto attr_set_unlock;
- }
- edi->edi_priority = priority;
- edi->edi_retention_time = retentionTime;
- if (publisherName) {
- edi->edi_pub_name = *publisherName;
- }
- if (!patternArray) {
- goto attr_set_unlock;
- }
- oldpatterns = edi->edi_patterns.patterns;
- oldnumber = edi->edi_patterns.patternsNumber;
- edi->edi_patterns.patterns = 0;
- edi->edi_patterns.patterns = malloc(sizeof(SaEvtEventPatternT) *
- patternArray->patternsNumber);
- if (!edi->edi_patterns.patterns) {
- error = SA_AIS_ERR_NO_MEMORY;
- goto attr_set_done_reset;
- }
- edi->edi_patterns.patternsNumber = patternArray->patternsNumber;
- edi->edi_patterns.allocatedNumber = patternArray->patternsNumber;
- /*
- * copy the patterns from the caller. allocating memory for
- * of all the strings.
- */
- for (i = 0; i < patternArray->patternsNumber; i++) {
- edi->edi_patterns.patterns[i].pattern =
- malloc(patternArray->patterns[i].patternSize);
- if (!edi->edi_patterns.patterns[i].pattern) {
- int j;
- for (j = 0; j < i; j++) {
- free(edi->edi_patterns.patterns[j].pattern);
- }
- free(edi->edi_patterns.patterns);
- error = SA_AIS_ERR_NO_MEMORY;
- goto attr_set_done_reset;
- }
- memcpy(edi->edi_patterns.patterns[i].pattern,
- patternArray->patterns[i].pattern,
- patternArray->patterns[i].patternSize);
- edi->edi_patterns.patterns[i].patternSize =
- patternArray->patterns[i].patternSize;
- edi->edi_patterns.patterns[i].allocatedSize =
- patternArray->patterns[i].patternSize;
- }
- /*
- * free up the old pattern memory
- */
- if (oldpatterns) {
- for (i = 0; i < oldnumber; i++) {
- if (oldpatterns[i].pattern) {
- free(oldpatterns[i].pattern);
- }
- }
- free (oldpatterns);
- }
- goto attr_set_unlock;
- attr_set_done_reset:
- edi->edi_patterns.patterns = oldpatterns;
- edi->edi_patterns.patternsNumber = oldnumber;
- attr_set_unlock:
- pthread_mutex_unlock(&edi->edi_mutex);
- saHandleInstancePut(&event_handle_db, eventHandle);
- attr_set_done:
- return error;
- }
- /*
- * This function takes as parameters an event handle eventHandle and the
- * attributes of the event with that handle. The function retrieves the
- * value of the attributes for the event and stores them at the address
- * provided for the out parameters.
- * It is the responsibility of the invoking process to allocate memory for
- * the out parameters before it invokes this function. The Event Service
- * assigns the out values into that memory. For each of the out, or in/out,
- * parameters, if the invoking process provides a NULL reference, the
- * Availability Management Framework does not return the out value.
- * Similarly, it is the responsibility of the invoking process to allocate
- * memory for the patternArray.
- */
- SaAisErrorT
- saEvtEventAttributesGet(
- SaEvtEventHandleT eventHandle,
- SaEvtEventPatternArrayT *patternArray,
- SaEvtEventPriorityT *priority,
- SaTimeT *retentionTime,
- SaNameT *publisherName,
- SaTimeT *publishTime,
- SaEvtEventIdT *eventId)
- {
- SaAisErrorT error;
- struct event_data_instance *edi;
- SaSizeT npats;
- int i;
- error = saHandleInstanceGet(&event_handle_db, eventHandle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- goto attr_get_done;
- }
- pthread_mutex_lock(&edi->edi_mutex);
- /*
- * Go through the args and send back information if the pointer
- * isn't NULL
- */
- if (eventId) {
- *eventId = edi->edi_event_id;
- }
- if (publishTime) {
- *publishTime = edi->edi_pub_time;
- }
- if (publisherName) {
- *publisherName = edi->edi_pub_name;
- }
- if (retentionTime) {
- *retentionTime = edi->edi_retention_time;
- }
- if (priority) {
- *priority = edi->edi_priority;
- }
- if (!patternArray) {
- goto attr_get_unlock;
- }
- /*
- * The spec says that if the called passes in a NULL patterns array,
- * then we allocate the required data and the caller is responsible
- * for dealocating later. Otherwise, we copy to pre-allocated space.
- * If there are more patterns than allcated space, we set the return
- * code to SA_AIS_ERR_NO_SPACE and copy as much as will fit. We will
- * return the total number of patterns available in the patternsNumber
- * regardless of how much was allocated.
- *
- */
- if (patternArray->patterns == NULL) {
- npats = edi->edi_patterns.patternsNumber;
- patternArray->allocatedNumber = edi->edi_patterns.patternsNumber;
- patternArray->patternsNumber = edi->edi_patterns.patternsNumber;
- patternArray->patterns = malloc(sizeof(*patternArray->patterns) *
- edi->edi_patterns.patternsNumber);
- if (!patternArray->patterns) {
- error = SA_AIS_ERR_LIBRARY;
- goto attr_get_unlock;
- }
- for (i = 0; i < edi->edi_patterns.patternsNumber; i++) {
- patternArray->patterns[i].allocatedSize =
- edi->edi_patterns.patterns[i].allocatedSize;
- patternArray->patterns[i].patternSize =
- edi->edi_patterns.patterns[i].patternSize;
- patternArray->patterns[i].pattern =
- malloc(edi->edi_patterns.patterns[i].patternSize);
- if (!patternArray->patterns[i].pattern) {
- int j;
- /*
- * back out previous mallocs
- */
- for (j = 0; j < i; j++) {
- free(patternArray->patterns[j].pattern);
- }
- free(patternArray->patterns);
- /*
- * saEvtEventAttributesGet doesn't return
- * SA_AIS_ERR_NO_MEMORY
- */
- error = SA_AIS_ERR_LIBRARY;
- goto attr_get_unlock;
- }
- }
- } else {
- if (patternArray->allocatedNumber < edi->edi_patterns.allocatedNumber) {
- error = SA_AIS_ERR_NO_SPACE;
- npats = patternArray->allocatedNumber;
- } else {
- npats = edi->edi_patterns.patternsNumber;
- }
- }
- patternArray->patternsNumber = edi->edi_patterns.patternsNumber;
- /*
- * copy the patterns to the callers structure. If we have pre-allocated
- * data, the patterns may not fit in the supplied space. In that case we
- * return NO_SPACE.
- */
- for (i = 0; i < npats; i++) {
- memcpy(patternArray->patterns[i].pattern,
- edi->edi_patterns.patterns[i].pattern,
- min(patternArray->patterns[i].allocatedSize,
- edi->edi_patterns.patterns[i].patternSize));
- if (patternArray->patterns[i].allocatedSize <
- edi->edi_patterns.patterns[i].patternSize) {
- error = SA_AIS_ERR_NO_SPACE;
- }
- patternArray->patterns[i].patternSize =
- edi->edi_patterns.patterns[i].patternSize;
- }
- attr_get_unlock:
- pthread_mutex_unlock(&edi->edi_mutex);
- saHandleInstancePut(&event_handle_db, eventHandle);
- attr_get_done:
- return error;
- }
- /*
- * The saEvtEventDataGet() function allows a process to retrieve the data
- * associated with an event previously delivered by
- * saEvtEventDeliverCallback().
- */
- SaAisErrorT
- saEvtEventDataGet(
- const SaEvtEventHandleT eventHandle,
- void *eventData,
- SaSizeT *eventDataSize)
- {
- SaAisErrorT error = SA_AIS_ERR_INVALID_PARAM;
- struct event_data_instance *edi;
- SaSizeT xfsize;
- if (!eventDataSize) {
- goto data_get_done;
- }
- error = saHandleInstanceGet(&event_handle_db, eventHandle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- goto data_get_done;
- }
- pthread_mutex_lock(&edi->edi_mutex);
- /*
- * If no buffer was supplied, then just tell the caller
- * how large a buffer is needed.
- */
- if (!eventData) {
- error = SA_AIS_ERR_NO_SPACE;
- *eventDataSize = edi->edi_event_data_size;
- goto unlock_put;
- }
- /*
- * Can't get data from an event that wasn't
- * a delivered event.
- */
- if (!edi->edi_ro) {
- error = SA_AIS_ERR_BAD_HANDLE;
- goto unlock_put;
- }
- if (edi->edi_event_data && edi->edi_event_data_size) {
- xfsize = min(*eventDataSize, edi->edi_event_data_size);
- if (*eventDataSize < edi->edi_event_data_size) {
- error = SA_AIS_ERR_NO_SPACE;
- }
- *eventDataSize = edi->edi_event_data_size;
- memcpy(eventData, edi->edi_event_data, xfsize);
- } else {
- *eventDataSize = 0;
- }
- unlock_put:
- pthread_mutex_unlock(&edi->edi_mutex);
- saHandleInstancePut(&event_handle_db, eventHandle);
- data_get_done:
- return error;
- }
- /*
- * Calculate the size in bytes for patterns
- */
- static size_t patt_size(const SaEvtEventPatternArrayT *patterns)
- {
- int i;
- size_t size = sizeof(SaEvtEventPatternArrayT);
- for (i = 0; i < patterns->patternsNumber; i++) {
- size += sizeof(SaEvtEventPatternT);
- size += patterns->patterns[i].patternSize;
- }
- return size;
- }
- /*
- * copy patterns to a form for sending to the server
- */
- static uint32_t aispatt_to_evt_patt(const SaEvtEventPatternArrayT *patterns,
- void *data)
- {
- int i;
- SaEvtEventPatternT *pats = data;
- SaUint8T *str = (SaUint8T *)pats +
- (patterns->patternsNumber * sizeof(*pats));
- /*
- * Pointers are replaced with offsets into the data array. These
- * will be later converted back into pointers when received as events.
- */
- for (i = 0; i < patterns->patternsNumber; i++) {
- memcpy(str, patterns->patterns[i].pattern,
- patterns->patterns[i].patternSize);
- pats->patternSize = patterns->patterns[i].patternSize;
- pats->pattern = (SaUint8T *)((void *)str - data);
- str += patterns->patterns[i].patternSize;
- pats++;
- }
- return patterns->patternsNumber;
- }
- /*
- * Calculate the size in bytes for filters
- */
- static size_t filt_size(const SaEvtEventFilterArrayT *filters)
- {
- int i;
- size_t size = sizeof(SaEvtEventFilterArrayT);
- for (i = 0; i < filters->filtersNumber; i++) {
- size += sizeof(SaEvtEventFilterT);
- size += filters->filters[i].filter.patternSize;
- }
- return size;
- }
- /*
- * Convert the Sa filters to a form that can be sent over the network
- * i.e. replace pointers with offsets. The pointers will be reconstituted
- * by the receiver.
- */
- static uint32_t aisfilt_to_evt_filt(const SaEvtEventFilterArrayT *filters,
- void *data)
- {
- int i;
- SaEvtEventFilterArrayT *filta = data;
- SaEvtEventFilterT *filts = data + sizeof(SaEvtEventFilterArrayT);
- SaUint8T *str = (SaUint8T *)filts +
- (filters->filtersNumber * sizeof(*filts));
- /*
- * Pointers are replaced with offsets into the data array. These
- * will be later converted back into pointers by the evt server.
- */
- filta->filters = (SaEvtEventFilterT *)((void *)filts - data);
- filta->filtersNumber = filters->filtersNumber;
- for (i = 0; i < filters->filtersNumber; i++) {
- filts->filterType = filters->filters[i].filterType;
- filts->filter.patternSize =
- filters->filters[i].filter.patternSize;
- memcpy(str,
- filters->filters[i].filter.pattern,
- filters->filters[i].filter.patternSize);
- filts->filter.pattern = (SaUint8T *)((void *)str - data);
- str += filters->filters[i].filter.patternSize;
- filts++;
- }
- return filters->filtersNumber;
- }
- /*
- * The saEvtEventPublish() function publishes an event on the associated
- * channel. The event to be published consists of a
- * standard set of attributes (the event header) and an optional data part.
- * Before an event can be published, the publisher process must invoke the
- * saEvtEventPatternArraySet() function to set the event patterns. The event
- * is delivered to subscribers whose subscription filter matches the event
- * patterns.
- * When the Event Service publishes an event, it automatically sets
- * the following readonly event attributes:
- * - Event attribute time
- * - Event publisher identifier
- * - Event publisher node identifier
- * - Event identifier
- * In addition to the event attributes, a process can supply values for the
- * eventData and eventDataSize parameters for publication as part of the
- * event. The data portion of the event may be at most SA_EVT_DATA_MAX_LEN
- * bytes in length.
- * The process may assume that the invocation of saEvtEventPublish() copies
- * all pertinent parameters, including the memory associated with the
- * eventHandle and eventData parameters, to its own local memory. However,
- * the invocation does not automatically deallocate memory associated with
- * the eventHandle and eventData parameters. It is the responsibility of the
- * invoking process to deallocate the memory for those parameters after
- * saEvtEventPublish() returns.
- */
- SaAisErrorT
- saEvtEventPublish(
- const SaEvtEventHandleT eventHandle,
- const void *eventData,
- SaSizeT eventDataSize,
- SaEvtEventIdT *eventId)
- {
- SaAisErrorT error;
- struct event_data_instance *edi;
- struct event_instance *evti;
- struct event_channel_instance *eci;
- struct lib_event_data *req;
- struct res_evt_event_publish res;
- size_t pattern_size;
- struct event_pattern *patterns;
- void *data_start;
- struct iovec iov;
- if (!eventId) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- if (eventDataSize > SA_EVT_DATA_MAX_LEN) {
- error = SA_AIS_ERR_TOO_BIG;
- goto pub_done;
- }
- error = saHandleInstanceGet(&event_handle_db, eventHandle,
- (void*)&edi);
- if (error != SA_AIS_OK) {
- goto pub_done;
- }
- pthread_mutex_lock(&edi->edi_mutex);
- error = saHandleInstanceGet(&channel_handle_db, edi->edi_channel_handle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- goto pub_put1;
- }
- /*
- * See if we can publish to this channel
- */
- if (!(eci->eci_open_flags & SA_EVT_CHANNEL_PUBLISHER)) {
- error = SA_AIS_ERR_ACCESS;
- goto pub_put2;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db,
- eci->eci_instance_handle, (void*)&evti);
- if (error != SA_AIS_OK) {
- goto pub_put2;
- }
- /*
- * Figure out how much memory we need for the patterns and data
- */
- pattern_size = patt_size(&edi->edi_patterns);
- req = malloc(sizeof(*req) + eventDataSize + pattern_size);
- if (!req) {
- error = SA_AIS_ERR_NO_MEMORY;
- goto pub_put3;
- }
- patterns = (struct event_pattern *)req->led_body;
- data_start = (void *)req->led_body + pattern_size;
- /*
- * copy everything to the request structure
- */
- aispatt_to_evt_patt(&edi->edi_patterns, patterns);
- req->led_patterns_number = edi->edi_patterns.patternsNumber;
- req->led_user_data_offset = pattern_size;
- if (eventData && eventDataSize) {
- memcpy(data_start, eventData, eventDataSize);
- req->led_user_data_size = eventDataSize;
- } else {
- req->led_user_data_size = 0;
- }
- req->led_head.id = MESSAGE_REQ_EVT_PUBLISH;
- req->led_head.size = sizeof(*req) + pattern_size + eventDataSize;
- req->led_svr_channel_handle = eci->eci_svr_channel_handle;
- req->led_retention_time = edi->edi_retention_time;
- req->led_publish_time = clustTimeNow();
- req->led_priority = edi->edi_priority;
- req->led_publisher_name = edi->edi_pub_name;
- iov.iov_base = req;
- iov.iov_len = req->led_head.size;
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1, &res,
- sizeof(res));
- pthread_mutex_unlock (&evti->ei_response_mutex);
- free(req);
- if (error != SA_AIS_OK) {
- pthread_mutex_unlock (&evti->ei_response_mutex);
- goto pub_put3;
- }
- error = res.iep_head.error;
- if (error == SA_AIS_OK) {
- *eventId = res.iep_event_id;
- }
- pub_put3:
- saHandleInstancePut (&evt_instance_handle_db, eci->eci_instance_handle);
- pub_put2:
- saHandleInstancePut (&channel_handle_db, edi->edi_channel_handle);
- pub_put1:
- pthread_mutex_unlock(&edi->edi_mutex);
- saHandleInstancePut(&event_handle_db, eventHandle);
- pub_done:
- return error;
- }
- /*
- * The saEvtEventSubscribe() function enables a process to subscribe for
- * events on an event channel by registering one or more filters on that
- * event channel. The process must have opened the event channel, designated
- * by channelHandle, with the SA_EVT_CHANNEL_SUBSCRIBER flag set for an
- * invocation of this function to be successful.
- * The memory associated with the filters is not deallocated by the
- * saEvtEventSubscribe() function. It is the responsibility of the invoking
- * process to deallocate the memory when the saEvtEventSubscribe() function
- * returns.
- * For a given subscription, the filters parameter cannot be modified. To
- * change the filters parameter without losing events, a process must
- * establish a new subscription with the new filters parameter. After the new
- * subscription is established, the old subscription can be removed by
- * invoking the saEvtEventUnsubscribe() function.
- */
- SaAisErrorT
- saEvtEventSubscribe(
- const SaEvtChannelHandleT channelHandle,
- const SaEvtEventFilterArrayT *filters,
- SaEvtSubscriptionIdT subscriptionId)
- {
- SaAisErrorT error;
- struct event_instance *evti;
- struct event_channel_instance *eci;
- struct req_evt_event_subscribe *req;
- struct res_evt_event_subscribe res;
- int sz;
- struct iovec iov;
- if (!filters) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&channel_handle_db, channelHandle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- goto subscribe_done;
- }
- /*
- * get the evt handle for the fd
- */
- error = saHandleInstanceGet(&evt_instance_handle_db,
- eci->eci_instance_handle, (void*)&evti);
- if (error != SA_AIS_OK) {
- goto subscribe_put1;
- }
- /*
- * Make sure that a deliver callback has been
- * registered before allowing the subscribe to continue.
- */
- if (!evti->ei_callback.saEvtEventDeliverCallback) {
- error = SA_AIS_ERR_INIT;
- goto subscribe_put2;
- }
- /*
- * See if we can subscribe to this channel
- */
- if (!(eci->eci_open_flags & SA_EVT_CHANNEL_SUBSCRIBER)) {
- error = SA_AIS_ERR_ACCESS;
- goto subscribe_put2;
- }
- /*
- * calculate size needed to store the filters
- */
- sz = filt_size(filters);
- req = malloc(sizeof(*req) + sz);
-
- if (!req) {
- error = SA_AIS_ERR_NO_MEMORY;
- goto subscribe_put2;
- }
- /*
- * Copy the supplied filters to the request
- */
- req->ics_filter_count = aisfilt_to_evt_filt(filters,
- req->ics_filter_data);
- req->ics_head.id = MESSAGE_REQ_EVT_SUBSCRIBE;
- req->ics_head.size = sizeof(*req) + sz;
- req->ics_channel_handle = eci->eci_svr_channel_handle;
- req->ics_sub_id = subscriptionId;
- req->ics_filter_size = sz;
- iov.iov_base = req;
- iov.iov_len = req->ics_head.size;
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
- &res, sizeof(res));
- pthread_mutex_unlock (&evti->ei_response_mutex);
- free(req);
- if (res.ics_head.id != MESSAGE_RES_EVT_SUBSCRIBE) {
- goto subscribe_put2;
- }
- error = res.ics_head.error;
- subscribe_put2:
- saHandleInstancePut(&evt_instance_handle_db,
- eci->eci_instance_handle);
- subscribe_put1:
- saHandleInstancePut(&channel_handle_db, channelHandle);
- subscribe_done:
- return error;
- }
- /*
- * The saEvtEventUnsubscribe() function allows a process to stop receiving
- * events for a particular subscription on an event channel by removing the
- * subscription. The saEvtEventUnsubscribe() operation is successful if the
- * subscriptionId parameter matches a previously registered subscription.
- * Pending events that no longer match any subscription, because the
- * saEvtEventUnsubscribe() operation had been invoked, are purged. a process
- * that wishes to modify a subscription without losing any events must
- * establish the new subscription before removing the existing subscription.
- */
- SaAisErrorT
- saEvtEventUnsubscribe(
- const SaEvtChannelHandleT channelHandle,
- SaEvtSubscriptionIdT subscriptionId)
- {
- SaAisErrorT error;
- struct event_instance *evti;
- struct event_channel_instance *eci;
- struct req_evt_event_unsubscribe req;
- struct res_evt_event_unsubscribe res;
- struct iovec iov;
- error = saHandleInstanceGet(&channel_handle_db, channelHandle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- goto unsubscribe_done;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db,
- eci->eci_instance_handle, (void*)&evti);
- if (error != SA_AIS_OK) {
- goto unsubscribe_put1;
- }
- req.icu_head.id = MESSAGE_REQ_EVT_UNSUBSCRIBE;
- req.icu_head.size = sizeof(req);
- req.icu_channel_handle = eci->eci_svr_channel_handle;
- req.icu_sub_id = subscriptionId;
- iov.iov_base = &req;
- iov.iov_len = sizeof(req);
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
- &res, sizeof(res));
- pthread_mutex_unlock (&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- goto unsubscribe_put2;
- }
- if (res.icu_head.id != MESSAGE_RES_EVT_UNSUBSCRIBE) {
- error = SA_AIS_ERR_LIBRARY;
- goto unsubscribe_put2;
- }
- error = res.icu_head.error;
- unsubscribe_put2:
- saHandleInstancePut(&evt_instance_handle_db,
- eci->eci_instance_handle);
- unsubscribe_put1:
- saHandleInstancePut(&channel_handle_db, channelHandle);
- unsubscribe_done:
- return error;
- }
- /*
- * The saEvtEventRetentionTimeClear() function is used to clear the retention
- * time of a published event. It indicates to the Event Service that it does
- * not need to keep the event any longer for potential new subscribers. Once
- * the value of the retention time is reset to 0, the event is no longer
- * available for new subscribers. The event is held until all old subscribers
- * in the system process the event and free the event using saEvtEventFree().
- */
- SaAisErrorT
- saEvtEventRetentionTimeClear(
- const SaEvtChannelHandleT channelHandle,
- const SaEvtEventIdT eventId)
- {
- SaAisErrorT error;
- struct event_instance *evti;
- struct event_channel_instance *eci;
- struct req_evt_event_clear_retentiontime req;
- struct res_evt_event_clear_retentiontime res;
- struct iovec iov;
- if (!is_valid_event_id(eventId)) {
- return SA_AIS_ERR_INVALID_PARAM;
- }
- error = saHandleInstanceGet(&channel_handle_db, channelHandle,
- (void*)&eci);
- if (error != SA_AIS_OK) {
- goto ret_time_done;
- }
- error = saHandleInstanceGet(&evt_instance_handle_db,
- eci->eci_instance_handle, (void*)&evti);
- if (error != SA_AIS_OK) {
- goto ret_time_put1;
- }
- req.iec_head.id = MESSAGE_REQ_EVT_CLEAR_RETENTIONTIME;
- req.iec_head.size = sizeof(req);
- req.iec_channel_handle = eci->eci_svr_channel_handle;
- req.iec_event_id = eventId;
- iov.iov_base = &req;
- iov.iov_len = sizeof(req);
- pthread_mutex_lock(&evti->ei_response_mutex);
- error = saSendMsgReceiveReply(evti->ei_response_fd, &iov, 1,
- &res, sizeof(res));
- pthread_mutex_unlock (&evti->ei_response_mutex);
- if (error != SA_AIS_OK) {
- goto ret_time_put2;
- }
- if (res.iec_head.id != MESSAGE_RES_EVT_CLEAR_RETENTIONTIME) {
- error = SA_AIS_ERR_LIBRARY;
- goto ret_time_put2;
- }
- error = res.iec_head.error;
- ret_time_put2:
- saHandleInstancePut(&evt_instance_handle_db,
- eci->eci_instance_handle);
- ret_time_put1:
- saHandleInstancePut(&channel_handle_db, channelHandle);
- ret_time_done:
- return error;
- }
- /*
- * vi: set autoindent tabstop=4 shiftwidth=4 :
- */
|