- split status.c into status.c and statusfile.c

- improvements for scriptcontext.c
This commit is contained in:
zolliker
2008-02-13 13:07:30 +00:00
parent 8ebd3b2772
commit 8cf738869d
13 changed files with 333 additions and 277 deletions

View File

@ -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)

View File

@ -58,7 +58,6 @@ typedef int (*writeFunc)(struct __SConnection *pCon,
int iUserRights;
int inUse;
int iGrab; /* grab flag for token*/
int parameterChange;
int sicsError;
/*

View File

@ -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\

View File

@ -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"

2
ofac.c
View File

@ -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 */
}

View File

@ -1,9 +1,9 @@
#include <stdio.h>
#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);
}

View File

@ -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;

View File

@ -24,6 +24,7 @@
#include <lld.h>
#include <stptok.h>
#include "protocol.h"
#include "statusfile.h"
#include <splitter.h>
#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;
}

View File

@ -9,4 +9,4 @@
MFLAGS=-f makefile_linux$(DUMMY)
HDFROOT=/afs/psi.ch/project/sinq/sl5
HDFROOT=/afs/psi.ch/project/sinq/sl-linux

215
status.c
View File

@ -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;
}

View File

@ -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

290
statusfile.c Normal file
View File

@ -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 <stdlib.h>
#include <assert.h>
#include <time.h>
#include "fortify.h"
#include <string.h>
#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);
}

24
statusfile.h Normal file
View File

@ -0,0 +1,24 @@
/*--------------------------------------------------------------------------
Status files
Mark Koennecke, November 1996
copyright: see implementation file
----------------------------------------------------------------------------*/
#ifndef STATUSFILE_H
#define STATUSFILE_H
#include <sics.h>
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