From d2214ac5075c56ba2bbd7f33f16171e1bad6fa48 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Wed, 20 Oct 2021 12:32:42 +0200 Subject: dlt-daemon: Only create directories if they do not exist yet dlt_mkdir_recursive runs mkdir for directories which already exist. This fails if the directory already exists and the dlt-daemon does not have the permissions to change the mode on the directory. On many systems the directories are not created by dlt-daemon itself but rather through the build system or while the root filesystem is created. At this stage the permissions are already set properly and they might be less permissive than what dlt-daemon expects but still sufficient. For example is rwx not necessary for each directory in the tree. Before this commit the following scenario would fail: * Path is set to /mnt/dlt/ * mnt has 755 permissions for root * dlt has 755 for dlt user As dlt daemon would try to run mkdir on /mnt dlt_mkdir_recursive would fail due to lack of permissions although the permissions are sufficient for dlt-daemon to work and write its files into /mnt/dlt/ With this commit /mnt would not be created anymore because it already exists. If the permissions are not sufficient for dlt-daemon to write in it will fail when trying to create files and exit at this point Signed-off-by: Alexander Mohr --- src/daemon/dlt-daemon.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index 955c231..55d5954 100644 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -33,7 +33,7 @@ #include /* for sockaddr_in and inet_addr() */ #include /* for atoi() and exit() */ #include /* for memset() */ -#include /* for close() */ +#include /* for close() and access */ #include #include #include @@ -837,12 +837,16 @@ static int dlt_mkdir_recursive(const char *dir) for (p = tmp + 1; ((*p) && (ret == 0)) || ((ret == -1 && errno == EEXIST) && (p != end)); p++) if (*p == '/') { *p = 0; - ret = mkdir(tmp, - #ifdef DLT_DAEMON_USE_FIFO_IPC - S_IRWXU); - #else - S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH /*S_IRWXU*/); - #endif + + if (access(tmp, F_OK) != 0 && errno == ENOENT) { + ret = mkdir(tmp, + #ifdef DLT_DAEMON_USE_FIFO_IPC + S_IRWXU); + #else + S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH /*S_IRWXU*/); + #endif + } + *p = '/'; } -- cgit v1.2.1