filename | src/plugin.c |
changeset | 1296:30ecee61f811 |
prev | 1075:1a21750d300c |
author | nkeynes |
date | Wed May 27 08:46:29 2015 +1000 (8 years ago) |
permissions | -rw-r--r-- |
last change | Add support for extracting the ELF symbol table and printing symbol names alongside the SH4 disassembly |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Plugin loader code
5 *
6 * Copyright (c) 2009 Nathan Keynes.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
19 #include <sys/stat.h>
20 #include <dirent.h>
21 #include <dlfcn.h>
22 #include <string.h>
23 #include <glib.h>
24 #include "plugin.h"
25 #include "lxpaths.h"
27 #ifdef APPLE_BUILD
28 #define SOEXT ".dylib"
29 #else
30 #define SOEXT ".so"
31 #endif
33 /** Dummy plugin used as a plugin directory marker */
34 #define DUMMY_PLUGIN ("lxdream_dummy" SOEXT)
36 const char *plugin_type_string[] = { "undefined", "audio driver", "input driver" };
38 int main(int argc, char *argv[]);
39 static const char *exec_path = NULL;
41 /**
42 * Return the full path to the main binary
43 */
44 static const char *get_exec_path()
45 {
46 if( exec_path == NULL ) {
47 Dl_info dli;
49 /* Use dladdr for this, since it should be available for any platform
50 * that we can support plugins on at all.
51 */
52 if( dladdr( main, &dli) ) {
53 gchar *path = g_strdup( dli.dli_fname );
54 char *i = strrchr( path, '/' );
55 if( i > path ) {
56 *i = '\0';
57 exec_path = path;
58 } else {
59 g_free(path);
60 }
61 }
62 }
63 return exec_path;
64 }
66 static gboolean plugin_load( const gchar *plugin_path )
67 {
68 void *so = dlopen(plugin_path, RTLD_NOW|RTLD_LOCAL);
69 if( so == NULL ) {
70 WARN("Failed to load plugin '%s': %s", plugin_path, dlerror());
71 return FALSE;
72 }
74 struct plugin_struct *plugin = (struct plugin_struct *)dlsym(so,"lxdream_plugin_entry");
75 if( plugin == NULL ) {
76 WARN("Failed to load plugin: '%s': Not an lxdream plugin", plugin_path);
77 dlclose(so);
78 return FALSE;
79 }
81 if( strcmp(lxdream_short_version, plugin->version) != 0 ) {
82 WARN("Failed to load plugin: '%s': Incompatible version (%s)", plugin_path, plugin->version);
83 dlclose(so);
84 return FALSE;
85 }
87 if( plugin->type == PLUGIN_NONE ) {
88 /* 'dummy' plugin - we don't actually want to load it */
89 dlclose(so);
90 return FALSE;
91 }
93 if( plugin->type < PLUGIN_MIN_TYPE || plugin->type > PLUGIN_MAX_TYPE ) {
94 WARN("Failed to load plugin: '%s': Unrecognized plugin type (%d)", plugin_path, plugin->type );
95 dlclose(so);
96 return FALSE;
97 }
99 if( plugin->register_plugin() == FALSE ) {
100 WARN("Failed to load plugin: '%s': Initialization failed", plugin_path);
101 dlclose(so);
102 return FALSE;
103 }
104 DEBUG("Loaded %s '%s'", plugin_type_string[plugin->type], plugin->name);
105 return TRUE;
106 }
108 static gboolean has_plugins( const gchar *path )
109 {
110 struct stat st;
112 gchar *dummy_name = g_strdup_printf( "%s/%s", path, DUMMY_PLUGIN );
113 if( stat( dummy_name, &st ) == 0 ) {
114 return TRUE;
115 } else {
116 return FALSE;
117 }
118 }
120 /**
121 * Scan the plugin dir and load all valid plugins.
122 */
123 static int plugin_load_all( const gchar *plugin_dir )
124 {
125 int plugin_count = 0;
126 struct dirent *ent;
128 DIR *dir = opendir(plugin_dir);
129 if( dir == NULL ) {
130 WARN( "Unable to open plugin directory '%s'", plugin_dir );
131 return 0;
132 }
134 while( (ent = readdir(dir)) != NULL ) {
135 const char *ext = strrchr(ent->d_name, '.');
136 if( ext != NULL && strcasecmp(SOEXT,ext) == 0 ) {
137 char *libname = g_strdup_printf( "%s/%s", plugin_dir,ent->d_name );
138 if( plugin_load( libname ) ) {
139 plugin_count++;
140 }
141 g_free(libname);
142 }
143 }
144 return plugin_count;
145 }
147 int plugin_init()
148 {
149 const char *path = get_exec_path();
150 if( path == NULL || !has_plugins(path) ) {
151 path = get_plugin_path();
152 }
154 DEBUG( "Plugin directory: %s", path );
155 return plugin_load_all( path );
156 }
.