diff mbox series

[v1,7/7] linux-gen: shm: user can define place for shm files

Message ID 1499205607-30786-8-git-send-email-odpbot@yandex.ru
State New
Headers show
Series [v1,1/7] linux-gen: pktio: ipc fix send return code on tx | expand

Commit Message

Github ODP bot July 4, 2017, 10 p.m. UTC
From: Maxim Uvarov <maxim.uvarov@linaro.org>


Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>

---
/** Email created from pull request 67 (muvarov:master_ipc_fixes)
 ** https://github.com/Linaro/odp/pull/67
 ** Patch: https://github.com/Linaro/odp/pull/67.patch
 ** Base sha: ceeab69f3af67701adb524c7b9757d19cefb1110
 ** Merge commit sha: 6f88e14c89b861d66bb7043b22f9b299b1f73ddf
 **/
 platform/linux-generic/_ishm.c                     | 83 ++++++++++++++++++++--
 platform/linux-generic/include/_ishm_internal.h    |  1 +
 platform/linux-generic/include/odp_internal.h      |  1 +
 platform/linux-generic/odp_init.c                  | 72 ++-----------------
 .../validation/api/shmem/shmem_common.h            |  5 +-
 .../validation/api/shmem/shmem_linux.c             |  8 ++-
 .../validation/api/shmem/shmem_odp1.c              |  8 ++-
 .../validation/api/shmem/shmem_odp2.c              |  4 +-
 8 files changed, 102 insertions(+), 80 deletions(-)
diff mbox series

Patch

diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c
index 6545bfce..cde2dbc3 100644
--- a/platform/linux-generic/_ishm.c
+++ b/platform/linux-generic/_ishm.c
@@ -71,6 +71,8 @@ 
 #include <inttypes.h>
 #include <sys/wait.h>
 #include <libgen.h>
+#include <sys/types.h>
+#include <dirent.h>
 
 /*
  * Maximum number of internal shared memory blocks.
@@ -99,6 +101,7 @@ 
 #define ISHM_FILENAME_MAXLEN (ISHM_NAME_MAXLEN + 64)
 #define ISHM_FILENAME_FORMAT "%s/odp-%d-ishm-%s"
 #define ISHM_FILENAME_NORMAL_PAGE_DIR "/dev/shm"
+#define _ODP_FILES_FMT "odp-%d-"
 
 /*
  * when the memory is to be shared with an external entity (such as another
@@ -106,7 +109,7 @@ 
  * export file is created describing the exported memory: this defines the
  * location and the filename format of this description file
  */
-#define ISHM_EXPTNAME_FORMAT "/dev/shm/%s/odp-%d-shm-%s"
+#define ISHM_EXPTNAME_FORMAT "%s/%s/odp-%d-shm-%s"
 
 /*
  * At worse case the virtual space gets so fragmented that there is
@@ -437,7 +440,7 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 			 odp_global_data.uid);
 	else
 		snprintf(dir, ISHM_FILENAME_MAXLEN, "%s/%s",
-			 ISHM_FILENAME_NORMAL_PAGE_DIR,
+			 odp_global_data.shm_dir,
 			 odp_global_data.uid);
 
 	snprintf(filename, ISHM_FILENAME_MAXLEN,
@@ -476,6 +479,7 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 			ISHM_FILENAME_MAXLEN - 1);
 		snprintf(new_block->exptname, ISHM_FILENAME_MAXLEN,
 			 ISHM_EXPTNAME_FORMAT,
+			 odp_global_data.shm_dir,
 			 odp_global_data.uid,
 			 odp_global_data.main_pid,
 			 (name && name[0]) ? name : seq_string);
@@ -955,6 +959,7 @@  int _odp_ishm_find_exported(const char *remote_name, pid_t external_odp_pid,
 	/* try to read the block description file: */
 	snprintf(export_filename, ISHM_FILENAME_MAXLEN,
 		 ISHM_EXPTNAME_FORMAT,
+		 odp_global_data.shm_dir,
 		 odp_global_data.uid,
 		 external_odp_pid,
 		 remote_name);
@@ -1383,22 +1388,86 @@  static int do_odp_ishm_init_local(void)
 	return 0;
 }
 
+/* remove all files staring with "odp-<pid>" from a directory "dir" */
+int _odp_ishm_cleanup_files(const char *dirpath)
+{
+	struct dirent *e;
+	DIR *dir;
+	char userdir[PATH_MAX];
+	char prefix[PATH_MAX];
+	char *fullpath;
+	int d_len = strlen(dirpath);
+	int p_len;
+	int f_len;
+
+	snprintf(userdir, PATH_MAX, "%s/%s", dirpath, odp_global_data.uid);
+
+	dir = opendir(userdir);
+	if (!dir) {
+		/* ok if the dir does not exist. no much to delete then! */
+		ODP_DBG("opendir failed for %s: %s\n",
+			dirpath, strerror(errno));
+		return 0;
+	}
+	snprintf(prefix, PATH_MAX, _ODP_FILES_FMT, odp_global_data.main_pid);
+	p_len = strlen(prefix);
+	while ((e = readdir(dir)) != NULL) {
+		if (strncmp(e->d_name, prefix, p_len) == 0) {
+			f_len = strlen(e->d_name);
+			fullpath = malloc(d_len + f_len + 2);
+			if (fullpath == NULL) {
+				closedir(dir);
+				return -1;
+			}
+			snprintf(fullpath, PATH_MAX, "%s/%s",
+				 dirpath, e->d_name);
+			ODP_DBG("deleting obsolete file: %s\n", fullpath);
+			if (unlink(fullpath))
+				ODP_ERR("unlink failed for %s: %s\n",
+					fullpath, strerror(errno));
+			free(fullpath);
+		}
+	}
+	closedir(dir);
+
+	return 0;
+}
+
 int _odp_ishm_init_global(void)
 {
 	void *addr;
 	void *spce_addr;
 	int i;
+	uid_t uid;
 
-	if ((getpid() != odp_global_data.main_pid) ||
-	    (syscall(SYS_gettid) != getpid()))
-		ODP_ERR("odp_init_global() must be performed by the main "
+	odp_global_data.main_pid = getpid();
+	odp_global_data.shm_dir = getenv("ODP_SHM_DIR");
+	odp_global_data.shm_dir =
+		calloc(1, sizeof(ISHM_FILENAME_NORMAL_PAGE_DIR));
+	sprintf(odp_global_data.shm_dir, "%s", ISHM_FILENAME_NORMAL_PAGE_DIR);
+
+	ODP_DBG("ishm: using dir %s\n", odp_global_data.shm_dir);
+
+	uid = getuid();
+	snprintf(odp_global_data.uid, UID_MAXLEN, "%d",
+		 uid);
+
+	if ((syscall(SYS_gettid)) != odp_global_data.main_pid) {
+		ODP_ERR("ishm init must be performed by the main "
 			"ODP process!\n.");
+		return -1;
+	}
 
 	if (!odp_global_data.hugepage_info.default_huge_page_dir)
 		ODP_DBG("NOTE: No support for huge pages\n");
-	else
+	else {
 		ODP_DBG("Huge pages mount point is: %s\n",
 			odp_global_data.hugepage_info.default_huge_page_dir);
+		_odp_ishm_cleanup_files(
+			odp_global_data.hugepage_info.default_huge_page_dir);
+	}
+
+	_odp_ishm_cleanup_files(odp_global_data.shm_dir);
 
 	/* allocate space for the internal shared mem block table: */
 	addr = mmap(NULL, sizeof(ishm_table_t),
@@ -1592,6 +1661,8 @@  int _odp_ishm_term_global(void)
 	if (_odp_ishmphy_unbook_va())
 		ret |= -1;
 
+	free(odp_global_data.shm_dir);
+
 	return ret;
 }
 
diff --git a/platform/linux-generic/include/_ishm_internal.h b/platform/linux-generic/include/_ishm_internal.h
index c7c33077..005d6b55 100644
--- a/platform/linux-generic/include/_ishm_internal.h
+++ b/platform/linux-generic/include/_ishm_internal.h
@@ -44,6 +44,7 @@  int   _odp_ishm_find_exported(const char *remote_name,
 void *_odp_ishm_address(int block_index);
 int   _odp_ishm_info(int block_index, _odp_ishm_info_t *info);
 int   _odp_ishm_status(const char *title);
+int _odp_ishm_cleanup_files(const char *dirpath);
 
 #ifdef __cplusplus
 }
diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index 7e6811f3..dd3374b7 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -42,6 +42,7 @@  typedef struct {
 } hugepage_info_t;
 
 struct odp_global_data_s {
+	char *shm_dir; /*< directory for odp mmaped files */
 	pid_t main_pid;
 	char uid[UID_MAXLEN];
 	odp_log_func_t log_fn;
diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index f62ccec0..ab2d6fb0 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -20,70 +20,15 @@ 
 #include <sys/types.h>
 #include <pwd.h>
 
-#define _ODP_FILES_FMT "odp-%d-"
-#define _ODP_TMPDIR    "/dev/shm"
-
 struct odp_global_data_s odp_global_data;
 
-/* remove all files staring with "odp-<pid>" from a directory "dir" */
-static int cleanup_files(const char *dirpath, int odp_pid)
-{
-	struct dirent *e;
-	DIR *dir;
-	char userdir[PATH_MAX];
-	char prefix[PATH_MAX];
-	char *fullpath;
-	int d_len = strlen(dirpath);
-	int p_len;
-	int f_len;
-
-	snprintf(userdir, PATH_MAX, "%s/%s", dirpath, odp_global_data.uid);
-
-	dir = opendir(userdir);
-	if (!dir) {
-		/* ok if the dir does not exist. no much to delete then! */
-		ODP_DBG("opendir failed for %s: %s\n",
-			dirpath, strerror(errno));
-		return 0;
-	}
-	snprintf(prefix, PATH_MAX, _ODP_FILES_FMT, odp_pid);
-	p_len = strlen(prefix);
-	while ((e = readdir(dir)) != NULL) {
-		if (strncmp(e->d_name, prefix, p_len) == 0) {
-			f_len = strlen(e->d_name);
-			fullpath = malloc(d_len + f_len + 2);
-			if (fullpath == NULL) {
-				closedir(dir);
-				return -1;
-			}
-			snprintf(fullpath, PATH_MAX, "%s/%s",
-				 dirpath, e->d_name);
-			ODP_DBG("deleting obsolete file: %s\n", fullpath);
-			if (unlink(fullpath))
-				ODP_ERR("unlink failed for %s: %s\n",
-					fullpath, strerror(errno));
-			free(fullpath);
-		}
-	}
-	closedir(dir);
-
-	return 0;
-}
-
 int odp_init_global(odp_instance_t *instance,
 		    const odp_init_t *params,
 		    const odp_platform_init_t *platform_params ODP_UNUSED)
 {
-	char *hpdir;
-	uid_t uid;
-
 	memset(&odp_global_data, 0, sizeof(struct odp_global_data_s));
 	odp_global_data.main_pid = getpid();
 
-	uid = getuid();
-	snprintf(odp_global_data.uid, UID_MAXLEN, "%d",
-		 uid);
-
 	enum init_stage stage = NO_INIT;
 	odp_global_data.log_fn = odp_override_log;
 	odp_global_data.abort_fn = odp_override_abort;
@@ -95,8 +40,6 @@  int odp_init_global(odp_instance_t *instance,
 			odp_global_data.abort_fn = params->abort_fn;
 	}
 
-	cleanup_files(_ODP_TMPDIR, odp_global_data.main_pid);
-
 	if (odp_cpumask_init_global(params)) {
 		ODP_ERR("ODP cpumask init failed.\n");
 		goto init_failed;
@@ -113,23 +56,20 @@  int odp_init_global(odp_instance_t *instance,
 		ODP_ERR("ODP system_info init failed.\n");
 		goto init_failed;
 	}
-	hpdir = odp_global_data.hugepage_info.default_huge_page_dir;
-	/* cleanup obsolete huge page files, if any */
-	if (hpdir)
-		cleanup_files(hpdir, odp_global_data.main_pid);
 	stage = SYSINFO_INIT;
 
+	if (_odp_ishm_init_global()) {
+		ODP_ERR("ODP ishm init failed.\n");
+		goto init_failed;
+	}
+	stage = ISHM_INIT;
+
 	if (_odp_fdserver_init_global()) {
 		ODP_ERR("ODP fdserver init failed.\n");
 		goto init_failed;
 	}
 	stage = FDSERVER_INIT;
 
-	if (_odp_ishm_init_global()) {
-		ODP_ERR("ODP ishm init failed.\n");
-		goto init_failed;
-	}
-	stage = ISHM_INIT;
 
 	if (odp_thread_init_global()) {
 		ODP_ERR("ODP thread init failed.\n");
diff --git a/test/linux-generic/validation/api/shmem/shmem_common.h b/test/linux-generic/validation/api/shmem/shmem_common.h
index 621e3431..e6c04d81 100644
--- a/test/linux-generic/validation/api/shmem/shmem_common.h
+++ b/test/linux-generic/validation/api/shmem/shmem_common.h
@@ -7,8 +7,9 @@ 
 #ifndef _COMMON_TEST_SHMEM_H_
 #define _COMMON_TEST_SHMEM_H_
 
-#define ODP_SHM_NAME "odp_linux_shared_mem"
-#define FIFO_NAME_FMT "/dev/shm/%d/shmem_test_fifo-%d"
+#define SHM_NAME "odp_linux_shared_mem"
+#define DEFAULT_SHM_DIR "/dev/shm"
+#define FIFO_NAME_FMT "/%s/%d/shmem_test_fifo-%d"
 #define ALIGN_SIZE  (128)
 #define TEST_SHARE_FOO (0xf0f0f0f0)
 #define TEST_SHARE_BAR (0xf0f0f0f)
diff --git a/test/linux-generic/validation/api/shmem/shmem_linux.c b/test/linux-generic/validation/api/shmem/shmem_linux.c
index d5931447..e7e699e7 100644
--- a/test/linux-generic/validation/api/shmem/shmem_linux.c
+++ b/test/linux-generic/validation/api/shmem/shmem_linux.c
@@ -79,6 +79,7 @@ 
 #include <linux/limits.h>
 #include <inttypes.h>
 #include <pwd.h>
+#include <stdlib.h>
 #include "shmem_linux.h"
 #include "shmem_common.h"
 
@@ -210,6 +211,7 @@  int main(int argc __attribute__((unused)), char *argv[])
 	test_shared_linux_data_t *addr;
 	int app2_status;
 	uid_t uid = getuid();
+	char *shm_dir = getenv("ODP_SHM_DIR");
 
 	/* odp_app1 is in the same directory as this file: */
 	strncpy(prg_name, argv[0], PATH_MAX - 1);
@@ -228,7 +230,9 @@  int main(int argc __attribute__((unused)), char *argv[])
 	/* wait max 30 sec for the fifo to be created by the ODP side.
 	 * Just die if time expire as there is no fifo to communicate
 	 * through... */
-	sprintf(fifo_name, FIFO_NAME_FMT, uid, odp_app1);
+	sprintf(fifo_name, FIFO_NAME_FMT,
+		shm_dir ? shm_dir : DEFAULT_SHM_DIR,
+		uid, odp_app1);
 	for (nb_sec = 0; nb_sec < MAX_FIFO_WAIT; nb_sec++) {
 		fifo_fd = open(fifo_name, O_WRONLY);
 		if (fifo_fd >= 0)
@@ -244,7 +248,7 @@  int main(int argc __attribute__((unused)), char *argv[])
 	 * check to see if linux can see the created shared memory: */
 
 	/* read the shared memory attributes (includes the shm filename): */
-	if (read_shmem_attribues(odp_app1, ODP_SHM_NAME,
+	if (read_shmem_attribues(odp_app1, SHM_NAME,
 				 shm_filename, &len, &flags,
 				 &user_len, &user_flags, &align) != 0)
 		test_failure(fifo_name, fifo_fd, odp_app1);
diff --git a/test/linux-generic/validation/api/shmem/shmem_odp1.c b/test/linux-generic/validation/api/shmem/shmem_odp1.c
index c402365a..26abc94b 100644
--- a/test/linux-generic/validation/api/shmem/shmem_odp1.c
+++ b/test/linux-generic/validation/api/shmem/shmem_odp1.c
@@ -11,6 +11,7 @@ 
 #include <stdio.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <stdlib.h>
 
 #include <odp_cunit_common.h>
 #include "shmem_odp1.h"
@@ -26,9 +27,10 @@  void shmem_test_odp_shm_proc(void)
 	odp_shm_t shm;
 	test_shared_data_t *test_shared_data;
 	char test_result;
+	char *shm_dir = getenv("ODP_SHM_DIR");
 
 	/* reminder: ODP_SHM_PROC => export to linux, ODP_SHM_EXPORT=>to odp */
-	shm = odp_shm_reserve(ODP_SHM_NAME,
+	shm = odp_shm_reserve(SHM_NAME,
 			      sizeof(test_shared_data_t),
 			      ALIGN_SIZE, ODP_SHM_PROC | ODP_SHM_EXPORT);
 	CU_ASSERT_FATAL(ODP_SHM_INVALID != shm);
@@ -41,7 +43,9 @@  void shmem_test_odp_shm_proc(void)
 
 	/* open the fifo: this will indicate to linux process that it can
 	 * start the shmem lookups and check if it sees the data */
-	sprintf(fifo_name, FIFO_NAME_FMT, getuid(), getpid());
+	sprintf(fifo_name, FIFO_NAME_FMT,
+		shm_dir ? shm_dir : DEFAULT_SHM_DIR,
+		getuid(), getpid());
 	CU_ASSERT_FATAL(mkfifo(fifo_name, 0666) == 0);
 
 	/* read from the fifo: the linux process result: */
diff --git a/test/linux-generic/validation/api/shmem/shmem_odp2.c b/test/linux-generic/validation/api/shmem/shmem_odp2.c
index 7d8c682b..2a4b67d7 100644
--- a/test/linux-generic/validation/api/shmem/shmem_odp2.c
+++ b/test/linux-generic/validation/api/shmem/shmem_odp2.c
@@ -53,8 +53,8 @@  int main(int argc, char *argv[])
 	odp1 = (odp_instance_t)atoi(argv[1]);
 
 	printf("shmem_odp2: trying to grab %s from pid %d\n",
-	       ODP_SHM_NAME, (int)odp1);
-	shm = odp_shm_import(ODP_SHM_NAME, odp1, ODP_SHM_NAME);
+	       SHM_NAME, (int)odp1);
+	shm = odp_shm_import(SHM_NAME, odp1, SHM_NAME);
 	if (shm == ODP_SHM_INVALID) {
 		fprintf(stderr, "error: odp_shm_lookup_external failed.\n");
 		return 1;