[PATCH rtems-examples 03/23] psx_initial_sigmask: Add POSIX initial signal mask reporter

Joel Sherrill joel at rtems.org
Wed Feb 23 22:21:31 UTC 2022


---
 posix_api/psx_initial_sigmask/Makefile       |  20 +++++
 posix_api/psx_initial_sigmask/main.c         | 125 +++++++++++++++++++++++++++
 posix_api/psx_initial_sigmask/rtems_config.c |  48 ++++++++++
 posix_api/psx_initial_sigmask/wscript        |  15 ++++
 4 files changed, 208 insertions(+)
 create mode 100644 posix_api/psx_initial_sigmask/Makefile
 create mode 100644 posix_api/psx_initial_sigmask/main.c
 create mode 100644 posix_api/psx_initial_sigmask/rtems_config.c
 create mode 100644 posix_api/psx_initial_sigmask/wscript

diff --git a/posix_api/psx_initial_sigmask/Makefile b/posix_api/psx_initial_sigmask/Makefile
new file mode 100644
index 0000000..2e252c1
--- /dev/null
+++ b/posix_api/psx_initial_sigmask/Makefile
@@ -0,0 +1,20 @@
+#
+#  RTEMS_MAKEFILE_PATH is typically set in an environment variable
+#
+
+PGM=${ARCH}/psx_initial_sigmask.exe
+
+# C source names
+CSRCS = main.c rtems_config.c
+COBJS = $(CSRCS:%.c=${ARCH}/%.o)
+
+include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
+include $(RTEMS_CUSTOM)
+include $(PROJECT_ROOT)/make/leaf.cfg
+
+OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS)
+
+all:    ${ARCH} $(PGM)
+
+$(PGM): $(OBJS)
+	$(make-exe)
diff --git a/posix_api/psx_initial_sigmask/main.c b/posix_api/psx_initial_sigmask/main.c
new file mode 100644
index 0000000..392d3a1
--- /dev/null
+++ b/posix_api/psx_initial_sigmask/main.c
@@ -0,0 +1,125 @@
+/*
+ * A C program that displays the default process signal mask and ensures
+ * that a created thread inherits its initial signal mask from the thread
+ * that created it.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+sigset_t main_sigmask;
+
+/*
+ * On Linux, sigset_t is a structure with an array in it. The size of
+ * the entire structure can be larger than the array itself due to padding.
+ * This is intended to ensure we only print and compare valid contents.
+ *
+ * Note if Linux moves beyond 1024 bits in the structure, this will break.
+ */
+#ifdef __linux__
+  #define SIGSET_NBYTES (_SIGSET_NWORDS * sizeof(unsigned long int))
+  #define SIGSET_NWORDS _SIGSET_NWORDS
+#else
+  #define SIGSET_NBYTES (sizeof(sigset_t))
+  #define SIGSET_NWORDS (sizeof(sigset_t) / sizeof(unsigned long int))
+#endif
+
+static void print_sigmask(const char *name, sigset_t *sigset_p)
+{
+  int            rc;
+  unsigned char *p;
+  int            i;
+
+  rc = sigemptyset(sigset_p);
+  assert(rc == 0);
+
+  rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
+  assert(rc == 0);
+  printf("%s signal mask (in hex):\n    ", name);
+
+  /*
+   * There is no assurance that sigset_t is a primitive type so
+   * we have to print it a long at a time.
+   */
+  p = (unsigned char *) sigset_p;
+  for (i=0 ; i < SIGSET_NBYTES ; i++) {
+    printf("%02x%s", *p++, (i % 16 == 15) ? "\n    " : " ");
+  }
+  printf("\n");
+}
+
+static void block_sigmask(int signo, sigset_t *sigset_p)
+{
+  sigset_t sigset;
+  int      rc;
+
+  /*
+   * Block the requested signal
+   */
+  rc = sigemptyset(&sigset);
+  assert(rc == 0);
+  rc = sigaddset(&sigset, signo);
+  assert(rc == 0);
+
+  rc = sigprocmask(SIG_BLOCK, &sigset, NULL);
+  assert(rc == 0);
+
+  /*
+   * Fetch the current signal mask reflecting the requested update
+   */
+  sigemptyset(sigset_p);
+  rc = sigprocmask(SIG_SETMASK, NULL, sigset_p);
+  assert(rc == 0);
+}
+
+static void *thread_body(void *arg)
+{
+  sigset_t    mask;
+  const char *name = (const char *)arg;
+
+  /*
+   * There is no assurance that sigset_t is a primitive type so
+   * we have to use memcmp().
+   */
+  printf("Ensure %s mask equals parent's mask\n", name);
+  print_sigmask("main", &main_sigmask);
+  print_sigmask(arg, &mask);
+  assert(memcmp(&main_sigmask, &mask, SIGSET_NWORDS) == 0);
+  
+  return NULL;
+}
+
+int main(int argc, char **argv)
+{
+  int        rc;
+  pthread_t  id;
+
+  puts("*** START OF SIGNAL MASK TEST ***");
+
+  sigset_t empty;
+  print_sigmask("empty", &empty);
+
+  print_sigmask("main", &main_sigmask);
+
+  rc = pthread_create(&id, NULL, thread_body, "thread1");
+  assert(rc == 0);
+  sleep(1);
+
+  block_sigmask(SIGUSR1, &main_sigmask);
+  rc = pthread_create(&id, NULL, thread_body, "thread2");
+  assert(rc == 0);
+  sleep(1);
+  
+
+  puts("*** END OF SIGNAL MASK TEST ***");
+  exit(0);
+  return 0;
+}
+
diff --git a/posix_api/psx_initial_sigmask/rtems_config.c b/posix_api/psx_initial_sigmask/rtems_config.c
new file mode 100644
index 0000000..fd0a0f8
--- /dev/null
+++ b/posix_api/psx_initial_sigmask/rtems_config.c
@@ -0,0 +1,48 @@
+/*
+ * This file contains the RTEMS Configuration for this example.
+ */
+
+/*
+ * Copyright 2018 Joel Sherrill (joel at rtems.org)
+ *
+ * This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
+ */
+
+#include <stdlib.h>
+
+int main(int argc, char **argv);
+
+static char *argv_list[] = {
+  "report",
+  ""
+};
+static void *POSIX_Init(void *arg)
+{
+  (void) arg;  /* deliberately ignored */
+
+  /*
+   * Initialize optional services
+   */
+
+  /*
+   * Could get arguments from command line or have a static set.
+   */
+  (void) main(1, argv_list);
+
+  return NULL;
+}
+
+#include <bsp.h> /* for device driver prototypes */
+
+/* NOTICE: the clock driver is explicitly disabled */
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+
+#define CONFIGURE_POSIX_INIT_THREAD_TABLE
+
+#define CONFIGURE_UNLIMITED_OBJECTS
+#define CONFIGURE_UNIFIED_WORK_AREAS
+#define CONFIGURE_MINIMUM_TASK_STACK_SIZE (64 * 1024)
+
+#define CONFIGURE_INIT
+#include <rtems/confdefs.h>
diff --git a/posix_api/psx_initial_sigmask/wscript b/posix_api/psx_initial_sigmask/wscript
new file mode 100644
index 0000000..bff7d50
--- /dev/null
+++ b/posix_api/psx_initial_sigmask/wscript
@@ -0,0 +1,15 @@
+# Copyright 2013 Gedare Bloom (gedare at rtems.org)
+#
+# This file's license is 2-clause BSD as in this distribution's LICENSE.2 file.
+#
+
+import rtems_waf.rtems as rtems
+
+def build(bld):
+    rtems.build(bld)
+
+    bld(features = 'c cprogram',
+        target = 'psx_initial_sigmask.exe',
+        source = ['main.c','rtems_config.c'],
+        lib = ['c'])
+
-- 
1.8.3.1



More information about the devel mailing list