1 | //roardl.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
5 | * |
---|
6 | * This file is part of libroar a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroar is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | #if defined(ROAR_HAVE_LIBDL) && !defined(RTLD_NEXT) |
---|
39 | #define RTLD_NEXT ((void *) -1L) |
---|
40 | #endif |
---|
41 | |
---|
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) { |
---|
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 | |
---|
95 | int 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 | |
---|
117 | void * roar_dl_getsym(struct roar_dl_lhandle * lhandle, const char * sym, int type) { |
---|
118 | #if defined(ROAR_HAVE_LIBDL) |
---|
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; |
---|
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; |
---|
134 | struct roar_dl_librarypara * para = NULL; |
---|
135 | int i; |
---|
136 | |
---|
137 | if ( (void*)lhandle < (void*)128 ) { |
---|
138 | if ( prefix == NULL ) |
---|
139 | return -1; |
---|
140 | } else { |
---|
141 | para = lhandle->para; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | if ( prefix != NULL ) { |
---|
146 | strcpy(name, "_"); |
---|
147 | strcat(name, prefix); |
---|
148 | strcat(name, _SUFFIX); |
---|
149 | } |
---|
150 | |
---|
151 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name); |
---|
152 | |
---|
153 | if ( (func = roar_dl_getsym(lhandle, name, -1)) == NULL ) |
---|
154 | return -1; |
---|
155 | |
---|
156 | ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func); |
---|
157 | |
---|
158 | lib = func(para); |
---|
159 | |
---|
160 | if ( lib == NULL ) |
---|
161 | return -1; |
---|
162 | |
---|
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 | |
---|
169 | if ( !((void*)lhandle < (void*)128) ) { |
---|
170 | lhandle->lib = lib; |
---|
171 | } |
---|
172 | |
---|
173 | for (i = 0; i < ROAR_DL_FN_MAX; i++) { |
---|
174 | if ( lib->func[i] != NULL ) |
---|
175 | lib->func[i](para, lib); |
---|
176 | } |
---|
177 | |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
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 | |
---|
189 | //ll |
---|