From 2bc722a97ed6716e3effca2ce5664ec20bcb5555 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Wed, 22 May 2024 19:44:17 -0400 Subject: [PATCH] posix: deprecate POSIX_FNMATCH GETOPT GETENTROPY The functions fnmatch(), getopt(), getentropy() and others are grouped into the standard Option Group POSIX_C_LIB_EXT. The getentropy() function is currently in-draft for Issue 8 as of 2021. https://www.opengroup.org/austin/docs/austin_1110.pdf Not surprisingly, the POSIX_C_LIB_EXT Option Group also includes the highly debated strnlen() function. Moving that function will be deferred until later. Signed-off-by: Chris Friedt --- .../portability/posix/kconfig/index.rst | 2 -- .../portability/posix/option_groups/index.rst | 25 ++++++++++++++++ doc/services/shell/index.rst | 2 +- drivers/sensor/seeed/grove/Kconfig | 2 -- include/zephyr/posix/unistd.h | 2 +- lib/crc/Kconfig | 2 +- lib/posix/options/CMakeLists.txt | 18 ++++++++--- lib/posix/options/Kconfig | 5 +--- lib/posix/options/Kconfig.c_lib_ext | 30 +++++++++++++++++++ lib/posix/options/Kconfig.deprecated | 27 +++++++++++++++++ lib/posix/options/Kconfig.fnmatch | 10 ------- lib/posix/options/Kconfig.getentropy | 14 --------- lib/posix/options/Kconfig.getopt | 5 ---- lib/posix/options/getentropy.c | 6 ++-- lib/posix/options/getopt/CMakeLists.txt | 20 ------------- lib/posix/options/getopt/Kconfig | 28 ----------------- lib/posix/options/getopt/getopt.h | 4 +++ samples/shields/npm6001_ek/prj.conf | 2 +- subsys/shell/Kconfig | 4 +-- subsys/shell/modules/Kconfig | 2 +- tests/posix/getentropy/prj.conf | 3 +- tests/posix/getentropy/src/main.c | 2 +- tests/posix/getopt/prj.conf | 2 +- tests/posix/headers/prj.conf | 2 +- 24 files changed, 115 insertions(+), 104 deletions(-) create mode 100644 lib/posix/options/Kconfig.c_lib_ext delete mode 100644 lib/posix/options/Kconfig.fnmatch delete mode 100644 lib/posix/options/Kconfig.getentropy delete mode 100644 lib/posix/options/Kconfig.getopt delete mode 100644 lib/posix/options/getopt/CMakeLists.txt delete mode 100644 lib/posix/options/getopt/Kconfig diff --git a/doc/services/portability/posix/kconfig/index.rst b/doc/services/portability/posix/kconfig/index.rst index 51dc795115e..14a77e3a3e7 100644 --- a/doc/services/portability/posix/kconfig/index.rst +++ b/doc/services/portability/posix/kconfig/index.rst @@ -9,8 +9,6 @@ implementation of the POSIX API. * :kconfig:option:`CONFIG_EVENTFD` * :kconfig:option:`CONFIG_EVENTFD_MAX` * :kconfig:option:`CONFIG_FDTABLE` -* :kconfig:option:`CONFIG_FNMATCH` -* :kconfig:option:`CONFIG_GETOPT` * :kconfig:option:`CONFIG_GETOPT_LONG` * :kconfig:option:`CONFIG_MAX_PTHREAD_COUNT` * :kconfig:option:`CONFIG_MAX_PTHREAD_KEY_COUNT` diff --git a/doc/services/portability/posix/option_groups/index.rst b/doc/services/portability/posix/option_groups/index.rst index 00a5c34ad4f..a2c316a6928 100644 --- a/doc/services/portability/posix/option_groups/index.rst +++ b/doc/services/portability/posix/option_groups/index.rst @@ -151,6 +151,31 @@ Group. For more information on developing Zephyr applications in the C programming language, please refer to :ref:`details`. +.. _posix_option_group_c_lib_ext: + +POSIX_C_LIB_EXT +=============== + +.. csv-table:: POSIX_C_LIB_EXT + :header: API, Supported + :widths: 50,10 + + fnmatch(), yes + getopt(), yes + getsubopt(), + optarg, yes + opterr, yes + optind, yes + optopt, yes + stpcpy(), + stpncpy(), + strcasecmp(), + strdup(), + strfmon(), + strncasecmp(), yes + strndup(), + strnlen(), yes + .. _posix_option_group_realtime_signals: POSIX_REALTIME_SIGNALS diff --git a/doc/services/shell/index.rst b/doc/services/shell/index.rst index 504bef139fe..582ddcd2f9c 100644 --- a/doc/services/shell/index.rst +++ b/doc/services/shell/index.rst @@ -556,7 +556,7 @@ is accomplished by the ``getopt`` family functions. For this purpose shell supports the getopt and getopt_long libraries available in the FreeBSD project. This feature is activated by: -:kconfig:option:`CONFIG_GETOPT` set to ``y`` and :kconfig:option:`CONFIG_GETOPT_LONG` +:kconfig:option:`CONFIG_POSIX_C_LIB_EXT` set to ``y`` and :kconfig:option:`CONFIG_GETOPT_LONG` set to ``y``. This feature can be used in thread safe as well as non thread safe manner. diff --git a/drivers/sensor/seeed/grove/Kconfig b/drivers/sensor/seeed/grove/Kconfig index 9563bdd7b3a..a91c66c0996 100644 --- a/drivers/sensor/seeed/grove/Kconfig +++ b/drivers/sensor/seeed/grove/Kconfig @@ -16,7 +16,6 @@ config GROVE_LIGHT_SENSOR bool "The Seeed Grove Light Sensor" default y depends on DT_HAS_SEEED_GROVE_LIGHT_ENABLED - depends on !MINIMAL_LIBC select ADC help Setting this value will enable driver support for the Grove Light @@ -26,7 +25,6 @@ config GROVE_TEMPERATURE_SENSOR bool "The Seeed Grove Temperature Sensor" default y depends on DT_HAS_SEEED_GROVE_TEMPERATURE_ENABLED - depends on !MINIMAL_LIBC select ADC help Setting this value will enable driver support for the Grove diff --git a/include/zephyr/posix/unistd.h b/include/zephyr/posix/unistd.h index 3102dd89be0..194b2bffe6f 100644 --- a/include/zephyr/posix/unistd.h +++ b/include/zephyr/posix/unistd.h @@ -52,7 +52,7 @@ static inline int gethostname(char *buf, size_t len) #endif /* CONFIG_POSIX_API */ -#ifdef CONFIG_GETOPT +#ifdef CONFIG_POSIX_C_LIB_EXT int getopt(int argc, char *const argv[], const char *optstring); extern char *optarg; extern int opterr, optind, optopt; diff --git a/lib/crc/Kconfig b/lib/crc/Kconfig index f0745f0aded..0402df59d01 100644 --- a/lib/crc/Kconfig +++ b/lib/crc/Kconfig @@ -10,7 +10,7 @@ if CRC config CRC_SHELL bool "CRC Shell" depends on SHELL - select GETOPT + select POSIX_C_LIB_EXT help Enable CRC checking for memory regions from the shell. endif # CRC diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index e4431df859b..9826156fd14 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -25,7 +25,7 @@ if(CONFIG_POSIX_SIGNAL) endif() if(CONFIG_POSIX_API OR CONFIG_PTHREAD_IPC OR CONFIG_POSIX_TIMERS OR - CONFIG_POSIX_MESSAGE_PASSING OR CONFIG_POSIX_FS OR CONFIG_EVENTFD OR CONFIG_GETOPT OR + CONFIG_POSIX_MESSAGE_PASSING OR CONFIG_POSIX_FS OR CONFIG_EVENTFD OR CONFIG_POSIX_C_LIB_EXT OR CONFIG_POSIX_SINGLE_PROCESS) # This is a temporary workaround so that Newlib declares the appropriate # types for us. POSIX features to be formalized as part of #51211 @@ -34,12 +34,15 @@ if(CONFIG_POSIX_API OR CONFIG_PTHREAD_IPC OR CONFIG_POSIX_TIMERS OR endif() zephyr_library() -add_subdirectory_ifdef(CONFIG_GETOPT getopt) zephyr_library_sources_ifdef(CONFIG_EVENTFD eventfd.c) -zephyr_library_sources_ifdef(CONFIG_FNMATCH fnmatch.c) -zephyr_library_sources_ifdef(CONFIG_GETENTROPY getentropy.c) zephyr_library_sources_ifdef(CONFIG_POSIX_ASYNCHRONOUS_IO aio.c) zephyr_library_sources_ifdef(CONFIG_POSIX_BARRIERS barrier.c) +zephyr_library_sources_ifdef(CONFIG_POSIX_C_LIB_EXT + fnmatch.c + getentropy.c + getopt/getopt.c + getopt/getopt_common.c +) zephyr_library_sources_ifdef(CONFIG_POSIX_DEVICE_IO # perror should be moved to the common libc perror.c @@ -77,6 +80,13 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_PRIORITY_SCHEDULING sched.c) zephyr_library_sources_ifdef(CONFIG_POSIX_READER_WRITER_LOCKS rwlock.c) zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC semaphore.c) +zephyr_library_sources_ifdef(CONFIG_GETOPT_LONG + getopt/getopt_long.c +) +zephyr_include_directories_ifdef(CONFIG_POSIX_C_LIB_EXT + getopt/ +) + zephyr_library_include_directories( ${ZEPHYR_BASE}/kernel/include ${ARCH_DIR}/${ARCH}/include diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index da9b00b9a84..8e1f0918196 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -26,13 +26,10 @@ config PTHREAD_IPC rsource "Kconfig.aio" rsource "Kconfig.barrier" rsource "Kconfig.cond" +rsource "Kconfig.c_lib_ext" rsource "Kconfig.device_io" -rsource "Kconfig.eventfd" rsource "Kconfig.fd_mgmt" -rsource "Kconfig.fnmatch" rsource "Kconfig.fs" -rsource "Kconfig.getentropy" -rsource "Kconfig.getopt" rsource "Kconfig.key" rsource "Kconfig.mem" rsource "Kconfig.mqueue" diff --git a/lib/posix/options/Kconfig.c_lib_ext b/lib/posix/options/Kconfig.c_lib_ext new file mode 100644 index 00000000000..8acfead9b34 --- /dev/null +++ b/lib/posix/options/Kconfig.c_lib_ext @@ -0,0 +1,30 @@ +# Copyright (c) 2024 Tenstorrent AI ULC +# +# SPDX-License-Identifier: Apache-2.0 + +menuconfig POSIX_C_LIB_EXT + bool "POSIX general C library extension" + default y if POSIX_API + help + Select 'y' here and Zephyr will provide an implementation of the POSIX_C_LIB_EXT Option + Group, consisting of fnmatch(), getopt(), getsubopt(), optarg, opterr, optind, optopt, + stpcpy(), stpncpy(), strcasecmp(), strdup(), strfmon(), and strncasecmp(), strndup(), and + strnlen(). + + For more informnation, please see + https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html + +if POSIX_C_LIB_EXT + +config GETOPT_LONG + bool "Getopt long library support" + help + This option adds support of the getopt long. + Different shell backends are using their own instance of getopt to + not interfere with each other. + All not shell threads share one global instance of getopt state, hence + apart from shell this library is not thread safe. User can add support + for other threads by extending function getopt_state_get in + getopt_common.c file. + +endif # POSIX_C_LIB_EXT diff --git a/lib/posix/options/Kconfig.deprecated b/lib/posix/options/Kconfig.deprecated index 5b16209fad0..f0e14373600 100644 --- a/lib/posix/options/Kconfig.deprecated +++ b/lib/posix/options/Kconfig.deprecated @@ -6,6 +6,33 @@ menu "Deprecated POSIX options" +config FNMATCH + bool "Support for fnmatch [DEPRECATED]" + select DEPRECATED + select POSIX_C_LIB_EXT + help + This option is deprecated. + + Please use CONFIG_POSIX_C_LIB_EXT instead. + +config GETENTROPY + bool "Support for getentropy [DEPRECATED]" + select DEPRECATED + select POSIX_C_LIB_EXT + help + This option is deprecated. + + Please use CONFIG_POSIX_C_LIB_EXT instead. + +config GETOPT + bool "Getopt library support [DEPRECATED]" + select DEPRECATED + select POSIX_C_LIB_EXT + help + This option is deprecated. + + Please use CONFIG_POSIX_C_LIB_EXT instead. + config MAX_TIMER_COUNT int "Maximum number of timer_t [DEPRECATED]" default POSIX_TIMER_MAX if POSIX_TIMERS diff --git a/lib/posix/options/Kconfig.fnmatch b/lib/posix/options/Kconfig.fnmatch deleted file mode 100644 index 81f1a00938f..00000000000 --- a/lib/posix/options/Kconfig.fnmatch +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2018 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: Apache-2.0 - -config FNMATCH - bool "Support for fnmatch" - default y if POSIX_API - help - Match filenames using the the fnmatch function. For example, the pattern - "*.c" matches the filename "hello.c". diff --git a/lib/posix/options/Kconfig.getentropy b/lib/posix/options/Kconfig.getentropy deleted file mode 100644 index 74454135019..00000000000 --- a/lib/posix/options/Kconfig.getentropy +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (c) 2024 Google LLC -# -# SPDX-License-Identifier: Apache-2.0 - -DT_CHOSEN_ZEPHYR_ENTROPY := zephyr,entropy - -config GETENTROPY - bool "Support for getentropy" - depends on !NATIVE_APPLICATION - select NATIVE_LIBC_INCOMPATIBLE - depends on ENTROPY_HAS_DRIVER - depends on $(dt_chosen_enabled,$(DT_CHOSEN_ZEPHYR_ENTROPY)) - help - Enable support for getentropy() function. diff --git a/lib/posix/options/Kconfig.getopt b/lib/posix/options/Kconfig.getopt deleted file mode 100644 index 4d3559cd855..00000000000 --- a/lib/posix/options/Kconfig.getopt +++ /dev/null @@ -1,5 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: Apache-2.0 - -rsource "getopt/Kconfig" diff --git a/lib/posix/options/getentropy.c b/lib/posix/options/getentropy.c index dcc271617cb..974ea4dce50 100644 --- a/lib/posix/options/getentropy.c +++ b/lib/posix/options/getentropy.c @@ -10,11 +10,11 @@ #include #include -#define ENTROPY_NODE DT_CHOSEN(zephyr_entropy) +#define ENTROPY_NODE DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_entropy)) int getentropy(void *buffer, size_t length) { - const struct device * const entropy = DEVICE_DT_GET(ENTROPY_NODE); + const struct device *const entropy = ENTROPY_NODE; if (!buffer) { errno = EFAULT; @@ -26,7 +26,7 @@ int getentropy(void *buffer, size_t length) return -1; } - if (!device_is_ready(entropy)) { + if (entropy == NULL || !device_is_ready(entropy)) { errno = EIO; return -1; } diff --git a/lib/posix/options/getopt/CMakeLists.txt b/lib/posix/options/getopt/CMakeLists.txt deleted file mode 100644 index 642e3b9949b..00000000000 --- a/lib/posix/options/getopt/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor -# -# SPDX-License-Identifier: Apache-2.0 -# - -zephyr_include_directories_ifdef( - CONFIG_GETOPT - . -) - -zephyr_sources_ifdef( - CONFIG_GETOPT - getopt.c - getopt_common.c -) - -zephyr_sources_ifdef( - CONFIG_GETOPT_LONG - getopt_long.c -) diff --git a/lib/posix/options/getopt/Kconfig b/lib/posix/options/getopt/Kconfig deleted file mode 100644 index 72c9961fb90..00000000000 --- a/lib/posix/options/getopt/Kconfig +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (c) 2021 Nordic Semiconductor -# SPDX-License-Identifier: Apache-2.0 - - -menuconfig GETOPT - bool "Getopt library support" - default y if POSIX_API - help - This option adds support of getopt. - Different shell backends are use their own instance of getopt to - not interfere with each other. - All not shell threads share one global instance of getopt state, hence - apart from shell this library is not thread safe. User can add support - for other threads by extending function getopt_state_get in - getopt_common.c file. - This option enables the following function: getopt. - -config GETOPT_LONG - bool "Getopt long library support" - depends on GETOPT - help - This option adds support of the getopt long. - Different shell backends are using their own instance of getopt to - not interfere with each other. - All not shell threads share one global instance of getopt state, hence - apart from shell this library is not thread safe. User can add support - for other threads by extending function getopt_state_get in - getopt_common.c file. diff --git a/lib/posix/options/getopt/getopt.h b/lib/posix/options/getopt/getopt.h index 3ae474f4304..666c5541c63 100644 --- a/lib/posix/options/getopt/getopt.h +++ b/lib/posix/options/getopt/getopt.h @@ -29,6 +29,10 @@ struct getopt_state { }; extern int optreset; /* reset getopt */ +extern char *optarg; +extern int opterr; +extern int optind; +extern int optopt; #define no_argument 0 #define required_argument 1 diff --git a/samples/shields/npm6001_ek/prj.conf b/samples/shields/npm6001_ek/prj.conf index 94da7cfdaef..b0292200688 100644 --- a/samples/shields/npm6001_ek/prj.conf +++ b/samples/shields/npm6001_ek/prj.conf @@ -3,7 +3,7 @@ CONFIG_SHELL=y CONFIG_SHELL_GETOPT=y -CONFIG_GETOPT=y +CONFIG_POSIX_C_LIB_EXT=y CONFIG_GETOPT_LONG=y CONFIG_GPIO=y CONFIG_REGULATOR=y diff --git a/subsys/shell/Kconfig b/subsys/shell/Kconfig index c9907839959..0c3919d164e 100644 --- a/subsys/shell/Kconfig +++ b/subsys/shell/Kconfig @@ -132,7 +132,7 @@ config SHELL_ASCII_FILTER config SHELL_WILDCARD bool "Wildcard support in shell" - select FNMATCH + select POSIX_C_LIB_EXT default y if !SHELL_MINIMAL help Enables using wildcards: * and ? in the shell. @@ -176,7 +176,7 @@ config SHELL_VT100_COLORS config SHELL_GETOPT bool "Threadsafe getopt support in shell" - select GETOPT + select POSIX_C_LIB_EXT help This config creates a separate getopt_state for the shell instance. It ensures that using getopt with shell is thread safe. diff --git a/subsys/shell/modules/Kconfig b/subsys/shell/modules/Kconfig index c57704ee2ae..86e9ac49a9b 100644 --- a/subsys/shell/modules/Kconfig +++ b/subsys/shell/modules/Kconfig @@ -42,6 +42,6 @@ config DATE_SHELL config DEVMEM_SHELL bool "Devmem shell" default y if !SHELL_MINIMAL - select GETOPT + select POSIX_C_LIB_EXT help This shell command provides read/write access to physical memory. diff --git a/tests/posix/getentropy/prj.conf b/tests/posix/getentropy/prj.conf index 58160099e32..9d73893e003 100644 --- a/tests/posix/getentropy/prj.conf +++ b/tests/posix/getentropy/prj.conf @@ -1,4 +1,3 @@ CONFIG_ENTROPY_GENERATOR=y -CONFIG_GETENTROPY=y -CONFIG_POSIX_API=y +CONFIG_POSIX_C_LIB_EXT=y CONFIG_ZTEST=y diff --git a/tests/posix/getentropy/src/main.c b/tests/posix/getentropy/src/main.c index a276c4b9663..010207e678d 100644 --- a/tests/posix/getentropy/src/main.c +++ b/tests/posix/getentropy/src/main.c @@ -6,7 +6,7 @@ #include -#include +#include ZTEST(getentropy_test_suite, test_getentropy_too_large) { diff --git a/tests/posix/getopt/prj.conf b/tests/posix/getopt/prj.conf index 034ce191ddf..dc97b22a9b5 100644 --- a/tests/posix/getopt/prj.conf +++ b/tests/posix/getopt/prj.conf @@ -1,4 +1,4 @@ -CONFIG_GETOPT=y +CONFIG_POSIX_C_LIB_EXT=y CONFIG_GETOPT_LONG=y CONFIG_LOG=n CONFIG_ZTEST=y diff --git a/tests/posix/headers/prj.conf b/tests/posix/headers/prj.conf index 6dc46dc7b4f..c785e459293 100644 --- a/tests/posix/headers/prj.conf +++ b/tests/posix/headers/prj.conf @@ -19,4 +19,4 @@ CONFIG_POSIX_FS=y CONFIG_POSIX_TIMERS=y CONFIG_POSIX_MESSAGE_PASSING=y CONFIG_EVENTFD=y -CONFIG_GETOPT=y +CONFIG_POSIX_C_LIB_EXT=y