PSI sics-cvs-psi-2008-10-02
This commit is contained in:
110
sicscron.c
110
sicscron.c
@@ -2,7 +2,7 @@
|
||||
S I C S C R O N
|
||||
|
||||
A cron like command facility for SICS which allows to repeat a command
|
||||
at given time intervalls.
|
||||
at given time intervals.
|
||||
|
||||
copyright: see copyright.h
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <tcl.h>
|
||||
#include "macro.h"
|
||||
#include "fortify.h"
|
||||
#include "sics.h"
|
||||
#include "splitter.h"
|
||||
@@ -19,13 +19,19 @@
|
||||
|
||||
|
||||
typedef struct {
|
||||
int iIntervall;
|
||||
int iInterval;
|
||||
time_t tNext;
|
||||
char *pCommand;
|
||||
SConnection *pCon;
|
||||
int iEnd;
|
||||
Statistics *stat;
|
||||
} Cron, *pCron;
|
||||
|
||||
typedef struct {
|
||||
SConnection *pCon;
|
||||
int dolater;
|
||||
} CronListData;
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
static void KillCron(void *pData)
|
||||
{
|
||||
@@ -43,66 +49,121 @@
|
||||
{
|
||||
SCDeleteConnection(self->pCon);
|
||||
}
|
||||
if (self->stat) {
|
||||
StatisticsKill(self->stat);
|
||||
}
|
||||
free(self);
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static int CronTask(void *pData)
|
||||
{
|
||||
Statistics *old;
|
||||
|
||||
pCron self = (pCron)pData;
|
||||
|
||||
int iRet;
|
||||
Tcl_Interp *pTcl = pServ->pSics->pTcl;
|
||||
time_t now;
|
||||
|
||||
if(!self)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(time(NULL) > self->tNext)
|
||||
now = time(NULL);
|
||||
if(now >= self->tNext)
|
||||
{
|
||||
SCInvoke(self->pCon,pServ->pSics,self->pCommand);
|
||||
self->tNext = time(NULL) + self->iIntervall;
|
||||
MacroPush(self->pCon);
|
||||
old=StatisticsBegin(self->stat);
|
||||
iRet = Tcl_Eval(pTcl,self->pCommand);
|
||||
StatisticsEnd(old);
|
||||
MacroPop();
|
||||
if (iRet != TCL_OK) {
|
||||
SCPrintf(self->pCon, eStatus,
|
||||
"ERROR in sicscron script: %s", pTcl->result);
|
||||
self->iEnd = 0;
|
||||
return 0;
|
||||
}
|
||||
if (self->iEnd == 2) { /* dolater command */
|
||||
self->iEnd = 0;
|
||||
return 0;
|
||||
}
|
||||
self->tNext += self->iInterval;
|
||||
if (now > self->tNext) self->tNext = now + 1;
|
||||
}
|
||||
return self->iEnd;
|
||||
return self->iEnd > 0;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static void CronSignal(void *pData, int iID, void *pSigData)
|
||||
{
|
||||
pCron self = (pCron)pData;
|
||||
int *iInt;
|
||||
|
||||
struct tm tm;
|
||||
char datim[24];
|
||||
CronListData *data;
|
||||
|
||||
if(iID == SICSINT)
|
||||
{
|
||||
iInt = (int *)pSigData;
|
||||
if(*iInt >= eEndServer)
|
||||
{
|
||||
{
|
||||
self->iEnd = 0;
|
||||
}
|
||||
}
|
||||
if(iID == CRONLIST) {
|
||||
data = pSigData;
|
||||
if (self->iEnd == 2 && data->dolater) {
|
||||
tm = *localtime(&self->tNext);
|
||||
strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm);
|
||||
SCPrintf(data->pCon, eStatus, "%s %s", datim,
|
||||
self->pCommand);
|
||||
} else if (self->iEnd == 1 && !data->dolater) {
|
||||
tm = *localtime(&self->tNext);
|
||||
strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm);
|
||||
SCPrintf(data->pCon, eStatus, "%s %8d %s", datim,
|
||||
self->iInterval, self->pCommand);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int MakeCron(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
pCron pNew = NULL;
|
||||
int iVal, iRet;
|
||||
int iVal, iRet, rights;
|
||||
char *cmd;
|
||||
CronListData data;
|
||||
|
||||
/* only managers may do this */
|
||||
if(!SCMatchRights(pCon,usMugger))
|
||||
/* need user priv. */
|
||||
if(!SCMatchRights(pCon,usUser))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (argc == 2 && strcasecmp(argv[1], "list") == 0) {
|
||||
if (strcasecmp(argv[0], "dolater") == 0) {
|
||||
data.dolater = 1;
|
||||
SCPrintf(pCon, eError, "Date Time Command");
|
||||
} else {
|
||||
data.dolater = 0;
|
||||
SCPrintf(pCon, eError, "Date Time Interval Command");
|
||||
}
|
||||
data.pCon = pCon;
|
||||
TaskSignal(pServ->pTasker, CRONLIST, &data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* enough arguments? */
|
||||
if(argc < 3)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: not enough arguments to sicscron",eError);
|
||||
SCPrintf(pCon,eError,"ERROR: not enough arguments to %s", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* interpret first argument as intervall in seconds */
|
||||
/* interpret first argument as interval in seconds */
|
||||
iRet = Tcl_GetInt(pSics->pTcl,argv[1],&iVal);
|
||||
if(iRet != TCL_OK)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: failed to convert intervall argument to int",
|
||||
SCWrite(pCon,"ERROR: failed to convert interval argument to int",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
@@ -123,10 +184,21 @@
|
||||
SCWrite(pCon,"ERROR: out of memory in sicscron",eError);
|
||||
return 0;
|
||||
}
|
||||
pNew->iIntervall = iVal;
|
||||
rights = SCGetRights(pCon);
|
||||
if (rights > usMugger) {
|
||||
/* transfer the rights to the dummy connection */
|
||||
SCSetRights(pNew->pCon, rights);
|
||||
}
|
||||
pNew->pCommand = cmd;
|
||||
pNew->tNext = 0;
|
||||
pNew->iEnd = 1;
|
||||
pNew->tNext = time(NULL) + iVal;
|
||||
if (strcasecmp(argv[0], "dolater") == 0) {
|
||||
pNew->iInterval = -1;
|
||||
pNew->iEnd = 2;
|
||||
} else {
|
||||
pNew->iInterval = iVal;
|
||||
pNew->iEnd = 1;
|
||||
}
|
||||
pNew->stat = StatisticsNew(cmd);
|
||||
|
||||
TaskRegister(pServ->pTasker,
|
||||
CronTask,
|
||||
|
||||
Reference in New Issue
Block a user