/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * EPICS BASE Versions 3.13.7 * and higher are distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. \*************************************************************************/ /* * Device support for network ping operations * * $Id: devPing.c,v 1.1 2014/09/03 18:21:06 zoven Exp $ */ #include #include #include #include #include #include #include #include #include #include "alarm.h" #include "callback.h" #include "cvtTable.h" #include "dbDefs.h" #include "dbAccess.h" #include "recGbl.h" #include "recSup.h" #include "devSup.h" #include "link.h" #include "dbCommon.h" #include "aiRecord.h" #include "cantProceed.h" #include "epicsEvent.h" #include "epicsEvent.h" #include "epicsSignal.h" #include "epicsString.h" #include "epicsThread.h" #include "epicsMutex.h" #include "epicsExport.h" #include "iocsh.h" #include "errlog.h" /* * Per-driver private information */ static char *pingPath; static int pingArgCount; static char **pingArgs; static double pingTime; /* * Per-record private information */ typedef struct drvPvt { struct aiRecord *pai; epicsMutexId lock; int wasClosed; char *host; char **pingArgs; epicsThreadId pingTid; int pingPid; int pingFd; double value; epicsTimeStamp whenRead; } drvPvt; /* * Forward references */ static void pingThread(void *p); /* * Debugging */ static int devPingDebug = 0; epicsExportAddress(int,devPingDebug); static long init_record(struct aiRecord *pai) { drvPvt *dpvt; int iarg; unsigned int workerThreadPriority; int waitStatus; int pipefd[2]; /* * Set up device private information */ if (pai->inp.type != INST_IO) { printf("devAiPing:init_record %s Illegal INP field\n", pai->name); pai->pact = TRUE; return -1; } if (pai->dpvt == NULL) { if (pingPath == NULL) { printf("devAiPing:init_record %s -- you forgot to run drvPingConfigure!\n", pai->name); pai->pact = TRUE; return -1; } dpvt = callocMustSucceed(1, sizeof *dpvt, "devAiPing drvPvt"); pai->dpvt = dpvt; dpvt->pai = pai; dpvt->value = -1; dpvt->host = epicsStrDup(pai->inp.value.instio.string); if (devPingDebug >= 1) printf("Start pinging '%s' -- pingtime %g.\n", dpvt->host, pingTime); dpvt->lock = epicsMutexMustCreate(); dpvt->pingArgs = callocMustSucceed(pingArgCount+2, sizeof(char *), "devPingInit"); for (iarg = 0 ; iarg < pingArgCount ; iarg++) dpvt->pingArgs[iarg] = pingArgs[iarg]; dpvt->pingArgs[iarg++] = dpvt->host; dpvt->pingArgs[iarg++] = NULL; /* * Set up ping process */ if (pipe(pipefd) < 0) { printf("devAiPing:init_record %s Can't create pipe: %s\n", pai->name, strerror(errno)); return -1; } if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) < 0) { close(pipefd[0]); close(pipefd[1]); printf("devAiPing:init_record %s Can't set pipe close-on-exec: %s\n", pai->name, strerror(errno)); return -1; } if ((dpvt->pingPid = fork()) == 0) { /* * Child */ close(0); open("/dev/null", O_RDONLY); close(1); dup(pipefd[1]); close(2); open("/dev/null", O_WRONLY); close(pipefd[0]); close(pipefd[1]); execv(pingPath, dpvt->pingArgs); _exit(120); } close(pipefd[1]); if (dpvt->pingPid < 0) { close(pipefd[0]); printf("devAiPing:init_record %s Can't fork: %s\n", pai->name, strerror(errno)); return -1; } /* * Find out if ping fails to start properly */ epicsThreadSleep(1.0); if (waitpid(dpvt->pingPid, &waitStatus, WNOHANG) == dpvt->pingPid) { close(pipefd[0]); if (WIFSIGNALED(waitStatus)) printf("devAiPing:init_record %s ping terminated due to signal %d\n", pai->name, WTERMSIG(waitStatus)); else if (WEXITSTATUS(waitStatus) == 120) printf("devAiPing:init_record %s failed to exec ping\n", pai->name); else printf("devAiPing:init_record %s ping terminated with status %d\n", pai->name, WEXITSTATUS(waitStatus)); return -1; } dpvt->pingFd = pipefd[0]; /* * Set up worker thread */ epicsThreadLowestPriorityLevelAbove(epicsThreadPriorityScanHigh, &workerThreadPriority); if ((dpvt->pingTid = epicsThreadCreate(dpvt->host, workerThreadPriority, epicsThreadGetStackSize(epicsThreadStackMedium), pingThread, dpvt)) == NULL) { close(pipefd[0]); kill(dpvt->pingPid, SIGKILL); epicsThreadSleep(1.0); waitpid(dpvt->pingPid, &waitStatus, WNOHANG); printf("devAiPing:init_record %s Can't create worker thread\n", pai->name); return -1; } } return 0; } static long read_ai(struct aiRecord *pai) { drvPvt *dpvt = pai->dpvt; epicsThreadId tid; epicsTimeStamp now; epicsTimeStamp when; double val; epicsMutexLock(dpvt->lock); tid = dpvt->pingTid; val = dpvt->value; when = dpvt->whenRead; epicsMutexUnlock(dpvt->lock); epicsTimeGetCurrent(&now); if ((val < 0) || (tid == 0) || (epicsTimeDiffInSeconds(&now, &when) > (2.2 * pingTime))) { recGblSetSevr(pai, UDF_ALARM, INVALID_ALARM); } else { pai->val = val; pai->udf = FALSE; } return 2; } static void pingThread(void *p) { drvPvt *dpvt = (drvPvt *)p; char cbuf[512]; char units[20]; char *cp; int nleft, nread; double xtmp; int waitStatus; cp = cbuf; nleft = sizeof cbuf; for(;;) { if (nleft == 0) { cp = cbuf; nleft = sizeof cbuf; } nread = read(dpvt->pingFd, cp, nleft); if (nread < 0) { errlogPrintf("pingThread(%s) -- read error: %s\n", dpvt->host, strerror(errno)); break; } if (nread == 0) { errlogPrintf("pingThread(%s) -- ping subprocess terminated\n", dpvt->host); break; } cp += nread; nleft -= nread; if (cp[-1] == '\n') { cp[-1] = '\0'; if (devPingDebug >= 3) printf("pingThread(%s) -- '%s'\n", dpvt->host, cbuf); while (cp > cbuf) { cp--; if ((*cp == 't') && (sscanf(cp, "time = %lg %19s", &xtmp, units) == 2)) { if (strcmp(units,"ms") == 0) xtmp *= 0.001; if (devPingDebug >= 2) printf("pingThread(%s) -- %g (%s)\n", dpvt->host, xtmp, units); epicsMutexLock(dpvt->lock); dpvt->value = xtmp; epicsTimeGetCurrent(&dpvt->whenRead); epicsMutexUnlock(dpvt->lock); break; } } cp = cbuf; nleft = sizeof cbuf; } } epicsMutexLock(dpvt->lock); dpvt->pingTid = 0; epicsMutexUnlock(dpvt->lock); close(dpvt->pingFd); kill(dpvt->pingPid, SIGKILL); epicsThreadSleep(5.0); waitpid(dpvt->pingPid, &waitStatus, WNOHANG); } /* * Create the dset for devAiPing */ struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read_ai; DEVSUPFUN special_linconv; } devAiPing = { 6, NULL, NULL, init_record, NULL, read_ai, NULL }; epicsExportAddress(dset,devAiPing); static long drvPingConfigure(int argc, char **argv) { int i; char *cp; if (pingPath != NULL) { printf("Can invoke drvPingConfigure only once!\n"); return -1; } if (argc <= 1) { printf("Missing 'ping' command\n"); return -1; } pingArgCount = argc - 1; pingArgs = callocMustSucceed(pingArgCount, sizeof(char *), "drvPingConfigure"); pingPath = epicsStrDup(argv[1]); cp = strrchr(pingPath, '/'); if (cp == NULL) cp = pingPath; else cp++; if (*cp == '\0') { printf("Invalid 'ping' command\n"); return -1; } pingArgs[0] = cp; pingTime = 0; for (i = 1 ; i < pingArgCount ; i++) { double xtmp; pingArgs[i] = epicsStrDup(argv[i+1]); xtmp = strtod(pingArgs[i], &cp); if ((cp != pingArgs[i]) && (*cp == '\0')) pingTime = xtmp; } if (pingTime <= 2) pingTime = 2; return 0; } static const iocshArg drvPingConfigureArg0 = { "/ping [pingArgs]", iocshArgArgv}; static const iocshArg * const drvPingConfigureArgs[1] = { &drvPingConfigureArg0 }; static const iocshFuncDef drvPingConfigureFuncDef = {"drvPingConfigure",1,drvPingConfigureArgs}; static void drvPingConfigureCallFunc(const iocshArgBuf *args) { drvPingConfigure(args[0].aval.ac, args[0].aval.av); } static void drvPingConfigureRegisterCommands(void) { static int firstTime = 1; if (firstTime) { iocshRegister(&drvPingConfigureFuncDef,drvPingConfigureCallFunc); firstTime = 0; } } epicsExportRegistrar(drvPingConfigureRegisterCommands);