aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/unix/sysv/linux/tst-gettid-kill.c
blob: 1b3b0bf4196c22ac91cb7e6c2ebdd705e87c1561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Verify the interaction of kill and thread groups.
   Copyright (C) 2019-2022 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
   <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>