• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file dlmisc.c
3  * \brief dynamic loader helpers
4  * \author Jaroslav Kysela <perex@perex.cz>
5  * \date 2001
6  *
7  * Dynamic loader helpers
8  */
9 /*
10  *  Dynamic loader helpers
11  *  Copyright (c) 2000 by Jaroslav Kysela <perex@perex.cz>
12  *
13  *
14  *   This library is free software; you can redistribute it and/or modify
15  *   it under the terms of the GNU Lesser General Public License as
16  *   published by the Free Software Foundation; either version 2.1 of
17  *   the License, or (at your option) any later version.
18  *
19  *   This program is distributed in the hope that it will be useful,
20  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   GNU Lesser General Public License for more details.
23  *
24  *   You should have received a copy of the GNU Lesser General Public
25  *   License along with this library; if not, write to the Free Software
26  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
27  *
28  */
29 
30 #include "list.h"
31 #include "local.h"
32 #ifdef HAVE_LIBPTHREAD
33 #include <pthread.h>
34 #endif
35 #include <limits.h>
36 
37 #if defined(HAVE_LIBDL) && defined(__GLIBC__) && !defined(__UCLIBC__)
38 #define DL_ORIGIN_AVAILABLE 1
39 #endif
40 
41 #ifndef DOC_HIDDEN
42 #ifndef PIC
43 struct snd_dlsym_link *snd_dlsym_start = NULL;
44 #endif
45 static int snd_plugin_dir_set = 0;
46 static char *snd_plugin_dir = NULL;
47 #endif
48 
49 #ifdef HAVE_LIBPTHREAD
50 static pthread_mutex_t snd_dlpath_mutex = PTHREAD_MUTEX_INITIALIZER;
51 
snd_dlpath_lock(void)52 static inline void snd_dlpath_lock(void)
53 {
54 	pthread_mutex_lock(&snd_dlpath_mutex);
55 }
56 
snd_dlpath_unlock(void)57 static inline void snd_dlpath_unlock(void)
58 {
59 	pthread_mutex_unlock(&snd_dlpath_mutex);
60 }
61 #else
snd_dlpath_lock(void)62 static inline void snd_dlpath_lock(void) {}
snd_dlpath_unlock(void)63 static inline void snd_dlpath_unlock(void) {}
64 #endif
65 
snd_dlinfo_origin(char * path,size_t path_len)66 static void snd_dlinfo_origin(char *path, size_t path_len)
67 {
68 #ifdef DL_ORIGIN_AVAILABLE
69 	struct link_map *links;
70 	Dl_info info;
71 	char origin[PATH_MAX];
72 	if (dladdr1(&snd_dlpath, &info, (void**)&links, RTLD_DL_LINKMAP) == 0)
73 		return;
74 	if (dlinfo(links, RTLD_DI_ORIGIN, origin))
75 		return;
76 	snprintf(path, path_len, "%s/alsa-lib", origin);
77 	if (access(path, X_OK) == 0)
78 		snd_plugin_dir = strdup(path);
79 #endif
80 }
81 
82 /**
83  *
84  * \brief Compose the dynamic path
85  * \param path Returned path (string)
86  * \param path_len Returned path max size (with trailing zero)
87  * \param name Plugin name (relative)
88  * \return Zero on success, otherwise a negative error code
89  */
snd_dlpath(char * path,size_t path_len,const char * name)90 int snd_dlpath(char *path, size_t path_len, const char *name)
91 {
92 	snd_dlpath_lock();
93 	if (!snd_plugin_dir_set) {
94 		const char *env = getenv("ALSA_PLUGIN_DIR");
95 		if (env) {
96 			snd_plugin_dir = strdup(env);
97 		} else {
98 			snd_dlinfo_origin(path, path_len);
99 		}
100 		snd_plugin_dir_set = 1;
101 	}
102 	snprintf(path, path_len, "%s/%s",
103 		 snd_plugin_dir ? snd_plugin_dir : ALSA_PLUGIN_DIR, name);
104 	snd_dlpath_unlock();
105 	return 0;
106 }
107 
108 /**
109  * \brief Opens a dynamic library - ALSA wrapper for \c dlopen.
110  * \param name name of the library, similar to \c dlopen.
111  * \param mode mode flags, similar to \c dlopen.
112  * \param errbuf a string buffer for the error message \c dlerror.
113  * \param errbuflen a length of the string buffer for the error message.
114  * \return Library handle if successful, otherwise \c NULL.
115  *
116  * This function can emulate dynamic linking for the static build of
117  * the alsa-lib library. In that case, \p name is set to \c NULL.
118  */
119 #ifndef DOXYGEN
INTERNAL(snd_dlopen)120 EXPORT_SYMBOL void *INTERNAL(snd_dlopen)(const char *name, int mode, char *errbuf, size_t errbuflen)
121 #else
122 void *snd_dlopen(const char *name, int mode, char *errbuf, size_t errbuflen)
123 #endif
124 {
125 #ifndef PIC
126 	if (name == NULL)
127 		return &snd_dlsym_start;
128 #else
129 #ifdef HAVE_LIBDL
130 	if (name == NULL) {
131 		static const char * self = NULL;
132 		if (self == NULL) {
133 			Dl_info dlinfo;
134 			if (dladdr(snd_dlopen, &dlinfo) > 0)
135 				self = dlinfo.dli_fname;
136 		}
137 		name = self;
138 	}
139 #endif
140 #endif
141 #ifdef HAVE_LIBDL
142 	/*
143 	 * Handle the plugin dir not being on the default dlopen search
144 	 * path, without resorting to polluting the entire system namespace
145 	 * via ld.so.conf.
146 	 */
147 	void *handle = NULL;
148 	const char *filename = name;
149 	char path[PATH_MAX];
150 
151 	if (name && name[0] != '/') {
152 		if (snd_dlpath(path, sizeof(path), name) == 0)
153 			filename = path;
154 	}
155 	handle = dlopen(filename, mode);
156 	if (!handle)
157 		goto errpath;
158 	return handle;
159 errpath:
160 	if (errbuf)
161 		snprintf(errbuf, errbuflen, "%s", dlerror());
162 #endif
163 	return NULL;
164 }
165 
166 #ifndef DOXYGEN
INTERNAL(snd_dlopen_old)167 EXPORT_SYMBOL void *INTERNAL(snd_dlopen_old)(const char *name, int mode)
168 {
169   return INTERNAL(snd_dlopen)(name, mode, NULL, 0);
170 }
171 #endif
172 
173 use_symbol_version(__snd_dlopen_old, snd_dlopen, ALSA_0.9);
174 use_default_symbol_version(__snd_dlopen, snd_dlopen, ALSA_1.1.6);
175 
176 /**
177  * \brief Closes a dynamic library - ALSA wrapper for \c dlclose.
178  * \param handle Library handle, similar to \c dlclose.
179  * \return Zero if successful, otherwise an error code.
180  *
181  * This function can emulate dynamic linking for the static build of
182  * the alsa-lib library.
183  */
snd_dlclose(void * handle)184 int snd_dlclose(void *handle)
185 {
186 #ifndef PIC
187 	if (handle == &snd_dlsym_start)
188 		return 0;
189 #endif
190 #ifdef HAVE_LIBDL
191 	return dlclose(handle);
192 #else
193 	return 0;
194 #endif
195 }
196 
197 /**
198  * \brief Verifies a dynamically loaded symbol.
199  * \param handle Library handle, similar to \c dlsym.
200  * \param name Symbol name.
201  * \param version Version of the symbol.
202  * \return Zero is successful, otherwise a negative error code.
203  *
204  * This function checks that the symbol with the version appended to its name
205  * does exist in the library.
206  */
snd_dlsym_verify(void * handle,const char * name,const char * version)207 static int snd_dlsym_verify(void *handle, const char *name, const char *version)
208 {
209 #ifdef HAVE_LIBDL
210 	int res;
211 	char *vname;
212 
213 	if (handle == NULL)
214 		return -EINVAL;
215 	vname = alloca(1 + strlen(name) + strlen(version) + 1);
216 	if (vname == NULL)
217 		return -ENOMEM;
218 	vname[0] = '_';
219 	strcpy(vname + 1, name);
220 	strcat(vname, version);
221 	res = dlsym(handle, vname) == NULL ? -ENOENT : 0;
222 	// printf("dlsym verify: %i, vname = '%s'\n", res, vname);
223 	if (res < 0)
224 		SNDERR("unable to verify version for symbol %s", name);
225 	return res;
226 #else
227 	return 0;
228 #endif
229 }
230 
231 /**
232  * \brief Resolves a symbol from a dynamic library - ALSA wrapper for \c dlsym.
233  * \param handle Library handle, similar to \c dlsym.
234  * \param name Symbol name.
235  * \param version Version of the symbol.
236  *
237  * This function can emulate dynamic linking for the static build of
238  * the alsa-lib library.
239  *
240  * This special version of the \c dlsym function checks also the version
241  * of the symbol. A versioned symbol should be defined using the
242  * #SND_DLSYM_BUILD_VERSION macro.
243  */
snd_dlsym(void * handle,const char * name,const char * version)244 void *snd_dlsym(void *handle, const char *name, const char *version)
245 {
246 	int err;
247 
248 #ifndef PIC
249 	if (handle == &snd_dlsym_start) {
250 		/* it's the funny part: */
251 		/* we are looking for a symbol in a static library */
252 		struct snd_dlsym_link *link = snd_dlsym_start;
253 		while (link) {
254 			if (!strcmp(name, link->dlsym_name))
255 				return (void *)link->dlsym_ptr;
256 			link = link->next;
257 		}
258 		return NULL;
259 	}
260 #endif
261 #ifdef HAVE_LIBDL
262 #ifdef VERSIONED_SYMBOLS
263 	if (version) {
264 		err = snd_dlsym_verify(handle, name, version);
265 		if (err < 0)
266 			return NULL;
267 	}
268 #endif
269 	return dlsym(handle, name);
270 #else
271 	return NULL;
272 #endif
273 }
274 
275 /*
276  * dlobj cache
277  */
278 
279 #ifndef DOC_HIDDEN
280 struct dlobj_cache {
281 	const char *lib;
282 	const char *name;
283 	void *dlobj;
284 	void *func;
285 	unsigned int refcnt;
286 	struct list_head list;
287 };
288 
289 #ifdef HAVE_LIBPTHREAD
290 static pthread_mutex_t snd_dlobj_mutex = PTHREAD_MUTEX_INITIALIZER;
291 
snd_dlobj_lock(void)292 static inline void snd_dlobj_lock(void)
293 {
294 	pthread_mutex_lock(&snd_dlobj_mutex);
295 }
296 
snd_dlobj_unlock(void)297 static inline void snd_dlobj_unlock(void)
298 {
299 	pthread_mutex_unlock(&snd_dlobj_mutex);
300 }
301 #else
snd_dlobj_lock(void)302 static inline void snd_dlobj_lock(void) {}
snd_dlobj_unlock(void)303 static inline void snd_dlobj_unlock(void) {}
304 #endif
305 
306 static LIST_HEAD(pcm_dlobj_list);
307 
308 static struct dlobj_cache *
snd_dlobj_cache_get0(const char * lib,const char * name,const char * version,int verbose)309 snd_dlobj_cache_get0(const char *lib, const char *name,
310 		     const char *version, int verbose)
311 {
312 	struct list_head *p;
313 	struct dlobj_cache *c;
314 	void *func, *dlobj;
315 	char errbuf[256];
316 
317 	list_for_each(p, &pcm_dlobj_list) {
318 		c = list_entry(p, struct dlobj_cache, list);
319 		if (c->lib && lib && strcmp(c->lib, lib) != 0)
320 			continue;
321 		if (!c->lib && lib)
322 			continue;
323 		if (!lib && c->lib)
324 			continue;
325 		if (strcmp(c->name, name) == 0) {
326 			c->refcnt++;
327 			return c;
328 		}
329 	}
330 
331 	errbuf[0] = '\0';
332 	dlobj = INTERNAL(snd_dlopen)(lib, RTLD_NOW,
333 	                   verbose ? errbuf : 0,
334 	                   verbose ? sizeof(errbuf) : 0);
335 	if (dlobj == NULL) {
336 		if (verbose)
337 			SNDERR("Cannot open shared library %s (%s)",
338 						lib ? lib : "[builtin]",
339 						errbuf);
340 		return NULL;
341 	}
342 
343 	func = snd_dlsym(dlobj, name, version);
344 	if (func == NULL) {
345 		if (verbose)
346 			SNDERR("symbol %s is not defined inside %s",
347 					name, lib ? lib : "[builtin]");
348 		goto __err;
349 	}
350 	c = malloc(sizeof(*c));
351 	if (! c)
352 		goto __err;
353 	c->refcnt = 1;
354 	c->lib = lib ? strdup(lib) : NULL;
355 	c->name = strdup(name);
356 	if ((lib && ! c->lib) || ! c->name) {
357 		free((void *)c->name);
358 		free((void *)c->lib);
359 		free(c);
360 	      __err:
361 		snd_dlclose(dlobj);
362 		return NULL;
363 	}
364 	c->dlobj = dlobj;
365 	c->func = func;
366 	list_add_tail(&c->list, &pcm_dlobj_list);
367 	return c;
368 }
369 
snd_dlobj_cache_get(const char * lib,const char * name,const char * version,int verbose)370 void *snd_dlobj_cache_get(const char *lib, const char *name,
371 			  const char *version, int verbose)
372 {
373 	struct dlobj_cache *c;
374 	void *func = NULL;
375 
376 	snd_dlobj_lock();
377 	c = snd_dlobj_cache_get0(lib, name, version, verbose);
378 	if (c)
379 		func = c->func;
380 	snd_dlobj_unlock();
381 	return func;
382 }
383 
snd_dlobj_cache_get2(const char * lib,const char * name,const char * version,int verbose)384 void *snd_dlobj_cache_get2(const char *lib, const char *name,
385 			   const char *version, int verbose)
386 {
387 	struct dlobj_cache *c;
388 	void *func = NULL;
389 
390 	snd_dlobj_lock();
391 	c = snd_dlobj_cache_get0(lib, name, version, verbose);
392 	if (c) {
393 		func = c->func;
394 		/* double reference */
395 		c->refcnt++;
396 	}
397 	snd_dlobj_unlock();
398 	return func;
399 }
400 
snd_dlobj_cache_put(void * func)401 int snd_dlobj_cache_put(void *func)
402 {
403 	struct list_head *p;
404 	struct dlobj_cache *c;
405 	unsigned int refcnt;
406 
407 	if (!func)
408 		return -ENOENT;
409 
410 	snd_dlobj_lock();
411 	list_for_each(p, &pcm_dlobj_list) {
412 		c = list_entry(p, struct dlobj_cache, list);
413 		if (c->func == func) {
414 			refcnt = c->refcnt;
415 			if (c->refcnt > 0)
416 				c->refcnt--;
417 			snd_dlobj_unlock();
418 			return refcnt == 1 ? 0 : -EINVAL;
419 		}
420 	}
421 	snd_dlobj_unlock();
422 	return -ENOENT;
423 }
424 
snd_dlobj_cache_cleanup(void)425 void snd_dlobj_cache_cleanup(void)
426 {
427 	struct list_head *p, *npos;
428 	struct dlobj_cache *c;
429 
430 	snd_dlobj_lock();
431 	list_for_each_safe(p, npos, &pcm_dlobj_list) {
432 		c = list_entry(p, struct dlobj_cache, list);
433 		if (c->refcnt)
434 			continue;
435 		list_del(p);
436 		snd_dlclose(c->dlobj);
437 		free((void *)c->name); /* shut up gcc warning */
438 		free((void *)c->lib); /* shut up gcc warning */
439 		free(c);
440 	}
441 	snd_dlobj_unlock();
442 	snd_dlpath_lock();
443 	snd_plugin_dir_set = 0;
444 	free(snd_plugin_dir);
445 	snd_plugin_dir = NULL;
446 	snd_dlpath_unlock();
447 }
448 #endif
449