source: roaraudio/libroar/roardl.c @ 5271:09aa484b25f4

Last change on this file since 5271:09aa484b25f4 was 5270:e25346c13638, checked in by phi, 12 years ago

fixed some gcc -Wextra warnings

File size: 4.5 KB
Line 
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)
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 void * ret = dlsym(_roardl2ldl(lhandle), sym);
120
121 (void)type;
122
123 ROAR_DBG("roar_dl_getsym(lhandle=%p, sym='%s', type=%i) = %p", lhandle, sym, type, ret);
124
125 return ret;
126#else
127 return NULL;
128#endif
129}
130
131int                      roar_dl_ra_init(struct roar_dl_lhandle * lhandle, const char * prefix) {
132#define _SUFFIX "_roaraudio_library_init"
133 char name[80] = _SUFFIX;
134 struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para);
135 struct roar_dl_libraryinst * lib;
136 struct roar_dl_librarypara * para = NULL;
137 int i;
138
139 if ( (void*)lhandle < (void*)128 ) {
140  if ( prefix == NULL )
141   return -1;
142 } else {
143  para = lhandle->para;
144 }
145
146
147 if ( prefix != NULL ) {
148  roar_mm_strscpy(name, "_");
149  roar_mm_strscat(name, prefix);
150  roar_mm_strscat(name, _SUFFIX);
151 }
152
153 ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): name='%s'", lhandle, prefix, name);
154
155 if ( (func = roar_dl_getsym(lhandle, name, -1)) == NULL )
156  return -1;
157
158 ROAR_DBG("roar_dl_ra_init(lhandle=%p, prefix='%s'): func=%p", lhandle, prefix, func);
159
160 lib = func(para);
161
162 if ( lib == NULL )
163  return -1;
164
165 if ( lib->version != ROAR_DL_LIBINST_VERSION )
166  return -1;
167
168 if ( sizeof(struct roar_dl_libraryinst) > lib->len )
169  return -1;
170
171 if ( !((void*)lhandle < (void*)128) ) {
172  lhandle->lib = lib;
173 }
174
175 for (i = 0; i < ROAR_DL_FN_MAX; i++) {
176  if ( lib->func[i] != NULL )
177   lib->func[i](para, lib);
178 }
179
180 return 0;
181}
182
183char *                   roar_dl_errstr(struct roar_dl_lhandle * lhandle) {
184#if defined(ROAR_HAVE_LIBDL)
185 (void)lhandle;
186 return dlerror();
187#else
188 return NULL;
189#endif
190}
191
192//ll
Note: See TracBrowser for help on using the repository browser.