Skip to content

Commit 88f9291

Browse files
committed
posix: ensure that unistd.h declares version and feature test macros
The specification requires that unistd.h (or a file that is included via unistd.h) declare version macros and feature test macros. Feature test macros declarations are also removed from `lib/posix/options/CMakeLists.txt`. Closes #97949 Signed-off-by: Chris Friedt <[email protected]>
1 parent 1c3e53f commit 88f9291

File tree

10 files changed

+227
-100
lines changed

10 files changed

+227
-100
lines changed

include/zephyr/posix/posix_features.h

Lines changed: 209 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88
#ifndef INCLUDE_ZEPHYR_POSIX_POSIX_FEATURES_H_
99
#define INCLUDE_ZEPHYR_POSIX_POSIX_FEATURES_H_
1010

11-
#include <zephyr/autoconf.h> /* CONFIG_* */
12-
#include <zephyr/sys/util_macro.h> /* COND_CODE_1() */
11+
#if defined(_POSIX_C_SOURCE) || defined(CONFIG_POSIX_SYSTEM_INTERFACES)
1312

1413
/*
1514
* POSIX Application Environment Profiles (AEP - IEEE Std 1003.13-2003)
1615
*/
1716

17+
#undef _POSIX_AEP_REALTIME_MINIMAL
1818
#ifdef CONFIG_POSIX_AEP_REALTIME_MINIMAL
1919
#define _POSIX_AEP_REALTIME_MINIMAL 200312L
2020
#endif
2121

22+
#undef _POSIX_AEP_REALTIME_CONTROLLER
2223
#ifdef CONFIG_POSIX_AEP_REALTIME_CONTROLLER
2324
#define _POSIX_AEP_REALTIME_CONTROLLER 200312L
2425
#endif
2526

27+
#undef _POSIX_AEP_REALTIME_DEDICATED
2628
#ifdef CONFIG_POSIX_AEP_REALTIME_DEDICATED
2729
#define _POSIX_AEP_REALTIME_DEDICATED 200312L
2830
#endif
@@ -36,30 +38,213 @@
3638
* POSIX System Interfaces
3739
*/
3840

41+
#define _POSIX_VERSION 200809L
42+
43+
#undef _POSIX_CHOWN_RESTRICTED
3944
#define _POSIX_CHOWN_RESTRICTED (0)
40-
#define _POSIX_NO_TRUNC (0)
41-
#define _POSIX_VDISABLE ('\0')
45+
#undef _POSIX_NO_TRUNC
46+
#define _POSIX_NO_TRUNC (0)
47+
#undef _POSIX_VDISABLE
48+
#define _POSIX_VDISABLE ('\0')
4249

4350
/* #define _POSIX_ADVISORY_INFO (-1L) */
4451

52+
#undef _POSIX_ASYNCHRONOUS_IO
53+
#ifdef CONFIG_POSIX_ASYNCHRONOUS_IO
54+
#define _POSIX_ASYNCHRONOUS_IO _POSIX_VERSION
55+
#endif
56+
57+
#undef _POSIX_BARRIERS
58+
#ifdef CONFIG_POSIX_BARRIERS
59+
#define _POSIX_BARRIERS _POSIX_VERSION
60+
#endif
61+
62+
#undef _POSIX_CLOCK_SELECTION
63+
#ifdef CONFIG_POSIX_CLOCK_SELECTION
64+
#define _POSIX_CLOCK_SELECTION _POSIX_VERSION
65+
#endif
66+
67+
#undef _POSIX_CPUTIME
68+
#ifdef CONFIG_POSIX_CPUTIME
69+
#define _POSIX_CPUTIME _POSIX_VERSION
70+
#endif
71+
72+
#undef _POSIX_FSYNC
73+
#ifdef CONFIG_POSIX_FSYNC
74+
#define _POSIX_FSYNC _POSIX_VERSION
75+
#endif
76+
77+
#undef _POSIX_IPV6
78+
#ifdef CONFIG_POSIX_IPV6
79+
#define _POSIX_IPV6 _POSIX_VERSION
80+
#endif
81+
4582
/* #define _POSIX_JOB_CONTROL (-1L) */
4683

84+
#undef _POSIX_MAPPED_FILES
85+
#ifdef CONFIG_POSIX_MAPPED_FILES
86+
#define _POSIX_MAPPED_FILES _POSIX_VERSION
87+
#endif
88+
89+
#ifdef CONFIG_POSIX_MEMLOCK
90+
#define _POSIX_MEMLOCK _POSIX_VERSION
91+
#endif
92+
93+
#undef _POSIX_MEMLOCK_RANGE
94+
#ifdef CONFIG_POSIX_MEMLOCK_RANGE
95+
#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION
96+
#endif
97+
98+
#undef _POSIX_MEMORY_PROTECTION
99+
#ifdef CONFIG_POSIX_MEMORY_PROTECTION
100+
#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION
101+
#endif
102+
103+
#undef _POSIX_MESSAGE_PASSING
104+
#ifdef CONFIG_POSIX_MESSAGE_PASSING
105+
#define _POSIX_MESSAGE_PASSING _POSIX_VERSION
106+
#endif
107+
108+
#undef _POSIX_MONOTONIC_CLOCK
109+
#ifdef CONFIG_POSIX_MONOTONIC_CLOCK
110+
#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION
111+
#endif
112+
47113
/* #define _POSIX_PRIORITIZED_IO (-1L) */
48114

115+
#undef _POSIX_PRIORITY_SCHEDULING
116+
#ifdef CONFIG_POSIX_PRIORITY_SCHEDULING
117+
#define _POSIX_PRIORITY_SCHEDULING _POSIX_VERSION
118+
#endif
119+
120+
#undef _POSIX_RAW_SOCKETS
121+
#ifdef CONFIG_POSIX_RAW_SOCKETS
122+
#define _POSIX_RAW_SOCKETS _POSIX_VERSION
123+
#endif
124+
125+
#undef _POSIX_READER_WRITER_LOCKS
126+
#ifdef CONFIG_POSIX_RW_LOCKS
127+
#define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION
128+
#endif
129+
130+
#undef _POSIX_REALTIME_SIGNALS
131+
#ifdef CONFIG_POSIX_REALTIME_SIGNALS
132+
#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION
133+
#endif
134+
49135
/* #define _POSIX_REGEXP (-1L) */
50136
/* #define _POSIX_SAVED_IDS (-1L) */
51137

138+
#undef _POSIX_SEMAPHORES
139+
#ifdef CONFIG_POSIX_SEMAPHORES
140+
#define _POSIX_SEMAPHORES _POSIX_VERSION
141+
#endif
142+
143+
#undef _POSIX_SHARED_MEMORY_OBJECTS
144+
#ifdef CONFIG_POSIX_SHARED_MEMORY_OBJECTS
145+
#define _POSIX_SHARED_MEMORY_OBJECTS _POSIX_VERSION
146+
#endif
147+
52148
/* #define _POSIX_SHELL (-1L) */
53149
/* #define _POSIX_SPAWN (-1L) */
54150

151+
#undef _POSIX_SPIN_LOCKS
152+
#ifdef CONFIG_POSIX_SPIN_LOCKS
153+
#define _POSIX_SPIN_LOCKS _POSIX_VERSION
154+
#endif
155+
55156
/* #define _POSIX_SPORADIC_SERVER (-1L) */
56157

158+
#undef _POSIX_SYNCHRONIZED_IO
159+
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
160+
#define _POSIX_SYNCHRONIZED_IO _POSIX_VERSION
161+
#endif
162+
163+
#undef _POSIX_THREADS
164+
#undef _POSIX_THREAD_PROCESS_SHARED
165+
#undef _POSIX_PRIORITY_SCHEDULING
166+
#undef _POSIX_THREAD_PRIORITY_SCHEDULING
167+
#ifdef CONFIG_POSIX_THREADS
168+
#define _POSIX_THREADS _POSIX_VERSION
169+
#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
170+
#define _POSIX_PRIORITY_SCHEDULING _POSIX_VERSION
171+
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
172+
#endif
173+
174+
#undef _POSIX_THREAD_ATTR_STACKADDR
175+
#ifdef CONFIG_POSIX_THREAD_ATTR_STACKADDR
176+
#define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION
177+
#endif
178+
179+
#undef _POSIX_THREAD_ATTR_STACKSIZE
180+
#ifdef CONFIG_POSIX_THREAD_ATTR_STACKSIZE
181+
#define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION
182+
#endif
183+
184+
#undef _POSIX_THREAD_CPUTIME
185+
#ifdef CONFIG_POSIX_THREAD_CPUTIME
186+
#define _POSIX_THREAD_CPUTIME _POSIX_VERSION
187+
#endif
188+
189+
#undef _POSIX_THREAD_PRIO_INHERIT
190+
#ifdef CONFIG_POSIX_THREAD_PRIO_INHERIT
191+
#define _POSIX_THREAD_PRIO_INHERIT _POSIX_VERSION
192+
#endif
193+
194+
#undef _POSIX_THREAD_PRIO_PROTECT
195+
#ifdef CONFIG_POSIX_THREAD_PRIO_PROTECT
196+
#define _POSIX_THREAD_PRIO_PROTECT _POSIX_VERSION
197+
#endif
198+
199+
#undef _POSIX_THREAD_PRIORITY_SCHEDULING
200+
#ifdef CONFIG_POSIX_THREAD_PRIORITY_SCHEDULING
201+
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
202+
#endif
203+
57204
/* #define _POSIX_THREAD_PROCESS_SHARED (-1L) */
58205
/* #define _POSIX_THREAD_ROBUST_PRIO_INHERIT (-1L) */
59206
/* #define _POSIX_THREAD_ROBUST_PRIO_PROTECT (-1L) */
60207

208+
#undef _POSIX_THREAD_SAFE_FUNCTIONS
209+
#if defined(CONFIG_POSIX_FILE_SYSTEM_R) && defined(CONFIG_POSIX_C_LANG_SUPPORT_R)
210+
#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
211+
#endif
212+
61213
/* #define _POSIX_THREAD_SPORADIC_SERVER (-1L) */
62214

215+
#undef _POSIX_THREADS
216+
#undef _POSIX_THREAD_PROCESS_SHARED
217+
#undef _POSIX_PRIORITY_SCHEDULING
218+
#undef _POSIX_THREAD_PRIORITY_SCHEDULING
219+
#ifdef CONFIG_POSIX_THREADS
220+
#define _POSIX_THREADS _POSIX_VERSION
221+
#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
222+
#define _POSIX_PRIORITY_SCHEDULING _POSIX_VERSION
223+
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
224+
#endif
225+
226+
#undef _POSIX_TIMEOUTS
227+
#ifdef CONFIG_POSIX_TIMEOUTS
228+
#define _POSIX_TIMEOUTS _POSIX_VERSION
229+
#endif
230+
231+
#undef _POSIX_CPUTIME
232+
#undef _POSIX_MONOTONIC_CLOCK
233+
#undef _POSIX_TIMEOUTS
234+
#undef _POSIX_TIMERS
235+
#ifdef CONFIG_POSIX_TIMERS
236+
#define _POSIX_TIMERS _POSIX_VERSION
237+
#define _POSIX_TIMEOUTS _POSIX_VERSION
238+
/*
239+
* FIXME: Until we have a Kconfig for XSI_ADVANCED_REALTIME, define _POSIX_CPUTIME and
240+
* _POSIX_MONOTONIC_CLOCK with _POSIX_TIMERS.
241+
* For more information on the Advanced Realtime Option Group, please see
242+
* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap02.html
243+
*/
244+
#define _POSIX_CPUTIME _POSIX_VERSION
245+
#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION
246+
#endif
247+
63248
/* #define _POSIX_TRACE (-1L) */
64249
/* #define _POSIX_TRACE_EVENT_FILTER (-1L) */
65250
/* #define _POSIX_TRACE_INHERIT (-1L) */
@@ -85,6 +270,8 @@
85270
/*
86271
* POSIX2 Options
87272
*/
273+
#undef _POSIX2_C_BIND
274+
#define _POSIX2_C_BIND _POSIX_VERSION
88275
/* #define _POSIX2_C_DEV (-1) */
89276
/* #define _POSIX2_CHAR_TERM (-1L) */
90277
/* #define _POSIX2_FORT_DEV (-1L) */
@@ -102,6 +289,12 @@
102289
/*
103290
* X/Open System Interfaces
104291
*/
292+
293+
#undef _XOPEN_VERSION
294+
#ifdef CONFIG_XSI
295+
#define _XOPEN_VERSION 700
296+
#endif
297+
105298
/* #define _XOPEN_CRYPT (-1L) */
106299
/* #define _XOPEN_ENH_I18N (-1L) */
107300
#if defined(CONFIG_XSI_REALTIME) || \
@@ -114,7 +307,18 @@
114307
/* #define _XOPEN_REALTIME_THREADS (-1L) */
115308
/* #define _XOPEN_SHM (-1L) */
116309

117-
/* #define _XOPEN_UNIX (-1L) */
310+
#undef _XOPEN_STREAMS
311+
#ifdef CONFIG_XSI_STREAMS
312+
#define _XOPEN_STREAMS _XOPEN_VERSION
313+
#endif
314+
315+
#undef _XOPEN_UNIX
316+
#ifdef CONFIG_XSI
317+
#define _XOPEN_UNIX _XOPEN_VERSION
318+
#endif
319+
118320
/* #define _XOPEN_UUCP (-1L) */
119321

322+
#endif /* defined(_POSIX_C_SOURCE) || defined(CONFIG_POSIX_SYSTEM_INTERFACES) */
323+
120324
#endif /* INCLUDE_ZEPHYR_POSIX_POSIX_FEATURES_H_ */

include/zephyr/posix/sys/times.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
extern "C" {
1414
#endif
1515

16-
#if defined(_POSIX_MULTI_PROCESS) || defined(__DOXYGEN__)
16+
#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__)
1717

1818
#if !defined(_TMS_DECLARED) && !defined(__tms_defined)
1919
struct tms {
@@ -28,7 +28,7 @@ struct tms {
2828

2929
clock_t times(struct tms *buf);
3030

31-
#endif /* _POSIX_MULTI_PROCESS */
31+
#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */
3232

3333
#ifdef __cplusplus
3434
}

lib/libc/arcmwdt/include/time.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include_next <time.h>
1111

12-
#if defined(_POSIX_C_SOURCE)
12+
#if defined(_POSIX_C_SOURCE) || defined(CONFIG_POSIX_SYSTEM_INTERFACES)
1313
/*
1414
* POSIX requires time.h to define pid_t and clockid_t
1515
* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html

lib/libc/arcmwdt/include/unistd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include_next <unistd.h>
1111

12+
#include <zephyr/posix/posix_features.h>
13+
1214
#ifdef __cplusplus
1315
extern "C" {
1416
#endif

lib/libc/newlib/include/unistd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include_next <unistd.h>
1111

12+
#include <zephyr/posix/posix_features.h>
13+
1214
#ifdef __cplusplus
1315
extern "C" {
1416
#endif

lib/libc/picolibc/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ zephyr_library_sources(
1111
stdio.c
1212
)
1313
zephyr_include_directories(include)
14+
if(CONFIG_PICOLIBC_USE_MODULE)
15+
# temporary workaround for https://github.com/picolibc/picolibc/pull/1079
16+
zephyr_compile_options(-Dpid_t=__pid_t)
17+
endif()
1418

1519
zephyr_library_compile_options($<TARGET_PROPERTY:compiler,prohibit_lto>)
1620

lib/libc/picolibc/include/time.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#ifndef ZEPHYR_LIB_LIBC_PICOLIBC_INCLUDE_TIME_H_
88
#define ZEPHYR_LIB_LIBC_PICOLIBC_INCLUDE_TIME_H_
99

10-
#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__)
10+
#if defined(_POSIX_C_SOURCE) || defined(CONFIG_POSIX_SYSTEM_INTERFACES)
1111

1212
/* temporary workaround for https://github.com/picolibc/picolibc/pull/1079 */
1313
#include <sys/_types.h>
@@ -25,7 +25,7 @@ typedef __pid_t pid_t;
2525
}
2626
#endif
2727

28-
#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */
28+
#endif /* defined(_POSIX_C_SOURCE) || defined(CONFIG_POSIX_SYSTEM_INTERFACES) */
2929

3030
#include_next <time.h>
3131

lib/libc/picolibc/include/unistd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include_next <unistd.h>
1111

12+
#include <zephyr/posix/posix_features.h>
13+
1214
#ifdef __cplusplus
1315
extern "C" {
1416
#endif
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
# Copyright The Zephyr Project Contributors
2-
# SPDX-License-Identifier: Apache-2.0
3-
4-
set(POSIX_VERSION 200809L)
5-
6-
if (CONFIG_POSIX_C_LANG_SUPPORT_R)
7-
zephyr_compile_definitions(-D_POSIX_THREAD_SAFE_FUNCTIONS=${POSIX_VERSION})
8-
endif()
1+
# intentionally blank

0 commit comments

Comments
 (0)