glibc/sysdeps/unix/sysv/linux/tst-gettid-kill.c

130 lines
3.9 KiB
C
Raw Normal View History

/* Verify the interaction of kill and thread groups.
Copyright (C) 2019-2023 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
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://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>