#!/bin/sh # # This is a shell archive. To extract its contents, # execute this file with /bin/sh to create the file(s): # # trigger.README trigger.c # # This shell archive created: Tue Sep 17 10:16:01 EDT 2002 # echo "Extracting file trigger.README" sed -e 's/^X//' <<\SHAR_EOF > trigger.README Xtrigger.README X X Xtrigger.c Attaches to the end of a file and spawns processes whenever X a key string is added (e. g., a message log file for something X that doesn't have an alarm). X X XN.B. --- this is 'as is' only X X-- X--------------------------------------- XMadison Pruet XEnterprise Replication Product Development XIBM Informix Dynamic Server XDallas, Texas SHAR_EOF if [ `wc -c < trigger.README` -ne 383 ] then echo "Lengths do not match -- Bad Copy of trigger.README" fi echo "Extracting file trigger.c" sed -e 's/^X//' <<\SHAR_EOF > trigger.c X#include X#include X#include X X X#define PGSIZE 2048 X#define PERROR(x) {perror((x)); exit(1);} Xtypedef struct msg_trigger { X struct msg_trigger *next; X time_t last_time; X int count; X char *msg; X } msg_t; X Xchar errmsg[200]; Xchar *msgpath=NULL; Xchar *command=NULL; X X/* X**************************************************************************** X* * X* usage() * X* * X**************************************************************************** X*/ Xvoid Xusage() X{ X printf("\nsniffer -f file -c command trigger_string ...\n"); X printf("\tthis program will tail the end of a file\n"); X printf("\tand if a certain message string is added \n"); X printf("\twill trigger the execution of a command\n"); X} X X X/* X************************************************************************* X* * X* int get_next_msg_line(char *linebuffer) * X* * X* purpose: read the next line from the informix message * X* log file into linebuffer * X* * X* parms passed: char *linebuffer - buffer to hold next line * X* * X************************************************************************* X*/ X Xint Xget_next_msg_line(char *linebuffer) X{ static FILE *msgfd = NULL; X static char iobuffer[PGSIZE]; X static int amt_read; X static int last_byte_read; X static char *chpnt; X static char *endpnt; X X/*-------------------------------------------------------------------+ X** + X** First time logic - open the file and prime the buffers + X** + X**-------------------------------------------------------------------+ X*/ X X if (msgfd == NULL) X { if ((msgfd = fopen(msgpath,"r")) == NULL) X { PERROR(msgpath); X } X fseek(msgfd,-PGSIZE,SEEK_END); /* position at last page of file */ X last_byte_read = ftell(msgfd); /* find where I'm at */ X X amt_read = fread(iobuffer,sizeof(char),PGSIZE,msgfd); X last_byte_read += amt_read; /* update file position */ X endpnt = iobuffer+amt_read; /* point to end of data read */ X for (chpnt=iobuffer; /* find a carriage return */ X *chpnt != '\n' && chpnt < endpnt; chpnt++); } chpnt++; X while (*chpnt != '\n') X { X if (chpnt >= endpnt) /* time to read more from disk?*/ X { do /* yep */ X { int loc; X X fseek(msgfd, /* position into file */ X last_byte_read, X SEEK_SET); X loc = ftell(msgfd); /* save where I'm at */ X X amt_read = fread(iobuffer, /* read page size */ X sizeof(char), X PGSIZE,msgfd); X if (amt_read) /* did I ready anything? */ X { last_byte_read += amt_read; X chpnt = iobuffer; X endpnt = iobuffer+amt_read; X } X else X { sleep(1); /* Nope - time to sleep */ X } X } while (amt_read == 0); X } X *(linebuffer++) = *(chpnt++); /* move next character to buffer*/ X } X *linebuffer = '\0'; /* and make a string of it */ X return 0; X} X X X X X X/* X**************************************************** X* * X* main() * X* * X**************************************************** X*/ X Xint Xmain(int nparm, char *parm[]) X{ X int tnum; X FILE *pin; X char buffer[200]; X char cmd[80]; X msg_t *msg_head = NULL; X msg_t *msg_tail = NULL; X msg_t *tmsg; X X if (nparm == 1) X { usage(); X return 0; X } X X for (tnum = 1; tnum < nparm; tnum++) X { if (!strcmp(parm[tnum],"-f")) X { msgpath = parm[++tnum]; X } X else X if (!strcmp(parm[tnum],"-")) X { usage(); X return 0; X } X else X if (!strcmp(parm[tnum],"-c")) X { command = parm[++tnum]; X } X else X { tmsg = (msg_t*)malloc(sizeof(msg_t)); X if (tmsg == NULL) X { PERROR("can not allocate msg_t buffer"); X } X tmsg->next = NULL; X tmsg->last_time = 0; X tmsg->count = 3; X tmsg->msg = (char*) malloc(strlen(parm[tnum])+1); X if (tmsg->msg == NULL) X { PERROR("can not allocate message buffer"); X } X strcpy(tmsg->msg,parm[tnum]); X if (msg_head == NULL) X msg_head = tmsg; X else msg_tail->next = tmsg; X msg_tail = tmsg; X } X } X X if (!msg_head || !msgpath || !command) X { usage(); X return(1); X } X X X for (;;) X { get_next_msg_line(buffer); X for (tmsg = msg_head; tmsg != NULL; tmsg = tmsg->next) X { if ((tmsg->count) && strstr(buffer,tmsg->msg)) X { time_t current_time = time(NULL); X if ((current_time - tmsg->last_time) > 300) X { if (system(command) < 0) X { sprintf(errmsg,"system(%s)",command); X perror(errmsg); X } X tmsg->last_time = current_time; X tmsg->count--; X break; X } X } X } X } X} SHAR_EOF if [ `wc -c < trigger.c` -ne 6487 ] then echo "Lengths do not match -- Bad Copy of trigger.c" fi echo "Done." exit 0