summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxsignal08/main.c
blob: c55a7712b9b761cea506def8f92159bdf0d9bc5b (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
/*
 * @brief psxsignal08 - Ensure initial signal mask and Classic Task inheritance
 */

/*
 * COPYRIGHT (c) 2019.
 * On-Line Applications Research Corporation (OAR).
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tmacros.h>
#include "test_support.h"
#include <errno.h>
#include <signal.h>
#include <unistd.h>

#include <rtems.h>

const char rtems_test_name[] = "PSXSIGNAL 8";

static sigset_t main_sigmask;

#define SIGSET_NBYTES (sizeof(sigset_t))

/* #define DEBUG_TEST */

static void fetch_sigmask(const char *name, sigset_t *sigset_p)
{
  int   rc;

  rc = sigemptyset(sigset_p);
  rtems_test_assert(rc == 0);

  rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
  rtems_test_assert(rc == 0);

#ifdef DEBUG_TEST
  /*
   * There is no assurance that sigset_t is a primitive type so
   * we have to print it a long at a time.
   */
  int            i;
  unsigned char *p = (unsigned char *) sigset_p;

  printf("%s signal mask (in hex):\n    ", name);
  for (i=0 ; i < SIGSET_NBYTES ; i++) {
    printf("%02x%s", *p++, (i % 16 == 15) ? "\n    " : " ");
  }
  printf("\n");
#endif
}

static void block_sigmask(int signo, sigset_t *sigset_p)
{
  sigset_t sigset;
  int      rc;

  /*
   * Block the requested signal
   */
  rc = sigemptyset(&sigset);
  rtems_test_assert(rc == 0);
  rc = sigaddset(&sigset, signo);
  rtems_test_assert(rc == 0);

  rc = sigprocmask(SIG_BLOCK, &sigset, NULL);
  rtems_test_assert(rc == 0);

  /*
   * Fetch the current signal mask reflecting the requested update
   */
  sigemptyset(sigset_p);
  rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
  rtems_test_assert(rc == 0);
}

static rtems_task task_body(rtems_task_argument arg)
{
  sigset_t    mask;

  /*
   * There is no assurance that sigset_t is a primitive type so
   * we have to use memcmp().
   */
  fetch_sigmask("main", &main_sigmask);
  fetch_sigmask((void *)arg, &mask);
  rtems_test_assert(main_sigmask == mask);
  
  (void) rtems_task_delete(RTEMS_SELF);
}

int main(int argc, char **argv)
{
  sigset_t          empty;
  rtems_status_code status;
  rtems_id          id;

  TEST_BEGIN();

  /*
   * Verify first task has empty signal mask
   */
  fetch_sigmask("main", &main_sigmask);

  sigemptyset(&empty);
  fetch_sigmask("empty", &empty);

  puts("Ensure main task's mask equals sigemptyset");
  rtems_test_assert(main_sigmask == empty);

  /*
   * Create a task and see if it inherits empty signal mask.
   */
  puts("Ensure parent's empty mask is inherited by thread");
  status = rtems_task_create(
    rtems_build_name( 'T', 'A', '1', ' ' ),
    1,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &id
  );
  directive_failed( status, "rtems_task_create" );

  status = rtems_task_start( id, task_body, 1 );
  directive_failed( status, "rtems_task_start" );

  sleep(1);

  /*
   * Create a task and see if it inherits non-empty signal mask.
   */
  puts("Ensure parent's mask with SIGUSR1 mask is inherited by thread");
  block_sigmask(SIGUSR1, &main_sigmask);

  status = rtems_task_create(
    rtems_build_name( 'T', 'A', '1', ' ' ),
    1,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &id
  );
  directive_failed( status, "rtems_task_create" );

  status = rtems_task_start( id, task_body, 1 );
  directive_failed( status, "rtems_task_start" );

  sleep(1);

  
  TEST_END();

  rtems_test_exit(0);
  return 0;
}