1 /*
2 * Copyright (c) 2023 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "posix/strsignal_table.h"
7
8 #include <errno.h>
9 #include <stdio.h>
10
11 #include <zephyr/posix/signal.h>
12
13 #define SIGNO_WORD_IDX(_signo) (signo / BITS_PER_LONG)
14 #define SIGNO_WORD_BIT(_signo) (signo & BIT_MASK(LOG2(BITS_PER_LONG)))
15
16 BUILD_ASSERT(CONFIG_POSIX_LIMITS_RTSIG_MAX >= 0);
17 BUILD_ASSERT(CONFIG_POSIX_RTSIG_MAX >= CONFIG_POSIX_LIMITS_RTSIG_MAX);
18
signo_valid(int signo)19 static inline bool signo_valid(int signo)
20 {
21 return ((signo > 0) && (signo < _NSIG));
22 }
23
signo_is_rt(int signo)24 static inline bool signo_is_rt(int signo)
25 {
26 return ((signo >= SIGRTMIN) && (signo <= SIGRTMAX));
27 }
28
sigemptyset(sigset_t * set)29 int sigemptyset(sigset_t *set)
30 {
31 *set = (sigset_t){0};
32 return 0;
33 }
34
sigfillset(sigset_t * set)35 int sigfillset(sigset_t *set)
36 {
37 for (int i = 0; i < ARRAY_SIZE(set->sig); i++) {
38 set->sig[i] = -1;
39 }
40
41 return 0;
42 }
43
sigaddset(sigset_t * set,int signo)44 int sigaddset(sigset_t *set, int signo)
45 {
46 if (!signo_valid(signo)) {
47 errno = EINVAL;
48 return -1;
49 }
50
51 WRITE_BIT(set->sig[SIGNO_WORD_IDX(signo)], SIGNO_WORD_BIT(signo), 1);
52
53 return 0;
54 }
55
sigdelset(sigset_t * set,int signo)56 int sigdelset(sigset_t *set, int signo)
57 {
58 if (!signo_valid(signo)) {
59 errno = EINVAL;
60 return -1;
61 }
62
63 WRITE_BIT(set->sig[SIGNO_WORD_IDX(signo)], SIGNO_WORD_BIT(signo), 0);
64
65 return 0;
66 }
67
sigismember(const sigset_t * set,int signo)68 int sigismember(const sigset_t *set, int signo)
69 {
70 if (!signo_valid(signo)) {
71 errno = EINVAL;
72 return -1;
73 }
74
75 return 1 & (set->sig[SIGNO_WORD_IDX(signo)] >> SIGNO_WORD_BIT(signo));
76 }
77
strsignal(int signum)78 char *strsignal(int signum)
79 {
80 /* Using -INT_MAX here because compiler resolves INT_MIN to (-2147483647 - 1) */
81 static char buf[sizeof("RT signal -" STRINGIFY(INT_MAX))];
82
83 if (!signo_valid(signum)) {
84 errno = EINVAL;
85 return "Invalid signal";
86 }
87
88 if (signo_is_rt(signum)) {
89 snprintf(buf, sizeof(buf), "RT signal %d", signum - SIGRTMIN);
90 return buf;
91 }
92
93 if (IS_ENABLED(CONFIG_POSIX_SIGNAL_STRING_DESC)) {
94 if (strsignal_list[signum] != NULL) {
95 return (char *)strsignal_list[signum];
96 }
97 }
98
99 snprintf(buf, sizeof(buf), "Signal %d", signum);
100
101 return buf;
102 }
103