| 1 | |
| 2 | /* module.c : Implementation of the module loading/unloading mechanism. |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | #include <lttv/module.h> |
| 7 | |
| 8 | |
| 9 | struct _LttvModule |
| 10 | { |
| 11 | GModule *module; |
| 12 | guint ref_count; |
| 13 | guint load_count; |
| 14 | GPtrArray *dependents; |
| 15 | }; |
| 16 | |
| 17 | |
| 18 | /* Table of loaded modules and paths where to search for modules */ |
| 19 | |
| 20 | static GHashTable *modules = NULL; |
| 21 | |
| 22 | static GPtrArray *modulesPaths = NULL; |
| 23 | |
| 24 | static void lttv_module_unload_all(); |
| 25 | |
| 26 | |
| 27 | void lttv_module_init(int argc, char **argv) |
| 28 | { |
| 29 | modules = g_hash_table_new(g_str_hash, g_str_equal); |
| 30 | modulesPaths = g_ptr_array_new(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | void lttv_module_destroy() |
| 35 | { |
| 36 | int i; |
| 37 | |
| 38 | /* Unload all modules */ |
| 39 | lttv_module_unload_all(); |
| 40 | |
| 41 | /* Free the modules paths pointer array as well as the elements */ |
| 42 | for(i = 0; i < modulesPaths->len ; i++) { |
| 43 | g_free(modulesPaths->pdata[i]); |
| 44 | } |
| 45 | g_ptr_array_free(modulesPaths,TRUE) ; |
| 46 | modulesPaths = NULL; |
| 47 | |
| 48 | /* destroy the hash table */ |
| 49 | g_hash_table_destroy(modules) ; |
| 50 | modules = NULL; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /* Add a new pathname to the modules loading search path */ |
| 55 | |
| 56 | void lttv_module_path_add(const char *name) |
| 57 | { |
| 58 | g_ptr_array_add(modulesPaths,(char*)g_strdup(name)); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | static LttvModule * |
| 63 | module_load(const char *name, int argc, char **argv) |
| 64 | { |
| 65 | GModule *gm; |
| 66 | |
| 67 | LttvModule *m; |
| 68 | |
| 69 | int i; |
| 70 | |
| 71 | char *pathname; |
| 72 | |
| 73 | const char *module_name; |
| 74 | |
| 75 | LttvModuleInit init_function; |
| 76 | |
| 77 | /* Try to find the module along all the user specified paths */ |
| 78 | |
| 79 | for(i = 0 ; i < modulesPaths->len ; i++) { |
| 80 | pathname = g_module_build_path(modulesPaths->pdata[i],name); |
| 81 | gm = g_module_open(pathname,0); |
| 82 | g_free(pathname); |
| 83 | |
| 84 | if(gm != NULL) break; |
| 85 | } |
| 86 | |
| 87 | /* Try the default system path */ |
| 88 | |
| 89 | if(gm == NULL) { |
| 90 | pathname = g_module_build_path(NULL,name); |
| 91 | gm = g_module_open(pathname,0); |
| 92 | g_free(pathname); |
| 93 | } |
| 94 | |
| 95 | /* Module cannot be found */ |
| 96 | |
| 97 | if(gm == NULL) return NULL; |
| 98 | |
| 99 | /* Check if the module was already opened using the hopefully canonical name |
| 100 | returned by g_module_name. */ |
| 101 | |
| 102 | module_name = g_module_name(gm); |
| 103 | |
| 104 | m = g_hash_table_lookup(modules, module_name); |
| 105 | |
| 106 | if(m == NULL) { |
| 107 | |
| 108 | /* Module loaded for the first time. Insert it in the table and call the |
| 109 | init function if any. */ |
| 110 | |
| 111 | m = g_new(LttvModule, 1); |
| 112 | m->module = gm; |
| 113 | m->ref_count = 0; |
| 114 | m->load_count = 0; |
| 115 | m->dependents = g_ptr_array_new(); |
| 116 | g_hash_table_insert(modules, (gpointer)module_name, m); |
| 117 | |
| 118 | if(!g_module_symbol(gm, "init", (gpointer)&init_function)) { |
| 119 | g_warning("module %s (%s) has no init function", name, pathname); |
| 120 | } |
| 121 | else init_function(m, argc, argv); |
| 122 | } |
| 123 | else { |
| 124 | |
| 125 | /* Module was already opened, check that it really is the same and |
| 126 | undo the extra g_module_open */ |
| 127 | |
| 128 | if(m->module != gm) g_error("Two gmodules with the same pathname"); |
| 129 | g_module_close(gm); |
| 130 | } |
| 131 | |
| 132 | m->ref_count++; |
| 133 | return m; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | LttvModule * |
| 138 | lttv_module_load(const char *name, int argc, char **argv) |
| 139 | { |
| 140 | LttvModule *m = module_load(name, argc, argv); |
| 141 | |
| 142 | if(m != NULL) m->load_count++; |
| 143 | return m; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | LttvModule * |
| 148 | lttv_module_require(LttvModule *m, const char *name, int argc, char **argv) |
| 149 | { |
| 150 | LttvModule *module; |
| 151 | |
| 152 | module = module_load(name, argc, argv); |
| 153 | if(module != NULL) g_ptr_array_add(m->dependents, module); |
| 154 | return module; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static void module_unload(LttvModule *m) |
| 159 | { |
| 160 | LttvModuleDestroy destroy_function; |
| 161 | |
| 162 | char *pathname; |
| 163 | |
| 164 | guint i, len; |
| 165 | |
| 166 | /* Decrement the reference count */ |
| 167 | |
| 168 | m->ref_count--; |
| 169 | if(m->ref_count > 0) return; |
| 170 | |
| 171 | /* We really have to unload the module, first unload its dependents */ |
| 172 | |
| 173 | len = m->dependents->len; |
| 174 | |
| 175 | for(i = 0 ; i < len ; i++) { |
| 176 | module_unload(m->dependents->pdata[i]); |
| 177 | } |
| 178 | |
| 179 | if(len != m->dependents->len) g_error("dependents list modified"); |
| 180 | |
| 181 | /* Unload the module itself */ |
| 182 | |
| 183 | if(!g_module_symbol(m->module, "destroy", (gpointer)&destroy_function)) { |
| 184 | g_warning("module (%s) has no destroy function", pathname); |
| 185 | } |
| 186 | else destroy_function(); |
| 187 | |
| 188 | g_hash_table_remove(modules, g_module_name(m->module)); |
| 189 | g_ptr_array_free(m->dependents, TRUE); |
| 190 | g_module_close(m->module); |
| 191 | g_free(m); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void lttv_module_unload(LttvModule *m) |
| 196 | { |
| 197 | if(m->load_count <= 0) { |
| 198 | g_error("more unload than load (%s)", g_module_name(m->module)); |
| 199 | return; |
| 200 | } |
| 201 | m->load_count--; |
| 202 | module_unload(m); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | static void |
| 207 | list_modules(gpointer key, gpointer value, gpointer user_data) |
| 208 | { |
| 209 | g_ptr_array_add((GPtrArray *)user_data, value); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | LttvModule ** |
| 214 | lttv_module_list(guint *nb) |
| 215 | { |
| 216 | GPtrArray *list = g_ptr_array_new(); |
| 217 | |
| 218 | LttvModule **array; |
| 219 | |
| 220 | g_hash_table_foreach(modules, list_modules, list); |
| 221 | *nb = list->len; |
| 222 | array = (LttvModule **)list->pdata; |
| 223 | g_ptr_array_free(list, FALSE); |
| 224 | return array; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | LttvModule ** |
| 229 | lttv_module_info(LttvModule *m, const char **name, |
| 230 | guint *ref_count, guint *load_count, guint *nb_dependents) |
| 231 | { |
| 232 | guint i, len = m->dependents->len; |
| 233 | |
| 234 | LttvModule **array = g_new(LttvModule *, len); |
| 235 | |
| 236 | *name = g_module_name(m->module); |
| 237 | *ref_count = m->ref_count; |
| 238 | *load_count = m->load_count; |
| 239 | *nb_dependents = len; |
| 240 | for(i = 0 ; i < len ; i++) array[i] = m->dependents->pdata[i]; |
| 241 | return array; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | static void |
| 246 | list_independent(gpointer key, gpointer value, gpointer user_data) |
| 247 | { |
| 248 | LttvModule *m = (LttvModule *)value; |
| 249 | |
| 250 | if(m->load_count > 0) g_ptr_array_add((GPtrArray *)user_data, m); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void |
| 255 | lttv_module_unload_all() |
| 256 | { |
| 257 | guint i; |
| 258 | |
| 259 | LttvModule *m; |
| 260 | |
| 261 | GPtrArray *independent_modules = g_ptr_array_new(); |
| 262 | |
| 263 | g_hash_table_foreach(modules, list_independent, independent_modules); |
| 264 | |
| 265 | for(i = 0 ; i < independent_modules->len ; i++) { |
| 266 | m = (LttvModule *)independent_modules->pdata[i]; |
| 267 | while(m->load_count > 0) lttv_module_unload(m); |
| 268 | } |
| 269 | |
| 270 | g_ptr_array_free(independent_modules, TRUE); |
| 271 | if(g_hash_table_size(modules) != 0) g_warning("cannot unload all modules"); |
| 272 | } |