Skip to content

Commit c5f1c43

Browse files
Nicholas Lowellkartben
authored andcommitted
posix: sysconf: match _SC* defines with newlib/picolib
It's possible for newlib/picolib libc libraries to internally call sysconf() which would execute zephyr's implementation. However, if the _SC* defines do not have matching values, then the incorrect switch case executes. This issue arises when using newlib/picolib libc that includes sysconf implementation for ARM. With current defaults, the zephyr sysconf() overrides the original libc sysconf() so we must ensure proper operation. We will switch to the #define list just like newlib/picolib. We can't currently use their unistd.h directly due to a domino of declaration conflicts. For the "small" macro implementation, we have to drop using CONCAT to prevent pre-expansion of the new #defines Signed-off-by: Nicholas Lowell <Nicholas.Lowell@lexmark.com>
1 parent c14f97f commit c5f1c43

File tree

3 files changed

+170
-146
lines changed

3 files changed

+170
-146
lines changed

include/zephyr/posix/sys/sysconf.h

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

16-
enum {
17-
_SC_ADVISORY_INFO,
18-
_SC_ASYNCHRONOUS_IO,
19-
_SC_BARRIERS,
20-
_SC_CLOCK_SELECTION,
21-
_SC_CPUTIME,
22-
_SC_FSYNC,
23-
_SC_IPV6,
24-
_SC_JOB_CONTROL,
25-
_SC_MAPPED_FILES,
26-
_SC_MEMLOCK,
27-
_SC_MEMLOCK_RANGE,
28-
_SC_MEMORY_PROTECTION,
29-
_SC_MESSAGE_PASSING,
30-
_SC_MONOTONIC_CLOCK,
31-
_SC_PRIORITIZED_IO,
32-
_SC_PRIORITY_SCHEDULING,
33-
_SC_RAW_SOCKETS,
34-
_SC_RE_DUP_MAX,
35-
_SC_READER_WRITER_LOCKS,
36-
_SC_REALTIME_SIGNALS,
37-
_SC_REGEXP,
38-
_SC_SAVED_IDS,
39-
_SC_SEMAPHORES,
40-
_SC_SHARED_MEMORY_OBJECTS,
41-
_SC_SHELL,
42-
_SC_SPAWN,
43-
_SC_SPIN_LOCKS,
44-
_SC_SPORADIC_SERVER,
45-
_SC_SS_REPL_MAX,
46-
_SC_SYNCHRONIZED_IO,
47-
_SC_THREAD_ATTR_STACKADDR,
48-
_SC_THREAD_ATTR_STACKSIZE,
49-
_SC_THREAD_CPUTIME,
50-
_SC_THREAD_PRIO_INHERIT,
51-
_SC_THREAD_PRIO_PROTECT,
52-
_SC_THREAD_PRIORITY_SCHEDULING,
53-
_SC_THREAD_PROCESS_SHARED,
54-
_SC_THREAD_ROBUST_PRIO_INHERIT,
55-
_SC_THREAD_ROBUST_PRIO_PROTECT,
56-
_SC_THREAD_SAFE_FUNCTIONS,
57-
_SC_THREAD_SPORADIC_SERVER,
58-
_SC_THREADS,
59-
_SC_TIMEOUTS,
60-
_SC_TIMERS,
61-
_SC_TRACE,
62-
_SC_TRACE_EVENT_FILTER,
63-
_SC_TRACE_EVENT_NAME_MAX,
64-
_SC_TRACE_INHERIT,
65-
_SC_TRACE_LOG,
66-
_SC_TRACE_NAME_MAX,
67-
_SC_TRACE_SYS_MAX,
68-
_SC_TRACE_USER_EVENT_MAX,
69-
_SC_TYPED_MEMORY_OBJECTS,
70-
_SC_VERSION,
71-
_SC_V7_ILP32_OFF32,
72-
_SC_V7_ILP32_OFFBIG,
73-
_SC_V7_LP64_OFF64,
74-
_SC_V7_LPBIG_OFFBIG,
75-
_SC_V6_ILP32_OFF32,
76-
_SC_V6_ILP32_OFFBIG,
77-
_SC_V6_LP64_OFF64,
78-
_SC_V6_LPBIG_OFFBIG,
79-
_SC_BC_BASE_MAX,
80-
_SC_BC_DIM_MAX,
81-
_SC_BC_SCALE_MAX,
82-
_SC_BC_STRING_MAX,
83-
_SC_2_C_BIND,
84-
_SC_2_C_DEV,
85-
_SC_2_CHAR_TERM,
86-
_SC_COLL_WEIGHTS_MAX,
87-
_SC_DELAYTIMER_MAX,
88-
_SC_EXPR_NEST_MAX,
89-
_SC_2_FORT_DEV,
90-
_SC_2_FORT_RUN,
91-
_SC_LINE_MAX,
92-
_SC_2_LOCALEDEF,
93-
_SC_2_PBS,
94-
_SC_2_PBS_ACCOUNTING,
95-
_SC_2_PBS_CHECKPOINT,
96-
_SC_2_PBS_LOCATE,
97-
_SC_2_PBS_MESSAGE,
98-
_SC_2_PBS_TRACK,
99-
_SC_2_SW_DEV,
100-
_SC_2_UPE,
101-
_SC_2_VERSION,
102-
_SC_XOPEN_CRYPT,
103-
_SC_XOPEN_ENH_I18N,
104-
_SC_XOPEN_REALTIME,
105-
_SC_XOPEN_REALTIME_THREADS,
106-
_SC_XOPEN_SHM,
107-
_SC_XOPEN_STREAMS,
108-
_SC_XOPEN_UNIX,
109-
_SC_XOPEN_UUCP,
110-
_SC_XOPEN_VERSION,
111-
_SC_CLK_TCK,
112-
_SC_GETGR_R_SIZE_MAX,
113-
_SC_GETPW_R_SIZE_MAX,
114-
_SC_AIO_LISTIO_MAX,
115-
_SC_AIO_MAX,
116-
_SC_AIO_PRIO_DELTA_MAX,
117-
_SC_ARG_MAX,
118-
_SC_ATEXIT_MAX,
119-
_SC_CHILD_MAX,
120-
_SC_HOST_NAME_MAX,
121-
_SC_IOV_MAX,
122-
_SC_LOGIN_NAME_MAX,
123-
_SC_NGROUPS_MAX,
124-
_SC_MQ_OPEN_MAX,
125-
_SC_MQ_PRIO_MAX,
126-
_SC_OPEN_MAX,
127-
_SC_PAGE_SIZE,
128-
_SC_PAGESIZE,
129-
_SC_THREAD_DESTRUCTOR_ITERATIONS,
130-
_SC_THREAD_KEYS_MAX,
131-
_SC_THREAD_STACK_MIN,
132-
_SC_THREAD_THREADS_MAX,
133-
_SC_RTSIG_MAX,
134-
_SC_SEM_NSEMS_MAX,
135-
_SC_SEM_VALUE_MAX,
136-
_SC_SIGQUEUE_MAX,
137-
_SC_STREAM_MAX,
138-
_SC_SYMLOOP_MAX,
139-
_SC_TIMER_MAX,
140-
_SC_TTY_NAME_MAX,
141-
_SC_TZNAME_MAX,
142-
};
16+
/* Values assigned are intended to match the values assigned in newlib and picolib
17+
* Even though the POSIX standard does not require specific values, this seems to be
18+
* required for proper sysconf() operation when called from within newlib/picolib itself.
19+
*/
20+
#define _SC_ARG_MAX 0
21+
#define _SC_CHILD_MAX 1
22+
#define _SC_CLK_TCK 2
23+
#define _SC_NGROUPS_MAX 3
24+
#define _SC_OPEN_MAX 4
25+
#define _SC_JOB_CONTROL 5
26+
#define _SC_SAVED_IDS 6
27+
#define _SC_VERSION 7
28+
#define _SC_PAGESIZE 8
29+
#define _SC_PAGE_SIZE _SC_PAGESIZE
30+
/* These are non-POSIX values we accidentally introduced in 2000 without
31+
* guarding them. Keeping them unguarded for backward compatibility.
32+
*/
33+
#define _SC_NPROCESSORS_CONF 9
34+
#define _SC_NPROCESSORS_ONLN 10
35+
#define _SC_PHYS_PAGES 11
36+
#define _SC_AVPHYS_PAGES 12
37+
/* End of non-POSIX values. */
38+
#define _SC_MQ_OPEN_MAX 13
39+
#define _SC_MQ_PRIO_MAX 14
40+
#define _SC_RTSIG_MAX 15
41+
#define _SC_SEM_NSEMS_MAX 16
42+
#define _SC_SEM_VALUE_MAX 17
43+
#define _SC_SIGQUEUE_MAX 18
44+
#define _SC_TIMER_MAX 19
45+
#define _SC_TZNAME_MAX 20
46+
#define _SC_ASYNCHRONOUS_IO 21
47+
#define _SC_FSYNC 22
48+
#define _SC_MAPPED_FILES 23
49+
#define _SC_MEMLOCK 24
50+
#define _SC_MEMLOCK_RANGE 25
51+
#define _SC_MEMORY_PROTECTION 26
52+
#define _SC_MESSAGE_PASSING 27
53+
#define _SC_PRIORITIZED_IO 28
54+
#define _SC_REALTIME_SIGNALS 29
55+
#define _SC_SEMAPHORES 30
56+
#define _SC_SHARED_MEMORY_OBJECTS 31
57+
#define _SC_SYNCHRONIZED_IO 32
58+
#define _SC_TIMERS 33
59+
#define _SC_AIO_LISTIO_MAX 34
60+
#define _SC_AIO_MAX 35
61+
#define _SC_AIO_PRIO_DELTA_MAX 36
62+
#define _SC_DELAYTIMER_MAX 37
63+
#define _SC_THREAD_KEYS_MAX 38
64+
#define _SC_THREAD_STACK_MIN 39
65+
#define _SC_THREAD_THREADS_MAX 40
66+
#define _SC_TTY_NAME_MAX 41
67+
#define _SC_THREADS 42
68+
#define _SC_THREAD_ATTR_STACKADDR 43
69+
#define _SC_THREAD_ATTR_STACKSIZE 44
70+
#define _SC_THREAD_PRIORITY_SCHEDULING 45
71+
#define _SC_THREAD_PRIO_INHERIT 46
72+
/* _SC_THREAD_PRIO_PROTECT was _SC_THREAD_PRIO_CEILING in early drafts */
73+
#define _SC_THREAD_PRIO_PROTECT 47
74+
#define _SC_THREAD_PRIO_CEILING _SC_THREAD_PRIO_PROTECT
75+
#define _SC_THREAD_PROCESS_SHARED 48
76+
#define _SC_THREAD_SAFE_FUNCTIONS 49
77+
#define _SC_GETGR_R_SIZE_MAX 50
78+
#define _SC_GETPW_R_SIZE_MAX 51
79+
#define _SC_LOGIN_NAME_MAX 52
80+
#define _SC_THREAD_DESTRUCTOR_ITERATIONS 53
81+
#define _SC_ADVISORY_INFO 54
82+
#define _SC_ATEXIT_MAX 55
83+
#define _SC_BARRIERS 56
84+
#define _SC_BC_BASE_MAX 57
85+
#define _SC_BC_DIM_MAX 58
86+
#define _SC_BC_SCALE_MAX 59
87+
#define _SC_BC_STRING_MAX 60
88+
#define _SC_CLOCK_SELECTION 61
89+
#define _SC_COLL_WEIGHTS_MAX 62
90+
#define _SC_CPUTIME 63
91+
#define _SC_EXPR_NEST_MAX 64
92+
#define _SC_HOST_NAME_MAX 65
93+
#define _SC_IOV_MAX 66
94+
#define _SC_IPV6 67
95+
#define _SC_LINE_MAX 68
96+
#define _SC_MONOTONIC_CLOCK 69
97+
#define _SC_RAW_SOCKETS 70
98+
#define _SC_READER_WRITER_LOCKS 71
99+
#define _SC_REGEXP 72
100+
#define _SC_RE_DUP_MAX 73
101+
#define _SC_SHELL 74
102+
#define _SC_SPAWN 75
103+
#define _SC_SPIN_LOCKS 76
104+
#define _SC_SPORADIC_SERVER 77
105+
#define _SC_SS_REPL_MAX 78
106+
#define _SC_SYMLOOP_MAX 79
107+
#define _SC_THREAD_CPUTIME 80
108+
#define _SC_THREAD_SPORADIC_SERVER 81
109+
#define _SC_TIMEOUTS 82
110+
#define _SC_TRACE 83
111+
#define _SC_TRACE_EVENT_FILTER 84
112+
#define _SC_TRACE_EVENT_NAME_MAX 85
113+
#define _SC_TRACE_INHERIT 86
114+
#define _SC_TRACE_LOG 87
115+
#define _SC_TRACE_NAME_MAX 88
116+
#define _SC_TRACE_SYS_MAX 89
117+
#define _SC_TRACE_USER_EVENT_MAX 90
118+
#define _SC_TYPED_MEMORY_OBJECTS 91
119+
#define _SC_V7_ILP32_OFF32 92
120+
#define _SC_V6_ILP32_OFF32 _SC_V7_ILP32_OFF32
121+
#define _SC_XBS5_ILP32_OFF32 _SC_V7_ILP32_OFF32
122+
#define _SC_V7_ILP32_OFFBIG 93
123+
#define _SC_V6_ILP32_OFFBIG _SC_V7_ILP32_OFFBIG
124+
#define _SC_XBS5_ILP32_OFFBIG _SC_V7_ILP32_OFFBIG
125+
#define _SC_V7_LP64_OFF64 94
126+
#define _SC_V6_LP64_OFF64 _SC_V7_LP64_OFF64
127+
#define _SC_XBS5_LP64_OFF64 _SC_V7_LP64_OFF64
128+
#define _SC_V7_LPBIG_OFFBIG 95
129+
#define _SC_V6_LPBIG_OFFBIG _SC_V7_LPBIG_OFFBIG
130+
#define _SC_XBS5_LPBIG_OFFBIG _SC_V7_LPBIG_OFFBIG
131+
#define _SC_XOPEN_CRYPT 96
132+
#define _SC_XOPEN_ENH_I18N 97
133+
#define _SC_XOPEN_LEGACY 98
134+
#define _SC_XOPEN_REALTIME 99
135+
#define _SC_STREAM_MAX 100
136+
#define _SC_PRIORITY_SCHEDULING 101
137+
#define _SC_XOPEN_REALTIME_THREADS 102
138+
#define _SC_XOPEN_SHM 103
139+
#define _SC_XOPEN_STREAMS 104
140+
#define _SC_XOPEN_UNIX 105
141+
#define _SC_XOPEN_VERSION 106
142+
#define _SC_2_CHAR_TERM 107
143+
#define _SC_2_C_BIND 108
144+
#define _SC_2_C_DEV 109
145+
#define _SC_2_FORT_DEV 110
146+
#define _SC_2_FORT_RUN 111
147+
#define _SC_2_LOCALEDEF 112
148+
#define _SC_2_PBS 113
149+
#define _SC_2_PBS_ACCOUNTING 114
150+
#define _SC_2_PBS_CHECKPOINT 115
151+
#define _SC_2_PBS_LOCATE 116
152+
#define _SC_2_PBS_MESSAGE 117
153+
#define _SC_2_PBS_TRACK 118
154+
#define _SC_2_SW_DEV 119
155+
#define _SC_2_UPE 120
156+
#define _SC_2_VERSION 121
157+
#define _SC_THREAD_ROBUST_PRIO_INHERIT 122
158+
#define _SC_THREAD_ROBUST_PRIO_PROTECT 123
159+
#define _SC_XOPEN_UUCP 124
160+
#define _SC_LEVEL1_ICACHE_SIZE 125
161+
#define _SC_LEVEL1_ICACHE_ASSOC 126
162+
#define _SC_LEVEL1_ICACHE_LINESIZE 127
163+
#define _SC_LEVEL1_DCACHE_SIZE 128
164+
#define _SC_LEVEL1_DCACHE_ASSOC 129
165+
#define _SC_LEVEL1_DCACHE_LINESIZE 130
166+
#define _SC_LEVEL2_CACHE_SIZE 131
167+
#define _SC_LEVEL2_CACHE_ASSOC 132
168+
#define _SC_LEVEL2_CACHE_LINESIZE 133
169+
#define _SC_LEVEL3_CACHE_SIZE 134
170+
#define _SC_LEVEL3_CACHE_ASSOC 135
171+
#define _SC_LEVEL3_CACHE_LINESIZE 136
172+
#define _SC_LEVEL4_CACHE_SIZE 137
173+
#define _SC_LEVEL4_CACHE_ASSOC 138
174+
#define _SC_LEVEL4_CACHE_LINESIZE 139
175+
#define _SC_POSIX_26_VERSION 140
143176

144177
#define __z_posix_sysconf_SC_ADVISORY_INFO (-1L)
145178
#define __z_posix_sysconf_SC_ASYNCHRONOUS_IO \
@@ -295,10 +328,6 @@ enum {
295328
#define __z_posix_sysconf_SC_TTY_NAME_MAX TTY_NAME_MAX
296329
#define __z_posix_sysconf_SC_TZNAME_MAX TZNAME_MAX
297330

298-
#ifdef CONFIG_POSIX_SYSCONF_IMPL_MACRO
299-
#define sysconf(x) (long)CONCAT(__z_posix_sysconf, x)
300-
#endif
301-
302331
#ifdef __cplusplus
303332
}
304333
#endif

include/zephyr/posix/unistd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ size_t confstr(int name, char *buf, size_t len);
6464
#endif
6565

6666
#ifdef CONFIG_POSIX_SYSCONF_IMPL_MACRO
67-
#define sysconf(x) (long)CONCAT(__z_posix_sysconf, x)
67+
/* Can't use CONCAT(), must concat directly to prevent expansion of 'x' */
68+
#define sysconf(x) (long)__z_posix_sysconf##x
6869
#else
6970
long sysconf(int opt);
7071
#endif /* CONFIG_POSIX_SYSCONF_IMPL_FULL */

lib/posix/options/sysconf.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
#undef _POSIX_C_SOURCE
88
#define _POSIX_C_SOURCE 200809L
9-
#include <zephyr/posix/pthread.h>
109
#include <zephyr/posix/sys/sysconf.h>
1110
#include <zephyr/posix/unistd.h>
1211

1312
#ifdef CONFIG_POSIX_SYSCONF_IMPL_FULL
14-
15-
#define z_sysconf(x) (long)CONCAT(__z_posix_sysconf, x)
13+
/* Can't use CONCAT(), must concat directly to prevent expansion of 'x' */
14+
#define z_sysconf(x) (long)__z_posix_sysconf##x
1615

1716
long sysconf(int x)
1817
{
@@ -125,21 +124,17 @@ long sysconf(int x)
125124
return z_sysconf(_SC_TYPED_MEMORY_OBJECTS);
126125
case _SC_VERSION:
127126
return z_sysconf(_SC_VERSION);
128-
case _SC_V6_ILP32_OFF32:
129-
return z_sysconf(_SC_V6_ILP32_OFF32);
130-
case _SC_V6_ILP32_OFFBIG:
131-
return z_sysconf(_SC_V6_ILP32_OFFBIG);
132-
case _SC_V6_LP64_OFF64:
133-
return z_sysconf(_SC_V6_LP64_OFF64);
134-
case _SC_V6_LPBIG_OFFBIG:
135-
return z_sysconf(_SC_V6_LPBIG_OFFBIG);
136127
case _SC_V7_ILP32_OFF32:
128+
/* case _SC_V6_ILP32_OFF32 */
137129
return z_sysconf(_SC_V7_ILP32_OFF32);
138130
case _SC_V7_ILP32_OFFBIG:
131+
/* case _SC_V6_ILP32_OFFBIG */
139132
return z_sysconf(_SC_V7_ILP32_OFFBIG);
140133
case _SC_V7_LP64_OFF64:
134+
/* case _SC_V6_LP64_OFF64 */
141135
return z_sysconf(_SC_V7_LP64_OFF64);
142136
case _SC_V7_LPBIG_OFFBIG:
137+
/* case _SC_V6_LPBIG_OFFBIG */
143138
return z_sysconf(_SC_V7_LPBIG_OFFBIG);
144139
case _SC_BC_BASE_MAX:
145140
return z_sysconf(_SC_BC_BASE_MAX);
@@ -237,10 +232,9 @@ long sysconf(int x)
237232
return z_sysconf(_SC_MQ_PRIO_MAX);
238233
case _SC_OPEN_MAX:
239234
return z_sysconf(_SC_OPEN_MAX);
240-
case _SC_PAGE_SIZE:
241-
return z_sysconf(_SC_PAGE_SIZE);
242235
case _SC_PAGESIZE:
243-
return z_sysconf(_SC_PAGESIZE);
236+
/* case _SC_PAGE_SIZE */
237+
return z_sysconf(_SC_PAGE_SIZE);
244238
case _SC_THREAD_DESTRUCTOR_ITERATIONS:
245239
return z_sysconf(_SC_THREAD_DESTRUCTOR_ITERATIONS);
246240
case _SC_THREAD_KEYS_MAX:

0 commit comments

Comments
 (0)