aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel
diff options
context:
space:
mode:
authorGreg Hackmann <ghackmann@google.com>2019-03-19 12:19:39 +0530
committerCyril Hrubis <chrubis@suse.cz>2019-03-19 14:38:31 +0100
commitd6ac3526fdc85cea4faa690fa89a1776b5f1af2c (patch)
treed44a8a2dca520e4070960aa5c01526556a1a3db0 /testcases/kernel
parentd2714ceab57603ba9869b207203005bc8e0d08a0 (diff)
syscalls/tgkill01: add new test
tgkill() delivers a signal to a specific thread. Test this by installing a SIGUSR1 handler which records the current pthread ID. Start a number of threads in parallel, then one-by-one call tgkill(..., tid, SIGUSR1) and check that the expected pthread ID was recorded. Signed-off-by: Greg Hackmann <ghackmann@google.com> Signed-off-by: Sumit Garg <sumit.garg@linaro.org> Reviewed-by: Li Wang <liwang@redhat.com> Acked-by: Cyril Hrubis <chrubis@suse.cz>
Diffstat (limited to 'testcases/kernel')
-rw-r--r--testcases/kernel/syscalls/tgkill/.gitignore1
-rw-r--r--testcases/kernel/syscalls/tgkill/Makefile10
-rw-r--r--testcases/kernel/syscalls/tgkill/tgkill.h22
-rw-r--r--testcases/kernel/syscalls/tgkill/tgkill01.c134
4 files changed, 167 insertions, 0 deletions
diff --git a/testcases/kernel/syscalls/tgkill/.gitignore b/testcases/kernel/syscalls/tgkill/.gitignore
new file mode 100644
index 000000000..f4566fde2
--- /dev/null
+++ b/testcases/kernel/syscalls/tgkill/.gitignore
@@ -0,0 +1 @@
+tgkill01
diff --git a/testcases/kernel/syscalls/tgkill/Makefile b/testcases/kernel/syscalls/tgkill/Makefile
new file mode 100644
index 000000000..602a59ba1
--- /dev/null
+++ b/testcases/kernel/syscalls/tgkill/Makefile
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2018 Google, Inc.
+
+top_srcdir ?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+CFLAGS += -pthread
diff --git a/testcases/kernel/syscalls/tgkill/tgkill.h b/testcases/kernel/syscalls/tgkill/tgkill.h
new file mode 100644
index 000000000..a7d96f452
--- /dev/null
+++ b/testcases/kernel/syscalls/tgkill/tgkill.h
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Google, Inc.
+ */
+
+#ifndef TGKILL_H
+#define TGKILL_H
+
+#include "config.h"
+#include "lapi/syscalls.h"
+
+static inline int sys_tgkill(int tgid, int tid, int sig)
+{
+ return tst_syscall(__NR_tgkill, tgid, tid, sig);
+}
+
+static inline pid_t sys_gettid(void)
+{
+ return tst_syscall(__NR_gettid);
+}
+
+#endif /* TGKILL_H */
diff --git a/testcases/kernel/syscalls/tgkill/tgkill01.c b/testcases/kernel/syscalls/tgkill/tgkill01.c
new file mode 100644
index 000000000..075bf4882
--- /dev/null
+++ b/testcases/kernel/syscalls/tgkill/tgkill01.c
@@ -0,0 +1,134 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2018 Google, Inc.
+ *
+ * tgkill() delivers a signal to a specific thread. Test this by installing
+ * a SIGUSR1 handler which records the current pthread ID. Start a number
+ * of threads in parallel, then one-by-one call tgkill(..., tid, SIGUSR1)
+ * and check that the expected pthread ID was recorded.
+ */
+
+#include <pthread.h>
+#include <stdlib.h>
+
+#include "tst_safe_pthread.h"
+#include "tst_test.h"
+#include "tgkill.h"
+
+struct thread_state {
+ pthread_t thread;
+ pid_t tid;
+};
+
+static char *str_threads;
+static int n_threads = 10;
+static struct thread_state *threads;
+
+static pthread_t sigusr1_thread;
+
+static void sigusr1_handler(int signum __attribute__((unused)))
+{
+ sigusr1_thread = pthread_self();
+}
+
+static void *thread_func(void *arg)
+{
+ struct thread_state *thread = arg;
+
+ /**
+ * There is no standard way to map pthread -> tid, so we will have the
+ * child stash its own tid then notify the parent that the stashed tid
+ * is available.
+ */
+ thread->tid = sys_gettid();
+
+ TST_CHECKPOINT_WAKE(0);
+
+ TST_CHECKPOINT_WAIT(1);
+
+ return arg;
+}
+
+static void start_thread(struct thread_state *thread)
+{
+ SAFE_PTHREAD_CREATE(&thread->thread, NULL, thread_func, thread);
+
+ TST_CHECKPOINT_WAIT(0);
+}
+
+static void stop_threads(void)
+{
+ int i;
+
+ TST_CHECKPOINT_WAKE2(1, n_threads);
+
+ for (i = 0; i < n_threads; i++) {
+ if (threads[i].tid == -1)
+ continue;
+
+ SAFE_PTHREAD_JOIN(threads[i].thread, NULL);
+ threads[i].tid = -1;
+ }
+
+ if (threads)
+ free(threads);
+}
+
+static void run(void)
+{
+ int i;
+
+ for (i = 0; i < n_threads; i++) {
+ sigusr1_thread = pthread_self();
+
+ TEST(sys_tgkill(getpid(), threads[i].tid, SIGUSR1));
+ if (TST_RET) {
+ tst_res(TFAIL | TTERRNO, "tgkill() failed");
+ return;
+ }
+
+ while (pthread_equal(sigusr1_thread, pthread_self()))
+ usleep(1000);
+
+ if (!pthread_equal(sigusr1_thread, threads[i].thread)) {
+ tst_res(TFAIL, "SIGUSR1 delivered to wrong thread");
+ return;
+ }
+ }
+
+ tst_res(TPASS, "SIGUSR1 delivered to correct threads");
+}
+
+static void setup(void)
+{
+ int i;
+
+ if (tst_parse_int(str_threads, &n_threads, 1, INT_MAX))
+ tst_brk(TBROK, "Invalid number of threads '%s'", str_threads);
+
+ threads = SAFE_MALLOC(sizeof(*threads) * n_threads);
+
+ struct sigaction sigusr1 = {
+ .sa_handler = sigusr1_handler,
+ };
+ SAFE_SIGACTION(SIGUSR1, &sigusr1, NULL);
+
+ for (i = 0; i < n_threads; i++)
+ threads[i].tid = -1;
+
+ for (i = 0; i < n_threads; i++)
+ start_thread(&threads[i]);
+}
+
+static struct tst_option options[] = {
+ {"t:", &str_threads, "-t Number of threads (default 10)"},
+ {NULL, NULL, NULL},
+};
+
+static struct tst_test test = {
+ .options = options,
+ .needs_checkpoints = 1,
+ .setup = setup,
+ .test_all = run,
+ .cleanup = stop_threads,
+};