diff --git a/event.h b/event.h index 9142b2e2..4e6556e9 100644 --- a/event.h +++ b/event.h @@ -69,7 +69,7 @@ typedef struct { #define TOKENGRAB 302 #define TOKENRELEASE 303 #define COMLOG 304 -#define CRONLIST 305 +#define CRONFUNC 305 #line 131 "event.w" diff --git a/sicscron.c b/sicscron.c index 4b200736..da2ec5d3 100644 --- a/sicscron.c +++ b/sicscron.c @@ -9,6 +9,8 @@ Mark Koennecke, November 1999 modified to give more error output: Mark Koennecke, December 2010 + + added more functionality: list, stop, count ------------------------------------------------------------------------*/ #include #include @@ -29,9 +31,13 @@ typedef struct { Statistics *stat; } Cron, *pCron; +typedef enum {cron_list, cron_stop, cron_count} CronFunc; + typedef struct { SConnection *pCon; - int dolater; + CronFunc func; + char *cmd; + int cnt; } CronListData; /*------------------------------------------------------------------------*/ @@ -58,13 +64,12 @@ static void KillCron(void *pData) static int CronTask(void *pData) { Statistics *old; - pCron self = (pCron) pData; int iRet; Tcl_Interp *pTcl = pServ->pSics->pTcl; time_t now; - struct tm *nowtm; - char buffer[1024]; + struct tm tm; + char datim[24]; if (!self) { return 0; @@ -79,16 +84,12 @@ static int CronTask(void *pData) MacroPop(); if (iRet != TCL_OK) { if (strcmp(pTcl->result, "stop") == 0) { - snprintf(buffer,1024,"sicscron script '%s' stopped", - self->pCommand); - SCPrintf(self->pCon, eLogError, buffer); - WriteToCommandLog("SICSCRON:", buffer); + SCPrintf(self->pCon, eLogError, "sicscron script '%s' stopped", self->pCommand); self->iEnd = 0; return 0; } - snprintf(buffer,1024,"ERROR in sicscron script '%s': %s", self->pCommand, pTcl->result); - SCPrintf(self->pCon, eLogError,buffer); - WriteToCommandLog("SICSCRON:", buffer); + + SCPrintf(self->pCon, eLogError, "ERROR in sicscron script '%s': %s", self->pCommand, pTcl->result); } if (self->iEnd == 2) { /* dolater command */ self->iEnd = 0; @@ -98,10 +99,10 @@ static int CronTask(void *pData) if (now > self->tNext) self->tNext = now; } -/* printf("CronTask return: %d\n", self->iEnd > 0);*/ - if(self->iEnd <= 0){ - snprintf(buffer,1024,"crontask terminating at %s on %d", ctime(&now), self->iEnd); - WriteToCommandLog("SICSCRON", buffer); + if (self->iEnd <= 0) { + tm = *localtime(&now); + strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm); + SCPrintf(self->pCon, eLog, "crontask'%s' terminating at %s", self->pCommand, datim); } return self->iEnd > 0; } @@ -121,17 +122,30 @@ static void CronSignal(void *pData, int iID, void *pSigData) self->iEnd = 0; } } - if (iID == CRONLIST) { + if (iID == CRONFUNC) { data = pSigData; - if (self->iEnd == 2 && data->dolater) { + switch (data->func) { + case cron_list: tm = *localtime(&self->tNext); strftime(datim, sizeof datim, "%Y-%m-%d %T", &tm); - SCPrintf(data->pCon, eLog, "%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, eLog, "%s %8d %s", datim, - self->iInterval, self->pCommand); + if (self->iInterval < 0) { + SCPrintf(data->pCon, eLog, "%s %s", datim, self->pCommand); + } else { + SCPrintf(data->pCon, eLog, "%s %8d %s", datim, + self->iInterval, self->pCommand); + } + break; + case cron_stop: + if (strcmp(data->cmd, self->pCommand) == 0) { + self->iEnd = 0; + data->cnt++; + } + break; + case cron_count: + if (strcmp(data->cmd, self->pCommand) == 0) { + data->cnt++; + } + break; } } } @@ -150,22 +164,38 @@ int MakeCron(SConnection * pCon, SicsInterp * pSics, void *pData, 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"); - } + if (argc >= 2 && strcasecmp(argv[1], "list") == 0) { + data.func = cron_list; + SCPrintf(pCon, eLog, "Date Time Interval Command"); data.pCon = pCon; - TaskSignal(pServ->pTasker, CRONLIST, &data); + TaskSignal(pServ->pTasker, CRONFUNC, &data); + return 1; + } + + if (argc > 2 && strcasecmp(argv[1], "stop") == 0) { + data.func = cron_stop; + data.cmd = argv[2]; + data.cnt = 0; + TaskSignal(pServ->pTasker, CRONFUNC, &data); + SCPrintf(pCon, eValue, "%d cron jobs stopped", data.cnt); + return 1; + } + + if (argc > 2 && strcasecmp(argv[1], "count") == 0) { + data.func = cron_count; + data.cmd = argv[2]; + data.cnt = 0; + TaskSignal(pServ->pTasker, CRONFUNC, &data); + SCPrintf(pCon, eValue, "%d", data.cnt); return 1; } /* enough arguments? */ if (argc < 3) { SCPrintf(pCon, eError, "ERROR: usage: %s