1 /* Tests for posix_spawn signal handling.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
24 #include <support/check.h>
25 #include <support/xsignal.h>
26 #include <support/xunistd.h>
29 /* Nonzero if the program gets called via `exec'. */
30 #define CMDLINE_OPTIONS \
31 { "restart", no_argument, &restart, 1 },
34 /* Hold the four initial argument used to respawn the process, plus the extra
35 '--direct', '--restart', the check type ('SIG_IGN' or 'SIG_DFL'), and a
37 static char *spargs
[8];
38 static int check_type_argc
;
40 /* Called on process re-execution. */
42 handle_restart (int argc
, char *argv
[])
46 if (strcmp (argv
[0], "SIG_DFL") == 0)
48 for (int i
= 1; i
< NSIG
; i
++)
51 int r
= sigaction (i
, NULL
, &sa
);
52 /* Skip internal signals (such as SIGCANCEL). */
55 TEST_VERIFY_EXIT (sa
.sa_handler
== SIG_DFL
);
59 else if (strcmp (argv
[0], "SIG_IGN") == 0)
61 for (int i
= 1; i
< NSIG
; i
++)
64 int r
= sigaction (i
, NULL
, &sa
);
65 /* Skip internal signals (such as SIGCANCEL). */
68 if (i
== SIGUSR1
|| i
== SIGUSR2
)
69 TEST_VERIFY_EXIT (sa
.sa_handler
== SIG_IGN
);
71 TEST_VERIFY_EXIT (sa
.sa_handler
== SIG_DFL
);
80 spawn_signal_test (const char *type
, const posix_spawnattr_t
*attr
)
82 spargs
[check_type_argc
] = (char*) type
;
87 TEST_COMPARE (posix_spawn (&pid
, spargs
[0], NULL
, attr
, spargs
, environ
), 0);
88 TEST_COMPARE (xwaitpid (pid
, &status
, 0), pid
);
89 TEST_VERIFY (WIFEXITED (status
));
90 TEST_VERIFY (!WIFSIGNALED (status
));
91 TEST_COMPARE (WEXITSTATUS (status
), 0);
95 dummy_sa_handler (int signal
)
100 do_test_signals (void)
103 /* Check if all signals handler are set to SIG_DFL on spawned process. */
104 spawn_signal_test ("SIG_DFL", NULL
);
108 /* Same as before, but set SIGUSR1 and SIGUSR2 to a handler different than
109 SIG_IGN or SIG_DFL. */
110 struct sigaction sa
= { 0 };
111 sa
.sa_handler
= dummy_sa_handler
;
112 xsigaction (SIGUSR1
, &sa
, NULL
);
113 xsigaction (SIGUSR2
, &sa
, NULL
);
114 spawn_signal_test ("SIG_DFL", NULL
);
118 /* Check if SIG_IGN is keep as is. */
119 struct sigaction sa
= { 0 };
120 sa
.sa_handler
= SIG_IGN
;
121 xsigaction (SIGUSR1
, &sa
, NULL
);
122 xsigaction (SIGUSR2
, &sa
, NULL
);
123 spawn_signal_test ("SIG_IGN", NULL
);
127 /* Check if SIG_IGN handlers are set to SIG_DFL. */
128 posix_spawnattr_t attr
;
129 posix_spawnattr_init (&attr
);
132 sigaddset (&mask
, SIGUSR1
);
133 sigaddset (&mask
, SIGUSR2
);
134 posix_spawnattr_setsigdefault (&attr
, &mask
);
135 posix_spawnattr_setflags (&attr
, POSIX_SPAWN_SETSIGDEF
);
136 spawn_signal_test ("SIG_DFL", &attr
);
137 posix_spawnattr_destroy (&attr
);
142 do_test (int argc
, char *argv
[])
144 /* We must have either:
146 - one or four parameters if called initially:
147 + argv[1]: path for ld.so optional
148 + argv[2]: "--library-path" optional
149 + argv[3]: the library path optional
150 + argv[4]: the application name
152 - six parameters left if called through re-execution:
153 + argv[1]: the application name
154 + argv[2]: check SIG_IGN/SIG_DFL.
156 * When built with --enable-hardcoded-path-in-tests or issued without
157 using the loader directly. */
160 handle_restart (argc
- 1, &argv
[1]);
162 TEST_VERIFY_EXIT (argc
== 2 || argc
== 5);
165 for (i
= 0; i
< argc
- 1; i
++)
166 spargs
[i
] = argv
[i
+ 1];
167 spargs
[i
++] = (char *) "--direct";
168 spargs
[i
++] = (char *) "--restart";
169 check_type_argc
= i
++;
178 #define TEST_FUNCTION_ARGV do_test
179 #include <support/test-driver.c>