This source file includes following definitions.
- loader_callback
- lt_dlloader_add
- loader_dump_callback
- lt_dlloader_dump
- lt_dlloader_next
- lt_dlloader_get
- lt_dlloader_remove
- lt_dlloader_find
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #include "lt__private.h"
33 #include "lt_dlloader.h"
34
35 #define RETURN_SUCCESS 0
36 #define RETURN_FAILURE 1
37
38 static void * loader_callback (SList *item, void *userdata);
39
40
41
42 static SList *loaders = 0;
43
44
45
46
47
48 static void *
49 loader_callback (SList *item, void *userdata)
50 {
51 const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
52 const char * name = (const char *) userdata;
53
54 assert (vtable);
55
56 return STREQ (vtable->name, name) ? (void *) item : NULL;
57 }
58
59
60
61
62 int
63 lt_dlloader_add (const lt_dlvtable *vtable)
64 {
65 SList *item;
66
67 if ((vtable == 0)
68 || (vtable->module_open == 0)
69 || (vtable->module_close == 0)
70 || (vtable->find_sym == 0)
71 || ((vtable->priority != LT_DLLOADER_PREPEND) &&
72 (vtable->priority != LT_DLLOADER_APPEND)))
73 {
74 LT__SETERROR (INVALID_LOADER);
75 return RETURN_FAILURE;
76 }
77
78 item = slist_box (vtable);
79 if (!item)
80 {
81 (*lt__alloc_die) ();
82
83
84
85 return RETURN_FAILURE;
86 }
87
88 if (vtable->priority == LT_DLLOADER_PREPEND)
89 {
90 loaders = slist_cons (item, loaders);
91 }
92 else
93 {
94 assert (vtable->priority == LT_DLLOADER_APPEND);
95 loaders = slist_concat (loaders, item);
96 }
97
98 return RETURN_SUCCESS;
99 }
100
101 #ifdef LT_DEBUG_LOADERS
102 static void *
103 loader_dump_callback (SList *item, void *userdata)
104 {
105 const lt_dlvtable *vtable = (const lt_dlvtable *) item->userdata;
106 fprintf (stderr, ", %s", (vtable && vtable->name) ? vtable->name : "(null)");
107 return 0;
108 }
109
110 void
111 lt_dlloader_dump (void)
112 {
113 fprintf (stderr, "loaders: ");
114 if (!loaders)
115 {
116 fprintf (stderr, "(empty)");
117 }
118 else
119 {
120 const lt_dlvtable *head = (const lt_dlvtable *) loaders->userdata;
121 fprintf (stderr, "%s", (head && head->name) ? head->name : "(null)");
122 if (slist_tail (loaders))
123 slist_foreach (slist_tail (loaders), loader_dump_callback, NULL);
124 }
125 fprintf (stderr, "\n");
126 }
127 #endif
128
129
130
131 lt_dlloader
132 lt_dlloader_next (lt_dlloader loader)
133 {
134 SList *item = (SList *) loader;
135 return (lt_dlloader) (item ? item->next : loaders);
136 }
137
138
139
140 const lt_dlvtable *
141 lt_dlloader_get (lt_dlloader loader)
142 {
143 return (const lt_dlvtable *) (loader ? ((SList *) loader)->userdata : NULL);
144 }
145
146
147
148
149
150
151
152
153 lt_dlvtable *
154 lt_dlloader_remove (const char *name)
155 {
156 const lt_dlvtable * vtable = lt_dlloader_find (name);
157 static const char id_string[] = "lt_dlloader_remove";
158 lt_dlinterface_id iface;
159 lt_dlhandle handle = 0;
160 int in_use = 0;
161 int in_use_by_resident = 0;
162
163 if (!vtable)
164 {
165 LT__SETERROR (INVALID_LOADER);
166 return 0;
167 }
168
169
170 iface = lt_dlinterface_register (id_string, NULL);
171 while ((handle = lt_dlhandle_iterate (iface, handle)))
172 {
173 lt_dlhandle cur = handle;
174 if (cur->vtable == vtable)
175 {
176 in_use = 1;
177 if (lt_dlisresident (handle))
178 in_use_by_resident = 1;
179 }
180 }
181 lt_dlinterface_free (iface);
182 if (in_use)
183 {
184 if (!in_use_by_resident)
185 LT__SETERROR (REMOVE_LOADER);
186 return 0;
187 }
188
189
190 if (vtable && vtable->dlloader_exit)
191 {
192 if ((*vtable->dlloader_exit) (vtable->dlloader_data) != 0)
193 {
194
195
196
197 return 0;
198 }
199 }
200
201
202 return (lt_dlvtable *)
203 slist_unbox ((SList *) slist_remove (&loaders, loader_callback, (void *) name));
204 }
205
206
207 const lt_dlvtable *
208 lt_dlloader_find (const char *name)
209 {
210 return lt_dlloader_get (slist_find (loaders, loader_callback, (void *) name));
211 }