Fix infinite loop in ftell when writing wide char data (BZ #16398)
[glibc.git] / nptl / tst-tls7.c
blob3e85a6ec85c793fc89585189805406b6a6260aa2
1 /* Copyright (C) 2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
19 /* This test checks that TLS in a dlopened object works when first accessed
20 from a signal handler. */
22 #include <assert.h>
23 #include <atomic.h>
24 #include <dlfcn.h>
25 #include <pthread.h>
26 #include <semaphore.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 void *
32 spin (void *ignored)
34 while (1)
36 /* busywork */
37 free (malloc (128));
40 /* never reached */
41 return NULL;
44 static void (*tls7mod_action) (int, siginfo_t *, void *);
46 static void
47 action (int signo, siginfo_t *info, void *ignored)
49 sem_t *sem = info->si_value.sival_ptr;
51 atomic_read_barrier ();
52 assert (tls7mod_action != NULL);
53 (*tls7mod_action) (signo, info, ignored);
55 /* This sem_post may trigger dlclose, which will invalidate tls7mod_action.
56 It is important to do that only after tls7mod_action is no longer
57 active. */
58 sem_post (sem);
61 int
62 do_test (void)
64 pthread_t th[10];
66 for (int i = 0; i < 10; ++i)
68 if (pthread_create (&th[i], NULL, spin, NULL))
70 puts ("pthread_create failed");
71 exit (1);
74 #define NITERS 75
76 for (int i = 0; i < NITERS; ++i)
78 void *h = dlopen ("tst-tls7mod.so", RTLD_LAZY);
79 if (h == NULL)
81 puts ("dlopen failed");
82 exit (1);
85 tls7mod_action = dlsym (h, "action");
86 if (tls7mod_action == NULL)
88 puts ("dlsym for action failed");
89 exit (1);
91 atomic_write_barrier ();
93 struct sigaction sa;
94 sa.sa_sigaction = action;
95 sigemptyset (&sa.sa_mask);
96 sa.sa_flags = SA_SIGINFO;
97 if (sigaction (SIGUSR1, &sa, NULL))
99 puts ("sigaction failed");
100 exit (1);
103 sem_t sem;
104 if (sem_init (&sem, 0, 0))
106 puts ("sem_init failed");
109 sigval_t val;
110 val.sival_ptr = &sem;
111 for (int i = 0; i < 10; ++i)
113 if (pthread_sigqueue (th[i], SIGUSR1, val))
115 puts ("pthread_sigqueue failed");
120 for (int i = 0; i < 10; ++i)
122 if (sem_wait (&sem))
124 puts ("sem_wait failed");
128 /* Paranoia. */
129 tls7mod_action = NULL;
131 if (dlclose (h))
133 puts ("dlclose failed");
134 exit (1);
137 return 0;
140 #define TIMEOUT 8
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"