From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by dpdk.org (Postfix) with ESMTP id BC75E4CB3 for ; Mon, 12 Mar 2018 12:33:09 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Mar 2018 04:33:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,461,1515484800"; d="scan'208";a="181922064" Received: from silpixa00399126.ir.intel.com (HELO silpixa00399126.ger.corp.intel.com) ([10.237.223.223]) by orsmga004.jf.intel.com with ESMTP; 12 Mar 2018 04:33:07 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson Date: Mon, 12 Mar 2018 11:32:59 +0000 Message-Id: <20180312113300.115551-1-bruce.richardson@intel.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180220170727.220340-1-bruce.richardson@intel.com> References: <20180220170727.220340-1-bruce.richardson@intel.com> Subject: [dpdk-dev] [PATCH 1/2] add support for strlcpy function 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: , X-List-Received-Date: Mon, 12 Mar 2018 11:33:10 -0000 The strncpy function is error prone for doing "safe" string copies, so we generally try to use "snprintf" instead in the code. The function "strlcpy" is a better alternative, since it better conveys the intention of the programmer, and doesn't suffer from the non-null terminating behaviour of it's n'ed brethern. The downside of this function is that it is not available by default on linux, though standard in the BSD's. It is available on most distros by installing "libbsd" package. This patch therefore provides the following in rte_string_fns.h to ensure that strlcpy is available there: * for BSD, include string.h as normal * if RTE_USE_LIBBSD is set, include * if not set, fallback to snprintf for strlcpy Using make build system, the RTE_USE_LIBBSD is a hard-coded value to "n", but when using meson, it's automatically set based on what is available on the platform. Signed-off-by: Bruce Richardson ---- RFC->v1: * split patch into 2, to separate out snprintf replacement * added missing include in mlx drivers * adding linking support against libbsd when feature is enabled * make strlcpy an inline function rather than macro * fix support on BSD where strict posix compliance is requested and therefore strlcpy is not available. --- config/common_base | 1 + config/meson.build | 7 +++++++ devtools/cocci/strlcpy.cocci | 8 +++++++ lib/librte_eal/common/include/rte_string_fns.h | 29 ++++++++++++++++++++++++++ mk/rte.app.mk | 3 +++ 5 files changed, 48 insertions(+) create mode 100644 devtools/cocci/strlcpy.cocci diff --git a/config/common_base b/config/common_base index ad03cf433..20d4cbcf2 100644 --- a/config/common_base +++ b/config/common_base @@ -76,6 +76,7 @@ CONFIG_RTE_EAL_VFIO=n CONFIG_RTE_MAX_VFIO_GROUPS=64 CONFIG_RTE_MALLOC_DEBUG=n CONFIG_RTE_EAL_NUMA_AWARE_HUGEPAGES=n +CONFIG_RTE_USE_LIBBSD=n # # Recognize/ignore the AVX/AVX512 CPU flags for performance/power testing. diff --git a/config/meson.build b/config/meson.build index f8c67578d..77af5d897 100644 --- a/config/meson.build +++ b/config/meson.build @@ -38,6 +38,13 @@ if numa_dep.found() and cc.has_header('numaif.h') dpdk_extra_ldflags += '-lnuma' endif +# check for strlcpy +if host_machine.system() == 'linux' and cc.find_library('bsd', required: false).found() + dpdk_conf.set('RTE_USE_LIBBSD', 1) + add_project_link_arguments('-lbsd', language: 'c') + dpdk_extra_ldflags += '-lbsd' +endif + # add -include rte_config to cflags add_project_arguments('-include', 'rte_config.h', language: 'c') diff --git a/devtools/cocci/strlcpy.cocci b/devtools/cocci/strlcpy.cocci new file mode 100644 index 000000000..335e27128 --- /dev/null +++ b/devtools/cocci/strlcpy.cocci @@ -0,0 +1,8 @@ +@use_strlcpy@ +identifier src, dst; +expression size; +@@ +( +- snprintf(dst, size, "%s", src) ++ strlcpy(dst, src, size) +) diff --git a/lib/librte_eal/common/include/rte_string_fns.h b/lib/librte_eal/common/include/rte_string_fns.h index e97047a47..f1f5b17b8 100644 --- a/lib/librte_eal/common/include/rte_string_fns.h +++ b/lib/librte_eal/common/include/rte_string_fns.h @@ -45,6 +45,35 @@ int rte_strsplit(char *string, int stringlen, char **tokens, int maxtokens, char delim); +/** + * @internal + * DPDK-specific version of strlcpy for systems without + * libc or libbsd copies of the function + */ +static inline size_t +rte_strlcpy(char *dst, const char *src, size_t size) +{ + return snprintf(dst, size, "%s", src); +} + +/* pull in a strlcpy function */ +#ifdef RTE_EXEC_ENV_BSDAPP +#include +#ifndef __BSD_VISIBLE /* non-standard functions are hidden */ +#define strlcpy(dst, src, size) rte_strlcpy(dst, src, size) +#endif + + +#else /* non-BSD platforms */ +#ifdef RTE_USE_LIBBSD +#include + +#else /* no BSD header files, create own */ +#define strlcpy(dst, src, size) rte_strlcpy(dst, src, size) + +#endif /* RTE_USE_LIBBSD */ +#endif /* BSDAPP */ + #ifdef __cplusplus } #endif diff --git a/mk/rte.app.mk b/mk/rte.app.mk index 3eb41d176..39511da93 100644 --- a/mk/rte.app.mk +++ b/mk/rte.app.mk @@ -248,6 +248,9 @@ _LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lrt ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP)$(CONFIG_RTE_EAL_NUMA_AWARE_HUGEPAGES),yy) _LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lnuma endif +ifeq ($(CONFIG_RTE_EXEC_ENV_LINUXAPP)$(CONFIG_RTE_USE_LIBBSD),yy) +_LDLIBS-$(CONFIG_RTE_LIBRTE_EAL) += -lbsd +endif _LDLIBS-$(CONFIG_RTE_LIBRTE_SCHED) += -lm _LDLIBS-$(CONFIG_RTE_LIBRTE_SCHED) += -lrt _LDLIBS-$(CONFIG_RTE_LIBRTE_MEMBER) += -lm -- 2.14.3