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