Debian Codegraph @

src/compat/compat_dl.c

raw
1 /*
2 compat_dl: dynamic loading of shared libs
3
4 This is a separate libcompat to avoid needlessly linking all mpg123 code
5 with libdl on Unix.
6
7 copyright 2007-2019 by the mpg123 project - free software under the terms of the LGPL 2.1
8 see COPYING and AUTHORS files in distribution or http://mpg123.org
9 initially written by Thomas Orgis, Windows Unicode stuff by JonY.
10 */
11
12 #include "config.h"
13 /* This source file does need _POSIX_SOURCE to get some sigaction. */
14 #define _POSIX_SOURCE
15 /* Fix pedantic error about w2upath being unused */
16 #define HIDE_w2upath
17 #include "compat.h"
18
19 #ifdef _MSC_VER
20 #include <io.h>
21
22 #if(defined(WINAPI_FAMILY) && (WINAPI_FAMILY==WINAPI_FAMILY_APP))
23 #define WINDOWS_UWP
24 #endif
25
26 #endif
27 #ifdef HAVE_SYS_STAT_H
28 # include <sys/stat.h>
29 #endif
30 #ifdef HAVE_DIRENT_H
31 # include <dirent.h>
32 #endif
33
34 /* Win32 is only supported with unicode now. These headers also cover
35 module stuff. The WANT_WIN32_UNICODE macro is synonymous with
36 "want windows-specific API, and only the unicode variants of which". */
37 #ifdef WANT_WIN32_UNICODE
38 #include <wchar.h>
39 #include <windows.h>
40 #include <winnls.h>
41 #include <shlwapi.h>
42 #endif
43
44 #ifdef USE_MODULES
45 # ifdef HAVE_DLFCN_H
46 # include <dlfcn.h>
47 # endif
48 #endif
49
50 #include "../common/debug.h"
51
52
53 #ifdef USE_MODULES
54 #include "wpathconv.h"
55 /*
56 This is what I expected the platform-specific dance for dynamic module
57 support to be. Little did I know about the peculiarities of (long)
58 paths and directory/file search on Windows.
59
60 LoadLibrary throws GUI error boxes, use SetThreadErrorMode to suppress.
61 It needs to be done on per-thread basis to avoid race conditions
62 clobbering each other when setting/restoring across different threads.
63 */
64
65 void *INT123_compat_dlopen(const char *path)
66 {
67 void *handle = NULL;
68 #ifdef WANT_WIN32_UNICODE
69 wchar_t *wpath;
70 wpath = u2wlongpath(path);
71 if(wpath) {
72 DWORD emode = GetThreadErrorMode();
73 int mode_ok = SetThreadErrorMode(emode | SEM_FAILCRITICALERRORS, NULL);
74 handle = LoadLibraryW(wpath);
75 if(mode_ok) {
76 SetThreadErrorMode(emode, NULL);
77 }
78 }
79 free(wpath);
80 #else
81 handle = dlopen(path, RTLD_NOW);
82 #endif
83 return handle;
84 }
85
86 void *INT123_compat_dlsym(void *handle, const char *name)
87 {
88 void *sym = NULL;
89 if(!handle)
90 return NULL;
91 #ifdef WANT_WIN32_UNICODE
92 sym = (void *)(uintptr_t)GetProcAddress(handle, name);
93 #else
94 sym = dlsym(handle, name);
95 #endif
96 return sym;
97 }
98
99 void INT123_compat_dlclose(void *handle)
100 {
101 if(!handle)
102 return;
103 #ifdef WANT_WIN32_UNICODE
104 FreeLibrary(handle);
105 #else
106 dlclose(handle);
107 #endif
108 }
109
110 #endif /* USE_MODULES */