diff options
author | Saya Sugiura <ssugiura@jp.adit-jv.com> | 2018-05-22 20:40:51 +0900 |
---|---|---|
committer | Saya Sugiura <ssugiura@jp.adit-jv.com> | 2019-04-24 17:13:57 +0900 |
commit | 1348d1c07dbda69c32317738b8e9c69734f181c3 (patch) | |
tree | 2fe7669cd7d20bc2ee2d5ab5a24a6ebe2bd32ef5 | |
parent | 37e8bc12a7e86fa54b3a5f260b45fd730a5a59a6 (diff) | |
download | DLT-daemon-1348d1c07dbda69c32317738b8e9c69734f181c3.tar.gz |
lib: daemon: Fix sem lock potential issue
sem_wait may return an error if a signal is received while waiting for
the lock,and therefore not take the lock before returning.
Changed to have a while loop.
Signed-off-by: Saya Sugiura <ssugiura@jp.adit-jv.com>
-rw-r--r-- | include/dlt/dlt_user.h | 7 | ||||
-rw-r--r-- | src/daemon/dlt_daemon_common.h | 8 |
2 files changed, 11 insertions, 4 deletions
diff --git a/include/dlt/dlt_user.h b/include/dlt/dlt_user.h index d736032..b885eaf 100644 --- a/include/dlt/dlt_user.h +++ b/include/dlt/dlt_user.h @@ -94,8 +94,11 @@ extern "C" { # define DLT_USER_RESENDBUF_MAX_SIZE (DLT_USER_BUF_MAX_SIZE + 100) /**< Size of resend buffer; Max DLT message size is 1390 bytes plus some extra header space */ /* Use a semaphore or mutex from your OS to prevent concurrent access to the DLT buffer. */ -# define DLT_SEM_LOCK() { sem_wait(&dlt_mutex); } -# define DLT_SEM_FREE() { sem_post(&dlt_mutex); } +#define DLT_SEM_LOCK() do{\ + while ((sem_wait(&dlt_mutex) == -1) && (errno == EINTR)) \ + continue; /* Restart if interrupted */ \ + } while(0) +#define DLT_SEM_FREE() { sem_post(&dlt_mutex); } /** * This structure is used for every context used in an application. diff --git a/src/daemon/dlt_daemon_common.h b/src/daemon/dlt_daemon_common.h index 112cb92..be29bca 100644 --- a/src/daemon/dlt_daemon_common.h +++ b/src/daemon/dlt_daemon_common.h @@ -97,8 +97,12 @@ extern "C" { /* Use a semaphore or mutex from your OS to prevent concurrent access to the DLT buffer. */ -# define DLT_DAEMON_SEM_LOCK() { sem_wait(&dlt_daemon_mutex); } -# define DLT_DAEMON_SEM_FREE() { sem_post(&dlt_daemon_mutex); } +#define DLT_DAEMON_SEM_LOCK() do{\ + while ((sem_wait(&dlt_daemon_mutex) == -1) && (errno == EINTR)) \ + continue; /* Restart if interrupted */ \ + } while(0) + +#define DLT_DAEMON_SEM_FREE() { sem_post(&dlt_daemon_mutex); } extern sem_t dlt_daemon_mutex; /** |