]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod.c
c9966e0ce435462ea04a1f0dc9d598745ebfdb08
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation version 2.1.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/utsname.h>
32 #include "libkmod-private.h"
33 #include "libkmod-index.h"
37 * @short_description: libkmod context
39 * The context contains the default values for the library user,
40 * and is passed to all library operations.
46 * Opaque object representing the library context.
50 void (*log_fn
)(struct kmod_ctx
*ctx
,
51 int priority
, const char *file
, int line
,
52 const char *fn
, const char *format
, va_list args
);
56 struct kmod_config config
;
59 void kmod_log(struct kmod_ctx
*ctx
,
60 int priority
, const char *file
, int line
, const char *fn
,
61 const char *format
, ...)
65 va_start(args
, format
);
66 ctx
->log_fn(ctx
, priority
, file
, line
, fn
, format
, args
);
70 static void log_stderr(struct kmod_ctx
*ctx
,
71 int priority
, const char *file
, int line
,
72 const char *fn
, const char *format
, va_list args
)
74 fprintf(stderr
, "libkmod: %s: ", fn
);
75 vfprintf(stderr
, format
, args
);
78 const char *kmod_get_dirname(const struct kmod_ctx
*ctx
)
85 * @ctx: kmod library context
87 * Retrieve stored data pointer from library context. This might be useful
88 * to access from callbacks like a custom logging function.
90 * Returns: stored userdata
92 KMOD_EXPORT
void *kmod_get_userdata(const struct kmod_ctx
*ctx
)
96 return (void *)ctx
->userdata
;
101 * @ctx: kmod library context
102 * @userdata: data pointer
104 * Store custom @userdata in the library context.
106 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, const void *userdata
)
110 ctx
->userdata
= userdata
;
113 static int log_priority(const char *priority
)
118 prio
= strtol(priority
, &endptr
, 10);
119 if (endptr
[0] == '\0' || isspace(endptr
[0]))
121 if (strncmp(priority
, "err", 3) == 0)
123 if (strncmp(priority
, "info", 4) == 0)
125 if (strncmp(priority
, "debug", 5) == 0)
130 static const char *dirname_default_prefix
= "/lib/modules";
132 static char *get_kernel_release(const char *dirname
)
138 return strdup(dirname
);
143 if (asprintf(&p
, "%s/%s", dirname_default_prefix
, u
.release
) < 0)
152 * Create kmod library context. This reads the kmod configuration
153 * and fills in the default values.
155 * The initial refcount is 1, and needs to be decremented to
156 * release the resources of the kmod library context.
158 * Returns: a new kmod library context
160 KMOD_EXPORT
struct kmod_ctx
*kmod_new(const char *dirname
)
163 struct kmod_ctx
*ctx
;
165 ctx
= calloc(1, sizeof(struct kmod_ctx
));
170 ctx
->log_fn
= log_stderr
;
171 ctx
->log_priority
= LOG_ERR
;
173 ctx
->dirname
= get_kernel_release(dirname
);
175 /* environment overwrites config */
176 env
= getenv("KMOD_LOG");
178 kmod_set_log_priority(ctx
, log_priority(env
));
180 kmod_parse_config(ctx
, &ctx
->config
);
182 INFO(ctx
, "ctx %p created\n", ctx
);
183 DBG(ctx
, "log_priority=%d\n", ctx
->log_priority
);
190 * @ctx: kmod library context
192 * Take a reference of the kmod library context.
194 * Returns: the passed kmod library context
196 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
206 * @ctx: kmod library context
208 * Drop a reference of the kmod library context. If the refcount
209 * reaches zero, the resources of the context will be released.
212 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
217 if (--ctx
->refcount
> 0)
219 INFO(ctx
, "context %p released\n", ctx
);
221 kmod_free_config(ctx
, &ctx
->config
);
228 * @ctx: kmod library context
229 * @log_fn: function to be called for logging messages
231 * The built-in logging writes to stderr. It can be
232 * overridden by a custom function, to plug log messages
233 * into the user's logging functionality.
236 KMOD_EXPORT
void kmod_set_log_fn(struct kmod_ctx
*ctx
,
237 void (*log_fn
)(struct kmod_ctx
*ctx
,
238 int priority
, const char *file
,
239 int line
, const char *fn
,
240 const char *format
, va_list args
))
242 ctx
->log_fn
= log_fn
;
243 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
247 * kmod_get_log_priority:
248 * @ctx: kmod library context
250 * Returns: the current logging priority
252 KMOD_EXPORT
int kmod_get_log_priority(const struct kmod_ctx
*ctx
)
254 return ctx
->log_priority
;
258 * kmod_set_log_priority:
259 * @ctx: kmod library context
260 * @priority: the new logging priority
262 * Set the current logging priority. The value controls which messages
265 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
267 ctx
->log_priority
= priority
;
271 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx
*ctx
,
274 struct kmod_list
**list
)
278 struct index_file
*idx
;
279 struct index_value
*realnames
, *realname
;
281 if (asprintf(&fn
, "%s/%s.bin", ctx
->dirname
, file
) < 0)
284 DBG(ctx
, "file=%s name=%s\n", fn
, name
);
286 idx
= index_file_open(fn
);
292 realnames
= index_searchwild(idx
, name
);
293 for (realname
= realnames
; realname
; realname
= realnames
->next
) {
294 struct kmod_module
*mod
;
296 err
= kmod_module_new_from_name(ctx
, realname
->value
, &mod
);
298 ERR(ctx
, "%s\n", strerror(-err
));
302 *list
= kmod_list_append(*list
, mod
);
306 index_values_free(realnames
);
307 index_file_close(idx
);
313 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
318 static const char *symbols_file
= "modules.symbols";
320 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx
*ctx
, const char *name
,
321 struct kmod_list
**list
)
323 if (!startswith(name
, "symbol:"))
326 return kmod_lookup_alias_from_alias_bin(ctx
, symbols_file
, name
, list
);
330 static const char *aliases_file
= "modules.alias";
332 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx
*ctx
, const char *name
,
333 struct kmod_list
**list
)
335 return kmod_lookup_alias_from_alias_bin(ctx
, aliases_file
, name
, list
);
338 static const char *moddep_file
= "modules.dep";
340 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx
*ctx
, const char *name
,
341 struct kmod_list
**list
)
344 struct index_file
*idx
;
348 * Module names do not contain ':'. Return early if we know it will
351 if (strchr(name
, ':'))
354 if (asprintf(&fn
, "%s/%s.bin", ctx
->dirname
, moddep_file
) < 0)
357 DBG(ctx
, "file=%s modname=%s\n", fn
, name
);
359 idx
= index_file_open(fn
);
365 line
= index_search(idx
, name
);
367 struct kmod_module
*mod
;
369 n
= kmod_module_new_from_name(ctx
, name
, &mod
);
371 ERR(ctx
, "%s\n", strerror(-n
));
375 *list
= kmod_list_append(*list
, mod
);
376 kmod_module_parse_dep(mod
, line
);
381 index_file_close(idx
);
387 int kmod_lookup_alias_from_config(struct kmod_ctx
*ctx
, const char *name
,
388 struct kmod_list
**list
)
390 struct kmod_config
*config
= &ctx
->config
;
394 kmod_list_foreach(l
, config
->aliases
) {
395 const char *aliasname
= kmod_alias_get_name(l
);
396 const char *modname
= kmod_alias_get_modname(l
);
398 if (fnmatch(aliasname
, name
, 0) == 0) {
399 struct kmod_module
*mod
;
401 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
403 ERR(ctx
, "%s", strerror(-err
));
407 *list
= kmod_list_append(*list
, mod
);
415 *list
= kmod_list_remove_n_latest(*list
, nmatch
);