-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
flb_plugin.c
393 lines (335 loc) · 10.1 KB
/
flb_plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2019 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_compat.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_plugin_proxy.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PLUGIN_PREFIX "flb-"
#define PLUGIN_EXTENSION ".so"
#define PLUGIN_STRUCT_SUFFIX "_plugin"
#define PLUGIN_STR_MIN \
((sizeof(PLUGIN_PREFIX) - 1) + sizeof(PLUGIN_EXTENSION) - 1)
static int is_input(char *name)
{
if (strncmp(name, "in_", 3) == 0) {
return FLB_TRUE;
}
return FLB_FALSE;
}
static int is_filter(char *name)
{
if (strncmp(name, "filter_", 7) == 0) {
return FLB_TRUE;
}
return FLB_FALSE;
}
static int is_output(char *name)
{
if (strncmp(name, "out_", 4) == 0) {
return FLB_TRUE;
}
return FLB_FALSE;
}
static void *get_handle(const char *path)
{
void *handle;
handle = dlopen(path, RTLD_LAZY);
if (!handle) {
flb_error("[plugin] dlopen() %s", dlerror());
return NULL;
}
return handle;
}
static void *load_symbol(void *dso_handle, const char *symbol)
{
void *s;
dlerror();
s = dlsym(dso_handle, symbol);
if (dlerror() != NULL) {
return NULL;
}
return s;
}
/*
* From a given path file (.so file), retrieve the expected structure name
* used to perform the plugin registration.
*/
static char *path_to_plugin_name(char *path)
{
int len;
int o_len;
char *bname;
char *name;
char *p;
/* Get the basename of the file */
bname = basename(path);
if (!bname) {
flb_error("[plugin] could not resolve basename(3) of the plugin");
return NULL;
}
len = strlen(bname);
if (len < PLUGIN_STR_MIN) {
flb_error("[plugin] invalid plugin name: %s", bname);
return NULL;
}
if (strncmp(bname, PLUGIN_PREFIX, sizeof(PLUGIN_PREFIX) - 1) != 0) {
flb_error("[plugin] invalid plugin prefix: %s", bname);
return NULL;
}
if (strncmp(bname + len - (sizeof(PLUGIN_EXTENSION) - 1),
PLUGIN_EXTENSION, sizeof(PLUGIN_EXTENSION) - 1) != 0) {
flb_error("[plugin] invalid plugin extension: %s", bname);
return NULL;
}
/* Get the expected structure name */
name = flb_malloc(len + (sizeof(PLUGIN_STRUCT_SUFFIX) - 1) + 1);
if (!name) {
flb_errno();
return NULL;
}
/* Name without prefix */
p = bname + (sizeof(PLUGIN_PREFIX) - 1);
o_len = len - (sizeof(PLUGIN_PREFIX) - 1) - (sizeof(PLUGIN_EXTENSION) - 1);
memcpy(name, p, o_len);
name[o_len] = '\0';
/* Validate expected plugin type */
if (is_input(name) == FLB_FALSE &&
is_filter(name) == FLB_FALSE &&
is_output(name) == FLB_FALSE) {
flb_error("[plugin] invalid plugin type: %s", name);
flb_free(name);
return NULL;
}
/* Append struct suffix */
p = name + o_len;
memcpy(p, PLUGIN_STRUCT_SUFFIX, sizeof(PLUGIN_STRUCT_SUFFIX) - 1);
o_len += sizeof(PLUGIN_STRUCT_SUFFIX) - 1;
name[o_len] = '\0';
return name;
}
static void destroy_plugin(struct flb_plugin *plugin)
{
flb_sds_destroy(plugin->path);
dlclose(plugin->dso_handle);
mk_list_del(&plugin->_head);
flb_free(plugin);
}
/* Creates the global plugin context for 'dynamic plugins' */
struct flb_plugins *flb_plugin_create()
{
struct flb_plugins *ctx;
ctx = flb_malloc(sizeof(struct flb_plugins));
if (!ctx) {
flb_errno();
return NULL;
}
mk_list_init(&ctx->input);
mk_list_init(&ctx->filter);
mk_list_init(&ctx->output);
return ctx;
}
int flb_plugin_load(char *path, struct flb_plugins *ctx,
struct flb_config *config)
{
int type = -1;
void *dso_handle;
void *symbol = NULL;
char *plugin_stname;
struct flb_plugin *plugin;
struct flb_input_plugin *input;
struct flb_filter_plugin *filter;
struct flb_output_plugin *output;
/* Open the shared object file: dlopen(3) */
dso_handle = get_handle(path);
if (!dso_handle) {
return -1;
}
/*
* Based on the shared object file name, compose the expected
* registration structure name.
*/
plugin_stname = path_to_plugin_name(path);
if (!plugin_stname) {
dlclose(dso_handle);
return -1;
}
/* Get the registration structure */
symbol = load_symbol(dso_handle, plugin_stname);
if (!symbol) {
flb_error("[plugin] cannot load plugin '%s', "
"registration structure is missing '%s'",
path, plugin_stname);
flb_free(plugin_stname);
dlclose(dso_handle);
return -1;
}
/* Detect plugin type and link it to the main context */
if (is_input(plugin_stname) == FLB_TRUE) {
type = FLB_PLUGIN_INPUT;
input = (struct flb_input_plugin *) symbol;
mk_list_add(&input->_head, &config->in_plugins);
}
else if (is_filter(plugin_stname) == FLB_TRUE) {
type = FLB_PLUGIN_FILTER;
filter = (struct flb_filter_plugin *) symbol;
mk_list_add(&filter->_head, &config->filter_plugins);
}
else if (is_output(plugin_stname) == FLB_TRUE) {
type = FLB_PLUGIN_OUTPUT;
output = (struct flb_output_plugin *) symbol;
mk_list_add(&output->_head, &config->out_plugins);
}
flb_free(plugin_stname);
if (type == -1) {
flb_error("[plugin] plugin type not defined on '%s'", path);
dlclose(dso_handle);
return -1;
}
/* Create plugin context (internal reference only) */
plugin = flb_malloc(sizeof(struct flb_plugin));
if (!plugin) {
flb_errno();
dlclose(dso_handle);
return -1;
}
plugin->type = type;
plugin->path = flb_sds_create(path);
plugin->dso_handle = dso_handle;
/* Link by type to the plugins parent context */
if (type == FLB_PLUGIN_INPUT) {
mk_list_add(&plugin->_head, &ctx->input);
}
else if (type == FLB_PLUGIN_FILTER) {
mk_list_add(&plugin->_head, &ctx->filter);
}
else if (type == FLB_PLUGIN_OUTPUT) {
mk_list_add(&plugin->_head, &ctx->output);
}
return 0;
}
int flb_plugin_load_router(char *path, struct flb_config *config)
{
int ret = -1;
char *bname;
bname = basename(path);
/* Is this a DSO C plugin ? */
if (strncmp(bname, PLUGIN_PREFIX, sizeof(PLUGIN_PREFIX) - 1) == 0) {
ret = flb_plugin_load(path, config->dso_plugins, config);
if (ret == -1) {
flb_error("[plugin] error loading DSO C plugin: %s", path);
return -1;
}
}
else {
#ifdef FLB_HAVE_PROXY_GO
if (flb_plugin_proxy_create(path, 0, config) == NULL) {
flb_error("[plugin] error loading proxy plugin: %s", path);
return -1;
}
#else
flb_error("[plugin] unsupported plugin type at: %s", path);
return -1;
#endif
}
return 0;
}
/* Load plugins from a configuration file */
int flb_plugin_load_config_file(const char *file, struct flb_config *config)
{
int ret;
char tmp[PATH_MAX + 1];
const char *cfg = NULL;
struct mk_rconf *fconf;
struct mk_rconf_section *section;
struct mk_rconf_entry *entry;
struct mk_list *head;
struct mk_list *head_e;
struct stat st;
#ifndef FLB_HAVE_STATIC_CONF
ret = stat(file, &st);
if (ret == -1 && errno == ENOENT) {
/* Try to resolve the real path (if exists) */
if (file[0] == '/') {
flb_utils_error(FLB_ERR_CFG_PLUGIN_FILE);
return -1;
}
if (config->conf_path) {
snprintf(tmp, PATH_MAX, "%s%s", config->conf_path, file);
cfg = tmp;
}
}
else {
cfg = file;
}
flb_debug("[plugin] opening configuration file %s", cfg);
fconf = mk_rconf_open(cfg);
#else
fconf = flb_config_static_open(file);
#endif
if (!fconf) {
return -1;
}
/* Read all [PLUGINS] sections */
mk_list_foreach(head, &fconf->sections) {
section = mk_list_entry(head, struct mk_rconf_section, _head);
if (strcasecmp(section->name, "PLUGINS") != 0) {
continue;
}
mk_list_foreach(head_e, §ion->entries) {
entry = mk_list_entry(head_e, struct mk_rconf_entry, _head);
if (strcmp(entry->key, "Path") != 0) {
continue;
}
/* Load plugin with router function */
ret = flb_plugin_load_router(entry->val, config);
if (ret == -1) {
mk_rconf_free(fconf);
return -1;
}
}
}
mk_rconf_free(fconf);
return 0;
}
/* Destroy plugin context */
void flb_plugin_destroy(struct flb_plugins *ctx)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_plugin *plugin;
mk_list_foreach_safe(head, tmp, &ctx->input) {
plugin = mk_list_entry(head, struct flb_plugin, _head);
destroy_plugin(plugin);
}
mk_list_foreach_safe(head, tmp, &ctx->filter) {
plugin = mk_list_entry(head, struct flb_plugin, _head);
destroy_plugin(plugin);
}
mk_list_foreach_safe(head, tmp, &ctx->output) {
plugin = mk_list_entry(head, struct flb_plugin, _head);
destroy_plugin(plugin);
}
flb_free(ctx);
}