Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client/castApp/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ testAddEnvVars_SRCS += testAddEnvVars.c
testAddEnvVars_SYS_LIBS_WIN32 = ws2_32
TESTS += testAddEnvVars

TESTPROD_HOST += testAddExcludePattern
testAddExcludePattern_SRCS += testAddExcludePattern.c
testAddExcludePattern_SYS_LIBS_WIN32 = ws2_32
TESTS += testAddExcludePattern

TESTSCRIPTS_HOST += $(TESTS:%=%.t)
ifneq ($(filter $(T_A),$(CROSS_COMPILER_RUNTEST_ARCHS)),)
TESTPROD = $(TESTPROD_HOST)
Expand Down
18 changes: 13 additions & 5 deletions client/castApp/src/caster.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,21 @@ void casterInit(caster_t *self)
self->onmsg = &casterShowMsgDefault;
self->current = casterStateInit;
self->timeout = reccastTimeout;
ellInit(&self->extra_envs);
ellInit(&self->exclude_patterns);

if(shSocketPair(self->wakeup))
errlogPrintf("Error: casterInit failed to create shutdown socket: %d\n", SOCKERRNO);
}

static void nodeFree(void *node) {
string_list_t *temp = (string_list_t *)node;
free(temp->item_str);
free(temp);
}

void casterShutdown(caster_t *self)
{
int i;
epicsUInt32 junk = htonl(0xdeadbeef);

epicsMutexMustLock(self->lock);
Expand All @@ -136,10 +143,11 @@ void casterShutdown(caster_t *self)
epicsEventMustWait(self->shutdownEvent);

epicsMutexMustLock(self->lock);
for (i = 0; i < self->num_extra_envs; i++) {
free(self->extra_envs[i]);
}
free(self->extra_envs);
ellFree2(&self->extra_envs, &nodeFree);
epicsMutexUnlock(self->lock);

epicsMutexMustLock(self->lock);
Comment on lines +147 to +149
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this lock/unlock?

ellFree2(&self->exclude_patterns, &nodeFree);
epicsMutexUnlock(self->lock);

epicsEventDestroy(self->shutdownEvent);
Expand Down
14 changes: 12 additions & 2 deletions client/castApp/src/caster.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <epicsEvent.h>
#include <epicsMutex.h>
#include <compilerDependencies.h>
#include <ellLib.h>

#include "sockhelpers.h"

Expand All @@ -35,6 +36,11 @@ typedef enum {
casterStateDone,
} casterState;

typedef struct {
ELLNODE node;
char *item_str;
} string_list_t;

typedef struct _caster_t {
double timeout;

Expand Down Expand Up @@ -68,8 +74,9 @@ typedef struct _caster_t {
int shutdown;
char lastmsg[MAX_STRING_SIZE];

char **extra_envs;
int num_extra_envs;
ELLLIST extra_envs;

ELLLIST exclude_patterns;

} caster_t;

Expand Down Expand Up @@ -101,6 +108,9 @@ int casterPushPDB(void *junk, caster_t *caster);
epicsShareFunc
void addReccasterEnvVars(caster_t* self, int argc, char **argv);

epicsShareFunc
void addReccasterExcludePattern(caster_t* self, int argc, char **argv);

/* internal */

epicsShareFunc
Expand Down
185 changes: 108 additions & 77 deletions client/castApp/src/castinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ static void casthook(initHookState state)
void addReccasterEnvVars(caster_t* self, int argc, char **argv)
{
size_t i, j;
int ret = 0;

argv++; argc--; /* skip function arg */
if(argc < 1) {
Expand All @@ -95,91 +94,54 @@ void addReccasterEnvVars(caster_t* self, int argc, char **argv)
epicsMutexUnlock(self->lock);
return;
}
int new_extra_envs_size = self->num_extra_envs + argc;
int num_new_extra_envs = self->num_extra_envs;

char **new_extra_envs = calloc(new_extra_envs_size, sizeof(*new_extra_envs));
if(new_extra_envs == NULL) {
errlogSevPrintf(errlogMajor, "Error in memory allocation of new_extra_envs from addReccasterEnvVars\n");
epicsMutexUnlock(self->lock);
return;
}
/* copy self->extra_envs into new_extra_envs with room for new envs */
for(i=0; i < self->num_extra_envs; i++) {
if((new_extra_envs[i] = strdup(self->extra_envs[i])) == NULL) {
errlogSevPrintf(errlogMinor, "strdup error for copying %s to new_extra_envs[%zu] from addReccasterEnvVars\n", self->extra_envs[i], i);
ret = 1;
break;
}
}
int found_dup;
/* sanitize input - check for dups and empty args */
if(!ret) {
for(i=0; i < argc; i++) {
if(argv[i] == NULL) {
errlogSevPrintf(errlogMinor, "Arg is NULL for addReccasterEnvVars\n");
continue;
}
else if(argv[i][0] == '\0') {
errlogSevPrintf(errlogMinor, "Arg is empty for addReccasterEnvVars\n");
continue;
}
found_dup = 0;
/* check if dup in self->default_envs */
for(j = 0; default_envs[j]; j++) {
if(strcmp(argv[i], default_envs[j]) == 0) {
found_dup = 1;
errlogSevPrintf(errlogMinor, "Env var %s is already in env list sent by reccaster by default\n", argv[i]);
break;
}
}
if(found_dup) {
continue;
}
/* check if dup in self->extra_envs */
for(j = 0; j < num_new_extra_envs; j++) {
if(new_extra_envs[j] == NULL) {
continue;
}
if(strcmp(argv[i], new_extra_envs[j]) == 0) {
found_dup = 1;
errlogSevPrintf(errlogMinor, "Env var %s is already in extra_envs list\n", argv[i]);
break;
}
}
if(found_dup) {
continue;
for(i=0; i < argc; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of duplication in this loop and in the one for exclude patterns, just a thought...

if(argv[i][0] == '\0') {
errlogSevPrintf(errlogMinor, "Arg is empty for addReccasterEnvVars\n");
continue;
}
found_dup = 0;
/* check if dup in self->default_envs */
for(j = 0; default_envs[j]; j++) {
if(strcmp(argv[i], default_envs[j]) == 0) {
found_dup = 1;
break;
}
if((new_extra_envs[num_new_extra_envs] = strdup(argv[i])) == NULL) {
errlogSevPrintf(errlogMinor, "strdup error for copying %s to new_extra_envs[%d] from addReccasterEnvVars\n", argv[i], num_new_extra_envs);
ret = 1;
}
if(found_dup) {
errlogSevPrintf(errlogMinor, "Env var %s is already in env list sent by reccaster by default\n", argv[i]);
continue;
}
/* check if dup in self->extra_envs */
ELLNODE *cur = ellFirst(&self->extra_envs);
while (cur != NULL) {
string_list_t *temp = (string_list_t *)cur;
if (strcmp(argv[i], temp->item_str) == 0) {
found_dup = 1;
break;
}
/* this is a valid arg and we have added the new env var to our array, increment new_extra_envs count */
num_new_extra_envs++;
cur = ellNext(cur);
}
if(found_dup) {
errlogSevPrintf(errlogMinor, "Env var %s is already in extra_envs list\n", argv[i]);
continue;
}
string_list_t *new_list = malloc(sizeof(string_list_t));
if (new_list == NULL) {
errlogSevPrintf(errlogMajor, "Error in addReccasterEnvVars - malloc error for creating linked list node");
break;
}
new_list->item_str = strdup(argv[i]);
Comment on lines +131 to +136
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi. These two allocations could be combined into one. For example: https://github.com/epics-base/epics-base/blob/e4fbd83c93a68a1d9cb863ff341735388d246347/modules/libcom/src/iocsh/afterIocRunning.c#L62-L65

    const size_t cmd_len = strlen(cmd) + 1;
    struct cmditem *const item = mallocMustSucceed(sizeof(struct cmditem) + cmd_len, "afterIocRunning");
    item->cmd = (char *)(item + 1);
    memcpy(item->cmd, cmd, cmd_len);

Also, iteration could be done in a more compact fashion: https://github.com/epics-base/epics-base/blob/e4fbd83c93a68a1d9cb863ff341735388d246347/modules/libcom/src/iocsh/initHooks.c#L64-L65

    for(cur = ellFirst(&functionList); cur; cur = ellNext(cur)) {
        const initHookLink *fn = CONTAINER(cur, initHookLink, node);

if (new_list->item_str == NULL) {
errlogSevPrintf(errlogMajor, "Error in addReccasterEnvVars - strdup error for copying %s to new->item_str from addReccasterEnvVars\n", argv[i]);
free(new_list); /* frees if strdup fails */
break;
}
ellAdd(&self->extra_envs, &new_list->node);
}
/* if we have no allocation issues and have at least one new env var that is valid, add to self->extra_envs */
if(!ret && num_new_extra_envs > self->num_extra_envs) {
/* from this point, nothing can fail */
char ** tmp;
tmp = self->extra_envs; /* swap pointers so we can clean up new_extra_envs on success/failure */
self->extra_envs = new_extra_envs;
new_extra_envs = tmp;

new_extra_envs_size = self->num_extra_envs; /* with swap of pointers also swap size */
self->num_extra_envs = num_new_extra_envs;
}
/* cleanup new_extra_envs[] on success or failure */
for(i = 0; i < new_extra_envs_size; i++) {
free(new_extra_envs[i]);
}
free(new_extra_envs);
epicsMutexUnlock(self->lock);

if(ret) {
errlogSevPrintf(errlogMajor, "Error in addReccasterEnvVars - reccaster might not send the extra env vars specified\n");
}
}

static const iocshArg addReccasterEnvVarsArg0 = { "environmentVar", iocshArgArgv };
Expand All @@ -200,6 +162,74 @@ static void addReccasterEnvVarsCallFunc(const iocshArgBuf *args)
addReccasterEnvVars(&thecaster, args[0].aval.ac, args[0].aval.av);
}

void addReccasterExcludePattern(caster_t* self, int argc, char **argv) {
size_t i;
argv++; argc--; /* skip function arg */
if (argc < 1) {
errlogSevPrintf(errlogMinor, "At least one argument expected for addReccasterExcludePattern\n");
return;
}
epicsMutexMustLock(self->lock);
if (self->shutdown) {
/* shutdown in progress, silent no-op */
epicsMutexUnlock(self->lock);
return;
}
/* error if called after iocInit() */
if (self->current != casterStateInit) {
errlogSevPrintf(errlogMinor, "addReccasterExcludePattern called after iocInit() when reccaster might already be connected. Not supported\n");
epicsMutexUnlock(self->lock);
return;
}

for (i = 0; i < argc; i++) {
if (argv[i][0] == '\0') {
errlogSevPrintf(errlogMinor, "Arg is empty for addReccasterExcludePattern\n");
continue;
}
/* check duplicates */
int dup = 0;
ELLNODE *cur = ellFirst(&self->exclude_patterns);
while (cur != NULL) {
string_list_t *temp = (string_list_t *)cur;
if (strcmp(argv[i], temp->item_str) == 0) {
dup = 1;
break;
}
cur = ellNext(cur);
}
if (dup) {
errlogSevPrintf(errlogMinor, "Duplicate pattern %s in addReccasterExcludePattern\n", argv[i]);
continue;
}
string_list_t *new_list = malloc(sizeof(string_list_t));
if (new_list == NULL) {
errlogSevPrintf(errlogMajor, "Error in addReccasterExcludePattern - malloc error for creating linked list node");
break;
}
new_list->item_str = strdup(argv[i]);
if (new_list->item_str == NULL) {
errlogSevPrintf(errlogMajor, "Error in addReccasterExcludePattern - strdup error for copying %s to new->item_str from addReccasterExcludePattern\n", argv[i]);
free(new_list); /* frees if strdup fails */
break;
}
ellAdd(&self->exclude_patterns, &new_list->node);
}
epicsMutexUnlock(self->lock);
}

static const iocshArg addReccasterExcludePatternArg0 = { "excludePattern", iocshArgArgv };
static const iocshArg * const addReccasterExcludePatternArgs[] = { &addReccasterExcludePatternArg0 };
static const iocshFuncDef addReccasterExcludePatternFuncDef = {
"addReccasterExcludePattern",
1,
addReccasterExcludePatternArgs
};

static void addReccasterExcludePatternCallFunc(const iocshArgBuf *args) {
addReccasterExcludePattern(&thecaster, args[0].aval.ac, args[0].aval.av);
}

static void reccasterRegistrar(void)
{
osiSockAttach();
Expand All @@ -210,6 +240,7 @@ static void reccasterRegistrar(void)
thepriv.laststate=casterStateInit;
strcpy(thepriv.lastmsg, "Initializing");
iocshRegister(&addReccasterEnvVarsFuncDef,addReccasterEnvVarsCallFunc);
iocshRegister(&addReccasterExcludePatternFuncDef,addReccasterExcludePatternCallFunc);
}

static long init_record(void* prec)
Expand Down
20 changes: 16 additions & 4 deletions client/castApp/src/dbcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ static int pushEnv(caster_t *caster)
}

epicsMutexMustLock(caster->lock);
for (i = 0; !ret && i < caster->num_extra_envs; i++) {
const char *val = getenv(caster->extra_envs[i]);
ELLNODE *env = ellFirst(&caster->extra_envs);
while (!ret && env != NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this change has addressed my previous comment about the loop iteration. Consider squashing these two commits?

string_list_t *temp = (string_list_t *)env;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love the variable name temp; envptr was better, and penvvar or somethign would be better yet (and more consistent; see how ellFirst/ellNext is used in EPICS base)

Copy link
Collaborator

@mdavidsaver mdavidsaver Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string_list_t *envptr = CONTAINER(env, string_list_t, node);

CONTAINER comes from dbDefs.h, and is intended for these structure "base" to "derived" casts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... Corrected missing first argument

const char *val = getenv(temp->item_str);
if (val && val[0] != '\0')
ret = casterSendInfo(caster, 0, caster->extra_envs[i], val);
ret = casterSendInfo(caster, 0, temp->item_str, val);
if (ret)
casterMsg(caster, "Error sending env %s", caster->extra_envs[i]);
casterMsg(caster, "Error sending env %s", temp->item_str);
env = ellNext(env);
}
epicsMutexUnlock(caster->lock);

Expand All @@ -87,12 +90,21 @@ static int pushRecord(caster_t *caster, DBENTRY *pent)
{
dbCommon *prec = pent->precnode->precord;
ssize_t rid;
ELLNODE *cur;
int ret = 0;
long status;

if(dbIsAlias(pent))
return 0;

cur = ellFirst(&caster->exclude_patterns);
while (cur != NULL) {
string_list_t *temp = (string_list_t *)cur;
if(epicsStrGlobMatch(prec->name, temp->item_str))
return 0;
cur = ellNext(cur);
}

rid = casterSendRecord(caster, prec->rdes->name, prec->name);
if(rid<=0)
return rid;
Expand Down
Loading