SRL Archive - Research & Development Lounge

[Utility] Basic Watch dog timer


slushpuppy

2012-04-26 11:33:58

This .dll is useful if your script enters a function or procedure that blocks indefinitely, preventing your main loop from ever being reached. This plugin is most useful with CRASH SMART


functions:


procedure setTimeOutInMS(timeout,loopintervals : Integer);

Set the timeout and loop intervals(in milliseconds)

function keepAlive() : Boolean;

Place this function in your main loop.




E.G. Simba code






program new;
{$DEFINE SRL5}
{$i SRL/srl.simba}
{$loadlib watchDog.dll}

begin

setTimeOutInMS(5000,1000);//this program will run for 5 seconds, with 1 second loop interval
keepAlive();
repeat
sleep(900000000);
until(false);
end.



Source:
#include <stdlib.h>
#include <windows.h>
#include <WinBase.h>
#include <process.h>
#include <time.h>
#define DllExport __declspec( dllexport ) __stdcall


static time_t lastPing = 0;
static int maxtime = 300;
static long interval = 500;
static HANDLE thread;

void __stdcall killProc(DWORD pid) {

HANDLE proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE,pid);
if (proc) {
TerminateProcess(proc,0);
CloseHandle(proc);
}
}
void threadProc(void *param) {
while (1) {
time_t tmp = time (NULL);
if (lastPing != 0 && (tmp - lastPing) >= maxtime) {
killProc(GetCurrentProcessId());
_endthread();
}
Sleep(interval);
}
}
static int __stdcall keepAlive() {
if (thread == NULL) {
thread = (HANDLE) _beginthread( threadProc,0,NULL);
}
if ((long) thread == 1L) {
return 0;
}
lastPing = time (NULL);
return 1;
}
static void __stdcall setTimeOutInMS(int timeout,int waitInterval) {
maxtime = timeout / 1000;
interval = waitInterval;
}
DllExport int GetFuncCount(void) {
return 2;
}

DllExport int GetFuncInfo(int x, void **addr, char **def) {
switch (x) {
case 0:
*addr = (void *)&keepAlive;
strcpy(*def, "function keepAlive() : Boolean;");
break;
case 1:
*addr = (void *)&setTimeOutInMS;
strcpy(*def, "procedure setTimeOutInMS(timeout,waitInterval : Integer);");
break;
default:
x = -1;
}
return x;
}

//static __stdcall int a(void);
//static __stdcall int GetFuncCount(void);
//static __stdcall int GetFuncInfo(int x, void **addr, char **def);

__declspec(dllexport) int GetFunctionCount(void) __attribute__((alias("GetFuncCount@0")));
__declspec(dllexport) int GetFunctionInfo(int x, void **addr, char **def) __attribute__((alias("GetFuncInfo@12")));

LordJashin

2012-06-29 16:41:14

What does each one do exactly? Looks like one has a kill process in it.