2011-11-21 21:35:15 +07:00
|
|
|
/*
|
2011-11-21 23:35:35 +07:00
|
|
|
* libkmod - interface to kernel module operations
|
|
|
|
*
|
2012-01-09 09:40:50 +07:00
|
|
|
* Copyright (C) 2011-2012 ProFUSION embedded systems
|
2011-11-21 23:35:35 +07:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2011-12-13 03:24:35 +07:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2011-11-21 23:35:35 +07:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2012-02-28 04:48:02 +07:00
|
|
|
#include <assert.h>
|
2011-11-21 21:35:15 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
2011-12-06 04:58:39 +07:00
|
|
|
#include <limits.h>
|
2011-11-21 21:35:15 +07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-12-01 04:03:41 +07:00
|
|
|
#include <fnmatch.h>
|
2011-11-21 21:35:15 +07:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2011-11-25 01:41:01 +07:00
|
|
|
#include <sys/utsname.h>
|
2012-01-01 04:28:31 +07:00
|
|
|
#include <sys/stat.h>
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
#include "libkmod.h"
|
|
|
|
#include "libkmod-private.h"
|
2011-12-01 05:31:45 +07:00
|
|
|
#include "libkmod-index.h"
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-12-06 12:38:37 +07:00
|
|
|
#define KMOD_HASH_SIZE (256)
|
|
|
|
#define KMOD_LRU_MAX (128)
|
2012-02-17 05:43:16 +07:00
|
|
|
#define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1
|
2011-12-06 12:38:37 +07:00
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* SECTION:libkmod
|
|
|
|
* @short_description: libkmod context
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
|
|
|
* The context contains the default values for the library user,
|
|
|
|
* and is passed to all library operations.
|
|
|
|
*/
|
|
|
|
|
2012-01-16 19:43:34 +07:00
|
|
|
static struct _index_files {
|
|
|
|
const char *fn;
|
|
|
|
const char *prefix;
|
|
|
|
} index_files[] = {
|
2012-01-16 21:01:48 +07:00
|
|
|
[KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" },
|
|
|
|
[KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " },
|
|
|
|
[KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "},
|
2012-02-17 05:43:16 +07:00
|
|
|
[KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""},
|
2011-12-08 23:56:48 +07:00
|
|
|
};
|
|
|
|
|
2011-12-12 05:37:01 +07:00
|
|
|
static const char *default_config_paths[] = {
|
|
|
|
"/run/modprobe.d",
|
2011-12-20 23:58:05 +07:00
|
|
|
SYSCONFDIR "/modprobe.d",
|
|
|
|
ROOTPREFIX "/lib/modprobe.d",
|
2011-12-12 05:37:01 +07:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_ctx:
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
|
|
|
* Opaque object representing the library context.
|
|
|
|
*/
|
2011-11-21 23:35:35 +07:00
|
|
|
struct kmod_ctx {
|
2011-11-21 21:35:15 +07:00
|
|
|
int refcount;
|
2011-12-03 06:10:24 +07:00
|
|
|
int log_priority;
|
2011-12-08 22:47:55 +07:00
|
|
|
void (*log_fn)(void *data,
|
2011-11-21 23:59:23 +07:00
|
|
|
int priority, const char *file, int line,
|
|
|
|
const char *fn, const char *format, va_list args);
|
2011-12-08 22:47:55 +07:00
|
|
|
void *log_data;
|
2011-12-03 05:24:07 +07:00
|
|
|
const void *userdata;
|
|
|
|
char *dirname;
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_config *config;
|
2011-12-27 21:13:54 +07:00
|
|
|
struct hash *modules_by_name;
|
2012-01-16 21:01:48 +07:00
|
|
|
struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE];
|
|
|
|
unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE];
|
2011-11-21 21:35:15 +07:00
|
|
|
};
|
|
|
|
|
2011-12-08 22:47:55 +07:00
|
|
|
void kmod_log(const struct kmod_ctx *ctx,
|
2011-11-21 23:59:23 +07:00
|
|
|
int priority, const char *file, int line, const char *fn,
|
|
|
|
const char *format, ...)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
2011-12-08 22:58:46 +07:00
|
|
|
if (ctx->log_fn == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
va_start(args, format);
|
2011-12-08 22:47:55 +07:00
|
|
|
ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args);
|
2011-11-21 21:35:15 +07:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:47:55 +07:00
|
|
|
static void log_filep(void *data,
|
2011-11-21 23:59:23 +07:00
|
|
|
int priority, const char *file, int line,
|
|
|
|
const char *fn, const char *format, va_list args)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
2011-12-08 22:47:55 +07:00
|
|
|
FILE *fp = data;
|
2012-01-08 04:25:03 +07:00
|
|
|
#ifdef ENABLE_DEBUG
|
|
|
|
char buf[16];
|
|
|
|
const char *priname;
|
|
|
|
switch (priority) {
|
|
|
|
case LOG_EMERG:
|
|
|
|
priname = "EMERGENCY";
|
|
|
|
break;
|
|
|
|
case LOG_ALERT:
|
|
|
|
priname = "ALERT";
|
|
|
|
break;
|
|
|
|
case LOG_CRIT:
|
|
|
|
priname = "CRITICAL";
|
|
|
|
break;
|
|
|
|
case LOG_ERR:
|
|
|
|
priname = "ERROR";
|
|
|
|
break;
|
|
|
|
case LOG_WARNING:
|
|
|
|
priname = "WARNING";
|
|
|
|
break;
|
|
|
|
case LOG_NOTICE:
|
|
|
|
priname = "NOTICE";
|
|
|
|
break;
|
|
|
|
case LOG_INFO:
|
|
|
|
priname = "INFO";
|
|
|
|
break;
|
|
|
|
case LOG_DEBUG:
|
|
|
|
priname = "DEBUG";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snprintf(buf, sizeof(buf), "L:%d", priority);
|
|
|
|
priname = buf;
|
|
|
|
}
|
|
|
|
fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn);
|
|
|
|
#else
|
2011-12-08 22:47:55 +07:00
|
|
|
fprintf(fp, "libkmod: %s: ", fn);
|
2012-01-08 04:25:03 +07:00
|
|
|
#endif
|
2011-12-08 22:47:55 +07:00
|
|
|
vfprintf(fp, format, args);
|
2011-11-21 21:35:15 +07:00
|
|
|
}
|
|
|
|
|
2011-12-03 05:24:07 +07:00
|
|
|
const char *kmod_get_dirname(const struct kmod_ctx *ctx)
|
2011-11-25 01:41:01 +07:00
|
|
|
{
|
|
|
|
return ctx->dirname;
|
|
|
|
}
|
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_get_userdata:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
|
|
|
* Retrieve stored data pointer from library context. This might be useful
|
2011-12-14 12:44:38 +07:00
|
|
|
* to access from callbacks.
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
|
|
|
* Returns: stored userdata
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-11-23 20:52:30 +07:00
|
|
|
KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
2011-12-03 05:24:07 +07:00
|
|
|
return (void *)ctx->userdata;
|
2011-11-21 21:35:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_set_userdata:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
* @userdata: data pointer
|
|
|
|
*
|
|
|
|
* Store custom @userdata in the library context.
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-12-03 05:24:07 +07:00
|
|
|
KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
|
|
|
ctx->userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int log_priority(const char *priority)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
int prio;
|
|
|
|
|
|
|
|
prio = strtol(priority, &endptr, 10);
|
|
|
|
if (endptr[0] == '\0' || isspace(endptr[0]))
|
|
|
|
return prio;
|
|
|
|
if (strncmp(priority, "err", 3) == 0)
|
|
|
|
return LOG_ERR;
|
|
|
|
if (strncmp(priority, "info", 4) == 0)
|
|
|
|
return LOG_INFO;
|
|
|
|
if (strncmp(priority, "debug", 5) == 0)
|
|
|
|
return LOG_DEBUG;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-20 23:58:05 +07:00
|
|
|
static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules";
|
2011-12-01 05:27:50 +07:00
|
|
|
|
2011-12-03 05:24:07 +07:00
|
|
|
static char *get_kernel_release(const char *dirname)
|
2011-11-25 01:41:01 +07:00
|
|
|
{
|
|
|
|
struct utsname u;
|
2011-12-01 05:27:50 +07:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (dirname != NULL)
|
2012-01-14 11:31:51 +07:00
|
|
|
return path_make_absolute_cwd(dirname);
|
2011-11-25 01:41:01 +07:00
|
|
|
|
|
|
|
if (uname(&u) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2011-12-01 05:27:50 +07:00
|
|
|
if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return p;
|
2011-11-25 01:41:01 +07:00
|
|
|
}
|
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_new:
|
2011-12-12 05:37:01 +07:00
|
|
|
* @dirname: what to consider as linux module's directory, if NULL
|
2012-01-14 21:09:34 +07:00
|
|
|
* defaults to $rootprefix/lib/modules/`uname -r`. If it's relative,
|
|
|
|
* it's treated as relative to current the current working
|
|
|
|
* directory. Otherwise, give an absolute dirname.
|
2011-12-12 05:37:01 +07:00
|
|
|
* @config_paths: ordered array of paths (directories or files) where
|
2011-12-14 12:44:38 +07:00
|
|
|
* to load from user-defined configuration parameters such as
|
|
|
|
* alias, blacklists, commands (install, remove). If
|
|
|
|
* NULL defaults to /run/modprobe.d, /etc/modprobe.d and
|
2012-01-14 21:09:34 +07:00
|
|
|
* $rootprefix/lib/modprobe.d. Give an empty vector if
|
|
|
|
* configuration should not be read. This array must be null
|
|
|
|
* terminated.
|
2011-12-12 05:37:01 +07:00
|
|
|
*
|
2012-01-09 12:09:49 +07:00
|
|
|
* Create kmod library context. This reads the kmod configuration
|
|
|
|
* and fills in the default values.
|
|
|
|
*
|
|
|
|
* The initial refcount is 1, and needs to be decremented to
|
|
|
|
* release the resources of the kmod library context.
|
|
|
|
*
|
2011-11-21 23:35:35 +07:00
|
|
|
* Returns: a new kmod library context
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-12-12 19:48:02 +07:00
|
|
|
KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
|
|
|
|
const char * const *config_paths)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
const char *env;
|
2011-11-22 00:07:27 +07:00
|
|
|
struct kmod_ctx *ctx;
|
2011-12-03 06:40:22 +07:00
|
|
|
int err;
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-11-22 00:07:27 +07:00
|
|
|
ctx = calloc(1, sizeof(struct kmod_ctx));
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-11-22 00:07:27 +07:00
|
|
|
ctx->refcount = 1;
|
2011-12-08 22:47:55 +07:00
|
|
|
ctx->log_fn = log_filep;
|
|
|
|
ctx->log_data = stderr;
|
2011-11-22 00:07:27 +07:00
|
|
|
ctx->log_priority = LOG_ERR;
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-12-01 05:27:50 +07:00
|
|
|
ctx->dirname = get_kernel_release(dirname);
|
2011-11-25 01:41:01 +07:00
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
/* environment overwrites config */
|
2011-11-21 23:35:35 +07:00
|
|
|
env = getenv("KMOD_LOG");
|
2011-11-21 21:35:15 +07:00
|
|
|
if (env != NULL)
|
2011-11-22 00:07:27 +07:00
|
|
|
kmod_set_log_priority(ctx, log_priority(env));
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-12-12 05:37:01 +07:00
|
|
|
if (config_paths == NULL)
|
|
|
|
config_paths = default_config_paths;
|
|
|
|
err = kmod_config_new(ctx, &ctx->config, config_paths);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (err < 0) {
|
2011-12-06 12:38:37 +07:00
|
|
|
ERR(ctx, "could not create config\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-12-27 21:13:54 +07:00
|
|
|
ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL);
|
2011-12-06 12:38:37 +07:00
|
|
|
if (ctx->modules_by_name == NULL) {
|
|
|
|
ERR(ctx, "could not create by-name hash\n");
|
|
|
|
goto fail;
|
2011-12-03 06:40:22 +07:00
|
|
|
}
|
2011-11-30 03:07:43 +07:00
|
|
|
|
2011-11-25 10:05:30 +07:00
|
|
|
INFO(ctx, "ctx %p created\n", ctx);
|
|
|
|
DBG(ctx, "log_priority=%d\n", ctx->log_priority);
|
2011-11-22 00:07:27 +07:00
|
|
|
|
|
|
|
return ctx;
|
2011-12-06 12:38:37 +07:00
|
|
|
|
|
|
|
fail:
|
|
|
|
free(ctx->modules_by_name);
|
|
|
|
free(ctx->dirname);
|
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
2011-11-21 21:35:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_ref:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
2011-11-21 23:35:35 +07:00
|
|
|
* Take a reference of the kmod library context.
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
2011-11-21 23:35:35 +07:00
|
|
|
* Returns: the passed kmod library context
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
ctx->refcount++;
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_unref:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
2011-11-21 23:35:35 +07:00
|
|
|
* Drop a reference of the kmod library context. If the refcount
|
2011-11-21 21:35:15 +07:00
|
|
|
* reaches zero, the resources of the context will be released.
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
2011-11-25 00:41:48 +07:00
|
|
|
|
|
|
|
if (--ctx->refcount > 0)
|
2011-11-21 21:35:15 +07:00
|
|
|
return ctx;
|
2011-12-08 23:59:51 +07:00
|
|
|
|
2011-11-25 10:05:30 +07:00
|
|
|
INFO(ctx, "context %p released\n", ctx);
|
2011-12-08 23:59:51 +07:00
|
|
|
|
|
|
|
kmod_unload_resources(ctx);
|
2011-12-27 21:13:54 +07:00
|
|
|
hash_free(ctx->modules_by_name);
|
2011-12-03 05:24:07 +07:00
|
|
|
free(ctx->dirname);
|
2011-12-03 06:40:22 +07:00
|
|
|
if (ctx->config)
|
|
|
|
kmod_config_free(ctx->config);
|
2011-12-08 23:59:51 +07:00
|
|
|
|
2011-11-21 21:35:15 +07:00
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_set_log_fn:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
* @log_fn: function to be called for logging messages
|
2012-01-05 06:07:59 +07:00
|
|
|
* @data: data to pass to log function
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
|
|
|
* The built-in logging writes to stderr. It can be
|
|
|
|
* overridden by a custom function, to plug log messages
|
|
|
|
* into the user's logging functionality.
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
|
2011-12-08 22:47:55 +07:00
|
|
|
void (*log_fn)(void *data,
|
2011-11-21 23:59:23 +07:00
|
|
|
int priority, const char *file,
|
|
|
|
int line, const char *fn,
|
2011-12-08 22:47:55 +07:00
|
|
|
const char *format, va_list args),
|
|
|
|
const void *data)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
2011-12-08 22:58:46 +07:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2011-11-21 21:35:15 +07:00
|
|
|
ctx->log_fn = log_fn;
|
2011-12-08 22:47:55 +07:00
|
|
|
ctx->log_data = (void *)data;
|
2011-11-25 10:05:30 +07:00
|
|
|
INFO(ctx, "custom logging function %p registered\n", log_fn);
|
2011-11-21 21:35:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_get_log_priority:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
|
|
|
* Returns: the current logging priority
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-11-23 20:52:30 +07:00
|
|
|
KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
2011-12-08 22:58:46 +07:00
|
|
|
if (ctx == NULL)
|
|
|
|
return -1;
|
2011-11-21 21:35:15 +07:00
|
|
|
return ctx->log_priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-11-21 23:35:35 +07:00
|
|
|
* kmod_set_log_priority:
|
|
|
|
* @ctx: kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
* @priority: the new logging priority
|
|
|
|
*
|
|
|
|
* Set the current logging priority. The value controls which messages
|
|
|
|
* are logged.
|
2011-12-10 01:42:14 +07:00
|
|
|
*/
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
2011-12-08 22:58:46 +07:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2011-11-21 21:35:15 +07:00
|
|
|
ctx->log_priority = priority;
|
|
|
|
}
|
2011-12-01 04:03:41 +07:00
|
|
|
|
2011-12-06 12:38:37 +07:00
|
|
|
struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
|
2011-12-15 22:11:51 +07:00
|
|
|
const char *key)
|
2011-12-06 12:38:37 +07:00
|
|
|
{
|
|
|
|
struct kmod_module *mod;
|
|
|
|
|
2011-12-27 21:13:54 +07:00
|
|
|
mod = hash_find(ctx->modules_by_name, key);
|
2011-12-06 12:38:37 +07:00
|
|
|
|
2011-12-15 22:11:51 +07:00
|
|
|
DBG(ctx, "get module name='%s' found=%p\n", key, mod);
|
2011-12-06 12:38:37 +07:00
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2011-12-15 22:11:51 +07:00
|
|
|
void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
|
|
|
|
const char *key)
|
2011-12-06 12:38:37 +07:00
|
|
|
{
|
2011-12-15 22:11:51 +07:00
|
|
|
DBG(ctx, "add %p key='%s'\n", mod, key);
|
2011-12-06 12:38:37 +07:00
|
|
|
|
2011-12-27 21:13:54 +07:00
|
|
|
hash_add(ctx->modules_by_name, key, mod);
|
2011-12-06 12:38:37 +07:00
|
|
|
}
|
|
|
|
|
2011-12-15 22:11:51 +07:00
|
|
|
void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
|
|
|
|
const char *key)
|
2011-12-06 12:38:37 +07:00
|
|
|
{
|
2011-12-15 22:11:51 +07:00
|
|
|
DBG(ctx, "del %p key='%s'\n", mod, key);
|
2011-12-06 12:38:37 +07:00
|
|
|
|
2011-12-27 21:13:54 +07:00
|
|
|
hash_del(ctx->modules_by_name, key);
|
2011-12-06 12:38:37 +07:00
|
|
|
}
|
2011-12-01 05:31:45 +07:00
|
|
|
|
2011-12-02 18:53:31 +07:00
|
|
|
static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
|
2011-12-10 01:06:04 +07:00
|
|
|
enum kmod_index index_number,
|
2011-12-02 01:25:37 +07:00
|
|
|
const char *name,
|
2011-12-01 05:31:45 +07:00
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2011-12-02 19:02:05 +07:00
|
|
|
int err, nmatch = 0;
|
2011-12-02 18:56:22 +07:00
|
|
|
struct index_file *idx;
|
2011-12-01 05:31:45 +07:00
|
|
|
struct index_value *realnames, *realname;
|
|
|
|
|
2011-12-10 01:11:42 +07:00
|
|
|
if (ctx->indexes[index_number] != NULL) {
|
2011-12-10 18:28:42 +07:00
|
|
|
DBG(ctx, "use mmaped index '%s' for name=%s\n",
|
2012-01-16 19:43:34 +07:00
|
|
|
index_files[index_number].fn, name);
|
2011-12-10 01:11:42 +07:00
|
|
|
realnames = index_mm_searchwild(ctx->indexes[index_number],
|
|
|
|
name);
|
2012-01-01 03:53:24 +07:00
|
|
|
} else {
|
2011-12-10 01:11:42 +07:00
|
|
|
char fn[PATH_MAX];
|
|
|
|
|
2011-12-10 18:21:03 +07:00
|
|
|
snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
|
2012-01-16 19:43:34 +07:00
|
|
|
index_files[index_number].fn);
|
2011-12-01 05:31:45 +07:00
|
|
|
|
2011-12-10 01:11:42 +07:00
|
|
|
DBG(ctx, "file=%s name=%s\n", fn, name);
|
2011-12-01 05:31:45 +07:00
|
|
|
|
2011-12-10 01:11:42 +07:00
|
|
|
idx = index_file_open(fn);
|
|
|
|
if (idx == NULL)
|
|
|
|
return -ENOSYS;
|
2011-12-01 05:31:45 +07:00
|
|
|
|
2011-12-10 01:11:42 +07:00
|
|
|
realnames = index_searchwild(idx, name);
|
|
|
|
index_file_close(idx);
|
|
|
|
}
|
2011-12-10 00:33:37 +07:00
|
|
|
|
2011-12-16 04:17:49 +07:00
|
|
|
for (realname = realnames; realname; realname = realname->next) {
|
2011-12-01 05:31:45 +07:00
|
|
|
struct kmod_module *mod;
|
|
|
|
|
2011-12-13 23:20:48 +07:00
|
|
|
err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
|
2011-12-01 05:31:45 +07:00
|
|
|
if (err < 0) {
|
2012-01-08 04:37:37 +07:00
|
|
|
ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n",
|
|
|
|
name, realname->value, strerror(-err));
|
2011-12-02 00:35:31 +07:00
|
|
|
goto fail;
|
2011-12-01 05:31:45 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
*list = kmod_list_append(*list, mod);
|
2011-12-02 00:35:31 +07:00
|
|
|
nmatch++;
|
2011-12-01 05:31:45 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
index_values_free(realnames);
|
2011-12-02 00:35:31 +07:00
|
|
|
return nmatch;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*list = kmod_list_remove_n_latest(*list, nmatch);
|
2011-12-01 05:31:45 +07:00
|
|
|
return err;
|
2011-12-02 01:25:37 +07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2011-12-28 22:33:26 +07:00
|
|
|
if (!strstartswith(name, "symbol:"))
|
2011-12-02 01:25:37 +07:00
|
|
|
return 0;
|
|
|
|
|
2012-01-16 21:01:48 +07:00
|
|
|
return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL,
|
|
|
|
name, list);
|
2011-12-01 05:31:45 +07:00
|
|
|
}
|
|
|
|
|
2011-12-02 01:27:04 +07:00
|
|
|
int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2012-01-16 21:01:48 +07:00
|
|
|
return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS,
|
|
|
|
name, list);
|
2011-12-02 01:27:04 +07:00
|
|
|
}
|
|
|
|
|
2012-02-17 05:43:16 +07:00
|
|
|
int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2012-02-28 04:48:02 +07:00
|
|
|
char *line = NULL;
|
|
|
|
int err = 0;
|
2012-02-17 05:43:16 +07:00
|
|
|
|
2012-02-28 04:48:02 +07:00
|
|
|
assert(*list == NULL);
|
2012-02-17 05:43:16 +07:00
|
|
|
|
2012-02-28 04:48:02 +07:00
|
|
|
if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) {
|
|
|
|
DBG(ctx, "use mmaped index '%s' modname=%s\n",
|
|
|
|
index_files[KMOD_INDEX_MODULES_BUILTIN].fn,
|
|
|
|
name);
|
|
|
|
line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN],
|
|
|
|
name);
|
|
|
|
} else {
|
|
|
|
struct index_file *idx;
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
|
|
|
|
snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
|
|
|
|
index_files[KMOD_INDEX_MODULES_BUILTIN].fn);
|
|
|
|
DBG(ctx, "file=%s modname=%s\n", fn, name);
|
|
|
|
|
|
|
|
idx = index_file_open(fn);
|
|
|
|
if (idx == NULL) {
|
|
|
|
DBG(ctx, "could not open builtin file '%s'\n", fn);
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = index_search(idx, name);
|
|
|
|
index_file_close(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line != NULL) {
|
|
|
|
struct kmod_module *mod;
|
|
|
|
|
|
|
|
err = kmod_module_new_from_name(ctx, name, &mod);
|
|
|
|
if (err < 0) {
|
|
|
|
ERR(ctx, "Could not create module from name %s: %s\n",
|
|
|
|
name, strerror(-err));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmod_module_set_builtin(mod, true);
|
|
|
|
*list = kmod_list_append(*list, mod);
|
|
|
|
if (*list == NULL)
|
|
|
|
err = -ENOMEM;
|
2012-02-17 05:43:16 +07:00
|
|
|
}
|
|
|
|
|
2012-02-28 04:48:02 +07:00
|
|
|
finish:
|
|
|
|
free(line);
|
2012-02-17 05:43:16 +07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-12-06 05:23:05 +07:00
|
|
|
char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
|
2011-12-06 04:58:39 +07:00
|
|
|
{
|
|
|
|
struct index_file *idx;
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
char *line;
|
|
|
|
|
2012-01-16 21:01:48 +07:00
|
|
|
if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) {
|
2011-12-10 18:26:27 +07:00
|
|
|
DBG(ctx, "use mmaped index '%s' modname=%s\n",
|
2012-01-16 21:01:48 +07:00
|
|
|
index_files[KMOD_INDEX_MODULES_DEP].fn, name);
|
|
|
|
return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP],
|
|
|
|
name);
|
2011-12-10 18:26:27 +07:00
|
|
|
}
|
|
|
|
|
2011-12-08 23:56:48 +07:00
|
|
|
snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
|
2012-01-16 21:01:48 +07:00
|
|
|
index_files[KMOD_INDEX_MODULES_DEP].fn);
|
2011-12-06 04:58:39 +07:00
|
|
|
|
|
|
|
DBG(ctx, "file=%s modname=%s\n", fn, name);
|
|
|
|
|
|
|
|
idx = index_file_open(fn);
|
|
|
|
if (idx == NULL) {
|
2012-02-17 14:00:09 +07:00
|
|
|
DBG(ctx, "could not open moddep file '%s'\n", fn);
|
2011-12-06 04:58:39 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = index_search(idx, name);
|
|
|
|
index_file_close(idx);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2011-12-02 00:57:53 +07:00
|
|
|
int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2011-12-06 04:58:39 +07:00
|
|
|
char *line;
|
2011-12-02 00:57:53 +07:00
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Module names do not contain ':'. Return early if we know it will
|
|
|
|
* not be found.
|
|
|
|
*/
|
|
|
|
if (strchr(name, ':'))
|
|
|
|
return 0;
|
|
|
|
|
2011-12-06 05:23:05 +07:00
|
|
|
line = kmod_search_moddep(ctx, name);
|
2011-12-02 00:57:53 +07:00
|
|
|
if (line != NULL) {
|
|
|
|
struct kmod_module *mod;
|
|
|
|
|
|
|
|
n = kmod_module_new_from_name(ctx, name, &mod);
|
|
|
|
if (n < 0) {
|
2012-01-08 04:37:37 +07:00
|
|
|
ERR(ctx, "Could not create module from name %s: %s\n",
|
|
|
|
name, strerror(-n));
|
2011-12-02 00:57:53 +07:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
*list = kmod_list_append(*list, mod);
|
2011-12-06 05:23:05 +07:00
|
|
|
kmod_module_parse_depline(mod, line);
|
2011-12-02 00:57:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
free(line);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2011-12-01 04:03:41 +07:00
|
|
|
int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
2011-12-03 06:40:22 +07:00
|
|
|
struct kmod_config *config = ctx->config;
|
2011-12-01 04:03:41 +07:00
|
|
|
struct kmod_list *l;
|
2011-12-02 00:35:31 +07:00
|
|
|
int err, nmatch = 0;
|
2011-12-01 04:03:41 +07:00
|
|
|
|
|
|
|
kmod_list_foreach(l, config->aliases) {
|
|
|
|
const char *aliasname = kmod_alias_get_name(l);
|
|
|
|
const char *modname = kmod_alias_get_modname(l);
|
|
|
|
|
|
|
|
if (fnmatch(aliasname, name, 0) == 0) {
|
|
|
|
struct kmod_module *mod;
|
|
|
|
|
2011-12-13 23:20:48 +07:00
|
|
|
err = kmod_module_new_from_alias(ctx, aliasname,
|
|
|
|
modname, &mod);
|
2011-12-01 04:03:41 +07:00
|
|
|
if (err < 0) {
|
2012-01-08 04:37:37 +07:00
|
|
|
ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n",
|
|
|
|
name, modname, strerror(-err));
|
2011-12-02 00:35:31 +07:00
|
|
|
goto fail;
|
2011-12-01 04:03:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
*list = kmod_list_append(*list, mod);
|
2011-12-02 00:35:31 +07:00
|
|
|
nmatch++;
|
2011-12-01 04:03:41 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 00:35:31 +07:00
|
|
|
return nmatch;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*list = kmod_list_remove_n_latest(*list, nmatch);
|
|
|
|
return err;
|
2011-12-01 04:03:41 +07:00
|
|
|
}
|
2011-12-08 14:17:43 +07:00
|
|
|
|
2011-12-16 12:57:12 +07:00
|
|
|
int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
|
|
|
|
struct kmod_list **list)
|
|
|
|
{
|
|
|
|
struct kmod_config *config = ctx->config;
|
|
|
|
struct kmod_list *l, *node;
|
|
|
|
int err, nmatch = 0;
|
|
|
|
|
|
|
|
kmod_list_foreach(l, config->install_commands) {
|
|
|
|
const char *modname = kmod_command_get_modname(l);
|
|
|
|
|
|
|
|
if (streq(modname, name)) {
|
|
|
|
const char *cmd = kmod_command_get_command(l);
|
|
|
|
struct kmod_module *mod;
|
|
|
|
|
|
|
|
err = kmod_module_new_from_name(ctx, modname, &mod);
|
|
|
|
if (err < 0) {
|
2012-01-08 04:37:37 +07:00
|
|
|
ERR(ctx, "Could not create module from name %s: %s\n",
|
|
|
|
modname, strerror(-err));
|
2011-12-16 12:57:12 +07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = kmod_list_append(*list, mod);
|
|
|
|
if (node == NULL) {
|
|
|
|
ERR(ctx, "out of memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*list = node;
|
|
|
|
nmatch = 1;
|
|
|
|
|
|
|
|
kmod_module_set_install_commands(mod, cmd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* match only the first one, like modprobe from
|
|
|
|
* module-init-tools does
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nmatch)
|
|
|
|
return nmatch;
|
|
|
|
|
|
|
|
kmod_list_foreach(l, config->remove_commands) {
|
|
|
|
const char *modname = kmod_command_get_modname(l);
|
|
|
|
|
|
|
|
if (streq(modname, name)) {
|
|
|
|
const char *cmd = kmod_command_get_command(l);
|
|
|
|
struct kmod_module *mod;
|
|
|
|
|
|
|
|
err = kmod_module_new_from_name(ctx, modname, &mod);
|
|
|
|
if (err < 0) {
|
2012-01-08 04:37:37 +07:00
|
|
|
ERR(ctx, "Could not create module from name %s: %s\n",
|
|
|
|
modname, strerror(-err));
|
2011-12-16 12:57:12 +07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = kmod_list_append(*list, mod);
|
|
|
|
if (node == NULL) {
|
|
|
|
ERR(ctx, "out of memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*list = node;
|
|
|
|
nmatch = 1;
|
|
|
|
|
|
|
|
kmod_module_set_remove_commands(mod, cmd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* match only the first one, like modprobe from
|
|
|
|
* module-init-tools does
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nmatch;
|
|
|
|
}
|
|
|
|
|
2012-01-18 10:26:44 +07:00
|
|
|
void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited)
|
|
|
|
{
|
|
|
|
struct hash_iter iter;
|
|
|
|
const void *v;
|
|
|
|
|
|
|
|
hash_iter_init(ctx->modules_by_name, &iter);
|
|
|
|
while (hash_iter_next(&iter, NULL, &v))
|
|
|
|
kmod_module_set_visited((struct kmod_module *)v, visited);
|
|
|
|
}
|
|
|
|
|
2012-01-01 04:28:31 +07:00
|
|
|
static bool is_cache_invalid(const char *path, unsigned long long stamp)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st) < 0)
|
|
|
|
return true;
|
|
|
|
|
2012-01-17 21:10:42 +07:00
|
|
|
if (stamp != stat_mstamp(&st))
|
2012-01-01 04:28:31 +07:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kmod_validate_resources:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Check if indexes and configuration files changed on disk and the current
|
|
|
|
* context is not valid anymore.
|
|
|
|
*
|
2012-01-09 11:35:41 +07:00
|
|
|
* Returns: KMOD_RESOURCES_OK if resources are still valid,
|
2012-01-01 04:28:31 +07:00
|
|
|
* KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
|
|
|
|
* kmod_unload_resources() and kmod_load_resources() or
|
|
|
|
* KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
|
|
|
|
*/
|
|
|
|
KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct kmod_list *l;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (ctx == NULL || ctx->config == NULL)
|
|
|
|
return KMOD_RESOURCES_MUST_RECREATE;
|
|
|
|
|
|
|
|
kmod_list_foreach(l, ctx->config->paths) {
|
|
|
|
struct kmod_config_path *cf = l->data;
|
|
|
|
|
|
|
|
if (is_cache_invalid(cf->path, cf->stamp))
|
|
|
|
return KMOD_RESOURCES_MUST_RECREATE;
|
|
|
|
}
|
|
|
|
|
2012-01-16 21:01:48 +07:00
|
|
|
for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
|
2012-01-01 04:28:31 +07:00
|
|
|
char path[PATH_MAX];
|
|
|
|
|
|
|
|
if (ctx->indexes[i] == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
|
2012-01-16 19:43:34 +07:00
|
|
|
index_files[i].fn);
|
2012-01-01 04:28:31 +07:00
|
|
|
|
|
|
|
if (is_cache_invalid(path, ctx->indexes_stamp[i]))
|
|
|
|
return KMOD_RESOURCES_MUST_RELOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return KMOD_RESOURCES_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:44:38 +07:00
|
|
|
/**
|
|
|
|
* kmod_load_resources:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Load indexes and keep them open in @ctx. This way it's faster to lookup
|
|
|
|
* information within the indexes. If this function is not called before a
|
|
|
|
* search, the necessary index is always opened and closed.
|
|
|
|
*
|
|
|
|
* If user will do more than one or two lookups, insertions, deletions, most
|
|
|
|
* likely it's good to call this function first. Particularly in a daemon like
|
|
|
|
* udev that on bootup issues hundreds of calls to lookup the index, calling
|
|
|
|
* this function will speedup the searches.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success or < 0 otherwise.
|
|
|
|
*/
|
2011-12-08 23:59:51 +07:00
|
|
|
KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2012-01-16 21:01:48 +07:00
|
|
|
for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
|
2011-12-14 12:58:31 +07:00
|
|
|
char path[PATH_MAX];
|
2011-12-14 12:53:43 +07:00
|
|
|
|
2011-12-20 21:29:13 +07:00
|
|
|
if (ctx->indexes[i] != NULL) {
|
2012-01-16 19:43:34 +07:00
|
|
|
INFO(ctx, "Index %s already loaded\n",
|
|
|
|
index_files[i].fn);
|
2011-12-14 12:53:43 +07:00
|
|
|
continue;
|
|
|
|
}
|
2011-12-10 19:27:27 +07:00
|
|
|
|
2011-12-14 12:58:31 +07:00
|
|
|
snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
|
2012-01-16 19:43:34 +07:00
|
|
|
index_files[i].fn);
|
2012-03-03 06:33:26 +07:00
|
|
|
ctx->indexes[i] = index_mm_open(ctx, path,
|
2012-01-01 03:53:24 +07:00
|
|
|
&ctx->indexes_stamp[i]);
|
2011-12-14 12:53:43 +07:00
|
|
|
if (ctx->indexes[i] == NULL)
|
|
|
|
goto fail;
|
2011-12-08 23:59:51 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
kmod_unload_resources(ctx);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:44:38 +07:00
|
|
|
/**
|
|
|
|
* kmod_unload_resources:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
*
|
|
|
|
* Unload all the indexes. This will free the resources to maintain the index
|
|
|
|
* open and all subsequent searches will need to open and close the index.
|
|
|
|
*
|
|
|
|
* User is free to call kmod_load_resources() and kmod_unload_resources() as
|
|
|
|
* many times as wanted during the lifecycle of @ctx. For example, if a daemon
|
|
|
|
* knows that when starting up it will lookup a lot of modules, it could call
|
|
|
|
* kmod_load_resources() and after the first burst of searches is gone, it
|
|
|
|
* could free the resources by calling kmod_unload_resources().
|
|
|
|
*
|
|
|
|
* Returns: 0 on success or < 0 otherwise.
|
|
|
|
*/
|
2011-12-08 23:59:51 +07:00
|
|
|
KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-01-16 21:01:48 +07:00
|
|
|
for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
|
2011-12-08 23:59:51 +07:00
|
|
|
if (ctx->indexes[i] != NULL) {
|
|
|
|
index_mm_close(ctx->indexes[i]);
|
|
|
|
ctx->indexes[i] = NULL;
|
2012-01-01 03:53:24 +07:00
|
|
|
ctx->indexes_stamp[i] = 0;
|
2011-12-08 23:59:51 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-11 05:47:01 +07:00
|
|
|
|
2012-01-17 01:43:47 +07:00
|
|
|
/**
|
|
|
|
* kmod_dump_index:
|
|
|
|
* @ctx: kmod library context
|
|
|
|
* @type: index to dump
|
|
|
|
* @fd: file descriptor to dump index to
|
|
|
|
*
|
2012-01-17 19:05:02 +07:00
|
|
|
* Dump index to file descriptor. Note that this function doesn't use stdio.h
|
|
|
|
* so call fflush() before calling this function to be sure data is written in
|
|
|
|
* order.
|
2012-01-17 01:43:47 +07:00
|
|
|
*
|
|
|
|
* Returns: 0 on success or < 0 otherwise.
|
|
|
|
*/
|
2012-01-17 00:56:17 +07:00
|
|
|
KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
|
|
|
|
int fd)
|
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
if (ctx->indexes[type] != NULL) {
|
|
|
|
DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
|
|
|
|
index_mm_dump(ctx->indexes[type], fd,
|
|
|
|
index_files[type].prefix);
|
|
|
|
} else {
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
struct index_file *idx;
|
|
|
|
|
|
|
|
snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
|
|
|
|
index_files[type].fn);
|
|
|
|
|
|
|
|
DBG(ctx, "file=%s\n", fn);
|
|
|
|
|
|
|
|
idx = index_file_open(fn);
|
|
|
|
if (idx == NULL)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
index_dump(idx, fd, index_files[type].prefix);
|
|
|
|
index_file_close(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-24 19:50:47 +07:00
|
|
|
const struct kmod_list *kmod_get_blacklists(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ctx->config->blacklists;
|
|
|
|
}
|
|
|
|
|
2011-12-11 05:47:01 +07:00
|
|
|
const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ctx->config->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ctx->config->install_commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ctx->config->remove_commands;
|
|
|
|
}
|
2011-12-17 06:18:10 +07:00
|
|
|
|
|
|
|
const struct kmod_list *kmod_get_softdeps(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ctx->config->softdeps;
|
|
|
|
}
|
2012-01-13 10:14:46 +07:00
|
|
|
|
|
|
|
const struct kmod_list *kmod_get_aliases(const struct kmod_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ctx->config->aliases;
|
|
|
|
}
|