Skip to content

Commit 60fb460

Browse files
committed
posix: ensure that unistd.h declares version and symbolic constants
The specification requires that unistd.h (or a file that is included via unistd.h) declare version macros and symbolic constants. Feature test macros declarations are also removed from `lib/posix/options/CMakeLists.txt`. Closes #97949 Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent 5300fc7 commit 60fb460

File tree

11 files changed

+241
-107
lines changed

11 files changed

+241
-107
lines changed

include/zephyr/posix/posix_features.h

Lines changed: 224 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,38 @@
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+
/*
12+
* Note: this header is included (currently) via
13+
* `-imacros ${ZEPHYR_BASE}/include/zephyr/posix/posix_features.h`.
14+
*
15+
* It is not appropriate to place anything other than "symbolic constants" in this file. In this
16+
* context, "symbolic constants" refers to those listed in the section "Constants for Options and
17+
* Option Groups", in the POSIX specification.
18+
* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html
19+
*
20+
* If there is a better alternative, to ensure that the contents of this file are available to
21+
* every compilation unit, for arbitrary C compilers, C libraries, toolchains, and so on, it is
22+
* to be investigated as part of the issue below.
23+
*
24+
* https://github.com/zephyrproject-rtos/zephyr/issues/98525
25+
*/
26+
#if defined(_POSIX_C_SOURCE) && defined(CONFIG_POSIX_SYSTEM_INTERFACES)
1327

1428
/*
1529
* POSIX Application Environment Profiles (AEP - IEEE Std 1003.13-2003)
1630
*/
1731

32+
#undef _POSIX_AEP_REALTIME_MINIMAL
1833
#ifdef CONFIG_POSIX_AEP_REALTIME_MINIMAL
1934
#define _POSIX_AEP_REALTIME_MINIMAL 200312L
2035
#endif
2136

37+
#undef _POSIX_AEP_REALTIME_CONTROLLER
2238
#ifdef CONFIG_POSIX_AEP_REALTIME_CONTROLLER
2339
#define _POSIX_AEP_REALTIME_CONTROLLER 200312L
2440
#endif
2541

42+
#undef _POSIX_AEP_REALTIME_DEDICATED
2643
#ifdef CONFIG_POSIX_AEP_REALTIME_DEDICATED
2744
#define _POSIX_AEP_REALTIME_DEDICATED 200312L
2845
#endif
@@ -36,30 +53,213 @@
3653
* POSIX System Interfaces
3754
*/
3855

56+
#define _POSIX_VERSION 200809L
57+
58+
#undef _POSIX_CHOWN_RESTRICTED
3959
#define _POSIX_CHOWN_RESTRICTED (0)
40-
#define _POSIX_NO_TRUNC (0)
41-
#define _POSIX_VDISABLE ('\0')
60+
#undef _POSIX_NO_TRUNC
61+
#define _POSIX_NO_TRUNC (0)
62+
#undef _POSIX_VDISABLE
63+
#define _POSIX_VDISABLE ('\0')
4264

4365
/* #define _POSIX_ADVISORY_INFO (-1L) */
4466

67+
#undef _POSIX_ASYNCHRONOUS_IO
68+
#ifdef CONFIG_POSIX_ASYNCHRONOUS_IO
69+
#define _POSIX_ASYNCHRONOUS_IO _POSIX_VERSION
70+
#endif
71+
72+
#undef _POSIX_BARRIERS
73+
#ifdef CONFIG_POSIX_BARRIERS
74+
#define _POSIX_BARRIERS _POSIX_VERSION
75+
#endif
76+
77+
#undef _POSIX_CLOCK_SELECTION
78+
#ifdef CONFIG_POSIX_CLOCK_SELECTION
79+
#define _POSIX_CLOCK_SELECTION _POSIX_VERSION
80+
#endif
81+
82+
#undef _POSIX_CPUTIME
83+
#ifdef CONFIG_POSIX_CPUTIME
84+
#define _POSIX_CPUTIME _POSIX_VERSION
85+
#endif
86+
87+
#undef _POSIX_FSYNC
88+
#ifdef CONFIG_POSIX_FSYNC
89+
#define _POSIX_FSYNC _POSIX_VERSION
90+
#endif
91+
92+
#undef _POSIX_IPV6
93+
#ifdef CONFIG_POSIX_IPV6
94+
#define _POSIX_IPV6 _POSIX_VERSION
95+
#endif
96+
4597
/* #define _POSIX_JOB_CONTROL (-1L) */
4698

99+
#undef _POSIX_MAPPED_FILES
100+
#ifdef CONFIG_POSIX_MAPPED_FILES
101+
#define _POSIX_MAPPED_FILES _POSIX_VERSION
102+
#endif
103+
104+
#ifdef CONFIG_POSIX_MEMLOCK
105+
#define _POSIX_MEMLOCK _POSIX_VERSION
106+
#endif
107+
108+
#undef _POSIX_MEMLOCK_RANGE
109+
#ifdef CONFIG_POSIX_MEMLOCK_RANGE
110+
#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION
111+
#endif
112+
113+
#undef _POSIX_MEMORY_PROTECTION
114+
#ifdef CONFIG_POSIX_MEMORY_PROTECTION
115+
#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION
116+
#endif
117+
118+
#undef _POSIX_MESSAGE_PASSING
119+
#ifdef CONFIG_POSIX_MESSAGE_PASSING
120+
#define _POSIX_MESSAGE_PASSING _POSIX_VERSION
121+
#endif
122+
123+
#undef _POSIX_MONOTONIC_CLOCK
124+
#ifdef CONFIG_POSIX_MONOTONIC_CLOCK
125+
#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION
126+
#endif
127+
47128
/* #define _POSIX_PRIORITIZED_IO (-1L) */
48129

130+
#undef _POSIX_PRIORITY_SCHEDULING
131+
#ifdef CONFIG_POSIX_PRIORITY_SCHEDULING
132+
#define _POSIX_PRIORITY_SCHEDULING _POSIX_VERSION
133+
#endif
134+
135+
#undef _POSIX_RAW_SOCKETS
136+
#ifdef CONFIG_POSIX_RAW_SOCKETS
137+
#define _POSIX_RAW_SOCKETS _POSIX_VERSION
138+
#endif
139+
140+
#undef _POSIX_READER_WRITER_LOCKS
141+
#ifdef CONFIG_POSIX_RW_LOCKS
142+
#define _POSIX_READER_WRITER_LOCKS _POSIX_VERSION
143+
#endif
144+
145+
#undef _POSIX_REALTIME_SIGNALS
146+
#ifdef CONFIG_POSIX_REALTIME_SIGNALS
147+
#define _POSIX_REALTIME_SIGNALS _POSIX_VERSION
148+
#endif
149+
49150
/* #define _POSIX_REGEXP (-1L) */
50151
/* #define _POSIX_SAVED_IDS (-1L) */
51152

153+
#undef _POSIX_SEMAPHORES
154+
#ifdef CONFIG_POSIX_SEMAPHORES
155+
#define _POSIX_SEMAPHORES _POSIX_VERSION
156+
#endif
157+
158+
#undef _POSIX_SHARED_MEMORY_OBJECTS
159+
#ifdef CONFIG_POSIX_SHARED_MEMORY_OBJECTS
160+
#define _POSIX_SHARED_MEMORY_OBJECTS _POSIX_VERSION
161+
#endif
162+
52163
/* #define _POSIX_SHELL (-1L) */
53164
/* #define _POSIX_SPAWN (-1L) */
54165

166+
#undef _POSIX_SPIN_LOCKS
167+
#ifdef CONFIG_POSIX_SPIN_LOCKS
168+
#define _POSIX_SPIN_LOCKS _POSIX_VERSION
169+
#endif
170+
55171
/* #define _POSIX_SPORADIC_SERVER (-1L) */
56172

173+
#undef _POSIX_SYNCHRONIZED_IO
174+
#ifdef CONFIG_POSIX_SYNCHRONIZED_IO
175+
#define _POSIX_SYNCHRONIZED_IO _POSIX_VERSION
176+
#endif
177+
178+
#undef _POSIX_THREADS
179+
#undef _POSIX_THREAD_PROCESS_SHARED
180+
#undef _POSIX_PRIORITY_SCHEDULING
181+
#undef _POSIX_THREAD_PRIORITY_SCHEDULING
182+
#ifdef CONFIG_POSIX_THREADS
183+
#define _POSIX_THREADS _POSIX_VERSION
184+
#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
185+
#define _POSIX_PRIORITY_SCHEDULING _POSIX_VERSION
186+
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
187+
#endif
188+
189+
#undef _POSIX_THREAD_ATTR_STACKADDR
190+
#ifdef CONFIG_POSIX_THREAD_ATTR_STACKADDR
191+
#define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION
192+
#endif
193+
194+
#undef _POSIX_THREAD_ATTR_STACKSIZE
195+
#ifdef CONFIG_POSIX_THREAD_ATTR_STACKSIZE
196+
#define _POSIX_THREAD_ATTR_STACKSIZE _POSIX_VERSION
197+
#endif
198+
199+
#undef _POSIX_THREAD_CPUTIME
200+
#ifdef CONFIG_POSIX_THREAD_CPUTIME
201+
#define _POSIX_THREAD_CPUTIME _POSIX_VERSION
202+
#endif
203+
204+
#undef _POSIX_THREAD_PRIO_INHERIT
205+
#ifdef CONFIG_POSIX_THREAD_PRIO_INHERIT
206+
#define _POSIX_THREAD_PRIO_INHERIT _POSIX_VERSION
207+
#endif
208+
209+
#undef _POSIX_THREAD_PRIO_PROTECT
210+
#ifdef CONFIG_POSIX_THREAD_PRIO_PROTECT
211+
#define _POSIX_THREAD_PRIO_PROTECT _POSIX_VERSION
212+
#endif
213+
214+
#undef _POSIX_THREAD_PRIORITY_SCHEDULING
215+
#ifdef CONFIG_POSIX_THREAD_PRIORITY_SCHEDULING
216+
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
217+
#endif
218+
57219
/* #define _POSIX_THREAD_PROCESS_SHARED (-1L) */
58220
/* #define _POSIX_THREAD_ROBUST_PRIO_INHERIT (-1L) */
59221
/* #define _POSIX_THREAD_ROBUST_PRIO_PROTECT (-1L) */
60222

223+
#undef _POSIX_THREAD_SAFE_FUNCTIONS
224+
#if defined(CONFIG_POSIX_FILE_SYSTEM_R) && defined(CONFIG_POSIX_C_LANG_SUPPORT_R)
225+
#define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
226+
#endif
227+
61228
/* #define _POSIX_THREAD_SPORADIC_SERVER (-1L) */
62229

230+
#undef _POSIX_THREADS
231+
#undef _POSIX_THREAD_PROCESS_SHARED
232+
#undef _POSIX_PRIORITY_SCHEDULING
233+
#undef _POSIX_THREAD_PRIORITY_SCHEDULING
234+
#ifdef CONFIG_POSIX_THREADS
235+
#define _POSIX_THREADS _POSIX_VERSION
236+
#define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
237+
#define _POSIX_PRIORITY_SCHEDULING _POSIX_VERSION
238+
#define _POSIX_THREAD_PRIORITY_SCHEDULING _POSIX_VERSION
239+
#endif
240+
241+
#undef _POSIX_TIMEOUTS
242+
#ifdef CONFIG_POSIX_TIMEOUTS
243+
#define _POSIX_TIMEOUTS _POSIX_VERSION
244+
#endif
245+
246+
#undef _POSIX_CPUTIME
247+
#undef _POSIX_MONOTONIC_CLOCK
248+
#undef _POSIX_TIMEOUTS
249+
#undef _POSIX_TIMERS
250+
#ifdef CONFIG_POSIX_TIMERS
251+
#define _POSIX_TIMERS _POSIX_VERSION
252+
#define _POSIX_TIMEOUTS _POSIX_VERSION
253+
/*
254+
* FIXME: Until we have a Kconfig for XSI_ADVANCED_REALTIME, define _POSIX_CPUTIME and
255+
* _POSIX_MONOTONIC_CLOCK with _POSIX_TIMERS.
256+
* For more information on the Advanced Realtime Option Group, please see
257+
* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap02.html
258+
*/
259+
#define _POSIX_CPUTIME _POSIX_VERSION
260+
#define _POSIX_MONOTONIC_CLOCK _POSIX_VERSION
261+
#endif
262+
63263
/* #define _POSIX_TRACE (-1L) */
64264
/* #define _POSIX_TRACE_EVENT_FILTER (-1L) */
65265
/* #define _POSIX_TRACE_INHERIT (-1L) */
@@ -85,6 +285,8 @@
85285
/*
86286
* POSIX2 Options
87287
*/
288+
#undef _POSIX2_C_BIND
289+
#define _POSIX2_C_BIND _POSIX_VERSION
88290
/* #define _POSIX2_C_DEV (-1) */
89291
/* #define _POSIX2_CHAR_TERM (-1L) */
90292
/* #define _POSIX2_FORT_DEV (-1L) */
@@ -102,6 +304,12 @@
102304
/*
103305
* X/Open System Interfaces
104306
*/
307+
308+
#undef _XOPEN_VERSION
309+
#ifdef CONFIG_XSI
310+
#define _XOPEN_VERSION 700
311+
#endif
312+
105313
/* #define _XOPEN_CRYPT (-1L) */
106314
/* #define _XOPEN_ENH_I18N (-1L) */
107315
#if defined(CONFIG_XSI_REALTIME) || \
@@ -114,7 +322,18 @@
114322
/* #define _XOPEN_REALTIME_THREADS (-1L) */
115323
/* #define _XOPEN_SHM (-1L) */
116324

117-
/* #define _XOPEN_UNIX (-1L) */
325+
#undef _XOPEN_STREAMS
326+
#ifdef CONFIG_XSI_STREAMS
327+
#define _XOPEN_STREAMS _XOPEN_VERSION
328+
#endif
329+
330+
#undef _XOPEN_UNIX
331+
#ifdef CONFIG_XSI
332+
#define _XOPEN_UNIX _XOPEN_VERSION
333+
#endif
334+
118335
/* #define _XOPEN_UUCP (-1L) */
119336

337+
#endif /* defined(_POSIX_C_SOURCE) || defined(CONFIG_POSIX_SYSTEM_INTERFACES) */
338+
120339
#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
@@ -13,6 +13,10 @@ zephyr_library_sources(
1313
if(CONFIG_POSIX_BASE_DEFINITIONS OR CONFIG_EVENTFD)
1414
zephyr_include_directories(include)
1515
endif()
16+
if(CONFIG_PICOLIBC_USE_MODULE)
17+
# temporary workaround for https://github.com/picolibc/picolibc/pull/1079
18+
zephyr_compile_options(-Dpid_t=__pid_t)
19+
endif()
1620

1721
zephyr_library_compile_options($<TARGET_PROPERTY:compiler,prohibit_lto>)
1822

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

lib/posix/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
# zephyr-keep-sorted-start
44
add_subdirectory_ifdef(CONFIG_EVENTFD eventfd)
5-
add_subdirectory_ifdef(CONFIG_POSIX_C_LANG_SUPPORT_R c_lang_support_r)
65
add_subdirectory_ifdef(CONFIG_POSIX_C_LIB_EXT c_lib_ext)
76
add_subdirectory_ifdef(CONFIG_POSIX_SHELL shell)
87
add_subdirectory_ifdef(CONFIG_POSIX_SYSTEM_INTERFACES options)

lib/posix/c_lang_support_r/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)