View | Details | Raw Unified | Return to bug 225082
Collapse All | Expand All

(-)a/Makefile (+6 lines)
Lines 25-30 LIBS=-lpthread Link Here
25
RANLIB=ranlib $@
25
RANLIB=ranlib $@
26
else
26
else
27
LIBS=-lpthread -lssl -lrt
27
LIBS=-lpthread -lssl -lrt
28
29
ifdef PATH_Plugins
30
CFLAGS += -rdynamic -DPATH_Plugins="$(PATH_Plugins)"
31
LIBS += -ldl
32
endif
33
28
endif
34
endif
29
endif
35
endif
30
endif
36
endif
(-)a/config.h (+15 lines)
Lines 79-84 Link Here
79
#define SERVICE_Streams         (TARGET_UNIX || TARGET_VXWORKS || TARGET_WINDOWS)
79
#define SERVICE_Streams         (TARGET_UNIX || TARGET_VXWORKS || TARGET_WINDOWS)
80
#endif
80
#endif
81
81
82
/*
83
 * If a plugins' path is defined, then enable plugins for UNIX/Linux.
84
 */
85
#ifdef PATH_Plugins
86
#define EN_Plugins              (TARGET_UNIX)
87
#endif
88
82
#if !defined(ENABLE_ZeroCopy)
89
#if !defined(ENABLE_ZeroCopy)
83
#define ENABLE_ZeroCopy           1
90
#define ENABLE_ZeroCopy           1
84
#endif
91
#endif
Lines 128-133 Link Here
128
#include "proxy.h"
135
#include "proxy.h"
129
#include "tcf_elf.h"
136
#include "tcf_elf.h"
130
137
138
#include "plugins.h"
139
131
static void ini_services(Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGroup * spg) {
140
static void ini_services(Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGroup * spg) {
132
#if SERVICE_Locator
141
#if SERVICE_Locator
133
    ini_locator_service(proto, bcg);
142
    ini_locator_service(proto, bcg);
Lines 177-185 static void ini_services(Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGr Link Here
177
#if ENABLE_ELF
186
#if ENABLE_ELF
178
    ini_elf();
187
    ini_elf();
179
#endif
188
#endif
189
190
#if EN_Plugins
191
    plugins_load(proto, bcg, spg);
192
#endif
193
180
    ini_diagnostics_service(proto);
194
    ini_diagnostics_service(proto);
181
}
195
}
182
196
183
#endif /* CONFIG_MAIN */
197
#endif /* CONFIG_MAIN */
184
198
185
#endif /* D_config */
199
#endif /* D_config */
200
(-)a/main.c (+6 lines)
Lines 30-35 Link Here
30
#include "myalloc.h"
30
#include "myalloc.h"
31
#include "test.h"
31
#include "test.h"
32
#include "cmdline.h"
32
#include "cmdline.h"
33
#include "plugins.h"
33
#include "channel_tcp.h"
34
#include "channel_tcp.h"
34
35
35
static char * progname;
36
static char * progname;
Lines 206-210 int main(int argc, char ** argv) { Link Here
206
    /* Process events - must run on the initial thread since ptrace()
207
    /* Process events - must run on the initial thread since ptrace()
207
     * returns ECHILD otherwise, thinking we are not the owner. */
208
     * returns ECHILD otherwise, thinking we are not the owner. */
208
    run_event_loop();
209
    run_event_loop();
210
211
#ifdef EN_Plugins
212
    plugins_destroy();
213
#endif // EN_Plugins
214
209
    return 0;
215
    return 0;
210
}
216
}
(-)a/plugins.c (+133 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Philippe Proulx, École Polytechnique de Montréal
3
 *                    Michael Sills-Lavoie, École Polytechnique de Montréal
4
 * and others. All rights reserved.
5
 * This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * and Eclipse Distribution License v1.0 which accompany this distribution.
8
 * The Eclipse Public License is available at
9
 * http://www.eclipse.org/legal/epl-v10.html
10
 * and the Eclipse Distribution License is available at
11
 * http://www.eclipse.org/org/documents/edl-v10.php.
12
 *
13
 * Contributors:
14
 *     Philippe Proulx - initial plugins system
15
 *******************************************************************************/
16
17
/*
18
 * Expression evaluation service.
19
 */
20
21
#include "config.h"
22
23
#if EN_Plugins
24
25
#ifndef _GNU_SOURCE
26
#define _GNU_SOURCE
27
#endif /* _GNU_SOURCE */
28
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <sys/types.h>
33
#include <dirent.h>
34
#include <dlfcn.h>
35
36
#include "trace.h"
37
#include "plugins.h"
38
39
static void ** plugins_handles = NULL;
40
static size_t plugins_count = 0;
41
42
static inline int plugins_ext_is(const char * ext, const char * filename) {
43
    int ret = 0;
44
    const char* real_ext = strrchr(filename, '.');
45
46
    if (real_ext != NULL) {
47
        ret = !strcmp(real_ext + 1, ext);
48
    }
49
50
    return ret;
51
}
52
53
int plugins_load(Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGroup * spg) {
54
    int ret = 0;
55
    char* cur_plugin_path;
56
    struct dirent* dirent;
57
    DIR* dir;
58
59
    dir = opendir(QUOTE(PATH_Plugins));
60
    if (!dir) {
61
        trace(LOG_ALWAYS, "plugins error: failed opening plugins directory \"" QUOTE(PATH_Plugins) "\"");
62
        return -1;
63
    }
64
    while (dirent = readdir(dir)) {
65
        if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) {
66
            continue;
67
        }
68
        if (!plugins_ext_is(PLUGINS_DEF_EXT, dirent->d_name) || dirent->d_type == DT_DIR) {
69
            continue;
70
        }
71
        if (asprintf(&cur_plugin_path, QUOTE(PATH_Plugins) "/%s", dirent->d_name) == -1) {
72
            trace(LOG_ALWAYS, "plugins error: `asprintf' failed for plugin \"%s\"", dirent->d_name);
73
            return -1;
74
        }
75
        if (plugin_init(cur_plugin_path, proto, bcg, spg)) {
76
            trace(LOG_ALWAYS, "plugins error: unable to start plugin \"%s\"", cur_plugin_path);
77
            ret = -1;
78
            goto error_free;
79
        }
80
    }
81
    error_free:
82
    free(cur_plugin_path);
83
84
    return ret;
85
}
86
87
int plugin_init(const char * name, Protocol * proto, TCFBroadcastGroup * bcg, TCFSuspendGroup * spg) {
88
    void* handle;
89
    void (* init)(Protocol *, TCFBroadcastGroup *, TCFSuspendGroup *);
90
    char* error;
91
92
    /* Plugin loading: */
93
    trace(LOG_ALWAYS, "loading plugin \"%s\"", name);
94
    handle = dlopen(name, RTLD_LAZY);
95
    if (!handle) {
96
        trace(LOG_ALWAYS, "plugins error: \"%s\"", dlerror());
97
        return -1;
98
    }
99
100
    /* Plugin initialization: */
101
    init = dlsym(handle, "tcf_init_plugin");
102
    if ((error = dlerror()) != NULL) {
103
        trace(LOG_ALWAYS, "plugins error: \"%s\"", error);
104
        return -1;
105
    }
106
    trace(LOG_ALWAYS, "initializing plugin \"%s\"", name);
107
    init(proto, bcg, spg);
108
109
    /* Handles table update: */
110
    plugins_handles = (void **) realloc(plugins_handles, ++plugins_count * sizeof(void *));
111
    plugins_handles[plugins_count - 1] = handle;
112
113
    return 0;
114
}
115
116
int plugins_destroy(void) {
117
    if (plugins_handles == NULL) {
118
        return 0;
119
    }
120
    size_t i;
121
122
    for (i = 0; i < plugins_count; ++i) {
123
        if (dlclose(plugins_handles[i])) {
124
            trace(LOG_ALWAYS, "plugins error: \"%s\"", dlerror());
125
        }
126
    }
127
    free(plugins_handles);
128
129
    return 0;
130
}
131
132
#endif  /* if EN_Plugins */
133
(-)a/plugins.h (+47 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 Philippe Proulx, École Polytechnique de Montréal
3
 *                    Michael Sills-Lavoie, École Polytechnique de Montréal
4
 * and others. All rights reserved.
5
 * This program and the accompanying materials
6
 * are made available under the terms of the Eclipse Public License v1.0
7
 * and Eclipse Distribution License v1.0 which accompany this distribution.
8
 * The Eclipse Public License is available at
9
 * http://www.eclipse.org/legal/epl-v10.html
10
 * and the Eclipse Distribution License is available at
11
 * http://www.eclipse.org/org/documents/edl-v10.php.
12
 *
13
 * Contributors:
14
 *     Philippe Proulx - initial plugins system
15
 *******************************************************************************/
16
17
/*
18
 * Plugins system.
19
 */
20
21
#ifndef D_plugins
22
#define D_plugins
23
24
#include "protocol.h"
25
26
#define _QUOTEME(x)     #x
27
#define QUOTE(x)      _QUOTEME(x)
28
29
#define PLUGINS_DEF_EXT     "so"        /* Default plugins' extension */
30
31
/*
32
 * Loads ALL plugins from the directory PATH_Plugins (from `config.h').
33
 */
34
int plugins_load(Protocol *, TCFBroadcastGroup *, TCFSuspendGroup *);
35
36
/*
37
 * Initializes a particular plugin according to its path.
38
 */
39
int plugin_init(const char *, Protocol *, TCFBroadcastGroup *, TCFSuspendGroup *);
40
41
/*
42
 * Destroys loaded plugins.
43
 */
44
int plugins_destroy(void);
45
46
#endif /* D_plugins */
47

Return to bug 225082