1 | #include <dlfcn.h>
|
---|
2 | #include <libintl.h>
|
---|
3 | #include <link.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <string.h>
|
---|
7 |
|
---|
8 | static int
|
---|
9 | check_loaded_objects (const char **loaded)
|
---|
10 | {
|
---|
11 | struct link_map *lm;
|
---|
12 | int n;
|
---|
13 | int *found = NULL;
|
---|
14 | int errors = 0;
|
---|
15 |
|
---|
16 | for (n = 0; loaded[n]; n++)
|
---|
17 | /* NOTHING */;
|
---|
18 |
|
---|
19 | if (n)
|
---|
20 | {
|
---|
21 | found = (int *) alloca (sizeof (int) * n);
|
---|
22 | memset (found, 0, sizeof (int) * n);
|
---|
23 | }
|
---|
24 |
|
---|
25 | printf(" Name\n");
|
---|
26 | printf(" --------------------------------------------------------\n");
|
---|
27 | for (lm = _r_debug.r_map; lm; lm = lm->l_next)
|
---|
28 | {
|
---|
29 | if (lm->l_name && lm->l_name[0])
|
---|
30 | printf(" %s, count = %d\n", lm->l_name, (int) lm->l_direct_opencount);
|
---|
31 | if (lm->l_type == lt_loaded && lm->l_name)
|
---|
32 | {
|
---|
33 | int match = 0;
|
---|
34 | for (n = 0; loaded[n] != NULL; n++)
|
---|
35 | {
|
---|
36 | if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
|
---|
37 | {
|
---|
38 | found[n] = 1;
|
---|
39 | match = 1;
|
---|
40 | break;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (match == 0)
|
---|
45 | {
|
---|
46 | ++errors;
|
---|
47 | printf ("ERRORS: %s is not unloaded\n", lm->l_name);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | for (n = 0; loaded[n] != NULL; n++)
|
---|
53 | {
|
---|
54 | if (found[n] == 0)
|
---|
55 | {
|
---|
56 | ++errors;
|
---|
57 | printf ("ERRORS: %s is not loaded\n", loaded[n]);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | return errors;
|
---|
62 | }
|
---|
63 |
|
---|
64 | int
|
---|
65 | main (void)
|
---|
66 | {
|
---|
67 | void *obj2[2];
|
---|
68 | void *obj3;
|
---|
69 | const char *loaded[] = { NULL, NULL, NULL, NULL };
|
---|
70 | int errors = 0;
|
---|
71 |
|
---|
72 | printf ("\nThis is what is in memory now:\n");
|
---|
73 | errors += check_loaded_objects (loaded);
|
---|
74 |
|
---|
75 | printf( "Loading shared object neededobj3.so\n");
|
---|
76 | obj3 = dlopen( "neededobj3.so", RTLD_LAZY);
|
---|
77 | if (obj3 == NULL)
|
---|
78 | {
|
---|
79 | printf ("%s\n", dlerror ());
|
---|
80 | exit (1);
|
---|
81 | }
|
---|
82 | loaded[0] = "neededobj1.so";
|
---|
83 | loaded[1] = "neededobj2.so";
|
---|
84 | loaded[2] = "neededobj3.so";
|
---|
85 | errors += check_loaded_objects (loaded);
|
---|
86 |
|
---|
87 | printf ("Now loading shared object neededobj2.so\n");
|
---|
88 | obj2[0] = dlopen ("neededobj2.so", RTLD_LAZY);
|
---|
89 | if (obj2[0] == NULL)
|
---|
90 | {
|
---|
91 | printf ("%s\n", dlerror ());
|
---|
92 | exit (1);
|
---|
93 | }
|
---|
94 | errors += check_loaded_objects (loaded);
|
---|
95 |
|
---|
96 | printf ("And loading shared object neededobj2.so again\n");
|
---|
97 | obj2[1] = dlopen ("neededobj2.so", RTLD_LAZY);
|
---|
98 | if (obj2[1] == NULL)
|
---|
99 | {
|
---|
100 | printf ("%s\n", dlerror ());
|
---|
101 | exit (1);
|
---|
102 | }
|
---|
103 | errors += check_loaded_objects (loaded);
|
---|
104 |
|
---|
105 | printf ("Closing neededobj2.so for the first time\n");
|
---|
106 | dlclose (obj2[0]);
|
---|
107 | errors += check_loaded_objects (loaded);
|
---|
108 |
|
---|
109 | printf ("Closing neededobj3.so\n");
|
---|
110 | dlclose (obj3);
|
---|
111 | loaded[2] = NULL;
|
---|
112 | errors += check_loaded_objects (loaded);
|
---|
113 |
|
---|
114 | printf ("Closing neededobj2.so for the second time\n");
|
---|
115 | dlclose (obj2[1]);
|
---|
116 | loaded[0] = NULL;
|
---|
117 | loaded[1] = NULL;
|
---|
118 | errors += check_loaded_objects (loaded);
|
---|
119 |
|
---|
120 | if (errors != 0)
|
---|
121 | printf ("%d errors found\n", errors);
|
---|
122 | return errors;
|
---|
123 | }
|
---|