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 "local.h"
31 #include "list.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
snd_dlopen(const char *name, int mode, char *errbuf, size_t errbuflen)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
snd_dlopen_old(const char *name, int mode)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 #ifndef DOC_HIDDEN
174 use_symbol_version(__snd_dlopen_old, snd_dlopen, ALSA_0.9);
175 use_default_symbol_version(__snd_dlopen, snd_dlopen, ALSA_1.1.6);
176 #endif /* DOC_HIDDEN */
177 
178 /**
179  * \brief Closes a dynamic library - ALSA wrapper for \c dlclose.
180  * \param handle Library handle, similar to \c dlclose.
181  * \return Zero if successful, otherwise an error code.
182  *
183  * This function can emulate dynamic linking for the static build of
184  * the alsa-lib library.
185  */
snd_dlclose(void *handle)186 int snd_dlclose(void *handle)
187 {
188 #ifndef PIC
189 	if (handle == &snd_dlsym_start)
190 		return 0;
191 #endif
192 #ifdef HAVE_LIBDL
193 	return dlclose(handle);
194 #else
195 	return 0;
196 #endif
197 }
198 
199 /**
200  * \brief Verifies a dynamically loaded symbol.
201  * \param handle Library handle, similar to \c dlsym.
202  * \param name Symbol name.
203  * \param version Version of the symbol.
204  * \return Zero is successful, otherwise a negative error code.
205  *
206  * This function checks that the symbol with the version appended to its name
207  * does exist in the library.
208  */
snd_dlsym_verify(void *handle, const char *name, const char *version)209 static int snd_dlsym_verify(void *handle, const char *name, const char *version)
210 {
211 #ifdef HAVE_LIBDL
212 	int res;
213 	char *vname;
214 
215 	if (handle == NULL)
216 		return -EINVAL;
217 	vname = alloca(1 + strlen(name) + strlen(version) + 1);
218 	if (vname == NULL)
219 		return -ENOMEM;
220 	vname[0] = '_';
221 	strcpy(vname + 1, name);
222 	strcat(vname, version);
223 	res = dlsym(handle, vname) == NULL ? -ENOENT : 0;
224 	// printf("dlsym verify: %i, vname = '%s'\n", res, vname);
225 	if (res < 0)
226 		SNDERR("unable to verify version for symbol %s", name);
227 	return res;
228 #else
229 	return 0;
230 #endif
231 }
232 
233 /**
234  * \brief Resolves a symbol from a dynamic library - ALSA wrapper for \c dlsym.
235  * \param handle Library handle, similar to \c dlsym.
236  * \param name Symbol name.
237  * \param version Version of the symbol.
238  *
239  * This function can emulate dynamic linking for the static build of
240  * the alsa-lib library.
241  *
242  * This special version of the \c dlsym function checks also the version
243  * of the symbol. A versioned symbol should be defined using the
244  * #SND_DLSYM_BUILD_VERSION macro.
245  */
snd_dlsym(void *handle, const char *name, const char *version)246 void *snd_dlsym(void *handle, const char *name, const char *version)
247 {
248 	int err;
249 
250 #ifndef PIC
251 	if (handle == &snd_dlsym_start) {
252 		/* it's the funny part: */
253 		/* we are looking for a symbol in a static library */
254 		struct snd_dlsym_link *link = snd_dlsym_start;
255 		while (link) {
256 			if (!strcmp(name, link->dlsym_name))
257 				return (void *)link->dlsym_ptr;
258 			link = link->next;
259 		}
260 		return NULL;
261 	}
262 #endif
263 #ifdef HAVE_LIBDL
264 #ifdef VERSIONED_SYMBOLS
265 	if (version) {
266 		err = snd_dlsym_verify(handle, name, version);
267 		if (err < 0)
268 			return NULL;
269 	}
270 #endif
271 	return dlsym(handle, name);
272 #else
273 	return NULL;
274 #endif
275 }
276 
277 /*
278  * dlobj cache
279  */
280 
281 #ifndef DOC_HIDDEN
282 struct dlobj_cache {
283 	const char *lib;
284 	const char *name;
285 	void *dlobj;
286 	void *func;
287 	unsigned int refcnt;
288 	struct list_head list;
289 };
290 
291 #ifdef HAVE_LIBPTHREAD
292 static pthread_mutex_t snd_dlobj_mutex = PTHREAD_MUTEX_INITIALIZER;
293 
snd_dlobj_lock(void)294 static inline void snd_dlobj_lock(void)
295 {
296 	pthread_mutex_lock(&snd_dlobj_mutex);
297 }
298 
snd_dlobj_unlock(void)299 static inline void snd_dlobj_unlock(void)
300 {
301 	pthread_mutex_unlock(&snd_dlobj_mutex);
302 }
303 #else
snd_dlobj_lock(void)304 static inline void snd_dlobj_lock(void) {}
snd_dlobj_unlock(void)305 static inline void snd_dlobj_unlock(void) {}
306 #endif
307 
308 static LIST_HEAD(pcm_dlobj_list);
309 
310 static struct dlobj_cache *
snd_dlobj_cache_get0(const char *lib, const char *name, const char *version, int verbose)311 snd_dlobj_cache_get0(const char *lib, const char *name,
312 		     const char *version, int verbose)
313 {
314 	struct list_head *p;
315 	struct dlobj_cache *c;
316 	void *func, *dlobj;
317 	char errbuf[256];
318 
319 	list_for_each(p, &pcm_dlobj_list) {
320 		c = list_entry(p, struct dlobj_cache, list);
321 		if (c->lib && lib && strcmp(c->lib, lib) != 0)
322 			continue;
323 		if (!c->lib && lib)
324 			continue;
325 		if (!lib && c->lib)
326 			continue;
327 		if (strcmp(c->name, name) == 0) {
328 			c->refcnt++;
329 			return c;
330 		}
331 	}
332 
333 	errbuf[0] = '\0';
334 	dlobj = INTERNAL(snd_dlopen)(lib, RTLD_NOW,
335 	                   verbose ? errbuf : 0,
336 	                   verbose ? sizeof(errbuf) : 0);
337 	if (dlobj == NULL) {
338 		if (verbose)
339 			SNDERR("Cannot open shared library %s (%s)",
340 						lib ? lib : "[builtin]",
341 						errbuf);
342 		return NULL;
343 	}
344 
345 	func = snd_dlsym(dlobj, name, version);
346 	if (func == NULL) {
347 		if (verbose)
348 			SNDERR("symbol %s is not defined inside %s",
349 					name, lib ? lib : "[builtin]");
350 		goto __err;
351 	}
352 	c = malloc(sizeof(*c));
353 	if (! c)
354 		goto __err;
355 	c->refcnt = 1;
356 	c->lib = lib ? strdup(lib) : NULL;
357 	c->name = strdup(name);
358 	if ((lib && ! c->lib) || ! c->name) {
359 		free((void *)c->name);
360 		free((void *)c->lib);
361 		free(c);
362 	      __err:
363 		snd_dlclose(dlobj);
364 		return NULL;
365 	}
366 	c->dlobj = dlobj;
367 	c->func = func;
368 	list_add_tail(&c->list, &pcm_dlobj_list);
369 	return c;
370 }
371 
snd_dlobj_cache_get(const char *lib, const char *name, const char *version, int verbose)372 void *snd_dlobj_cache_get(const char *lib, const char *name,
373 			  const char *version, int verbose)
374 {
375 	struct dlobj_cache *c;
376 	void *func = NULL;
377 
378 	snd_dlobj_lock();
379 	c = snd_dlobj_cache_get0(lib, name, version, verbose);
380 	if (c)
381 		func = c->func;
382 	snd_dlobj_unlock();
383 	return func;
384 }
385 
snd_dlobj_cache_get2(const char *lib, const char *name, const char *version, int verbose)386 void *snd_dlobj_cache_get2(const char *lib, const char *name,
387 			   const char *version, int verbose)
388 {
389 	struct dlobj_cache *c;
390 	void *func = NULL;
391 
392 	snd_dlobj_lock();
393 	c = snd_dlobj_cache_get0(lib, name, version, verbose);
394 	if (c) {
395 		func = c->func;
396 		/* double reference */
397 		c->refcnt++;
398 	}
399 	snd_dlobj_unlock();
400 	return func;
401 }
402 
snd_dlobj_cache_put(void *func)403 int snd_dlobj_cache_put(void *func)
404 {
405 	struct list_head *p;
406 	struct dlobj_cache *c;
407 	unsigned int refcnt;
408 
409 	if (!func)
410 		return -ENOENT;
411 
412 	snd_dlobj_lock();
413 	list_for_each(p, &pcm_dlobj_list) {
414 		c = list_entry(p, struct dlobj_cache, list);
415 		if (c->func == func) {
416 			refcnt = c->refcnt;
417 			if (c->refcnt > 0)
418 				c->refcnt--;
419 			snd_dlobj_unlock();
420 			return refcnt == 1 ? 0 : -EINVAL;
421 		}
422 	}
423 	snd_dlobj_unlock();
424 	return -ENOENT;
425 }
426 
snd_dlobj_cache_cleanup(void)427 void snd_dlobj_cache_cleanup(void)
428 {
429 	struct list_head *p, *npos;
430 	struct dlobj_cache *c;
431 
432 	snd_dlobj_lock();
433 	list_for_each_safe(p, npos, &pcm_dlobj_list) {
434 		c = list_entry(p, struct dlobj_cache, list);
435 		if (c->refcnt)
436 			continue;
437 		list_del(p);
438 		snd_dlclose(c->dlobj);
439 		free((void *)c->name); /* shut up gcc warning */
440 		free((void *)c->lib); /* shut up gcc warning */
441 		free(c);
442 	}
443 	snd_dlobj_unlock();
444 	snd_dlpath_lock();
445 	snd_plugin_dir_set = 0;
446 	free(snd_plugin_dir);
447 	snd_plugin_dir = NULL;
448 	snd_dlpath_unlock();
449 }
450 #endif
451