1 /* Check if printf like functions does not disable asynchronous cancellation
2    mode (BZ#29214).
3 
4    Copyright (C) 2022 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6 
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11 
12    The GNU C Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16 
17    You should have received a copy of the GNU Lesser General Public
18    License along with the GNU C Library; if not, see
19    <https://www.gnu.org/licenses/>.  */
20 
21 #include <support/check.h>
22 #include <support/xstdio.h>
23 #include <support/xthread.h>
24 #include <sys/syscall.h>
25 #include <unistd.h>
26 
27 static pthread_barrier_t b;
28 
29 static void *
tf(void * arg)30 tf (void *arg)
31 {
32   int old;
33 
34   TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL), 0);
35 
36   TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0);
37   TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS);
38 
39   /* Check if internal lock cleanup routines restore the cancellation type
40      correctly.  */
41   printf ("...\n");
42   TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0);
43   TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS);
44 
45   xpthread_barrier_wait (&b);
46 
47   /* Wait indefinitely for cancellation, which only works if asynchronous
48      cancellation is enabled.  */
49 #ifdef SYS_pause
50   syscall (SYS_pause);
51 #elif defined SYS_ppoll || defined SYS_ppoll_time64
52 # ifndef SYS_ppoll_time64
53 #  define SYS_ppoll_time64 SYS_ppoll
54 # endif
55   syscall (SYS_ppoll_time64, NULL, 0, NULL, NULL);
56 #else
57   for (;;);
58 #endif
59 
60   return 0;
61 }
62 
63 static int
do_test(void)64 do_test (void)
65 {
66   xpthread_barrier_init (&b, NULL, 2);
67 
68   pthread_t th = xpthread_create (NULL, tf, NULL);
69 
70   xpthread_barrier_wait (&b);
71 
72   xpthread_cancel (th);
73 
74   void *status = xpthread_join (th);
75   TEST_VERIFY (status == PTHREAD_CANCELED);
76 
77   return 0;
78 }
79 
80 /* There is no need to wait full TIMEOUT if asynchronous is not working.  */
81 #define TIMEOUT 3
82 #include <support/test-driver.c>
83