From patchwork Tue Jul 4 22:00:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 107033 Delivered-To: patch@linaro.org Received: by 10.140.101.44 with SMTP id t41csp59284qge; Tue, 4 Jul 2017 15:04:15 -0700 (PDT) X-Received: by 10.55.221.220 with SMTP id u89mr48299388qku.236.1499205855604; Tue, 04 Jul 2017 15:04:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499205855; cv=none; d=google.com; s=arc-20160816; b=fPHhMOf7tDaSpkinNrq5Q0ESXnqHfvhYfS/TWFDNCx6wuiRRERtrnpCU+KPSdTf2g2 JIRykI+z9IU35dYKOfXoRYcMUEZDIjGMhrhmYncZg0UcRqfU+R6UMldC3xn3AAzovDS4 ybc6cXnhyL86HFW+5bxfRpsmvJWVxbmWw+FgcngQRTgthcjB8YhcWJGPoAIyd0sxuBZh 1RA6kGzW0S9A25vjM2j/Q2xEWOrzDclZFsHuVmPYuNFfSmlGb0Im/QisJsfCSq4qKiQl OvY5hmpaTj5/BGZLpm75uCiQCBVjY59Msq1Znd6yUeuTwGgaI6Bxdgw6s6pq5wOIPl5V woGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=+HI+qrZ4US7lPJl5yvVIcRq/B30EtUpOa6tpES665Ok=; b=VjDeI/P4jcpFhI5np6+qE06fEMfXtLadYfs+0a9dYpycfwBrKwI1bIYiaHi71ECjtu iHjxWuC55RtBTDa2GmCC/3Wubde5tcJGGWWwzAgQOSQftAiBGvMyodR8z40HEomgJ511 mDdVNoWOuLiNRxXVCMES5zIQP5awooQefuGAfEX01vxvTJgQR8Xud4wD3gXNMiJrzRLb rK6+oc6v1t9VdoDcvW9spCazrzMHGJYPAgMCSZPQbOjoDMiZDdBVJGUnSOHdLXVgIH32 /bd6Y4yvnO01FDoWkPZ1VD1G4kzdhY6szDpL+0oBmn6xxUKpOUU7+Cepe/HPobwYXEhH B//g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id o187si18034749qkf.157.2017.07.04.15.04.15; Tue, 04 Jul 2017 15:04:15 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id 38E6160C13; Tue, 4 Jul 2017 22:04:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 7030A60C0D; Tue, 4 Jul 2017 22:01:14 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 0716660C0E; Tue, 4 Jul 2017 22:01:08 +0000 (UTC) Received: from forward2p.cmail.yandex.net (forward2p.cmail.yandex.net [77.88.31.17]) by lists.linaro.org (Postfix) with ESMTPS id 419AE60C0E for ; Tue, 4 Jul 2017 22:00:19 +0000 (UTC) Received: from smtp2p.mail.yandex.net (smtp2p.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b6:7]) by forward2p.cmail.yandex.net (Yandex) with ESMTP id 2C33921802 for ; Wed, 5 Jul 2017 01:00:17 +0300 (MSK) Received: from smtp2p.mail.yandex.net (localhost.localdomain [127.0.0.1]) by smtp2p.mail.yandex.net (Yandex) with ESMTP id C30201A8003E for ; Wed, 5 Jul 2017 01:00:16 +0300 (MSK) Received: by smtp2p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id b3wfOILtJj-0FPGCZtl; Wed, 05 Jul 2017 01:00:15 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) X-Yandex-Suid-Status: 1 0 From: Github ODP bot To: lng-odp@lists.linaro.org Date: Wed, 5 Jul 2017 01:00:06 +0300 Message-Id: <1499205607-30786-7-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1499205607-30786-1-git-send-email-odpbot@yandex.ru> References: <1499205607-30786-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 67 Subject: [lng-odp] [PATCH v1 6/7] shm: add uid to shm files X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Maxim Uvarov 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 --- /** 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 --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 #include #include +#include #include #include #include @@ -57,8 +58,9 @@ #include #include -#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: /odp--ishm- @@ -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 #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 #include #include +#include +#include #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 #include #include +#include #include #include #include @@ -77,12 +78,14 @@ #include #include #include +#include #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--shm- */ +/* odp--shm- */ +#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: */