Mercurial > audlegacy
annotate src/audacious/pluginenum.c @ 2624:840fb578a834 trunk
[svn] - [security, backport to 1.3] fix improper comparisons of hashtables used by the plugin loader.
| author | nenolod |
|---|---|
| date | Fri, 16 Mar 2007 00:25:44 -0700 |
| parents | 0ad10a95ed10 |
| children | c3cd6e47faf6 |
| rev | line source |
|---|---|
| 2313 | 1 /* Audacious - Cross-platform multimedia player |
| 2 * Copyright (C) 2005-2007 Audacious development team | |
| 3 * | |
| 4 * Based on BMP: | |
| 5 * Copyright (C) 2003-2004 BMP development team | |
| 6 * | |
| 7 * Based on XMMS: | |
| 8 * Copyright (C) 1998-2003 XMMS development team | |
| 9 * | |
| 10 * This program is free software; you can redistribute it and/or modify | |
| 11 * it under the terms of the GNU General Public License as published by | |
| 12 * the Free Software Foundation; under version 2 of the License. | |
| 13 * | |
| 14 * This program 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 program; if not, write to the Free Software | |
| 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 22 */ | |
| 23 | |
| 24 #ifdef HAVE_CONFIG_H | |
| 25 # include "config.h" | |
| 26 #endif | |
| 27 | |
| 28 #ifndef SHARED_SUFFIX | |
| 29 # define SHARED_SUFFIX G_MODULE_SUFFIX | |
| 30 #endif | |
| 31 | |
| 32 #include "pluginenum.h" | |
| 33 | |
| 34 #include <glib.h> | |
| 35 #include <gmodule.h> | |
| 36 #include <glib/gprintf.h> | |
| 37 #include <string.h> | |
| 38 | |
| 39 #include "controlsocket.h" | |
| 40 #include "main.h" | |
| 41 #include "ui_main.h" | |
| 42 #include "playback.h" | |
| 43 #include "playlist.h" | |
|
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2313
diff
changeset
|
44 #include "strings.h" |
| 2313 | 45 #include "util.h" |
| 46 | |
| 47 #include "effect.h" | |
| 48 #include "general.h" | |
| 49 #include "input.h" | |
| 50 #include "output.h" | |
| 51 #include "visualization.h" | |
| 52 | |
| 53 const gchar *plugin_dir_list[] = { | |
| 54 PLUGINSUBS, | |
| 55 NULL | |
| 56 }; | |
| 57 | |
| 58 GHashTable *plugin_matrix = NULL; | |
| 59 GList *lowlevel_list = NULL; | |
| 60 | |
| 2623 | 61 extern GList *vfs_transports; |
| 62 | |
| 2313 | 63 static gint |
| 64 inputlist_compare_func(gconstpointer a, gconstpointer b) | |
| 65 { | |
| 66 const InputPlugin *ap = a, *bp = b; | |
| 67 return strcasecmp(ap->description, bp->description); | |
| 68 } | |
| 69 | |
| 70 static gint | |
| 71 outputlist_compare_func(gconstpointer a, gconstpointer b) | |
| 72 { | |
| 73 const OutputPlugin *ap = a, *bp = b; | |
| 74 return strcasecmp(ap->description, bp->description); | |
| 75 } | |
| 76 | |
| 77 static gint | |
| 78 effectlist_compare_func(gconstpointer a, gconstpointer b) | |
| 79 { | |
| 80 const EffectPlugin *ap = a, *bp = b; | |
| 81 return strcasecmp(ap->description, bp->description); | |
| 82 } | |
| 83 | |
| 84 static gint | |
| 85 generallist_compare_func(gconstpointer a, gconstpointer b) | |
| 86 { | |
| 87 const GeneralPlugin *ap = a, *bp = b; | |
| 88 return strcasecmp(ap->description, bp->description); | |
| 89 } | |
| 90 | |
| 91 static gint | |
| 92 vislist_compare_func(gconstpointer a, gconstpointer b) | |
| 93 { | |
| 94 const VisPlugin *ap = a, *bp = b; | |
| 95 return strcasecmp(ap->description, bp->description); | |
| 96 } | |
| 97 | |
| 98 static gboolean | |
| 99 plugin_is_duplicate(const gchar * filename) | |
| 100 { | |
| 101 GList *l; | |
| 102 const gchar *basename = g_basename(filename); | |
| 103 | |
| 104 /* FIXME: messy stuff */ | |
| 105 | |
| 106 for (l = ip_data.input_list; l; l = g_list_next(l)) | |
| 107 if (!strcmp(basename, g_basename(INPUT_PLUGIN(l->data)->filename))) | |
| 108 return TRUE; | |
| 109 | |
| 110 for (l = op_data.output_list; l; l = g_list_next(l)) | |
| 111 if (!strcmp(basename, g_basename(OUTPUT_PLUGIN(l->data)->filename))) | |
| 112 return TRUE; | |
| 113 | |
| 114 for (l = ep_data.effect_list; l; l = g_list_next(l)) | |
| 115 if (!strcmp(basename, g_basename(EFFECT_PLUGIN(l->data)->filename))) | |
| 116 return TRUE; | |
| 117 | |
| 118 for (l = gp_data.general_list; l; l = g_list_next(l)) | |
| 119 if (!strcmp(basename, g_basename(GENERAL_PLUGIN(l->data)->filename))) | |
| 120 return TRUE; | |
| 121 | |
| 122 for (l = vp_data.vis_list; l; l = g_list_next(l)) | |
| 123 if (!strcmp(basename, g_basename(VIS_PLUGIN(l->data)->filename))) | |
| 124 return TRUE; | |
| 125 | |
| 126 for (l = lowlevel_list; l; l = g_list_next(l)) | |
| 127 if (!strcmp(basename, g_basename(VIS_PLUGIN(l->data)->filename))) | |
| 128 return TRUE; | |
| 129 | |
| 130 return FALSE; | |
| 131 } | |
| 132 | |
| 133 | |
| 134 #define PLUGIN_GET_INFO(x) ((PluginGetInfoFunc)(x))() | |
| 135 typedef Plugin * (*PluginGetInfoFunc) (void); | |
| 136 | |
| 137 static void | |
| 138 input_plugin_init(Plugin * plugin) | |
| 139 { | |
| 140 InputPlugin *p = INPUT_PLUGIN(plugin); | |
| 141 | |
| 142 p->get_vis_type = input_get_vis_type; | |
| 143 p->add_vis_pcm = input_add_vis_pcm; | |
| 144 | |
| 145 /* Pretty const casts courtesy of XMMS's plugin.h legacy. Anyone | |
| 146 else thinks we could use a CONST macro to solve the warnings? | |
| 147 - descender */ | |
| 148 p->set_info = (void (*)(gchar *, gint, gint, gint, gint)) playlist_set_info_old_abi; | |
| 149 p->set_info_text = (void (*)(gchar *)) input_set_info_text; | |
| 150 p->set_status_buffering = (void (*)(gboolean)) input_set_status_buffering; | |
| 151 | |
| 152 ip_data.input_list = g_list_append(ip_data.input_list, p); | |
| 153 | |
| 154 g_hash_table_replace(plugin_matrix, g_path_get_basename(p->filename), | |
| 155 GINT_TO_POINTER(1)); | |
| 156 } | |
| 157 | |
| 158 static void | |
| 159 output_plugin_init(Plugin * plugin) | |
| 160 { | |
| 161 OutputPlugin *p = OUTPUT_PLUGIN(plugin); | |
| 162 op_data.output_list = g_list_append(op_data.output_list, p); | |
| 163 } | |
| 164 | |
| 165 static void | |
| 166 effect_plugin_init(Plugin * plugin) | |
| 167 { | |
| 168 EffectPlugin *p = EFFECT_PLUGIN(plugin); | |
| 169 ep_data.effect_list = g_list_append(ep_data.effect_list, p); | |
| 170 } | |
| 171 | |
| 172 static void | |
| 173 general_plugin_init(Plugin * plugin) | |
| 174 { | |
| 175 GeneralPlugin *p = GENERAL_PLUGIN(plugin); | |
| 176 p->xmms_session = ctrlsocket_get_session_id(); | |
| 177 gp_data.general_list = g_list_append(gp_data.general_list, p); | |
| 178 } | |
| 179 | |
| 180 static void | |
| 181 vis_plugin_init(Plugin * plugin) | |
| 182 { | |
| 183 VisPlugin *p = VIS_PLUGIN(plugin); | |
| 184 p->xmms_session = ctrlsocket_get_session_id(); | |
| 185 p->disable_plugin = vis_disable_plugin; | |
| 186 vp_data.vis_list = g_list_append(vp_data.vis_list, p); | |
| 187 } | |
| 188 | |
| 189 static void | |
| 190 lowlevel_plugin_init(Plugin * plugin) | |
| 191 { | |
| 192 LowlevelPlugin *p = LOWLEVEL_PLUGIN(plugin); | |
| 193 lowlevel_list = g_list_append(lowlevel_list, p); | |
| 194 } | |
| 195 | |
| 196 /* FIXME: Placed here (hopefully) temporarily - descender */ | |
| 197 | |
| 198 typedef struct { | |
| 199 const gchar *name; | |
| 200 const gchar *id; | |
| 201 void (*init)(Plugin *); | |
| 202 } PluginType; | |
| 203 | |
| 204 static PluginType plugin_types[] = { | |
| 205 { "input" , "get_iplugin_info", input_plugin_init }, | |
| 206 { "output" , "get_oplugin_info", output_plugin_init }, | |
| 207 { "effect" , "get_eplugin_info", effect_plugin_init }, | |
| 208 { "general" , "get_gplugin_info", general_plugin_init }, | |
| 209 { "visualization", "get_vplugin_info", vis_plugin_init }, | |
| 210 { "lowlevel" , "get_lplugin_info", lowlevel_plugin_init }, | |
| 211 { NULL, NULL, NULL } | |
| 212 }; | |
| 213 | |
| 214 static void | |
| 215 add_plugin(const gchar * filename) | |
| 216 { | |
| 217 PluginType *type; | |
| 218 GModule *module; | |
| 219 gpointer func; | |
| 220 | |
| 221 if (plugin_is_duplicate(filename)) | |
| 222 return; | |
| 223 | |
| 2623 | 224 g_message("Loaded plugin (%s)", filename); |
| 225 | |
| 2313 | 226 if (!(module = g_module_open(filename, G_MODULE_BIND_LOCAL))) { |
| 227 printf("Failed to load plugin (%s): %s\n", | |
| 228 filename, g_module_error()); | |
| 229 return; | |
| 230 } | |
| 231 | |
| 232 for (type = plugin_types; type->name; type++) | |
| 233 { | |
| 234 if (g_module_symbol(module, type->id, &func)) { | |
| 235 Plugin *plugin = PLUGIN_GET_INFO(func); | |
| 236 | |
| 237 plugin->handle = module; | |
| 238 plugin->filename = g_strdup(filename); | |
| 239 type->init(PLUGIN_GET_INFO(func)); | |
| 240 | |
| 241 return; | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 printf("Invalid plugin (%s)\n", filename); | |
| 246 g_module_close(module); | |
| 247 } | |
| 248 | |
| 249 static gboolean | |
| 250 scan_plugin_func(const gchar * path, const gchar * basename, gpointer data) | |
| 251 { | |
| 252 if (!str_has_suffix_nocase(basename, SHARED_SUFFIX)) | |
| 253 return FALSE; | |
| 254 | |
| 255 if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) | |
| 256 return FALSE; | |
| 257 | |
| 258 add_plugin(path); | |
| 259 | |
| 260 return FALSE; | |
| 261 } | |
| 262 | |
| 263 static void | |
| 264 scan_plugins(const gchar * path) | |
| 265 { | |
| 266 dir_foreach(path, scan_plugin_func, NULL, NULL); | |
| 267 } | |
| 268 | |
| 269 void | |
| 270 plugin_system_init(void) | |
| 271 { | |
| 272 gchar *dir, **disabled; | |
| 273 GList *node; | |
| 274 OutputPlugin *op; | |
| 275 InputPlugin *ip; | |
| 276 LowlevelPlugin *lp; | |
| 277 gint dirsel = 0, i = 0; | |
| 278 | |
| 279 if (!g_module_supported()) { | |
| 280 report_error("Module loading not supported! Plugins will not be loaded.\n"); | |
| 281 return; | |
| 282 } | |
| 283 | |
|
2624
840fb578a834
[svn] - [security, backport to 1.3] fix improper comparisons of hashtables used by the plugin loader.
nenolod
parents:
2623
diff
changeset
|
284 plugin_matrix = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
| 2313 | 285 NULL); |
| 286 | |
| 287 #ifndef DISABLE_USER_PLUGIN_DIR | |
| 288 scan_plugins(bmp_paths[BMP_PATH_USER_PLUGIN_DIR]); | |
| 289 /* | |
| 290 * This is in a separate loop so if the user puts them in the | |
| 291 * wrong dir we'll still get them in the right order (home dir | |
| 292 * first) - Zinx | |
| 293 */ | |
| 294 while (plugin_dir_list[dirsel]) { | |
| 295 dir = g_build_filename(bmp_paths[BMP_PATH_USER_PLUGIN_DIR], | |
| 296 plugin_dir_list[dirsel++], NULL); | |
| 297 scan_plugins(dir); | |
| 298 g_free(dir); | |
| 299 } | |
| 300 dirsel = 0; | |
| 301 #endif | |
| 302 | |
| 303 while (plugin_dir_list[dirsel]) { | |
| 304 dir = g_build_filename(PLUGIN_DIR, plugin_dir_list[dirsel++], NULL); | |
| 305 scan_plugins(dir); | |
| 306 g_free(dir); | |
| 307 } | |
| 308 | |
| 309 op_data.output_list = g_list_sort(op_data.output_list, outputlist_compare_func); | |
| 310 if (!op_data.current_output_plugin | |
| 311 && g_list_length(op_data.output_list)) { | |
| 312 op_data.current_output_plugin = op_data.output_list->data; | |
| 313 } | |
| 314 | |
| 315 ip_data.input_list = g_list_sort(ip_data.input_list, inputlist_compare_func); | |
| 316 | |
| 317 ep_data.effect_list = g_list_sort(ep_data.effect_list, effectlist_compare_func); | |
| 318 ep_data.enabled_list = NULL; | |
| 319 | |
| 320 gp_data.general_list = g_list_sort(gp_data.general_list, generallist_compare_func); | |
| 321 gp_data.enabled_list = NULL; | |
| 322 | |
| 323 vp_data.vis_list = g_list_sort(vp_data.vis_list, vislist_compare_func); | |
| 324 vp_data.enabled_list = NULL; | |
| 325 | |
| 326 general_enable_from_stringified_list(cfg.enabled_gplugins); | |
| 327 vis_enable_from_stringified_list(cfg.enabled_vplugins); | |
| 328 effect_enable_from_stringified_list(cfg.enabled_eplugins); | |
| 329 | |
| 330 g_free(cfg.enabled_gplugins); | |
| 331 cfg.enabled_gplugins = NULL; | |
| 332 | |
| 333 g_free(cfg.enabled_vplugins); | |
| 334 cfg.enabled_vplugins = NULL; | |
| 335 | |
| 336 g_free(cfg.enabled_eplugins); | |
| 337 cfg.enabled_eplugins = NULL; | |
| 338 | |
| 339 for (node = op_data.output_list; node; node = g_list_next(node)) { | |
| 340 op = OUTPUT_PLUGIN(node->data); | |
| 341 /* | |
| 342 * Only test basename to avoid problems when changing | |
| 343 * prefix. We will only see one plugin with the same | |
| 344 * basename, so this is usually what the user want. | |
| 345 */ | |
| 346 if (!strcmp(g_basename(cfg.outputplugin), g_basename(op->filename))) | |
| 347 op_data.current_output_plugin = op; | |
| 348 if (op->init) | |
| 349 op->init(); | |
| 350 } | |
| 351 | |
| 352 for (node = ip_data.input_list; node; node = g_list_next(node)) { | |
| 353 ip = INPUT_PLUGIN(node->data); | |
| 354 if (ip->init) | |
| 355 ip->init(); | |
| 356 } | |
| 357 | |
| 358 for (node = lowlevel_list; node; node = g_list_next(node)) { | |
| 359 lp = LOWLEVEL_PLUGIN(node->data); | |
| 360 if (lp->init) | |
| 361 lp->init(); | |
| 362 } | |
| 363 | |
| 364 if (cfg.disabled_iplugins) { | |
| 365 disabled = g_strsplit(cfg.disabled_iplugins, ":", 0); | |
| 366 while (disabled[i]) { | |
| 367 g_hash_table_replace(plugin_matrix, disabled[i], | |
| 368 GINT_TO_POINTER(FALSE)); | |
| 369 i++; | |
| 370 } | |
| 371 | |
| 372 g_free(disabled); | |
| 373 | |
| 374 g_free(cfg.disabled_iplugins); | |
| 375 cfg.disabled_iplugins = NULL; | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 void | |
| 380 plugin_system_cleanup(void) | |
| 381 { | |
| 382 InputPlugin *ip; | |
| 383 OutputPlugin *op; | |
| 384 EffectPlugin *ep; | |
| 385 GeneralPlugin *gp; | |
| 386 VisPlugin *vp; | |
| 387 LowlevelPlugin *lp; | |
| 388 GList *node; | |
| 389 | |
| 390 g_message("Shutting down plugin system"); | |
| 391 | |
| 392 if (playback_get_playing()) { | |
| 393 ip_data.stop = TRUE; | |
| 394 playback_stop(); | |
| 395 ip_data.stop = FALSE; | |
| 396 } | |
| 397 | |
| 2623 | 398 /* FIXME: race condition -nenolod */ |
| 399 op_data.current_output_plugin = NULL; | |
| 400 | |
| 2313 | 401 for (node = get_input_list(); node; node = g_list_next(node)) { |
| 402 ip = INPUT_PLUGIN(node->data); | |
| 403 if (ip && ip->cleanup) { | |
| 404 ip->cleanup(); | |
| 405 GDK_THREADS_LEAVE(); | |
| 406 while (g_main_context_iteration(NULL, FALSE)); | |
| 407 GDK_THREADS_ENTER(); | |
| 408 } | |
| 409 g_module_close(ip->handle); | |
| 410 } | |
| 411 | |
| 2623 | 412 if (ip_data.input_list != NULL) |
| 413 { | |
| 2313 | 414 g_list_free(ip_data.input_list); |
| 2623 | 415 ip_data.input_list = NULL; |
| 416 } | |
| 2313 | 417 |
| 418 for (node = get_output_list(); node; node = g_list_next(node)) { | |
| 419 op = OUTPUT_PLUGIN(node->data); | |
| 420 if (op && op->cleanup) { | |
| 421 op->cleanup(); | |
| 422 GDK_THREADS_LEAVE(); | |
| 423 while (g_main_context_iteration(NULL, FALSE)); | |
| 424 GDK_THREADS_ENTER(); | |
| 425 } | |
| 426 g_module_close(op->handle); | |
| 427 } | |
| 428 | |
| 2623 | 429 if (op_data.output_list != NULL) |
| 430 { | |
| 2313 | 431 g_list_free(op_data.output_list); |
| 2623 | 432 op_data.output_list = NULL; |
| 433 } | |
| 2313 | 434 |
| 435 for (node = get_effect_list(); node; node = g_list_next(node)) { | |
| 436 ep = EFFECT_PLUGIN(node->data); | |
| 437 if (ep && ep->cleanup) { | |
| 438 ep->cleanup(); | |
| 439 GDK_THREADS_LEAVE(); | |
| 440 while (g_main_context_iteration(NULL, FALSE)); | |
| 441 GDK_THREADS_ENTER(); | |
| 442 } | |
| 443 g_module_close(ep->handle); | |
| 444 } | |
| 445 | |
| 2623 | 446 if (ep_data.effect_list != NULL) |
| 447 { | |
| 2313 | 448 g_list_free(ep_data.effect_list); |
| 2623 | 449 ep_data.effect_list = NULL; |
| 2313 | 450 } |
| 451 | |
| 452 for (node = get_general_list(); node; node = g_list_next(node)) { | |
| 453 gp = GENERAL_PLUGIN(node->data); | |
| 454 if (gp && gp->cleanup) { | |
| 455 gp->cleanup(); | |
| 456 GDK_THREADS_LEAVE(); | |
| 457 while (g_main_context_iteration(NULL, FALSE)); | |
| 458 GDK_THREADS_ENTER(); | |
| 459 } | |
| 460 g_module_close(gp->handle); | |
| 461 } | |
| 462 | |
| 2623 | 463 if (gp_data.general_list != NULL) |
| 464 { | |
| 2313 | 465 g_list_free(gp_data.general_list); |
| 2623 | 466 gp_data.general_list = NULL; |
| 2313 | 467 } |
| 468 | |
| 469 for (node = get_vis_list(); node; node = g_list_next(node)) { | |
| 470 vp = VIS_PLUGIN(node->data); | |
| 471 if (vp && vp->cleanup) { | |
| 472 vp->cleanup(); | |
| 473 GDK_THREADS_LEAVE(); | |
| 474 while (g_main_context_iteration(NULL, FALSE)); | |
| 475 GDK_THREADS_ENTER(); | |
| 476 } | |
| 477 g_module_close(vp->handle); | |
| 478 } | |
| 479 | |
| 2623 | 480 if (vp_data.vis_list != NULL) |
| 481 { | |
| 2313 | 482 g_list_free(vp_data.vis_list); |
| 2623 | 483 vp_data.vis_list = NULL; |
| 484 } | |
| 2313 | 485 |
| 486 for (node = lowlevel_list; node; node = g_list_next(node)) { | |
| 487 lp = LOWLEVEL_PLUGIN(node->data); | |
| 488 if (lp && lp->cleanup) { | |
| 489 lp->cleanup(); | |
| 490 GDK_THREADS_LEAVE(); | |
| 491 while (g_main_context_iteration(NULL, FALSE)); | |
| 492 GDK_THREADS_ENTER(); | |
| 493 } | |
| 494 g_module_close(lp->handle); | |
| 495 } | |
| 496 | |
| 2623 | 497 if (lowlevel_list != NULL) |
| 498 { | |
| 2313 | 499 g_list_free(lowlevel_list); |
| 2623 | 500 lowlevel_list = NULL; |
| 501 } | |
| 502 | |
| 503 /* XXX: vfs will crash otherwise. -nenolod */ | |
| 504 if (vfs_transports != NULL) | |
| 505 { | |
| 506 g_list_free(vfs_transports); | |
| 507 vfs_transports = NULL; | |
| 508 } | |
| 2313 | 509 } |
