From 3fdecfb0d8dcd1faccd9dcd944fa2474a878e3a7 Mon Sep 17 00:00:00 2001 From: erickson Date: Mon, 12 Sep 2005 13:33:26 +0000 Subject: [PATCH] added the md5 server (opensrf.version) some memory debugging slight api change (OSRF_METHOD_VERIFY_CONTEXT no longer creates local vars) added a basic 'application' level logging numerous small changes git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@534 9efc2488-bf62-4759-914b-345cdb29e865 --- src/Makefile | 4 +- src/c-apps/Makefile | 9 +++- src/c-apps/osrf_dbmath.c | 20 +++++---- src/c-apps/osrf_math.c | 21 +++++---- src/c-apps/osrf_version.c | 98 +++++++++++++++++++++++++++++++++++++++++ src/libstack/Makefile | 5 ++- src/libstack/osrf_app_session.c | 40 ++++++----------- src/libstack/osrf_app_session.h | 1 + src/libstack/osrf_application.h | 38 +++++++++------- src/libstack/osrf_cache.c | 19 +++++--- src/libstack/osrf_cache.h | 6 +-- src/libstack/osrf_log.c | 64 +++++++++++++++++++++++++++ src/libstack/osrf_log.h | 9 ++++ src/libstack/osrf_prefork.c | 5 ++- src/libstack/osrf_stack.c | 4 +- src/libstack/osrf_system.c | 1 - src/objson/Makefile | 8 +++- src/srfsh/srfsh.c | 29 ------------ src/srfsh/srfsh.h | 2 - src/utils/Makefile | 4 +- src/utils/utils.c | 30 +++++++++++++ src/utils/utils.h | 26 +++++++++++ 22 files changed, 333 insertions(+), 110 deletions(-) create mode 100644 src/c-apps/osrf_version.c create mode 100644 src/libstack/osrf_log.c create mode 100644 src/libstack/osrf_log.h diff --git a/src/Makefile b/src/Makefile index e0fe5d9..41a493d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,7 @@ export PERLDIR = $(LIBDIR)/perl5 export INCLUDEDIR = $(PREFIX)/include export LDLIBS += -export LDFLAGS += -L $(TMPDIR) -L . +export LDFLAGS += -L $(TMPDIR) -L . -L /opt/lib export CFLAGS += -g -Wall -O2 -fPIC -I$(LIBXML2_HEADERS) -I$(APACHE2_HEADERS) \ -I$(LIBXML2_HEADERS)/libxml -I$(TMP) -I$(TMPDIR) @@ -28,6 +28,7 @@ OPENSRF_TARGETS = libtransport/transport_session.o \ libstack/osrf_application.o \ libstack/osrf_cache.o \ libstack/xml_utils.o \ + libstack/osrf_log.o \ utils/socket_bundle.o \ utils/string_array.o \ utils/utils.o \ @@ -48,6 +49,7 @@ OPENSRF_HEADERS = libtransport/transport_session.h \ libstack/osrf_application.h \ libstack/osrf_cache.h \ libstack/xml_utils.h \ + libstack/osrf_log.h \ utils/socket_bundle.h \ utils/string_array.h \ utils/utils.h \ diff --git a/src/c-apps/Makefile b/src/c-apps/Makefile index 62301e7..ceda792 100644 --- a/src/c-apps/Makefile +++ b/src/c-apps/Makefile @@ -1,10 +1,11 @@ LDLIBS += -lobjson -lopensrf +CFLAGS += -DOSRF_LOG_PARAMS - -all: osrf_math.so osrf_dbmath.so +all: osrf_math.so osrf_dbmath.so osrf_version.so osrf_math.o: osrf_math.c osrf_dbmath.o: osrf_dbmath.c +osrf_version.o: osrf_version.c osrf_math.so: osrf_math.o $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_math.o -o $(TMPDIR)/osrf_math.so @@ -12,9 +13,13 @@ osrf_math.so: osrf_math.o osrf_dbmath.so: osrf_dbmath.o $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_dbmath.o -o $(TMPDIR)/osrf_dbmath.so +osrf_version.so: osrf_version.o + $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) osrf_version.o -o $(TMPDIR)/osrf_version.so + install: cp $(TMPDIR)/osrf_math.so $(LIBDIR)/ cp $(TMPDIR)/osrf_dbmath.so $(LIBDIR)/ + cp $(TMPDIR)/osrf_version.so $(LIBDIR)/ clean: rm -f *.o *.so diff --git a/src/c-apps/osrf_dbmath.c b/src/c-apps/osrf_dbmath.c index 92ce665..07cf0f6 100644 --- a/src/c-apps/osrf_dbmath.c +++ b/src/c-apps/osrf_dbmath.c @@ -1,6 +1,7 @@ #include "opensrf/osrf_app_session.h" #include "opensrf/osrf_application.h" #include "objson/object.h" +#include "opensrf/osrf_log.h" int osrfAppInitialize(); int osrfAppChildInit(); @@ -8,6 +9,7 @@ int osrfMathRun( osrfMethodContext* ); int osrfAppInitialize() { + osrfLogInit("opensrf.dbmath"); osrfAppRegisterMethod( "opensrf.dbmath", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 ); osrfAppRegisterMethod( "opensrf.dbmath", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 ); osrfAppRegisterMethod( "opensrf.dbmath", "mult", "osrfMathRun", "send 2 numbers and I'll multiply them", 2 ); @@ -19,12 +21,12 @@ int osrfAppChildInit() { return 0; } -int osrfMathRun( osrfMethodContext* d ) { +int osrfMathRun( osrfMethodContext* ctx ) { - OSRF_METHOD_VERIFY_CONTEXT(d); + OSRF_METHOD_VERIFY_CONTEXT(ctx); - jsonObject* x = jsonObjectGetIndex(params, 0); - jsonObject* y = jsonObjectGetIndex(params, 1); + jsonObject* x = jsonObjectGetIndex(ctx->params, 0); + jsonObject* y = jsonObjectGetIndex(ctx->params, 1); if( x && y ) { @@ -37,13 +39,13 @@ int osrfMathRun( osrfMethodContext* d ) { double j = strtod(b, NULL); double r = 0; - if(!strcmp(method->name, "add")) r = i + j; - if(!strcmp(method->name, "sub")) r = i - j; - if(!strcmp(method->name, "mult")) r = i * j; - if(!strcmp(method->name, "div")) r = i / j; + if(!strcmp(ctx->method->name, "add")) r = i + j; + if(!strcmp(ctx->method->name, "sub")) r = i - j; + if(!strcmp(ctx->method->name, "mult")) r = i * j; + if(!strcmp(ctx->method->name, "div")) r = i / j; jsonObject* resp = jsonNewNumberObject(r); - osrfAppRequestRespondComplete( session, request, resp ); + osrfAppRequestRespondComplete( ctx->session, ctx->request, resp ); jsonObjectFree(resp); free(a); free(b); diff --git a/src/c-apps/osrf_math.c b/src/c-apps/osrf_math.c index b074f17..9cb22aa 100644 --- a/src/c-apps/osrf_math.c +++ b/src/c-apps/osrf_math.c @@ -1,6 +1,7 @@ #include "opensrf/osrf_app_session.h" #include "opensrf/osrf_application.h" #include "objson/object.h" +#include "opensrf/osrf_log.h" int osrfAppInitialize(); int osrfAppChildInit(); @@ -8,7 +9,7 @@ int osrfMathRun( osrfMethodContext* ); int osrfAppInitialize() { - + osrfLogInit("opensrf.math"); /* tell the server about the methods we handle */ osrfAppRegisterMethod( "opensrf.math", "add", "osrfMathRun", "send 2 numbers and I'll add them", 2 ); osrfAppRegisterMethod( "opensrf.math", "sub", "osrfMathRun", "send 2 numbers and I'll divide them", 2 ); @@ -21,13 +22,15 @@ int osrfAppChildInit() { return 0; } -int osrfMathRun( osrfMethodContext* c ) { +int osrfMathRun( osrfMethodContext* ctx ) { + + OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */ - OSRF_METHOD_VERIFY_CONTEXT(c); /* see osrf_application.h */ + osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name ); /* collect the request params */ - jsonObject* x = jsonObjectGetIndex(params, 0); - jsonObject* y = jsonObjectGetIndex(params, 1); + jsonObject* x = jsonObjectGetIndex(ctx->params, 0); + jsonObject* y = jsonObjectGetIndex(ctx->params, 1); if( x && y ) { @@ -46,16 +49,18 @@ int osrfMathRun( osrfMethodContext* c ) { osrfAppSession* ses = osrfAppSessionClientInit("opensrf.dbmath"); /* dbmath uses the same method names that math does */ - int req_id = osrfAppSessionMakeRequest( ses, newParams, method->name, 1, NULL ); + int req_id = osrfAppSessionMakeRequest( ses, newParams, ctx->method->name, 1, NULL ); osrfMessage* omsg = osrfAppSessionRequestRecv( ses, req_id, 60 ); if(omsg) { - /* return dbmath's response to the user */ - osrfAppRequestRespondComplete( session, request, osrfMessageGetResult(omsg) ); + osrfAppRequestRespondComplete( ctx->session, ctx->request, osrfMessageGetResult(omsg) ); osrfMessageFree(omsg); + osrfAppSessionFree(ses); return 0; } + + osrfAppSessionFree(ses); } } diff --git a/src/c-apps/osrf_version.c b/src/c-apps/osrf_version.c new file mode 100644 index 0000000..6785315 --- /dev/null +++ b/src/c-apps/osrf_version.c @@ -0,0 +1,98 @@ +#include "opensrf/osrf_app_session.h" +#include "opensrf/osrf_application.h" +#include "objson/object.h" +#include "opensrf/utils.h" +#include "opensrf/osrf_log.h" + +#define OSRF_VERSION_CACHE_TIME 300 + +int osrfAppInitialize(); +int osrfAppChildInit(); +int osrfVersion( osrfMethodContext* ); + + +int osrfAppInitialize() { + osrfLogInit("opensrf.version"); + osrfAppRegisterMethod( + "opensrf.version", + "opensrf.version.verify", + "osrfVersion", + "Send the service, method, and params as paramters to this method " + "The data for this service/method/params combination will be retrieved " + "from the necessary server and the MD5 sum of the total values received " + "will be returned", 2 ); + + return 0; +} + +int osrfAppChildInit() { + return 0; +} + +int osrfVersion( osrfMethodContext* ctx ) { + + OSRF_METHOD_VERIFY_CONTEXT(ctx); + + /* First, see if the data is in the cache */ + char* json = jsonObjectToJSON(ctx->params); + char* paramsmd5 = md5sum(json); + char* cachedmd5 = osrfCacheGetString(paramsmd5); + free(json); + + if( cachedmd5 ) { + osrfLog( OSRF_DEBUG, "Found %s object in cache, returning....", cachedmd5 ); + jsonObject* resp = jsonNewObject(cachedmd5); + osrfAppRequestRespondComplete( ctx->session, ctx->request, resp ); + jsonObjectFree(resp); + free(paramsmd5); + free(cachedmd5); + return 0; + } + + jsonObject* serv = jsonObjectGetIndex(ctx->params, 0); + jsonObject* meth = jsonObjectGetIndex(ctx->params, 1); + char* service = jsonObjectGetString(serv); + char* methd = jsonObjectGetString(meth); + + if( service && methd ) { + /* shove the additional params into an array */ + jsonObject* tmpArray = jsonNewObject(NULL); + int i; + for( i = 2; i != ctx->params->size; i++ ) + jsonObjectPush( tmpArray, jsonObjectClone(jsonObjectGetIndex(ctx->params, i))); + + osrfAppSession* ses = osrfAppSessionClientInit(service); + int reqid = osrfAppSessionMakeRequest( ses, tmpArray, methd, 1, NULL ); + osrfMessage* omsg = osrfAppSessionRequestRecv( ses, reqid, 60 ); + jsonObjectFree(tmpArray); + + if( omsg ) { + + jsonObject* result = osrfMessageGetResult( omsg ); + char* resultjson = jsonObjectToJSON(result); + char* resultmd5 = md5sum(resultjson); + free(resultjson); + osrfMessageFree(omsg); + + if( resultmd5 ) { + jsonObject* resp = jsonNewObject(resultmd5); + osrfAppRequestRespondComplete( ctx->session, ctx->request, resp ); + jsonObjectFree(resp); + osrfAppSessionFree(ses); + osrfLog(OSRF_DEBUG, "Found version string %s, caching and returning...", resultmd5 ); + osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME ); + free(resultmd5); + free(paramsmd5); + return 0; + } + } + osrfAppSessionFree(ses); + } + + free(paramsmd5); + + return -1; +} + + + diff --git a/src/libstack/Makefile b/src/libstack/Makefile index 2059027..a6e5c00 100644 --- a/src/libstack/Makefile +++ b/src/libstack/Makefile @@ -1,5 +1,5 @@ -CFLAGS += -DASSUME_STATELESS -rdynamic -fno-strict-aliasing +CFLAGS += -DASSUME_STATELESS -rdynamic -fno-strict-aliasing LDLIBS += -lxml2 -lobjson -ldl -lmemcache TARGETS = osrf_message.o \ @@ -11,6 +11,7 @@ TARGETS = osrf_message.o \ osrfConfig.o \ osrf_application.o \ osrf_cache.o \ + osrf_log.o \ xml_utils.o HEADERS = osrf_message.h \ @@ -22,6 +23,7 @@ HEADERS = osrf_message.h \ osrfConfig.h \ osrf_application.h \ osrf_cache.h \ + osrf_log.h \ xml_utils.h all: xml_utils.o $(TARGETS) copy @@ -43,6 +45,7 @@ osrf_prefork.o: osrf_prefork.c osrf_prefork.h osrfConfig.o: osrfConfig.c osrfConfig.h xml_utils.o osrf_application.o: osrf_application.c osrf_application.h osrf_cache.o: osrf_cache.c osrf_cache.h +osrf_log.o: osrf_log.c osrf_log.h clean: /bin/rm -f *.o libopensrf_stack.so xml_utils.h xml_utils.c diff --git a/src/libstack/osrf_app_session.c b/src/libstack/osrf_app_session.c index 131b315..559053a 100644 --- a/src/libstack/osrf_app_session.c +++ b/src/libstack/osrf_app_session.c @@ -47,13 +47,12 @@ void _osrf_app_request_free( osrf_app_request * req ){ */ free( req ); - debug_handler("after request free"); } /** Pushes the given message onto the list of 'responses' to this request */ void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){ if(req == NULL || result == NULL) return; - debug_handler( "App Session pushing [%d] onto request queue", result->thread_trace ); + debug_handler( "App Session pushing request [%d] onto request queue", result->thread_trace ); if(req->result == NULL) req->result = result; else { @@ -67,7 +66,6 @@ void osrf_app_session_request_finish( osrf_app_session* session, int req_id ){ if(session == NULL) return; - debug_handler("Finishing request %d", req_id ); osrf_app_request* req = _osrf_app_session_get_request( session, req_id ); if(req == NULL) return; _osrf_app_session_remove_request( req->session, req ); @@ -77,7 +75,7 @@ void osrf_app_session_request_finish( void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) { if(session == NULL) return; - debug_handler("Resetting timeout %d", req_id ); + debug_handler("Resetting request timeout %d", req_id ); osrf_app_request* req = _osrf_app_session_get_request( session, req_id ); if(req == NULL) return; req->reset_timeout = 1; @@ -93,7 +91,6 @@ osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) { if(req == NULL) return NULL; if( req->result != NULL ) { - debug_handler("app_request receive already has a message, returning it"); /* pop off the first message in the list */ osrf_message* tmp_msg = req->result; req->result = req->result->next; @@ -167,7 +164,6 @@ int _osrf_app_request_resend( osrf_app_request* req ) { /** returns a session from the global session hash */ osrf_app_session* osrf_app_session_find_session( char* session_id ) { osrf_app_session* ptr = app_session_cache; - debug_handler("Searching for session in global cache with id [%s]", session_id ); while( ptr != NULL ) { if( !strcmp(ptr->session_id,session_id) ) return ptr; @@ -186,8 +182,6 @@ void _osrf_app_session_push_session( osrf_app_session* session ) { } osrf_app_session* ptr = app_session_cache; - debug_handler( "Pushing [%s] [%s] onto global session cache", - session->remote_service, session->session_id ); while( ptr != NULL ) { if( !strcmp(ptr->session_id, session->session_id) ) return; @@ -271,10 +265,10 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { #ifdef ASSUME_STATELESS session->stateless = 1; - debug_handler("session is stateless"); + debug_handler("%s session is stateless", remote_service ); #else session->stateless = 0; - debug_handler("session is NOT stateless"); + debug_handler("%s session is NOT stateless", remote_service ); #endif /* build a chunky, random session id */ @@ -473,7 +467,6 @@ osrf_app_request* _osrf_app_session_get_request( if(session == NULL) return NULL; - debug_handler( "App Session searching for request [%d] in request queue",request_id ); osrf_app_request* req = session->request_queue; while( req != NULL ) { if(req->request_id == request_id) @@ -512,19 +505,12 @@ int osrf_app_session_push_queue( if(session == NULL || msg == NULL) return 0; - debug_handler( "AppSession pushing result for [%d] onto request payload queue", - msg->thread_trace ); - osrf_app_request* req = session->request_queue; - if(req == NULL) { - warning_handler( "app_session has no app_requests in its queue yet we have a result for [%d]", msg->thread_trace ); - return 0; - } + if(req == NULL) return 0; while( req != NULL ) { if(req->request_id == msg->thread_trace) { - debug_handler( "Found app_request for tt [%d]", msg->thread_trace ); _osrf_app_request_push_queue( req, msg ); return 1; } @@ -542,7 +528,6 @@ int osrf_app_session_connect(osrf_app_session* session){ return 0; if(session->state == OSRF_SESSION_CONNECTED) { - debug_handler("Already Connected, returning"); return 1; } @@ -567,7 +552,7 @@ int osrf_app_session_connect(osrf_app_session* session){ } if(session->state == OSRF_SESSION_CONNECTED) - debug_handler(" * Connected Successfully"); + debug_handler(" * Connected Successfully to %s", session->remote_service ); if(session->state != OSRF_SESSION_CONNECTED) return 0; @@ -604,7 +589,6 @@ int osrf_app_session_disconnect( osrf_app_session* session){ } int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) { - debug_handler("In reqeust resend searching for resend-able messages"); osrf_app_request* req = _osrf_app_session_get_request( session, req_id ); return _osrf_app_request_resend( req ); } @@ -644,7 +628,7 @@ int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int transport_message* t_msg = message_init( string, "", session->session_id, session->remote_id, NULL ); - debug_handler("Session [%s] [%s] sending to %s \nXML:\n%s", + debug_handler("Session [%s] [%s] sending to %s \nData: %s", session->remote_service, session->session_id, t_msg->recipient, string ); retval = client_send_message( session->transport_handle, t_msg ); @@ -686,7 +670,12 @@ int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){ /** Disconnects (if client) and removes the given session from the global session cache * ! This free's all attached app_requests ! */ -void osrf_app_session_destroy ( osrf_app_session* session ){ +void osrfAppSessionFree( osrfAppSession* ses ) { + osrf_app_session_destroy( ses ); +} + + +void osrf_app_session_destroy( osrf_app_session* session ){ if(session == NULL) return; debug_handler( "AppSession [%s] [%s] destroying self and deleting requests", @@ -699,8 +688,6 @@ void osrf_app_session_destroy ( osrf_app_session* session ){ } //session->state = OSRF_SESSION_DISCONNECTED; _osrf_app_session_remove_session(session->session_id); - debug_handler("AppSession [%s] [%s] removed from cache", - session->remote_service, session->session_id ); osrf_app_request* req; while( session->request_queue != NULL ) { @@ -720,7 +707,6 @@ osrf_message* osrf_app_session_request_recv( osrf_app_session* session, int req_id, int timeout ) { if(req_id < 0 || session == NULL) return NULL; - debug_handler("somebody callled recv"); osrf_app_request* req = _osrf_app_session_get_request( session, req_id ); return _osrf_app_request_recv( req, timeout ); } diff --git a/src/libstack/osrf_app_session.h b/src/libstack/osrf_app_session.h index df74979..84a7c09 100644 --- a/src/libstack/osrf_app_session.h +++ b/src/libstack/osrf_app_session.h @@ -162,6 +162,7 @@ int osrf_app_session_queue_wait( osrf_app_session*, int timeout ); * session is completey done. */ void osrf_app_session_destroy ( osrf_app_session* ); +void osrfAppSessionFree( osrfAppSession* ); diff --git a/src/libstack/osrf_application.h b/src/libstack/osrf_application.h index da6d2e8..2921952 100644 --- a/src/libstack/osrf_application.h +++ b/src/libstack/osrf_application.h @@ -8,25 +8,31 @@ /** This macro verifies methods receive the correct parameters - It also creates local variables "session", "method", - "params", and "request" */ -#define OSRF_METHOD_VERIFY_CONTEXT(__d) \ - if(!__d) return -1; \ - \ - osrfAppSession* session = __d->session; \ - osrfMethod* method = __d->method; \ - jsonObject* params = __d->params; \ - int request = __d->request; \ - \ - if( !(session && method && params) ) return -1; \ - if( !params->type == JSON_ARRAY ) return -1; \ - if( !method->name ) return -1; \ - \ - char* __j = jsonObjectToJSON(params);\ +#define _OSRF_METHOD_VERIFY_CONTEXT(d) \ + if(!d) return -1; \ + if(!d->session) { osrfLog( OSRF_ERROR, "Session is NULL in app reqeust" ); return -1; }\ + if(!d->method) { osrfLog( OSRF_ERROR, "Method is NULL in app reqeust" ); return -1; }\ + if(!d->params) { osrfLog( OSRF_ERROR, "Params is NULL in app reqeust %s", d->method->name ); return -1; }\ + if( d->params->type != JSON_ARRAY ) { \ + osrfLog( OSRF_ERROR, "'params' is not a JSON array for method %s", d->method->name);\ + return -1; }\ + if( !d->method->name ) { osrfLog(OSRF_ERROR, "Method name is NULL"); return -1; } + + +#ifdef OSRF_LOG_PARAMS +#define OSRF_METHOD_VERIFY_CONTEXT(d) \ + _OSRF_METHOD_VERIFY_CONTEXT(d); \ + char* __j = jsonObjectToJSON(d->params);\ if(__j) { \ - debug_handler("Service: %s | Params: %s", session->remote_service, __j);free(__j);} + osrfLog( OSRF_DEBUG, "[%s:%s] params: %s", d->session->remote_service, d->method->name, __j);\ + free(__j); \ + } +#else +#define OSRF_METHOD_VERIFY_CONTEXT(d) _OSRF_METHOD_VERIFY_CONTEXT(d); +#endif + diff --git a/src/libstack/osrf_cache.c b/src/libstack/osrf_cache.c index 2e77c40..f676419 100644 --- a/src/libstack/osrf_cache.c +++ b/src/libstack/osrf_cache.c @@ -47,10 +47,11 @@ int osrfCachePutString( char* key, const char* value, time_t seconds ) { return 0; } -jsonObject* osrfCacheGetObject( char* key ) { +jsonObject* osrfCacheGetObject( char* key, ... ) { jsonObject* obj = NULL; if( key ) { - char* data = (char*) mc_aget( __osrfCache, key, strlen(key) ); + VA_LIST_TO_STRING(key); + char* data = (char*) mc_aget( __osrfCache, VA_BUF, strlen(VA_BUF) ); if( data ) { obj = jsonParseString( data ); return obj; @@ -59,14 +60,20 @@ jsonObject* osrfCacheGetObject( char* key ) { return NULL; } -char* osrfCacheGetString( char* key ) { - if( key ) return (char*) mc_aget(__osrfCache, key, strlen(key) ); +char* osrfCacheGetString( char* key, ... ) { + if( key ) { + VA_LIST_TO_STRING(key); + return (char*) mc_aget(__osrfCache, VA_BUF, strlen(VA_BUF) ); + } return NULL; } -int osrfCacheRemove( char* key ) { - if( key ) return mc_delete(__osrfCache, key, strlen(key), 0 ); +int osrfCacheRemove( char* key, ... ) { + if( key ) { + VA_LIST_TO_STRING(key); + return mc_delete(__osrfCache, VA_BUF, strlen(VA_BUF), 0 ); + } return -1; } diff --git a/src/libstack/osrf_cache.h b/src/libstack/osrf_cache.h index d81a6f0..664036a 100644 --- a/src/libstack/osrf_cache.h +++ b/src/libstack/osrf_cache.h @@ -58,20 +58,20 @@ int osrfCachePutString( char* key, const char* value, time_t seconds); @param key The cache key @return The object (which must be freed) if it exists, otherwise returns NULL */ -jsonObject* osrfCacheGetObject( char* key ); +jsonObject* osrfCacheGetObject( char* key, ... ); /** Grabs a string from the cache. @param key The cache key @return The string (which must be freed) if it exists, otherwise returns NULL */ -char* osrfCacheGetString( char* key ); +char* osrfCacheGetString( char* key, ... ); /** Removes the item with the given key from the cache. @return 0 on success, -1 on error. */ -int osrfCacheRemove( char* key ); +int osrfCacheRemove( char* key, ... ); diff --git a/src/libstack/osrf_log.c b/src/libstack/osrf_log.c new file mode 100644 index 0000000..a39de32 --- /dev/null +++ b/src/libstack/osrf_log.c @@ -0,0 +1,64 @@ +#include "osrf_log.h" +#include + +char* __osrfLogAppName = NULL; +char* __osrfLogDir = NULL; +int __osrfLogLevel = 1; + +int osrfLogInit(char* appname) { + + if( !appname ) return -1; + info_handler("Initing application log for app %s", appname ); + + char* dir = osrf_settings_host_value("/dirs/log"); + if(!dir) return warning_handler("No '/dirs/log' setting in host config"); + + char* level = osrfConfigGetValue(NULL, "/loglevel"); + if(level) { __osrfLogLevel = atoi(level); free(level); } + + __osrfLogAppName = strdup(appname); + __osrfLogDir = strdup(dir); + return 0; +} + + +void osrfLog( enum OSRF_LOG_LEVEL level, char* msg, ... ) { + + if( !(__osrfLogDir && __osrfLogAppName) ) return; + if( level > __osrfLogLevel ) return; + + time_t t = time(NULL); + struct tm* tms = localtime(&t); + + char datebuf[24]; + bzero(datebuf, 24); + strftime( datebuf, 23, "%d%m%Y", tms ); + + char timebuf[24]; + bzero(timebuf, 24); + strftime( timebuf, 23, "%Y:%m:%d %H:%M:%S", tms ); + + char millis[12]; + bzero(millis, 12); + double d = get_timestamp_millis(); + d = d - (int) d; + sprintf(millis, "%.6f", d); + + + char* filename = va_list_to_string( + "%s/%s.%s.log", __osrfLogDir, __osrfLogAppName, datebuf); + + FILE* file = fopen(filename, "a"); + free(filename); + + if(!file) { + warning_handler("Unable to open application log file %s\n", filename); + return; + } + + VA_LIST_TO_STRING(msg); + fprintf(file, "[%s.%s %d %d] %s\n", timebuf, millis + 2, getpid(), level, VA_BUF ); + fclose(file); +} + + diff --git a/src/libstack/osrf_log.h b/src/libstack/osrf_log.h new file mode 100644 index 0000000..5daf2e4 --- /dev/null +++ b/src/libstack/osrf_log.h @@ -0,0 +1,9 @@ +#include "opensrf/utils.h" +#include "osrf_settings.h" +#include "osrfConfig.h" + +enum OSRF_LOG_LEVEL { OSRF_ERROR = 1, OSRF_WARN = 2, OSRF_INFO = 3, OSRF_DEBUG = 4 }; + +int osrfLogInit(char* appname); +void osrfLog( enum OSRF_LOG_LEVEL, char* msg, ... ); + diff --git a/src/libstack/osrf_prefork.c b/src/libstack/osrf_prefork.c index 54be50e..2c89bb8 100644 --- a/src/libstack/osrf_prefork.c +++ b/src/libstack/osrf_prefork.c @@ -118,7 +118,10 @@ void prefork_child_process_request(prefork_child* child, char* data) { osrfAppSession* session = osrf_stack_transport_handler(msg, child->appname); if(!session) return; - if( session->stateless && session->state != OSRF_SESSION_CONNECTED ) return; + if( session->stateless && session->state != OSRF_SESSION_CONNECTED ) { + osrfAppSessionFree( session ); + return; + } /* keepalive loop for stateful sessions */ diff --git a/src/libstack/osrf_stack.c b/src/libstack/osrf_stack.c index 53f3129..c69529f 100644 --- a/src/libstack/osrf_stack.c +++ b/src/libstack/osrf_stack.c @@ -197,7 +197,7 @@ osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) { return NULL; case DISCONNECT: - osrf_app_session_destroy(session); + /* session will be freed by the forker */ return NULL; case CONNECT: @@ -237,6 +237,8 @@ int osrf_stack_application_handler( osrf_app_session* session, osrf_message* msg jsonObject* params = msg->_params; osrfAppRunMethod( app, method, session, msg->thread_trace, params ); + + osrfMessageFree(msg); return 1; diff --git a/src/libstack/osrf_system.c b/src/libstack/osrf_system.c index 0459209..77e5021 100644 --- a/src/libstack/osrf_system.c +++ b/src/libstack/osrf_system.c @@ -70,7 +70,6 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { osrfStringArray* arr = osrfNewStringArray(8); _osrfSystemInitCache(); - return 0; if(apps) { int i = 0; diff --git a/src/objson/Makefile b/src/objson/Makefile index 5cf18da..7c38643 100644 --- a/src/objson/Makefile +++ b/src/objson/Makefile @@ -26,7 +26,7 @@ # # -------------------------------------------------------------------- -OBJS = utils.o object.o json_parser.o +OBJS = md5.o utils.o object.o json_parser.o UTIL_DIR = ../utils DEST_INCLUDE = objson CFLAGS += -DSTRICT_JSON_WRITE #-DSTRICT_JSON_READ @@ -57,6 +57,12 @@ utils.o: $(UTIL_DIR)/utils.h $(UTIL_DIR)/utils.c cp $(UTIL_DIR)/utils.c . $(CC) -c $(CFLAGS) utils.c -o $@ +md5.o: $(UTIL_DIR)/md5.h $(UTIL_DIR)/md5.c + cp $(UTIL_DIR)/md5.h . + cp $(UTIL_DIR)/md5.c . + $(CC) -c $(CFLAGS) md5.c -o $@ + + clean: /bin/rm -f *.o *.so utils.c utils.h libobjson.so diff --git a/src/srfsh/srfsh.c b/src/srfsh/srfsh.c index ebc9a61..6de6183 100644 --- a/src/srfsh/srfsh.c +++ b/src/srfsh/srfsh.c @@ -279,35 +279,6 @@ int handle_login( char* words[]) { return 0; } -char* md5sum( char* text ) { - - struct md5_ctx ctx; - unsigned char digest[16]; - - MD5_start (&ctx); - - int i; - for ( i=0 ; i != strlen(text) ; i++ ) - MD5_feed (&ctx, text[i]); - - MD5_stop (&ctx, digest); - - char buf[16]; - memset(buf,0,16); - - char final[256]; - memset(final,0,256); - - for ( i=0 ; i<16 ; i++ ) { - sprintf(buf, "%02x", digest[i]); - strcat( final, buf ); - } - - return strdup(final); - -} - - int handle_set( char* words[]) { char* variable; diff --git a/src/srfsh/srfsh.h b/src/srfsh/srfsh.h index b3719fd..df61cbb 100644 --- a/src/srfsh/srfsh.h +++ b/src/srfsh/srfsh.h @@ -6,7 +6,6 @@ #include #include -#include "md5.h" #include "utils.h" #include "logging.h" @@ -72,4 +71,3 @@ int handle_math( char* words[] ); int do_math( int count, int style ); int handle_introspect(char* words[]); int handle_login( char* words[]); -char* md5sum( char* text ); diff --git a/src/utils/Makefile b/src/utils/Makefile index 01068ba..7a80440 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -1,6 +1,6 @@ -UTIL_HEADERS = logging.h utils.h socket_bundle.h md5.h sha.h string_array.h xml_utils.h -UTIL_OBJECTS = logging.o utils.o socket_bundle.o md5.o sha.o string_array.o +UTIL_HEADERS = md5.h logging.h utils.h socket_bundle.h sha.h string_array.h xml_utils.h +UTIL_OBJECTS = md5.o logging.o utils.o socket_bundle.o sha.o string_array.o all: $(UTIL_OBJECTS) copy diff --git a/src/utils/utils.c b/src/utils/utils.c index 972049c..0922544 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -396,3 +396,33 @@ char* file_to_string(const char* filename) { return data; } + +char* md5sum( char* text ) { + + struct md5_ctx ctx; + unsigned char digest[16]; + + MD5_start (&ctx); + + int i; + for ( i=0 ; i != strlen(text) ; i++ ) + MD5_feed (&ctx, text[i]); + + MD5_stop (&ctx, digest); + + char buf[16]; + memset(buf,0,16); + + char final[256]; + memset(final,0,256); + + for ( i=0 ; i<16 ; i++ ) { + sprintf(buf, "%02x", digest[i]); + strcat( final, buf ); + } + + return strdup(final); + +} + + diff --git a/src/utils/utils.h b/src/utils/utils.h index 6074499..a5ecb45 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -29,6 +29,7 @@ GNU General Public License for more details. #include //#include +#include "md5.h" /* turns a va_list into a string */ #define VA_LIST_TO_STRING(x) \ @@ -70,6 +71,26 @@ GNU General Public License for more details. char* INTSTR = __b; +/* +#define MD5SUM(s) \ + struct md5_ctx ctx; \ + unsigned char digest[16];\ + MD5_start (&ctx);\ + int i;\ + for ( i=0 ; i != strlen(text) ; i++ ) MD5_feed (&ctx, text[i]);\ + MD5_stop (&ctx, digest);\ + char buf[16];\ + memset(buf,0,16);\ + char final[256];\ + memset(final,0,256);\ + for ( i=0 ; i<16 ; i++ ) {\ + sprintf(buf, "%02x", digest[i]);\ + strcat( final, buf );\ + }\ + char* MD5STR = final; + */ + + @@ -156,6 +177,11 @@ char* file_to_string(const char* filename); +/** + Calculates the md5 of the text provided. + The returned string must be freed by the caller. + */ +char* md5sum( char* text ); #endif -- 2.11.0