source: roaraudio/libroar/roardl.c @ 5335:dba934a2d1e0

Last change on this file since 5335:dba934a2d1e0 was 5335:dba934a2d1e0, checked in by phi, 12 years ago

cleanup and updates to roardl API

File size: 15.0 KB
Line 
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
42struct roar_dl_lhandle {
43 size_t refc; // currently unused.
44 int flags;
45 char * libname; // only used for ROAR_DL_FLAG_STATIC.
46 struct roar_dl_librarypara * para;
47 struct roar_dl_libraryinst * lib;
48 struct {
49  struct roar_error_state error_state;
50  void *  global_data;
51  void *  global_data_state;
52  struct roar_notify_core * notifycore;
53 } context;
54#if defined(ROAR_HAVE_LIBDL)
55 void * handle;
56#endif
57};
58
59
60struct roar_dl_librarypara * roar_dl_para_new(const char * args, void * binargv,
61                                              const char * appname, const char * abiversion) {
62 struct roar_dl_librarypara * ret = roar_mm_malloc(sizeof(struct roar_dl_librarypara));
63 ssize_t argc;
64 int err;
65
66 if ( ret == NULL )
67  return NULL;
68
69 memset(ret, 0, sizeof(struct roar_dl_librarypara));
70
71 ret->version    = ROAR_DL_LIBPARA_VERSION;
72 ret->len        = sizeof(struct roar_dl_librarypara);
73 ret->refc       = 1;
74 ret->argc       = 0;
75 ret->argv       = NULL;
76 ret->args_store = NULL;
77 ret->binargv    = binargv;
78 ret->appname    = appname;
79 ret->abiversion = abiversion;
80
81 if ( args != NULL ) {
82  ret->args_store = roar_mm_strdup(args);
83  if ( ret->args_store == NULL ) {
84   err = roar_error;
85   roar_mm_free(ret);
86   roar_error = err;
87   return NULL;
88  }
89
90  argc = roar_keyval_split(&(ret->argv), ret->args_store, NULL, NULL, 0);
91  if ( argc == -1 ) {
92   err = roar_error;
93   roar_mm_free(ret->args_store);
94   roar_mm_free(ret);
95   roar_error = err;
96   return NULL;
97  }
98
99  ret->argc = argc;
100 }
101
102 return ret;
103}
104
105int roar_dl_para_ref                    (struct roar_dl_librarypara * para) {
106 if ( para == NULL ) {
107  roar_err_set(ROAR_ERROR_FAULT);
108  return -1;
109 }
110
111 para->refc++;
112
113 return 0;
114}
115
116int roar_dl_para_unref                  (struct roar_dl_librarypara * para) {
117 if ( para == NULL ) {
118  roar_err_set(ROAR_ERROR_FAULT);
119  return -1;
120 }
121
122 para->refc--;
123
124 if ( para->refc )
125  return 0;
126
127 if ( para->notifycore != NULL )
128  roar_notify_core_unref(para->notifycore);
129
130 if ( para->args_store != NULL ) {
131  roar_mm_free(para->args_store);
132  roar_mm_free(para->argv);
133 }
134
135 roar_mm_free(para);
136
137 return 0;
138}
139int roar_dl_para_check_version          (struct roar_dl_librarypara * para,
140                                         const char * appname, const char * abiversion) {
141 if ( para == NULL ) {
142  roar_err_set(ROAR_ERROR_FAULT);
143  return -1;
144 }
145
146 // check if both appnames are NULL or non-NULL.
147 if ( (para->appname == NULL && appname != NULL) || (para->appname != NULL && appname == NULL) ) {
148  roar_err_set(ROAR_ERROR_BADVERSION);
149  return -1;
150 }
151
152 // check if the appname matches if given.
153 if ( para->appname != NULL && !!strcmp(para->appname, appname) ) {
154  roar_err_set(ROAR_ERROR_BADVERSION);
155  return -1;
156 }
157
158 // check if both ABI versions are NULL or non-NULL.
159 if ( (para->abiversion == NULL && abiversion != NULL) || (para->abiversion != NULL && abiversion == NULL) ) {
160  roar_err_set(ROAR_ERROR_BADVERSION);
161  return -1;
162 }
163
164 // check if the ABI versions matches if given.
165 if ( para->abiversion != NULL && !!strcmp(para->abiversion, abiversion) ) {
166  roar_err_set(ROAR_ERROR_BADVERSION);
167  return -1;
168 }
169
170 return 0;
171}
172
173
174#if defined(ROAR_HAVE_LIBDL)
175static void * _roardl2ldl (struct roar_dl_lhandle * lhandle) {
176 ROAR_DBG("_roardl2ldl(lhandle=%p) = ?", lhandle);
177
178 if ( lhandle == ROAR_DL_HANDLE_DEFAULT ) {
179  ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)RTLD_DEFAULT);
180  return RTLD_DEFAULT;
181 }
182
183 if ( lhandle == ROAR_DL_HANDLE_NEXT ) {
184  ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)RTLD_NEXT);
185  return RTLD_NEXT;
186 }
187
188 ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)(lhandle->handle));
189 return lhandle->handle;
190}
191#endif
192
193struct roar_dl_lhandle * roar_dl_open(const char * filename, int flags,
194                                      int ra_init, struct roar_dl_librarypara * para) {
195 struct roar_dl_lhandle * ret = NULL;
196#if defined(ROAR_HAVE_LIBDL)
197#ifdef RTLD_DEEPBIND
198 int libdl_flags = RTLD_NOW|RTLD_DEEPBIND;
199#else
200 int libdl_flags = RTLD_NOW;
201#endif
202#endif
203 int err;
204
205 if ( flags == ROAR_DL_FLAG_DEFAULTS )
206  flags = ROAR_DL_FLAG_NONE;
207
208 if ( (ret = roar_mm_malloc(sizeof(struct roar_dl_lhandle))) == NULL )
209  return NULL;
210
211 memset(ret, 0, sizeof(struct roar_dl_lhandle));
212
213 roar_err_initstore(&(ret->context.error_state));
214
215 ret->flags = flags;
216 ret->refc  = 1;
217
218 if ( flags & ROAR_DL_FLAG_STATIC ) {
219  if ( filename == NULL ) {
220   ret->libname = NULL;
221  } else {
222   ret->libname = roar_mm_strdup(filename);
223  }
224 } else {
225#if defined(ROAR_HAVE_LIBDL)
226  ret->handle = dlopen(filename, libdl_flags);
227
228  if ( ret->handle == NULL ) {
229   ROAR_DBG("roar_dl_open(filename='%s', flags=%i, ra_init=%i, para=%p): Can not load library: %s", filename, flags, ra_init, para, roar_dl_errstr(ret));
230   roar_mm_free(ret);
231   return NULL;
232  }
233#else
234  roar_mm_free(ret);
235  roar_err_set(ROAR_ERROR_NOSYS);
236  return NULL;
237#endif
238 }
239
240 ret->para = para;
241
242 if ( ra_init ) {
243  if ( roar_dl_ra_init(ret, NULL, para) == -1 ) {
244   err = roar_error;
245   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));
246#if defined(ROAR_HAVE_LIBDL)
247   if ( ret->handle != NULL )
248    dlclose(ret->handle);
249#endif
250   roar_mm_free(ret);
251   roar_error = err;
252   return NULL;
253  }
254 }
255
256 if ( para != NULL )
257  roar_dl_para_ref(para);
258
259 return ret;
260}
261
262int                      roar_dl_ref    (struct roar_dl_lhandle * lhandle) {
263 if ( (void*)lhandle < (void*)128 ) {
264  roar_err_set(ROAR_ERROR_BADFH);
265  return -1;
266 }
267
268 lhandle->refc++;
269
270 return 0;
271}
272
273int                      roar_dl_unref  (struct roar_dl_lhandle * lhandle) {
274 int ret = -1;
275
276 if ( (void*)lhandle < (void*)128 ) {
277  roar_err_set(ROAR_ERROR_BADFH);
278  return -1;
279 }
280
281 lhandle->refc++;
282
283 if ( lhandle->refc )
284  return 0;
285
286 if ( lhandle->lib != NULL && lhandle->lib->unload != NULL ) {
287  roar_dl_context_restore(lhandle);
288  lhandle->lib->unload(lhandle->para, lhandle->lib);
289  roar_dl_context_store(lhandle);
290 }
291
292#if defined(ROAR_HAVE_LIBDL)
293 if ( lhandle->handle == NULL ) {
294  ret = 0;
295 } else {
296  ret = dlclose(_roardl2ldl(lhandle));
297 }
298#else
299 ret = -1;
300#endif
301
302 if ( lhandle->context.global_data != NULL )
303  roar_mm_free(lhandle->context.global_data);
304
305 if ( lhandle->para != NULL )
306  roar_dl_para_unref(lhandle->para);
307
308 if ( lhandle->libname != NULL )
309  roar_mm_free(lhandle->libname);
310
311 roar_mm_free(lhandle);
312
313 return ret;
314}
315
316void                   * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) {
317#if defined(ROAR_HAVE_LIBDL)
318 void * ret = dlsym(_roardl2ldl(lhandle), sym);
319
320 (void)type;
321
322 ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i): errno=%s, dlerror()='%s'", lhandle, sym, type, ret, strerror(errno), dlerror());
323 ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = %p", lhandle, sym, type, ret);
324
325 return ret;
326#else
327 ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = NULL // errno=NOSYS", lhandle, sym, type, ret);
328 roar_err_set(ROAR_ERROR_NOSYS);
329 return NULL;
330#endif
331}
332
333int                      roar_dl_ra_init(struct roar_dl_lhandle * lhandle,
334                                         const char * prefix,
335                                         struct roar_dl_librarypara * para) {
336#define _SUFFIX "_roaraudio_library_init"
337 char name[80] = _SUFFIX;
338 struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para);
339 struct roar_dl_libraryinst * lib;
340 struct roar_dl_lhandle     * getsymhandle = lhandle;
341 void * global_data_state = NULL;
342 int i;
343
344 if ( (void*)lhandle < (void*)128 ) {
345  if ( prefix == NULL )
346   return -1;
347 } else {
348  if ( prefix == NULL )
349   prefix = lhandle->libname;
350
351  if ( para == NULL )
352   para = lhandle->para;
353
354  if ( lhandle->flags & ROAR_DL_FLAG_STATIC )
355   getsymhandle = ROAR_DL_HANDLE_DEFAULT;
356 }
357
358
359 if ( prefix != NULL ) {
360  roar_mm_strscpy(name, "_");
361  roar_mm_strscat(name, prefix);
362  roar_mm_strscat(name, _SUFFIX);
363 }
364
365 ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name);
366
367 if ( (func = roar_dl_getsym(getsymhandle, name, -1)) == NULL ) {
368  ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s') = -1", lhandle, prefix);
369  return -1;
370 }
371
372 ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func);
373
374 lib = func(para);
375
376 if ( lib == NULL )
377  return -1;
378
379 if ( lib->version != ROAR_DL_LIBINST_VERSION )
380  return -1;
381
382 if ( sizeof(struct roar_dl_libraryinst) > lib->len )
383  return -1;
384
385 if ( !((void*)lhandle < (void*)128) ) {
386  lhandle->lib = lib;
387
388  if ( lib->global_data_len ) {
389   lhandle->context.global_data = roar_mm_malloc(lib->global_data_len);
390   if ( lhandle->context.global_data == NULL )
391    return -1;
392
393   if ( lib->global_data_init == NULL ) {
394    memset(lhandle->context.global_data, 0, lib->global_data_len);
395   } else {
396    memcpy(lhandle->context.global_data, lib->global_data_init, lib->global_data_len);
397   }
398  }
399 }
400
401 if ( lib->global_data_pointer != NULL ) {
402  global_data_state = *(lib->global_data_pointer);
403  if ( (void*)lhandle < (void*)128 ) {
404   *(lib->global_data_pointer) = lib->global_data_init;
405  } else {
406   *(lib->global_data_pointer) = lhandle->context.global_data;
407  }
408 }
409
410 for (i = 0; i < ROAR_DL_FN_MAX; i++) {
411  if ( lib->func[i] != NULL )
412   lib->func[i](para, lib);
413 }
414
415 if ( lib->global_data_pointer != NULL ) {
416  if ( !((void*)lhandle < (void*)128) ) {
417   *(lib->global_data_pointer) = global_data_state;
418  }
419 }
420
421 return 0;
422}
423
424const char * roar_dl_errstr(struct roar_dl_lhandle * lhandle) {
425#if defined(ROAR_HAVE_LIBDL)
426 (void)lhandle;
427 return dlerror();
428#else
429 return NULL;
430#endif
431}
432
433struct roar_dl_librarypara       * roar_dl_getpara(struct roar_dl_lhandle * lhandle) {
434 if ( (void*)lhandle < (void*)128 ) {
435  roar_err_set(ROAR_ERROR_NOTSUP);
436  return NULL;
437 }
438
439 if ( lhandle->para == NULL ) {
440  roar_err_set(ROAR_ERROR_NOENT);
441  return NULL;
442 }
443
444 if ( roar_dl_para_ref(lhandle->para) == -1 )
445  return NULL;
446
447 return lhandle->para;
448}
449
450const struct roar_dl_libraryname * roar_dl_getlibname(struct roar_dl_lhandle * lhandle) {
451 if ( (void*)lhandle < (void*)128 ) {
452  roar_err_set(ROAR_ERROR_NOTSUP);
453  return NULL;
454 }
455
456 if ( lhandle->lib == NULL ) {
457  roar_err_set(ROAR_ERROR_BADLIB);
458  return NULL;
459 }
460
461 if ( lhandle->lib->libname == NULL ) {
462  roar_err_set(ROAR_ERROR_NOENT);
463  return NULL;
464 }
465
466 if ( lhandle->lib->libname->version != ROAR_DL_LIBNAME_VERSION ) {
467  roar_err_set(ROAR_ERROR_BADVERSION);
468  return NULL;
469 }
470
471 if ( lhandle->lib->libname->len != sizeof(struct roar_dl_libraryname) ) {
472  roar_err_set(ROAR_ERROR_HOLE);
473  return NULL;
474 }
475
476 return lhandle->lib->libname;
477}
478
479int                      roar_dl_context_restore(struct roar_dl_lhandle * lhandle) {
480 struct roar_error_state error_state;
481
482 ROAR_DBG("roar_dl_context_restore(lhandle=%p) = ?", lhandle);
483
484 if ( (void*)lhandle < (void*)128 ) {
485  ROAR_DBG("roar_dl_context_restore(lhandle=%p) = -1 // errno=NOTSUP", lhandle);
486  roar_err_set(ROAR_ERROR_NOTSUP);
487  return -1;
488 }
489
490 roar_err_store(&error_state);
491 roar_err_restore(&(lhandle->context.error_state));
492 lhandle->context.error_state = error_state;
493
494 if ( lhandle->lib->global_data_pointer != NULL ) {
495  ROAR_DBG("roar_dl_context_restore(lhandle=%p): gptr(%p): %p -> %p", lhandle,
496           lhandle->lib->global_data_pointer, *(lhandle->lib->global_data_pointer), lhandle->context.global_data);
497  lhandle->context.global_data_state = *(lhandle->lib->global_data_pointer);
498  *(lhandle->lib->global_data_pointer) = lhandle->context.global_data;
499 }
500
501 if ( lhandle->para->notifycore != NULL )
502  lhandle->context.notifycore = roar_notify_core_swap_global(lhandle->para->notifycore);
503
504 ROAR_DBG("roar_dl_context_restore(lhandle=%p) = 0", lhandle);
505 return 0;
506}
507
508int                      roar_dl_context_store(struct roar_dl_lhandle * lhandle) {
509 struct roar_error_state error_state;
510
511 ROAR_DBG("roar_dl_context_store(lhandle=%p) = ?", lhandle);
512
513 if ( (void*)lhandle < (void*)128 ) {
514  ROAR_DBG("roar_dl_context_store(lhandle=%p) = -1 // errno=NOTSUP", lhandle);
515  roar_err_set(ROAR_ERROR_NOTSUP);
516  return -1;
517 }
518
519 if ( lhandle->para->notifycore != NULL ) {
520  roar_notify_core_unref(roar_notify_core_swap_global(lhandle->context.notifycore));
521  roar_notify_core_unref(lhandle->context.notifycore);
522  lhandle->context.notifycore = NULL;
523 }
524
525 if ( lhandle->lib->global_data_pointer != NULL ) {
526  ROAR_DBG("roar_dl_context_store(lhandle=%p): gptr(%p): %p -> %p", lhandle,
527           lhandle->lib->global_data_pointer, *(lhandle->lib->global_data_pointer), lhandle->context.global_data_state);
528  *(lhandle->lib->global_data_pointer) = lhandle->context.global_data_state;
529 }
530
531 roar_err_store(&error_state);
532 roar_err_restore(&(lhandle->context.error_state));
533 lhandle->context.error_state = error_state;
534
535 ROAR_DBG("roar_dl_context_store(lhandle=%p) = 0", lhandle);
536 return 0;
537}
538
539int                      roar_dl_appsched_trigger(struct roar_dl_lhandle * lhandle, enum roar_dl_appsched_trigger trigger) {
540 int (*func)  (struct roar_dl_librarypara * para) = NULL;
541 int ret;
542
543 if ( (void*)lhandle < (void*)128 ) {
544  roar_err_set(ROAR_ERROR_NOTSUP);
545  return -1;
546 }
547
548 if ( lhandle->lib->appsched == NULL ) {
549  roar_err_set(ROAR_ERROR_NOENT);
550  return -1;
551 }
552
553 switch (trigger) {
554#define _trig(lname,uname) \
555  case ROAR_DL_APPSCHED_ ## uname :             \
556    func = lhandle->lib->appsched->lname;       \
557   break;
558  _trig(init, INIT);
559  _trig(free, FREE);
560  _trig(update, UPDATE);
561  _trig(tick, TICK);
562  _trig(wait, WAIT);
563// use ifndef here so warnings of unhandled enum values will be shown in DEBUG mode.
564#ifndef DEBUG
565  default:
566    roar_err_set(ROAR_ERROR_BADRQC);
567    return -1;
568   break;
569#endif
570 }
571
572 if ( func == NULL ) {
573  roar_err_set(ROAR_ERROR_NOENT);
574  return -1;
575 }
576
577 roar_dl_context_restore(lhandle);
578 ret = func(lhandle->para);
579 roar_dl_context_store(lhandle);
580 return ret;
581}
582
583//ll
Note: See TracBrowser for help on using the repository browser.