[3297] | 1 | //roardl.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
[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 | |
---|
[5427] | 38 | #ifdef ROAR_HAVE_H_DIRENT |
---|
| 39 | #include <dirent.h> |
---|
| 40 | #endif |
---|
| 41 | |
---|
[3367] | 42 | #if defined(ROAR_HAVE_LIBDL) && !defined(RTLD_NEXT) |
---|
| 43 | #define RTLD_NEXT ((void *) -1L) |
---|
| 44 | #endif |
---|
| 45 | |
---|
[5275] | 46 | struct roar_dl_lhandle { |
---|
[5317] | 47 | size_t refc; // currently unused. |
---|
| 48 | int flags; |
---|
| 49 | char * libname; // only used for ROAR_DL_FLAG_STATIC. |
---|
[5275] | 50 | struct roar_dl_librarypara * para; |
---|
| 51 | struct roar_dl_libraryinst * lib; |
---|
[5312] | 52 | struct { |
---|
| 53 | struct roar_error_state error_state; |
---|
| 54 | void * global_data; |
---|
| 55 | void * global_data_state; |
---|
[5317] | 56 | struct roar_notify_core * notifycore; |
---|
[5312] | 57 | } context; |
---|
[5431] | 58 | int ra_inited; |
---|
[5275] | 59 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 60 | void * handle; |
---|
[5481] | 61 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 62 | HMODULE handle; |
---|
[5275] | 63 | #endif |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | struct roar_dl_librarypara * roar_dl_para_new(const char * args, void * binargv, |
---|
| 68 | const char * appname, const char * abiversion) { |
---|
| 69 | struct roar_dl_librarypara * ret = roar_mm_malloc(sizeof(struct roar_dl_librarypara)); |
---|
| 70 | ssize_t argc; |
---|
| 71 | int err; |
---|
| 72 | |
---|
| 73 | if ( ret == NULL ) |
---|
| 74 | return NULL; |
---|
| 75 | |
---|
| 76 | memset(ret, 0, sizeof(struct roar_dl_librarypara)); |
---|
| 77 | |
---|
| 78 | ret->version = ROAR_DL_LIBPARA_VERSION; |
---|
| 79 | ret->len = sizeof(struct roar_dl_librarypara); |
---|
| 80 | ret->refc = 1; |
---|
| 81 | ret->argc = 0; |
---|
| 82 | ret->argv = NULL; |
---|
| 83 | ret->args_store = NULL; |
---|
| 84 | ret->binargv = binargv; |
---|
| 85 | ret->appname = appname; |
---|
| 86 | ret->abiversion = abiversion; |
---|
| 87 | |
---|
| 88 | if ( args != NULL ) { |
---|
| 89 | ret->args_store = roar_mm_strdup(args); |
---|
| 90 | if ( ret->args_store == NULL ) { |
---|
| 91 | err = roar_error; |
---|
| 92 | roar_mm_free(ret); |
---|
| 93 | roar_error = err; |
---|
| 94 | return NULL; |
---|
| 95 | } |
---|
| 96 | |
---|
[5482] | 97 | argc = roar_keyval_split(&(ret->argv), ret->args_store, NULL, NULL, 1); |
---|
[5275] | 98 | if ( argc == -1 ) { |
---|
| 99 | err = roar_error; |
---|
| 100 | roar_mm_free(ret->args_store); |
---|
| 101 | roar_mm_free(ret); |
---|
| 102 | roar_error = err; |
---|
| 103 | return NULL; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | ret->argc = argc; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | return ret; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | int roar_dl_para_ref (struct roar_dl_librarypara * para) { |
---|
| 113 | if ( para == NULL ) { |
---|
[5438] | 114 | ROAR_DBG("roar_dl_para_ref(para=%p) = -1 // error=FAULT", para); |
---|
[5275] | 115 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 116 | return -1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | para->refc++; |
---|
| 120 | |
---|
[5438] | 121 | ROAR_DBG("roar_dl_para_ref(para=%p) = 0", para); |
---|
[5275] | 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int roar_dl_para_unref (struct roar_dl_librarypara * para) { |
---|
| 126 | if ( para == NULL ) { |
---|
[5438] | 127 | ROAR_DBG("roar_dl_para_unref(para=%p) = -1 // error=FAULT", para); |
---|
[5275] | 128 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | para->refc--; |
---|
| 133 | |
---|
[5438] | 134 | if ( para->refc ) { |
---|
| 135 | ROAR_DBG("roar_dl_para_unref(para=%p) = 0", para); |
---|
[5317] | 136 | return 0; |
---|
[5438] | 137 | } |
---|
[5317] | 138 | |
---|
| 139 | if ( para->notifycore != NULL ) |
---|
| 140 | roar_notify_core_unref(para->notifycore); |
---|
| 141 | |
---|
| 142 | if ( para->args_store != NULL ) { |
---|
| 143 | roar_mm_free(para->args_store); |
---|
| 144 | roar_mm_free(para->argv); |
---|
[5275] | 145 | } |
---|
| 146 | |
---|
[5317] | 147 | roar_mm_free(para); |
---|
| 148 | |
---|
[5438] | 149 | ROAR_DBG("roar_dl_para_unref(para=%p) = 0", para); |
---|
[5275] | 150 | return 0; |
---|
| 151 | } |
---|
| 152 | int roar_dl_para_check_version (struct roar_dl_librarypara * para, |
---|
| 153 | const char * appname, const char * abiversion) { |
---|
| 154 | if ( para == NULL ) { |
---|
| 155 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 156 | return -1; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | // check if both appnames are NULL or non-NULL. |
---|
| 160 | if ( (para->appname == NULL && appname != NULL) || (para->appname != NULL && appname == NULL) ) { |
---|
[5371] | 161 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
[5275] | 162 | return -1; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | // check if the appname matches if given. |
---|
| 166 | if ( para->appname != NULL && !!strcmp(para->appname, appname) ) { |
---|
[5371] | 167 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
[5275] | 168 | return -1; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | // check if both ABI versions are NULL or non-NULL. |
---|
| 172 | if ( (para->abiversion == NULL && abiversion != NULL) || (para->abiversion != NULL && abiversion == NULL) ) { |
---|
| 173 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 174 | return -1; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | // check if the ABI versions matches if given. |
---|
| 178 | if ( para->abiversion != NULL && !!strcmp(para->abiversion, abiversion) ) { |
---|
| 179 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 180 | return -1; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return 0; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
[3297] | 187 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 188 | static void * _roardl2ldl (struct roar_dl_lhandle * lhandle) { |
---|
[5317] | 189 | ROAR_DBG("_roardl2ldl(lhandle=%p) = ?", lhandle); |
---|
| 190 | |
---|
| 191 | if ( lhandle == ROAR_DL_HANDLE_DEFAULT ) { |
---|
| 192 | ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)RTLD_DEFAULT); |
---|
[3297] | 193 | return RTLD_DEFAULT; |
---|
[5317] | 194 | } |
---|
[3297] | 195 | |
---|
[5317] | 196 | if ( lhandle == ROAR_DL_HANDLE_NEXT ) { |
---|
| 197 | ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)RTLD_NEXT); |
---|
[3297] | 198 | return RTLD_NEXT; |
---|
[5317] | 199 | } |
---|
[3297] | 200 | |
---|
[5317] | 201 | ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)(lhandle->handle)); |
---|
[3297] | 202 | return lhandle->handle; |
---|
| 203 | } |
---|
[5481] | 204 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 205 | static HMODULE _roardl2winhandle(struct roar_dl_lhandle * lhandle) { |
---|
| 206 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 207 | return NULL; |
---|
| 208 | } |
---|
| 209 | return lhandle->handle; |
---|
| 210 | } |
---|
[3297] | 211 | #endif |
---|
| 212 | |
---|
[5427] | 213 | #ifdef ROAR_HAVE_H_DIRENT |
---|
| 214 | // pvn = prefix, host vendor, host name |
---|
| 215 | static struct roar_dl_lhandle * _load_from_path_pvn(const char * name, |
---|
| 216 | int flags, int ra_init, |
---|
| 217 | struct roar_dl_librarypara * para, |
---|
| 218 | const char * prefix, |
---|
| 219 | const char * hostvendor, |
---|
| 220 | const char * hostname) { |
---|
| 221 | struct roar_dl_lhandle * ret = NULL; |
---|
| 222 | int i, j; |
---|
| 223 | char path[1024]; |
---|
| 224 | const char * path_format[] = {"%s/%s/%s", "%s/%s/universal", "%s/universal/universal"}; |
---|
| 225 | DIR * dir; |
---|
| 226 | struct dirent * dirent; |
---|
| 227 | char file[1024]; |
---|
| 228 | const char * file_format[] = {"%s/%s/%s", "%s/%s/%s" ROAR_SHARED_SUFFIX, "%s/%s/lib%s" ROAR_SHARED_SUFFIX}; |
---|
| 229 | // cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para); |
---|
| 230 | //#vars: $PREFIX_PLUGINS, $hostvendor, $hostname, $name |
---|
| 231 | //#search order: $PREFIX_PLUGINS/{$hostvendor/{$hostname,universal},universal/universal}/*/{,lib}$name.so |
---|
| 232 | |
---|
| 233 | for (i = 0; i < 3; i++) { |
---|
| 234 | snprintf(path, sizeof(path), path_format[i], prefix, hostvendor, hostname); |
---|
| 235 | dir = opendir(path); |
---|
| 236 | if ( dir == NULL ) |
---|
| 237 | continue; |
---|
| 238 | |
---|
| 239 | while ((dirent = readdir(dir)) != NULL) { |
---|
| 240 | if ( !strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..") ) |
---|
| 241 | continue; |
---|
| 242 | |
---|
| 243 | for (j = 0; j < 3; j++) { |
---|
| 244 | snprintf(file, sizeof(file), file_format[j], path, dirent->d_name, name); |
---|
| 245 | ret = roar_dl_open(file, flags, ra_init, para); |
---|
| 246 | if ( ret != NULL ) { |
---|
| 247 | closedir(dir); |
---|
| 248 | return ret; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | closedir(dir); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | return NULL; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | static struct roar_dl_lhandle * _load_from_path(const char * name, int flags, int ra_init, |
---|
| 260 | struct roar_dl_librarypara * para) { |
---|
| 261 | struct roar_dl_lhandle * ret = NULL; |
---|
| 262 | char * host = NULL; |
---|
| 263 | char * hostvendor_buffer = NULL; |
---|
| 264 | char * hostvendor = NULL; |
---|
| 265 | char * hostname = NULL; |
---|
| 266 | char * c, * d; |
---|
| 267 | |
---|
| 268 | if ( para != NULL && para->appname != NULL && para->appname[0] != 0 ) { |
---|
| 269 | host = roar_mm_strdup(para->appname); |
---|
| 270 | |
---|
| 271 | // if we are out of memory we will likely not pass the rest, so just return in error. |
---|
| 272 | if ( host == NULL ) |
---|
| 273 | return NULL; |
---|
| 274 | |
---|
| 275 | hostname = host; |
---|
| 276 | c = strstr(host, "<"); |
---|
| 277 | if ( c != NULL ) { |
---|
| 278 | while (c[-1] == ' ') c--; |
---|
| 279 | *c = 0; |
---|
| 280 | c++; |
---|
| 281 | while (*c == ' ' || *c == '<') c++; |
---|
| 282 | if ( *c ) { |
---|
| 283 | d = strstr(c, ">"); |
---|
| 284 | if ( d != NULL ) |
---|
| 285 | *d = 0; |
---|
| 286 | |
---|
| 287 | d = strstr(c, "/"); |
---|
| 288 | if ( d != NULL ) { |
---|
| 289 | *d = '-'; |
---|
| 290 | hostvendor = c; |
---|
| 291 | } else { |
---|
| 292 | hostvendor_buffer = roar_mm_malloc(roar_mm_strlen(c) + 1 /* tailing \0 */ + 6 /* "unreg-" */); |
---|
| 293 | |
---|
| 294 | // see above |
---|
| 295 | if ( hostvendor_buffer == NULL ) { |
---|
| 296 | roar_mm_free(host); |
---|
| 297 | return NULL; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | strcpy(hostvendor_buffer, "unreg-"); |
---|
| 301 | strcat(hostvendor_buffer, c); |
---|
| 302 | hostvendor = hostvendor_buffer; |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | if ( hostvendor == NULL ) |
---|
| 309 | hostvendor = "universal"; |
---|
| 310 | |
---|
| 311 | if ( hostname == NULL ) |
---|
| 312 | hostname = "universal"; |
---|
| 313 | |
---|
| 314 | ret = _load_from_path_pvn(name, flags, ra_init, para, ROAR_PREFIX_PLUGINS, hostvendor, hostname); |
---|
| 315 | |
---|
| 316 | if ( host != NULL ) |
---|
| 317 | roar_mm_free(host); |
---|
| 318 | if ( hostvendor_buffer != NULL ) |
---|
| 319 | roar_mm_free(hostvendor_buffer); |
---|
| 320 | return ret; |
---|
| 321 | } |
---|
| 322 | #endif |
---|
| 323 | |
---|
| 324 | static struct roar_dl_lhandle * _roar_dl_open_pluginpath(const char * filename, int flags, |
---|
| 325 | int ra_init, struct roar_dl_librarypara * para) { |
---|
| 326 | flags |= ROAR_DL_FLAG_PLUGINPATH; |
---|
| 327 | flags -= ROAR_DL_FLAG_PLUGINPATH; |
---|
| 328 | |
---|
| 329 | #ifdef ROAR_HAVE_H_DIRENT |
---|
[5430] | 330 | // do normal open for everything with a path name. |
---|
| 331 | if ( strstr(filename, "/") != NULL ) |
---|
| 332 | return roar_dl_open(filename, flags, ra_init, para); |
---|
| 333 | |
---|
[5427] | 334 | return _load_from_path(filename, flags, ra_init, para); |
---|
| 335 | #else |
---|
| 336 | // fall back to system open function. |
---|
| 337 | return roar_dl_open(filename, flags, ra_init, para); |
---|
| 338 | #endif |
---|
| 339 | } |
---|
| 340 | |
---|
[5275] | 341 | struct roar_dl_lhandle * roar_dl_open(const char * filename, int flags, |
---|
| 342 | int ra_init, struct roar_dl_librarypara * para) { |
---|
[3298] | 343 | struct roar_dl_lhandle * ret = NULL; |
---|
[5317] | 344 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 345 | #ifdef RTLD_DEEPBIND |
---|
[5344] | 346 | int libdl_flags = RTLD_DEEPBIND; |
---|
[5317] | 347 | #else |
---|
[5344] | 348 | int libdl_flags = 0; |
---|
[5317] | 349 | #endif |
---|
| 350 | #endif |
---|
[5275] | 351 | int err; |
---|
[3298] | 352 | |
---|
[5427] | 353 | switch (flags) { |
---|
| 354 | case ROAR_DL_FLAG_DEFAULTS: flags = ROAR_DL_FLAG_NONE; break; |
---|
| 355 | case ROAR_DL_FLAG_PLUGIN: flags = ROAR_DL_FLAG_PLUGINPATH; break; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | if ( flags & ROAR_DL_FLAG_PLUGINPATH ) |
---|
| 359 | return _roar_dl_open_pluginpath(filename, flags, ra_init, para); |
---|
[3304] | 360 | |
---|
[5344] | 361 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 362 | if ( flags & ROAR_DL_FLAG_LAZY ) { |
---|
| 363 | libdl_flags |= RTLD_LAZY; |
---|
| 364 | } else { |
---|
| 365 | libdl_flags |= RTLD_NOW; |
---|
| 366 | } |
---|
| 367 | #endif |
---|
| 368 | |
---|
[3304] | 369 | if ( (ret = roar_mm_malloc(sizeof(struct roar_dl_lhandle))) == NULL ) |
---|
| 370 | return NULL; |
---|
| 371 | |
---|
| 372 | memset(ret, 0, sizeof(struct roar_dl_lhandle)); |
---|
| 373 | |
---|
[5312] | 374 | roar_err_initstore(&(ret->context.error_state)); |
---|
| 375 | |
---|
[5317] | 376 | ret->flags = flags; |
---|
[5321] | 377 | ret->refc = 1; |
---|
[3366] | 378 | |
---|
[5317] | 379 | if ( flags & ROAR_DL_FLAG_STATIC ) { |
---|
| 380 | if ( filename == NULL ) { |
---|
| 381 | ret->libname = NULL; |
---|
| 382 | } else { |
---|
| 383 | ret->libname = roar_mm_strdup(filename); |
---|
| 384 | } |
---|
| 385 | } else { |
---|
| 386 | #if defined(ROAR_HAVE_LIBDL) |
---|
| 387 | ret->handle = dlopen(filename, libdl_flags); |
---|
[3304] | 388 | |
---|
[5317] | 389 | if ( ret->handle == NULL ) { |
---|
| 390 | 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)); |
---|
| 391 | roar_mm_free(ret); |
---|
| 392 | return NULL; |
---|
| 393 | } |
---|
[5481] | 394 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 395 | ret->handle = LoadLibrary(filename); |
---|
| 396 | |
---|
| 397 | if ( ret->handle == NULL ) { |
---|
| 398 | roar_mm_free(ret); |
---|
| 399 | return NULL; |
---|
| 400 | } |
---|
[3304] | 401 | #else |
---|
[5317] | 402 | roar_mm_free(ret); |
---|
| 403 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 404 | return NULL; |
---|
[3304] | 405 | #endif |
---|
[5317] | 406 | } |
---|
[3304] | 407 | |
---|
[5275] | 408 | ret->para = para; |
---|
[3298] | 409 | |
---|
| 410 | if ( ra_init ) { |
---|
[5275] | 411 | if ( roar_dl_ra_init(ret, NULL, para) == -1 ) { |
---|
| 412 | err = roar_error; |
---|
| 413 | 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)); |
---|
| 414 | #if defined(ROAR_HAVE_LIBDL) |
---|
[5317] | 415 | if ( ret->handle != NULL ) |
---|
| 416 | dlclose(ret->handle); |
---|
[5481] | 417 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 418 | if ( ret->handle != NULL ) |
---|
| 419 | FreeLibrary(ret->handle); |
---|
[5275] | 420 | #endif |
---|
| 421 | roar_mm_free(ret); |
---|
| 422 | roar_error = err; |
---|
| 423 | return NULL; |
---|
| 424 | } |
---|
[3298] | 425 | } |
---|
| 426 | |
---|
[5275] | 427 | if ( para != NULL ) |
---|
| 428 | roar_dl_para_ref(para); |
---|
| 429 | |
---|
[3298] | 430 | return ret; |
---|
[3297] | 431 | } |
---|
| 432 | |
---|
[5321] | 433 | int roar_dl_ref (struct roar_dl_lhandle * lhandle) { |
---|
| 434 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5438] | 435 | ROAR_DBG("roar_dl_ref(lhandle=%p) = -1 // error=BADFH", lhandle); |
---|
[5275] | 436 | roar_err_set(ROAR_ERROR_BADFH); |
---|
[3301] | 437 | return -1; |
---|
[5275] | 438 | } |
---|
[3301] | 439 | |
---|
[5321] | 440 | lhandle->refc++; |
---|
| 441 | |
---|
[5438] | 442 | ROAR_DBG("roar_dl_ref(lhandle=%p) = 0", lhandle); |
---|
[5321] | 443 | return 0; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | int roar_dl_unref (struct roar_dl_lhandle * lhandle) { |
---|
| 447 | int ret = -1; |
---|
| 448 | |
---|
| 449 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5438] | 450 | ROAR_DBG("roar_dl_unref(lhandle=%p) = -1 // error=BADFH", lhandle); |
---|
[5321] | 451 | roar_err_set(ROAR_ERROR_BADFH); |
---|
| 452 | return -1; |
---|
| 453 | } |
---|
| 454 | |
---|
[5438] | 455 | lhandle->refc--; |
---|
[5321] | 456 | |
---|
[5438] | 457 | if ( lhandle->refc ) { |
---|
| 458 | ROAR_DBG("roar_dl_unref(lhandle=%p) = 0", lhandle); |
---|
[5321] | 459 | return 0; |
---|
[5438] | 460 | } |
---|
[5321] | 461 | |
---|
[5317] | 462 | if ( lhandle->lib != NULL && lhandle->lib->unload != NULL ) { |
---|
| 463 | roar_dl_context_restore(lhandle); |
---|
[3301] | 464 | lhandle->lib->unload(lhandle->para, lhandle->lib); |
---|
[5317] | 465 | roar_dl_context_store(lhandle); |
---|
| 466 | } |
---|
[3301] | 467 | |
---|
[3297] | 468 | #if defined(ROAR_HAVE_LIBDL) |
---|
[5317] | 469 | if ( lhandle->handle == NULL ) { |
---|
| 470 | ret = 0; |
---|
| 471 | } else { |
---|
| 472 | ret = dlclose(_roardl2ldl(lhandle)); |
---|
| 473 | } |
---|
[5481] | 474 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 475 | if ( lhandle->handle == NULL ) { |
---|
| 476 | ret = 0; |
---|
| 477 | } else { |
---|
| 478 | if ( FreeLibrary(_roardl2winhandle(lhandle)) ) { |
---|
| 479 | ret = 0; |
---|
| 480 | } else { |
---|
| 481 | ret = -1; |
---|
| 482 | } |
---|
| 483 | } |
---|
[3297] | 484 | #else |
---|
[3304] | 485 | ret = -1; |
---|
[3297] | 486 | #endif |
---|
[3304] | 487 | |
---|
[5312] | 488 | if ( lhandle->context.global_data != NULL ) |
---|
| 489 | roar_mm_free(lhandle->context.global_data); |
---|
| 490 | |
---|
[5275] | 491 | if ( lhandle->para != NULL ) |
---|
| 492 | roar_dl_para_unref(lhandle->para); |
---|
| 493 | |
---|
[5317] | 494 | if ( lhandle->libname != NULL ) |
---|
| 495 | roar_mm_free(lhandle->libname); |
---|
| 496 | |
---|
[3304] | 497 | roar_mm_free(lhandle); |
---|
| 498 | |
---|
[5438] | 499 | ROAR_DBG("roar_dl_unref(lhandle=%p) = %i", lhandle, ret); |
---|
[3304] | 500 | return ret; |
---|
[3297] | 501 | } |
---|
| 502 | |
---|
| 503 | void * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) { |
---|
| 504 | #if defined(ROAR_HAVE_LIBDL) |
---|
[3776] | 505 | void * ret = dlsym(_roardl2ldl(lhandle), sym); |
---|
| 506 | |
---|
[5270] | 507 | (void)type; |
---|
| 508 | |
---|
[5317] | 509 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i): errno=%s, dlerror()='%s'", lhandle, sym, type, ret, strerror(errno), dlerror()); |
---|
[3776] | 510 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = %p", lhandle, sym, type, ret); |
---|
| 511 | |
---|
| 512 | return ret; |
---|
[5481] | 513 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 514 | FARPROC ret = GetProcAddress(_roardl2winhandle(lhandle), sym); |
---|
| 515 | return (void*)ret; |
---|
[3297] | 516 | #else |
---|
[5317] | 517 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = NULL // errno=NOSYS", lhandle, sym, type, ret); |
---|
| 518 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[3297] | 519 | return NULL; |
---|
| 520 | #endif |
---|
| 521 | } |
---|
| 522 | |
---|
[5275] | 523 | int roar_dl_ra_init(struct roar_dl_lhandle * lhandle, |
---|
| 524 | const char * prefix, |
---|
| 525 | struct roar_dl_librarypara * para) { |
---|
[3297] | 526 | #define _SUFFIX "_roaraudio_library_init" |
---|
| 527 | char name[80] = _SUFFIX; |
---|
| 528 | struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para); |
---|
| 529 | struct roar_dl_libraryinst * lib; |
---|
[5317] | 530 | struct roar_dl_lhandle * getsymhandle = lhandle; |
---|
[5312] | 531 | void * global_data_state = NULL; |
---|
[3300] | 532 | int i; |
---|
[3297] | 533 | |
---|
[3305] | 534 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 535 | if ( prefix == NULL ) |
---|
| 536 | return -1; |
---|
| 537 | } else { |
---|
[5431] | 538 | if ( lhandle->ra_inited ) |
---|
| 539 | return 0; |
---|
| 540 | |
---|
[5317] | 541 | if ( prefix == NULL ) |
---|
| 542 | prefix = lhandle->libname; |
---|
| 543 | |
---|
[5275] | 544 | if ( para == NULL ) |
---|
| 545 | para = lhandle->para; |
---|
[5317] | 546 | |
---|
| 547 | if ( lhandle->flags & ROAR_DL_FLAG_STATIC ) |
---|
| 548 | getsymhandle = ROAR_DL_HANDLE_DEFAULT; |
---|
[3305] | 549 | } |
---|
| 550 | |
---|
[3297] | 551 | |
---|
| 552 | if ( prefix != NULL ) { |
---|
[5008] | 553 | roar_mm_strscpy(name, "_"); |
---|
| 554 | roar_mm_strscat(name, prefix); |
---|
| 555 | roar_mm_strscat(name, _SUFFIX); |
---|
[3297] | 556 | } |
---|
| 557 | |
---|
[3307] | 558 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name); |
---|
| 559 | |
---|
[5317] | 560 | if ( (func = roar_dl_getsym(getsymhandle, name, -1)) == NULL ) { |
---|
| 561 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s') = -1", lhandle, prefix); |
---|
[3297] | 562 | return -1; |
---|
[5317] | 563 | } |
---|
[3297] | 564 | |
---|
[3307] | 565 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func); |
---|
| 566 | |
---|
[3300] | 567 | lib = func(para); |
---|
[3297] | 568 | |
---|
| 569 | if ( lib == NULL ) |
---|
| 570 | return -1; |
---|
| 571 | |
---|
[3300] | 572 | if ( lib->version != ROAR_DL_LIBINST_VERSION ) |
---|
| 573 | return -1; |
---|
| 574 | |
---|
| 575 | if ( sizeof(struct roar_dl_libraryinst) > lib->len ) |
---|
| 576 | return -1; |
---|
| 577 | |
---|
[5353] | 578 | if ( lib->host_appname != NULL || lib->host_abiversion != NULL ) { |
---|
| 579 | // check for correct host. |
---|
| 580 | if ( para == NULL ) { |
---|
| 581 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 582 | return -1; |
---|
| 583 | } |
---|
| 584 | if ( roar_dl_para_check_version(para, lib->host_appname, lib->host_abiversion) == -1 ) |
---|
| 585 | return -1; |
---|
| 586 | } |
---|
| 587 | |
---|
[5347] | 588 | if ( (lib->libdep == NULL && lib->libdep_len) || (lib->libdep != NULL && !lib->libdep_len) ) { |
---|
| 589 | roar_err_set(ROAR_ERROR_BADLIB); |
---|
| 590 | return -1; |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | if ( lib->libdep != NULL && lib->libdep_len ) { |
---|
| 594 | // dynamic loader infos are currently not supported. |
---|
| 595 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 596 | return -1; |
---|
| 597 | } |
---|
| 598 | |
---|
[3361] | 599 | if ( !((void*)lhandle < (void*)128) ) { |
---|
| 600 | lhandle->lib = lib; |
---|
[5312] | 601 | |
---|
| 602 | if ( lib->global_data_len ) { |
---|
| 603 | lhandle->context.global_data = roar_mm_malloc(lib->global_data_len); |
---|
| 604 | if ( lhandle->context.global_data == NULL ) |
---|
| 605 | return -1; |
---|
| 606 | |
---|
| 607 | if ( lib->global_data_init == NULL ) { |
---|
| 608 | memset(lhandle->context.global_data, 0, lib->global_data_len); |
---|
| 609 | } else { |
---|
| 610 | memcpy(lhandle->context.global_data, lib->global_data_init, lib->global_data_len); |
---|
| 611 | } |
---|
| 612 | } |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | if ( lib->global_data_pointer != NULL ) { |
---|
| 616 | global_data_state = *(lib->global_data_pointer); |
---|
[5313] | 617 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5312] | 618 | *(lib->global_data_pointer) = lib->global_data_init; |
---|
| 619 | } else { |
---|
| 620 | *(lib->global_data_pointer) = lhandle->context.global_data; |
---|
| 621 | } |
---|
[3361] | 622 | } |
---|
| 623 | |
---|
[3300] | 624 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
| 625 | if ( lib->func[i] != NULL ) |
---|
| 626 | lib->func[i](para, lib); |
---|
| 627 | } |
---|
| 628 | |
---|
[5312] | 629 | if ( lib->global_data_pointer != NULL ) { |
---|
[5313] | 630 | if ( !((void*)lhandle < (void*)128) ) { |
---|
| 631 | *(lib->global_data_pointer) = global_data_state; |
---|
| 632 | } |
---|
[5312] | 633 | } |
---|
| 634 | |
---|
[5431] | 635 | if ( !((void*)lhandle < (void*)128) ) |
---|
| 636 | lhandle->ra_inited = 1; |
---|
| 637 | |
---|
[3302] | 638 | return 0; |
---|
[3297] | 639 | } |
---|
| 640 | |
---|
[5275] | 641 | const char * roar_dl_errstr(struct roar_dl_lhandle * lhandle) { |
---|
[3364] | 642 | #if defined(ROAR_HAVE_LIBDL) |
---|
[5270] | 643 | (void)lhandle; |
---|
[3364] | 644 | return dlerror(); |
---|
[5481] | 645 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 646 | roar_err_from_errno(); |
---|
| 647 | return roar_error2str(roar_error); |
---|
[3364] | 648 | #else |
---|
| 649 | return NULL; |
---|
| 650 | #endif |
---|
| 651 | } |
---|
| 652 | |
---|
[5275] | 653 | struct roar_dl_librarypara * roar_dl_getpara(struct roar_dl_lhandle * lhandle) { |
---|
| 654 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 655 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 656 | return NULL; |
---|
| 657 | } |
---|
| 658 | |
---|
| 659 | if ( lhandle->para == NULL ) { |
---|
| 660 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 661 | return NULL; |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | if ( roar_dl_para_ref(lhandle->para) == -1 ) |
---|
| 665 | return NULL; |
---|
| 666 | |
---|
| 667 | return lhandle->para; |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | const struct roar_dl_libraryname * roar_dl_getlibname(struct roar_dl_lhandle * lhandle) { |
---|
| 671 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 672 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 673 | return NULL; |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | if ( lhandle->lib == NULL ) { |
---|
| 677 | roar_err_set(ROAR_ERROR_BADLIB); |
---|
| 678 | return NULL; |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | if ( lhandle->lib->libname == NULL ) { |
---|
| 682 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 683 | return NULL; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | if ( lhandle->lib->libname->version != ROAR_DL_LIBNAME_VERSION ) { |
---|
| 687 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 688 | return NULL; |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | if ( lhandle->lib->libname->len != sizeof(struct roar_dl_libraryname) ) { |
---|
| 692 | roar_err_set(ROAR_ERROR_HOLE); |
---|
| 693 | return NULL; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | return lhandle->lib->libname; |
---|
| 697 | } |
---|
| 698 | |
---|
[5312] | 699 | int roar_dl_context_restore(struct roar_dl_lhandle * lhandle) { |
---|
| 700 | struct roar_error_state error_state; |
---|
| 701 | |
---|
[5317] | 702 | ROAR_DBG("roar_dl_context_restore(lhandle=%p) = ?", lhandle); |
---|
| 703 | |
---|
[5312] | 704 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5317] | 705 | ROAR_DBG("roar_dl_context_restore(lhandle=%p) = -1 // errno=NOTSUP", lhandle); |
---|
[5312] | 706 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 707 | return -1; |
---|
| 708 | } |
---|
| 709 | |
---|
| 710 | roar_err_store(&error_state); |
---|
| 711 | roar_err_restore(&(lhandle->context.error_state)); |
---|
| 712 | lhandle->context.error_state = error_state; |
---|
| 713 | |
---|
| 714 | if ( lhandle->lib->global_data_pointer != NULL ) { |
---|
[5317] | 715 | ROAR_DBG("roar_dl_context_restore(lhandle=%p): gptr(%p): %p -> %p", lhandle, |
---|
| 716 | lhandle->lib->global_data_pointer, *(lhandle->lib->global_data_pointer), lhandle->context.global_data); |
---|
[5312] | 717 | lhandle->context.global_data_state = *(lhandle->lib->global_data_pointer); |
---|
| 718 | *(lhandle->lib->global_data_pointer) = lhandle->context.global_data; |
---|
| 719 | } |
---|
| 720 | |
---|
[5317] | 721 | if ( lhandle->para->notifycore != NULL ) |
---|
| 722 | lhandle->context.notifycore = roar_notify_core_swap_global(lhandle->para->notifycore); |
---|
| 723 | |
---|
| 724 | ROAR_DBG("roar_dl_context_restore(lhandle=%p) = 0", lhandle); |
---|
[5312] | 725 | return 0; |
---|
| 726 | } |
---|
| 727 | |
---|
| 728 | int roar_dl_context_store(struct roar_dl_lhandle * lhandle) { |
---|
| 729 | struct roar_error_state error_state; |
---|
| 730 | |
---|
[5317] | 731 | ROAR_DBG("roar_dl_context_store(lhandle=%p) = ?", lhandle); |
---|
| 732 | |
---|
[5312] | 733 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5317] | 734 | ROAR_DBG("roar_dl_context_store(lhandle=%p) = -1 // errno=NOTSUP", lhandle); |
---|
[5312] | 735 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 736 | return -1; |
---|
| 737 | } |
---|
| 738 | |
---|
[5317] | 739 | if ( lhandle->para->notifycore != NULL ) { |
---|
| 740 | roar_notify_core_unref(roar_notify_core_swap_global(lhandle->context.notifycore)); |
---|
| 741 | roar_notify_core_unref(lhandle->context.notifycore); |
---|
| 742 | lhandle->context.notifycore = NULL; |
---|
| 743 | } |
---|
| 744 | |
---|
[5312] | 745 | if ( lhandle->lib->global_data_pointer != NULL ) { |
---|
[5317] | 746 | ROAR_DBG("roar_dl_context_store(lhandle=%p): gptr(%p): %p -> %p", lhandle, |
---|
| 747 | lhandle->lib->global_data_pointer, *(lhandle->lib->global_data_pointer), lhandle->context.global_data_state); |
---|
[5312] | 748 | *(lhandle->lib->global_data_pointer) = lhandle->context.global_data_state; |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | roar_err_store(&error_state); |
---|
| 752 | roar_err_restore(&(lhandle->context.error_state)); |
---|
| 753 | lhandle->context.error_state = error_state; |
---|
| 754 | |
---|
[5317] | 755 | ROAR_DBG("roar_dl_context_store(lhandle=%p) = 0", lhandle); |
---|
[5312] | 756 | return 0; |
---|
| 757 | } |
---|
| 758 | |
---|
[5317] | 759 | int roar_dl_appsched_trigger(struct roar_dl_lhandle * lhandle, enum roar_dl_appsched_trigger trigger) { |
---|
[5335] | 760 | int (*func) (struct roar_dl_librarypara * para) = NULL; |
---|
[5317] | 761 | int ret; |
---|
| 762 | |
---|
| 763 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 764 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 765 | return -1; |
---|
| 766 | } |
---|
| 767 | |
---|
[5446] | 768 | if ( lhandle->lib == NULL ) { |
---|
| 769 | roar_err_set(ROAR_ERROR_TYPEMM); |
---|
| 770 | return -1; |
---|
| 771 | } |
---|
| 772 | |
---|
[5317] | 773 | if ( lhandle->lib->appsched == NULL ) { |
---|
| 774 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 775 | return -1; |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | switch (trigger) { |
---|
| 779 | #define _trig(lname,uname) \ |
---|
| 780 | case ROAR_DL_APPSCHED_ ## uname : \ |
---|
[5335] | 781 | func = lhandle->lib->appsched->lname; \ |
---|
[5317] | 782 | break; |
---|
| 783 | _trig(init, INIT); |
---|
| 784 | _trig(free, FREE); |
---|
| 785 | _trig(update, UPDATE); |
---|
[5335] | 786 | _trig(tick, TICK); |
---|
| 787 | _trig(wait, WAIT); |
---|
| 788 | // use ifndef here so warnings of unhandled enum values will be shown in DEBUG mode. |
---|
| 789 | #ifndef DEBUG |
---|
| 790 | default: |
---|
| 791 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
| 792 | return -1; |
---|
| 793 | break; |
---|
| 794 | #endif |
---|
[5317] | 795 | } |
---|
| 796 | |
---|
[5335] | 797 | if ( func == NULL ) { |
---|
| 798 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 799 | return -1; |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | roar_dl_context_restore(lhandle); |
---|
| 803 | ret = func(lhandle->para); |
---|
| 804 | roar_dl_context_store(lhandle); |
---|
| 805 | return ret; |
---|
[5317] | 806 | } |
---|
| 807 | |
---|
[3297] | 808 | //ll |
---|