src/module.h
author Tero Marttila <terom@fixme.fi>
Sun, 15 Mar 2009 23:22:57 +0200
branchmodules
changeset 57 ce1accba5fc7
parent 56 942370000450
child 65 d7508879ad01
permissions -rw-r--r--
slight cleanup to move module funcs to a 'struct module_funcs'
#ifndef MODULE_H
#define MODULE_H

/**
 * @file
 *
 * Dynamically loadable modules for use with nexus.
 *
 * The modules are loaded using dlopen(), and hence should be standard dynamic libraries. Module initialization happens
 * using a module_init_func_t named "<name>_init", which should return some kind of context pointer, which can later be
 * used to perform other operations on the module.
 */
#include "nexus.h"
#include "error.h"

#include <sys/queue.h>

/**
 * Information required to load/identify a module.
 */
struct module_info {
    /** Human-readable name */
    const char *name;
    
    /** Filesystem path to the .so */
    const char *path;
};

/**
 * A module's behaviour is defined as a set of function pointers, which is dynamically resolved from the module DSO,
 * using the <mod_name>_funcs symbol.
 */
struct module_funcs {
    /**
     * Initialize the module, returning an opaque context pointer that is stored in the module state, and supplied for
     * subsequent calls. The supplied nexus arg can be used to access the global state.
     *
     * @param nexus a pointer to the nexus struct containing the global state
     * @param ctx_ptr the context pointer should be returned via this
     * @param err returned error info
     */
    err_t (*init) (struct nexus *nexus, void **ctx_ptr, struct error_info *err);

    /**
     * Set a configuration option with the given name and value, given by the user. Configuration settings are not
     * regarded as unique-per-name, but rather, several invocations of 'conf' with the same name and different values
     * could set up a multitude of things.
     *
     * XXX: make value a non-modifyable string
     *
     * @param ctx the module's context pointer as returned by init
     * @param name the name of the configuration setting
     * @param value the value of the configuration setting
     * @param err returned error info
     */
    err_t (*conf) (void *ctx, const char *name, char *value, struct error_info *err);
};

/**
 * A loaded module.
 */
struct module {
    /** The identifying info for the module */
    struct module_info info;

    /** The dlopen handle */
    void *handle;

    /** The resolved function table */
    struct module_funcs *funcs;

    /** The module context object */
    void *ctx;

    /** Our entry in the list of modules */
    TAILQ_ENTRY(module) modules_list;
};

/**
 * A set of loaded modules, and functionality to load more
 */
struct modules {
    /** The nexus in use */
    struct nexus *nexus;

    /** List of loaded modules */
    TAILQ_HEAD(module_ctx_modules, module) list;
};

/**
 * Possible error codes
 */
enum module_error_code {
    _ERR_MODULE_BEGIN = _ERR_MODULE,
    
    ERR_MODULE_OPEN,        ///< dlopen() failed
    ERR_MODULE_NAME,        ///< invalid module_info.name
    ERR_MODULE_SYM,         ///< invalid symbol
    ERR_MODULE_INIT_FUNC,   ///< invalid module_init_func_t
    ERR_MODULE_CONF,        ///< value error in configuration data
};


/**
 * Maximum length of a module name
 */
#define MODULE_NAME_MAX 24

/**
 * Maximum length of module symbol suffix
 */
#define MODULE_SUFFIX_MAX 16

/**
 * Maximum length of symbol name name, including terminating NUL
 */
#define MODULE_SYMBOL_MAX (MODULE_NAME_MAX + 1 + MODULE_SUFFIX_MAX + 1)

/**
 * Create a new modules state
 */
err_t modules_create (struct modules **modules_ptr, struct nexus *nexus);

/**
 * Load a new module
 */
err_t module_load (struct modules *modules, struct module **module_ptr, const struct module_info *info, struct error_info *err);

/**
 * Set a module configuration option
 */
err_t module_conf (struct module *module, const char *name, char *value, struct error_info *err);

/**
 * Unload a module
 *
 * XXX: not implemented
 */
err_t module_unload (struct module *module);

#endif