| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2007 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Plugin management. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <string.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
#include <signal.h> |
| 16 |
#include <fcntl.h> |
| 17 |
#include <assert.h> |
| 18 |
#include <dlfcn.h> |
| 19 |
|
| 20 |
#include "utils.h" |
| 21 |
#include "plugin.h" |
| 22 |
|
| 23 |
/* Plugin list */ |
| 24 |
static struct plugin *plugin_list = NULL; |
| 25 |
|
| 26 |
/* Find a symbol address */ |
| 27 |
void *plugin_find_symbol(struct plugin *plugin,char *symbol) |
| 28 |
{ |
| 29 |
return((plugin != NULL) ? dlsym(plugin->dl_handle,symbol) : NULL); |
| 30 |
} |
| 31 |
|
| 32 |
/* Initialize a plugin */ |
| 33 |
static int plugin_init(struct plugin *plugin) |
| 34 |
{ |
| 35 |
plugin_init_t init; |
| 36 |
|
| 37 |
if (!(init = plugin_find_symbol(plugin,"init"))) |
| 38 |
return(-1); |
| 39 |
|
| 40 |
return(init()); |
| 41 |
} |
| 42 |
|
| 43 |
/* Load a plugin */ |
| 44 |
struct plugin *plugin_load(char *filename) |
| 45 |
{ |
| 46 |
struct plugin *p; |
| 47 |
|
| 48 |
if (!(p = malloc(sizeof(*p)))) |
| 49 |
return NULL; |
| 50 |
|
| 51 |
memset(p,0,sizeof(*p)); |
| 52 |
|
| 53 |
if (!(p->filename = strdup(filename))) |
| 54 |
goto err_strdup; |
| 55 |
|
| 56 |
if (!(p->dl_handle = dlopen(filename,RTLD_LAZY))) { |
| 57 |
fprintf(stderr,"plugin_load(\"%s\"): %s\n",filename,dlerror()); |
| 58 |
goto err_dlopen; |
| 59 |
} |
| 60 |
|
| 61 |
if (plugin_init(p) == -1) |
| 62 |
goto err_init; |
| 63 |
|
| 64 |
p->next = plugin_list; |
| 65 |
plugin_list = p; |
| 66 |
return p; |
| 67 |
|
| 68 |
err_init: |
| 69 |
dlclose(p->dl_handle); |
| 70 |
err_dlopen: |
| 71 |
free(p->filename); |
| 72 |
err_strdup: |
| 73 |
free(p); |
| 74 |
return NULL; |
| 75 |
} |