Add support for rtld directory different from slib directory
[glibc.git] / stdlib / tst-tls-atexit.c
blob3d3777c31b42540d695d1d409fdddb5a76d81381
1 /* Verify that DSO is unloaded only if its TLS objects are destroyed.
2 Copyright (C) 2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* There are two tests in this test case. The first is implicit where it is
20 assumed that the destructor call on exit of the LOAD function does not
21 segfault. The other is a verification that after the thread has exited, a
22 dlclose will unload the DSO. */
24 #include <dlfcn.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
31 void *handle;
32 pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
34 void *
35 load (void *u)
37 pthread_mutex_lock (&m);
38 handle = dlopen ("$ORIGIN/tst-tls-atexit-lib.so", RTLD_LAZY);
39 if (!handle)
41 printf ("Unable to load DSO: %s\n", dlerror ());
42 return (void *) (uintptr_t) 1;
45 void (*foo) (void) = (void (*) (void)) dlsym(handle, "do_foo");
47 if (!foo)
49 printf ("Unable to find symbol: %s\n", dlerror ());
50 exit (1);
53 foo ();
55 /* This should not unload the DSO. If it does, then the thread exit will
56 result in a segfault. */
57 dlclose (handle);
58 pthread_mutex_unlock (&m);
60 return NULL;
63 int
64 main (void)
66 pthread_t t;
67 int ret;
68 void *thr_ret;
70 if ((ret = pthread_create (&t, NULL, load, NULL)) != 0)
72 printf ("pthread_create failed: %s\n", strerror (ret));
73 return 1;
76 if ((ret = pthread_join (t, &thr_ret)) != 0)
78 printf ("pthread_create failed: %s\n", strerror (ret));
79 return 1;
82 if (thr_ret != NULL)
83 return 1;
85 /* Now this should unload the DSO. */
86 dlclose (handle);
88 /* Run through our maps and ensure that the DSO is unloaded. */
89 FILE *f = fopen ("/proc/self/maps", "r");
91 if (f == NULL)
93 perror ("Failed to open /proc/self/maps");
94 fprintf (stderr, "Skipping verification of DSO unload\n");
95 return 0;
98 char *line = NULL;
99 size_t s = 0;
100 while (getline (&line, &s, f) > 0)
102 if (strstr (line, "tst-tls-atexit-lib.so"))
104 printf ("DSO not unloaded yet:\n%s", line);
105 return 1;
108 free (line);
110 return 0;