Skip to content

Commit

Permalink
asTrap serverSpecific is dbChannel
Browse files Browse the repository at this point in the history
Save/restore dbAddr::pfield around callbacks to
avoid corruption if CB forgets to restore.

Need to peak at dbChannel.h during libCom build.
So generate dbCoreAPI.h early, and add extra
-I to source location when compiling dbTrapWrite.c
  • Loading branch information
mdavidsaver committed Jun 12, 2024
1 parent 6ca716a commit ec77d5c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
3 changes: 1 addition & 2 deletions modules/database/src/ioc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ USR_CPPFLAGS += -DUSE_TYPED_RSET -DUSE_TYPED_DSET
# Shared library ABI version.
SHRLIB_VERSION = $(EPICS_DATABASE_MAJOR_VERSION).$(EPICS_DATABASE_MINOR_VERSION).$(EPICS_DATABASE_MAINTENANCE_VERSION)

API_HEADER = dbCoreAPI.h
dbCore_API = dbCore
# dbCoreAPI.h generated earlier during libcom/

LIBRARY_IOC += dbCore
dbCore_LIBS += ca Com
Expand Down
3 changes: 3 additions & 0 deletions modules/libcom/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ include $(LIBCOM)/yajl/Makefile
# Generate library API header file
API_HEADER = libComAPI.h
Com_API = libCom
# Generate early to allow a sneak peak at dbChannel.h
API_HEADER += dbCoreAPI.h
dbCore_API = dbCore

# Library to build:
LIBRARY=Com
Expand Down
4 changes: 4 additions & 0 deletions modules/libcom/src/as/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ INC += asTrapWrite.h
Com_SRCS += asLib.c
Com_SRCS += asTrapWrite.c

# Allow early access to dbChannel.h
asTrapWrite_CPPFLAGS = -I$(LIBCOM)/../../database/src/ioc/db


CLEANS += asLib.c asLib_lex.c
4 changes: 3 additions & 1 deletion modules/libcom/src/as/asLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
extern "C" {
#endif

struct dbChannel;

/* 0 - Use (unverified) client provided host name string.
* 1 - Use actual client IP address. HAG() are resolved to IPs at ACF load time.
*/
Expand Down Expand Up @@ -121,7 +123,7 @@ LIBCOM_API int epicsStdCall asDumpHash(void);
LIBCOM_API int epicsStdCall asDumpHashFP(FILE *fp);

LIBCOM_API void * epicsStdCall asTrapWriteBeforeWithData(
const char *userid, const char *hostid, void *addr,
const char *userid, const char *hostid, struct dbChannel *addr,
int dbrType, int no_elements, void *data);

LIBCOM_API void epicsStdCall asTrapWriteAfterWrite(void *pvt);
Expand Down
14 changes: 11 additions & 3 deletions modules/libcom/src/as/asTrapWrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

#include "ellLib.h"
#include "freeList.h"
#include "epicsStdio.h"
#include "cantProceed.h"
#include "epicsMutex.h"
#include "ellLib.h"
#include <dbChannel.h>

#include "asLib.h"
#include "asTrapWrite.h"
Expand Down Expand Up @@ -112,11 +112,12 @@ void epicsStdCall asTrapWriteUnregisterListener(asTrapWriteId id)
}

void * epicsStdCall asTrapWriteBeforeWithData(
const char *userid, const char *hostid, void *addr,
const char *userid, const char *hostid, dbChannel *chan,
int dbrType, int no_elements, void *data)
{
writeMessage *pwriteMessage;
listener *plistener;
void *pfieldsave;

if (pasTrapWritePvt == 0 ||
ellCount(&pasTrapWritePvt->listenerList) <= 0) return 0;
Expand All @@ -125,13 +126,14 @@ void * epicsStdCall asTrapWriteBeforeWithData(
pasTrapWritePvt->freeListWriteMessage);
pwriteMessage->message.userid = userid;
pwriteMessage->message.hostid = hostid;
pwriteMessage->message.serverSpecific = addr;
pwriteMessage->message.serverSpecific = chan;
pwriteMessage->message.dbrType = dbrType;
pwriteMessage->message.no_elements = no_elements;
pwriteMessage->message.data = data;
ellInit(&pwriteMessage->listenerPvtList);

epicsMutexMustLock(pasTrapWritePvt->lock);
pfieldsave = chan->addr.pfield;
ellAdd(&pasTrapWritePvt->writeMessageList, &pwriteMessage->node);
plistener = (listener *)ellFirst(&pasTrapWritePvt->listenerList);
while (plistener) {
Expand All @@ -141,6 +143,7 @@ void * epicsStdCall asTrapWriteBeforeWithData(
plistenerPvt->plistener = plistener;
pwriteMessage->message.userPvt = 0;
plistener->func(&pwriteMessage->message, 0);
chan->addr.pfield = pfieldsave;
plistenerPvt->userPvt = pwriteMessage->message.userPvt;
ellAdd(&pwriteMessage->listenerPvtList, &plistenerPvt->node);
plistener = (listener *)ellNext(&plistener->node);
Expand All @@ -153,18 +156,23 @@ void epicsStdCall asTrapWriteAfterWrite(void *pvt)
{
writeMessage *pwriteMessage = (writeMessage *)pvt;
listenerPvt *plistenerPvt;
dbChannel *chan;
void *pfieldsave;

if (pwriteMessage == 0 ||
pasTrapWritePvt == 0) return;

epicsMutexMustLock(pasTrapWritePvt->lock);
chan = pwriteMessage->message.serverSpecific;
pfieldsave = chan->addr.pfield;
plistenerPvt = (listenerPvt *)ellFirst(&pwriteMessage->listenerPvtList);
while (plistenerPvt) {
listenerPvt *pnext = (listenerPvt *)ellNext(&plistenerPvt->node);
listener *plistener = plistenerPvt->plistener;

pwriteMessage->message.userPvt = plistenerPvt->userPvt;
plistener->func(&pwriteMessage->message, 1);
chan->addr.pfield = pfieldsave;
ellDelete(&pwriteMessage->listenerPvtList, &plistenerPvt->node);
freeListFree(pasTrapWritePvt->freeListListenerPvt, plistenerPvt);
plistenerPvt = pnext;
Expand Down
4 changes: 3 additions & 1 deletion modules/libcom/src/as/asTrapWrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
extern "C" {
#endif

struct dbChannel;

/**
* \brief The message passed to registered listeners.
*/
Expand All @@ -39,7 +41,7 @@ typedef struct asTrapWriteMessage {
* the value the server provides to asTrapWriteWithData(), which
* for RSRV is the dbChannel pointer for the target field.
*/
void *serverSpecific;
struct dbChannel *serverSpecific;
/** \brief A field for use by the \ref asTrapWriteListener.
*
* When the listener is called before the write, this has the
Expand Down

0 comments on commit ec77d5c

Please sign in to comment.