From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id A7BFDA00C5; Sun, 26 Apr 2020 17:28:51 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 4886C1BF8E; Sun, 26 Apr 2020 17:28:39 +0200 (CEST) Received: from mail-lj1-f196.google.com (mail-lj1-f196.google.com [209.85.208.196]) by dpdk.org (Postfix) with ESMTP id 214A91BF76 for ; Sun, 26 Apr 2020 17:28:36 +0200 (CEST) Received: by mail-lj1-f196.google.com with SMTP id l19so14879650lje.10 for ; Sun, 26 Apr 2020 08:28:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=azDOlfmk1GQwE0ooSyeCwDczGhulza2iBFr3QlD7rt0=; b=k5WiCc+4lleUGvlxuKnEQrT0ufU+TtQARbo8tucoBKph4xqOkSRDezDWge2IPf5fCI tOzmgBH4Sz6dzOjmO/OQUcr6swz5jCLK5/r8RRfIWhKqiETb3nFgc+U8V27mVZJ3jD3y RZeBkmpJUSLHXPXFDYPkRo5UgPq5xPl5kVaoagB26QtF7sFX0vnJAuxSyWInf+1uN4cF BWpP80R+UWHwBhBSYF/tWCE3vA/bjrBuTzqOk4yyq7ep0A3BkjIP4p8g4kEWJddugmtk o9dfhZtFIEJRWjRNj+iPZ1HT7Xfp5K00KpGU/b6ECOtYsL9xSa4E9D0ye/bQKkdFU22K LLxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=azDOlfmk1GQwE0ooSyeCwDczGhulza2iBFr3QlD7rt0=; b=T0Th4MGEl1zI/9e0JDV+2MnMq4Tbp6H+TKzLlb8vkFc7P7q1jlXTsPpv7mADw2Pz3p +0WH+LW5wiMBBvcrb2W0iVwsD/qZCvZ5g+1dY4ZD7fTaGoclnRUMvDIbfjNiuQuwmftN MBeIrCvaME52eTE2Qf/1/gXuykoFabeUByrwxBOl1vGTPeWYA/qpDvc8fsHJA/G1/41W bQr+mBqFiIybIpmIK81IYPunP16UYUbY57L25m+sLJ5RHgC+FQTn63tmEotskjUbprJo 7hHQ3hVUqnLup1zie6rK0KsJSkQ1ghXBnUSKr2Qo5OVG3pP1adwLRaNceb8cSMDKa262 LzSQ== X-Gm-Message-State: AGi0PubisVn9SG4LlOwRf2ED6sPPBujO444Dfrx3hh5vk/c1O0KnXJn/ kxEtIO/eSl0923PTcQ9ZijImqw+a63s= X-Google-Smtp-Source: APiQypJddpBZ4HubTwSo0BdYuvwAHPLIPVIlsQYSlMSXxC5wf4HHeHde5VHuKEoB0r6lWppCMr7lGg== X-Received: by 2002:a2e:9456:: with SMTP id o22mr11774201ljh.94.1587914915274; Sun, 26 Apr 2020 08:28:35 -0700 (PDT) Received: from localhost.localdomain (broadband-37-110-65-23.ip.moscow.rt.ru. [37.110.65.23]) by smtp.googlemail.com with ESMTPSA id p13sm8211340ljg.103.2020.04.26.08.28.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Apr 2020 08:28:34 -0700 (PDT) From: Dmitry Kozlyuk To: dev@dpdk.org Cc: Pallavi Kadam , Narcisa Ana Maria Vasile , Ranjit Menon , Dmitry Kozlyuk , Jerin Jacob , Sunil Kumar Kori , Bruce Richardson Date: Sun, 26 Apr 2020 18:28:17 +0300 Message-Id: <20200426152819.2496610-3-dmitry.kozliuk@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200426152819.2496610-1-dmitry.kozliuk@gmail.com> References: <20200426032245.2437733-1-dmitry.kozliuk@gmail.com> <20200426152819.2496610-1-dmitry.kozliuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2 2/3] eal: add internal directory management API X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add functions for handling directories in a platform-independent way: * eal_persistent_data_path() * eal_dir_create() Currently, only tracing requires this API for its common code. Signed-off-by: Dmitry Kozlyuk --- .../common/eal_common_trace_utils.c | 26 +++------- lib/librte_eal/common/eal_filesystem.h | 30 ++++++++++- lib/librte_eal/freebsd/Makefile | 4 ++ lib/librte_eal/linux/Makefile | 4 ++ lib/librte_eal/meson.build | 4 ++ lib/librte_eal/unix/eal_unix_filesystem.c | 51 +++++++++++++++++++ lib/librte_eal/unix/meson.build | 6 +++ 7 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 lib/librte_eal/unix/eal_unix_filesystem.c create mode 100644 lib/librte_eal/unix/meson.build diff --git a/lib/librte_eal/common/eal_common_trace_utils.c b/lib/librte_eal/common/eal_common_trace_utils.c index fce8892c3..78df3b41e 100644 --- a/lib/librte_eal/common/eal_common_trace_utils.c +++ b/lib/librte_eal/common/eal_common_trace_utils.c @@ -3,8 +3,6 @@ */ #include -#include -#include #include #include @@ -321,22 +319,14 @@ trace_dir_default_path_get(char *dir_path) { struct trace *trace = trace_obj_get(); uint32_t size = sizeof(trace->dir); - struct passwd *pwd; - char *home_dir; - - /* First check for shell environment variable */ - home_dir = getenv("HOME"); - if (home_dir == NULL) { - /* Fallback to password file entry */ - pwd = getpwuid(getuid()); - if (pwd == NULL) - return -EINVAL; - - home_dir = pwd->pw_dir; - } + const char *perm_dir; + + perm_dir = eal_permanent_data_path(); + if (perm_dir == NULL) + return -EINVAL; /* Append dpdk-traces to directory */ - if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0) + if (snprintf(dir_path, size, "%s/dpdk-traces/", perm_dir) < 0) return -ENAMETOOLONG; return 0; @@ -371,7 +361,7 @@ trace_mkdir(void) } /* Create the path if it t exist, no "mkdir -p" available here */ - rc = mkdir(trace->dir, 0700); + rc = eal_dir_create(trace->dir); if (rc < 0 && errno != EEXIST) { trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno)); rte_errno = errno; @@ -385,7 +375,7 @@ trace_mkdir(void) if (rc < 0) return rc; - rc = mkdir(trace->dir, 0700); + rc = eal_dir_create(trace->dir); if (rc < 0) { trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno)); rte_errno = errno; diff --git a/lib/librte_eal/common/eal_filesystem.h b/lib/librte_eal/common/eal_filesystem.h index 5d21f07c2..77fe3be69 100644 --- a/lib/librte_eal/common/eal_filesystem.h +++ b/lib/librte_eal/common/eal_filesystem.h @@ -4,8 +4,8 @@ /** * @file - * Stores functions and path defines for files and directories - * on the filesystem for Linux, that are used by the Linux EAL. + * Stores functions and path defines for files and directories used by DPDK. + * Parts of this file are Unix-specific for historical reasons. */ #ifndef EAL_FILESYSTEM_H @@ -28,6 +28,32 @@ eal_create_runtime_dir(void); int eal_clean_runtime_dir(void); +/** + * Get absolute path to the directory where permanent data can be stored. + * + * @return + * Statically allocated string on success, NULL on failure. + */ +const char * +eal_permanent_data_path(void); + +/** + * Create a directory accessible to the current user only. + * + * This function does not create intermediate directories, + * thus only the last path component may be nonexistent. + * + * This function succeeds if path already exists and is a directory. + * + * Platform-independent code should use forward slash as path separator. + * + * @param path + * Path to be created. + * @return + * 0 on success, (-1) on failure and rte_errno is set. + */ +int eal_dir_create(const char *path); + /** Function to return hugefile prefix that's currently set up */ const char * eal_get_hugefile_prefix(void); diff --git a/lib/librte_eal/freebsd/Makefile b/lib/librte_eal/freebsd/Makefile index a8400f20a..5170a85ab 100644 --- a/lib/librte_eal/freebsd/Makefile +++ b/lib/librte_eal/freebsd/Makefile @@ -7,6 +7,7 @@ LIB = librte_eal.a ARCH_DIR ?= $(RTE_ARCH) VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR) +VPATH += $(RTE_SDK)/lib/librte_eal/unix VPATH += $(RTE_SDK)/lib/librte_eal/common CFLAGS += -I$(SRCDIR)/include @@ -74,6 +75,9 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_service.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_random.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_reciprocal.c +# from unix dir +SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += eal_unix_filesystem.c + # from arch dir SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_cpuflags.c SRCS-$(CONFIG_RTE_EXEC_ENV_FREEBSD) += rte_hypervisor.c diff --git a/lib/librte_eal/linux/Makefile b/lib/librte_eal/linux/Makefile index a77eb1757..bdbcb9801 100644 --- a/lib/librte_eal/linux/Makefile +++ b/lib/librte_eal/linux/Makefile @@ -7,6 +7,7 @@ LIB = librte_eal.a ARCH_DIR ?= $(RTE_ARCH) VPATH += $(RTE_SDK)/lib/librte_eal/$(ARCH_DIR) +VPATH += $(RTE_SDK)/lib/librte_eal/unix VPATH += $(RTE_SDK)/lib/librte_eal/common CFLAGS += -I$(SRCDIR)/include @@ -81,6 +82,9 @@ SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_service.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_random.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_reciprocal.c +# from unix dir +SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += eal_unix_filesystem.c + # from arch dir SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_cpuflags.c SRCS-$(CONFIG_RTE_EXEC_ENV_LINUX) += rte_hypervisor.c diff --git a/lib/librte_eal/meson.build b/lib/librte_eal/meson.build index 0267c3b9d..98c97dd07 100644 --- a/lib/librte_eal/meson.build +++ b/lib/librte_eal/meson.build @@ -6,6 +6,10 @@ subdir('include') subdir('common') +if not is_windows + subdir('unix') +endif + dpdk_conf.set('RTE_EXEC_ENV_' + exec_env.to_upper(), 1) subdir(exec_env) diff --git a/lib/librte_eal/unix/eal_unix_filesystem.c b/lib/librte_eal/unix/eal_unix_filesystem.c new file mode 100644 index 000000000..f5a64eecc --- /dev/null +++ b/lib/librte_eal/unix/eal_unix_filesystem.c @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2020 Dmitry Kozlyuk + */ + +#include + +#include +#include +#include + +#include + +#include + +const char * +eal_permanent_data_path(void) +{ + static char path[PATH_MAX]; /* static so auto-zeroed */ + + const char *home_dir; + struct passwd *pwd; + + if (path[0] != '\0') + return path; + + /* First check for shell environment variable */ + home_dir = getenv("HOME"); + if (home_dir == NULL) { + /* Fallback to password file entry */ + pwd = getpwuid(getuid()); + if (pwd == NULL) + return NULL; + + home_dir = pwd->pw_dir; + } + + if (strlen(home_dir) >= sizeof(path)) + return NULL; + + strncpy(path, home_dir, sizeof(path)); + return path; +} + +int +eal_dir_create(const char *path) +{ + int ret = mkdir(path, 0700); + if (ret) + rte_errno = errno; + return ret; +} diff --git a/lib/librte_eal/unix/meson.build b/lib/librte_eal/unix/meson.build new file mode 100644 index 000000000..fafa41685 --- /dev/null +++ b/lib/librte_eal/unix/meson.build @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright (c) 2020 Dmitry Kozlyuk + +sources += files( + 'eal_unix_filesystem.c', +) -- 2.25.1