add _DEFAULT_SOURCE wherever _BSD_SOURCE was used
[libc-test] / src / functional / dlopen.c
1 #include <dlfcn.h>
2 #include "test.h"
3
4 int main(int argc, char *argv[])
5 {
6         void *h, *g;
7         int *i, *i2;
8         char *s;
9         void (*f)(void);
10         char buf[512];
11
12         if (!t_pathrel(buf, sizeof buf, argv[0], "dlopen_dso.so")) {
13                 t_error("failed to obtain relative path to dlopen_dso.so\n");
14                 return 1;
15         }
16         h = dlopen(buf, RTLD_LAZY|RTLD_LOCAL);
17         if (!h)
18                 t_error("dlopen %s failed: %s\n", buf, dlerror());
19         i = dlsym(h, "i");
20         if (!i)
21                 t_error("dlsym i failed: %s\n", dlerror());
22         if (*i != 1)
23                 t_error("initialization failed: want i=1 got i=%d\n", *i);
24         f = (void (*)(void))dlsym(h, "f");
25         if (!f)
26                 t_error("dlsym f failed: %s\n", dlerror());
27         f();
28         if (*i != 2)
29                 t_error("f call failed: want i=2 got i=%d\n", *i);
30
31         g = dlopen(0, RTLD_LAZY|RTLD_LOCAL);
32         if (!g)
33                 t_error("dlopen 0 failed: %s\n", dlerror());
34         i2 = dlsym(g, "i");
35         s = dlerror();
36         if (i2 || s == 0)
37                 t_error("dlsym i should have failed\n");
38         if (dlsym(g, "main") != (void*)main)
39                 t_error("dlsym main failed: %s\n", dlerror());
40
41         /* close+open reinitializes the dso with glibc but not with musl */
42         h = dlopen(buf, RTLD_LAZY|RTLD_GLOBAL);
43         i2 = dlsym(g, "i");
44         if (!i2)
45                 t_error("dlsym i failed: %s\n", dlerror());
46         if (i2 != i)
47                 t_error("reopened dso should have the same symbols, want %p, got %p\n", i, i2);
48         if (*i2 != 2)
49                 t_error("reopened dso should have the same symbols, want i2==2, got i2==%d\n", *i2);
50         if (dlclose(g))
51                 t_error("dlclose failed: %s\n", dlerror());
52         if (dlclose(h))
53                 t_error("dlclose failed: %s\n", dlerror());
54         return t_status;
55 }