From 8fb404ff3d803964764452f7e027ded5bf1d85b3 Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Thu, 26 Feb 2009 16:32:20 -0500 Subject: Major changes. Added high level API. Unstable. Checkin before testing. --- dispatcher/Makefile.am | 2 +- dispatcher/elapi_dispatcher.c | 19 +++--- dispatcher/elapi_dispatcher.h | 34 +++++++++- dispatcher/elapi_dispatcher_ut.c | 136 +++++++++++++++++++++++++++++++++------ 4 files changed, 161 insertions(+), 30 deletions(-) (limited to 'dispatcher') diff --git a/dispatcher/Makefile.am b/dispatcher/Makefile.am index ea5b5ff..0fbb031 100644 --- a/dispatcher/Makefile.am +++ b/dispatcher/Makefile.am @@ -4,7 +4,7 @@ AM_CPPFLAGS = -I$(srcdir)/../collection -I$(srcdir)/../sinks -I/usr/include/libx common_headers=elapi_collection.h elapi_debug.h elapi_tools.h elapi_dispatcher.h elapi_sink.h noinst_LIBRARIES = libelapi_dispatcher.a -libelapi_dispatcher_a_SOURCES = elapi_dispatcher.c $(common_headers) +libelapi_dispatcher_a_SOURCES = elapi_dispatcher.c elapi_api.c $(common_headers) noinst_PROGRAMS = elapi_dispatcher_ut elapi_dispatcher_ut_SOURCES = elapi_dispatcher_ut.c $(common_headers) diff --git a/dispatcher/elapi_dispatcher.c b/dispatcher/elapi_dispatcher.c index 3995310..27a3eca 100644 --- a/dispatcher/elapi_dispatcher.c +++ b/dispatcher/elapi_dispatcher.c @@ -28,10 +28,8 @@ #include "elapi_collection.h" #include "elapi_sink.h" -#define SINK_COLLECTION "sinks" - -char def_application_name[] = "unknown"; - +char sink_collection[] = "sinks"; +char def_application_name[] = "default"; char *default_sinks[] = { "ipa","kernel","syslog","file","stderr", NULL }; @@ -39,6 +37,7 @@ char *default_sinks[] = { "ipa","kernel","syslog","file","stderr", NULL }; struct sink_context { struct collection_item *event; struct dispatcher_handle *handle; + char *format; char *previous; int previous_status; }; @@ -374,6 +373,7 @@ static int sink_handler(char *sink, sink_env->previous, sink_env->previous_status, sink_env->event, + sink_env->format, sink_data, (sink_env->handle)->custom_data, &error); @@ -428,6 +428,7 @@ static int default_router(char *sink, char *previous_sink, int previous_status, struct collection_item *event, + char *format_string struct sink_descriptor *sink_data, void *custom_data, int *error) @@ -441,7 +442,7 @@ static int default_router(char *sink, */ - *error = log_event_to_sink(sink_data,event,custom_data); + *error = log_event_to_sink(sink_data,event,format_string,custom_data); @@ -462,7 +463,7 @@ static int construct_sink_list(struct dispatcher_handle *handle) DEBUG_STRING("construct_sink_list","Entry"); /* Allocate collection to store sinks */ - error=create_collection(&(handle->sink_list),SINK_COLLECTION); + error=create_collection(&(handle->sink_list),sink_collection); if(error != 0) { DEBUG_NUMBER("Failed to create sink collection. Error",error); /* No cleanup here. @@ -603,6 +604,7 @@ void destroy_audit_dispatcher(struct dispatcher_handle *dispatcher) /* Log evento into a specific sink */ int log_event_to_sink(struct sink_descriptor *sink_data, struct collection_item *event, + char *format_string, void *custom_data) { int error = EOK; @@ -617,7 +619,7 @@ int log_event_to_sink(struct sink_descriptor *sink_data, DEBUG_SINK(sink_data); /* Format (serialize the event) */ - error = sink_cpb->format_cb(&(sink_data->dblock),event); + error = sink_cpb->format_cb(&(sink_data->dblock),format_string,event); if(error != EOK) { DEBUG_NUMBER("Format function returned error",error); return error; @@ -637,7 +639,7 @@ int log_event_to_sink(struct sink_descriptor *sink_data, /* Function to clean memory associated with the audit dispatcher */ -void log_audit_event(struct dispatcher_handle *dispatcher, struct collection_item *event) +void log_audit_event(struct dispatcher_handle *dispatcher, char *format_str, struct collection_item *event) { struct sink_context sink_env; @@ -653,6 +655,7 @@ void log_audit_event(struct dispatcher_handle *dispatcher, struct collection_ite sink_env.handle = dispatcher; sink_env.event = event; + sink_env.format = format_str; /* Logging an event is just iterating through the sinks and calling the sink_handler */ (void)traverse_collection(dispatcher->sink_list,ELAPI_TRAVERSE_ONELEVEL,sink_handler,(void *)(&sink_env)); diff --git a/dispatcher/elapi_dispatcher.h b/dispatcher/elapi_dispatcher.h index c7a582e..c47e8ff 100644 --- a/dispatcher/elapi_dispatcher.h +++ b/dispatcher/elapi_dispatcher.h @@ -43,6 +43,7 @@ typedef int (*event_router_fn)(char *sink, char *previous_sink, int previous_status, struct collection_item *event, + char *format_string, struct sink_descriptor *sink_data, void *custom_data, int *error); @@ -56,6 +57,11 @@ struct dispatcher_handle { void *custom_data; }; +/******************** Low level thread safe interface ************************************/ +/* This interface should be used if application plans to control the dispatcher, + * implement its own sinks that can be added dynamically or implements it own routing finction. + */ + /* Function to create a dispatcher */ int create_audit_dispatcher(struct dispatcher_handle **dispatcher, /* Handle of the dispatcher will be stored in this variable */ const char *appname, /* Application name. Passed to the sinks to do initialization */ @@ -68,7 +74,7 @@ int create_audit_dispatcher(struct dispatcher_handle **dispatcher, /* Handle of void destroy_audit_dispatcher(struct dispatcher_handle *dispatcher); /* Function to log an event */ -void log_audit_event(struct dispatcher_handle *dispatcher, struct collection_item *event); +void log_audit_event(struct dispatcher_handle *dispatcher, char *format_string, struct collection_item *event); /* Advanced functions */ /* Managing the sink collection */ @@ -79,7 +85,33 @@ int alter_audit_dispatcher(struct dispatcher_handle *dispatcher, /* Dispatcher * /* This function is exposed in case you are providing your own routing callback */ int log_event_to_sink(struct sink_descriptor *sink_data, struct collection_item *event, + char *format_string, void *custom_data); +/******************** High level interface ************************************/ +/* This interface is not thread safe but hides the dispatcher. */ + +/* Function to open audit using default routing functions */ +/* If appname is NULL - uses module name */ +int open_audit(const char *appname, char **desired_sinks); + +/* Function to open audit using custom routing function */ +int open_audit_with_router(const char *appname, + char **desired_sinks, + event_router_fn desired_router, + void *custom_data); + +/* Log event */ +void log_event(char *format_string,struct collection_item *event); + +/* Get dispatcher if you want to add sink to a default deispatcher or do some advaced operations */ +struct dispatcher_handle *get_dispatcher(void); + +/* Close audit */ +void close_audit(void); + +/* Creates collection with the timestamp already prepopulated */ +int create_event(struct collection_item **event,char *name); + #endif diff --git a/dispatcher/elapi_dispatcher_ut.c b/dispatcher/elapi_dispatcher_ut.c index e860096..aa0a041 100644 --- a/dispatcher/elapi_dispatcher_ut.c +++ b/dispatcher/elapi_dispatcher_ut.c @@ -124,25 +124,102 @@ int construct_event() return EOK; } - -int main() +int high_level_test() { int error = EOK; struct dispatcher_handle *dispatcher; struct dispatcher_handle *dispatcher2; - char *sinks[]= { "foo", "stderr", "bar", NULL }; + char *sinks[]= { "foo", "stderr", "file", NULL }; + char bin[] = { '\1','\2','\3','\4','\5','\6','\7','\8' }; - printf("Test start\n"); + printf("High level test start\n"); + printf("%s","=================\nOpening audit\n"); + error = open_audit("my_app1",sinks); + if(error!) { + printf("open_audit returned %d\n", error); + return error; + } + else printf("Success : %d\n",error); - error = create_audit_dispatcher(&dispatcher,"my_app",sinks,NULL,NULL); - printf("create_audit_dispatcher returned %d\n", error); + printf("%s","=================\nAdding syslog - first time - expect success\n"); + error = alter_audit_dispatcher(get_dispatcher(),"syslog",ELAPI_SINK_ACTION_ADD); + if(error != 0) { + printf("Expected success got failure %d\n",error); + return error; + } + else printf("Success : %d\n",error); + + printf("%s","=================\nAdding syslog again\n"); + error = alter_audit_dispatcher(get_dispatcher(),"syslog",ELAPI_SINK_ACTION_ADD); + if(error == 0) { + printf("Expected failure got success %d\n",error); + return EINVAL; + } + else printf("Expected error : %d\n",error); + + printf("%s","=================\nAdding syslog - first time - expect success\n"); + error = alter_audit_dispatcher(get_dispatcher(),"foo",ELAPI_SINK_ACTION_DELETE); + if(error != 0) { + printf("Expected success got failure %d\n",error); + return error; + } + else printf("Success : %d\n",error); + + printf("%s","=================\nCreating collection\n"); + if((error=create_event(&event,"test_event")) || + (error=add_str_property(event,NULL,"name","some name",0)) || + (error=add_int_property(event,NULL,"number",-100)) || + (error=add_binary_property(event,NULL,"binary",bin,8)) || + (error=add_double_property(event,NULL,"double",3.141592))) { + printf("Failed to create collection. Error %d\n",error); + return error; + } + else printf("Success : %d\n",error); + + printf("%s","=================\nLog event using NULL format\n"); + log_event(NULL, event); + + printf("%s","=================\nLog event using First format\n"); + log_event("%(timestamp), %s(name), %(number), %(bin), %%, %e(double)", event); + + printf("%s","=================\nLog event using Second format\n"); + log_event("%08X(time), %50s(name), %u(number), %s(bin), %%, %A(double)", event); + + printf("%s","=================\nLog event using Third format\n"); + log_event("%-50s(timestamp), %50s(name), %lo(number), %a(bin), %%, %.8f(double)", event); - /* Try to log with invalid parameters */ - printf("%s","=================\nNegative test1\n"); - log_audit_event(NULL, NULL); - printf("%s","=================\nNegative test2\n"); - log_audit_event(dispatcher, NULL); + + printf("%s","=================\nNegative tests\n"); + printf("%s","=================\nLog event using Third format\n"); + log_event("%-50s(timestamp), %50s(name, %lo(number), %a(bin), %%, %.8f(double)", event); + log_event("%(times), %z(name), %lo(number), %a(bin), %%, %.8f(double)", event); + log_event("%(times), %(name ), %lo(number), %a(bin), %%, %.8f(double)", event); + log_event("%(times), (name ), %lo(number), %a(bin), %%, %.8f(double)", event); + log_event("%(times), (name ) %, %.8f(double)", event); + log_event("%(times), (name ) %f (double)", event); + + + destroy_collection(event); + close_audit(void); + + printf("Low level test end\n"); + return error; +} + + +int low_level_test() +{ + int error = EOK; + char *sinks[]= { "foo", "stderr", "bar", NULL }; + + printf("Low level test start\n"); + + error = create_audit_dispatcher(&dispatcher,"my_app",sinks,NULL,NULL); + if(error!) { + printf("create_audit_dispatcher returned %d\n", error); + return error; + } printf("%s","=================\nCreating collection\n"); error = construct_event(); @@ -154,16 +231,16 @@ int main() printf("%s","=================\nLogging peer - expect success\n"); print_collection(peer); - log_audit_event(dispatcher, peer); + log_audit_event(dispatcher, NULL, peer); printf("%s","=================\nLogging host - expect success\n"); print_collection(host); - log_audit_event(dispatcher, host); + log_audit_event(dispatcher, NULL, host); printf("%s","=================\nLogging socket - expect success\n"); print_collection(socket); - log_audit_event(dispatcher, socket); + log_audit_event(dispatcher, NULL, socket); printf("%s","=================\nLogging event - expect success\n"); print_collection(event); - log_audit_event(dispatcher, event); + log_audit_event(dispatcher, NULL, event); /* Try to alter list of sinks */ printf("%s","=================\nSeries of negative test cases.\n"); @@ -201,16 +278,16 @@ int main() /* Log event into syslog */ printf("%s","=================\nLogging peer - expect success\n"); print_collection(peer); - log_audit_event(dispatcher, peer); + log_audit_event(dispatcher, NULL, peer); printf("%s","=================\nLogging host - expect success\n"); print_collection(host); - log_audit_event(dispatcher, host); + log_audit_event(dispatcher, NULL, host); printf("%s","=================\nLogging socket - expect success\n"); print_collection(socket); - log_audit_event(dispatcher, socket); + log_audit_event(dispatcher, NULL, socket); printf("%s","=================\nLogging event - expect success\n"); print_collection(event); - log_audit_event(dispatcher, event); + log_audit_event(dispatcher, NULL, event); /* Pulse */ error = alter_audit_dispatcher(dispatcher,"syslog",ELAPI_SINK_ACTION_PULSE); @@ -251,7 +328,26 @@ int main() destroy_collection(host); destroy_collection(socket); destroy_collection(event); - printf("Test end\n"); + printf("Low level test end\n"); + return error; +} + + +int main() +{ + int error = EOK; + + error = low_level_test(); + printf("Low level test returned: %d\n",error); + if(error) return error; + + error = high_level_test(); + printf("High level test first run returned: %d\n",error); + if(error) return error; + + error = high_level_test(); + printf("High level test second run returned: %d\n",error); + return error; } -- cgit