diff options
author | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2014-06-10 17:43:09 +0200 |
---|---|---|
committer | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2014-06-11 14:17:00 +0200 |
commit | eccb685c7402702b778810a4e82469a8419c06ae (patch) | |
tree | 7165c0f00862adb225e40e5c7e1b7c05b3c37c5a | |
parent | c50f0797f5619f786ff14446ae657b65f9c3866d (diff) | |
download | dlt-daemon-eccb685c7402702b778810a4e82469a8419c06ae.tar.gz dlt-daemon-eccb685c7402702b778810a4e82469a8419c06ae.tar.bz2 dlt-daemon-eccb685c7402702b778810a4e82469a8419c06ae.zip |
Added configuration of dbus filter.
Signed-off-by: Alexander Wenzel <Alexander.AW.Wenzel@bmw.de>
-rw-r--r-- | src/dbus/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/dbus/dlt-dbus-options.c | 243 | ||||
-rw-r--r-- | src/dbus/dlt-dbus.c | 176 | ||||
-rw-r--r-- | src/dbus/dlt-dbus.conf | 18 | ||||
-rw-r--r-- | src/dbus/dlt-dbus.h | 40 |
5 files changed, 393 insertions, 86 deletions
diff --git a/src/dbus/CMakeLists.txt b/src/dbus/CMakeLists.txt index 4d26358..284a17b 100644 --- a/src/dbus/CMakeLists.txt +++ b/src/dbus/CMakeLists.txt @@ -20,7 +20,7 @@ include_directories( ${DBUS_INCLUDE_DIRS} ) -set(dlt_dbus_SRCS dlt-dbus.c) +set(dlt_dbus_SRCS dlt-dbus.c dlt-dbus-options.c) add_executable(dlt-dbus ${dlt_dbus_SRCS}) target_link_libraries(dlt-dbus dlt ${DBUS_LIBRARIES}) diff --git a/src/dbus/dlt-dbus-options.c b/src/dbus/dlt-dbus-options.c new file mode 100644 index 0000000..f1c72dd --- /dev/null +++ b/src/dbus/dlt-dbus-options.c @@ -0,0 +1,243 @@ +/** + * @licence app begin@ + * Copyright (C) 2014 BMW AG + * + * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps. + * + * Contributions are licensed to the GENIVI Alliance under one or more + * Contribution License Agreements. + * + * \copyright + * This Source Code Form is subject to the terms of the + * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with + * this file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * \author Alexander Wenzel <alexander.wenzel@bmw.de> + * + * \file dlt-dbus-options.c + * For further information see http://www.genivi.org/. + * @licence end@ + */ + +#include "dlt-dbus.h" + +#include <stdlib.h> +#include <string.h> + +/** + * Print information how to use this program. + */ +void usage(char *prog_name) +{ + char version[255]; + dlt_get_version(version,255); + + printf("Usage: %s [options]\n", prog_name); + printf("Application to forward dbus messages to DLT.\n"); + printf("%s\n", version); + printf("Options:\n"); + printf(" -d Daemonize. Detach from terminal and run in background.\n"); + printf(" -c filename Use configuration file. \n"); + printf(" -a appid Used application id. \n"); + printf(" Default: %s\n", DEFAULT_CONF_FILE); + printf(" -b type Used bus type. \n"); + printf(" Session = 0, System = 1.\n"); + printf(" -h This help message.\n"); +} + +/** + * Initialize command line options with default values. + */ +void init_cli_options(DltDBusCliOptions *options) +{ + options->ConfigurationFileName = DEFAULT_CONF_FILE; + options->ApplicationId = 0; + options->BusType = 0; + options->Daemonize = 0; +} + +/** + * Read command line options and set the values in provided structure + */ +int read_command_line(DltDBusCliOptions *options, int argc, char *argv[]) +{ + init_cli_options(options); + int opt; + + while((opt = getopt(argc, argv, "c:b:a:hd")) != -1) + { + switch(opt) { + case 'd': + { + options->Daemonize = 1; + break; + } + case 'b': + { + options->BusType = malloc(strlen(optarg)+1); + MALLOC_ASSERT(options->BusType); + strcpy(options->BusType, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + break; + } + case 'a': + { + options->ApplicationId = malloc(strlen(optarg)+1); + MALLOC_ASSERT(options->ApplicationId); + strcpy(options->ApplicationId, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + break; + } + case 'c': + { + options->ConfigurationFileName = malloc(strlen(optarg)+1); + MALLOC_ASSERT(options->ConfigurationFileName); + strcpy(options->ConfigurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + break; + } + case 'h': + { + usage(argv[0]); + exit(0); + return -1;//for parasoft + } + default: + { + fprintf(stderr, "Unknown option '%c'\n", optopt); + usage(argv[0]); + return -1; + } + } + } + return 0; +} + +/** + * Initialize configuration to default values. + */ +void init_configuration(DltDBusConfiguration *config) +{ + // Common + config->ApplicationId = "IPC0"; + + // DBus + config->DBus.ContextId = "ALL"; + config->DBus.BusType = 0; + config->DBus.FilterCount = 0; + +} + +/** + * Read options from the configuration file + */ +int read_configuration_file(DltDBusConfiguration *config, char *file_name) +{ + FILE *file; + char *line, *token, *value, *filter, *pch; + int ret = 0; + char *filterBegin,*filterEnd; + + init_configuration(config); + + file = fopen(file_name, "r"); + + if(file == NULL) + { + fprintf(stderr, "dlt-dbus-options, could not open configuration file.\n"); + return -1; + } + + line = malloc(MAX_LINE); + token = malloc(MAX_LINE); + value = malloc(MAX_LINE); + filter = malloc(MAX_LINE); + + MALLOC_ASSERT(line); + MALLOC_ASSERT(token); + MALLOC_ASSERT(value); + MALLOC_ASSERT(filter); + + while(fgets(line, MAX_LINE, file) != NULL) + { + token[0] = 0; + value[0] = 0; + filter[0] = 0; + + filterBegin = strchr(line,'='); + filterEnd = strpbrk (line,"\r\n"); + + if(filterBegin) + { + if(filterEnd && (filterEnd>filterBegin)) + { + strncpy(filter,filterBegin+1,filterEnd-filterBegin-1); + filter[filterEnd-filterBegin-1]=0; + } + else + { + strcpy(filter,filterBegin+1); + } + } + + pch = strtok (line, " =\r\n"); + while(pch != NULL) + { + if(pch[0] == '#') + break; + + if(token[0] == 0) + { + strncpy(token, pch, MAX_LINE-1); + token[MAX_LINE-1]=0; + } + else + { + strncpy(value, pch, MAX_LINE); + value[MAX_LINE-1]=0; + break; + } + + pch = strtok (NULL, " =\r\n"); + } + + if(token[0] && value[0]) + { + // Common + if(strcmp(token, "ApplicationId") == 0) + { + config->ApplicationId = malloc(strlen(value)+1); + MALLOC_ASSERT(config->ApplicationId); + strcpy(config->ApplicationId, value); /* strcpy unritical here, because size matches exactly the size to be copied */ + } + // ContextId + else if(strcmp(token, "ContextId") == 0) + { + config->DBus.ContextId = malloc(strlen(value)+1); + MALLOC_ASSERT(config->DBus.ContextId); + strcpy(config->DBus.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */ + } + // BusType + else if(strcmp(token, "BusType") == 0) + { + config->DBus.BusType = malloc(strlen(value)+1); + MALLOC_ASSERT(config->DBus.BusType); + strcpy(config->DBus.BusType, value); /* strcpy unritical here, because size matches exactly the size to be copied */ + } + // BusType + else if(strcmp(token, "FilterMatch") == 0) + { + if(config->DBus.FilterCount<DLT_DBUS_FILTER_MAX) + { + config->DBus.FilterMatch[config->DBus.FilterCount]= malloc(strlen(filter)+1); + MALLOC_ASSERT(config->DBus.FilterMatch[config->DBus.FilterCount]); + strcpy(config->DBus.FilterMatch[config->DBus.FilterCount], filter); + config->DBus.FilterCount++; + } + } + } + } + fclose(file); + free(value); + free(token); + free(filter); + free(line); + return ret; +} diff --git a/src/dbus/dlt-dbus.c b/src/dbus/dlt-dbus.c index 53eb94c..5cbba85 100644 --- a/src/dbus/dlt-dbus.c +++ b/src/dbus/dlt-dbus.c @@ -12,7 +12,6 @@ * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with * this file, You can obtain one at http://mozilla.org/MPL/2.0/. * - * * \author Alexander Wenzel <alexander.wenzel@bmw.de> * * \file dlt-dbus.c @@ -37,41 +36,20 @@ #define EAVESDROPPING_RULE "" #endif +DLT_DECLARE_CONTEXT(dbusLog); DLT_DECLARE_CONTEXT(dbusContext); static char dbus_message_buffer[DBUS_MAXIMUM_MESSAGE_LENGTH]; -static const char* -type_to_name (int message_type) -{ - switch (message_type) - { - case DBUS_MESSAGE_TYPE_SIGNAL: - return "signal"; - case DBUS_MESSAGE_TYPE_METHOD_CALL: - return "method call"; - case DBUS_MESSAGE_TYPE_METHOD_RETURN: - return "method return"; - case DBUS_MESSAGE_TYPE_ERROR: - return "error"; - default: - return "(unknown message type)"; - } -} - static DBusHandlerResult filter_func (DBusConnection *con, DBusMessage *message, void *data) { - DBusMessageIter iter; - const char *sender; - const char *destination; char **buf; - int message_type; - char *log_message; - int log_type; int len_p; + UNUSED(con); + UNUSED(data); buf = (char**)&dbus_message_buffer; if (!dbus_message_marshal(message, @@ -81,7 +59,7 @@ filter_func (DBusConnection *con, fprintf (stderr, "Failed to serialize DBus message!\n"); return DBUS_HANDLER_RESULT_HANDLED; } - //DLT_LOG (dbusContext, DLT_LOG_INFO, DLT_STRING("dbus message"), DLT_RAW ((void *)*buf, len_p)); + DLT_TRACE_NETWORK_SEGMENTED(dbusContext,DLT_NW_TRACE_IPC,0,0,len_p,(void *)*buf); if (dbus_message_is_signal (message, @@ -103,66 +81,96 @@ filter_func (DBusConnection *con, int main (int argc, char *argv[]) { - /* DLT initialisation */ - DLT_REGISTER_APP ("IPC0", "DBus Logging"); - DLT_REGISTER_CONTEXT(dbusContext, "ALL", "DBus Context for Logging"); - - DBusConnection *connection; - DBusError error; - DBusBusType type = DBUS_BUS_SESSION; - //DBusBusType type = DBUS_BUS_SYSTEM; - - dbus_error_init (&error); - - connection = dbus_bus_get (type, &error); - if (NULL == connection) - { - fprintf (stderr, "Failed to open connection to %s: %s\n", - DBUS_BUS_SYSTEM, - error.message); - dbus_error_free (&error); - exit (1); - } - - dbus_bus_add_match (connection, - EAVESDROPPING_RULE "type='signal'", - &error); - if (dbus_error_is_set (&error)) - goto fail; - dbus_bus_add_match (connection, - EAVESDROPPING_RULE "type='method_call'", - &error); - if (dbus_error_is_set (&error)) - goto fail; - dbus_bus_add_match (connection, - EAVESDROPPING_RULE "type='method_return'", - &error); - if (dbus_error_is_set (&error)) - goto fail; - dbus_bus_add_match (connection, - EAVESDROPPING_RULE "type='error'", - &error); - if (dbus_error_is_set (&error)) - goto fail; - - if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) { - fprintf (stderr, "Couldn't add filter!\n"); - exit (1); - } - - while (dbus_connection_read_write_dispatch(connection, -1)) - ; - - DLT_UNREGISTER_CONTEXT (dbusContext); - DLT_UNREGISTER_APP (); - exit(1); + DltDBusCliOptions options; + DltDBusConfiguration config; + + DBusConnection *connection; + DBusError error; + DBusBusType type; + + int num; + + if(read_command_line(&options, argc, argv) < 0) + { + fprintf(stderr, "Failed to read command line!\n"); + return -1; + } + + if(read_configuration_file(&config, options.ConfigurationFileName) < 0) + { + fprintf(stderr, "Failed to read configuration file!\n"); + return -1; + } + + // register application + if(options.ApplicationId) + DLT_REGISTER_APP (options.ApplicationId, "DBus Logging"); + else + DLT_REGISTER_APP (config.ApplicationId, "DBus Logging"); + + // register context + DLT_REGISTER_CONTEXT(dbusContext, config.DBus.ContextId, "DBus Context for Logging"); + DLT_REGISTER_CONTEXT(dbusLog, "Log", "DBus Context for Logging Generic information"); + + // initialise error handler + dbus_error_init (&error); + + // set DBus bus type + if(options.BusType) + type = (DBusBusType) atoi(options.BusType); + else + type = (DBusBusType) atoi(config.DBus.BusType); + + // get connection + connection = dbus_bus_get (type, &error); + + if(type==0) + DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_STRING("Session Bus")); + else if(type==1) + DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_STRING("System Bus")); + else + DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_INT(type)); + + if (NULL == connection) + { + fprintf (stderr, "Failed to open connection to %d: %s\n", + DBUS_BUS_SYSTEM, + error.message); + dbus_error_free (&error); + exit (1); + } + + for(num=0;num<config.DBus.FilterCount;num++) + { + dbus_bus_add_match (connection, + config.DBus.FilterMatch[num], + &error); + printf("Added FilterMatch: %s\n",config.DBus.FilterMatch[num]); + DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("FilterMatch"),DLT_UINT(num+1),DLT_STRING(config.DBus.FilterMatch[num])); + if (dbus_error_is_set (&error)) + goto fail; + } + + if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) { + fprintf (stderr, "Couldn't add filter!\n"); + exit (1); + } + + while (dbus_connection_read_write_dispatch(connection, -1)) + ; + + DLT_UNREGISTER_CONTEXT (dbusContext); + DLT_UNREGISTER_CONTEXT (dbusLog); + DLT_UNREGISTER_APP (); + exit(1); fail: - /* fail */ - fprintf (stderr, "Error: %s\n", error.message); - DLT_UNREGISTER_CONTEXT (dbusContext); - DLT_UNREGISTER_APP (); - exit(1); + /* fail */ + fprintf (stderr, "Error: %s\n", error.message); + DLT_UNREGISTER_CONTEXT (dbusContext); + DLT_UNREGISTER_CONTEXT (dbusLog); + DLT_UNREGISTER_APP (); + exit(1); } diff --git a/src/dbus/dlt-dbus.conf b/src/dbus/dlt-dbus.conf index 29ec241..2e8a90f 100644 --- a/src/dbus/dlt-dbus.conf +++ b/src/dbus/dlt-dbus.conf @@ -5,6 +5,22 @@ # General configuration ######################################################################## -# The application Id used for the System manager (Default: SYS) +# The application Id used for DBus Trace (Default: DBUS) +# If command line parameter used, the command line application id is used instead ApplicationId = DBUS +# The context Id used for the DBus Trace(Default: ALL) +ContextId = ALL + +# The DBus bus to be logged (Default: 1) +# DBUS_BUS_SESSION = 0 +# DBUS_BUS_SYSTEM = 1 +# DBUS_BUS_STARTER = 2 +BusType = 1 + +# Add one or several filters +# Filter String is beginning directly after first occurence of character '=' +FilterMatch=type='signal' +FilterMatch=type='method_call' +FilterMatch=type='method_return' +FilterMatch=type='error'
\ No newline at end of file diff --git a/src/dbus/dlt-dbus.h b/src/dbus/dlt-dbus.h index da93f7b..07f5b5f 100644 --- a/src/dbus/dlt-dbus.h +++ b/src/dbus/dlt-dbus.h @@ -22,6 +22,46 @@ #ifndef DLT_DBUS_H_ #define DLT_DBUS_H_ +// DLT related includes. +#include "dlt.h" +#include "dlt_common.h" +#define DEFAULT_CONF_FILE "/etc/dlt-dbus.conf" + +#define DLT_DBUS_FILTER_MAX 32 + +// Macros +#define UNUSED(x) (void)(x) +#define MALLOC_ASSERT(x) if(x == NULL) {\ + fprintf(stderr, "Out of memory\n");\ + abort();} + +#define MAX_LINE 1024 + +// Command line options +typedef struct { + char *ConfigurationFileName; + char *ApplicationId; + char *BusType; + int Daemonize; +} DltDBusCliOptions; + +// Configuration dbus options +typedef struct { + char *ContextId; + char *BusType; + int FilterCount; + char *FilterMatch[DLT_DBUS_FILTER_MAX]; +} DBusOptions; + +typedef struct { + char *ApplicationId; + DBusOptions DBus; + +} DltDBusConfiguration; + +extern void init_cli_options(DltDBusCliOptions *options); +extern int read_command_line(DltDBusCliOptions *options, int argc, char *argv[]); +extern int read_configuration_file(DltDBusConfiguration *config, char *file_name); #endif /* DLT_DBUS_H_ */ |