| 1 |
/* |
| 2 |
module.c: modular code loader |
| 3 |
|
| 4 |
copyright 1995-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Nicholas J Humfrey |
| 7 |
*/ |
| 8 |
|
| 9 |
/* Need snprintf(). */ |
| 10 |
#define _DEFAULT_SOURCE |
| 11 |
#define _BSD_SOURCE |
| 12 |
#include "config.h" |
| 13 |
#include "stringlists.h" |
| 14 |
#include "../compat/compat.h" |
| 15 |
#include <errno.h> |
| 16 |
|
| 17 |
#include "module.h" |
| 18 |
#include "../common/debug.h" |
| 19 |
|
| 20 |
#ifndef USE_MODULES |
| 21 |
#error This is a build without modules. Why am I here? |
| 22 |
#endif |
| 23 |
|
| 24 |
#define MODULE_SYMBOL_PREFIX "mpg123_" |
| 25 |
#define MODULE_SYMBOL_SUFFIX "_module_info" |
| 26 |
|
| 27 |
/* Windows code can convert these from UTF-8 (or ASCII, does not matter) |
| 28 |
to wide and then replace / by \. No need to define another list. */ |
| 29 |
static const char* modulesearch[] = |
| 30 |
{ |
| 31 |
"../lib/mpg123" |
| 32 |
,"plugins" |
| 33 |
,"libout123/modules/.libs" |
| 34 |
,"libout123/modules" |
| 35 |
,"../libout123/modules/.libs" |
| 36 |
,"../libout123/modules" |
| 37 |
}; |
| 38 |
|
| 39 |
static char *get_module_dir(int verbose, const char* bindir) |
| 40 |
{ |
| 41 |
char *moddir = NULL; |
| 42 |
char *defaultdir; |
| 43 |
/* First the environment override, then relative to bindir, then installation prefix. */ |
| 44 |
defaultdir = INT123_compat_getenv("MPG123_MODDIR"); |
| 45 |
if(defaultdir) |
| 46 |
{ |
| 47 |
if(verbose > 1) |
| 48 |
fprintf(stderr, "Trying module directory from environment: %s\n", defaultdir); |
| 49 |
if(INT123_compat_isdir(defaultdir)) |
| 50 |
moddir = defaultdir; |
| 51 |
else |
| 52 |
free(defaultdir); |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
if(bindir) /* Search relative to binary. */ |
| 57 |
{ |
| 58 |
size_t i; |
| 59 |
if(verbose > 1) |
| 60 |
fprintf(stderr, "Module dir search relative to: %s\n", bindir); |
| 61 |
for(i=0; i<sizeof(modulesearch)/sizeof(char*); ++i) |
| 62 |
{ |
| 63 |
moddir = INT123_compat_catpath(bindir, modulesearch[i]); |
| 64 |
if(!moddir) |
| 65 |
continue; |
| 66 |
if(verbose > 1) |
| 67 |
fprintf(stderr, "Looking for module dir: %s\n", moddir); |
| 68 |
if(INT123_compat_isdir(moddir)) |
| 69 |
break; /* found it! */ |
| 70 |
else |
| 71 |
{ |
| 72 |
free(moddir); |
| 73 |
moddir=NULL; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
if(!moddir) /* Resort to installation prefix. */ |
| 78 |
{ |
| 79 |
if(INT123_compat_isdir(PKGLIBDIR)) |
| 80 |
{ |
| 81 |
if(verbose > 1) |
| 82 |
fprintf(stderr, "Using default module dir: %s\n", PKGLIBDIR); |
| 83 |
moddir = INT123_compat_strdup(PKGLIBDIR); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if(verbose > 1) |
| 89 |
fprintf(stderr, "Module dir: %s\n", moddir != NULL ? moddir : "<nil>"); |
| 90 |
return moddir; |
| 91 |
} |
| 92 |
|
| 93 |
/* Open a module in given directory. */ |
| 94 |
mpg123_module_t* open_module_here( const char *dir, const char* type |
| 95 |
, const char* name, int verbose ) |
| 96 |
{ |
| 97 |
void *handle = NULL; |
| 98 |
mpg123_module_t *module = NULL; |
| 99 |
char *module_file = NULL; |
| 100 |
size_t module_file_len = 0; |
| 101 |
char *module_symbol = NULL; |
| 102 |
size_t module_symbol_len = 0; |
| 103 |
char *module_path = NULL; |
| 104 |
|
| 105 |
/* Work out the path of the module to open */ |
| 106 |
module_file_len = strlen(type) + 1 + strlen(name) + strlen(LT_MODULE_EXT) + 1; |
| 107 |
module_file = malloc(module_file_len); |
| 108 |
if(!module_file) |
| 109 |
{ |
| 110 |
if(verbose > -1) |
| 111 |
error1( "Failed to allocate memory for module name: %s", INT123_strerror(errno) ); |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
snprintf(module_file, module_file_len, "%s_%s%s", type, name, LT_MODULE_EXT); |
| 115 |
module_path = INT123_compat_catpath(dir, module_file); |
| 116 |
free(module_file); |
| 117 |
if(!module_path) |
| 118 |
{ |
| 119 |
if(verbose > -1) |
| 120 |
error("Failed to construct full path (out of memory?)."); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
if(verbose > 1) |
| 124 |
fprintf(stderr, "Module path: %s\n", module_path ); |
| 125 |
|
| 126 |
/* Open the module */ |
| 127 |
handle = INT123_compat_dlopen(module_path); |
| 128 |
free(module_path); |
| 129 |
if (handle==NULL) |
| 130 |
{ |
| 131 |
if(verbose > -1) |
| 132 |
error1("Failed to open module %s.", name); |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
/* Work out the symbol name */ |
| 137 |
module_symbol_len = strlen( MODULE_SYMBOL_PREFIX ) + |
| 138 |
strlen( type ) + |
| 139 |
strlen( MODULE_SYMBOL_SUFFIX ) + 1; |
| 140 |
module_symbol = malloc(module_symbol_len); |
| 141 |
if (module_symbol == NULL) { |
| 142 |
if(verbose > -1) |
| 143 |
error1( "Failed to allocate memory for module symbol: %s", INT123_strerror(errno) ); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
snprintf( module_symbol, module_symbol_len, "%s%s%s", MODULE_SYMBOL_PREFIX, type, MODULE_SYMBOL_SUFFIX ); |
| 147 |
debug1( "Module symbol: %s", module_symbol ); |
| 148 |
|
| 149 |
/* Get the information structure from the module */ |
| 150 |
module = (mpg123_module_t*)INT123_compat_dlsym(handle, module_symbol); |
| 151 |
free( module_symbol ); |
| 152 |
if (module==NULL) { |
| 153 |
if(verbose > -1) |
| 154 |
error("Failed to get module symbol."); |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
/* Check the API version */ |
| 159 |
if (MPG123_MODULE_API_VERSION != module->api_version) |
| 160 |
{ |
| 161 |
if(verbose > -1) |
| 162 |
error2( "API version of module does not match (got %i, expected %i).", module->api_version, MPG123_MODULE_API_VERSION); |
| 163 |
INT123_compat_dlclose(handle); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
/* Store handle in the data structure */ |
| 168 |
module->handle = handle; |
| 169 |
return module; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/* Open a module, including directory search. */ |
| 174 |
mpg123_module_t* INT123_open_module( const char* type, const char* name, int verbose |
| 175 |
, const char* bindir ) |
| 176 |
{ |
| 177 |
mpg123_module_t *module = NULL; |
| 178 |
char *moddir = NULL; |
| 179 |
|
| 180 |
moddir = get_module_dir(verbose, bindir); |
| 181 |
if(!moddir) |
| 182 |
{ |
| 183 |
if(verbose > -1) |
| 184 |
error("Failure getting module directory! (Perhaps set MPG123_MODDIR?)"); |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
module = open_module_here(moddir, type, name, verbose); |
| 189 |
|
| 190 |
free(moddir); |
| 191 |
return module; |
| 192 |
} |
| 193 |
|
| 194 |
void INT123_close_module( mpg123_module_t* module, int verbose ) |
| 195 |
{ |
| 196 |
INT123_compat_dlclose(module->handle); |
| 197 |
} |
| 198 |
|
| 199 |
int INT123_list_modules( const char *type, char ***names, char ***descr, int verbose |
| 200 |
, const char* bindir ) |
| 201 |
{ |
| 202 |
char *moddir = NULL; |
| 203 |
int count = 0; |
| 204 |
struct compat_dir *dir; |
| 205 |
char *filename; |
| 206 |
|
| 207 |
debug1("verbose:%i", verbose); |
| 208 |
|
| 209 |
*names = NULL; |
| 210 |
*descr = NULL; |
| 211 |
|
| 212 |
moddir = get_module_dir(verbose, bindir); |
| 213 |
if(moddir == NULL) |
| 214 |
{ |
| 215 |
if(verbose > -1) |
| 216 |
error("Failure getting module directory! (Perhaps set MPG123_MODDIR?)"); |
| 217 |
return -1; |
| 218 |
} |
| 219 |
debug1("module dir: %s", moddir); |
| 220 |
|
| 221 |
/* Open the module directory */ |
| 222 |
dir = INT123_compat_diropen(moddir); |
| 223 |
if (dir==NULL) { |
| 224 |
if(verbose > -1) |
| 225 |
error2("Failed to open the module directory (%s): %s\n" |
| 226 |
, moddir, INT123_strerror(errno)); |
| 227 |
free(moddir); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
while((filename=INT123_compat_nextfile(dir))) |
| 232 |
{ |
| 233 |
/* Pointers to the pieces. */ |
| 234 |
char *module_name = NULL; |
| 235 |
char *module_type = NULL; |
| 236 |
char *uscore_pos = NULL; |
| 237 |
mpg123_module_t *module = NULL; |
| 238 |
char* ext; |
| 239 |
size_t name_len; |
| 240 |
|
| 241 |
/* Various checks as loop shortcuts, avoiding too much nesting. */ |
| 242 |
debug1("checking entry: %s", filename); |
| 243 |
|
| 244 |
name_len = strlen(filename); |
| 245 |
if(name_len < strlen(LT_MODULE_EXT)) |
| 246 |
goto list_modules_continue; |
| 247 |
ext = filename |
| 248 |
+ name_len |
| 249 |
- strlen(LT_MODULE_EXT); |
| 250 |
if(strcmp(ext, LT_MODULE_EXT)) |
| 251 |
goto list_modules_continue; |
| 252 |
debug("has suffix"); |
| 253 |
|
| 254 |
/* Extract the module type and name */ |
| 255 |
uscore_pos = strchr( filename, '_' ); |
| 256 |
if( uscore_pos==NULL |
| 257 |
|| (uscore_pos>=filename+name_len+1) ) |
| 258 |
{ |
| 259 |
debug("no underscore"); |
| 260 |
goto list_modules_continue; |
| 261 |
} |
| 262 |
*uscore_pos = '\0'; |
| 263 |
module_type = filename; |
| 264 |
module_name = uscore_pos+1; |
| 265 |
/* Only list modules of desired type. */ |
| 266 |
if(strcmp(type, module_type)) |
| 267 |
{ |
| 268 |
debug("wrong type"); |
| 269 |
goto list_modules_continue; |
| 270 |
} |
| 271 |
debug("has type"); |
| 272 |
|
| 273 |
/* Extract the short name of the module */ |
| 274 |
name_len -= uscore_pos - filename + 1; |
| 275 |
if(name_len <= strlen(LT_MODULE_EXT)) |
| 276 |
{ |
| 277 |
debug("name too short"); |
| 278 |
goto list_modules_continue; |
| 279 |
} |
| 280 |
name_len -= strlen(LT_MODULE_EXT); |
| 281 |
module_name[name_len] = '\0'; |
| 282 |
|
| 283 |
debug("opening module"); |
| 284 |
/* Open the module |
| 285 |
Yes, this re-builds the file name we chopped to pieces just now. */ |
| 286 |
if((module=open_module_here(moddir, module_type, module_name, verbose))) |
| 287 |
{ |
| 288 |
if( INT123_stringlists_add( names, descr |
| 289 |
, module->name, module->description, &count) ) |
| 290 |
if(verbose > -1) |
| 291 |
error("OOM"); |
| 292 |
/* Close the module again */ |
| 293 |
INT123_close_module(module, verbose); |
| 294 |
} |
| 295 |
list_modules_continue: |
| 296 |
free(filename); |
| 297 |
} |
| 298 |
INT123_compat_dirclose(dir); |
| 299 |
return count; |
| 300 |
} |
| 301 |
|
| 302 |
|