47 static bool modules_have_module (
const char *module);
48 bool modules_in_use (
void);
49 static bool modules_probe (
void);
51 void modules_quit (
void);
65 static int modules_probed = -1;
69 static struct kmod_ctx *modules_ctx = 0;
75 static bool modules_have_module(
const char *module)
82 struct kmod_list *list = 0;
84 if( kmod_module_new_from_lookup(modules_ctx, module, &list) < 0 )
94 kmod_module_unref_list(list);
96 log_debug(
"module %s does%s exist", module, ack ?
"" :
"not ");
101 bool modules_in_use(
void)
103 LOG_REGISTER_CONTEXT;
105 if( modules_probed < 0 )
106 log_debug(
"modules_in_use() called before modules_probe()");
108 return modules_probed > 0;
111 static bool modules_probe(
void)
113 LOG_REGISTER_CONTEXT;
115 static const char *
const lut[] = {
123 if( modules_probed == -1 ) {
124 modules_probed =
false;
128 for(
size_t i = 0; lut[i] ; ++i ) {
129 if( modules_have_module(lut[i]) ) {
130 modules_probed =
true;
134 log_warning(
"MODULES %sdetected", modules_probed ?
"" :
"not ");
137 return modules_in_use();
146 LOG_REGISTER_CONTEXT;
151 if( !(modules_ctx = kmod_new(NULL, NULL)) )
155 if( kmod_load_resources(modules_ctx) < 0 )
158 if( !modules_probe() )
167 void modules_quit(
void)
169 LOG_REGISTER_CONTEXT;
172 kmod_unref(modules_ctx), modules_ctx = 0;
183 LOG_REGISTER_CONTEXT;
187 const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
188 struct kmod_module *mod;
189 char *charging_args = NULL;
192 if(!strcmp(module, MODULE_NONE))
195 if( !modules_in_use() ) {
196 log_warning(
"load module %s - without module support", module);
201 load = strdup(module);
202 if(!strcmp(module, MODULE_CHARGING) || !strcmp(module, MODULE_CHARGE_FALLBACK))
211 strings = g_strsplit(MODULE_CHARGE_FALLBACK,
" ", 2);
213 charging_args = strdup(strings[1]);
216 load = strdup(strings[0]);
220 ret = kmod_module_new_from_name(modules_ctx, load, &mod);
223 if(!strcmp(module, MODULE_MASS_STORAGE) &&
224 (kmod_module_get_path(mod) == NULL))
226 log_debug(
"Fallback on older g_file_storage\n");
227 ret = kmod_module_new_from_name(modules_ctx, MODULE_FILE_STORAGE, &mod);
231 ret = kmod_module_probe_insert_module(mod, probe_flags, NULL, NULL, NULL, NULL);
234 ret = kmod_module_probe_insert_module(mod, probe_flags, charging_args, NULL, NULL, NULL);
237 kmod_module_unref(mod);
241 log_info(
"Module %s loaded successfully\n", module);
243 log_info(
"Module %s failed to load\n", module);
255 LOG_REGISTER_CONTEXT;
259 struct kmod_module *mod;
261 if(!strcmp(module, MODULE_NONE))
264 if( !modules_in_use() ) {
265 log_warning(
"unload module %s - without module support", module);
269 kmod_module_new_from_name(modules_ctx, module, &mod);
270 ret = kmod_module_remove_module(mod, KMOD_REMOVE_NOWAIT);
271 kmod_module_unref(mod);