From ed412acac8e9f8a5745e55adeed9652b34b9ee4f Mon Sep 17 00:00:00 2001 From: Stefan Vacek Date: Mon, 31 Aug 2015 10:13:07 +0200 Subject: Allow multiple instances of dlt-daemon - Make dlt-daemon configurable to specify directory of fifos and port of dlt-daemon, this allows to run multiple instances of dlt-daemon at one node at the same time. This is useful in testing environment where simultanous tests should not interfere due to the same instance of dlt-daemon - dlt-daemon: add option -t to specify a directory where all fifos will be created - dlt-daemon: add option -p to specify the port under which dlt-daemon can be connected - client-library: add environment variable DLT_PIPE_DIR to specify a non-default directory of fifos for logging applications - client-library: add environment variable DLT_DAEMON_TCP_PORT to specify the port under which dlt-daemon can be reached (especially when using dlt-receive) Signed-off-by: Stefan Vacek Signed-off-by: Lutz Helwing --- include/dlt/dlt_common.h | 15 ++++++- src/daemon/dlt-daemon.c | 95 ++++++++++++++++++++++++++++++--------- src/daemon/dlt-daemon.h | 3 ++ src/daemon/dlt_daemon_common.c | 4 +- src/daemon/dlt_daemon_socket.c | 6 +-- src/daemon/dlt_daemon_socket.h | 2 +- src/lib/dlt_client.c | 17 ++++++- src/lib/dlt_client_cfg.h | 3 ++ src/lib/dlt_user.c | 40 +++++++++++------ src/shared/dlt_common.c | 53 ++++++++++++++++++++++ src/shared/dlt_user_shared_cfg.h | 6 --- tests/gtest_dlt_daemon_common.cpp | 10 +++++ 12 files changed, 205 insertions(+), 49 deletions(-) diff --git a/include/dlt/dlt_common.h b/include/dlt/dlt_common.h index 10334ca..da9835c 100644 --- a/include/dlt/dlt_common.h +++ b/include/dlt/dlt_common.h @@ -76,6 +76,7 @@ */ #include +#include #if !defined(_MSC_VER) #include @@ -186,12 +187,12 @@ enum { }; /** - * The standard TCP Port used for DLT daemon + * The standard TCP Port used for DLT daemon, can be overwritten via -p when starting dlt-daemon */ #define DLT_DAEMON_TCP_PORT 3490 -/* Initi value for file descritpor */ +/* Initial value for file descriptor */ #define DLT_FD_INIT -1 /* Minimum value for a file descriptor except the POSIX Standards: stdin=0, stdout=1, stderr=2 */ @@ -331,7 +332,11 @@ extern const char dltSerialHeader[DLT_ID_SIZE]; extern char dltSerialHeaderChar[DLT_ID_SIZE]; /** + * The common base-path of the dlt-daemon-fifo and application-generated fifos + */ +extern char dltFifoBaseDir[PATH_MAX + 1]; +/** * The type of a DLT ID (context id, application id, etc.) */ typedef char ID4[DLT_ID_SIZE]; @@ -1287,6 +1292,12 @@ extern "C" */ void dlt_check_envvar(); + /** + * Create the specified path, recursive if necessary + * behaves like calling mkdir -p on the console + */ + int dlt_mkdir_recursive(const char *dir); + #ifdef __cplusplus } #endif diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index 0c70cde..5022da4 100644 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -96,6 +96,14 @@ void usage() printf(" -d Daemonize\n"); printf(" -h Usage\n"); printf(" -c filename DLT daemon configuration file (Default: " CONFIGURATION_FILES_DIR "/dlt.conf)\n"); + printf(" -t directory Directory for local fifo and user-pipes (Default: /tmp)\n"); + printf(" (Applications wanting to connect to a daemon using a\n"); + printf(" custom directory need to be started with the environment \n"); + printf(" variable DLT_PIPE_DIR set appropriately)\n"); + printf(" -p port port to monitor for incoming requests (Default: 3490)\n"); + printf(" (Applications wanting to connect to a daemon using a custom\n"); + printf(" port need to be started with the environment variable\n"); + printf(" DLT_DAEMON_TCP_PORT set appropriately)\n"); } /* usage() */ /** @@ -114,9 +122,13 @@ int option_handling(DltDaemonLocal *daemon_local,int argc, char* argv[]) /* Initialize flags */ memset(daemon_local,0,sizeof(DltDaemonLocal)); + /* default values */ + daemon_local->flags.port = DLT_DAEMON_TCP_PORT; + strncpy(dltFifoBaseDir, "/tmp", NAME_MAX); + opterr = 0; - while ((c = getopt (argc, argv, "hdc:")) != -1) + while ((c = getopt (argc, argv, "hdc:t:p:")) != -1) { switch (c) { @@ -130,6 +142,21 @@ int option_handling(DltDaemonLocal *daemon_local,int argc, char* argv[]) strncpy(daemon_local->flags.cvalue,optarg,NAME_MAX); break; } + case 't': + { + strncpy(dltFifoBaseDir, optarg, NAME_MAX); + break; + } + case 'p': + { + daemon_local->flags.port = atoi(optarg); + if (daemon_local->flags.port == 0) + { + fprintf (stderr, "Invalid port `%s' specified.\n", optarg); + return -1; + } + break; + } case 'h': { usage(); @@ -137,7 +164,7 @@ int option_handling(DltDaemonLocal *daemon_local,int argc, char* argv[]) } case '?': { - if (optopt == 'c') + if (optopt == 'c' || optopt == 't' || optopt == 'p') { fprintf (stderr, "Option -%c requires an argument.\n", optopt); } @@ -161,6 +188,9 @@ int option_handling(DltDaemonLocal *daemon_local,int argc, char* argv[]) } /* switch() */ } + snprintf(daemon_local->flags.userPipesDir, NAME_MAX + 1, "%s/dltpipes", dltFifoBaseDir); + snprintf(daemon_local->flags.daemonFifoName, NAME_MAX + 1, "%s/dlt", dltFifoBaseDir); + return 0; } /* option_handling() */ @@ -186,7 +216,7 @@ int option_file_parser(DltDaemonLocal *daemon_local) daemon_local->flags.offlineTraceMaxSize = 0; daemon_local->flags.loggingMode = DLT_LOG_TO_CONSOLE; daemon_local->flags.loggingLevel = LOG_INFO; - strncpy(daemon_local->flags.loggingFilename, DLT_USER_DIR "/dlt.log",sizeof(daemon_local->flags.loggingFilename)-1); + snprintf(daemon_local->flags.loggingFilename, sizeof(daemon_local->flags.loggingFilename)-1, "%s/dlt.log", dltFifoBaseDir); daemon_local->flags.loggingFilename[sizeof(daemon_local->flags.loggingFilename)-1]=0; daemon_local->timeoutOnSend = 4; daemon_local->RingbufferMinSize = DLT_DAEMON_RINGBUFFER_MIN_SIZE; @@ -444,6 +474,14 @@ int main(int argc, char* argv[]) PRINT_FUNCTION_VERBOSE(daemon_local.flags.vflag); + /* Make sure the parent user directory is created */ + if (dlt_mkdir_recursive(dltFifoBaseDir) != 0) + { + snprintf(str,DLT_DAEMON_TEXTBUFSIZE, "Base dir %s cannot be created!\n", dltFifoBaseDir); + dlt_log(LOG_ERR, str); + return -1; + } + /* --- Daemon init phase 1 begin --- */ if (dlt_daemon_local_init_p1(&daemon, &daemon_local, daemon_local.flags.vflag)==-1) { @@ -671,20 +709,22 @@ int dlt_daemon_local_init_p1(DltDaemon *daemon, DltDaemonLocal *daemon_local, in } #endif + const char *tmpFifo = daemon_local->flags.userPipesDir; + /* create dlt pipes directory */ - ret=mkdir(DLT_USER_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX ); + ret=mkdir(tmpFifo, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX ); if (ret==-1 && errno != EEXIST) { - snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user dir %s cannot be created (%s)!\n", DLT_USER_DIR, strerror(errno)); + snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user dir %s cannot be created (%s)!\n", tmpFifo, strerror(errno)); dlt_log(LOG_WARNING, str); return -1; } // S_ISGID cannot be set by mkdir, let's reassign right bits - ret=chmod(DLT_USER_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH | S_ISGID | S_ISVTX ); + ret=chmod(tmpFifo, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH | S_ISGID | S_ISVTX ); if (ret==-1) { - snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user dir %s cannot be chmoded (%s)!\n", DLT_USER_DIR, strerror(errno)); + snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user dir %s cannot be chmoded (%s)!\n", tmpFifo, strerror(errno)); dlt_log(LOG_WARNING, str); return -1; } @@ -846,27 +886,30 @@ int dlt_daemon_local_connection_init(DltDaemon *daemon, DltDaemonLocal *daemon_l umask(0); /* Try to delete existing pipe, ignore result of unlink */ - unlink(DLT_USER_FIFO); + const char *tmpFifo = daemon_local->flags.daemonFifoName; + unlink(tmpFifo); - ret=mkfifo(DLT_USER_FIFO, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); + ret=mkfifo(tmpFifo, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP ); if (ret==-1) { - snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user %s cannot be created (%s)!\n",DLT_USER_FIFO, strerror(errno)); + snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user %s cannot be created (%s)!\n",tmpFifo, strerror(errno)); dlt_log(LOG_WARNING, str); return -1; } /* if */ - daemon_local->fp = open(DLT_USER_FIFO, O_RDWR); + daemon_local->fp = open(tmpFifo, O_RDWR); if (daemon_local->fp==-1) { - snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user %s cannot be opened (%s)!\n",DLT_USER_FIFO, strerror(errno)); + snprintf(str,DLT_DAEMON_TEXTBUFSIZE,"FIFO user %s cannot be opened (%s)!\n",tmpFifo, strerror(errno)); dlt_log(LOG_WARNING, str); return -1; } /* if */ /* create and open socket to receive incoming connections from client */ - if(dlt_daemon_socket_open(&(daemon_local->sock))) + if(dlt_daemon_socket_open(&(daemon_local->sock), daemon_local->flags.port)) + { return -1; + } /* prepare usage of select(), add FIFO and receiving socket */ FD_ZERO(&(daemon_local->master)); @@ -1044,7 +1087,7 @@ void dlt_daemon_local_cleanup(DltDaemon *daemon, DltDaemonLocal *daemon_local, i dlt_file_free(&(daemon_local->file),daemon_local->flags.vflag); /* Try to delete existing pipe, ignore result of unlink() */ - unlink(DLT_USER_FIFO); + unlink(daemon_local->flags.daemonFifoName); #ifdef DLT_SHM_ENABLE /* free shared memory */ @@ -1069,10 +1112,16 @@ void dlt_daemon_signal_handler(int sig) dlt_log(LOG_NOTICE, str); /* Try to delete existing pipe, ignore result of unlink() */ - unlink(DLT_USER_FIFO); + char tmp[PATH_MAX + 1]; + snprintf(tmp, PATH_MAX, "%s/dlt", dltFifoBaseDir); + tmp[PATH_MAX] = 0; + unlink(tmp); /* Try to delete lock file, ignore result of unlink() */ - unlink(DLT_DAEMON_LOCK_FILE); + snprintf(tmp, PATH_MAX, "%s/%s", dltFifoBaseDir, DLT_DAEMON_LOCK_FILE); + tmp[PATH_MAX] = 0; + unlink(tmp); + /* Terminate program */ exit(0); @@ -1138,12 +1187,18 @@ void dlt_daemon_daemonize(int verbose) umask(DLT_DAEMON_UMASK); /* Change to known directory */ - if(chdir(DLT_USER_DIR) < 0) - dlt_log(LOG_WARNING, "Failed to chdir to DLT_USER_DIR.\n");; + if(chdir(dltFifoBaseDir) < 0) + { + snprintf(str, DLT_DAEMON_TEXTBUFSIZE, "Failed to chdir to fifo-dir %s\n", dltFifoBaseDir); + dlt_log(LOG_WARNING, str); + } - /* Ensure single copy of daemon; + /* Ensure single copy of daemon; if started with same directory run only one instance at a time */ - lfp=open(DLT_DAEMON_LOCK_FILE,O_RDWR|O_CREAT,DLT_DAEMON_LOCK_FILE_PERM); + char dlt_daemon_lock_file[PATH_MAX + 1]; + snprintf(dlt_daemon_lock_file, PATH_MAX, "%s/%s", dltFifoBaseDir, DLT_DAEMON_LOCK_FILE); + dlt_daemon_lock_file[PATH_MAX] = 0; + lfp=open(dlt_daemon_lock_file,O_RDWR|O_CREAT,DLT_DAEMON_LOCK_FILE_PERM); if (lfp<0) { dlt_log(LOG_CRIT, "Can't open lock file, exiting DLT daemon\n"); diff --git a/src/daemon/dlt-daemon.h b/src/daemon/dlt-daemon.h index fee6767..8edeead 100644 --- a/src/daemon/dlt-daemon.h +++ b/src/daemon/dlt-daemon.h @@ -112,6 +112,9 @@ typedef struct char pathToECUSoftwareVersion[256]; /**< (String: Filename) The file from which to read the ECU version from. */ int sendTimezone; /**< (Boolean) Send Timezone perdiodically */ int offlineLogstorageMaxDevices; /**< (int) Maximum devices to be used as offline logstorage devices */ + char userPipesDir[NAME_MAX + 1]; /**< (String: Directory) directory where dltpipes reside (Default: /tmp/dltpipes) */ + char daemonFifoName[NAME_MAX + 1]; /**< (String: Filename) name of local fifo (Default: /tmp/dlt) */ + unsigned int port; /**< port number */ } DltDaemonFlags; /** diff --git a/src/daemon/dlt_daemon_common.c b/src/daemon/dlt_daemon_common.c index 2064e94..af97b16 100644 --- a/src/daemon/dlt_daemon_common.c +++ b/src/daemon/dlt_daemon_common.c @@ -390,7 +390,7 @@ DltDaemonApplication* dlt_daemon_application_add(DltDaemon *daemon,char *apid,pi { if ( close(application->user_handle) < 0 ) { - snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "close() failed to %s, errno=%d (%s)!\n",filename,errno,strerror(errno)); /* errno 2: ENOENT - No such file or directory */ + snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "close() failed to %s/dltpipes/dlt%d, errno=%d (%s)!\n",dltFifoBaseDir,pid,errno,strerror(errno)); /* errno 2: ENOENT - No such file or directory */ dlt_log(LOG_WARNING, str); } @@ -402,7 +402,7 @@ DltDaemonApplication* dlt_daemon_application_add(DltDaemon *daemon,char *apid,pi /* open user pipe only if it is not yet opened */ if (application->user_handle==DLT_FD_INIT && pid!=0) { - snprintf(filename,DLT_DAEMON_COMMON_TEXTBUFSIZE,"%s/dlt%d",DLT_USER_DIR,pid); + snprintf(filename,DLT_DAEMON_COMMON_TEXTBUFSIZE,"%s/dltpipes/dlt%d",dltFifoBaseDir,pid); dlt_user_handle = open(filename, O_WRONLY|O_NONBLOCK); if ( dlt_user_handle < 0 ) diff --git a/src/daemon/dlt_daemon_socket.c b/src/daemon/dlt_daemon_socket.c index 709868f..fa91485 100644 --- a/src/daemon/dlt_daemon_socket.c +++ b/src/daemon/dlt_daemon_socket.c @@ -63,7 +63,7 @@ /** Global text output buffer, mainly used for creation of error/warning strings */ static char str[DLT_DAEMON_TEXTBUFSIZE]; -int dlt_daemon_socket_open(int *sock) +int dlt_daemon_socket_open(int *sock, unsigned int servPort) { int yes = 1; char portnumbuffer[33]; @@ -75,7 +75,7 @@ int dlt_daemon_socket_open(int *sock) hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // use my IP address - sprintf(portnumbuffer, "%d", DLT_DAEMON_TCP_PORT); + snprintf(portnumbuffer, 32, "%d", servPort); if ((rv = getaddrinfo(NULL, portnumbuffer, &hints, &servinfo)) != 0) { snprintf(str, DLT_DAEMON_TEXTBUFSIZE, "getaddrinfo: %s\n", gai_strerror(rv)); dlt_log(LOG_WARNING, str); @@ -115,7 +115,7 @@ int dlt_daemon_socket_open(int *sock) freeaddrinfo(servinfo); - snprintf(str, DLT_DAEMON_TEXTBUFSIZE, "%s: Listening on port: %u\n", __FUNCTION__, DLT_DAEMON_TCP_PORT); + snprintf(str, DLT_DAEMON_TEXTBUFSIZE, "%s: Listening on port: %u\n", __FUNCTION__, servPort); dlt_log(LOG_INFO, str); // get socket buffer size diff --git a/src/daemon/dlt_daemon_socket.h b/src/daemon/dlt_daemon_socket.h index 5d4718c..cc8dfb5 100644 --- a/src/daemon/dlt_daemon_socket.h +++ b/src/daemon/dlt_daemon_socket.h @@ -62,7 +62,7 @@ #include "dlt_common.h" #include "dlt_user.h" -int dlt_daemon_socket_open(int *sock); +int dlt_daemon_socket_open(int *sock, unsigned int servPort); int dlt_daemon_socket_close(int sock); int dlt_daemon_socket_get_send_qeue_max_size(int sock); diff --git a/src/lib/dlt_client.c b/src/lib/dlt_client.c index 2c9f46a..b77556d 100644 --- a/src/lib/dlt_client.c +++ b/src/lib/dlt_client.c @@ -131,7 +131,9 @@ int dlt_client_connect(DltClient *client, int verbose) char portnumbuffer[33]; struct addrinfo hints, *servinfo, *p; int rv; - + char *env_daemon_port; + /* the port may be specified by an environment variable, defaults to DLT_DAEMON_TCP_PORT */ + unsigned short servPort = DLT_DAEMON_TCP_PORT; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; @@ -140,9 +142,20 @@ int dlt_client_connect(DltClient *client, int verbose) return -1; } + /* the port may be specified by an environment variable */ + env_daemon_port = getenv(DLT_CLIENT_ENV_DAEMON_TCP_PORT); + if (env_daemon_port != NULL) + { + servPort = atoi(env_daemon_port); + } + if (servPort == 0) + { + servPort = DLT_DAEMON_TCP_PORT; + } + if (client->serial_mode==0) { - sprintf(portnumbuffer, "%d", DLT_DAEMON_TCP_PORT); + snprintf(portnumbuffer, 32, "%d", servPort); if ((rv = getaddrinfo(client->servIP, portnumbuffer, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return -1; diff --git a/src/lib/dlt_client_cfg.h b/src/lib/dlt_client_cfg.h index c2592e6..45d1864 100644 --- a/src/lib/dlt_client_cfg.h +++ b/src/lib/dlt_client_cfg.h @@ -92,6 +92,9 @@ #define DLT_CLIENT_INITIAL_BAUDRATE 0 #endif +/* Name of environment variable for specifying the daemon port */ +#define DLT_CLIENT_ENV_DAEMON_TCP_PORT "DLT_DAEMON_TCP_PORT" + /************************/ /* Don't change please! */ /************************/ diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index 9cf4070..f961bb5 100644 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -64,6 +64,9 @@ static DltUser dlt_user; static int dlt_user_initialised = 0; static int dlt_user_freeing = 0; +static char dlt_user_dir[NAME_MAX + 1]; +static char dlt_daemon_fifo[NAME_MAX + 1]; + static char str[DLT_USER_BUFFER_LENGTH]; static sem_t dlt_mutex; @@ -183,6 +186,9 @@ int dlt_init(void) /* check environment variables */ dlt_check_envvar(); + snprintf(dlt_user_dir, NAME_MAX, "%s/dltpipes", dltFifoBaseDir); + snprintf(dlt_daemon_fifo, NAME_MAX, "%s/dlt", dltFifoBaseDir); + dlt_user.dlt_is_file = 0; dlt_user.overflow = 0; dlt_user.overflow_counter = 0; @@ -191,10 +197,18 @@ int dlt_init(void) #endif /* create dlt pipes directory */ - ret=mkdir(DLT_USER_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX ); + /* Make sure the parent user directory is created */ + if (dlt_mkdir_recursive(dltFifoBaseDir) != 0) + { + snprintf(str,DLT_USER_BUFFER_LENGTH, "Base dir %s cannot be created!\n", dltFifoBaseDir); + dlt_log(LOG_ERR, str); + return -1; + } + + ret=mkdir(dlt_user_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | S_ISVTX ); if (ret==-1 && errno != EEXIST) { - snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user dir %s cannot be created!\n", DLT_USER_DIR); + snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user dir %s cannot be created!\n", dlt_user_dir); dlt_log(LOG_ERR, str); return -1; } @@ -203,25 +217,25 @@ int dlt_init(void) if(ret == 0) { // S_ISGID cannot be set by mkdir, let's reassign right bits - ret=chmod(DLT_USER_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH | S_ISGID | S_ISVTX ); + ret=chmod(dlt_user_dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH | S_ISGID | S_ISVTX ); if (ret==-1) { - snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user dir %s cannot be chmoded!\n", DLT_USER_DIR); + snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user dir %s cannot be chmoded!\n", dlt_user_dir); dlt_log(LOG_ERR, str); return -1; } } /* create and open DLT user FIFO */ - snprintf(filename,DLT_USER_MAX_FILENAME_LENGTH,"%s/dlt%d",DLT_USER_DIR,getpid()); - + snprintf(filename,DLT_USER_MAX_FILENAME_LENGTH,"%s/dlt%d",dlt_user_dir,getpid()); + /* Try to delete existing pipe, ignore result of unlink */ unlink(filename); ret=mkfifo(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP ); if (ret==-1) { - snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, FIFO user %s cannot be created!\n",filename); + snprintf(str,DLT_USER_BUFFER_LENGTH,"Logging disabled, FIFO user %s cannot be created!\n",filename); dlt_log(LOG_WARNING, str); /* return 0; */ /* removed to prevent error, when FIFO already exists */ } @@ -230,7 +244,7 @@ int dlt_init(void) ret=chmod(filename, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP ); if (ret==-1) { - snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user %s cannot be chmoded!\n", DLT_USER_DIR); + snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO user %s cannot be chmoded!\n", dlt_user_dir); dlt_log(LOG_WARNING, str); return -1; } @@ -238,20 +252,20 @@ int dlt_init(void) dlt_user.dlt_user_handle = open(filename, O_RDWR | O_CLOEXEC); if (dlt_user.dlt_user_handle == DLT_FD_INIT) { - snprintf(str,DLT_USER_BUFFER_LENGTH,"Loging disabled, FIFO user %s cannot be opened!\n",filename); + snprintf(str,DLT_USER_BUFFER_LENGTH,"Logging disabled, FIFO user %s cannot be opened!\n",filename); dlt_log(LOG_WARNING, str); unlink(filename); return 0; } /* open DLT output FIFO */ - dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK | O_CLOEXEC ); + dlt_user.dlt_log_handle = open(dlt_daemon_fifo, O_WRONLY | O_NONBLOCK | O_CLOEXEC ); if (dlt_user.dlt_log_handle==-1) { /* This is a normal usecase. It is OK that the daemon (and thus the FIFO /tmp/dlt) starts later and some DLT users have already been started before. Thus it is OK if the FIFO can't be opened. */ - snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO %s cannot be opened. Retrying later...\n",DLT_USER_FIFO); + snprintf(str,DLT_USER_BUFFER_LENGTH,"FIFO %s cannot be opened. Retrying later...\n",dlt_daemon_fifo); dlt_log(LOG_INFO, str); //return 0; } @@ -612,7 +626,7 @@ int dlt_free(void) if (dlt_user.dlt_user_handle!=DLT_FD_INIT) { - snprintf(filename,DLT_USER_MAX_FILENAME_LENGTH,"%s/dlt%d",DLT_USER_DIR,getpid()); + snprintf(filename,DLT_USER_MAX_FILENAME_LENGTH,"%s/dlt%d",dlt_user_dir,getpid()); close(dlt_user.dlt_user_handle); dlt_user.dlt_user_handle=DLT_FD_INIT; @@ -4252,7 +4266,7 @@ void dlt_user_log_reattach_to_daemon(void) dlt_user.dlt_log_handle=-1; /* try to open pipe to dlt daemon */ - dlt_user.dlt_log_handle = open(DLT_USER_FIFO, O_WRONLY | O_NONBLOCK); + dlt_user.dlt_log_handle = open(dlt_daemon_fifo, O_WRONLY | O_NONBLOCK); if (dlt_user.dlt_log_handle > 0) { if (dlt_user_log_init(&handle,&log_new)==-1) diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 98852fd..b0123b6 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -34,6 +34,9 @@ #include /* for NAME_MAX */ #include /* for PRI formatting macro */ +#include +#include /* for mkdir() */ + #include "dlt_common.h" #include "dlt_common_cfg.h" @@ -64,6 +67,7 @@ static char str[DLT_COMMON_BUFFER_LENGTH]; const char dltSerialHeader[DLT_ID_SIZE] = { 'D','L','S',1 }; char dltSerialHeaderChar[DLT_ID_SIZE] = { 'D','L','S',1 }; +char dltFifoBaseDir[PATH_MAX + 1] = "/tmp"; /* internal logging parameters */ static int logging_mode = DLT_LOG_TO_CONSOLE; @@ -1983,6 +1987,12 @@ void dlt_log_set_filename(const char *filename) } +void dlt_log_set_fifo_basedir(const char * env_pipe_dir) +{ + strncpy(dltFifoBaseDir, env_pipe_dir, PATH_MAX); + dltFifoBaseDir[PATH_MAX] = 0; +} + void dlt_log_init(int mode) { logging_mode = mode; @@ -3723,5 +3733,48 @@ void dlt_check_envvar() } } + char* env_pipe_dir = getenv("DLT_PIPE_DIR"); + if( env_pipe_dir != NULL ) + { + dlt_log_set_fifo_basedir(env_pipe_dir); + } +} + +int dlt_mkdir_recursive(const char *dir) +{ + int ret = 0; + char tmp[PATH_MAX+1]; + char *p = NULL; + char *end = NULL; + size_t len; + + strncpy(tmp, dir, PATH_MAX); + len = strlen(tmp); + if(tmp[len - 1] == '/') + { + tmp[len - 1] = 0; + } + end = tmp + len; + + for(p = tmp + 1; ((*p) && (ret == 0)) || ((ret == -1 && errno == EEXIST) && (p != end)); p++) + { + if(*p == '/') + { + *p = 0; + ret = mkdir(tmp, S_IRWXU); + *p = '/'; + } + } + + if (ret == 0 || (ret == -1 && errno == EEXIST)) + { + ret = mkdir(tmp, S_IRWXU); + } + + if (ret == -1 && errno == EEXIST) + { + ret = 0; + } + return ret; } diff --git a/src/shared/dlt_user_shared_cfg.h b/src/shared/dlt_user_shared_cfg.h index fc74c0c..dc31e4d 100644 --- a/src/shared/dlt_user_shared_cfg.h +++ b/src/shared/dlt_user_shared_cfg.h @@ -74,12 +74,6 @@ /* Changable */ /*************/ -/* Directory, where the named pipes to the DLT daemon resides */ -#define DLT_USER_DIR "/tmp/dltpipes" - -/* Name of named pipe to DLT daemon */ -#define DLT_USER_FIFO "/tmp/dlt" - /************************/ /* Don't change please! */ /************************/ diff --git a/tests/gtest_dlt_daemon_common.cpp b/tests/gtest_dlt_daemon_common.cpp index 8049965..58cc77e 100644 --- a/tests/gtest_dlt_daemon_common.cpp +++ b/tests/gtest_dlt_daemon_common.cpp @@ -44,6 +44,16 @@ extern "C" { #include "dlt_offline_trace.h" } +#ifndef DLT_USER_DIR +#define DLT_USER_DIR "/tmp/dltpipes" +#endif + +/* Name of named pipe to DLT daemon */ +#ifndef DLT_USER_FIFO +#define DLT_USER_FIFO "/tmp/dlt" +#endif + + /* Begin Method:dlt_daemon_common::dlt_daemon_application_add */ TEST(t_dlt_daemon_application_add, normal) { -- cgit v1.2.1