aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/tst-gettid-kill.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2019-02-02 15:17:02 +0100
committerFlorian Weimer <fweimer@redhat.com>2019-02-08 11:27:55 +0100
commit1d0fc213824eaa2a8f8c4385daaa698ee8fb7c92 (patch)
treea1979a3d4ef860e9f4565aab20cb2549b0a3303e /sysdeps/unix/sysv/linux/tst-gettid-kill.c
parentf289e656ec8221756519a601042bc9fbe1b310fb (diff)
downloadglibc-1d0fc213824eaa2a8f8c4385daaa698ee8fb7c92.tar
glibc-1d0fc213824eaa2a8f8c4385daaa698ee8fb7c92.tar.gz
glibc-1d0fc213824eaa2a8f8c4385daaa698ee8fb7c92.tar.bz2
glibc-1d0fc213824eaa2a8f8c4385daaa698ee8fb7c92.zip
Linux: Add gettid system call wrapper [BZ #6399]
This commit adds gettid to <unistd.h> on Linux, and not to the kernel-independent GNU API. gettid is now supportable on Linux because too many things assume a 1:1 mapping between libpthread threads and kernel threads. Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Diffstat (limited to 'sysdeps/unix/sysv/linux/tst-gettid-kill.c')
-rw-r--r--sysdeps/unix/sysv/linux/tst-gettid-kill.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/sysdeps/unix/sysv/linux/tst-gettid-kill.c b/sysdeps/unix/sysv/linux/tst-gettid-kill.c
new file mode 100644
index 0000000000..98a7c2769e
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/tst-gettid-kill.c
@@ -0,0 +1,129 @@
+/* Verify the interaction of kill and thread groups.
+ Copyright (C) 2019 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+/* This test demonstrates that a signal which is sent to a specified
+ thread ID using the kill function is delivered to the entire thread
+ group (as if it had been sent to the process group). */
+
+#include <errno.h>
+#include <signal.h>
+#include <support/check.h>
+#include <support/xsignal.h>
+#include <support/xthread.h>
+#include <support/xunistd.h>
+
+/* Signal set containing SIGUSR1. */
+static sigset_t sigusr1_set;
+
+/* Used to synchronize the threads. */
+static pthread_barrier_t barrier;
+
+/* TID of the thread to which the signal is sent. */
+static pid_t target_tid;
+
+/* Thread which is expected to receive the SIGUSR1 signal. */
+static pthread_t signal_thread;
+
+/* Pipe used to block and terminate the signal thread. */
+static int pipe_signal[2];
+
+static volatile sig_atomic_t handler_tid;
+
+static void
+sigusr1_handler (int signo)
+{
+ TEST_COMPARE (signo, SIGUSR1);
+ TEST_VERIFY (pthread_self () == signal_thread);
+ TEST_COMPARE (handler_tid, 0);
+ handler_tid = gettid ();
+ TEST_VERIFY (handler_tid > 0);
+ /* Ensure that the read system call in thread_read exits if the
+ signal is delivered before the system call is invoked. */
+ char ch = 'X';
+ xwrite (pipe_signal[1], &ch, 1);
+}
+
+/* Thread which calls pause without expecting it to return. The TID
+ of this thread is used as the target in the kill function call. */
+static void *
+thread_pause_noreturn (void *closure)
+{
+ target_tid = gettid ();
+ TEST_VERIFY (target_tid > 0);
+ xpthread_barrier_wait (&barrier);
+ pause ();
+ FAIL_EXIT1 ("The pause function returned");
+ return NULL;
+}
+
+/* Thread which is expected to receive the signal. */
+static void *
+thread_read_signal (void *closure)
+{
+ xpthread_sigmask (SIG_UNBLOCK, &sigusr1_set, NULL);
+ xpthread_barrier_wait (&barrier);
+ TEST_VERIFY (target_tid > 0);
+ TEST_VERIFY (gettid () != target_tid);
+ char ch;
+ ssize_t ret = read (pipe_signal[0], &ch, 1);
+ if (ret == 1)
+ /* The signal was delivered before we entered the read system
+ call. */
+ TEST_COMPARE (ch, 'X');
+ else
+ {
+ /* The signal was delivered while blocked in the read system
+ call. */
+ TEST_COMPARE (ret, -1);
+ TEST_COMPARE (errno, EINTR);
+ }
+ TEST_COMPARE (handler_tid, gettid ());
+ return NULL;
+}
+
+static int
+do_test (void)
+{
+ /* Block the SIGUSR1 signal in all threads. */
+ sigemptyset (&sigusr1_set);
+ sigaddset (&sigusr1_set, SIGUSR1);
+ xpthread_sigmask (SIG_BLOCK, &sigusr1_set, NULL);
+
+ xsignal (SIGUSR1, sigusr1_handler);
+ xpipe (pipe_signal);
+
+ xpthread_barrier_init (&barrier, NULL, 3);
+
+ pthread_t target_thread
+ = xpthread_create (NULL, thread_pause_noreturn, NULL);
+ signal_thread = xpthread_create (NULL, thread_read_signal, NULL);
+ xpthread_barrier_wait (&barrier);
+
+ /* Send the SIGUSR1 signal to the thread which has it blocked, and
+ expect it to be delivered to the other thread. */
+ TEST_COMPARE (kill (target_tid, SIGUSR1), 0);
+
+ xpthread_join (signal_thread);
+ xpthread_cancel (target_thread);
+ xpthread_join (target_thread);
+
+ xpthread_barrier_destroy (&barrier);
+ return 0;
+}
+
+#include <support/test-driver.c>