[3297] | 1 | //roardl.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
[3297] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[3297] | 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 | |
---|
[3367] | 38 | #if defined(ROAR_HAVE_LIBDL) && !defined(RTLD_NEXT) |
---|
| 39 | #define RTLD_NEXT ((void *) -1L) |
---|
| 40 | #endif |
---|
| 41 | |
---|
[3297] | 42 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 43 | static void * _roardl2ldl (struct roar_dl_lhandle * lhandle) { |
---|
| 44 | if ( lhandle == ROAR_DL_HANDLE_DEFAULT ) |
---|
| 45 | return RTLD_DEFAULT; |
---|
| 46 | |
---|
| 47 | if ( lhandle == ROAR_DL_HANDLE_NEXT ) |
---|
| 48 | return RTLD_NEXT; |
---|
| 49 | |
---|
| 50 | return lhandle->handle; |
---|
| 51 | } |
---|
| 52 | #endif |
---|
| 53 | |
---|
| 54 | struct roar_dl_lhandle * roar_dl_open(const char * filename, int flags, int ra_init) { |
---|
[3298] | 55 | struct roar_dl_lhandle * ret = NULL; |
---|
| 56 | |
---|
[3304] | 57 | // early errors just return. |
---|
| 58 | |
---|
| 59 | if ( flags != ROAR_DL_FLAG_DEFAUTS ) |
---|
| 60 | return NULL; |
---|
| 61 | |
---|
| 62 | if ( (ret = roar_mm_malloc(sizeof(struct roar_dl_lhandle))) == NULL ) |
---|
| 63 | return NULL; |
---|
| 64 | |
---|
| 65 | memset(ret, 0, sizeof(struct roar_dl_lhandle)); |
---|
| 66 | |
---|
| 67 | #if defined(ROAR_HAVE_LIBDL) |
---|
[3366] | 68 | flags = RTLD_NOW; |
---|
| 69 | |
---|
| 70 | #ifdef RTLD_DEEPBIND |
---|
| 71 | flags |= RTLD_DEEPBIND; |
---|
| 72 | #endif |
---|
| 73 | |
---|
| 74 | ret->handle = dlopen(filename, flags); |
---|
[3304] | 75 | |
---|
| 76 | if ( ret->handle == NULL ) { |
---|
| 77 | roar_mm_free(ret); |
---|
| 78 | return NULL; |
---|
| 79 | } |
---|
| 80 | #else |
---|
| 81 | roar_mm_free(ret); |
---|
| 82 | return NULL; |
---|
| 83 | #endif |
---|
| 84 | |
---|
[3298] | 85 | if ( ret == NULL ) |
---|
| 86 | return NULL; |
---|
| 87 | |
---|
| 88 | if ( ra_init ) { |
---|
| 89 | roar_dl_ra_init(ret, NULL); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return ret; |
---|
[3297] | 93 | } |
---|
| 94 | |
---|
| 95 | int roar_dl_close(struct roar_dl_lhandle * lhandle) { |
---|
[3304] | 96 | int ret = -1; |
---|
| 97 | |
---|
[3301] | 98 | if ( lhandle == ROAR_DL_HANDLE_DEFAULT ) |
---|
| 99 | return -1; |
---|
| 100 | if ( lhandle == ROAR_DL_HANDLE_NEXT ) |
---|
| 101 | return -1; |
---|
| 102 | |
---|
| 103 | if ( lhandle->lib != NULL && lhandle->lib->unload != NULL ) |
---|
| 104 | lhandle->lib->unload(lhandle->para, lhandle->lib); |
---|
| 105 | |
---|
[3297] | 106 | #if defined(ROAR_HAVE_LIBDL) |
---|
[3304] | 107 | ret = dlclose(_roardl2ldl(lhandle)); |
---|
[3297] | 108 | #else |
---|
[3304] | 109 | ret = -1; |
---|
[3297] | 110 | #endif |
---|
[3304] | 111 | |
---|
| 112 | roar_mm_free(lhandle); |
---|
| 113 | |
---|
| 114 | return ret; |
---|
[3297] | 115 | } |
---|
| 116 | |
---|
| 117 | void * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) { |
---|
| 118 | #if defined(ROAR_HAVE_LIBDL) |
---|
[3776] | 119 | void * ret = dlsym(_roardl2ldl(lhandle), sym); |
---|
| 120 | |
---|
| 121 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = %p", lhandle, sym, type, ret); |
---|
| 122 | |
---|
| 123 | return ret; |
---|
[3297] | 124 | #else |
---|
| 125 | return NULL; |
---|
| 126 | #endif |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | int roar_dl_ra_init(struct roar_dl_lhandle * lhandle, const char * prefix) { |
---|
| 130 | #define _SUFFIX "_roaraudio_library_init" |
---|
| 131 | char name[80] = _SUFFIX; |
---|
| 132 | struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para); |
---|
| 133 | struct roar_dl_libraryinst * lib; |
---|
[3300] | 134 | struct roar_dl_librarypara * para = NULL; |
---|
| 135 | int i; |
---|
[3297] | 136 | |
---|
[3305] | 137 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 138 | if ( prefix == NULL ) |
---|
| 139 | return -1; |
---|
| 140 | } else { |
---|
| 141 | para = lhandle->para; |
---|
| 142 | } |
---|
| 143 | |
---|
[3297] | 144 | |
---|
| 145 | if ( prefix != NULL ) { |
---|
[5008] | 146 | roar_mm_strscpy(name, "_"); |
---|
| 147 | roar_mm_strscat(name, prefix); |
---|
| 148 | roar_mm_strscat(name, _SUFFIX); |
---|
[3297] | 149 | } |
---|
| 150 | |
---|
[3307] | 151 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name); |
---|
| 152 | |
---|
[3297] | 153 | if ( (func = roar_dl_getsym(lhandle, name, -1)) == NULL ) |
---|
| 154 | return -1; |
---|
| 155 | |
---|
[3307] | 156 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func); |
---|
| 157 | |
---|
[3300] | 158 | lib = func(para); |
---|
[3297] | 159 | |
---|
| 160 | if ( lib == NULL ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
[3300] | 163 | if ( lib->version != ROAR_DL_LIBINST_VERSION ) |
---|
| 164 | return -1; |
---|
| 165 | |
---|
| 166 | if ( sizeof(struct roar_dl_libraryinst) > lib->len ) |
---|
| 167 | return -1; |
---|
| 168 | |
---|
[3361] | 169 | if ( !((void*)lhandle < (void*)128) ) { |
---|
| 170 | lhandle->lib = lib; |
---|
| 171 | } |
---|
| 172 | |
---|
[3300] | 173 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
| 174 | if ( lib->func[i] != NULL ) |
---|
| 175 | lib->func[i](para, lib); |
---|
| 176 | } |
---|
| 177 | |
---|
[3302] | 178 | return 0; |
---|
[3297] | 179 | } |
---|
| 180 | |
---|
[3364] | 181 | char * roar_dl_errstr(struct roar_dl_lhandle * lhandle) { |
---|
| 182 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 183 | return dlerror(); |
---|
| 184 | #else |
---|
| 185 | return NULL; |
---|
| 186 | #endif |
---|
| 187 | } |
---|
| 188 | |
---|
[3297] | 189 | //ll |
---|