Skip to content

Commit

Permalink
posix: signal: implement strsignal
Browse files Browse the repository at this point in the history
Implementation and ztest for strsignal.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin committed Jul 14, 2023
1 parent 3cbc1a1 commit 0171b20
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/services/portability/posix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ required for error and event handling.
sigpending(),
sigprocmask(),
igsuspend(),
sigwait()
sigwait(),
strsignal(),yes

.. csv-table:: POSIX_SPIN_LOCKS
:header: API, Supported
Expand Down
1 change: 1 addition & 0 deletions include/zephyr/posix/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typedef struct {
unsigned long sig[DIV_ROUND_UP(_NSIG, BITS_PER_LONG)];
} sigset_t;

char *strsignal(int signum);
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signo);
Expand Down
18 changes: 17 additions & 1 deletion lib/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

set(GEN_DIR ${ZEPHYR_BINARY_DIR}/include/generated)

zephyr_syscall_header(
${ZEPHYR_BASE}/include/zephyr/posix/time.h
)
Expand All @@ -10,6 +12,20 @@ if(CONFIG_POSIX_API)
zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/posix)
endif()

if(CONFIG_POSIX_SIGNAL)
set(STRSIGNAL_TABLE_H ${GEN_DIR}/posix/strsignal_table.h)

add_custom_command(
OUTPUT ${STRSIGNAL_TABLE_H}
COMMAND
${PYTHON_EXECUTABLE}
${ZEPHYR_BASE}/scripts/build/gen_strsignal_table.py
-i ${ZEPHYR_BASE}/include/zephyr/posix/signal.h
-o ${STRSIGNAL_TABLE_H}
DEPENDS ${ZEPHYR_BASE}/include/zephyr/posix/signal.h
)
endif()

if(CONFIG_POSIX_API OR CONFIG_PTHREAD_IPC OR CONFIG_POSIX_CLOCK OR
CONFIG_POSIX_MQUEUE OR CONFIG_POSIX_FS OR CONFIG_EVENTFD OR CONFIG_GETOPT)
# This is a temporary workaround so that Newlib declares the appropriate
Expand All @@ -29,7 +45,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK sleep.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK timer.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_FS fs.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_MQUEUE mqueue.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_SIGNAL signal.c)
zephyr_library_sources_ifdef(CONFIG_POSIX_SIGNAL signal.c ${STRSIGNAL_TABLE_H})
zephyr_library_sources_ifdef(CONFIG_POSIX_UNAME uname.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC _common.c)
zephyr_library_sources_ifdef(CONFIG_PTHREAD_IPC barrier.c)
Expand Down
7 changes: 7 additions & 0 deletions lib/posix/Kconfig.signal
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ config POSIX_SIGNAL_NSIG
help
Define the maximum number of signal.

config POSIX_SIGNAL_STRING_DESC
bool "Use full description for the strsignal API"
default y
help
Use full description for the strsignal API.
Will use 256 bytes of ROM.

endif
28 changes: 28 additions & 0 deletions lib/posix/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "posix/strsignal_table.h"

#include <errno.h>
#include <stdio.h>

#include <zephyr/posix/signal.h>

Expand Down Expand Up @@ -69,3 +72,28 @@ int sigismember(const sigset_t *set, int signo)

return 1 & (set->sig[SIGNO_WORD_IDX(signo)] >> SIGNO_WORD_BIT(signo));
}

char *strsignal(int signum)
{
static char buf[sizeof("RT signal " STRINGIFY(SIGRTMAX))];

if (!signo_valid(signum)) {
errno = EINVAL;
return "Invalid signal";
}

if (signo_is_rt(signum)) {
snprintf(buf, sizeof(buf), "RT signal %d", signum - SIGRTMIN);
return buf;
}

if (IS_ENABLED(CONFIG_POSIX_SIGNAL_STRING_DESC)) {
if (strsignal_list[signum] != NULL) {
return (char *)strsignal_list[signum];
}
}

snprintf(buf, sizeof(buf), "Signal %d", signum);

return buf;
}
92 changes: 92 additions & 0 deletions scripts/build/gen_strsignal_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3
#
# Copyright (c) 2023 Meta
#
# SPDX-License-Identifier: Apache-2.0

import argparse
import os
import re


def front_matter():
return f'''
/*
* This file is generated by {__file__}
*/
#include <zephyr/posix/signal.h>
'''


def gen_strsignal_table(input, output):
with open(input, 'r') as inf:

highest_signo = 0
symbols = []
msgs = {}

for line in inf.readlines():
# Select items of the form below (note: SIGNO is numeric)
# #define SYMBOL SIGNO /**< MSG */
pat = r'^#define[\s]+(SIG[A-Z_]*)[\s]+([1-9][0-9]*)[\s]+/\*\*<[\s]+(.*)[\s]+\*/[\s]*$'
match = re.match(pat, line)

if not match:
continue

symbol = match[1]
signo = int(match[2])
msg = match[3]

symbols.append(symbol)
msgs[symbol] = msg

highest_signo = max(int(signo), highest_signo)

try:
os.makedirs(os.path.dirname(output))
except BaseException:
# directory already present
pass

with open(output, 'w') as outf:

print(front_matter(), file=outf)

# Generate string table
print(
f'static const char *const strsignal_list[{highest_signo + 1}] = {{', file=outf)
for symbol in symbols:
print(f'\t[{symbol}] = "{msgs[symbol]}",', file=outf)

print('};', file=outf)


def parse_args():
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument(
'-i',
'--input',
dest='input',
required=True,
help='input file (e.g. include/zephyr/posix/signal.h)')
parser.add_argument(
'-o',
'--output',
dest='output',
required=True,
help='output file (e.g. build/zephyr/misc/generated/lib/posix/strsignal_table.h)')

args = parser.parse_args()

return args


def main():
args = parse_args()
gen_strsignal_table(args.input, args.output)


if __name__ == '__main__':
main()
24 changes: 24 additions & 0 deletions tests/posix/common/src/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <errno.h>
#include <signal.h>
#include <stdio.h>

#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
Expand Down Expand Up @@ -192,3 +193,26 @@ ZTEST(posix_apis, test_signal_ismember)
zassert_equal(sigismember(&set, SIGKILL), 0, "%s not expected to be member", "SIGKILL");
zassert_equal(sigismember(&set, SIGTERM), 0, "%s not expected to be member", "SIGTERM");
}

ZTEST(posix_apis, test_signal_strsignal)
{
char buf[sizeof("RT signal " STRINGIFY(SIGRTMAX))] = {0};

zassert_mem_equal(strsignal(-1), "Invalid signal", sizeof("Invalid signal"));
zassert_mem_equal(strsignal(0), "Invalid signal", sizeof("Invalid signal"));
zassert_mem_equal(strsignal(_NSIG + 1), "Invalid signal", sizeof("Invalid signal"));

zassert_mem_equal(strsignal(30), "Signal 30", sizeof("Signal 30"));
snprintf(buf, sizeof(buf), "RT signal %d", SIGRTMIN - SIGRTMIN);
zassert_mem_equal(strsignal(SIGRTMIN), buf, strlen(buf));
snprintf(buf, sizeof(buf), "RT signal %d", SIGRTMAX - SIGRTMIN);
zassert_mem_equal(strsignal(SIGRTMAX), buf, strlen(buf));

#ifdef CONFIG_POSIX_SIGNAL_STRING_DESC
zassert_mem_equal(strsignal(SIGHUP), "Hangup", sizeof("Hangup"));
zassert_mem_equal(strsignal(SIGSYS), "Bad system call", sizeof("Bad system call"));
#else
zassert_mem_equal(strsignal(SIGHUP), "Signal 1", sizeof("Signal 1"));
zassert_mem_equal(strsignal(SIGSYS), "Signal 31", sizeof("Signal 31"));
#endif
}
6 changes: 6 additions & 0 deletions tests/posix/common/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ tests:
- CONFIG_SPIN_VALIDATE=n
integration_platforms:
- mps2_an385
portability.posix.common.strsignal_no_desc:
extra_configs:
- CONFIG_POSIX_SIGNAL_STRING_DESC=n
portability.posix.common.strsignal_rtsig_gt_99:
extra_configs:
- CONFIG_POSIX_SIGNAL_NSIG=200
1 change: 1 addition & 0 deletions tests/posix/headers/src/signal_h.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ ZTEST(posix_headers, test_signal_h)
zassert_not_null(sigaddset);
zassert_not_null(sigdelset);
zassert_not_null(sigismember);
zassert_not_null(strsignal);
#endif /* CONFIG_POSIX_SIGNAL */

if (IS_ENABLED(CONFIG_POSIX_API)) {
Expand Down

0 comments on commit 0171b20

Please sign in to comment.