2011-11-21 21:35:15 +07:00
|
|
|
/*
|
2011-11-21 23:35:35 +07:00
|
|
|
* libkmod - interface to kernel module operations
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 ProFUSION embedded systems
|
|
|
|
* Copyright (C) 2011 Lucas De Marchi <lucas.de.marchi@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
#include "libkmod.h"
|
|
|
|
#include "libkmod-private.h"
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
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-11-21 23:35:35 +07:00
|
|
|
void (*log_fn)(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, va_list args);
|
2011-11-21 21:35:15 +07:00
|
|
|
void *userdata;
|
|
|
|
int log_priority;
|
|
|
|
};
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
void kmod_log(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;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
ctx->log_fn(ctx, priority, file, line, fn, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
static void log_stderr(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, va_list args)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
2011-11-21 23:35:35 +07:00
|
|
|
fprintf(stderr, "libkmod: %s: ", fn);
|
2011-11-21 21:35:15 +07:00
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
* to access from callbacks like a custom logging function.
|
|
|
|
*
|
|
|
|
* Returns: stored userdata
|
|
|
|
**/
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT void *kmod_get_userdata(struct kmod_ctx *ctx)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return NULL;
|
|
|
|
return ctx->userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, 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-11-21 23:35:35 +07:00
|
|
|
* kmod_new:
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
2011-11-21 23:35:35 +07:00
|
|
|
* Create kmod library context. This reads the kmod configuration
|
2011-11-21 21:35:15 +07:00
|
|
|
* and fills in the default values.
|
|
|
|
*
|
|
|
|
* The initial refcount is 1, and needs to be decremented to
|
2011-11-21 23:35:35 +07:00
|
|
|
* release the resources of the kmod library context.
|
2011-11-21 21:35:15 +07:00
|
|
|
*
|
2011-11-21 23:35:35 +07:00
|
|
|
* Returns: a new kmod library context
|
2011-11-21 21:35:15 +07:00
|
|
|
**/
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT int kmod_new(struct kmod_ctx **ctx)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
const char *env;
|
2011-11-21 23:35:35 +07:00
|
|
|
struct kmod_ctx *c;
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
c = calloc(1, sizeof(struct kmod_ctx));
|
2011-11-21 21:35:15 +07:00
|
|
|
if (!c)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
c->refcount = 1;
|
|
|
|
c->log_fn = log_stderr;
|
|
|
|
c->log_priority = LOG_ERR;
|
|
|
|
|
|
|
|
/* 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-21 23:35:35 +07:00
|
|
|
kmod_set_log_priority(c, log_priority(env));
|
2011-11-21 21:35:15 +07:00
|
|
|
|
|
|
|
info(c, "ctx %p created\n", c);
|
|
|
|
dbg(c, "log_priority=%d\n", c->log_priority);
|
|
|
|
*ctx = c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-11-21 21:35:15 +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-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;
|
|
|
|
ctx->refcount--;
|
|
|
|
if (ctx->refcount > 0)
|
|
|
|
return ctx;
|
|
|
|
info(ctx, "context %p released\n", ctx);
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* 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-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
|
2011-11-21 23:59:23 +07:00
|
|
|
void (*log_fn)(struct kmod_ctx *ctx,
|
|
|
|
int priority, const char *file,
|
|
|
|
int line, const char *fn,
|
|
|
|
const char *format, va_list args))
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
ctx->log_fn = log_fn;
|
|
|
|
info(ctx, "custom logging function %p registered\n", log_fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT int kmod_get_log_priority(struct kmod_ctx *ctx)
|
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-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
|
|
|
{
|
|
|
|
ctx->log_priority = priority;
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
struct kmod_list_entry;
|
|
|
|
struct kmod_list_entry *kmod_list_entry_get_next(struct kmod_list_entry *list_entry);
|
|
|
|
const char *kmod_list_entry_get_name(struct kmod_list_entry *list_entry);
|
|
|
|
const char *kmod_list_entry_get_value(struct kmod_list_entry *list_entry);
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
struct kmod_thing {
|
|
|
|
struct kmod_ctx *ctx;
|
2011-11-21 21:35:15 +07:00
|
|
|
int refcount;
|
|
|
|
};
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT struct kmod_thing *kmod_thing_ref(struct kmod_thing *thing)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (!thing)
|
|
|
|
return NULL;
|
|
|
|
thing->refcount++;
|
|
|
|
return thing;
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT struct kmod_thing *kmod_thing_unref(struct kmod_thing *thing)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
if (thing == NULL)
|
|
|
|
return NULL;
|
|
|
|
thing->refcount--;
|
|
|
|
if (thing->refcount > 0)
|
|
|
|
return thing;
|
|
|
|
dbg(thing->ctx, "context %p released\n", thing);
|
|
|
|
free(thing);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT struct kmod_ctx *kmod_thing_get_ctx(struct kmod_thing *thing)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
return thing->ctx;
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT int kmod_thing_new_from_string(struct kmod_ctx *ctx, const char *string, struct kmod_thing **thing)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
2011-11-21 23:35:35 +07:00
|
|
|
struct kmod_thing *t;
|
2011-11-21 21:35:15 +07:00
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
t = calloc(1, sizeof(struct kmod_thing));
|
2011-11-21 21:35:15 +07:00
|
|
|
if (!t)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
t->refcount = 1;
|
|
|
|
t->ctx = ctx;
|
|
|
|
*thing = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-21 23:35:35 +07:00
|
|
|
KMOD_EXPORT struct kmod_list_entry *kmod_thing_get_some_list_entry(struct kmod_thing *thing)
|
2011-11-21 21:35:15 +07:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|