diff --git a/doc/services/portability/posix/conformance/index.rst b/doc/services/portability/posix/conformance/index.rst index 439f12b86d6..8408560f3a7 100644 --- a/doc/services/portability/posix/conformance/index.rst +++ b/doc/services/portability/posix/conformance/index.rst @@ -68,7 +68,7 @@ POSIX System Interfaces _POSIX_MEMORY_PROTECTION, -1, :ref:`†` :ref:`_POSIX_READER_WRITER_LOCKS`, 200809L, :kconfig:option:`CONFIG_POSIX_READER_WRITER_LOCKS` :ref:`_POSIX_REALTIME_SIGNALS`, -1, - :ref:`_POSIX_SEMAPHORES`, 200809L, :kconfig:option:`CONFIG_PTHREAD_IPC` + :ref:`_POSIX_SEMAPHORES`, 200809L, :kconfig:option:`CONFIG_POSIX_SEMAPHORES` :ref:`_POSIX_SPIN_LOCKS`, 200809L, :kconfig:option:`CONFIG_POSIX_SPIN_LOCKS` :ref:`_POSIX_THREAD_SAFE_FUNCTIONS`, -1, :ref:`_POSIX_THREADS`, -1, :kconfig:option:`CONFIG_PTHREAD_IPC` diff --git a/doc/services/portability/posix/kconfig/index.rst b/doc/services/portability/posix/kconfig/index.rst index cc2954c00af..be5b2b4e8c7 100644 --- a/doc/services/portability/posix/kconfig/index.rst +++ b/doc/services/portability/posix/kconfig/index.rst @@ -35,5 +35,7 @@ implementation of the POSIX API. * :kconfig:option:`CONFIG_PTHREAD_KEY` * :kconfig:option:`CONFIG_PTHREAD_MUTEX` * :kconfig:option:`CONFIG_PTHREAD_RECYCLER_DELAY_MS` -* :kconfig:option:`CONFIG_SEM_VALUE_MAX` +* :kconfig:option:`CONFIG_POSIX_SEM_NAMELEN_MAX` +* :kconfig:option:`CONFIG_POSIX_SEM_NSEMS_MAX` +* :kconfig:option:`CONFIG_POSIX_SEM_VALUE_MAX` * :kconfig:option:`CONFIG_TIMER_CREATE_WAIT` diff --git a/include/zephyr/posix/posix_features.h b/include/zephyr/posix/posix_features.h index 710c7c8f148..96d98d0e0f5 100644 --- a/include/zephyr/posix/posix_features.h +++ b/include/zephyr/posix/posix_features.h @@ -79,7 +79,7 @@ /* #define _POSIX_REGEXP (-1L) */ /* #define _POSIX_SAVED_IDS (-1L) */ -#ifdef CONFIG_PTHREAD_IPC +#ifdef CONFIG_POSIX_SEMAPHORES #define _POSIX_SEMAPHORES _POSIX_VERSION #endif @@ -217,8 +217,10 @@ #define _POSIX_RE_DUP_MAX (255) #define _POSIX_RTSIG_MAX \ COND_CODE_1(CONFIG_POSIX_REALTIME_SIGNALS, (CONFIG_POSIX_RTSIG_MAX), (0)) -#define _POSIX_SEM_NSEMS_MAX CONFIG_SEM_NAMELEN_MAX -#define _POSIX_SEM_VALUE_MAX CONFIG_SEM_VALUE_MAX +#define _POSIX_SEM_NSEMS_MAX \ + COND_CODE_1(CONFIG_POSIX_SEMAPHORES, (CONFIG_POSIX_SEM_NSEMS_MAX), (0)) +#define _POSIX_SEM_VALUE_MAX \ + COND_CODE_1(CONFIG_POSIX_SEMAPHORES, (CONFIG_POSIX_SEM_VALUE_MAX), (0)) #define _POSIX_SIGQUEUE_MAX (32) #define _POSIX_SSIZE_MAX (32767) #define _POSIX_SS_REPL_MAX (4) @@ -284,7 +286,7 @@ #define PTHREAD_THREADS_MAX _POSIX_THREAD_THREADS_MAX #define RTSIG_MAX _POSIX_RTSIG_MAX #define SEM_NSEMS_MAX _POSIX_SEM_NSEMS_MAX -#define SEM_VALUE_MAX CONFIG_SEM_VALUE_MAX +#define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX #define SIGQUEUE_MAX _POSIX_SIGQUEUE_MAX #define STREAM_MAX _POSIX_STREAM_MAX #define SYMLOOP_MAX _POSIX_SYMLOOP_MAX diff --git a/include/zephyr/posix/sys/sysconf.h b/include/zephyr/posix/sys/sysconf.h index d90f13580e5..bc640938f38 100644 --- a/include/zephyr/posix/sys/sysconf.h +++ b/include/zephyr/posix/sys/sysconf.h @@ -173,7 +173,7 @@ enum { #define __z_posix_sysconf_SC_REGEXP (-1L) #define __z_posix_sysconf_SC_SAVED_IDS (-1L) #define __z_posix_sysconf_SC_SEMAPHORES \ - COND_CODE_1(CONFIG_PTHREAD_IPC, (_POSIX_SEMAPHORES), (-1L)) + COND_CODE_1(CONFIG_POSIX_SEMAPHORES, (_POSIX_SEMAPHORES), (-1L)) #define __z_posix_sysconf_SC_SHARED_MEMORY_OBJECTS (-1L) #define __z_posix_sysconf_SC_SHELL (-1L) #define __z_posix_sysconf_SC_SPAWN (-1L) diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index e488fe263b2..dcc850da084 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -79,7 +79,7 @@ zephyr_library_sources_ifdef(CONFIG_PTHREAD_MUTEX mutex.c) zephyr_library_sources_ifdef(CONFIG_PTHREAD pthread.c) 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_POSIX_SEMAPHORES semaphore.c) zephyr_library_sources_ifdef(CONFIG_GETOPT_LONG getopt/getopt_long.c diff --git a/lib/posix/options/Kconfig.deprecated b/lib/posix/options/Kconfig.deprecated index 719105d12bc..5c354268cdc 100644 --- a/lib/posix/options/Kconfig.deprecated +++ b/lib/posix/options/Kconfig.deprecated @@ -192,4 +192,22 @@ config TIMER_DELAYTIMER_MAX Please use CONFIG_POSIX_DELAYTIMER_MAX instead. +config SEM_NAMELEN_MAX + int "Maximum name length [DEPRECATED]" + default POSIX_SEM_NAMELEN_MAX if POSIX_SEMAPHORES + default 0 + help + This option is deprecated. + + Please use CONFIG_POSIX_SEM_NAMELEN_MAX instead. + +config SEM_VALUE_MAX + int "Maximum semaphore limit [DEPRECATED]" + default POSIX_SEM_VALUE_MAX if POSIX_SEMAPHORES + default 0 + help + This option is deprecated. + + Please use CONFIG_POSIX_SEM_VALUE_MAX instead. + endmenu diff --git a/lib/posix/options/Kconfig.semaphore b/lib/posix/options/Kconfig.semaphore index 53fb030736b..231565f3258 100644 --- a/lib/posix/options/Kconfig.semaphore +++ b/lib/posix/options/Kconfig.semaphore @@ -2,21 +2,36 @@ # # SPDX-License-Identifier: Apache-2.0 -menu "sem_t support" +menuconfig POSIX_SEMAPHORES + bool "POSIX semaphore support" + default y if POSIX_API + help + Enable this option for POSIX semaphore support. -config SEM_VALUE_MAX - int "Maximum semaphore limit" +if POSIX_SEMAPHORES + +config POSIX_SEM_VALUE_MAX + int "Maximum semaphore value" default 32767 range 1 32767 help Maximum semaphore count in POSIX compliant Application. -config SEM_NAMELEN_MAX - int "Maximum name length" +config POSIX_SEM_NSEMS_MAX + int "Maximum number of semaphores" + default 256 + help + Maximum number of semaphores in a POSIX application. + + Note: currently, in Zephyr, this only limits the number of named semaphores (i.e. those + created via sem_open()). + +config POSIX_SEM_NAMELEN_MAX + int "Maximum semaphore name length" default 16 range 2 255 help Maximum length of name for a named semaphore. The max value of 255 corresponds to {NAME_MAX}. -endmenu # "sem_t support" +endif # POSIX_SEMAPHORES diff --git a/lib/posix/options/semaphore.c b/lib/posix/options/semaphore.c index 45cac48372f..fddad0caf6b 100644 --- a/lib/posix/options/semaphore.c +++ b/lib/posix/options/semaphore.c @@ -120,7 +120,7 @@ int sem_getvalue(sem_t *semaphore, int *value) */ int sem_init(sem_t *semaphore, int pshared, unsigned int value) { - if (value > CONFIG_SEM_VALUE_MAX) { + if (value > CONFIG_POSIX_SEM_VALUE_MAX) { errno = EINVAL; return -1; } @@ -131,7 +131,7 @@ int sem_init(sem_t *semaphore, int pshared, unsigned int value) */ __ASSERT(pshared == 0, "pshared should be 0"); - k_sem_init(semaphore, value, CONFIG_SEM_VALUE_MAX); + k_sem_init(semaphore, value, CONFIG_POSIX_SEM_VALUE_MAX); return 0; } @@ -232,7 +232,7 @@ sem_t *sem_open(const char *name, int oflags, ...) value = va_arg(va, unsigned int); va_end(va); - if (value > CONFIG_SEM_VALUE_MAX) { + if (value > CONFIG_POSIX_SEM_VALUE_MAX) { errno = EINVAL; return (sem_t *)SEM_FAILED; } @@ -243,7 +243,7 @@ sem_t *sem_open(const char *name, int oflags, ...) } namelen = strlen(name); - if ((namelen + 1) > CONFIG_SEM_NAMELEN_MAX) { + if ((namelen + 1) > CONFIG_POSIX_SEM_NAMELEN_MAX) { errno = ENAMETOOLONG; return (sem_t *)SEM_FAILED; } @@ -291,7 +291,7 @@ sem_t *sem_open(const char *name, int oflags, ...) /* 1 for this open instance, +1 for the linked name */ nsem->ref_count = 2; - (void)k_sem_init(&nsem->sem, value, CONFIG_SEM_VALUE_MAX); + (void)k_sem_init(&nsem->sem, value, CONFIG_POSIX_SEM_VALUE_MAX); sys_slist_append(&nsem_list, (sys_snode_t *)&(nsem->snode)); @@ -318,7 +318,7 @@ int sem_unlink(const char *name) return -1; } - if ((strlen(name) + 1) > CONFIG_SEM_NAMELEN_MAX) { + if ((strlen(name) + 1) > CONFIG_POSIX_SEM_NAMELEN_MAX) { errno = ENAMETOOLONG; return -1; } diff --git a/tests/posix/common/prj.conf b/tests/posix/common/prj.conf index 5c5613d0e68..1c99d64cf5a 100644 --- a/tests/posix/common/prj.conf +++ b/tests/posix/common/prj.conf @@ -2,7 +2,7 @@ CONFIG_PTHREAD_IPC=y CONFIG_POSIX_API=y CONFIG_MAX_PTHREAD_COUNT=6 CONFIG_ZTEST=y -CONFIG_SEM_VALUE_MAX=32767 +CONFIG_POSIX_SEM_VALUE_MAX=32767 CONFIG_POSIX_MESSAGE_PASSING=y CONFIG_POSIX_PRIORITY_SCHEDULING=y CONFIG_HEAP_MEM_POOL_SIZE=4096 diff --git a/tests/posix/common/src/semaphore.c b/tests/posix/common/src/semaphore.c index a1f15cf5c5d..f9cfe8e8c94 100644 --- a/tests/posix/common/src/semaphore.c +++ b/tests/posix/common/src/semaphore.c @@ -28,10 +28,10 @@ static void semaphore_test(sem_t *sem) struct timespec abstime; /* TESTPOINT: Check if sema value is less than - * CONFIG_SEM_VALUE_MAX + * CONFIG_POSIX_SEM_VALUE_MAX */ - zassert_equal(sem_init(sem, 0, (CONFIG_SEM_VALUE_MAX + 1)), -1, - "value larger than %d\n", CONFIG_SEM_VALUE_MAX); + zassert_equal(sem_init(sem, 0, (CONFIG_POSIX_SEM_VALUE_MAX + 1)), -1, + "value larger than %d\n", CONFIG_POSIX_SEM_VALUE_MAX); zassert_equal(errno, EINVAL); zassert_equal(sem_init(sem, 0, 0), 0, "sem_init failed"); @@ -158,8 +158,8 @@ ZTEST(semaphore, test_named_semaphore) zassert_equal_ptr(sem1, SEM_FAILED); zassert_equal(nsem_get_list_len(), 0); - /* Name exceeds CONFIG_SEM_NAMELEN_MAX */ - char name_too_long[CONFIG_SEM_NAMELEN_MAX + 2]; + /* Name exceeds CONFIG_POSIX_SEM_NAMELEN_MAX */ + char name_too_long[CONFIG_POSIX_SEM_NAMELEN_MAX + 2]; for (size_t i = 0; i < sizeof(name_too_long) - 1; i++) { name_too_long[i] = 'a'; @@ -168,12 +168,12 @@ ZTEST(semaphore, test_named_semaphore) sem1 = sem_open(name_too_long, 0, 0, 0); zassert_equal(errno, ENAMETOOLONG, "\"%s\" should be longer than %d", name_too_long, - CONFIG_SEM_NAMELEN_MAX); + CONFIG_POSIX_SEM_NAMELEN_MAX); zassert_equal_ptr(sem1, SEM_FAILED); zassert_equal(nsem_get_list_len(), 0); - /* `value` greater than CONFIG_SEM_VALUE_MAX */ - sem1 = sem_open("sem1", O_CREAT, 0, (CONFIG_SEM_VALUE_MAX + 1)); + /* `value` greater than CONFIG_POSIX_SEM_VALUE_MAX */ + sem1 = sem_open("sem1", O_CREAT, 0, (CONFIG_POSIX_SEM_VALUE_MAX + 1)); zassert_equal(errno, EINVAL); zassert_equal_ptr(sem1, SEM_FAILED); zassert_equal(nsem_get_list_len(), 0); @@ -238,13 +238,13 @@ ZTEST(semaphore, test_named_semaphore) zassert_equal(errno, ENOENT); zassert_equal(nsem_get_list_len(), 2); - /* Name exceeds CONFIG_SEM_NAMELEN_MAX */ - char long_sem_name[CONFIG_SEM_NAMELEN_MAX + 2]; + /* Name exceeds CONFIG_POSIX_SEM_NAMELEN_MAX */ + char long_sem_name[CONFIG_POSIX_SEM_NAMELEN_MAX + 2]; - for (int i = 0; i < CONFIG_SEM_NAMELEN_MAX + 1; i++) { + for (int i = 0; i < CONFIG_POSIX_SEM_NAMELEN_MAX + 1; i++) { long_sem_name[i] = 'a'; } - long_sem_name[CONFIG_SEM_NAMELEN_MAX + 1] = '\0'; + long_sem_name[CONFIG_POSIX_SEM_NAMELEN_MAX + 1] = '\0'; zassert_equal(sem_unlink(long_sem_name), -1); zassert_equal(errno, ENAMETOOLONG);