[3297] | 1 | //roardl.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013 |
---|
[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 | |
---|
[5517] | 42 | #if defined(ROAR_HAVE_H_DLFCN) && !defined(RTLD_NEXT) |
---|
[3367] | 43 | #define RTLD_NEXT ((void *) -1L) |
---|
| 44 | #endif |
---|
| 45 | |
---|
[5503] | 46 | #define RTF_RA_INITED 0x0001 |
---|
| 47 | #define RTF_APPSCHED_INITED 0x0002 |
---|
| 48 | #define RTF_APPSCHED_FREED 0x0004 |
---|
| 49 | |
---|
[5275] | 50 | struct roar_dl_lhandle { |
---|
[5317] | 51 | size_t refc; // currently unused. |
---|
| 52 | int flags; |
---|
| 53 | char * libname; // only used for ROAR_DL_FLAG_STATIC. |
---|
[5275] | 54 | struct roar_dl_librarypara * para; |
---|
| 55 | struct roar_dl_libraryinst * lib; |
---|
[5312] | 56 | struct { |
---|
| 57 | struct roar_error_state error_state; |
---|
| 58 | void * global_data; |
---|
| 59 | void * global_data_state; |
---|
[5317] | 60 | struct roar_notify_core * notifycore; |
---|
[5834] | 61 | struct roar_vio_calls * stdvios[3]; |
---|
[5312] | 62 | } context; |
---|
[5503] | 63 | unsigned int runtime_flags; |
---|
[5517] | 64 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5275] | 65 | void * handle; |
---|
[5481] | 66 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 67 | HMODULE handle; |
---|
[5275] | 68 | #endif |
---|
| 69 | }; |
---|
| 70 | |
---|
[5511] | 71 | // TODO: this should be removed on next SONAME change. |
---|
[5514] | 72 | static struct roar_dl_lhandle * __currently_inited = NULL; |
---|
| 73 | static int __currently_inited_fn = -1; |
---|
[5275] | 74 | |
---|
| 75 | struct roar_dl_librarypara * roar_dl_para_new(const char * args, void * binargv, |
---|
| 76 | const char * appname, const char * abiversion) { |
---|
| 77 | struct roar_dl_librarypara * ret = roar_mm_malloc(sizeof(struct roar_dl_librarypara)); |
---|
| 78 | ssize_t argc; |
---|
| 79 | int err; |
---|
| 80 | |
---|
| 81 | if ( ret == NULL ) |
---|
| 82 | return NULL; |
---|
| 83 | |
---|
| 84 | memset(ret, 0, sizeof(struct roar_dl_librarypara)); |
---|
| 85 | |
---|
| 86 | ret->version = ROAR_DL_LIBPARA_VERSION; |
---|
| 87 | ret->len = sizeof(struct roar_dl_librarypara); |
---|
| 88 | ret->refc = 1; |
---|
| 89 | ret->argc = 0; |
---|
| 90 | ret->argv = NULL; |
---|
| 91 | ret->args_store = NULL; |
---|
| 92 | ret->binargv = binargv; |
---|
| 93 | ret->appname = appname; |
---|
| 94 | ret->abiversion = abiversion; |
---|
| 95 | |
---|
| 96 | if ( args != NULL ) { |
---|
| 97 | ret->args_store = roar_mm_strdup(args); |
---|
| 98 | if ( ret->args_store == NULL ) { |
---|
| 99 | err = roar_error; |
---|
| 100 | roar_mm_free(ret); |
---|
| 101 | roar_error = err; |
---|
| 102 | return NULL; |
---|
| 103 | } |
---|
| 104 | |
---|
[5482] | 105 | argc = roar_keyval_split(&(ret->argv), ret->args_store, NULL, NULL, 1); |
---|
[5275] | 106 | if ( argc == -1 ) { |
---|
| 107 | err = roar_error; |
---|
| 108 | roar_mm_free(ret->args_store); |
---|
| 109 | roar_mm_free(ret); |
---|
| 110 | roar_error = err; |
---|
| 111 | return NULL; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | ret->argc = argc; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | return ret; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | int roar_dl_para_ref (struct roar_dl_librarypara * para) { |
---|
| 121 | if ( para == NULL ) { |
---|
[5438] | 122 | ROAR_DBG("roar_dl_para_ref(para=%p) = -1 // error=FAULT", para); |
---|
[5275] | 123 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 124 | return -1; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | para->refc++; |
---|
| 128 | |
---|
[5438] | 129 | ROAR_DBG("roar_dl_para_ref(para=%p) = 0", para); |
---|
[5275] | 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | int roar_dl_para_unref (struct roar_dl_librarypara * para) { |
---|
| 134 | if ( para == NULL ) { |
---|
[5438] | 135 | ROAR_DBG("roar_dl_para_unref(para=%p) = -1 // error=FAULT", para); |
---|
[5275] | 136 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 137 | return -1; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | para->refc--; |
---|
| 141 | |
---|
[5438] | 142 | if ( para->refc ) { |
---|
| 143 | ROAR_DBG("roar_dl_para_unref(para=%p) = 0", para); |
---|
[5317] | 144 | return 0; |
---|
[5438] | 145 | } |
---|
[5317] | 146 | |
---|
| 147 | if ( para->notifycore != NULL ) |
---|
| 148 | roar_notify_core_unref(para->notifycore); |
---|
| 149 | |
---|
| 150 | if ( para->args_store != NULL ) { |
---|
| 151 | roar_mm_free(para->args_store); |
---|
| 152 | roar_mm_free(para->argv); |
---|
[5275] | 153 | } |
---|
| 154 | |
---|
[5317] | 155 | roar_mm_free(para); |
---|
| 156 | |
---|
[5438] | 157 | ROAR_DBG("roar_dl_para_unref(para=%p) = 0", para); |
---|
[5275] | 158 | return 0; |
---|
| 159 | } |
---|
| 160 | int roar_dl_para_check_version (struct roar_dl_librarypara * para, |
---|
| 161 | const char * appname, const char * abiversion) { |
---|
| 162 | if ( para == NULL ) { |
---|
| 163 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | // check if both appnames are NULL or non-NULL. |
---|
| 168 | if ( (para->appname == NULL && appname != NULL) || (para->appname != NULL && appname == NULL) ) { |
---|
[5371] | 169 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
[5275] | 170 | return -1; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // check if the appname matches if given. |
---|
| 174 | if ( para->appname != NULL && !!strcmp(para->appname, appname) ) { |
---|
[5371] | 175 | roar_err_set(ROAR_ERROR_BADHOST); |
---|
[5275] | 176 | return -1; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | // check if both ABI versions are NULL or non-NULL. |
---|
| 180 | if ( (para->abiversion == NULL && abiversion != NULL) || (para->abiversion != NULL && abiversion == NULL) ) { |
---|
| 181 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 182 | return -1; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | // check if the ABI versions matches if given. |
---|
| 186 | if ( para->abiversion != NULL && !!strcmp(para->abiversion, abiversion) ) { |
---|
| 187 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 188 | return -1; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | return 0; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
[5517] | 195 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[3297] | 196 | static void * _roardl2ldl (struct roar_dl_lhandle * lhandle) { |
---|
[5317] | 197 | ROAR_DBG("_roardl2ldl(lhandle=%p) = ?", lhandle); |
---|
| 198 | |
---|
[5511] | 199 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 200 | switch ((int)(void*)lhandle) { |
---|
| 201 | case (int)(void*)ROAR_DL_HANDLE_DEFAULT: |
---|
| 202 | case (int)(void*)ROAR_DL_HANDLE_LIBROAR: |
---|
| 203 | case (int)(void*)ROAR_DL_HANDLE_APPLICATION: |
---|
| 204 | ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)RTLD_DEFAULT); |
---|
| 205 | return RTLD_DEFAULT; |
---|
| 206 | break; |
---|
| 207 | case (int)(void*)ROAR_DL_HANDLE_NEXT: |
---|
| 208 | ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)RTLD_NEXT); |
---|
| 209 | return RTLD_NEXT; |
---|
| 210 | break; |
---|
| 211 | } |
---|
[5317] | 212 | } |
---|
[3297] | 213 | |
---|
[5317] | 214 | ROAR_DBG("_roardl2ldl(lhandle=%p) = %p", lhandle, (void*)(lhandle->handle)); |
---|
[3297] | 215 | return lhandle->handle; |
---|
| 216 | } |
---|
[5481] | 217 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 218 | static HMODULE _roardl2winhandle(struct roar_dl_lhandle * lhandle) { |
---|
| 219 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 220 | return NULL; |
---|
| 221 | } |
---|
| 222 | return lhandle->handle; |
---|
| 223 | } |
---|
[3297] | 224 | #endif |
---|
| 225 | |
---|
[5427] | 226 | #ifdef ROAR_HAVE_H_DIRENT |
---|
| 227 | // pvn = prefix, host vendor, host name |
---|
| 228 | static struct roar_dl_lhandle * _load_from_path_pvn(const char * name, |
---|
| 229 | int flags, int ra_init, |
---|
| 230 | struct roar_dl_librarypara * para, |
---|
| 231 | const char * prefix, |
---|
| 232 | const char * hostvendor, |
---|
| 233 | const char * hostname) { |
---|
| 234 | struct roar_dl_lhandle * ret = NULL; |
---|
| 235 | int i, j; |
---|
| 236 | char path[1024]; |
---|
| 237 | const char * path_format[] = {"%s/%s/%s", "%s/%s/universal", "%s/universal/universal"}; |
---|
| 238 | DIR * dir; |
---|
| 239 | struct dirent * dirent; |
---|
| 240 | char file[1024]; |
---|
| 241 | const char * file_format[] = {"%s/%s/%s", "%s/%s/%s" ROAR_SHARED_SUFFIX, "%s/%s/lib%s" ROAR_SHARED_SUFFIX}; |
---|
| 242 | // cont->handle[idx] = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, para); |
---|
| 243 | //#vars: $PREFIX_PLUGINS, $hostvendor, $hostname, $name |
---|
| 244 | //#search order: $PREFIX_PLUGINS/{$hostvendor/{$hostname,universal},universal/universal}/*/{,lib}$name.so |
---|
| 245 | |
---|
| 246 | for (i = 0; i < 3; i++) { |
---|
| 247 | snprintf(path, sizeof(path), path_format[i], prefix, hostvendor, hostname); |
---|
| 248 | dir = opendir(path); |
---|
| 249 | if ( dir == NULL ) |
---|
| 250 | continue; |
---|
| 251 | |
---|
| 252 | while ((dirent = readdir(dir)) != NULL) { |
---|
| 253 | if ( !strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..") ) |
---|
| 254 | continue; |
---|
| 255 | |
---|
| 256 | for (j = 0; j < 3; j++) { |
---|
| 257 | snprintf(file, sizeof(file), file_format[j], path, dirent->d_name, name); |
---|
| 258 | ret = roar_dl_open(file, flags, ra_init, para); |
---|
| 259 | if ( ret != NULL ) { |
---|
| 260 | closedir(dir); |
---|
| 261 | return ret; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | closedir(dir); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | return NULL; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | static struct roar_dl_lhandle * _load_from_path(const char * name, int flags, int ra_init, |
---|
| 273 | struct roar_dl_librarypara * para) { |
---|
| 274 | struct roar_dl_lhandle * ret = NULL; |
---|
| 275 | char * host = NULL; |
---|
| 276 | char * hostvendor_buffer = NULL; |
---|
| 277 | char * hostvendor = NULL; |
---|
| 278 | char * hostname = NULL; |
---|
[5688] | 279 | size_t hostvendor_len; |
---|
[5427] | 280 | char * c, * d; |
---|
| 281 | |
---|
| 282 | if ( para != NULL && para->appname != NULL && para->appname[0] != 0 ) { |
---|
| 283 | host = roar_mm_strdup(para->appname); |
---|
| 284 | |
---|
| 285 | // if we are out of memory we will likely not pass the rest, so just return in error. |
---|
| 286 | if ( host == NULL ) |
---|
| 287 | return NULL; |
---|
| 288 | |
---|
| 289 | hostname = host; |
---|
| 290 | c = strstr(host, "<"); |
---|
| 291 | if ( c != NULL ) { |
---|
| 292 | while (c[-1] == ' ') c--; |
---|
| 293 | *c = 0; |
---|
| 294 | c++; |
---|
| 295 | while (*c == ' ' || *c == '<') c++; |
---|
| 296 | if ( *c ) { |
---|
| 297 | d = strstr(c, ">"); |
---|
| 298 | if ( d != NULL ) |
---|
| 299 | *d = 0; |
---|
| 300 | |
---|
| 301 | d = strstr(c, "/"); |
---|
| 302 | if ( d != NULL ) { |
---|
| 303 | *d = '-'; |
---|
| 304 | hostvendor = c; |
---|
| 305 | } else { |
---|
[5688] | 306 | hostvendor_len = roar_mm_strlen(c) + 1 /* tailing \0 */ + 6 /* "unreg-" */; |
---|
| 307 | hostvendor_buffer = roar_mm_malloc(hostvendor_len); |
---|
[5427] | 308 | |
---|
| 309 | // see above |
---|
| 310 | if ( hostvendor_buffer == NULL ) { |
---|
| 311 | roar_mm_free(host); |
---|
| 312 | return NULL; |
---|
| 313 | } |
---|
| 314 | |
---|
[5688] | 315 | roar_mm_strlcpy(hostvendor_buffer, "unreg-", hostvendor_len); |
---|
| 316 | roar_mm_strlcat(hostvendor_buffer, c, hostvendor_len); |
---|
[5427] | 317 | hostvendor = hostvendor_buffer; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | if ( hostvendor == NULL ) |
---|
| 324 | hostvendor = "universal"; |
---|
| 325 | |
---|
| 326 | if ( hostname == NULL ) |
---|
| 327 | hostname = "universal"; |
---|
| 328 | |
---|
| 329 | ret = _load_from_path_pvn(name, flags, ra_init, para, ROAR_PREFIX_PLUGINS, hostvendor, hostname); |
---|
| 330 | |
---|
| 331 | if ( host != NULL ) |
---|
| 332 | roar_mm_free(host); |
---|
| 333 | if ( hostvendor_buffer != NULL ) |
---|
| 334 | roar_mm_free(hostvendor_buffer); |
---|
| 335 | return ret; |
---|
| 336 | } |
---|
| 337 | #endif |
---|
| 338 | |
---|
| 339 | static struct roar_dl_lhandle * _roar_dl_open_pluginpath(const char * filename, int flags, |
---|
| 340 | int ra_init, struct roar_dl_librarypara * para) { |
---|
| 341 | flags |= ROAR_DL_FLAG_PLUGINPATH; |
---|
| 342 | flags -= ROAR_DL_FLAG_PLUGINPATH; |
---|
| 343 | |
---|
| 344 | #ifdef ROAR_HAVE_H_DIRENT |
---|
[5430] | 345 | // do normal open for everything with a path name. |
---|
| 346 | if ( strstr(filename, "/") != NULL ) |
---|
| 347 | return roar_dl_open(filename, flags, ra_init, para); |
---|
| 348 | |
---|
[5427] | 349 | return _load_from_path(filename, flags, ra_init, para); |
---|
| 350 | #else |
---|
| 351 | // fall back to system open function. |
---|
| 352 | return roar_dl_open(filename, flags, ra_init, para); |
---|
| 353 | #endif |
---|
| 354 | } |
---|
| 355 | |
---|
[5275] | 356 | struct roar_dl_lhandle * roar_dl_open(const char * filename, int flags, |
---|
| 357 | int ra_init, struct roar_dl_librarypara * para) { |
---|
[3298] | 358 | struct roar_dl_lhandle * ret = NULL; |
---|
[5517] | 359 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5834] | 360 | #if defined(RTLD_DEEPBIND) && 0 |
---|
[5835] | 361 | // FIXME: This is currently disabled. See #296. |
---|
[5344] | 362 | int libdl_flags = RTLD_DEEPBIND; |
---|
[5317] | 363 | #else |
---|
[5344] | 364 | int libdl_flags = 0; |
---|
[5317] | 365 | #endif |
---|
| 366 | #endif |
---|
[5275] | 367 | int err; |
---|
[3298] | 368 | |
---|
[5427] | 369 | switch (flags) { |
---|
| 370 | case ROAR_DL_FLAG_DEFAULTS: flags = ROAR_DL_FLAG_NONE; break; |
---|
| 371 | case ROAR_DL_FLAG_PLUGIN: flags = ROAR_DL_FLAG_PLUGINPATH; break; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | if ( flags & ROAR_DL_FLAG_PLUGINPATH ) |
---|
| 375 | return _roar_dl_open_pluginpath(filename, flags, ra_init, para); |
---|
[3304] | 376 | |
---|
[5517] | 377 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5344] | 378 | if ( flags & ROAR_DL_FLAG_LAZY ) { |
---|
| 379 | libdl_flags |= RTLD_LAZY; |
---|
| 380 | } else { |
---|
| 381 | libdl_flags |= RTLD_NOW; |
---|
| 382 | } |
---|
| 383 | #endif |
---|
| 384 | |
---|
[3304] | 385 | if ( (ret = roar_mm_malloc(sizeof(struct roar_dl_lhandle))) == NULL ) |
---|
| 386 | return NULL; |
---|
| 387 | |
---|
| 388 | memset(ret, 0, sizeof(struct roar_dl_lhandle)); |
---|
| 389 | |
---|
[5312] | 390 | roar_err_initstore(&(ret->context.error_state)); |
---|
| 391 | |
---|
[5317] | 392 | ret->flags = flags; |
---|
[5321] | 393 | ret->refc = 1; |
---|
[3366] | 394 | |
---|
[5317] | 395 | if ( flags & ROAR_DL_FLAG_STATIC ) { |
---|
| 396 | if ( filename == NULL ) { |
---|
| 397 | ret->libname = NULL; |
---|
| 398 | } else { |
---|
| 399 | ret->libname = roar_mm_strdup(filename); |
---|
| 400 | } |
---|
| 401 | } else { |
---|
[5517] | 402 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5317] | 403 | ret->handle = dlopen(filename, libdl_flags); |
---|
[3304] | 404 | |
---|
[5317] | 405 | if ( ret->handle == NULL ) { |
---|
| 406 | 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)); |
---|
| 407 | roar_mm_free(ret); |
---|
| 408 | return NULL; |
---|
| 409 | } |
---|
[5481] | 410 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 411 | ret->handle = LoadLibrary(filename); |
---|
| 412 | |
---|
| 413 | if ( ret->handle == NULL ) { |
---|
| 414 | roar_mm_free(ret); |
---|
| 415 | return NULL; |
---|
| 416 | } |
---|
[3304] | 417 | #else |
---|
[5317] | 418 | roar_mm_free(ret); |
---|
| 419 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 420 | return NULL; |
---|
[3304] | 421 | #endif |
---|
[5317] | 422 | } |
---|
[3304] | 423 | |
---|
[5275] | 424 | ret->para = para; |
---|
[3298] | 425 | |
---|
[5834] | 426 | if ( roar_vio_ref(roar_stdin) == 0 ) |
---|
| 427 | ret->context.stdvios[0] = roar_stdin; |
---|
| 428 | if ( roar_vio_ref(roar_stdout) == 0 ) |
---|
| 429 | ret->context.stdvios[1] = roar_stdout; |
---|
| 430 | if ( roar_vio_ref(roar_stderr) == 0 ) |
---|
| 431 | ret->context.stdvios[2] = roar_stderr; |
---|
| 432 | |
---|
[3298] | 433 | if ( ra_init ) { |
---|
[5275] | 434 | if ( roar_dl_ra_init(ret, NULL, para) == -1 ) { |
---|
| 435 | err = roar_error; |
---|
| 436 | 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)); |
---|
[5517] | 437 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5317] | 438 | if ( ret->handle != NULL ) |
---|
| 439 | dlclose(ret->handle); |
---|
[5481] | 440 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 441 | if ( ret->handle != NULL ) |
---|
| 442 | FreeLibrary(ret->handle); |
---|
[5275] | 443 | #endif |
---|
[5834] | 444 | roar_vio_unref(ret->context.stdvios[0]); |
---|
| 445 | roar_vio_unref(ret->context.stdvios[1]); |
---|
| 446 | roar_vio_unref(ret->context.stdvios[2]); |
---|
[5275] | 447 | roar_mm_free(ret); |
---|
| 448 | roar_error = err; |
---|
| 449 | return NULL; |
---|
| 450 | } |
---|
[3298] | 451 | } |
---|
| 452 | |
---|
[5275] | 453 | if ( para != NULL ) |
---|
| 454 | roar_dl_para_ref(para); |
---|
| 455 | |
---|
[3298] | 456 | return ret; |
---|
[3297] | 457 | } |
---|
| 458 | |
---|
[5321] | 459 | int roar_dl_ref (struct roar_dl_lhandle * lhandle) { |
---|
| 460 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5438] | 461 | ROAR_DBG("roar_dl_ref(lhandle=%p) = -1 // error=BADFH", lhandle); |
---|
[5275] | 462 | roar_err_set(ROAR_ERROR_BADFH); |
---|
[3301] | 463 | return -1; |
---|
[5275] | 464 | } |
---|
[3301] | 465 | |
---|
[5321] | 466 | lhandle->refc++; |
---|
| 467 | |
---|
[5438] | 468 | ROAR_DBG("roar_dl_ref(lhandle=%p) = 0", lhandle); |
---|
[5321] | 469 | return 0; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | int roar_dl_unref (struct roar_dl_lhandle * lhandle) { |
---|
| 473 | int ret = -1; |
---|
| 474 | |
---|
| 475 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5438] | 476 | ROAR_DBG("roar_dl_unref(lhandle=%p) = -1 // error=BADFH", lhandle); |
---|
[5321] | 477 | roar_err_set(ROAR_ERROR_BADFH); |
---|
| 478 | return -1; |
---|
| 479 | } |
---|
| 480 | |
---|
[5438] | 481 | lhandle->refc--; |
---|
[5321] | 482 | |
---|
[5438] | 483 | if ( lhandle->refc ) { |
---|
| 484 | ROAR_DBG("roar_dl_unref(lhandle=%p) = 0", lhandle); |
---|
[5321] | 485 | return 0; |
---|
[5438] | 486 | } |
---|
[5321] | 487 | |
---|
[5511] | 488 | roar_dl_unregister_fn(lhandle); |
---|
| 489 | |
---|
[5317] | 490 | if ( lhandle->lib != NULL && lhandle->lib->unload != NULL ) { |
---|
| 491 | roar_dl_context_restore(lhandle); |
---|
[3301] | 492 | lhandle->lib->unload(lhandle->para, lhandle->lib); |
---|
[5317] | 493 | roar_dl_context_store(lhandle); |
---|
| 494 | } |
---|
[3301] | 495 | |
---|
[5517] | 496 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5317] | 497 | if ( lhandle->handle == NULL ) { |
---|
| 498 | ret = 0; |
---|
| 499 | } else { |
---|
| 500 | ret = dlclose(_roardl2ldl(lhandle)); |
---|
| 501 | } |
---|
[5481] | 502 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 503 | if ( lhandle->handle == NULL ) { |
---|
| 504 | ret = 0; |
---|
| 505 | } else { |
---|
| 506 | if ( FreeLibrary(_roardl2winhandle(lhandle)) ) { |
---|
| 507 | ret = 0; |
---|
| 508 | } else { |
---|
| 509 | ret = -1; |
---|
| 510 | } |
---|
| 511 | } |
---|
[3297] | 512 | #else |
---|
[3304] | 513 | ret = -1; |
---|
[3297] | 514 | #endif |
---|
[3304] | 515 | |
---|
[5834] | 516 | roar_vio_unref(lhandle->context.stdvios[0]); |
---|
| 517 | roar_vio_unref(lhandle->context.stdvios[1]); |
---|
| 518 | roar_vio_unref(lhandle->context.stdvios[2]); |
---|
| 519 | |
---|
[5312] | 520 | if ( lhandle->context.global_data != NULL ) |
---|
| 521 | roar_mm_free(lhandle->context.global_data); |
---|
| 522 | |
---|
[5275] | 523 | if ( lhandle->para != NULL ) |
---|
| 524 | roar_dl_para_unref(lhandle->para); |
---|
| 525 | |
---|
[5317] | 526 | if ( lhandle->libname != NULL ) |
---|
| 527 | roar_mm_free(lhandle->libname); |
---|
| 528 | |
---|
[3304] | 529 | roar_mm_free(lhandle); |
---|
| 530 | |
---|
[5438] | 531 | ROAR_DBG("roar_dl_unref(lhandle=%p) = %i", lhandle, ret); |
---|
[3304] | 532 | return ret; |
---|
[3297] | 533 | } |
---|
| 534 | |
---|
| 535 | void * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) { |
---|
[5517] | 536 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[3776] | 537 | void * ret = dlsym(_roardl2ldl(lhandle), sym); |
---|
| 538 | |
---|
[5270] | 539 | (void)type; |
---|
| 540 | |
---|
[5651] | 541 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i): ret=%p, errno=%s(%i), dlerror()='%s'", lhandle, sym, type, ret, strerror(errno), errno, dlerror()); |
---|
[3776] | 542 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = %p", lhandle, sym, type, ret); |
---|
| 543 | |
---|
| 544 | return ret; |
---|
[5481] | 545 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 546 | FARPROC ret = GetProcAddress(_roardl2winhandle(lhandle), sym); |
---|
| 547 | return (void*)ret; |
---|
[3297] | 548 | #else |
---|
[5317] | 549 | ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = NULL // errno=NOSYS", lhandle, sym, type, ret); |
---|
| 550 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[3297] | 551 | return NULL; |
---|
| 552 | #endif |
---|
| 553 | } |
---|
| 554 | |
---|
[5275] | 555 | int roar_dl_ra_init(struct roar_dl_lhandle * lhandle, |
---|
| 556 | const char * prefix, |
---|
| 557 | struct roar_dl_librarypara * para) { |
---|
[5511] | 558 | struct roar_dl_lhandle * old_init; |
---|
[5514] | 559 | int old_fn; |
---|
[3297] | 560 | #define _SUFFIX "_roaraudio_library_init" |
---|
| 561 | char name[80] = _SUFFIX; |
---|
| 562 | struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para); |
---|
| 563 | struct roar_dl_libraryinst * lib; |
---|
[5317] | 564 | struct roar_dl_lhandle * getsymhandle = lhandle; |
---|
[5312] | 565 | void * global_data_state = NULL; |
---|
[3300] | 566 | int i; |
---|
[3297] | 567 | |
---|
[3305] | 568 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 569 | if ( prefix == NULL ) |
---|
| 570 | return -1; |
---|
| 571 | } else { |
---|
[5503] | 572 | if ( lhandle->runtime_flags & RTF_RA_INITED ) |
---|
[5431] | 573 | return 0; |
---|
| 574 | |
---|
[5317] | 575 | if ( prefix == NULL ) |
---|
| 576 | prefix = lhandle->libname; |
---|
| 577 | |
---|
[5275] | 578 | if ( para == NULL ) |
---|
| 579 | para = lhandle->para; |
---|
[5317] | 580 | |
---|
| 581 | if ( lhandle->flags & ROAR_DL_FLAG_STATIC ) |
---|
| 582 | getsymhandle = ROAR_DL_HANDLE_DEFAULT; |
---|
[3305] | 583 | } |
---|
| 584 | |
---|
[3297] | 585 | |
---|
| 586 | if ( prefix != NULL ) { |
---|
[5008] | 587 | roar_mm_strscpy(name, "_"); |
---|
| 588 | roar_mm_strscat(name, prefix); |
---|
| 589 | roar_mm_strscat(name, _SUFFIX); |
---|
[3297] | 590 | } |
---|
| 591 | |
---|
[3307] | 592 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name); |
---|
| 593 | |
---|
[5317] | 594 | if ( (func = roar_dl_getsym(getsymhandle, name, -1)) == NULL ) { |
---|
| 595 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s') = -1", lhandle, prefix); |
---|
[3297] | 596 | return -1; |
---|
[5317] | 597 | } |
---|
[3297] | 598 | |
---|
[3307] | 599 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func); |
---|
| 600 | |
---|
[5512] | 601 | roar_err_set(ROAR_ERROR_BADLIB); // set a default error in case it returns NULL but not set an error code. |
---|
[3300] | 602 | lib = func(para); |
---|
[3297] | 603 | |
---|
| 604 | if ( lib == NULL ) |
---|
| 605 | return -1; |
---|
| 606 | |
---|
[3300] | 607 | if ( lib->version != ROAR_DL_LIBINST_VERSION ) |
---|
| 608 | return -1; |
---|
| 609 | |
---|
| 610 | if ( sizeof(struct roar_dl_libraryinst) > lib->len ) |
---|
| 611 | return -1; |
---|
| 612 | |
---|
[5353] | 613 | if ( lib->host_appname != NULL || lib->host_abiversion != NULL ) { |
---|
| 614 | // check for correct host. |
---|
| 615 | if ( para == NULL ) { |
---|
| 616 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 617 | return -1; |
---|
| 618 | } |
---|
| 619 | if ( roar_dl_para_check_version(para, lib->host_appname, lib->host_abiversion) == -1 ) |
---|
| 620 | return -1; |
---|
| 621 | } |
---|
| 622 | |
---|
[5347] | 623 | if ( (lib->libdep == NULL && lib->libdep_len) || (lib->libdep != NULL && !lib->libdep_len) ) { |
---|
| 624 | roar_err_set(ROAR_ERROR_BADLIB); |
---|
| 625 | return -1; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | if ( lib->libdep != NULL && lib->libdep_len ) { |
---|
| 629 | // dynamic loader infos are currently not supported. |
---|
| 630 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 631 | return -1; |
---|
| 632 | } |
---|
| 633 | |
---|
[3361] | 634 | if ( !((void*)lhandle < (void*)128) ) { |
---|
| 635 | lhandle->lib = lib; |
---|
[5312] | 636 | |
---|
| 637 | if ( lib->global_data_len ) { |
---|
| 638 | lhandle->context.global_data = roar_mm_malloc(lib->global_data_len); |
---|
| 639 | if ( lhandle->context.global_data == NULL ) |
---|
| 640 | return -1; |
---|
| 641 | |
---|
| 642 | if ( lib->global_data_init == NULL ) { |
---|
| 643 | memset(lhandle->context.global_data, 0, lib->global_data_len); |
---|
| 644 | } else { |
---|
| 645 | memcpy(lhandle->context.global_data, lib->global_data_init, lib->global_data_len); |
---|
| 646 | } |
---|
| 647 | } |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | if ( lib->global_data_pointer != NULL ) { |
---|
| 651 | global_data_state = *(lib->global_data_pointer); |
---|
[5313] | 652 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5312] | 653 | *(lib->global_data_pointer) = lib->global_data_init; |
---|
| 654 | } else { |
---|
| 655 | *(lib->global_data_pointer) = lhandle->context.global_data; |
---|
| 656 | } |
---|
[3361] | 657 | } |
---|
| 658 | |
---|
[5761] | 659 | roar_dl_context_restore(lhandle); |
---|
| 660 | |
---|
[5511] | 661 | old_init = __currently_inited; |
---|
[5514] | 662 | old_fn = __currently_inited_fn; |
---|
[5511] | 663 | __currently_inited = lhandle; |
---|
[3300] | 664 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
[5514] | 665 | __currently_inited_fn = i; |
---|
[3300] | 666 | if ( lib->func[i] != NULL ) |
---|
| 667 | lib->func[i](para, lib); |
---|
| 668 | } |
---|
[5514] | 669 | __currently_inited_fn = old_fn; |
---|
[5511] | 670 | __currently_inited = old_init; |
---|
| 671 | |
---|
[5761] | 672 | roar_dl_context_store(lhandle); |
---|
[3300] | 673 | |
---|
[5312] | 674 | if ( lib->global_data_pointer != NULL ) { |
---|
[5313] | 675 | if ( !((void*)lhandle < (void*)128) ) { |
---|
| 676 | *(lib->global_data_pointer) = global_data_state; |
---|
| 677 | } |
---|
[5312] | 678 | } |
---|
| 679 | |
---|
[5431] | 680 | if ( !((void*)lhandle < (void*)128) ) |
---|
[5503] | 681 | lhandle->runtime_flags |= RTF_RA_INITED; |
---|
[5431] | 682 | |
---|
[3302] | 683 | return 0; |
---|
[3297] | 684 | } |
---|
| 685 | |
---|
[5275] | 686 | const char * roar_dl_errstr(struct roar_dl_lhandle * lhandle) { |
---|
[5517] | 687 | #if defined(ROAR_HAVE_H_DLFCN) |
---|
[5270] | 688 | (void)lhandle; |
---|
[3364] | 689 | return dlerror(); |
---|
[5481] | 690 | #elif defined(ROAR_TARGET_WIN32) |
---|
| 691 | roar_err_from_errno(); |
---|
| 692 | return roar_error2str(roar_error); |
---|
[3364] | 693 | #else |
---|
| 694 | return NULL; |
---|
| 695 | #endif |
---|
| 696 | } |
---|
| 697 | |
---|
[5275] | 698 | struct roar_dl_librarypara * roar_dl_getpara(struct roar_dl_lhandle * lhandle) { |
---|
| 699 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 700 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 701 | return NULL; |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | if ( lhandle->para == NULL ) { |
---|
| 705 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 706 | return NULL; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | if ( roar_dl_para_ref(lhandle->para) == -1 ) |
---|
| 710 | return NULL; |
---|
| 711 | |
---|
| 712 | return lhandle->para; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | const struct roar_dl_libraryname * roar_dl_getlibname(struct roar_dl_lhandle * lhandle) { |
---|
| 716 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 717 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 718 | return NULL; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | if ( lhandle->lib == NULL ) { |
---|
| 722 | roar_err_set(ROAR_ERROR_BADLIB); |
---|
| 723 | return NULL; |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | if ( lhandle->lib->libname == NULL ) { |
---|
| 727 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 728 | return NULL; |
---|
| 729 | } |
---|
| 730 | |
---|
| 731 | if ( lhandle->lib->libname->version != ROAR_DL_LIBNAME_VERSION ) { |
---|
| 732 | roar_err_set(ROAR_ERROR_BADVERSION); |
---|
| 733 | return NULL; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | if ( lhandle->lib->libname->len != sizeof(struct roar_dl_libraryname) ) { |
---|
| 737 | roar_err_set(ROAR_ERROR_HOLE); |
---|
| 738 | return NULL; |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | return lhandle->lib->libname; |
---|
| 742 | } |
---|
| 743 | |
---|
[5834] | 744 | static inline void __swap_stdvios(struct roar_dl_lhandle * lhandle) { |
---|
| 745 | struct roar_vio_calls * vio; |
---|
| 746 | |
---|
| 747 | vio = roar_stdin; |
---|
| 748 | roar_stdin = lhandle->context.stdvios[0]; |
---|
| 749 | lhandle->context.stdvios[0] = vio; |
---|
| 750 | vio = roar_stdout; |
---|
| 751 | roar_stdout = lhandle->context.stdvios[1]; |
---|
| 752 | lhandle->context.stdvios[1] = vio; |
---|
| 753 | vio = roar_stderr; |
---|
| 754 | roar_stderr = lhandle->context.stdvios[2]; |
---|
| 755 | lhandle->context.stdvios[2] = vio; |
---|
| 756 | } |
---|
| 757 | |
---|
[5312] | 758 | int roar_dl_context_restore(struct roar_dl_lhandle * lhandle) { |
---|
| 759 | struct roar_error_state error_state; |
---|
| 760 | |
---|
[5317] | 761 | ROAR_DBG("roar_dl_context_restore(lhandle=%p) = ?", lhandle); |
---|
| 762 | |
---|
[5312] | 763 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5317] | 764 | ROAR_DBG("roar_dl_context_restore(lhandle=%p) = -1 // errno=NOTSUP", lhandle); |
---|
[5312] | 765 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 766 | return -1; |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | roar_err_store(&error_state); |
---|
| 770 | roar_err_restore(&(lhandle->context.error_state)); |
---|
| 771 | lhandle->context.error_state = error_state; |
---|
| 772 | |
---|
[5834] | 773 | __swap_stdvios(lhandle); |
---|
| 774 | |
---|
[5312] | 775 | if ( lhandle->lib->global_data_pointer != NULL ) { |
---|
[5317] | 776 | ROAR_DBG("roar_dl_context_restore(lhandle=%p): gptr(%p): %p -> %p", lhandle, |
---|
| 777 | lhandle->lib->global_data_pointer, *(lhandle->lib->global_data_pointer), lhandle->context.global_data); |
---|
[5312] | 778 | lhandle->context.global_data_state = *(lhandle->lib->global_data_pointer); |
---|
| 779 | *(lhandle->lib->global_data_pointer) = lhandle->context.global_data; |
---|
| 780 | } |
---|
| 781 | |
---|
[5583] | 782 | if ( lhandle->para != NULL && lhandle->para->notifycore != NULL ) |
---|
[5317] | 783 | lhandle->context.notifycore = roar_notify_core_swap_global(lhandle->para->notifycore); |
---|
| 784 | |
---|
| 785 | ROAR_DBG("roar_dl_context_restore(lhandle=%p) = 0", lhandle); |
---|
[5312] | 786 | return 0; |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | int roar_dl_context_store(struct roar_dl_lhandle * lhandle) { |
---|
| 790 | struct roar_error_state error_state; |
---|
| 791 | |
---|
[5317] | 792 | ROAR_DBG("roar_dl_context_store(lhandle=%p) = ?", lhandle); |
---|
| 793 | |
---|
[5312] | 794 | if ( (void*)lhandle < (void*)128 ) { |
---|
[5317] | 795 | ROAR_DBG("roar_dl_context_store(lhandle=%p) = -1 // errno=NOTSUP", lhandle); |
---|
[5312] | 796 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 797 | return -1; |
---|
| 798 | } |
---|
| 799 | |
---|
[5583] | 800 | if ( lhandle->para != NULL && lhandle->para->notifycore != NULL ) { |
---|
[5317] | 801 | roar_notify_core_unref(roar_notify_core_swap_global(lhandle->context.notifycore)); |
---|
| 802 | roar_notify_core_unref(lhandle->context.notifycore); |
---|
| 803 | lhandle->context.notifycore = NULL; |
---|
| 804 | } |
---|
| 805 | |
---|
[5312] | 806 | if ( lhandle->lib->global_data_pointer != NULL ) { |
---|
[5317] | 807 | ROAR_DBG("roar_dl_context_store(lhandle=%p): gptr(%p): %p -> %p", lhandle, |
---|
| 808 | lhandle->lib->global_data_pointer, *(lhandle->lib->global_data_pointer), lhandle->context.global_data_state); |
---|
[5312] | 809 | *(lhandle->lib->global_data_pointer) = lhandle->context.global_data_state; |
---|
| 810 | } |
---|
| 811 | |
---|
[5834] | 812 | __swap_stdvios(lhandle); |
---|
| 813 | |
---|
[5312] | 814 | roar_err_store(&error_state); |
---|
| 815 | roar_err_restore(&(lhandle->context.error_state)); |
---|
| 816 | lhandle->context.error_state = error_state; |
---|
| 817 | |
---|
[5317] | 818 | ROAR_DBG("roar_dl_context_store(lhandle=%p) = 0", lhandle); |
---|
[5312] | 819 | return 0; |
---|
| 820 | } |
---|
| 821 | |
---|
[5836] | 822 | int roar_dl_appsched_trigger__handle_about(struct roar_dl_lhandle * lhandle) { |
---|
| 823 | libroar_dl_service_apitype(roar_service_about) api; |
---|
| 824 | int ret, err; |
---|
| 825 | |
---|
| 826 | if ( roar_dl_service_get_api(NULL, ROAR_SERVICE_ABOUT_NAME, ROAR_SERVICE_ABOUT_ABI, &api) == -1 ) |
---|
| 827 | return -1; |
---|
| 828 | |
---|
| 829 | ret = libroar_dl_service_run_func(api, show, int, roar_dl_getlibname(lhandle)); |
---|
| 830 | err = roar_error; |
---|
| 831 | |
---|
| 832 | libroar_dl_service_free_api(api); |
---|
| 833 | |
---|
| 834 | roar_error = err; |
---|
| 835 | return ret; |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | int roar_dl_appsched_trigger__handle_help(struct roar_dl_lhandle * lhandle) { |
---|
| 839 | libroar_dl_service_apitype(roar_service_help) api; |
---|
| 840 | int ret, err; |
---|
| 841 | |
---|
| 842 | if ( roar_dl_service_get_api(NULL, ROAR_SERVICE_HELP_NAME, ROAR_SERVICE_HELP_ABI, &api) == -1 ) |
---|
| 843 | return -1; |
---|
| 844 | |
---|
| 845 | ret = libroar_dl_service_run_func(api, show, int, roar_dl_getlibname(lhandle), NULL); |
---|
| 846 | err = roar_error; |
---|
| 847 | |
---|
| 848 | libroar_dl_service_free_api(api); |
---|
| 849 | |
---|
| 850 | roar_error = err; |
---|
| 851 | return ret; |
---|
| 852 | } |
---|
| 853 | |
---|
| 854 | int roar_dl_appsched_trigger__handle_preferences(struct roar_dl_lhandle * lhandle) { |
---|
| 855 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 856 | return -1; |
---|
| 857 | } |
---|
| 858 | |
---|
[5317] | 859 | int roar_dl_appsched_trigger(struct roar_dl_lhandle * lhandle, enum roar_dl_appsched_trigger trigger) { |
---|
[5335] | 860 | int (*func) (struct roar_dl_librarypara * para) = NULL; |
---|
[5317] | 861 | int ret; |
---|
| 862 | |
---|
| 863 | if ( (void*)lhandle < (void*)128 ) { |
---|
| 864 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 865 | return -1; |
---|
| 866 | } |
---|
| 867 | |
---|
[5446] | 868 | if ( lhandle->lib == NULL ) { |
---|
| 869 | roar_err_set(ROAR_ERROR_TYPEMM); |
---|
| 870 | return -1; |
---|
| 871 | } |
---|
| 872 | |
---|
[5317] | 873 | if ( lhandle->lib->appsched == NULL ) { |
---|
| 874 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 875 | return -1; |
---|
| 876 | } |
---|
| 877 | |
---|
| 878 | switch (trigger) { |
---|
| 879 | #define _trig(lname,uname) \ |
---|
| 880 | case ROAR_DL_APPSCHED_ ## uname : \ |
---|
[5335] | 881 | func = lhandle->lib->appsched->lname; \ |
---|
[5317] | 882 | break; |
---|
| 883 | _trig(init, INIT); |
---|
| 884 | _trig(free, FREE); |
---|
| 885 | _trig(update, UPDATE); |
---|
[5335] | 886 | _trig(tick, TICK); |
---|
| 887 | _trig(wait, WAIT); |
---|
[5836] | 888 | |
---|
| 889 | // not yet supported by struct roar_dl_appsched. |
---|
| 890 | case ROAR_DL_APPSCHED_ABOUT: |
---|
| 891 | case ROAR_DL_APPSCHED_HELP: |
---|
| 892 | case ROAR_DL_APPSCHED_PREFERENCES: |
---|
| 893 | func = NULL; |
---|
| 894 | break; |
---|
[5335] | 895 | // use ifndef here so warnings of unhandled enum values will be shown in DEBUG mode. |
---|
| 896 | #ifndef DEBUG |
---|
| 897 | default: |
---|
| 898 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
| 899 | return -1; |
---|
| 900 | break; |
---|
| 901 | #endif |
---|
[5317] | 902 | } |
---|
| 903 | |
---|
[5335] | 904 | if ( func == NULL ) { |
---|
[5503] | 905 | if ( trigger == ROAR_DL_APPSCHED_INIT ) { |
---|
| 906 | lhandle->runtime_flags |= RTF_APPSCHED_INITED|RTF_APPSCHED_FREED; |
---|
| 907 | lhandle->runtime_flags -= RTF_APPSCHED_FREED; |
---|
| 908 | } else if ( trigger == ROAR_DL_APPSCHED_FREE ) { |
---|
| 909 | lhandle->runtime_flags |= RTF_APPSCHED_INITED|RTF_APPSCHED_FREED; |
---|
| 910 | lhandle->runtime_flags -= RTF_APPSCHED_INITED; |
---|
| 911 | } |
---|
[5836] | 912 | switch (trigger) { |
---|
| 913 | case ROAR_DL_APPSCHED_ABOUT: |
---|
| 914 | return roar_dl_appsched_trigger__handle_about(lhandle); |
---|
| 915 | break; |
---|
| 916 | case ROAR_DL_APPSCHED_HELP: |
---|
| 917 | return roar_dl_appsched_trigger__handle_help(lhandle); |
---|
| 918 | break; |
---|
| 919 | case ROAR_DL_APPSCHED_PREFERENCES: |
---|
| 920 | return roar_dl_appsched_trigger__handle_preferences(lhandle); |
---|
| 921 | break; |
---|
| 922 | default: |
---|
| 923 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 924 | return -1; |
---|
| 925 | break; |
---|
| 926 | } |
---|
[5335] | 927 | } |
---|
| 928 | |
---|
[5503] | 929 | if ( trigger == ROAR_DL_APPSCHED_INIT ) { |
---|
| 930 | if ( lhandle->runtime_flags & RTF_APPSCHED_INITED ) { |
---|
| 931 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 932 | return -1; |
---|
| 933 | } |
---|
| 934 | } else { |
---|
| 935 | if ( !(lhandle->runtime_flags & RTF_APPSCHED_INITED) ) { |
---|
| 936 | roar_err_set(ROAR_ERROR_BUSY); |
---|
| 937 | return -1; |
---|
| 938 | } |
---|
| 939 | } |
---|
| 940 | |
---|
[5335] | 941 | roar_dl_context_restore(lhandle); |
---|
| 942 | ret = func(lhandle->para); |
---|
| 943 | roar_dl_context_store(lhandle); |
---|
[5503] | 944 | |
---|
| 945 | if ( trigger == ROAR_DL_APPSCHED_INIT ) { |
---|
| 946 | lhandle->runtime_flags |= RTF_APPSCHED_INITED|RTF_APPSCHED_FREED; |
---|
| 947 | lhandle->runtime_flags -= RTF_APPSCHED_FREED; |
---|
| 948 | } else if ( trigger == ROAR_DL_APPSCHED_FREE ) { |
---|
| 949 | lhandle->runtime_flags |= RTF_APPSCHED_INITED|RTF_APPSCHED_FREED; |
---|
| 950 | lhandle->runtime_flags -= RTF_APPSCHED_INITED; |
---|
| 951 | } |
---|
| 952 | |
---|
[5335] | 953 | return ret; |
---|
[5317] | 954 | } |
---|
| 955 | |
---|
[5511] | 956 | |
---|
| 957 | #define __MAX_FNREGS 24 |
---|
| 958 | static struct __fnregs { |
---|
| 959 | struct roar_dl_lhandle * lhandle; |
---|
| 960 | int subtype; |
---|
| 961 | const void * object; |
---|
| 962 | size_t objectlen; |
---|
| 963 | int version; |
---|
| 964 | int options; |
---|
| 965 | } __fnrefs[ROAR_DL_FN_MAX][__MAX_FNREGS]; |
---|
| 966 | |
---|
| 967 | static int __fnreg_check_trigger(const struct __fnregs * handle) { |
---|
| 968 | if ( handle->subtype != ROAR_DL_FNREG_SUBTYPE ) |
---|
| 969 | return -1; |
---|
| 970 | if ( handle->version != ROAR_DL_FNREG_VERSION ) |
---|
| 971 | return -1; |
---|
| 972 | if ( handle->objectlen != ROAR_DL_FNREG_SIZE ) |
---|
| 973 | return -1; |
---|
| 974 | |
---|
| 975 | return 0; |
---|
| 976 | } |
---|
| 977 | |
---|
| 978 | static void __fnreg_trigger_if_match(const struct roar_dl_fnreg * callback, |
---|
| 979 | const struct __fnregs * reg, |
---|
| 980 | int fn, |
---|
| 981 | enum roar_dl_fnreg_action action) { |
---|
| 982 | if ( callback->fn != -1 && callback->fn != fn ) |
---|
| 983 | return; |
---|
| 984 | if ( callback->subtype != -1 && callback->subtype != reg->subtype ) |
---|
| 985 | return; |
---|
| 986 | if ( callback->version != -1 && callback->version != reg->version ) |
---|
| 987 | return; |
---|
| 988 | |
---|
| 989 | if ( callback->callback == NULL ) |
---|
| 990 | return; |
---|
| 991 | |
---|
| 992 | callback->callback(action, fn, reg->subtype, reg->object, reg->objectlen, reg->version, reg->options, callback->userdata, reg->lhandle); |
---|
| 993 | } |
---|
| 994 | |
---|
| 995 | static void __fnreg_trigger_by_reg(const struct __fnregs * reg, enum roar_dl_fnreg_action action, int fn) { |
---|
| 996 | size_t j; |
---|
| 997 | |
---|
| 998 | for (j = 0; j < __MAX_FNREGS; j++) { |
---|
| 999 | if ( __fnrefs[ROAR_DL_FN_REGFN][j].lhandle != NULL ) { |
---|
| 1000 | if ( __fnreg_check_trigger(&(__fnrefs[ROAR_DL_FN_REGFN][j])) == -1 ) |
---|
| 1001 | continue; |
---|
| 1002 | __fnreg_trigger_if_match(__fnrefs[ROAR_DL_FN_REGFN][j].object, reg, fn, action); |
---|
| 1003 | } |
---|
| 1004 | } |
---|
| 1005 | } |
---|
| 1006 | |
---|
| 1007 | static void __fnreg_trigger_by_handler(const struct __fnregs * handle, enum roar_dl_fnreg_action action) { |
---|
| 1008 | size_t i, j; |
---|
| 1009 | |
---|
| 1010 | if ( __fnreg_check_trigger(handle) == -1 ) |
---|
| 1011 | return; |
---|
| 1012 | |
---|
| 1013 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
| 1014 | for (j = 0; j < __MAX_FNREGS; j++) { |
---|
| 1015 | if ( __fnrefs[i][j].lhandle != NULL ) { |
---|
| 1016 | __fnreg_trigger_if_match(handle->object, &(__fnrefs[i][j]), i, action); |
---|
| 1017 | } |
---|
| 1018 | } |
---|
| 1019 | } |
---|
| 1020 | } |
---|
| 1021 | |
---|
| 1022 | int roar_dl_register_fn(struct roar_dl_lhandle * lhandle, int fn, int subtype, const void * object, size_t objectlen, int version, int options) { |
---|
| 1023 | struct __fnregs * c = NULL; |
---|
| 1024 | size_t i; |
---|
| 1025 | |
---|
| 1026 | if ( lhandle == NULL ) |
---|
| 1027 | lhandle = __currently_inited; |
---|
| 1028 | |
---|
[5514] | 1029 | if ( fn == -1 ) |
---|
| 1030 | fn = __currently_inited_fn; |
---|
| 1031 | |
---|
[5511] | 1032 | if ( lhandle == NULL ) { |
---|
| 1033 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 1034 | return -1; |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | if ( fn < 0 || fn >= ROAR_DL_FN_MAX ) { |
---|
| 1038 | roar_err_set(ROAR_ERROR_RANGE); |
---|
| 1039 | return -1; |
---|
| 1040 | } |
---|
| 1041 | |
---|
| 1042 | if ( object == NULL || objectlen == 0 ) { |
---|
| 1043 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 1044 | return -1; |
---|
| 1045 | } |
---|
| 1046 | |
---|
| 1047 | for (i = 0; i < __MAX_FNREGS; i++) { |
---|
| 1048 | if ( __fnrefs[fn][i].lhandle != NULL ) |
---|
| 1049 | continue; |
---|
| 1050 | c = &(__fnrefs[fn][i]); |
---|
| 1051 | break; |
---|
| 1052 | } |
---|
| 1053 | |
---|
| 1054 | if ( c == NULL ) { |
---|
| 1055 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
| 1056 | return -1; |
---|
| 1057 | } |
---|
| 1058 | |
---|
| 1059 | c->lhandle = lhandle; |
---|
| 1060 | c->subtype = subtype; |
---|
| 1061 | c->object = object; |
---|
| 1062 | c->objectlen = objectlen; |
---|
| 1063 | c->version = version; |
---|
| 1064 | |
---|
| 1065 | if ( fn == ROAR_DL_FN_REGFN ) { |
---|
| 1066 | __fnreg_trigger_by_handler(c, ROAR_DL_FNREG); |
---|
| 1067 | } else { |
---|
| 1068 | __fnreg_trigger_by_reg(c, ROAR_DL_FNREG, fn); |
---|
| 1069 | } |
---|
| 1070 | |
---|
| 1071 | return 0; |
---|
| 1072 | } |
---|
| 1073 | |
---|
[5770] | 1074 | int roar_dl_unregister_fn2(struct roar_dl_lhandle * lhandle, int fn, int subtype, const void * object, size_t objectlen, int version, int options) { |
---|
| 1075 | struct __fnregs * c = NULL; |
---|
[5511] | 1076 | size_t i, j; |
---|
| 1077 | |
---|
| 1078 | if ( lhandle == NULL ) { |
---|
| 1079 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 1080 | return -1; |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
[5770] | 1084 | if ( fn != -1 && fn != i ) |
---|
| 1085 | continue; |
---|
| 1086 | |
---|
[5511] | 1087 | for (j = 0; j < __MAX_FNREGS; j++) { |
---|
[5770] | 1088 | c = &(__fnrefs[i][j]); |
---|
| 1089 | |
---|
| 1090 | if ( c->lhandle != lhandle ) |
---|
| 1091 | continue; |
---|
| 1092 | |
---|
| 1093 | if ( subtype != -1 && subtype != c->subtype ) |
---|
| 1094 | continue; |
---|
| 1095 | if ( object != NULL && object != c->object ) |
---|
| 1096 | continue; |
---|
| 1097 | if ( objectlen != (size_t)-1 && objectlen != c->objectlen ) |
---|
| 1098 | continue; |
---|
| 1099 | if ( version != -1 && version != c->version ) |
---|
| 1100 | continue; |
---|
| 1101 | if ( options != -1 && options != c->options ) |
---|
| 1102 | continue; |
---|
| 1103 | |
---|
| 1104 | if ( i == ROAR_DL_FN_REGFN ) { |
---|
| 1105 | __fnreg_trigger_by_handler(&(__fnrefs[i][j]), ROAR_DL_FNUNREG); |
---|
| 1106 | } else { |
---|
| 1107 | __fnreg_trigger_by_reg(&(__fnrefs[i][j]), ROAR_DL_FNUNREG, i); |
---|
[5511] | 1108 | } |
---|
[5770] | 1109 | memset(&(__fnrefs[i][j]), 0, sizeof(__fnrefs[i][j])); |
---|
| 1110 | __fnrefs[i][j].lhandle = NULL; |
---|
[5511] | 1111 | } |
---|
| 1112 | } |
---|
| 1113 | |
---|
| 1114 | return 0; |
---|
| 1115 | } |
---|
| 1116 | |
---|
[5770] | 1117 | int roar_dl_unregister_fn(struct roar_dl_lhandle * lhandle) { |
---|
| 1118 | return roar_dl_unregister_fn2(lhandle, -1, -1, NULL, (size_t)-1, -1, -1); |
---|
| 1119 | } |
---|
| 1120 | |
---|
[5819] | 1121 | static inline int __load_plugin_any_get_api(struct roar_dl_librarypara * para, const char * appname, const char * appabi, const char * servicename, const char * serviceabi, int universal, struct roar_dl_service_api * api) { |
---|
| 1122 | struct roar_dl_lhandle * lhandle = NULL; |
---|
| 1123 | struct roar_dl_librarypara * mypara = NULL; |
---|
| 1124 | char name[80]; |
---|
| 1125 | int err; |
---|
| 1126 | int ret; |
---|
| 1127 | |
---|
| 1128 | mypara = roar_dl_para_new(NULL, NULL, LIBROAR_DL_APPNAME, LIBROAR_DL_ABIVERSION); |
---|
| 1129 | if ( mypara == NULL ) |
---|
| 1130 | return -1; |
---|
| 1131 | |
---|
| 1132 | if ( serviceabi != NULL ) { |
---|
| 1133 | snprintf(name, sizeof(name), "service-%s-%s", servicename, serviceabi); |
---|
| 1134 | lhandle = roar_dl_open(name, ROAR_DL_FLAG_PLUGIN, 1, mypara); |
---|
| 1135 | } |
---|
| 1136 | |
---|
| 1137 | if ( lhandle == NULL ) { |
---|
| 1138 | snprintf(name, sizeof(name), "service-%s", servicename); |
---|
| 1139 | lhandle = roar_dl_open(name, ROAR_DL_FLAG_PLUGIN, 1, mypara); |
---|
| 1140 | } |
---|
| 1141 | |
---|
| 1142 | err = roar_error; |
---|
| 1143 | roar_dl_para_unref(mypara); |
---|
| 1144 | roar_error = err; |
---|
| 1145 | |
---|
| 1146 | if ( lhandle == NULL ) |
---|
| 1147 | return -1; |
---|
| 1148 | |
---|
| 1149 | roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_INIT); |
---|
| 1150 | |
---|
| 1151 | ret = libroar_dl_service_get_api_real(para, appname, appabi, servicename, serviceabi, universal, api, 0); |
---|
| 1152 | |
---|
| 1153 | err = roar_error; |
---|
| 1154 | roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_FREE); |
---|
| 1155 | roar_dl_unref(lhandle); |
---|
| 1156 | roar_error = err; |
---|
| 1157 | |
---|
| 1158 | return ret; |
---|
| 1159 | } |
---|
| 1160 | |
---|
| 1161 | int libroar_dl_service_get_api_real(struct roar_dl_librarypara * para, const char * appname, const char * appabi, const char * servicename, const char * serviceabi, int universal, struct roar_dl_service_api * api, int retry) { |
---|
[5814] | 1162 | const struct roar_dl_service * s; |
---|
| 1163 | struct __fnregs * c = NULL; |
---|
| 1164 | size_t i; |
---|
[5816] | 1165 | int err; |
---|
| 1166 | |
---|
[5819] | 1167 | if ( servicename == NULL || serviceabi == NULL || api == NULL ) { |
---|
[5816] | 1168 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 1169 | return -1; |
---|
| 1170 | } |
---|
| 1171 | |
---|
[5819] | 1172 | if ( para != NULL ) { |
---|
| 1173 | if ( roar_dl_para_ref(para) == -1 ) |
---|
| 1174 | return -1; |
---|
| 1175 | } else { |
---|
| 1176 | para = roar_dl_para_new(NULL, NULL, NULL, NULL); |
---|
| 1177 | if ( para == NULL ) |
---|
| 1178 | return -1; |
---|
| 1179 | } |
---|
| 1180 | |
---|
[5816] | 1181 | memset(api, 0, sizeof(struct roar_dl_service_api)); |
---|
[5814] | 1182 | |
---|
| 1183 | for (i = 0; i < __MAX_FNREGS; i++) { |
---|
| 1184 | c = &(__fnrefs[ROAR_DL_FN_SERVICE][i]); |
---|
| 1185 | s = c->object; |
---|
| 1186 | |
---|
| 1187 | if ( c->lhandle == NULL ) |
---|
| 1188 | continue; |
---|
| 1189 | |
---|
| 1190 | if ( c->subtype != ROAR_DL_SERVICE_SUBTYPE ) |
---|
| 1191 | continue; |
---|
| 1192 | if ( c->objectlen != ROAR_DL_SERVICE_SIZE ) |
---|
| 1193 | continue; |
---|
| 1194 | if ( c->version != ROAR_DL_SERVICE_VERSION ) |
---|
| 1195 | continue; |
---|
| 1196 | |
---|
| 1197 | if ( !universal && s->appname == NULL ) |
---|
| 1198 | continue; |
---|
[5817] | 1199 | if ( universal == 2 && s->appname != NULL ) |
---|
| 1200 | continue; |
---|
[5814] | 1201 | if ( s->appname != NULL && !!strcmp(s->appname, appname) ) |
---|
| 1202 | continue; |
---|
| 1203 | if ( (s->appabi == NULL && appabi != NULL) || (s->appabi != NULL && appabi == NULL) || |
---|
| 1204 | (s->appabi != NULL && appabi != NULL && !!strcmp(s->appabi, appabi) ) ) |
---|
| 1205 | continue; |
---|
| 1206 | if ( s->servicename == NULL || !!strcmp(s->servicename, servicename) ) |
---|
| 1207 | continue; |
---|
| 1208 | if ( s->serviceabi == NULL || !!strcmp(s->serviceabi, serviceabi) ) |
---|
| 1209 | continue; |
---|
| 1210 | |
---|
| 1211 | if ( s->get_api == NULL ) |
---|
| 1212 | continue; |
---|
| 1213 | |
---|
[5816] | 1214 | if ( roar_dl_ref(c->lhandle) != 0 ) |
---|
| 1215 | continue; |
---|
| 1216 | |
---|
[5818] | 1217 | api->service = s; |
---|
[5816] | 1218 | api->lhandle = c->lhandle; |
---|
| 1219 | |
---|
| 1220 | api->api = s->get_api(s, para); |
---|
| 1221 | if ( api->api == NULL ) { |
---|
| 1222 | err = roar_error; |
---|
| 1223 | roar_dl_unref(c->lhandle); |
---|
[5819] | 1224 | roar_dl_para_unref(para); |
---|
[5816] | 1225 | api->lhandle = NULL; |
---|
| 1226 | roar_error = err; |
---|
| 1227 | return -1; |
---|
[5815] | 1228 | } |
---|
| 1229 | |
---|
[5819] | 1230 | roar_dl_para_unref(para); |
---|
[5816] | 1231 | return 0; |
---|
[5814] | 1232 | } |
---|
| 1233 | |
---|
[5819] | 1234 | if ( retry ) { |
---|
| 1235 | if ( __load_plugin_any_get_api(para, appname, appabi, servicename, serviceabi, universal, api) == 0 ) { |
---|
| 1236 | roar_dl_para_unref(para); |
---|
| 1237 | return 0; |
---|
| 1238 | } |
---|
| 1239 | } |
---|
| 1240 | |
---|
| 1241 | roar_dl_para_unref(para); |
---|
[5814] | 1242 | roar_err_set(ROAR_ERROR_NOENT); |
---|
[5816] | 1243 | return -1; |
---|
| 1244 | } |
---|
| 1245 | |
---|
| 1246 | int libroar_dl_service_free_api_real(struct roar_dl_service_api * api) { |
---|
| 1247 | if ( api == NULL ) { |
---|
| 1248 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 1249 | return -1; |
---|
| 1250 | } |
---|
| 1251 | |
---|
| 1252 | roar_dl_unref(api->lhandle); |
---|
| 1253 | |
---|
| 1254 | memset(api, 0, sizeof(struct roar_dl_service_api)); |
---|
| 1255 | |
---|
| 1256 | return 0; |
---|
[5814] | 1257 | } |
---|
| 1258 | |
---|
[3297] | 1259 | //ll |
---|