[PATCH v2 3/4] Cygwin: testsuite: test signal mask and ignore options.
Jeremy Drake
cygwin@jdrake.com
Fri Jun 20 18:03:25 GMT 2025
Test both that SIG_IGN and sigprocmask are inherited by default, and
that posix_spawnattr options can prevent this.
Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
---
winsup/testsuite/Makefile.am | 1 +
.../winsup.api/posix_spawn/signals.c | 82 +++++++++++++++++++
.../testsuite/winsup.api/posix_spawn/test.h | 10 +++
3 files changed, 93 insertions(+)
create mode 100644 winsup/testsuite/winsup.api/posix_spawn/signals.c
diff --git a/winsup/testsuite/Makefile.am b/winsup/testsuite/Makefile.am
index 1cda72905e..b92532e4fe 100644
--- a/winsup/testsuite/Makefile.am
+++ b/winsup/testsuite/Makefile.am
@@ -312,6 +312,7 @@ check_PROGRAMS = \
winsup.api/pthread/threadidafterfork \
winsup.api/pthread/tsd1 \
winsup.api/posix_spawn/errors \
+ winsup.api/posix_spawn/signals \
winsup.api/posix_spawn/spawnp \
winsup.api/samples/sample-fail \
winsup.api/samples/sample-pass
diff --git a/winsup/testsuite/winsup.api/posix_spawn/signals.c b/winsup/testsuite/winsup.api/posix_spawn/signals.c
new file mode 100644
index 0000000000..f64404ddcb
--- /dev/null
+++ b/winsup/testsuite/winsup.api/posix_spawn/signals.c
@@ -0,0 +1,82 @@
+#include "test.h"
+#include <signal.h>
+#include <spawn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int handle_child (char *arg)
+{
+ struct sigaction sa;
+ sigset_t mask;
+ int ret;
+
+ negError (sigaction (SIGUSR1, NULL, &sa));
+ negError (sigprocmask (SIG_SETMASK, NULL, &mask));
+ negError (ret = sigismember (&mask, SIGUSR2));
+
+ if (!strcmp (arg, "inherited"))
+ {
+ testAssert (sa.sa_handler == SIG_IGN);
+ testAssertMsg (ret, "SIGUSR2 not masked");
+ }
+ else
+ {
+ testAssert (sa.sa_handler == SIG_DFL);
+ testAssertMsg (!ret, "SIGUSR2 masked");
+ }
+
+ return 0;
+}
+
+int main (int argc, char **argv)
+{
+ posix_spawnattr_t sa;
+ pid_t pid;
+ int status;
+ sigset_t sigusr1mask, sigusr2mask, emptymask;
+ char *childargv[] = {"signal", "--child", "inherited", NULL};
+
+ /* unbuffer stdout */
+ setvbuf(stdout, NULL, _IONBF, 0);
+
+ if (argc == 3 && !strcmp (argv[1], "--child"))
+ return handle_child (argv[2]);
+
+ negError (sigemptyset (&sigusr1mask));
+ negError (sigaddset (&sigusr1mask, SIGUSR1));
+ negError (sigemptyset (&sigusr2mask));
+ negError (sigaddset (&sigusr2mask, SIGUSR2));
+ negError (sigemptyset (&emptymask));
+
+ /* set all signals to default */
+ for (int i = 1; i < NSIG; ++i)
+ if (i != SIGKILL && i != SIGSTOP)
+ signal (i, SIG_DFL);
+
+ /* change some signal states to test signal-related posix_spawn flags */
+ sigError (signal (SIGUSR1, SIG_IGN));
+ negError (sigprocmask (SIG_SETMASK, &sigusr2mask, NULL));
+
+ /* ensure ignored and blocked signals are inherited by default */
+ errCode (posix_spawn (&pid, MYSELF, NULL, NULL, childargv, environ));
+ negError (waitpid (pid, &status, 0));
+ exitStatus (status, 0);
+
+ errCode (posix_spawnattr_init (&sa));
+ errCode (posix_spawnattr_setsigmask (&sa, &emptymask));
+ errCode (posix_spawnattr_setsigdefault (&sa, &sigusr1mask));
+ errCode (posix_spawnattr_setflags (&sa,
+ POSIX_SPAWN_SETSIGDEF|POSIX_SPAWN_SETSIGMASK));
+
+ /* ensure setsigmask and setsigdefault work */
+ childargv[2] = "spawnattr";
+ errCode (posix_spawn (&pid, MYSELF, NULL, &sa, childargv, environ));
+ negError (waitpid (pid, &status, 0));
+ exitStatus (status, 0);
+
+ errCode (posix_spawnattr_destroy (&sa));
+
+ return 0;
+}
diff --git a/winsup/testsuite/winsup.api/posix_spawn/test.h b/winsup/testsuite/winsup.api/posix_spawn/test.h
index e10b6ce5e3..531d11c4dc 100644
--- a/winsup/testsuite/winsup.api/posix_spawn/test.h
+++ b/winsup/testsuite/winsup.api/posix_spawn/test.h
@@ -34,5 +34,15 @@
"child exited with code %d", WEXITSTATUS ((status))); \
} while (0)
+/* first vararg to testAssertMsg must be string msg */
+#define testAssertMsg(cond, ...) do { \
+ if (!(cond)) \
+ error_at_line (1, 0, __FILE__, __LINE__, __VA_ARGS__); \
+} while (0);
+
+#define testAssert(cond) testAssertMsg(cond, "%s", #cond)
+
+#define MYSELF "/proc/self/exe"
+
#endif /* _POSIX_SPAWN_TEST_H_ */
--
2.49.0.windows.1
More information about the Cygwin-patches
mailing list