src/module.c
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'
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "module.h"
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
     2
#include "log.h"
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <stdlib.h>
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <dlfcn.h>
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
     6
#include <string.h>
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
     7
#include <assert.h>
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
err_t modules_create (struct modules **modules_ptr, struct nexus *nexus)
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
{
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    11
    struct modules *modules;
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    // alloc
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    if ((modules = calloc(1, sizeof(*modules))) == NULL)
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
        return ERR_CALLOC;
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    // init
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    18
    TAILQ_INIT(&modules->list);
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    // store
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    modules->nexus = nexus;
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    22
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    23
    // ok
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    24
    *modules_ptr = modules;
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    25
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    26
    return SUCCESS;
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
}
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    29
/**
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    30
 * Load the symbol named "<module>_<suffix>".
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    31
 */ 
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    32
static err_t module_symbol (struct module *module, void **sym, const char *suffix)
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    33
{
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    34
    char sym_name[MODULE_SYMBOL_MAX];
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    35
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    36
    // validate the length of the suffix
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    37
    assert(strlen(module->info.name) <= MODULE_NAME_MAX);
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    38
    assert(strlen(suffix) <= MODULE_SUFFIX_MAX);
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    39
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    40
    // format
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    41
    sprintf(sym_name, "%s_%s", module->info.name, suffix);
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    42
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    43
    // load
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    44
    if ((*sym = dlsym(module->handle, sym_name)) == NULL)
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    45
       return ERR_MODULE_SYM;
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    46
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    47
    // ok
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    48
    return SUCCESS;
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    49
}
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    50
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    51
err_t module_load (struct modules *modules, struct module **module_ptr, const struct module_info *info, struct error_info *err)
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
{
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    struct module *module;
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    55
    // validate the module name
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    56
    if (strlen(info->name) > MODULE_NAME_MAX)
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    57
        return SET_ERROR(err, ERR_MODULE_NAME);
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    58
    
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    59
    // alloc
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    60
    if ((module = calloc(1, sizeof(*module))) == NULL)
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    61
        return SET_ERROR(err, ERR_CALLOC);
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    62
    
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    63
    // store
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    64
    module->info = *info;
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    66
    // clear dlerrors
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    67
    (void) dlerror();
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    68
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    69
    // load it
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    70
    if ((module->handle = dlopen(info->path, RTLD_NOW)) == NULL)
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    71
        JUMP_SET_ERROR_STR(err, ERR_MODULE_OPEN, dlerror());
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    72
    
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    73
    // load the funcs symbol
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    74
    if ((ERROR_CODE(err) = module_symbol(module, (void **) &module->funcs, "funcs")))
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    75
        JUMP_SET_ERROR_STR(err, ERROR_CODE(err), dlerror());
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    76
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    77
    // call the init func
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    78
    if ((module->funcs->init(modules->nexus, &module->ctx, err)))
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    79
        goto error;
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    80
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    81
    // add to modules list
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    82
    TAILQ_INSERT_TAIL(&modules->list, module, modules_list);
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    83
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    84
    // ok
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    85
    *module_ptr = module;
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    86
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    87
    return SUCCESS;
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    88
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    89
error:
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    90
    // XXX: cleanup
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    91
    free(module);
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    92
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    93
    return ERROR_CODE(err);    
54
9f74e924b01a initial modules code
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
}
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    95
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    96
err_t module_conf (struct module *module, const char *name, char *value, struct error_info *err)
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
    97
{
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    98
    // call the conf func
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    99
    return module->funcs->conf(module->ctx, name, value, err);
55
6f7f6ae729d0 'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
Tero Marttila <terom@fixme.fi>
parents: 54
diff changeset
   100
}