Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/fluent-bit/flb_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define FLB_ERR_CFG_FLUSH 20
#define FLB_ERR_CFG_FLUSH_CREATE 21
#define FLB_ERR_CFG_FLUSH_REGISTER 22
#define FLB_ERR_EVENT_LOOP_CREATE 30
#define FLB_ERR_CUSTOM_INVALID 49
#define FLB_ERR_INPUT_INVALID 50
#define FLB_ERR_INPUT_UNDEF 51
Expand Down
2 changes: 2 additions & 0 deletions include/fluent-bit/flb_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ typedef struct flb_lib_ctx flb_ctx_t;
struct flb_processor;

FLB_EXPORT void flb_init_env();
FLB_EXPORT int flb_create_event_loop(flb_ctx_t *ctx);
FLB_EXPORT int flb_destroy_event_loop(flb_ctx_t *ctx);
FLB_EXPORT flb_ctx_t *flb_create();
FLB_EXPORT void flb_destroy(flb_ctx_t *ctx);
FLB_EXPORT int flb_input(flb_ctx_t *ctx, const char *input, void *data);
Expand Down
105 changes: 79 additions & 26 deletions src/flb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,83 @@ void flb_init_env()
cmt_initialize();
}

int flb_create_event_loop(flb_ctx_t *ctx)
{
int ret;
struct flb_config *config;

if (ctx == NULL)
return FLB_LIB_ERROR;

config = ctx->config;

/* Create the event loop to receive notifications */
ctx->event_loop = mk_event_loop_create(256);
if (!ctx->event_loop)
return FLB_LIB_ERROR;

config->ch_evl = ctx->event_loop;

/* Prepare the notification channels */
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
if (!ctx->event_channel) {
perror("calloc");
goto error_1;
}

MK_EVENT_ZERO(ctx->event_channel);

ret = mk_event_channel_create(config->ch_evl,
&config->ch_notif[0],
&config->ch_notif[1],
ctx->event_channel);
if (ret != 0) {
flb_error("[lib] could not create notification channels");
goto error_2;
}

return 0;

error_2:
flb_free(ctx->event_channel);
ctx->event_channel = NULL;
error_1:
mk_event_loop_destroy(ctx->event_loop);
ctx->event_loop = NULL;
return FLB_LIB_ERROR;
}

int flb_destroy_event_loop(flb_ctx_t *ctx)
{
int ret;
struct flb_config *config;

if (ctx == NULL || ctx->config == NULL)
return 0;

config = ctx->config;
if (ctx->event_channel != NULL) {
ret = mk_event_channel_destroy(config->ch_evl,
config->ch_notif[0],
config->ch_notif[1],
ctx->event_channel);
if (ret != 0) {
/* make sure to close file descriptors */
close(config->ch_notif[0]);
close(config->ch_notif[1]);
}
flb_free(ctx->event_channel);
ctx->event_channel = NULL;
}

if (ctx->event_loop != NULL) {
mk_event_loop_destroy(ctx->event_loop);
ctx->event_loop = NULL;
}

return 0;
}

flb_ctx_t *flb_create()
{
int ret;
Expand Down Expand Up @@ -184,37 +261,13 @@ flb_ctx_t *flb_create()
return NULL;
}

/* Create the event loop to receive notifications */
ctx->event_loop = mk_event_loop_create(256);
if (!ctx->event_loop) {
flb_config_exit(ctx->config);
flb_free(ctx);
return NULL;
}
config->ch_evl = ctx->event_loop;

/* Prepare the notification channels */
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
if (!ctx->event_channel) {
perror("calloc");
ret = flb_create_event_loop(ctx);
if (ret != 0) {
flb_config_exit(ctx->config);
flb_free(ctx);
return NULL;
}

MK_EVENT_ZERO(ctx->event_channel);

ret = mk_event_channel_create(config->ch_evl,
&config->ch_notif[0],
&config->ch_notif[1],
ctx->event_channel);
if (ret != 0) {
flb_error("[lib] could not create notification channels");
flb_stop(ctx);
flb_destroy(ctx);
return NULL;
}

#ifdef FLB_HAVE_AWS_ERROR_REPORTER
if (is_error_reporting_enabled()) {
error_reporter = flb_aws_error_reporter_create();
Expand Down
3 changes: 3 additions & 0 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ void flb_utils_error(int err)
case FLB_ERR_CFG_FLUSH_REGISTER:
msg = "could not register timer for flushing";
break;
case FLB_ERR_EVENT_LOOP_CREATE:
msg = "could not create event loop";
break;
case FLB_ERR_INPUT_INVALID:
msg = "invalid input type";
break;
Expand Down
11 changes: 11 additions & 0 deletions src/fluent-bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,18 @@ static int flb_main_run(int argc, char **argv)
#ifdef FLB_HAVE_FORK
/* Run in background/daemon mode */
if (config->daemon == FLB_TRUE) {
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__)
flb_destroy_event_loop(ctx);
#endif
flb_utils_set_daemon(config);
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__)
if (flb_create_event_loop(ctx) != 0) {
flb_error("[daemon] failed to recreate event loop after daemonizing");
flb_utils_error(FLB_ERR_EVENT_LOOP_CREATE);
}
#endif
}
#endif

Expand Down
Loading