Commit 25028e46 authored by Adam Wujek's avatar Adam Wujek 💬

userspace/libwr: check shmem consistency in wrs_shm_get_and_check

Check the consistency of shmem during opening with wrs_shm_get_and_check.
It was possible that sequence number was increased during the opening, but it
was interpreted as version error.

Add defines describing different return errors.

This change is backward compatible.
Signed-off-by: Adam Wujek's avatarAdam Wujek <adam.wujek@cern.ch>
parent 477f1cef
...@@ -45,6 +45,12 @@ struct wrs_shm_head { ...@@ -45,6 +45,12 @@ struct wrs_shm_head {
#define WRS_SHM_LOCK_MASK 0x0001 #define WRS_SHM_LOCK_MASK 0x0001
/* return values of wrs_shm_get_and_check */
#define WRS_SHM_OPEN_OK 0x0001
#define WRS_SHM_OPEN_FAILED 0x0001
#define WRS_SHM_WRONG_VERSION 0x0002
#define WRS_SHM_INCONSISTENT_DATA 0x0003
/* Set custom path for shmem */ /* Set custom path for shmem */
void wrs_shm_set_path(char *new_path); void wrs_shm_set_path(char *new_path);
......
...@@ -141,25 +141,33 @@ int wrs_shm_put(void *headptr) ...@@ -141,25 +141,33 @@ int wrs_shm_put(void *headptr)
/* Open shmem and check if data is available /* Open shmem and check if data is available
* return 0 when ok, otherwise error * return 0 when ok, otherwise error
* 1 when openning shmem failed * 1 when openning shmem failed
* 2 when version is 0 */ * 2 when version is 0
* 3 when data in shmem is inconsistent, function shall be called again
*/
int wrs_shm_get_and_check(enum wrs_shm_name shm_name, int wrs_shm_get_and_check(enum wrs_shm_name shm_name,
struct wrs_shm_head **head) struct wrs_shm_head **head)
{ {
int ii; int ii;
int version; int version;
int ret;
/* try to open shmem */ /* try to open shmem */
if (!(*head) && !(*head = wrs_shm_get(shm_name, "", if (!(*head) && !(*head = wrs_shm_get(shm_name, "",
WRS_SHM_READ | WRS_SHM_LOCKED))) { WRS_SHM_READ | WRS_SHM_LOCKED))) {
return 1; return WRS_SHM_OPEN_FAILED;
} }
ii = wrs_shm_seqbegin(*head); ii = wrs_shm_seqbegin(*head);
/* read head version */ /* read head version */
version = (*head)->version; version = (*head)->version;
if (wrs_shm_seqretry(*head, ii) || !version) { ret = wrs_shm_seqretry(*head, ii);
/* data in shmem available and version not zero */ if (ret) {
return 2; /* inconsistent data in shmem */
return WRS_SHM_INCONSISTENT_DATA;
}
if (!version) {
/* data in shmem available and version is zero */
return WRS_SHM_WRONG_VERSION;
} }
/* all ok */ /* all ok */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment