From ccec00f7817de0bc13e034a010a4d63bee3abc70 Mon Sep 17 00:00:00 2001
From: Gleb Natapov <gleb@cloudius-systems.com>
Date: Mon, 10 Mar 2014 10:24:03 +0200
Subject: [PATCH] tests: add System V shared memory test

Signed-off-by: Gleb Natapov <gleb@cloudius-systems.com>
---
 build.mk         |  1 +
 tests/tst-shm.cc | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
 create mode 100644 tests/tst-shm.cc

diff --git a/build.mk b/build.mk
index 0bb5ca3ce..556131223 100644
--- a/build.mk
+++ b/build.mk
@@ -245,6 +245,7 @@ tests += tests/tst-chdir.so
 tests += tests/tst-hello.so
 tests += tests/tst-concurrent-init.so
 tests += tests/tst-ring-spsc-wraparound.so
+tests += tests/tst-shm.so
 
 tests/hello/Hello.class: javabase=tests/hello
 
diff --git a/tests/tst-shm.cc b/tests/tst-shm.cc
new file mode 100644
index 000000000..bcad510ad
--- /dev/null
+++ b/tests/tst-shm.cc
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2014 Cloudius Systems, Ltd.
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+#include <sys/mman.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <string.h>
+#include <iostream>
+#include <cstdlib>
+#include <errno.h>
+
+int main(int argc, char **argv)
+{
+  int ret = 0;
+  std::cerr << "Running shm tests" << std::endl;
+  key_t shmkey = 1;
+  int shmid = shmget(shmkey, 1024, IPC_CREAT | 0666);
+  if (shmid < 0) {
+    std::cerr << "Cannot create shm segment for key " << shmkey << ": " << strerror(errno) << std::endl;
+    return 1;
+  }
+  char *addr1 = (char*)shmat(shmid, NULL, 0);
+  if (addr1 == MAP_FAILED) {
+    std::cerr << "Cannot attach to shm id " << shmid << ": " << strerror(errno) << std::endl;
+    return 1;
+  }
+  std::cerr << "Attached addr1: " << static_cast<const void*>(addr1) << std::endl;
+  char *addr2 = (char*)shmat(shmid, NULL, 0);
+  if (addr2 == MAP_FAILED) {
+    std::cerr << "Cannot attach to shm id " << shmid << " second time: " << strerror(errno) << std::endl;
+    return 1;
+  }
+  std::cerr << "Attached addr2: " << static_cast<const void*>(addr2) << std::endl;
+  strcpy(addr1, "shmem test");
+  if (strcmp(addr1, addr2) != 0) {
+    std::cerr << "shm test failed" << std::endl;
+    ret = 1;
+  } else {
+    std::cerr << "shm test succeeded" << std::endl;
+  }
+  shmdt(addr1);
+  shmdt(addr2);
+  shmctl(shmid, IPC_RMID, NULL);
+  return ret;
+}
-- 
GitLab