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