/* * wrap.c: a way to wrap another NSAPI program with a log * * * Rob Crittenden * * How to install this program: * * Add the following lines to your obj.conf: * * Init fn=load-modules shlib= funcs=wrap_init * Init fn=wrap_init function= * */ #include "nsapi.h" static CRITICAL wrap_lock; static FuncPtr wrap_function; int wentin, cameout; char *function2wrap; /* Forward declarations */ static int wrap(pblock *pb, Session *sn, Request *rq); NSAPI_PUBLIC void clean_up(void *parameter); /* * wrap_init() * * Initialization function. * * */ NSAPI_PUBLIC int wrap_init(pblock *pb, Session *sn, Request *rq) { function2wrap = pblock_findval("function", pb); wrap_lock = crit_init(); daemon_atrestart(clean_up, NULL); wentin = 0; cameout = 0; ereport(LOG_INFORM, " replacing %s", function2wrap); wrap_function = func_replace(function2wrap, wrap); if (!wrap_function) { ereport(LOG_FAILURE, "wrap: failed to replace %s function", function2wrap); return REQ_ABORTED; } return REQ_PROCEED; } /* * wrap() * * Print messages around the actual call, and return the value from the call * */ static int wrap(pblock *pb, Session *sn, Request *rq) { int value; char *request; /* increment the counter within a thread-safe lock */ crit_enter(wrap_lock); wentin++; crit_exit(wrap_lock); request = pblock_findval("clf-request", rq->reqpb); log_error(LOG_INFORM, "wrap", sn, rq, " %s entered %s", function2wrap, request); /* call the wrapped function */ value = wrap_function(pb,sn,rq); /* increment the counter within a thread-safe lock */ crit_enter(wrap_lock); cameout++; crit_exit(wrap_lock); log_error(LOG_INFORM, "wrap", sn, rq, "%s returned from %s, Entered %d times, Exited %d times", function2wrap, request, wentin, cameout); FREE(request); return(value); } /* * clean_up() * * this is run when the server is stopped * */ NSAPI_PUBLIC void clean_up(void* parameter) { ereport(LOG_INFORM, "Process ending with %s entered %d times and exited %d times", function2wrap, wentin, cameout); FREE(function2wrap); FREE(wrap_lock); }