summary refs log tree commit diff
path: root/signal/tst-sigset2.c
blob: f65332332152e6f7974879486b839325269a9459 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* sigset_SIG_HOLD_bug.c [BZ #1951] */
#include <errno.h>
#include <error.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TEST_SIG SIGINT


/* Print mask of blocked signals for this process */
static void
printSigMask (const char *msg)
{
  sigset_t currMask;
  int sig;
  int cnt;

  if (msg != NULL)
    printf ("%s", msg);

  if (sigprocmask (SIG_BLOCK, NULL, &currMask) == -1)
    error (1, errno, "sigaction");

  cnt = 0;
  for (sig = 1; sig < NSIG; sig++)
    {
      if (sigismember (&currMask, sig))
	{
	  cnt++;
	  printf ("\t\t%d (%s)\n", sig, strsignal (sig));
        }
    }

  if (cnt == 0)
    printf ("\t\t<empty signal set>\n");
} /* printSigMask */

static void
handler (int sig)
{
  printf ("Caught signal %d\n", sig);
  printSigMask ("Signal mask in handler\n");
  printf ("Handler returning\n");
  _exit (1);
} /* handler */

static void
printDisposition (sighandler_t disp)
{
  if (disp == SIG_HOLD)
    printf ("SIG_HOLD");
  else if (disp == SIG_DFL)
    printf ("SIG_DFL");
  else if (disp == SIG_IGN)
    printf ("SIG_IGN");
  else
    printf ("handled at %" PRIxPTR, (uintptr_t) disp);
} /* printDisposition */

static int
returnTest1 (void)
{
  sighandler_t prev;

  printf ("===== TEST 1 =====\n");
  printf ("Blocking signal with sighold()\n");
  if (sighold (TEST_SIG) == -1)
    error (1, errno, "sighold");
  printSigMask ("Signal mask after sighold()\n");

  printf ("About to use sigset() to establish handler\n");
  prev = sigset (TEST_SIG, handler);
  if (prev == SIG_ERR)
    error(1, errno, "sigset");

  printf ("Previous disposition: ");
  printDisposition (prev);
  printf (" (should be SIG_HOLD)\n");
  if (prev != SIG_HOLD)
    {
      printf("TEST FAILED!!!\n");
      return 1;
    }
  return 0;
} /* returnTest1 */

static int
returnTest2 (void)
{
  sighandler_t prev;

  printf ("\n===== TEST 2 =====\n");

  printf ("About to use sigset() to set SIG_HOLD\n");
  prev = sigset (TEST_SIG, SIG_HOLD);
  if (prev == SIG_ERR)
    error (1, errno, "sigset");

  printf ("Previous disposition: ");
  printDisposition (prev);
  printf (" (should be SIG_DFL)\n");
  if (prev != SIG_DFL)
    {
      printf("TEST FAILED!!!\n");
      return 1;
    }
  return 0;
} /* returnTest2 */

static int
returnTest3 (void)
{
  sighandler_t prev;

  printf ("\n===== TEST 3 =====\n");

  printf ("About to use sigset() to set SIG_HOLD\n");
  prev = sigset (TEST_SIG, SIG_HOLD);
  if (prev == SIG_ERR)
    error (1, errno, "sigset");

  printf ("About to use sigset() to set SIG_HOLD (again)\n");
  prev = sigset (TEST_SIG, SIG_HOLD);
  if (prev == SIG_ERR)
    error (1, errno, "sigset");

  printf ("Previous disposition: ");
  printDisposition (prev);
  printf (" (should be SIG_HOLD)\n");
  if (prev != SIG_HOLD)
    {
      printf("TEST FAILED!!!\n");
      return 1;
    }
  return 0;
} /* returnTest3 */

int
main (int argc, char *argv[])
{
  pid_t childPid;

  childPid = fork();
  if (childPid == -1)
    error (1, errno, "fork");

  if (childPid == 0)
    exit (returnTest1 ());

  int status;
  if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
    error (1, errno, "waitpid");
  int result = !WIFEXITED (status) || WEXITSTATUS (status) != 0;

  childPid = fork();
  if (childPid == -1)
    error (1, errno, "fork");

  if (childPid == 0)
    exit (returnTest2 ());

  if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
    error (1, errno, "waitpid");
  result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;

  childPid = fork();
  if (childPid == -1)
    error (1, errno, "fork");

  if (childPid == 0)
    exit (returnTest3 ());

  if (TEMP_FAILURE_RETRY (waitpid (childPid, &status, 0)) != childPid)
    error (1, errno, "waitpid");
  result |= !WIFEXITED (status) || WEXITSTATUS (status) != 0;

  return result;
} /* main */