summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoel Sherrill <joel@rtems.org>2019-11-08 08:44:30 -0600
committerJoel Sherrill <joel@rtems.org>2021-08-20 15:58:13 -0500
commitd7174f6230c559ad38e40ba94a952a6910d91e3e (patch)
treeddcb153ae9d1ef97b578204d0042c37e364217ba
parentpsx_sigint: Add new example of POSIX signal and default mask (diff)
downloadrtems-examples-d7174f6230c559ad38e40ba94a952a6910d91e3e.tar.bz2
psx_initial_sigmask: Add POSIX initial signal mask reporter
-rw-r--r--posix_api/psx_initial_sigmask/Makefile20
-rw-r--r--posix_api/psx_initial_sigmask/main.c125
-rw-r--r--posix_api/psx_initial_sigmask/rtems_config.c48
-rw-r--r--posix_api/psx_initial_sigmask/wscript15
4 files changed, 208 insertions, 0 deletions
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@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@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'])
+