Skip to content

Commit 2feea75

Browse files
cfriedtjhedberg
authored andcommitted
posix: headers: create a more conformant posix_signal.h
Create a header for satisfying POSIX conformance requirements of signal.h that is named posix_signal.h rather than signal.h. The primary reason for doing so, is that the de-facto owner of signal.h is the C library. This new header only defines required POSIX symbols that form a strict superset over the ISO C signal.h symbols. V2 Signed-off-by: Chris Friedt <[email protected]>
1 parent 5db2b58 commit 2feea75

File tree

1 file changed

+398
-0
lines changed

1 file changed

+398
-0
lines changed
Lines changed: 398 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,398 @@
1+
/*
2+
* Copyright (c) 2025 The Zephyr Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef ZEPHYR_INCLUDE_POSIX_POSIX_SIGNAL_H_
7+
#define ZEPHYR_INCLUDE_POSIX_POSIX_SIGNAL_H_
8+
9+
#if defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__)
10+
11+
#include <zephyr/toolchain.h>
12+
#include <zephyr/sys/util.h>
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
/* SIG_DFL must be defined by the libc signal.h */
19+
/* SIG_ERR must be defined by the libc signal.h */
20+
21+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
22+
#define SIG_HOLD ((void *)-2)
23+
#endif
24+
25+
/* SIG_IGN must be defined by the libc signal.h */
26+
27+
#if defined(_POSIX_THREADS) || defined(__DOXYGEN__)
28+
29+
#if !defined(_PTHREAD_T_DECLARED) && !defined(__pthread_t_defined)
30+
typedef unsigned int pthread_t;
31+
#define _PTHREAD_T_DECLARED
32+
#define __pthread_t_defined
33+
#endif
34+
35+
#endif /* defined(_POSIX_THREADS) || defined(__DOXYGEN__) */
36+
37+
/* size_t must be defined by the libc stddef.h */
38+
#include <stddef.h>
39+
40+
#if !defined(_UID_T_DECLARED) && !defined(__uid_t_defined)
41+
typedef int uid_t;
42+
#define _UID_T_DECLARED
43+
#define __uid_t_defined
44+
#endif
45+
46+
#if !defined(_TIME_T_DECLARED) && !defined(__time_t_defined)
47+
typedef long time_t;
48+
#define _TIME_T_DECLARED
49+
#define __time_t_defined
50+
#endif
51+
52+
#if !defined(_TIMESPEC_DECLARED) && !defined(__timespec_defined)
53+
struct timespec {
54+
time_t tv_sec;
55+
long tv_nsec;
56+
};
57+
#define _TIMESPEC_DECLARED
58+
#define __timespec_defined
59+
#endif
60+
61+
/* sig_atomic_t must be defined by the libc signal.h */
62+
63+
#define SIGRTMIN 32
64+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
65+
BUILD_ASSERT(CONFIG_POSIX_RTSIG_MAX >= 0);
66+
#define SIGRTMAX (SIGRTMIN + CONFIG_POSIX_RTSIG_MAX)
67+
#else
68+
#define SIGRTMAX SIGRTMIN
69+
#endif
70+
71+
#if !defined(_SIGSET_T_DECLARED) && !defined(__sigset_t_defined)
72+
typedef struct {
73+
unsigned long sig[DIV_ROUND_UP(SIGRTMAX + 1, BITS_PER_LONG)];
74+
} sigset_t;
75+
#define _SIGSET_T_DECLARED
76+
#define __sigset_t_defined
77+
#endif
78+
79+
#if !defined(_PID_T_DECLARED) && !defined(__pid_t_defined)
80+
typedef long pid_t;
81+
#define _PID_T_DECLARED
82+
#define __pid_t_defined
83+
#endif
84+
85+
#if defined(_POSIX_THREADS) || defined(__DOXYGEN__)
86+
87+
#if !defined(_PTHREAD_ATTR_T_DECLARED) && !defined(__pthread_attr_t_defined)
88+
typedef struct {
89+
void *stack;
90+
unsigned int details[2];
91+
} pthread_attr_t;
92+
#define _PTHREAD_ATTR_T_DECLARED
93+
#define __pthread_attr_t_defined
94+
#endif
95+
96+
#endif
97+
98+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
99+
100+
union sigval; /* forward declaration (to preserve spec order) */
101+
102+
#if !defined(_SIGEVENT_DECLARED) && !defined(__sigevent_defined)
103+
typedef struct {
104+
#if defined(_POSIX_THREADS) || defined(__DOXYGEN__)
105+
pthread_attr_t *sigev_thread_attr;
106+
#endif
107+
union sigval sigev_value;
108+
int sigev_notify;
109+
int sigev_signo;
110+
} sigevent_t;
111+
#define _SIGEVENT_DECLARED
112+
#define __sigevent_defined
113+
#endif
114+
115+
#define SIGEV_NONE 1
116+
#define SIGEV_SIGNAL 2
117+
#define SIGEV_THREAD 3
118+
119+
/* Signal constants are defined below */
120+
121+
#endif /* defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__) */
122+
123+
#if !defined(_SIGVAL_DECLARED) && !defined(__sigval_defined)
124+
union sigval {
125+
int sival_int;
126+
void *sival_ptr;
127+
};
128+
#define _SIGVAL_DECLARED
129+
#define __sigval_defined
130+
#endif
131+
132+
/* SIGRTMIN and SIGRTMAX defined above */
133+
134+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
135+
136+
#if !defined(_SIGACTION_DECLARED) && !defined(__sigaction_defined)
137+
struct sigaction {
138+
union {
139+
void (*sa_handler)(int sig);
140+
void (*sa_sigaction)(int sig, siginfo_t *info, void *context);
141+
};
142+
sigset_t sa_mask;
143+
int sa_flags;
144+
};
145+
#define _SIGACTION_DECLARED
146+
#define __sigaction_defined
147+
#endif
148+
149+
#define SIG_BLOCK 1
150+
#define SIG_UNBLOCK 2
151+
#define SIG_SETMASK 0
152+
153+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
154+
#define SA_NOCLDSTOP 0x00000001
155+
#define SA_ONSTACK 0x00000002
156+
#endif
157+
#define SA_RESETHAND 0x00000004
158+
#define SA_RESTART 0x00000008
159+
#define SA_SIGINFO 0x00000010
160+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
161+
#define SA_NOCLDWAIT 0x00000020
162+
#endif
163+
#define SA_NODEFER 0x00000040
164+
#define SS_ONSTACK 0x00000001
165+
#define SS_DISABLE 0x00000002
166+
#define MINSIGSTKSZ 4096
167+
#define SIGSTKSZ 4096
168+
169+
#if !defined(_MCONTEXT_T_DECLARED) && !defined(__mcontext_t_defined)
170+
typedef struct {
171+
/* FIXME: there should be a much better Zephyr-specific structure that can be used here */
172+
unsigned long gregs[32];
173+
unsigned long flags;
174+
} mcontext_t;
175+
#define _MCONTEXT_T_DECLARED
176+
#define __mcontext_defined
177+
#endif
178+
179+
#if !defined(_UCONTEXT_T_DECLARED) && !defined(__ucontext_t_defined)
180+
typedef struct {
181+
struct ucontext *uc_link;
182+
sigset_t uc_sigmask;
183+
stack_t uc_stack;
184+
mcontext_t uc_mcontext;
185+
} ucontext_t;
186+
#define _UCONTEXT_T_DECLARED
187+
#define __ucontext_defined
188+
#endif
189+
190+
#if !defined(_STACK_T_DECLARED) && !defined(__stack_t_defined)
191+
typedef struct {
192+
void *ss_sp;
193+
size_t ss_size;
194+
int ss_flags;
195+
} stack_t;
196+
#define _STACK_T_DECLARED
197+
#define __stack_t_defined
198+
#endif
199+
200+
#endif /* defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__) */
201+
202+
#if !defined(_SIGINFO_T_DECLARED) && !defined(__siginfo_t_defined)
203+
typedef struct {
204+
void *si_addr;
205+
#if defined(_XOPEN_STREAMS) || defined(__DOXYGEN__)
206+
long si_band;
207+
#endif
208+
union sigval si_value;
209+
pid_t si_pid;
210+
uid_t si_uid;
211+
int si_signo;
212+
int si_code;
213+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
214+
int si_errno;
215+
#endif
216+
int si_status;
217+
} siginfo_t;
218+
#define _SIGINFO_T_DECLARED
219+
#define __siginfo_t_defined
220+
#endif
221+
222+
/* Siginfo codes are defined below */
223+
224+
#if !defined(_SIGHANDLER_T_DECLARED) && !defined(__sighandler_t_defined)
225+
typedef void (*sighandler_t)(int sig);
226+
#define _SIGHANDLER_T_DECLARED
227+
#define __sighandler_t_defined
228+
#endif
229+
230+
int kill(pid_t pid, int sig);
231+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
232+
int killpg(pid_t pgrp, int sig);
233+
#endif
234+
void psiginfo(const siginfo_t *info, const char *message);
235+
void psignal(int sig, const char *message);
236+
#if defined(_POSIX_THREADS) || defined(__DOXYGEN__)
237+
int pthread_kill(pthread_t thread, int sig);
238+
int pthread_sigmask(int how, const sigset_t *ZRESTRICT set, sigset_t *ZRESTRICT oset);
239+
#endif
240+
/* raise() must be defined by the libc signal.h */
241+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
242+
TOOLCHAIN_DISABLE_WARNING(TOOLCHAIN_WARNING_SHADOW);
243+
int sigaction(int sig, const struct sigaction *ZRESTRICT act, struct sigaction *ZRESTRICT oact);
244+
TOOLCHAIN_ENABLE_WARNING(TOOLCHAIN_WARNING_SHADOW);
245+
#endif
246+
int sigaddset(sigset_t *set, int sig);
247+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
248+
int sigaltstack(const stack_t *ZRESTRICT ss, stack_t *ZRESTRICT oss);
249+
#endif
250+
int sigdelset(sigset_t *set, int sig);
251+
int sigemptyset(sigset_t *set);
252+
int sigfillset(sigset_t *set);
253+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
254+
int sighold(int sig);
255+
int sigignore(int sig);
256+
int siginterrupt(int sig, int flag);
257+
#endif
258+
int sigismember(const sigset_t *set, int sig);
259+
/* signal() must be defined by the libc signal.h */
260+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
261+
int sigpause(int sig);
262+
#endif
263+
int sigpending(sigset_t *set);
264+
int sigprocmask(int how, const sigset_t *ZRESTRICT set, sigset_t *ZRESTRICT oset);
265+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
266+
int sigqueue(pid_t pid, int sig, union sigval value);
267+
#endif
268+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
269+
int sigrelse(int sig);
270+
sighandler_t sigset(int sig, sighandler_t disp);
271+
#endif
272+
int sigsuspend(const sigset_t *set);
273+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
274+
int sigtimedwait(const sigset_t *ZRESTRICT set, siginfo_t *ZRESTRICT info,
275+
const struct timespec *ZRESTRICT timeout);
276+
#endif
277+
int sigwait(const sigset_t *ZRESTRICT set, int *ZRESTRICT sig);
278+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
279+
int sigwaitinfo(const sigset_t *ZRESTRICT set, siginfo_t *ZRESTRICT info);
280+
#endif
281+
282+
/* Note: only ANSI / ISO C signals are guarded below */
283+
284+
#define SIGHUP 1 /**< Hangup */
285+
#if !defined(SIGINT) || defined(__DOXYGEN__)
286+
#define SIGINT 2 /**< Interrupt */
287+
#endif
288+
#define SIGQUIT 3 /**< Quit */
289+
#if !defined(SIGILL) || defined(__DOXYGEN__)
290+
#define SIGILL 4 /**< Illegal instruction */
291+
#endif
292+
#define SIGTRAP 5 /**< Trace/breakpoint trap */
293+
#if !defined(SIGABRT) || defined(__DOXYGEN__)
294+
#define SIGABRT 6 /**< Aborted */
295+
#endif
296+
#define SIGBUS 7 /**< Bus error */
297+
#if !defined(SIGFPE) || defined(__DOXYGEN__)
298+
#define SIGFPE 8 /**< Arithmetic exception */
299+
#endif
300+
#define SIGKILL 9 /**< Killed */
301+
#define SIGUSR1 10 /**< User-defined signal 1 */
302+
#if !defined(SIGSEGV) || defined(__DOXYGEN__)
303+
#define SIGSEGV 11 /**< Invalid memory reference */
304+
#endif
305+
#define SIGUSR2 12 /**< User-defined signal 2 */
306+
#define SIGPIPE 13 /**< Broken pipe */
307+
#define SIGALRM 14 /**< Alarm clock */
308+
#if !defined(SIGTERM) || defined(__DOXYGEN__)
309+
#define SIGTERM 15 /**< Terminated */
310+
#endif
311+
/* 16 not used */
312+
#define SIGCHLD 17 /**< Child status changed */
313+
#define SIGCONT 18 /**< Continued */
314+
#define SIGSTOP 19 /**< Stop executing */
315+
#define SIGTSTP 20 /**< Stopped */
316+
#define SIGTTIN 21 /**< Stopped (read) */
317+
#define SIGTTOU 22 /**< Stopped (write) */
318+
#define SIGURG 23 /**< Urgent I/O condition */
319+
#define SIGXCPU 24 /**< CPU time limit exceeded */
320+
#define SIGXFSZ 25 /**< File size limit exceeded */
321+
#define SIGVTALRM 26 /**< Virtual timer expired */
322+
#define SIGPROF 27 /**< Profiling timer expired */
323+
/* 28 not used */
324+
#define SIGPOLL 29 /**< Pollable event occurred */
325+
/* 30 not used */
326+
#define SIGSYS 31 /**< Bad system call */
327+
328+
#if defined(_POSIX_REALTIME_SIGNALS) || defined(__DOXYGEN__)
329+
330+
/* SIGILL */
331+
#define ILL_ILLOPC 1 /**< Illegal opcode */
332+
#define ILL_ILLOPN 2 /**< Illegal operand */
333+
#define ILL_ILLADR 3 /**< Illegal addressing mode */
334+
#define ILL_ILLTRP 4 /**< Illegal trap */
335+
#define ILL_PRVOPC 5 /**< Privileged opcode */
336+
#define ILL_PRVREG 6 /**< Privileged register */
337+
#define ILL_COPROC 7 /**< Coprocessor error */
338+
#define ILL_BADSTK 8 /**< Internal stack error */
339+
340+
/* SIGFPE */
341+
#define FPE_INTDIV 9 /**< Integer divide by zero */
342+
#define FPE_INTOVF 10 /**< Integer overflow */
343+
#define FPE_FLTDIV 11 /**< Floating-point divide by zero */
344+
#define FPE_FLTOVF 12 /**< Floating-point overflow */
345+
#define FPE_FLTUND 13 /**< Floating-point underflow */
346+
#define FPE_FLTRES 15 /**< Floating-point inexact result */
347+
#define FPE_FLTINV 16 /**< Invalid floating-point operation */
348+
#define FPE_FLTSUB 17 /**< Subscript out of range */
349+
350+
/* SIGSEGV */
351+
#define SEGV_MAPERR 18 /**< Address not mapped to object */
352+
#define SEGV_ACCERR 19 /**< Invalid permissions for mapped object */
353+
354+
/* SIGBUS */
355+
#define BUS_ADRALN 20 /**< Invalid address alignment */
356+
#define BUS_ADRERR 21 /**< Nonexistent physical address */
357+
#define BUS_OBJERR 22 /**< Object-specific hardware error */
358+
359+
#if defined(_XOPEN_SOURCE) || defined(__DOXYGEN__)
360+
/* SIGTRAP */
361+
#define TRAP_BRKPT 23 /**< Process breakpoint */
362+
#define TRAP_TRACE 24 /**< Process trace trap */
363+
#endif
364+
365+
/* SIGCHLD */
366+
#define CLD_EXITED 25 /**< Child has exited */
367+
#define CLD_KILLED 26 /**< Child has terminated abnormally and did not create a core file */
368+
#define CLD_DUMPED 27 /**< Child has terminated abnormally and created a core file */
369+
#define CLD_TRAPPED 28 /**< Traced child has trapped */
370+
#define CLD_STOPPED 29 /**< Child has stopped */
371+
#define CLD_CONTINUED 30 /**< Stopped child has continued */
372+
373+
#if defined(_XOPEN_STREAMS) || defined(__DOXYGEN__)
374+
/* SIGPOLL */
375+
#define POLL_IN 31 /**< Data input available */
376+
#define POLL_OUT 32 /**< Output buffers available */
377+
#define POLL_MSG 33 /**< Input message available */
378+
#define POLL_ERR 34 /**< I/O error */
379+
#define POLL_PRI 35 /**< High priority input available */
380+
#define POLL_HUP 36 /**< Device disconnected */
381+
#endif
382+
383+
/* Any */
384+
#define SI_USER 37 /**< Signal sent by kill() */
385+
#define SI_QUEUE 38 /**< Signal sent by sigqueue() */
386+
#define SI_TIMER 39 /**< Signal generated by expiration of a timer set by timer_settime() */
387+
#define SI_ASYNCIO 40 /**< Signal generated by completion of an asynchronous I/O request */
388+
#define SI_MESGQ 41 /**< Signal generated by arrival of a message on an empty message queue */
389+
390+
#endif
391+
392+
#ifdef __cplusplus
393+
}
394+
#endif
395+
396+
#endif /* defined(_POSIX_C_SOURCE) || defined(__DOXYGEN__) */
397+
398+
#endif /* ZEPHYR_INCLUDE_POSIX_POSIX_SIGNAL_H_ */

0 commit comments

Comments
 (0)