Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:40

0001 /*
0002  * @brief psxsignal08 - Ensure initial signal mask and Classic Task inheritance
0003  */
0004 
0005 /*
0006  * COPYRIGHT (c) 2019.
0007  * On-Line Applications Research Corporation (OAR).
0008  *
0009  * SPDX-License-Identifier: BSD-2-Clause
0010  */
0011 
0012 
0013 #ifdef HAVE_CONFIG_H
0014 #include "config.h"
0015 #endif
0016 
0017 #include <tmacros.h>
0018 #include "test_support.h"
0019 #include <errno.h>
0020 #include <signal.h>
0021 #include <unistd.h>
0022 
0023 #include <rtems.h>
0024 
0025 const char rtems_test_name[] = "PSXSIGNAL 8";
0026 
0027 static sigset_t main_sigmask;
0028 
0029 #define SIGSET_NBYTES (sizeof(sigset_t))
0030 
0031 /* #define DEBUG_TEST */
0032 
0033 static void fetch_sigmask(const char *name, sigset_t *sigset_p)
0034 {
0035   int   rc;
0036 
0037   rc = sigemptyset(sigset_p);
0038   rtems_test_assert(rc == 0);
0039 
0040   rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
0041   rtems_test_assert(rc == 0);
0042 
0043 #ifdef DEBUG_TEST
0044   /*
0045    * There is no assurance that sigset_t is a primitive type so
0046    * we have to print it a long at a time.
0047    */
0048   int            i;
0049   unsigned char *p = (unsigned char *) sigset_p;
0050 
0051   printf("%s signal mask (in hex):\n    ", name);
0052   for (i=0 ; i < SIGSET_NBYTES ; i++) {
0053     printf("%02x%s", *p++, (i % 16 == 15) ? "\n    " : " ");
0054   }
0055   printf("\n");
0056 #endif
0057 }
0058 
0059 static void block_sigmask(int signo, sigset_t *sigset_p)
0060 {
0061   sigset_t sigset;
0062   int      rc;
0063 
0064   /*
0065    * Block the requested signal
0066    */
0067   rc = sigemptyset(&sigset);
0068   rtems_test_assert(rc == 0);
0069   rc = sigaddset(&sigset, signo);
0070   rtems_test_assert(rc == 0);
0071 
0072   rc = sigprocmask(SIG_BLOCK, &sigset, NULL);
0073   rtems_test_assert(rc == 0);
0074 
0075   /*
0076    * Fetch the current signal mask reflecting the requested update
0077    */
0078   sigemptyset(sigset_p);
0079   rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
0080   rtems_test_assert(rc == 0);
0081 }
0082 
0083 static rtems_task task_body(rtems_task_argument arg)
0084 {
0085   sigset_t    mask;
0086 
0087   /*
0088    * There is no assurance that sigset_t is a primitive type so
0089    * we have to use memcmp().
0090    */
0091   fetch_sigmask("main", &main_sigmask);
0092   fetch_sigmask((void *)arg, &mask);
0093   rtems_test_assert(main_sigmask == mask);
0094   
0095   (void) rtems_task_delete(RTEMS_SELF);
0096 }
0097 
0098 int main(int argc, char **argv)
0099 {
0100   sigset_t          empty;
0101   rtems_status_code status;
0102   rtems_id          id;
0103 
0104   TEST_BEGIN();
0105 
0106   /*
0107    * Verify first task has empty signal mask
0108    */
0109   fetch_sigmask("main", &main_sigmask);
0110 
0111   sigemptyset(&empty);
0112   fetch_sigmask("empty", &empty);
0113 
0114   puts("Ensure main task's mask equals sigemptyset");
0115   rtems_test_assert(main_sigmask == empty);
0116 
0117   /*
0118    * Create a task and see if it inherits empty signal mask.
0119    */
0120   puts("Ensure parent's empty mask is inherited by thread");
0121   status = rtems_task_create(
0122     rtems_build_name( 'T', 'A', '1', ' ' ),
0123     1,
0124     RTEMS_MINIMUM_STACK_SIZE,
0125     RTEMS_DEFAULT_MODES,
0126     RTEMS_DEFAULT_ATTRIBUTES,
0127     &id
0128   );
0129   directive_failed( status, "rtems_task_create" );
0130 
0131   status = rtems_task_start( id, task_body, 1 );
0132   directive_failed( status, "rtems_task_start" );
0133 
0134   sleep(1);
0135 
0136   /*
0137    * Create a task and see if it inherits non-empty signal mask.
0138    */
0139   puts("Ensure parent's mask with SIGUSR1 mask is inherited by thread");
0140   block_sigmask(SIGUSR1, &main_sigmask);
0141 
0142   status = rtems_task_create(
0143     rtems_build_name( 'T', 'A', '1', ' ' ),
0144     1,
0145     RTEMS_MINIMUM_STACK_SIZE,
0146     RTEMS_DEFAULT_MODES,
0147     RTEMS_DEFAULT_ATTRIBUTES,
0148     &id
0149   );
0150   directive_failed( status, "rtems_task_create" );
0151 
0152   status = rtems_task_start( id, task_body, 1 );
0153   directive_failed( status, "rtems_task_start" );
0154 
0155   sleep(1);
0156 
0157   
0158   TEST_END();
0159 
0160   rtems_test_exit(0);
0161   return 0;
0162 }
0163