source: roaraudio/libroar/roardl.c @ 3517:1a3218a3fc5b

Last change on this file since 3517:1a3218a3fc5b was 3517:1a3218a3fc5b, checked in by phi, 14 years ago

updated license headers, FSF moved office

File size: 4.4 KB
Line 
1//roardl.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#if defined(ROAR_HAVE_LIBDL) && !defined(RTLD_NEXT)
39#define RTLD_NEXT ((void *) -1L)
40#endif
41
42#if defined(ROAR_HAVE_LIBDL)
43static 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
54struct roar_dl_lhandle * roar_dl_open(const char * filename, int flags, int ra_init) {
55 struct roar_dl_lhandle * ret = NULL;
56
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)
68 flags  = RTLD_NOW;
69
70#ifdef RTLD_DEEPBIND
71 flags |= RTLD_DEEPBIND;
72#endif
73
74 ret->handle = dlopen(filename, flags);
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
85 if ( ret == NULL )
86  return NULL;
87
88 if ( ra_init ) {
89  roar_dl_ra_init(ret, NULL);
90 }
91
92 return ret;
93}
94
95int                      roar_dl_close(struct roar_dl_lhandle * lhandle) {
96 int ret = -1;
97
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
106#if defined(ROAR_HAVE_LIBDL)
107 ret = dlclose(_roardl2ldl(lhandle));
108#else
109 ret = -1;
110#endif
111
112 roar_mm_free(lhandle);
113
114 return ret;
115}
116
117void                   * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) {
118#if defined(ROAR_HAVE_LIBDL)
119 return dlsym(_roardl2ldl(lhandle), sym);
120#else
121 return NULL;
122#endif
123}
124
125int                      roar_dl_ra_init(struct roar_dl_lhandle * lhandle, const char * prefix) {
126#define _SUFFIX "_roaraudio_library_init"
127 char name[80] = _SUFFIX;
128 struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para);
129 struct roar_dl_libraryinst * lib;
130 struct roar_dl_librarypara * para = NULL;
131 int i;
132
133 if ( (void*)lhandle < (void*)128 ) {
134  if ( prefix == NULL )
135   return -1;
136 } else {
137  para = lhandle->para;
138 }
139
140
141 if ( prefix != NULL ) {
142  strcpy(name, "_");
143  strcat(name, prefix);
144  strcat(name, _SUFFIX);
145 }
146
147 ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name);
148
149 if ( (func = roar_dl_getsym(lhandle, name, -1)) == NULL )
150  return -1;
151
152 ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func);
153
154 lib = func(para);
155
156 if ( lib == NULL )
157  return -1;
158
159 if ( lib->version != ROAR_DL_LIBINST_VERSION )
160  return -1;
161
162 if ( sizeof(struct roar_dl_libraryinst) > lib->len )
163  return -1;
164
165 if ( !((void*)lhandle < (void*)128) ) {
166  lhandle->lib = lib;
167 }
168
169 for (i = 0; i < ROAR_DL_FN_MAX; i++) {
170  if ( lib->func[i] != NULL )
171   lib->func[i](para, lib);
172 }
173
174 return 0;
175}
176
177char *                   roar_dl_errstr(struct roar_dl_lhandle * lhandle) {
178#if defined(ROAR_HAVE_LIBDL)
179 return dlerror();
180#else
181 return NULL;
182#endif
183}
184
185//ll
Note: See TracBrowser for help on using the repository browser.