Lines Matching refs:ctx

90 void kmod_log(const struct kmod_ctx *ctx,
96 if (ctx->log_fn == NULL)
100 ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args);
152 * @ctx: kmod library context
157 KMOD_EXPORT const char *kmod_get_dirname(const struct kmod_ctx *ctx)
159 return ctx->dirname;
164 * @ctx: kmod library context
171 KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx)
173 if (ctx == NULL)
175 return (void *)ctx->userdata;
180 * @ctx: kmod library context
185 KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata)
187 if (ctx == NULL)
189 ctx->userdata = userdata;
253 struct kmod_ctx *ctx;
256 ctx = calloc(1, sizeof(struct kmod_ctx));
257 if (!ctx)
260 ctx->refcount = 1;
261 ctx->log_fn = log_filep;
262 ctx->log_data = stderr;
263 ctx->log_priority = LOG_ERR;
265 ctx->dirname = get_kernel_release(dirname);
270 kmod_set_log_priority(ctx, log_priority(env));
274 err = kmod_config_new(ctx, &ctx->config, config_paths);
276 ERR(ctx, "could not create config\n");
280 ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL);
281 if (ctx->modules_by_name == NULL) {
282 ERR(ctx, "could not create by-name hash\n");
286 INFO(ctx, "ctx %p created\n", ctx);
287 DBG(ctx, "log_priority=%d\n", ctx->log_priority);
289 return ctx;
292 free(ctx->modules_by_name);
293 free(ctx->dirname);
294 free(ctx);
300 * @ctx: kmod library context
306 KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
308 if (ctx == NULL)
310 ctx->refcount++;
311 return ctx;
316 * @ctx: kmod library context
323 KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
325 if (ctx == NULL)
328 if (--ctx->refcount > 0)
329 return ctx;
331 INFO(ctx, "context %p released\n", ctx);
333 kmod_unload_resources(ctx);
334 hash_free(ctx->modules_by_name);
335 free(ctx->dirname);
336 if (ctx->config)
337 kmod_config_free(ctx->config);
339 free(ctx);
345 * @ctx: kmod library context
353 KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
360 if (ctx == NULL)
362 ctx->log_fn = log_fn;
363 ctx->log_data = (void *)data;
364 INFO(ctx, "custom logging function %p registered\n", log_fn);
369 * @ctx: kmod library context
373 KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
375 if (ctx == NULL)
377 return ctx->log_priority;
382 * @ctx: kmod library context
388 KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
390 if (ctx == NULL)
392 ctx->log_priority = priority;
395 struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
400 mod = hash_find(ctx->modules_by_name, key);
402 DBG(ctx, "get module name='%s' found=%p\n", key, mod);
407 void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
410 DBG(ctx, "add %p key='%s'\n", mod, key);
412 hash_add(ctx->modules_by_name, key, mod);
415 void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
418 DBG(ctx, "del %p key='%s'\n", mod, key);
420 hash_del(ctx->modules_by_name, key);
423 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
432 if (ctx->indexes[index_number] != NULL) {
433 DBG(ctx, "use mmaped index '%s' for name=%s\n",
435 realnames = index_mm_searchwild(ctx->indexes[index_number],
440 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
443 DBG(ctx, "file=%s name=%s\n", fn, name);
456 err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
458 ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n",
477 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name,
483 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL,
487 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
490 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS,
494 static char *lookup_builtin_file(struct kmod_ctx *ctx, const char *name)
498 if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) {
499 DBG(ctx, "use mmaped index '%s' modname=%s\n",
502 line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN],
508 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
510 DBG(ctx, "file=%s modname=%s\n", fn, name);
514 DBG(ctx, "could not open builtin file '%s'\n", fn);
525 int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name,
533 line = lookup_builtin_file(ctx, name);
537 err = kmod_module_new_from_name(ctx, name, &mod);
539 ERR(ctx, "Could not create module from name %s: %s\n",
557 bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
561 line = lookup_builtin_file(ctx, name);
566 char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
572 if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) {
573 DBG(ctx, "use mmaped index '%s' modname=%s\n",
575 return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP],
579 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
582 DBG(ctx, "file=%s modname=%s\n", fn, name);
586 DBG(ctx, "could not open moddep file '%s'\n", fn);
596 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
609 line = kmod_search_moddep(ctx, name);
613 n = kmod_module_new_from_name(ctx, name, &mod);
615 ERR(ctx, "Could not create module from name %s: %s\n",
630 int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
633 struct kmod_config *config = ctx->config;
644 err = kmod_module_new_from_alias(ctx, aliasname,
647 ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n",
664 int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
667 struct kmod_config *config = ctx->config;
678 err = kmod_module_new_from_name(ctx, modname, &mod);
680 ERR(ctx, "Could not create module from name %s: %s\n",
687 ERR(ctx, "out of memory\n");
714 err = kmod_module_new_from_name(ctx, modname, &mod);
716 ERR(ctx, "Could not create module from name %s: %s\n",
723 ERR(ctx, "out of memory\n");
743 void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited)
748 hash_iter_init(ctx->modules_by_name, &iter);
753 void kmod_set_modules_required(struct kmod_ctx *ctx, bool required)
758 hash_iter_init(ctx->modules_by_name, &iter);
778 * @ctx: kmod library context
786 * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
788 KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
793 if (ctx == NULL || ctx->config == NULL)
796 kmod_list_foreach(l, ctx->config->paths) {
806 if (ctx->indexes[i] == NULL)
809 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
812 if (is_cache_invalid(path, ctx->indexes_stamp[i]))
821 * @ctx: kmod library context
823 * Load indexes and keep them open in @ctx. This way it's faster to lookup
834 KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
838 if (ctx == NULL)
844 if (ctx->indexes[i] != NULL) {
845 INFO(ctx, "Index %s already loaded\n",
850 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
852 ctx->indexes[i] = index_mm_open(ctx, path,
853 &ctx->indexes_stamp[i]);
854 if (ctx->indexes[i] == NULL)
861 kmod_unload_resources(ctx);
867 * @ctx: kmod library context
873 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
880 KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
884 if (ctx == NULL)
888 if (ctx->indexes[i] != NULL) {
889 index_mm_close(ctx->indexes[i]);
890 ctx->indexes[i] = NULL;
891 ctx->indexes_stamp[i] = 0;
898 * @ctx: kmod library context
912 KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
915 if (ctx == NULL)
921 if (ctx->indexes[type] != NULL) {
922 DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
923 index_mm_dump(ctx->indexes[type], fd,
929 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
932 DBG(ctx, "file=%s\n", fn);
945 const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx)
947 return ctx->config;