Debian Codegraph @

gnulib-tests/test-sigprocmask.c

raw
1 /* Test of sigprocmask.
2 Copyright (C) 2011-2025 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2011. */
18
19 #include <config.h>
20
21 #include <signal.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (sigprocmask, int, (int, const sigset_t *, sigset_t *));
25
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #include "virtualbox.h"
33 #include "macros.h"
34
35 #if !(defined _WIN32 && !defined __CYGWIN__)
36
37 static volatile int sigint_occurred;
38
39 static void
40 sigint_handler (_GL_UNUSED int sig)
41 {
42 sigint_occurred++;
43 }
44
45 int
46 main ()
47 {
48 /* This test occasionally fails on Linux (glibc or musl libc), in a
49 VirtualBox VM with paravirtualization = Default or KVM, with ≥ 2 CPUs.
50 Skip the test in this situation. */
51 if (is_running_under_virtualbox_kvm () && num_cpus () > 1)
52 {
53 fputs ("Skipping test: avoiding VirtualBox bug with KVM paravirtualization\n",
54 stderr);
55 return 77;
56 }
57
58 sigset_t set;
59 intmax_t pid = getpid ();
60 char command[80];
61
62 signal (SIGINT, sigint_handler);
63
64 sigemptyset (&set);
65 sigaddset (&set, SIGINT);
66
67 /* Check error handling. */
68 ASSERT (sigprocmask (1729, &set, NULL) == -1);
69 ASSERT (errno == EINVAL);
70
71 /* Block SIGINT. */
72 ASSERT (sigprocmask (SIG_BLOCK, &set, NULL) == 0);
73
74 /* Request a SIGINT signal from outside. */
75 sprintf (command, "sh -c 'sleep 1; kill -INT %"PRIdMAX"' &", pid);
76 ASSERT (system (command) == 0);
77
78 /* Wait. */
79 sleep (2);
80
81 /* The signal should not have arrived yet, because it is blocked. */
82 ASSERT (sigint_occurred == 0);
83
84 /* Unblock SIGINT. */
85 ASSERT (sigprocmask (SIG_UNBLOCK, &set, NULL) == 0);
86
87 /* The signal should have arrived now, because POSIX says
88 "If there are any pending unblocked signals after the call to
89 sigprocmask(), at least one of those signals shall be delivered
90 before the call to sigprocmask() returns." */
91 ASSERT (sigint_occurred == 1);
92
93 return test_exit_status;
94 }
95
96 #else
97
98 /* On native Windows, getpid() values and the arguments that are passed to
99 the (Cygwin?) 'kill' program are not necessarily related. */
100
101 int
102 main ()
103 {
104 fputs ("Skipping test: native Windows platform\n", stderr);
105 return 77;
106 }
107
108 #endif