1 | //roardl.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | #if defined(ROAR_HAVE_LIBDL) && !defined(RTLD_NEXT) |
---|
39 | #define RTLD_NEXT ((void *) -1L) |
---|
40 | #endif |
---|
41 | |
---|
42 | struct roar_dl_lhandle { |
---|
43 | struct roar_dl_librarypara * para; |
---|
44 | struct roar_dl_libraryinst * lib; |
---|
45 | struct { |
---|
46 | struct roar_error_state error_state; |
---|
47 | void * global_data; |
---|
48 | void * global_data_state; |
---|
49 | } context; |
---|
50 | #if defined(ROAR_HAVE_LIBDL) |
---|
51 | void * handle; |
---|
52 | #endif |
---|
53 | }; |
---|
54 | |
---|
55 | |
---|
56 | struct roar_dl_librarypara * roar_dl_para_new(const char * args, void * binargv, |
---|
57 | const char * appname, const char * abiversion) { |
---|
58 | struct roar_dl_librarypara * ret = roar_mm_malloc(sizeof(struct roar_dl_librarypara)); |
---|
59 | ssize_t argc; |
---|
60 | int err; |
---|
61 | |
---|
62 | if ( ret == NULL ) |
---|
63 | return NULL; |
---|
64 | |
---|
65 | memset(ret, 0, sizeof(struct roar_dl_librarypara)); |
---|
66 | |
---|
67 | ret->version = ROAR_DL_LIBPARA_VERSION; |
---|
68 | ret->len = sizeof(struct roar_dl_librarypara); |
---|
69 | ret->refc = 1; |
---|
70 | ret->argc = 0; |
---|
71 | ret->argv = NULL; |
---|
72 | ret->args_store = NULL; |
---|
73 | ret->binargv = binargv; |
---|
74 | ret->appname = appname; |
---|
75 | ret->abiversion = abiversion; |
---|
76 | |
---|
77 | if ( args != NULL ) { |
---|
78 | ret->args_store = roar_mm_strdup(args); |
---|
79 | if ( ret->args_store == NULL ) { |
---|
80 | err = roar_error; |
---|
81 | roar_mm_free(ret); |
---|
82 | roar_error = err; |
---|
83 | return NULL; |
---|
84 | } |
---|
85 | |
---|
86 | argc = roar_keyval_split(&(ret->argv), ret->args_store, NULL, NULL, 0); |
---|
87 | if ( argc == -1 ) { |
---|
88 | err = roar_error; |
---|
89 | roar_mm_free(ret->args_store); |
---|
90 | roar_mm_free(ret); |
---|
91 | roar_error = err; |
---|
92 | return NULL; |
---|
93 | } |
---|
94 | |
---|
95 | ret->argc = argc; |
---|
96 | } |
---|
97 | |
---|
98 | return ret; |
---|
99 | } |
---|
100 | |
---|
101 | int roar_dl_para_ref (struct roar_dl_librarypara * para) { |
---|
102 | if ( para == NULL ) { |
---|
103 | roar_err_set(ROAR_ERROR_FAULT); |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | para->refc++; |
---|
108 | |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | int roar_dl_para_unref (struct roar_dl_librarypara * para) { |
---|
113 | if ( para == NULL ) { |
---|
114 | roar_err_set(ROAR_ERROR_FAULT); |
---|
115 | return -1; |
---|
116 | } |
---|
117 | |
---|
118 | para->refc--; |
---|
119 | |
---|
120 | if ( para->refc == 0 ) { |
---|
121 | if ( para->args_store != NULL ) { |
---|
122 | roar_mm_free(para->args_store); |
---|
123 | roar_mm_free(para->argv); |
---|
124 | } |
---|
125 | roar_mm_free(para); |
---|
126 | } |
---|
127 | |
---|
128 | return 0; |
---|
129 | } |
---|
130 | int roar_dl_para_check_version (struct roar_dl_librarypara * para, |
---|
131 | const char * appname, const char * abiversion) { |
---|
132 | if ( para == NULL ) { |
---|
133 | roar_err_set(ROAR_ERROR_FAULT); |
---|
134 | return -1; |
---|
135 | } |
---|
136 | |
---|
137 | // check if both appnames are NULL or non-NULL. |
---|
138 | if ( (para->appname == NULL && appname != NULL) || (para->appname != NULL && appname == NULL) ) { |
---|
139 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
140 | return -1; |
---|
141 | } |
---|
142 | |
---|
143 | // check if the appname matches if given. |
---|
144 | if ( para->appname != NULL && !!strcmp(para->appname, appname) ) { |
---|
145 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
146 | return -1; |
---|
147 | } |
---|
148 | |
---|
149 | // check if both ABI versions are NULL or non-NULL. |
---|
150 | if ( (para->abiversion == NULL && abiversion != NULL) || (para->abiversion != NULL && abiversion == NULL) ) { |
---|
151 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
152 | return -1; |
---|
153 | } |
---|
154 | |
---|
155 | // check if the ABI versions matches if given. |
---|
156 | if ( para->abiversion != NULL && !!strcmp(para->abiversion, abiversion) ) { |
---|
157 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
158 | return -1; |
---|
159 | } |
---|
160 | |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | #if defined(ROAR_HAVE_LIBDL) |
---|
166 | static void * _roardl2ldl (struct roar_dl_lhandle * lhandle) { |
---|
167 | if ( lhandle == ROAR_DL_HANDLE_DEFAULT ) |
---|
168 | return RTLD_DEFAULT; |
---|
169 | |
---|
170 | if ( lhandle == ROAR_DL_HANDLE_NEXT ) |
---|
171 | return RTLD_NEXT; |
---|
172 | |
---|
173 | return lhandle->handle; |
---|
174 | } |
---|
175 | #endif |
---|
176 | |
---|
177 | struct roar_dl_lhandle * roar_dl_open(const char * filename, int flags, |
---|
178 | int ra_init, struct roar_dl_librarypara * para) { |
---|
179 | struct roar_dl_lhandle * ret = NULL; |
---|
180 | int err; |
---|
181 | |
---|
182 | // early errors just return. |
---|
183 | |
---|
184 | if ( flags != ROAR_DL_FLAG_DEFAUTS ) { |
---|
185 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
186 | return NULL; |
---|
187 | } |
---|
188 | |
---|
189 | if ( (ret = roar_mm_malloc(sizeof(struct roar_dl_lhandle))) == NULL ) |
---|
190 | return NULL; |
---|
191 | |
---|
192 | memset(ret, 0, sizeof(struct roar_dl_lhandle)); |
---|
193 | |
---|
194 | roar_err_initstore(&(ret->context.error_state)); |
---|
195 | |
---|
196 | #if defined(ROAR_HAVE_LIBDL) |
---|
197 | flags = RTLD_NOW; |
---|
198 | |
---|
199 | #ifdef RTLD_DEEPBIND |
---|
200 | flags |= RTLD_DEEPBIND; |
---|
201 | #endif |
---|
202 | |
---|
203 | ret->handle = dlopen(filename, flags); |
---|
204 | |
---|
205 | if ( ret->handle == NULL ) { |
---|
206 | roar_mm_free(ret); |
---|
207 | return NULL; |
---|
208 | } |
---|
209 | #else |
---|
210 | roar_mm_free(ret); |
---|
211 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
212 | return NULL; |
---|
213 | #endif |
---|
214 | |
---|
215 | ret->para = para; |
---|
216 | |
---|
217 | if ( ra_init ) { |
---|
218 | if ( roar_dl_ra_init(ret, NULL, para) == -1 ) { |
---|
219 | err = roar_error; |
---|
220 | ROAR_WARN("roar_dl_open(filename='%s', flags=%i, ra_init=%i, para=%p): Can not init RA lib: %s", filename, flags, ra_init, para, roar_error2str(err)); |
---|
221 | #if defined(ROAR_HAVE_LIBDL) |
---|
222 | dlclose(ret->handle); |
---|
223 | #endif |
---|
224 | roar_mm_free(ret); |
---|
225 | roar_error = err; |
---|
226 | return NULL; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | if ( para != NULL ) |
---|
231 | roar_dl_para_ref(para); |
---|
232 | |
---|
233 | return ret; |
---|
234 | } |
---|
235 | |
---|
236 | int roar_dl_close(struct roar_dl_lhandle * lhandle) { |
---|
237 | int ret = -1; |
---|
238 | |
---|
239 | if ( lhandle == ROAR_DL_HANDLE_DEFAULT || lhandle == ROAR_DL_HANDLE_NEXT ) { |
---|
240 | roar_err_set(ROAR_ERROR_BADFH); |
---|
241 | return -1; |
---|
242 | } |
---|
243 | |
---|
244 | if ( lhandle->lib != NULL && lhandle->lib->unload != NULL ) |
---|
245 | lhandle->lib->unload(lhandle->para, lhandle->lib); |
---|
246 | |
---|
247 | #if defined(ROAR_HAVE_LIBDL) |
---|
248 | ret = dlclose(_roardl2ldl(lhandle)); |
---|
249 | #else |
---|
250 | ret = -1; |
---|
251 | #endif |
---|
252 | |
---|
253 | if ( lhandle->context.global_data != NULL ) |
---|
254 | roar_mm_free(lhandle->context.global_data); |
---|
255 | |
---|
256 | if ( lhandle->para != NULL ) |
---|
257 | roar_dl_para_unref(lhandle->para); |
---|
258 | |
---|
259 | roar_mm_free(lhandle); |
---|
260 | |
---|
261 | return ret; |
---|
262 | } |
---|
263 | |
---|
264 | void * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) { |
---|
265 | #if defined(ROAR_HAVE_LIBDL) |
---|
266 | void * ret = dlsym(_roardl2ldl(lhandle), sym); |
---|
267 | |
---|
268 | (void)type; |
---|
269 | |
---|
270 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = %p", lhandle, sym, type, ret); |
---|
271 | |
---|
272 | return ret; |
---|
273 | #else |
---|
274 | return NULL; |
---|
275 | #endif |
---|
276 | } |
---|
277 | |
---|
278 | int roar_dl_ra_init(struct roar_dl_lhandle * lhandle, |
---|
279 | const char * prefix, |
---|
280 | struct roar_dl_librarypara * para) { |
---|
281 | #define _SUFFIX "_roaraudio_library_init" |
---|
282 | char name[80] = _SUFFIX; |
---|
283 | struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para); |
---|
284 | struct roar_dl_libraryinst * lib; |
---|
285 | void * global_data_state = NULL; |
---|
286 | int i; |
---|
287 | |
---|
288 | if ( (void*)lhandle < (void*)128 ) { |
---|
289 | if ( prefix == NULL ) |
---|
290 | return -1; |
---|
291 | } else { |
---|
292 | if ( para == NULL ) |
---|
293 | para = lhandle->para; |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | if ( prefix != NULL ) { |
---|
298 | roar_mm_strscpy(name, "_"); |
---|
299 | roar_mm_strscat(name, prefix); |
---|
300 | roar_mm_strscat(name, _SUFFIX); |
---|
301 | } |
---|
302 | |
---|
303 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name); |
---|
304 | |
---|
305 | if ( (func = roar_dl_getsym(lhandle, name, -1)) == NULL ) |
---|
306 | return -1; |
---|
307 | |
---|
308 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func); |
---|
309 | |
---|
310 | lib = func(para); |
---|
311 | |
---|
312 | if ( lib == NULL ) |
---|
313 | return -1; |
---|
314 | |
---|
315 | if ( lib->version != ROAR_DL_LIBINST_VERSION ) |
---|
316 | return -1; |
---|
317 | |
---|
318 | if ( sizeof(struct roar_dl_libraryinst) > lib->len ) |
---|
319 | return -1; |
---|
320 | |
---|
321 | if ( !((void*)lhandle < (void*)128) ) { |
---|
322 | lhandle->lib = lib; |
---|
323 | |
---|
324 | if ( lib->global_data_len ) { |
---|
325 | lhandle->context.global_data = roar_mm_malloc(lib->global_data_len); |
---|
326 | if ( lhandle->context.global_data == NULL ) |
---|
327 | return -1; |
---|
328 | |
---|
329 | if ( lib->global_data_init == NULL ) { |
---|
330 | memset(lhandle->context.global_data, 0, lib->global_data_len); |
---|
331 | } else { |
---|
332 | memcpy(lhandle->context.global_data, lib->global_data_init, lib->global_data_len); |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | if ( lib->global_data_pointer != NULL ) { |
---|
338 | global_data_state = *(lib->global_data_pointer); |
---|
339 | if ( (void*)lhandle < (void*)128 ) { |
---|
340 | *(lib->global_data_pointer) = lib->global_data_init; |
---|
341 | } else { |
---|
342 | *(lib->global_data_pointer) = lhandle->context.global_data; |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
347 | if ( lib->func[i] != NULL ) |
---|
348 | lib->func[i](para, lib); |
---|
349 | } |
---|
350 | |
---|
351 | if ( lib->global_data_pointer != NULL ) { |
---|
352 | if ( !((void*)lhandle < (void*)128) ) { |
---|
353 | *(lib->global_data_pointer) = global_data_state; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | return 0; |
---|
358 | } |
---|
359 | |
---|
360 | const char * roar_dl_errstr(struct roar_dl_lhandle * lhandle) { |
---|
361 | #if defined(ROAR_HAVE_LIBDL) |
---|
362 | (void)lhandle; |
---|
363 | return dlerror(); |
---|
364 | #else |
---|
365 | return NULL; |
---|
366 | #endif |
---|
367 | } |
---|
368 | |
---|
369 | struct roar_dl_librarypara * roar_dl_getpara(struct roar_dl_lhandle * lhandle) { |
---|
370 | if ( (void*)lhandle < (void*)128 ) { |
---|
371 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
372 | return NULL; |
---|
373 | } |
---|
374 | |
---|
375 | if ( lhandle->para == NULL ) { |
---|
376 | roar_err_set(ROAR_ERROR_NOENT); |
---|
377 | return NULL; |
---|
378 | } |
---|
379 | |
---|
380 | if ( roar_dl_para_ref(lhandle->para) == -1 ) |
---|
381 | return NULL; |
---|
382 | |
---|
383 | return lhandle->para; |
---|
384 | } |
---|
385 | |
---|
386 | const struct roar_dl_libraryname * roar_dl_getlibname(struct roar_dl_lhandle * lhandle) { |
---|
387 | if ( (void*)lhandle < (void*)128 ) { |
---|
388 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
389 | return NULL; |
---|
390 | } |
---|
391 | |
---|
392 | if ( lhandle->lib == NULL ) { |
---|
393 | roar_err_set(ROAR_ERROR_BADLIB); |
---|
394 | return NULL; |
---|
395 | } |
---|
396 | |
---|
397 | if ( lhandle->lib->libname == NULL ) { |
---|
398 | roar_err_set(ROAR_ERROR_NOENT); |
---|
399 | return NULL; |
---|
400 | } |
---|
401 | |
---|
402 | if ( lhandle->lib->libname->version != ROAR_DL_LIBNAME_VERSION ) { |
---|
403 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
404 | return NULL; |
---|
405 | } |
---|
406 | |
---|
407 | if ( lhandle->lib->libname->len != sizeof(struct roar_dl_libraryname) ) { |
---|
408 | roar_err_set(ROAR_ERROR_HOLE); |
---|
409 | return NULL; |
---|
410 | } |
---|
411 | |
---|
412 | return lhandle->lib->libname; |
---|
413 | } |
---|
414 | |
---|
415 | int roar_dl_context_restore(struct roar_dl_lhandle * lhandle) { |
---|
416 | struct roar_error_state error_state; |
---|
417 | |
---|
418 | if ( (void*)lhandle < (void*)128 ) { |
---|
419 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
420 | return -1; |
---|
421 | } |
---|
422 | |
---|
423 | roar_err_store(&error_state); |
---|
424 | roar_err_restore(&(lhandle->context.error_state)); |
---|
425 | lhandle->context.error_state = error_state; |
---|
426 | |
---|
427 | if ( lhandle->lib->global_data_pointer != NULL ) { |
---|
428 | lhandle->context.global_data_state = *(lhandle->lib->global_data_pointer); |
---|
429 | *(lhandle->lib->global_data_pointer) = lhandle->context.global_data; |
---|
430 | } |
---|
431 | |
---|
432 | return 0; |
---|
433 | } |
---|
434 | |
---|
435 | int roar_dl_context_store(struct roar_dl_lhandle * lhandle) { |
---|
436 | struct roar_error_state error_state; |
---|
437 | |
---|
438 | if ( (void*)lhandle < (void*)128 ) { |
---|
439 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
440 | return -1; |
---|
441 | } |
---|
442 | |
---|
443 | if ( lhandle->lib->global_data_pointer != NULL ) { |
---|
444 | *(lhandle->lib->global_data_pointer) = lhandle->context.global_data_state; |
---|
445 | } |
---|
446 | |
---|
447 | roar_err_store(&error_state); |
---|
448 | roar_err_restore(&(lhandle->context.error_state)); |
---|
449 | lhandle->context.error_state = error_state; |
---|
450 | |
---|
451 | return 0; |
---|
452 | } |
---|
453 | |
---|
454 | //ll |
---|