From 8cf738869d813af14ac25a330dff1ad04f102dac Mon Sep 17 00:00:00 2001 From: zolliker Date: Wed, 13 Feb 2008 13:07:30 +0000 Subject: [PATCH] - split status.c into status.c and statusfile.c - improvements for scriptcontext.c --- conman.c | 22 +--- conman.h | 1 - make_gen | 2 +- nserver.c | 1 + ofac.c | 2 + savehdb.c | 22 +--- scriptcontext.c | 3 + sicshipadaba.c | 17 +-- sllinux_def | 2 +- status.c | 215 ----------------------------------- status.h | 9 -- statusfile.c | 290 ++++++++++++++++++++++++++++++++++++++++++++++++ statusfile.h | 24 ++++ 13 files changed, 333 insertions(+), 277 deletions(-) create mode 100644 statusfile.c create mode 100644 statusfile.h diff --git a/conman.c b/conman.c index f3888ec0..0cc27db8 100644 --- a/conman.c +++ b/conman.c @@ -67,6 +67,7 @@ #include "uubuffer.h" #include "commandlog.h" #include "stptok.h" +#include "statusfile.h" #include "sicshipadaba.h" #include "protocol.h" /* @@ -1594,7 +1595,6 @@ pDynString SCEndBuffering(SConnection *pCon) /* invoke */ self->inUse++; self->eInterrupt = eContinue; - self->parameterChange = 0; /* get first word of command */ @@ -1603,18 +1603,8 @@ pDynString SCEndBuffering(SConnection *pCon) SCAdvanceContext(self,pBueffel); iRet = InterpExecute(pInter,self,pCommand); SCPopContext(self); - if(self->parameterChange == 1) - { - /* - automatically save changed parameters - */ - pFile = IFindOption(pSICSOptions,"statusfile"); - if(pFile != NULL) - { - WriteSicsStatus(pInter,pFile,0); - self->parameterChange = 0; - } - } + StatusFileTask(NULL); /* save changed parameters */ + self->inUse--; return iRet; } @@ -2221,11 +2211,7 @@ pDynString SCEndBuffering(SConnection *pCon) /*-----------------------------------------------------------------------*/ void SCparChange(SConnection *self) { - if(!VerifyConnection(self)) - { - return; - } - self->parameterChange = 1; + StatusFileDirty(); } /*------------------------------------------------------------------------*/ int SCActive(SConnection *self) diff --git a/conman.h b/conman.h index c88a9042..a26b56bf 100644 --- a/conman.h +++ b/conman.h @@ -58,7 +58,6 @@ typedef int (*writeFunc)(struct __SConnection *pCon, int iUserRights; int inUse; int iGrab; /* grab flag for token*/ - int parameterChange; int sicsError; /* diff --git a/make_gen b/make_gen index ec9697e4..8b87bb79 100644 --- a/make_gen +++ b/make_gen @@ -32,7 +32,7 @@ SOBJ = network.o ifile.o conman.o SCinter.o splitter.o passwd.o \ mcstashm.o initializer.o remob.o tclmotdriv.o protocol.o \ sinfox.o sicslist.o cone.o hipadaba.o sicshipadaba.o statistics.o \ ascon.o errormsg.o scriptcontext.o logger.o logreader.o logsetup.o \ - savehdb.o \ + savehdb.o statusfile.o \ moregress.o hdbcommand.o multicounter.o regresscter.o histregress.o \ sicshdbadapter.o polldriv.o sicspoll.o statemon.o hmslave.o \ nwatch.o asyncqueue.o asyncprotocol.o sicsobj.o hdbqueue.o\ diff --git a/nserver.c b/nserver.c index f4a2cce0..de27ba07 100644 --- a/nserver.c +++ b/nserver.c @@ -24,6 +24,7 @@ #include "network.h" #include "ifile.h" #include "status.h" +#include "statusfile.h" #include "devexec.h" #include "passwd.h" #include "lld.h" diff --git a/ofac.c b/ofac.c index 6dd5fed3..966b5da6 100644 --- a/ofac.c +++ b/ofac.c @@ -56,6 +56,7 @@ #include "servlog.h" #include "nserver.h" #include "status.h" +#include "statusfile.h" #include "configfu.h" #include "devexec.h" #include "mumo.h" @@ -441,6 +442,7 @@ INIT(SctStartup); INIT(LogReaderInit); INIT(LogSetupInit); + INIT(StatusFileInit); INIT(SiteInit); /* site specific initializations */ } diff --git a/savehdb.c b/savehdb.c index 73919f70..647f4587 100644 --- a/savehdb.c +++ b/savehdb.c @@ -1,9 +1,9 @@ #include #include "dynstring.h" +#include "statusfile.h" #include "sicshipadaba.h" static pDummy creationCommands = NULL; -static int saveit = 0; static void SaveHdbBranch(pHdb node, FILE *fil) { pHdb child; @@ -32,7 +32,7 @@ static int SaveHdbTree(void *object, char *name, FILE *fil) { } static int SaveHdbCallback(void *user, void *conn, pHdb node, hdbValue value) { - saveit = 1; + StatusFileDirty(); return 1; } @@ -62,22 +62,7 @@ static int SaveHdbEnable(SConnection *con, SicsInterp *sics, SetHdbProperty(node, "save", "kids"); } } - saveit = 1; - return 1; -} - -int SaveHdbTask(void *data) { - char *pFile = NULL; - - if (saveit) { - saveit = 0; - - assert(pServ->pSics); - pFile = IFindOption(pSICSOptions,"statusfile"); - if (pFile) { - WriteSicsStatus(pServ->pSics,pFile,0); - } - } + StatusFileDirty(); return 1; } @@ -87,5 +72,4 @@ void SaveHdbInit(void) { hs = CreateDummy("hdb saver"); hs->pDescriptor->SaveStatus = SaveHdbTree; AddCommandWithFlag(pServ->pSics, "hsave", SaveHdbEnable, KillDummy, hs, 0); - TaskRegister(pServ->pTasker, SaveHdbTask, NULL, NULL, NULL, 0); } diff --git a/scriptcontext.c b/scriptcontext.c index 151a34c3..459b2df9 100644 --- a/scriptcontext.c +++ b/scriptcontext.c @@ -492,6 +492,9 @@ static char *SctCall(char *command) { } static char *SctCallChain(SctChain *sc) { + if (sc->command[0] == '\0' || strcmp(sc->command, "0") == 0) { + return NULL; /* empty script: do nothing */ + } sct->runningChain = sc; sct->relatedPath = sc->relatedPath; sc->msg = NULL; diff --git a/sicshipadaba.c b/sicshipadaba.c index 358694ec..7984f871 100644 --- a/sicshipadaba.c +++ b/sicshipadaba.c @@ -24,6 +24,7 @@ #include #include #include "protocol.h" +#include "statusfile.h" #include #include "sicsobj.h" @@ -2592,23 +2593,13 @@ static int ChainHdbNode(SConnection *pCon, SicsInterp *pSics, void *pData, /* invoke */ self->inUse++; self->eInterrupt = eContinue; - self->parameterChange = 0; /* get first word of command */ iRet = InterpExecute(pInter,self,pCommand); - if(self->parameterChange == 1) - { - /* - automatically save changed parameters - */ - pFile = IFindOption(pSICSOptions,"statusfile"); - if(pFile != NULL) - { - WriteSicsStatus(pInter,pFile,0); - self->parameterChange = 0; - } - } + + StatusFileTask(NULL); /* save changed parameters */ + self->inUse--; return iRet; } diff --git a/sllinux_def b/sllinux_def index 38576c3a..ea98bcdb 100644 --- a/sllinux_def +++ b/sllinux_def @@ -9,4 +9,4 @@ MFLAGS=-f makefile_linux$(DUMMY) -HDFROOT=/afs/psi.ch/project/sinq/sl5 +HDFROOT=/afs/psi.ch/project/sinq/sl-linux diff --git a/status.c b/status.c index 0a851a60..ca7b15d9 100644 --- a/status.c +++ b/status.c @@ -51,11 +51,7 @@ #include "sics.h" #include "status.h" #include "interrupt.h" -#include "devexec.h" #include "sicshipadaba.h" -#include "lld_str.h" -#include "lld.h" -#include "exebuf.h" #undef VALUECHANGE #define VALUECHANGE 2 @@ -209,7 +205,6 @@ commandContext cc) { pHdb node = NULL; - char pBueffel[80]; hdbValue v; assert(pUser); @@ -349,213 +344,3 @@ pOwner->pSock = pCon->pSock; return 1; } -/*---------------------------------------------------------------------*/ -static int motorSave = 0; -/*-----------------------------------------------------------------------*/ - int BackupStatus(SConnection *pCon, SicsInterp *pSics, void *pData, - int argc, char *argv[]) - { - int iRet; - char pBueffel[512]; - char *pFile = NULL; - - assert(pSics); - assert(pCon); - - if(argc < 2) - { - pFile = IFindOption(pSICSOptions,"statusfile"); - if(pFile) - { - iRet = WriteSicsStatus(pSics,pFile,motorSave); - } - else - { - SCWrite(pCon,"ERROR: No filename given for backup, Aborted.", - eError); - return 0; - } - } - else - { - if(strcmp(argv[1],"motorSave") == 0) - { - if(motorSave== 1) - motorSave= 0; - else - motorSave= 1; - sprintf(pBueffel,"New Value of motorSave= %d\n",motorSave); - SCWrite(pCon,pBueffel,eValue); - return 1; - } - else - { - iRet = WriteSicsStatus(pSics,argv[1],motorSave); - } - } - - if(!iRet) - { - sprintf(pBueffel,"ERROR: could not open file %s\n", argv[1]); - SCWrite(pCon,pBueffel,eError); - return 0; - } - SCSendOK(pCon); - return 1; - } -/*---------------------------------------------------------------------*/ -static int restoreOccurred = 0; -int hasRestored(){ - return restoreOccurred; -} -/*---------------------------------------------------------------------------*/ -typedef struct { - pObjectDescriptor pDes; - int errList; -}RestoreObj, *pRestoreObj; -/*---------------------------------------------------------------------------*/ -static void killRestore(void *data){ - pRestoreObj self = (pRestoreObj)data; - if(self == NULL){ - return; - } - if(self->errList >= 0){ - LLDdeleteBlob(self->errList); - } - if(self->pDes != NULL){ - DeleteDescriptor(self->pDes); - } - free(self); -} -/*--------------------------------------------------------------------------*/ -static int SaveRestore(void *obj, char *name, FILE *fd){ - int status; - char buffer[1024]; - - pRestoreObj self = (pRestoreObj)obj; - if(self == NULL){ - return 0; - } - fprintf(fd,"\n#--- BEGIN (commands producing errors on last restore)\n"); - status = LLDnodePtr2First(self->errList); - while(status == 1){ - LLDstringData(self->errList,buffer); - fprintf(fd,"%s", buffer); - status = LLDnodePtr2Next(self->errList); - } - fprintf(fd,"#--- END (commands producing errors on last restore)\n\n"); - return 1; -} -/*---------------------------------------------------------------------------*/ -int InstallBckRestore(SConnection *pCon, SicsInterp *pSics){ - pRestoreObj pNew = NULL; - - pNew = malloc(sizeof(RestoreObj)); - if(pNew == NULL){ - SCWrite(pCon,"ERROR: no memory to create restore object! This is SERIOUS!!!", - eError); - return 0; - } - pNew->pDes = CreateDescriptor("BckRestore"); - pNew->errList = LLDstringCreate(); - if(pNew->pDes == NULL || pNew->errList < 0){ - SCWrite(pCon,"ERROR: no memory to create restore object! This is SERIOUS!!!", - eError); - return 0; - } - pNew->pDes->SaveStatus = SaveRestore; - AddCommand(pSics,"Backup",BackupStatus,NULL,NULL); - AddCommand(pSics,"Restore",RestoreStatus,killRestore,pNew); - - return 1; -} -/*-----------------------------------------------------------------------*/ -static int listRestoreErr(pRestoreObj self, SConnection *pCon){ - char buffer[1024]; - int status; - pDynString data = NULL; - - SCStartBuffering(pCon); - status = LLDnodePtr2First(self->errList); - while(status == 1){ - LLDstringData(self->errList,buffer); - SCWrite(pCon,buffer,eValue); - status = LLDnodePtr2Next(self->errList); - } - data = SCEndBuffering(pCon); - if(data != NULL){ - SCWrite(pCon,GetCharArray(data),eValue); - } - return 1; -} -/*-----------------------------------------------------------------------*/ - int RestoreStatus(SConnection *pCon, SicsInterp *pSics, void *pData, - int argc, char *argv[]) - { - char pBueffel[512]; - int iRights; - int iRet; - char *pFile = NULL; - writeFunc oldWrite; - pExeBuf buffi = NULL; - pRestoreObj self = (pRestoreObj)pData; - - assert(pSics); - assert(pCon); - assert(self != NULL); - - - if(argc < 2) - { - pFile = IFindOption(pSICSOptions,"statusfile"); - if(pFile) - { - sprintf(pBueffel,"%s",pFile); - } - else - { - SCWrite(pCon,"ERROR: No filename given for backup, Aborted.", - eError); - return 0; - } - } - else - { - if(strcmp(argv[1],"listerr") == 0){ - return listRestoreErr(self,pCon); - } else { - sprintf(pBueffel,"%s",argv[1]); - } - } - - buffi = exeBufCreate("restore"); - if(buffi == NULL){ - SCWrite(pCon,"ERROR: failed to allocate buffer for restore",eError); - return 0; - } - iRet = exeBufLoad(buffi,pBueffel); - if(iRet != 1){ - exeBufDelete(buffi); - SCWrite(pCon,"ERROR: failed open status file",eError); - return 0; - } - LLDdeleteBlob(self->errList); - self->errList = LLDstringCreate(); - iRights = SCGetRights(pCon); - pCon->iUserRights = usInternal; - oldWrite = SCGetWriteFunc(pCon); - SCSetWriteFunc(pCon,SCNotWrite); - iRet = exeBufProcessErrList(buffi,pSics,pCon,self->errList); - restoreOccurred = 1; - SCSetWriteFunc(pCon,oldWrite); - pCon->iUserRights = iRights; - exeBufDelete(buffi); - /* - if we do not override parameterChange here, the backup file - would be overwritten after each restore... Not the right thing - to do! - */ - pCon->parameterChange = 0; - SCSendOK(pCon); - return iRet; - } diff --git a/status.h b/status.h index 9d00020e..f80774da 100644 --- a/status.h +++ b/status.h @@ -50,13 +50,4 @@ int ResetStatus(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]); - int InstallBckRestore(SConnection *pCon, SicsInterp *pSics); - int BackupStatus(SConnection *pCon, SicsInterp *pSics, void *pData, - int argc, char *argv[]); - - int RestoreStatus(SConnection *pCon, SicsInterp *pSics, void *pData, - int argc, char *argv[]); - int hasRestored(); - - #endif diff --git a/statusfile.c b/statusfile.c new file mode 100644 index 00000000..4bf28b51 --- /dev/null +++ b/statusfile.c @@ -0,0 +1,290 @@ +/*-------------------------------------------------------------------------- + + Handling of status files + + + Mark Koennecke, November 1996 + + Updated in order to prevent status floods + Mark Koennecke, July 2004 + + Reworked restore to keep parameters from uninitialized devices + Mark Koennecke, November 2007 + + Copyright: + + Labor fuer Neutronenstreuung + Paul Scherrer Institut + CH-5423 Villigen-PSI + + + The authors hereby grant permission to use, copy, modify, distribute, + and license this software and its documentation for any purpose, provided + that existing copyright notices are retained in all copies and that this + notice is included verbatim in any distributions. No written agreement, + license, or royalty fee is required for any of the authorized uses. + Modifications to this software may be copyrighted by their authors + and need not follow the licensing terms described here, provided that + the new terms are clearly indicated on the first page of each file where + they apply. + + IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY + FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY + DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE + IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE + NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR + MODIFICATIONS. +-----------------------------------------------------------------------------*/ +#include +#include +#include +#include "fortify.h" +#include +#include "sics.h" +#include "statusfile.h" +#include "lld_str.h" +#include "lld.h" +#include "exebuf.h" + +static int parameterChange = 0; +/*-----------------------------------------------------------------------*/ +int StatusFileTask(void *data) { + char *pFile = NULL; + + if (parameterChange) { + parameterChange = 0; + + assert(pServ->pSics); + pFile = IFindOption(pSICSOptions,"statusfile"); + if (pFile) { + WriteSicsStatus(pServ->pSics,pFile,0); + } + } + return 1; +} +/*---------------------------------------------------------------------*/ +static int motorSave = 0; +/*-----------------------------------------------------------------------*/ + int BackupStatus(SConnection *pCon, SicsInterp *pSics, void *pData, + int argc, char *argv[]) + { + int iRet; + char pBueffel[512]; + char *pFile = NULL; + + assert(pSics); + assert(pCon); + + if(argc < 2) + { + pFile = IFindOption(pSICSOptions,"statusfile"); + if(pFile) + { + iRet = WriteSicsStatus(pSics,pFile,motorSave); + } + else + { + SCWrite(pCon,"ERROR: No filename given for backup, Aborted.", + eError); + return 0; + } + } + else + { + if(strcmp(argv[1],"motorSave") == 0) + { + if(motorSave== 1) + motorSave= 0; + else + motorSave= 1; + sprintf(pBueffel,"New Value of motorSave= %d\n",motorSave); + SCWrite(pCon,pBueffel,eValue); + return 1; + } + else + { + iRet = WriteSicsStatus(pSics,argv[1],motorSave); + } + } + + if(!iRet) + { + sprintf(pBueffel,"ERROR: could not open file %s\n", argv[1]); + SCWrite(pCon,pBueffel,eError); + return 0; + } + SCSendOK(pCon); + return 1; + } +/*---------------------------------------------------------------------*/ +static int restoreOccurred = 0; +int hasRestored(){ + return restoreOccurred; +} +/*---------------------------------------------------------------------------*/ +typedef struct { + pObjectDescriptor pDes; + int errList; +}RestoreObj, *pRestoreObj; +/*---------------------------------------------------------------------------*/ +static void killRestore(void *data){ + pRestoreObj self = (pRestoreObj)data; + if(self == NULL){ + return; + } + if(self->errList >= 0){ + LLDdeleteBlob(self->errList); + } + if(self->pDes != NULL){ + DeleteDescriptor(self->pDes); + } + free(self); +} +/*--------------------------------------------------------------------------*/ +static int SaveRestore(void *obj, char *name, FILE *fd){ + int status; + char buffer[1024]; + + pRestoreObj self = (pRestoreObj)obj; + if(self == NULL){ + return 0; + } + fprintf(fd,"\n#--- BEGIN (commands producing errors on last restore)\n"); + status = LLDnodePtr2First(self->errList); + while(status == 1){ + LLDstringData(self->errList,buffer); + fprintf(fd,"%s", buffer); + status = LLDnodePtr2Next(self->errList); + } + fprintf(fd,"#--- END (commands producing errors on last restore)\n\n"); + return 1; +} +/*---------------------------------------------------------------------------*/ +int InstallBckRestore(SConnection *pCon, SicsInterp *pSics){ + pRestoreObj pNew = NULL; + + pNew = malloc(sizeof(RestoreObj)); + if(pNew == NULL){ + SCWrite(pCon,"ERROR: no memory to create restore object! This is SERIOUS!!!", + eError); + return 0; + } + pNew->pDes = CreateDescriptor("BckRestore"); + pNew->errList = LLDstringCreate(); + if(pNew->pDes == NULL || pNew->errList < 0){ + SCWrite(pCon,"ERROR: no memory to create restore object! This is SERIOUS!!!", + eError); + return 0; + } + pNew->pDes->SaveStatus = SaveRestore; + AddCommand(pSics,"Backup",BackupStatus,NULL,NULL); + AddCommand(pSics,"Restore",RestoreStatus,killRestore,pNew); + + return 1; +} +/*-----------------------------------------------------------------------*/ +static int listRestoreErr(pRestoreObj self, SConnection *pCon){ + char buffer[1024]; + int status; + pDynString data = NULL; + + SCStartBuffering(pCon); + status = LLDnodePtr2First(self->errList); + while(status == 1){ + LLDstringData(self->errList,buffer); + SCWrite(pCon,buffer,eValue); + status = LLDnodePtr2Next(self->errList); + } + data = SCEndBuffering(pCon); + if(data != NULL){ + SCWrite(pCon,GetCharArray(data),eValue); + } + return 1; +} +/*-----------------------------------------------------------------------*/ + int RestoreStatus(SConnection *pCon, SicsInterp *pSics, void *pData, + int argc, char *argv[]) + { + char pBueffel[512]; + int iRights; + int iRet; + char *pFile = NULL; + writeFunc oldWrite; + pExeBuf buffi = NULL; + pRestoreObj self = (pRestoreObj)pData; + + assert(pSics); + assert(pCon); + assert(self != NULL); + StatusFileTask(NULL); /* do a save now, see also parameterChange = 0 at the end */ + + if(argc < 2) + { + pFile = IFindOption(pSICSOptions,"statusfile"); + if(pFile) + { + sprintf(pBueffel,"%s",pFile); + } + else + { + SCWrite(pCon,"ERROR: No filename given for backup, Aborted.", + eError); + return 0; + } + } + else + { + if(strcmp(argv[1],"listerr") == 0){ + return listRestoreErr(self,pCon); + } else { + sprintf(pBueffel,"%s",argv[1]); + } + } + + buffi = exeBufCreate("restore"); + if(buffi == NULL){ + SCWrite(pCon,"ERROR: failed to allocate buffer for restore",eError); + return 0; + } + iRet = exeBufLoad(buffi,pBueffel); + if(iRet != 1){ + exeBufDelete(buffi); + SCWrite(pCon,"ERROR: failed open status file",eError); + return 0; + } + LLDdeleteBlob(self->errList); + self->errList = LLDstringCreate(); + iRights = SCGetRights(pCon); + pCon->iUserRights = usInternal; + oldWrite = SCGetWriteFunc(pCon); + SCSetWriteFunc(pCon,SCNotWrite); + iRet = exeBufProcessErrList(buffi,pSics,pCon,self->errList); + restoreOccurred = 1; + SCSetWriteFunc(pCon,oldWrite); + pCon->iUserRights = iRights; + exeBufDelete(buffi); + /* + if we do not override parameterChange here, the backup file + would be overwritten after each restore... Not the right thing + to do! As we have called StatusFileTask before, we are sure not + to loose a change + */ + parameterChange = 0; + SCSendOK(pCon); + return iRet; + } +/*-----------------------------------------------------------------------*/ +void StatusFileDirty(void) { + parameterChange = 1; +} +/*-----------------------------------------------------------------------*/ +void StatusFileInit(void) { + TaskRegister(pServ->pTasker, StatusFileTask, NULL, NULL, NULL, 0); +} + diff --git a/statusfile.h b/statusfile.h new file mode 100644 index 00000000..39976d8b --- /dev/null +++ b/statusfile.h @@ -0,0 +1,24 @@ +/*-------------------------------------------------------------------------- + Status files + + Mark Koennecke, November 1996 + copyright: see implementation file +----------------------------------------------------------------------------*/ +#ifndef STATUSFILE_H +#define STATUSFILE_H + +#include + + int InstallBckRestore(SConnection *pCon, SicsInterp *pSics); + int BackupStatus(SConnection *pCon, SicsInterp *pSics, void *pData, + int argc, char *argv[]); + + int RestoreStatus(SConnection *pCon, SicsInterp *pSics, void *pData, + int argc, char *argv[]); + int hasRestored(); + + int StatusFileTask(void *data); /* saves status file if parameters have changed */ + + void StatusFileDirty(void); /* indicate that the status file has to be rewritten */ +#endif +