diff mbox series

[v1,6/7] shm: add uid to shm files

Message ID 1499205607-30786-7-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>


new processes can get some earliar pid numbers which
processes can be already dead. Because of previous process
can be from other user it can have different access right
to that file. To avoid that place shm files to
/dev/shm/uid/odp- files

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/_fdserver.c                 | 25 ++++++++++++++++----
 platform/linux-generic/_ishm.c                     | 27 ++++++++++++++--------
 platform/linux-generic/include/odp_internal.h      |  2 ++
 platform/linux-generic/odp_init.c                  | 12 +++++++++-
 .../validation/api/shmem/shmem_common.h            |  2 +-
 .../validation/api/shmem/shmem_linux.c             | 15 ++++++++----
 .../validation/api/shmem/shmem_odp1.c              |  2 +-
 7 files changed, 62 insertions(+), 23 deletions(-)
diff mbox series

Patch

diff --git a/platform/linux-generic/_fdserver.c b/platform/linux-generic/_fdserver.c
index 20d6e233..7ed5e7a6 100644
--- a/platform/linux-generic/_fdserver.c
+++ b/platform/linux-generic/_fdserver.c
@@ -48,6 +48,7 @@ 
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <signal.h>
 #include <sys/socket.h>
@@ -57,8 +58,9 @@ 
 #include <sys/mman.h>
 #include <sys/wait.h>
 
-#define FDSERVER_SOCKPATH_MAXLEN 32
-#define FDSERVER_SOCKPATH_FORMAT "/dev/shm/odp-%d-fdserver"
+#define FDSERVER_SOCKPATH_MAXLEN 255
+#define FDSERVER_SOCK_FORMAT "/dev/shm/%s/odp-%d-fdserver"
+#define FDSERVER_SOCKDIR_FORMAT "/dev/shm/%s"
 #define FDSERVER_BACKLOG 5
 
 #ifndef MAP_ANONYMOUS
@@ -238,7 +240,8 @@  static int get_socket(void)
 	int len;
 
 	/* construct the named socket path: */
-	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKPATH_FORMAT,
+	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCK_FORMAT,
+		 odp_global_data.uid,
 		 odp_global_data.main_pid);
 
 	s_sock = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -581,8 +584,14 @@  int _odp_fdserver_init_global(void)
 
 	odp_spinlock_init(client_lock);
 
+	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKDIR_FORMAT,
+		 odp_global_data.uid);
+
+	mkdir(sockpath, 0744);
+
 	/* construct the server named socket path: */
-	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKPATH_FORMAT,
+	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCK_FORMAT,
+		 odp_global_data.uid,
 		 odp_global_data.main_pid);
 
 	/* create UNIX domain socket: */
@@ -663,11 +672,17 @@  int _odp_fdserver_term_global(void)
 	wait(&status);
 
 	/* construct the server named socket path: */
-	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKPATH_FORMAT,
+	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCK_FORMAT,
+		 odp_global_data.uid,
 		 odp_global_data.main_pid);
 
 	/* delete the UNIX domain socket: */
 	unlink(sockpath);
 
+	/* delete shm files directory */
+	snprintf(sockpath, FDSERVER_SOCKPATH_MAXLEN, FDSERVER_SOCKDIR_FORMAT,
+		 odp_global_data.uid);
+	unlink(sockpath);
+
 	return 0;
 }
diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c
index c0623f18..6545bfce 100644
--- a/platform/linux-generic/_ishm.c
+++ b/platform/linux-generic/_ishm.c
@@ -86,7 +86,7 @@ 
  * Maximum internal shared memory block name length in chars
  * probably taking the same number as SHM name size make sense at this stage
  */
-#define ISHM_NAME_MAXLEN 32
+#define ISHM_NAME_MAXLEN 128
 
 /*
  * Linux underlying file name: <directory>/odp-<odp_pid>-ishm-<name>
@@ -106,7 +106,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/odp-%d-shm-%s"
+#define ISHM_EXPTNAME_FORMAT "/dev/shm/%s/odp-%d-shm-%s"
 
 /*
  * At worse case the virtual space gets so fragmented that there is
@@ -417,6 +417,7 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 					      *		    /mnt/huge */
 	int  oflag = O_RDWR | O_CREAT | O_TRUNC; /* flags for open	      */
 	FILE *export_file;
+	char dir[ISHM_FILENAME_MAXLEN];
 
 	new_block = &ishm_tbl->block[block_index];
 	name = new_block->name;
@@ -431,17 +432,21 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 		return -1;
 
 	if (huge == HUGE)
-		snprintf(filename, ISHM_FILENAME_MAXLEN,
-			 ISHM_FILENAME_FORMAT,
+		snprintf(dir, ISHM_FILENAME_MAXLEN, "%s/%s",
 			 odp_global_data.hugepage_info.default_huge_page_dir,
-			 odp_global_data.main_pid,
-			 (name && name[0]) ? name : seq_string);
+			 odp_global_data.uid);
 	else
-		snprintf(filename, ISHM_FILENAME_MAXLEN,
-			 ISHM_FILENAME_FORMAT,
+		snprintf(dir, ISHM_FILENAME_MAXLEN, "%s/%s",
 			 ISHM_FILENAME_NORMAL_PAGE_DIR,
-			 odp_global_data.main_pid,
-			 (name && name[0]) ? name : seq_string);
+			 odp_global_data.uid);
+
+	snprintf(filename, ISHM_FILENAME_MAXLEN,
+		 ISHM_FILENAME_FORMAT,
+		 dir,
+		 odp_global_data.main_pid,
+		 (name && name[0]) ? name : seq_string);
+
+	mkdir(dir, 0744);
 
 	fd = open(filename, oflag, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 	if (fd < 0) {
@@ -471,6 +476,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.uid,
 			 odp_global_data.main_pid,
 			 (name && name[0]) ? name : seq_string);
 		export_file = fopen(new_block->exptname, "w");
@@ -949,6 +955,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.uid,
 		 external_odp_pid,
 		 remote_name);
 
diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index 8bae028d..7e6811f3 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -25,6 +25,7 @@  extern "C" {
 #include <sys/types.h>
 
 #define MAX_CPU_NUMBER 128
+#define UID_MAXLEN 30
 
 typedef struct {
 	uint64_t cpu_hz_max[MAX_CPU_NUMBER];
@@ -42,6 +43,7 @@  typedef struct {
 
 struct odp_global_data_s {
 	pid_t main_pid;
+	char uid[UID_MAXLEN];
 	odp_log_func_t log_fn;
 	odp_abort_func_t abort_fn;
 	system_info_t system_info;
diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index b4abb181..f62ccec0 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -17,6 +17,8 @@ 
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <pwd.h>
 
 #define _ODP_FILES_FMT "odp-%d-"
 #define _ODP_TMPDIR    "/dev/shm"
@@ -28,13 +30,16 @@  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;
 
-	dir = opendir(dirpath);
+	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",
@@ -70,10 +75,15 @@  int odp_init_global(odp_instance_t *instance,
 		    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;
diff --git a/test/linux-generic/validation/api/shmem/shmem_common.h b/test/linux-generic/validation/api/shmem/shmem_common.h
index 16227ecd..621e3431 100644
--- a/test/linux-generic/validation/api/shmem/shmem_common.h
+++ b/test/linux-generic/validation/api/shmem/shmem_common.h
@@ -8,7 +8,7 @@ 
 #define _COMMON_TEST_SHMEM_H_
 
 #define ODP_SHM_NAME "odp_linux_shared_mem"
-#define FIFO_NAME_FMT "/tmp/shmem_test_fifo-%d"
+#define FIFO_NAME_FMT "/dev/shm/%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 2f4c7628..d5931447 100644
--- a/test/linux-generic/validation/api/shmem/shmem_linux.c
+++ b/test/linux-generic/validation/api/shmem/shmem_linux.c
@@ -8,7 +8,7 @@ 
  * flag is visible under linux, and checks that memory created with the
  * ODP_SHM_EXPORT flag is visible by other ODP instances.
  * It therefore checks both that the link
- * name under /tmp is correct, and also checks that the memory contents
+ * name under /dev/shm is correct, and also checks that the memory contents
  * is indeed shared.
  * we want:
  * -the odp test to run using C UNIT
@@ -69,6 +69,7 @@ 
 #include <string.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/wait.h>
 #include <linux/limits.h>
 #include <stdio.h>
@@ -77,12 +78,14 @@ 
 #include <libgen.h>
 #include <linux/limits.h>
 #include <inttypes.h>
+#include <pwd.h>
 #include "shmem_linux.h"
 #include "shmem_common.h"
 
 #define ODP_APP1_NAME "shmem_odp1" /* name of the odp1 program, in this dir  */
 #define ODP_APP2_NAME "shmem_odp2" /* name of the odp2 program, in this dir  */
-#define DEVNAME_FMT "/tmp/odp-%" PRIu64 "-shm-%s"  /* odp-<pid>-shm-<name>   */
+/* odp-<pid>-shm-<name> */
+#define DEVNAME_FMT "/dev/shm/%d/odp-%" PRIu64 "-shm-%s"
 #define MAX_FIFO_WAIT 30         /* Max time waiting for the fifo (sec)      */
 
 /*
@@ -108,7 +111,8 @@  static int read_shmem_attribues(uint64_t ext_odp_pid, const char *blockname,
 	char shm_attr_filename[PATH_MAX];
 	FILE *export_file;
 
-	sprintf(shm_attr_filename, DEVNAME_FMT, ext_odp_pid, blockname);
+	sprintf(shm_attr_filename, DEVNAME_FMT, getuid(),
+		ext_odp_pid, blockname);
 
 	/* O_CREAT flag not given => failure if shm_attr_filename does not
 	 * already exist */
@@ -205,6 +209,7 @@  int main(int argc __attribute__((unused)), char *argv[])
 	int shm_fd;
 	test_shared_linux_data_t *addr;
 	int app2_status;
+	uid_t uid = getuid();
 
 	/* odp_app1 is in the same directory as this file: */
 	strncpy(prg_name, argv[0], PATH_MAX - 1);
@@ -223,7 +228,7 @@  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, odp_app1);
+	sprintf(fifo_name, FIFO_NAME_FMT, 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 +249,7 @@  int main(int argc __attribute__((unused)), char *argv[])
 				 &user_len, &user_flags, &align) != 0)
 		test_failure(fifo_name, fifo_fd, odp_app1);
 
-	/* open the shm filename (which is either on /tmp or on hugetlbfs)
+	/* open the shm filename (which is either on /dev/shm/ or on hugetlbfs)
 	 * O_CREAT flag not given => failure if shm_devname does not already
 	 * exist */
 	shm_fd = open(shm_filename, O_RDONLY,
diff --git a/test/linux-generic/validation/api/shmem/shmem_odp1.c b/test/linux-generic/validation/api/shmem/shmem_odp1.c
index 3869c2e1..c402365a 100644
--- a/test/linux-generic/validation/api/shmem/shmem_odp1.c
+++ b/test/linux-generic/validation/api/shmem/shmem_odp1.c
@@ -41,7 +41,7 @@  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, getpid());
+	sprintf(fifo_name, FIFO_NAME_FMT, getuid(), getpid());
 	CU_ASSERT_FATAL(mkfifo(fifo_name, 0666) == 0);
 
 	/* read from the fifo: the linux process result: */