xref: /netbsd-src/external/gpl3/gcc.old/dist/libphobos/testsuite/libphobos.shared/host.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg #include <stdlib.h>
2*627f7eb2Smrg #include <string.h>
3*627f7eb2Smrg #include <dlfcn.h>
4*627f7eb2Smrg #include <assert.h>
5*627f7eb2Smrg 
main(int argc,char * argv[])6*627f7eb2Smrg int main(int argc, char* argv[])
7*627f7eb2Smrg {
8*627f7eb2Smrg #if defined(__FreeBSD__)
9*627f7eb2Smrg     // workaround for Bugzilla 14824
10*627f7eb2Smrg     void *druntime = dlopen(argv[1], RTLD_LAZY); // load druntime
11*627f7eb2Smrg     assert(druntime);
12*627f7eb2Smrg #endif
13*627f7eb2Smrg 
14*627f7eb2Smrg     const size_t pathlen = strrchr(argv[0], '/') - argv[0] + 1;
15*627f7eb2Smrg     char *name = malloc(pathlen + sizeof("plugin1.so"));
16*627f7eb2Smrg     memcpy(name, argv[0], pathlen);
17*627f7eb2Smrg     memcpy(name+pathlen, "plugin1.so", sizeof("plugin1.so"));
18*627f7eb2Smrg 
19*627f7eb2Smrg     void* plugin1 = dlopen(name, RTLD_LAZY);
20*627f7eb2Smrg     name[pathlen + sizeof("plugin1.so") - 5] = '2';
21*627f7eb2Smrg     void* plugin2 = dlopen(name, RTLD_LAZY);
22*627f7eb2Smrg 
23*627f7eb2Smrg     int (*plugin1_init)() = dlsym(plugin1, "plugin_init");
24*627f7eb2Smrg     int (*plugin1_term)() = dlsym(plugin1, "plugin_term");
25*627f7eb2Smrg     int (*runTests1)() = dlsym(plugin1, "runTests");
26*627f7eb2Smrg     int (*plugin2_init)() = dlsym(plugin2, "plugin_init");
27*627f7eb2Smrg     int (*plugin2_term)() = dlsym(plugin2, "plugin_term");
28*627f7eb2Smrg     int (*runTests2)() = dlsym(plugin2, "runTests");
29*627f7eb2Smrg     assert(plugin1_init());
30*627f7eb2Smrg     assert(runTests1());
31*627f7eb2Smrg     assert(plugin2_init());
32*627f7eb2Smrg     assert(runTests2());
33*627f7eb2Smrg 
34*627f7eb2Smrg     assert(plugin1_term());
35*627f7eb2Smrg     assert(dlclose(plugin1) == 0);
36*627f7eb2Smrg     assert(runTests2());
37*627f7eb2Smrg 
38*627f7eb2Smrg     name[pathlen + sizeof("plugin1.so") - 5] = '1';
39*627f7eb2Smrg     plugin1 = dlopen(name, RTLD_LAZY);
40*627f7eb2Smrg     plugin1_init = dlsym(plugin1, "plugin_init");
41*627f7eb2Smrg     plugin1_term = dlsym(plugin1, "plugin_term");
42*627f7eb2Smrg     runTests1 = dlsym(plugin1, "runTests");
43*627f7eb2Smrg     assert(plugin1_init());
44*627f7eb2Smrg     assert(runTests1());
45*627f7eb2Smrg     assert(runTests2());
46*627f7eb2Smrg 
47*627f7eb2Smrg     assert(plugin2_term());
48*627f7eb2Smrg     assert(dlclose(plugin2) == 0);
49*627f7eb2Smrg     assert(runTests1());
50*627f7eb2Smrg 
51*627f7eb2Smrg     assert(plugin1_term());
52*627f7eb2Smrg     assert(dlclose(plugin1) == 0);
53*627f7eb2Smrg 
54*627f7eb2Smrg     free(name);
55*627f7eb2Smrg 
56*627f7eb2Smrg #if defined(__FreeBSD__)
57*627f7eb2Smrg     dlclose(druntime);
58*627f7eb2Smrg #endif
59*627f7eb2Smrg     return EXIT_SUCCESS;
60*627f7eb2Smrg }
61