From 7937627b65e0257833af9ab76338d98bd8f547c3 Mon Sep 17 00:00:00 2001 From: wangchengdong Date: Tue, 23 Dec 2025 11:46:28 +0800 Subject: [PATCH] sched/signal: Add support to disable partial signals Fix dependency issue when signals are only partially enabled Co-authored-by: guoshichao Signed-off-by: Chengdong Wang --- examples/buttons/buttons_main.c | 2 ++ examples/chrono/chrono_main.c | 2 ++ examples/timer/Kconfig | 2 +- netutils/dhcpc/dhcpc.c | 16 ++++++++++++++ netutils/ping/icmp_ping.c | 4 ++++ netutils/telnetd/telnetd_daemon.c | 6 +++--- nshlib/nsh_builtin.c | 23 ++++++++++++++------ nshlib/nsh_proccmds.c | 7 ++++++ system/cu/cu_main.c | 36 +++++++++++++++++-------------- testing/ostest/ostest_main.c | 10 +++++++-- 10 files changed, 80 insertions(+), 28 deletions(-) diff --git a/examples/buttons/buttons_main.c b/examples/buttons/buttons_main.c index d4b36431c95..1f5314b1dbd 100644 --- a/examples/buttons/buttons_main.c +++ b/examples/buttons/buttons_main.c @@ -219,9 +219,11 @@ static int button_daemon(int argc, char *argv[]) goto errout_with_fd; } +#ifdef CONFIG_ENABLE_ALL_SIGNALS /* Ignore the default signal action */ signal(CONFIG_EXAMPLES_BUTTONS_SIGNO, SIG_IGN); +#endif #endif /* Now loop forever, waiting BUTTONs events */ diff --git a/examples/chrono/chrono_main.c b/examples/chrono/chrono_main.c index ee6658591d1..9ce5dbb5831 100644 --- a/examples/chrono/chrono_main.c +++ b/examples/chrono/chrono_main.c @@ -165,9 +165,11 @@ static int chrono_daemon(int argc, char *argv[]) goto errout_with_fd; } +#ifdef CONFIG_ENABLE_ALL_SIGNALS /* Ignore the default signal action */ signal(BUTTON_SIGNO, SIG_IGN); +#endif /* Now loop forever, waiting BUTTONs events */ diff --git a/examples/timer/Kconfig b/examples/timer/Kconfig index 3a997ccab24..bb676737e2b 100644 --- a/examples/timer/Kconfig +++ b/examples/timer/Kconfig @@ -6,7 +6,7 @@ config EXAMPLES_TIMER tristate "Timer example" default n - depends on TIMER + depends on TIMER && ENABLE_ALL_SIGNALS ---help--- Enable the timer example diff --git a/netutils/dhcpc/dhcpc.c b/netutils/dhcpc/dhcpc.c index dc6b46230b3..e343bfb19f0 100644 --- a/netutils/dhcpc/dhcpc.c +++ b/netutils/dhcpc/dhcpc.c @@ -533,8 +533,16 @@ static void *dhcpc_run(void *args) struct dhcpc_state result; int ret; +#ifdef CONFIG_DISABLE_ALL_SIGNALS + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); +#endif + while (1) { +#ifdef CONFIG_DISABLE_ALL_SIGNALS + pthread_testcancel(); +#endif ret = dhcpc_request(pdhcpc, &result); if (ret == OK) { @@ -701,7 +709,9 @@ void dhcpc_close(FAR void *handle) void dhcpc_cancel(FAR void *handle) { struct dhcpc_state_s *pdhcpc = (struct dhcpc_state_s *)handle; +#ifdef CONFIG_ENABLE_ALL_SIGNALS sighandler_t old; +#endif int ret; if (pdhcpc) @@ -710,6 +720,9 @@ void dhcpc_cancel(FAR void *handle) if (pdhcpc->thread) { +#ifdef CONFIG_DISABLE_ALL_SIGNALS + pthread_cancel(pdhcpc->thread); +#else old = signal(SIGQUIT, SIG_IGN); /* Signal the dhcpc_run */ @@ -719,6 +732,7 @@ void dhcpc_cancel(FAR void *handle) { nerr("ERROR: pthread_kill DHCPC thread\n"); } +#endif /* Wait for the end of dhcpc_run */ @@ -729,7 +743,9 @@ void dhcpc_cancel(FAR void *handle) } pdhcpc->thread = 0; +#ifdef CONFIG_ENABLE_ALL_SIGNALS signal(SIGQUIT, old); +#endif } } } diff --git a/netutils/ping/icmp_ping.c b/netutils/ping/icmp_ping.c index efbc8a61a78..b9f7a28dc38 100644 --- a/netutils/ping/icmp_ping.c +++ b/netutils/ping/icmp_ping.c @@ -91,6 +91,7 @@ static volatile bool g_exiting; * Private Functions ****************************************************************************/ +#ifdef CONFIG_ENABLE_ALL_SIGNALS /**************************************************************************** * Name: sigexit ****************************************************************************/ @@ -99,6 +100,7 @@ static void sigexit(int signo) { g_exiting = true; } +#endif /**************************************************************************** * Name: ping_newid @@ -199,7 +201,9 @@ void icmp_ping(FAR const struct ping_info_s *info) int i; g_exiting = false; +#ifdef CONFIG_ENABLE_ALL_SIGNALS signal(SIGINT, sigexit); +#endif /* Initialize result structure */ diff --git a/netutils/telnetd/telnetd_daemon.c b/netutils/telnetd/telnetd_daemon.c index ed02b156909..989de34f9da 100644 --- a/netutils/telnetd/telnetd_daemon.c +++ b/netutils/telnetd/telnetd_daemon.c @@ -74,7 +74,7 @@ int telnetd_daemon(FAR const struct telnetd_config_s *config) #endif } addr; -#ifdef CONFIG_SCHED_HAVE_PARENT +#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_ENABLE_ALL_SIGNALS) struct sigaction sa; sigset_t blockset; #endif @@ -85,7 +85,7 @@ int telnetd_daemon(FAR const struct telnetd_config_s *config) int optval; #endif -#ifdef CONFIG_SCHED_HAVE_PARENT +#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_ENABLE_ALL_SIGNALS) /* Call sigaction with the SA_NOCLDWAIT flag so that we do not transform * children into "zombies" when they terminate: Child exit status will * not be retained. @@ -113,7 +113,7 @@ int telnetd_daemon(FAR const struct telnetd_config_s *config) nerr("ERROR: sigprocmask failed: %d\n", errno); goto errout; } -#endif /* CONFIG_SCHED_HAVE_PARENT */ +#endif /* CONFIG_SCHED_HAVE_PARENT && CONFIG_ENABLE_ALL_SIGNALS */ /* Create a new TCP socket to use to listen for connections */ diff --git a/nshlib/nsh_builtin.c b/nshlib/nsh_builtin.c index 196220f41fa..76f542f8f60 100644 --- a/nshlib/nsh_builtin.c +++ b/nshlib/nsh_builtin.c @@ -74,10 +74,14 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, FAR char **argv, FAR const struct nsh_param_s *param) { -#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS) +#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS) && \ + defined(CONFIG_ENABLE_ALL_SIGNALS) struct sigaction act; struct sigaction old; -#endif +#endif /* !CONFIG_NSH_DISABLEBG && CONFIG_SCHED_CHILD_STATUS && + * CONFIG_ENABLE_ALL_SIGNALS + */ + int ret = OK; /* Lock the scheduler in an attempt to prevent the application from @@ -86,7 +90,8 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, sched_lock(); -#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS) +#if !defined(CONFIG_NSH_DISABLEBG) && \ + defined(CONFIG_SCHED_CHILD_STATUS) && defined(CONFIG_ENABLE_ALL_SIGNALS) /* Ignore the child status if run the application on background. */ if (vtbl->np.np_bg == true) @@ -98,7 +103,9 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, sigaction(SIGCHLD, &act, &old); } -#endif /* CONFIG_NSH_DISABLEBG */ +#endif /* !CONFIG_NSH_DISABLEBG && !CONFIG_SCHED_CHILD_STATUS && + * CONFIG_ENABLE_ALL_SIGNALS + */ /* Try to find and execute the command within the list of builtin * applications. @@ -230,7 +237,8 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, #if !defined(CONFIG_SCHED_WAITPID) || !defined(CONFIG_NSH_DISABLEBG) { -#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS) +#if !defined(CONFIG_NSH_DISABLEBG) && defined(CONFIG_SCHED_CHILD_STATUS) && \ + defined(CONFIG_ENABLE_ALL_SIGNALS) /* Restore the old actions */ @@ -241,7 +249,10 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, sigaction(SIGCHLD, &old, NULL); } -# endif +# endif /* !CONFIG_NSH_DISABLEBG && CONFIG_SCHED_CHILD_STATUS && \ + * CONFIG_ENABLE_ALL_SIGNALS + */ + struct sched_param sched; sched_getparam(ret, &sched); nsh_output(vtbl, "%s [%d:%d]\n", cmd, ret, sched.sched_priority); diff --git a/nshlib/nsh_proccmds.c b/nshlib/nsh_proccmds.c index fe98105648c..f71deb5b5d6 100644 --- a/nshlib/nsh_proccmds.c +++ b/nshlib/nsh_proccmds.c @@ -873,6 +873,7 @@ static int top_cmpcpuload(FAR const void *item1, FAR const void *item2) } } +#ifdef CONFIG_ENABLE_FULL_SIGNALS /**************************************************************************** * Name: top_exit ****************************************************************************/ @@ -882,6 +883,8 @@ static void top_exit(int signo, FAR siginfo_t *siginfo, FAR void *context) *(FAR bool *)siginfo->si_user = true; } +#endif /* CONFIG_ENABLE_FULL_SIGNALS */ + #endif /**************************************************************************** @@ -1357,7 +1360,9 @@ int cmd_top(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) FAR char *pidlist = NULL; size_t num = SIZE_MAX; size_t i; +#ifdef CONFIG_ENABLE_FULL_SIGNALS struct sigaction act; +#endif bool quit = false; int delay = 3; int ret = 0; @@ -1393,6 +1398,7 @@ int cmd_top(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) } } +#ifdef CONFIG_ENABLE_FULL_SIGNALS act.sa_user = &quit; act.sa_sigaction = top_exit; sigemptyset(&act.sa_mask); @@ -1402,6 +1408,7 @@ int cmd_top(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) nsh_error(vtbl, g_fmtcmdfailed, "top", "sigaction", NSH_ERRNO); return ERROR; } +#endif if (vtbl->isctty) { diff --git a/system/cu/cu_main.c b/system/cu/cu_main.c index 8d25038ed0b..75fd96afc7c 100644 --- a/system/cu/cu_main.c +++ b/system/cu/cu_main.c @@ -124,10 +124,12 @@ static FAR void *cu_listener(FAR void *parameter) return NULL; } +#ifdef CONFIG_ENABLE_ALL_SIGNALS static void sigint(int sig) { g_cu.force_exit = true; } +#endif #ifdef CONFIG_SERIAL_TERMIOS static int set_termios(FAR struct cu_globals_s *cu, int rate, @@ -275,7 +277,9 @@ static int cu_cmd(FAR struct cu_globals_s *cu, char bcmd) int main(int argc, FAR char *argv[]) { pthread_attr_t attr; +#ifdef CONFIG_ENABLE_ALL_SIGNALS struct sigaction sa; +#endif FAR const char *devname = CONFIG_SYSTEM_CUTERM_DEFAULT_DEVICE; FAR struct cu_globals_s *cu = &g_cu; #ifdef CONFIG_SERIAL_TERMIOS @@ -295,13 +299,13 @@ int main(int argc, FAR char *argv[]) memset(cu, 0, sizeof(*cu)); cu->escape = '~'; - +#ifdef CONFIG_ENABLE_ALL_SIGNALS /* Install signal handlers */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = sigint; sigaction(SIGINT, &sa, NULL); - +#endif optind = 0; /* Global that needs to be reset in FLAT mode */ while ((option = getopt(argc, argv, "l:s:ceE:fho?")) != ERROR) { @@ -474,7 +478,7 @@ int main(int argc, FAR char *argv[]) } if (start_of_line == 1 && ch == cu->escape) - { + { /* Normal character */ if (i > nwrite) @@ -484,27 +488,27 @@ int main(int argc, FAR char *argv[]) nwrite = i + 1; - /* We've seen and escape (~) character, echo it to local - * terminal and read the next char from serial - */ + /* We've seen and escape (~) character, echo it to local + * terminal and read the next char from serial + */ - write(STDOUT_FILENO, &ch, 1); + write(STDOUT_FILENO, &ch, 1); start_of_line = 0; escaping = true; continue; } - /* Determine if we are now at the start of a new line or not */ + /* Determine if we are now at the start of a new line or not */ - if (ch == '\n' || ch == '\r') - { - start_of_line = 1; - } - else - { - start_of_line = 0; - } + if (ch == '\n' || ch == '\r') + { + start_of_line = 1; + } + else + { + start_of_line = 0; } + } /* Normal character */ diff --git a/testing/ostest/ostest_main.c b/testing/ostest/ostest_main.c index 201d52e389f..4dfd9cd1686 100644 --- a/testing/ostest/ostest_main.c +++ b/testing/ostest/ostest_main.c @@ -278,7 +278,8 @@ static int user_main(int argc, char *argv[]) * verify that status is retained correctly. */ -#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS) +#if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS) && \ + defined(CONFIG_ENABLE_ALL_SIGNALS) { struct sigaction sa; int ret; @@ -511,6 +512,7 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif +#ifdef CONFIG_ENABLE_ALL_SIGNALS /* Verify that we can modify the signal mask */ printf("\nuser_main: sigprocmask test\n"); @@ -534,6 +536,8 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif +#endif /* CONFIG_DISABLE_ALL_SIGNALS */ + #ifdef CONFIG_BUILD_FLAT printf("\nuser_main: wdog test\n"); wdog_test(); @@ -541,11 +545,13 @@ static int user_main(int argc, char *argv[]) #endif #ifndef CONFIG_DISABLE_POSIX_TIMERS +#ifdef CONFIG_ENABLE_ALL_SIGNALS /* Verify posix timers (with SIGEV_SIGNAL) */ printf("\nuser_main: POSIX timer test\n"); timer_test(); check_test_memory_usage(); +#endif #ifdef CONFIG_SIG_EVTHREAD /* Verify posix timers (with SIGEV_THREAD) */ @@ -705,7 +711,7 @@ int main(int argc, FAR char **argv) stdio_test(); #ifdef SDCC - /* I am not yet certain why SDCC does not like the following initilizers. + /* I am not yet certain why SDCC does not like the following initializers. * It involves some issues with 2- vs 3-byte pointer types. */