diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b85212..c794866 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -380,6 +380,7 @@ if(WITH_GIT_SUBMODULE) endif() if(WITH_DLT_UNIT_TESTS) + set(DLT_IPC "UNIX_SOCKET") find_package(GTest) if(GTEST_FOUND) find_package(PkgConfig REQUIRED) diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index 15ac863..bb28a4f 100644 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -1590,9 +1590,13 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local) /* open named pipe(FIFO) to receive DLT messages from users */ umask(0); - /* Try to delete existing pipe, ignore result of unlink */ + /* Valid fifo means there is a daemon running, stop init phase of the new */ const char *tmpFifo = daemon_local->flags.daemonFifoName; - unlink(tmpFifo); + if (access(tmpFifo, F_OK) == 0) { + dlt_vlog(LOG_WARNING, "FIFO user %s is in use (%s)!\n", + tmpFifo, strerror(errno)); + return -1; + } ret = mkfifo(tmpFifo, S_IRUSR | S_IWUSR | S_IWGRP); @@ -1602,13 +1606,30 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local) return -1; } /* if */ + const char* nameDir = "/tmp"; + int dir_fd; + dir_fd = open(nameDir, O_RDONLY); + if (dir_fd == -1) { + dlt_vlog(LOG_WARNING, "Directory %s of fifo cannot be opened (%s)!\n", + nameDir, strerror(errno)); + return -1; + } + + fd = openat(dir_fd, tmpFifo, O_RDWR); + + if (fd == -1) { + dlt_vlog(LOG_WARNING, "FIFO user %s cannot be opened (%s)!\n", + tmpFifo, strerror(errno)); + return -1; + } /* if */ + /* Set group of daemon FIFO */ if (daemon_local->flags.daemonFifoGroup[0] != 0) { errno = 0; struct group *group_dlt = getgrnam(daemon_local->flags.daemonFifoGroup); if (group_dlt) { - ret = chown(tmpFifo, -1, group_dlt->gr_gid); + ret = fchown(fd, -1, group_dlt->gr_gid); if (ret == -1) dlt_vlog(LOG_ERR, "FIFO user %s cannot be chowned to group %s (%s)\n", @@ -1628,14 +1649,6 @@ static int dlt_daemon_init_fifo(DltDaemonLocal *daemon_local) } } - fd = open(tmpFifo, O_RDWR); - - if (fd == -1) { - dlt_vlog(LOG_WARNING, "FIFO user %s cannot be opened (%s)!\n", - tmpFifo, strerror(errno)); - return -1; - } /* if */ - #ifdef __linux__ /* F_SETPIPE_SZ and F_GETPIPE_SZ are only supported for Linux. * For other OSes it depends on its system e.g. pipe manager.