Back to home page

LXR

 
 

    


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

0001 /*
0002  * @brief psxsignal07 - Ensure initial signal mask and thread 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 #ifdef HAVE_CONFIG_H
0013 #include "config.h"
0014 #endif
0015 
0016 #include <tmacros.h>
0017 #include "test_support.h"
0018 #include <errno.h>
0019 #include <pthread.h>
0020 #include <signal.h>
0021 
0022 const char rtems_test_name[] = "PSXSIGNAL 7";
0023 
0024 static sigset_t main_sigmask;
0025 
0026 #define SIGSET_NBYTES (sizeof(sigset_t))
0027 
0028 /* #define DEBUG_TEST */
0029 
0030 static void fetch_sigmask(const char *name, sigset_t *sigset_p)
0031 {
0032   int   rc;
0033 
0034   rc = sigemptyset(sigset_p);
0035   rtems_test_assert(rc == 0);
0036 
0037   rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
0038   rtems_test_assert(rc == 0);
0039 
0040 #ifdef DEBUG_TEST
0041   /*
0042    * There is no assurance that sigset_t is a primitive type so
0043    * we have to print it a long at a time.
0044    */
0045   int            i;
0046   unsigned char *p = (unsigned char *) sigset_p;
0047 
0048   printf("%s signal mask (in hex):\n    ", name);
0049   for (i=0 ; i < SIGSET_NBYTES ; i++) {
0050     printf("%02x%s", *p++, (i % 16 == 15) ? "\n    " : " ");
0051   }
0052   printf("\n");
0053 #endif
0054 }
0055 
0056 static void block_sigmask(int signo, sigset_t *sigset_p)
0057 {
0058   sigset_t sigset;
0059   int      rc;
0060 
0061   /*
0062    * Block the requested signal
0063    */
0064   rc = sigemptyset(&sigset);
0065   rtems_test_assert(rc == 0);
0066   rc = sigaddset(&sigset, signo);
0067   rtems_test_assert(rc == 0);
0068 
0069   rc = sigprocmask(SIG_BLOCK, &sigset, NULL);
0070   rtems_test_assert(rc == 0);
0071 
0072   /*
0073    * Fetch the current signal mask reflecting the requested update
0074    */
0075   sigemptyset(sigset_p);
0076   rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
0077   rtems_test_assert(rc == 0);
0078 }
0079 
0080 static void *thread_body(void *arg)
0081 {
0082   sigset_t    mask;
0083 
0084   /*
0085    * There is no assurance that sigset_t is a primitive type so
0086    * we have to use memcmp().
0087    */
0088   fetch_sigmask("main", &main_sigmask);
0089   fetch_sigmask(arg, &mask);
0090   rtems_test_assert(main_sigmask == mask);
0091   
0092   return NULL;
0093 }
0094 
0095 int main(int argc, char **argv)
0096 {
0097   int        rc;
0098   pthread_t  id;
0099   sigset_t   empty;
0100 
0101   TEST_BEGIN();
0102 
0103   /*
0104    * Verify first thread has empty signal mask
0105    */
0106   fetch_sigmask("main", &main_sigmask);
0107 
0108   sigemptyset(&empty);
0109   fetch_sigmask("empty", &empty);
0110 
0111   puts("Ensure main thread's mask equals sigemptyset");
0112   rtems_test_assert(main_sigmask == empty);
0113 
0114   /*
0115    * Verify first thread has empty signal mask
0116    */
0117   puts("Ensure parent's empty mask is inherited by thread");
0118   rc = pthread_create(&id, NULL, thread_body, "thread1");
0119   rtems_test_assert(rc == 0);
0120   sleep(1);
0121 
0122   /*
0123    * Create a thread and see if it inherits non-empty signal mask.
0124    */
0125   puts("Ensure parent's mask with SIGUSR1 mask is inherited by thread");
0126   block_sigmask(SIGUSR1, &main_sigmask);
0127   rc = pthread_create(&id, NULL, thread_body, "thread2");
0128   rtems_test_assert(rc == 0);
0129   sleep(1);
0130   
0131   TEST_END();
0132 
0133   rtems_test_exit(0);
0134   return 0;
0135 }
0136