Skip to content

Commit 403a863

Browse files
Yuichiro NAITOyuichiro-naito
authored andcommitted
core: Re-create the event loop after daemonizing on BSD platforms.
Because kqueue(2) is not inherited by the child process. Signed-off-by: Yuichiro NAITO <naito.yuichiro@gmail.com>
1 parent 0717103 commit 403a863

File tree

3 files changed

+92
-26
lines changed

3 files changed

+92
-26
lines changed

include/fluent-bit/flb_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ typedef struct flb_lib_ctx flb_ctx_t;
4949
struct flb_processor;
5050

5151
FLB_EXPORT void flb_init_env();
52+
FLB_EXPORT int flb_create_event_loop(flb_ctx_t *ctx);
53+
FLB_EXPORT int flb_destroy_event_loop(flb_ctx_t *ctx);
5254
FLB_EXPORT flb_ctx_t *flb_create();
5355
FLB_EXPORT void flb_destroy(flb_ctx_t *ctx);
5456
FLB_EXPORT int flb_input(flb_ctx_t *ctx, const char *input, void *data);

src/flb_lib.c

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,83 @@ void flb_init_env()
140140
cmt_initialize();
141141
}
142142

143+
int flb_create_event_loop(flb_ctx_t *ctx)
144+
{
145+
int ret;
146+
struct flb_config *config;
147+
148+
if (ctx == NULL)
149+
return FLB_LIB_ERROR;
150+
151+
config = ctx->config;
152+
153+
/* Create the event loop to receive notifications */
154+
ctx->event_loop = mk_event_loop_create(256);
155+
if (!ctx->event_loop)
156+
return FLB_LIB_ERROR;
157+
158+
config->ch_evl = ctx->event_loop;
159+
160+
/* Prepare the notification channels */
161+
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
162+
if (!ctx->event_channel) {
163+
perror("calloc");
164+
goto error_1;
165+
}
166+
167+
MK_EVENT_ZERO(ctx->event_channel);
168+
169+
ret = mk_event_channel_create(config->ch_evl,
170+
&config->ch_notif[0],
171+
&config->ch_notif[1],
172+
ctx->event_channel);
173+
if (ret != 0) {
174+
flb_error("[lib] could not create notification channels");
175+
goto error_2;
176+
}
177+
178+
return 0;
179+
180+
error_2:
181+
flb_free(ctx->event_channel);
182+
ctx->event_channel = NULL;
183+
error_1:
184+
mk_event_loop_destroy(ctx->event_loop);
185+
ctx->event_loop = NULL;
186+
return FLB_LIB_ERROR;
187+
}
188+
189+
int flb_destroy_event_loop(flb_ctx_t *ctx)
190+
{
191+
int ret;
192+
struct flb_config *config;
193+
194+
if (ctx == NULL || ctx->config == NULL)
195+
return 0;
196+
197+
config = ctx->config;
198+
if (ctx->event_channel != NULL) {
199+
ret = mk_event_channel_destroy(config->ch_evl,
200+
config->ch_notif[0],
201+
config->ch_notif[1],
202+
ctx->event_channel);
203+
if (ret != 0) {
204+
/* make sure to close file descriptors */
205+
close(config->ch_notif[0]);
206+
close(config->ch_notif[1]);
207+
}
208+
free(ctx->event_channel);
209+
ctx->event_channel = NULL;
210+
}
211+
212+
if (ctx->event_loop != NULL) {
213+
mk_event_loop_destroy(ctx->event_loop);
214+
ctx->event_loop = NULL;
215+
}
216+
217+
return 0;
218+
}
219+
143220
flb_ctx_t *flb_create()
144221
{
145222
int ret;
@@ -184,37 +261,13 @@ flb_ctx_t *flb_create()
184261
return NULL;
185262
}
186263

187-
/* Create the event loop to receive notifications */
188-
ctx->event_loop = mk_event_loop_create(256);
189-
if (!ctx->event_loop) {
190-
flb_config_exit(ctx->config);
191-
flb_free(ctx);
192-
return NULL;
193-
}
194-
config->ch_evl = ctx->event_loop;
195-
196-
/* Prepare the notification channels */
197-
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
198-
if (!ctx->event_channel) {
199-
perror("calloc");
264+
ret = flb_create_event_loop(ctx);
265+
if (ret != 0) {
200266
flb_config_exit(ctx->config);
201267
flb_free(ctx);
202268
return NULL;
203269
}
204270

205-
MK_EVENT_ZERO(ctx->event_channel);
206-
207-
ret = mk_event_channel_create(config->ch_evl,
208-
&config->ch_notif[0],
209-
&config->ch_notif[1],
210-
ctx->event_channel);
211-
if (ret != 0) {
212-
flb_error("[lib] could not create notification channels");
213-
flb_stop(ctx);
214-
flb_destroy(ctx);
215-
return NULL;
216-
}
217-
218271
#ifdef FLB_HAVE_AWS_ERROR_REPORTER
219272
if (is_error_reporting_enabled()) {
220273
error_reporter = flb_aws_error_reporter_create();

src/fluent-bit.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,18 @@ static int flb_main_run(int argc, char **argv)
14151415
#ifdef FLB_HAVE_FORK
14161416
/* Run in background/daemon mode */
14171417
if (config->daemon == FLB_TRUE) {
1418+
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
1419+
defined(__OpenBSD__) || defined(__DragonFly__)
1420+
flb_destroy_event_loop(ctx);
1421+
#endif
14181422
flb_utils_set_daemon(config);
1423+
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
1424+
defined(__OpenBSD__) || defined(__DragonFly__)
1425+
if (flb_create_event_loop(ctx) != 0) {
1426+
flb_error("[daemon] failed to recreate event loop after daemonizing");
1427+
flb_utils_error(FLB_ERR_CFG_FLUSH);
1428+
}
1429+
#endif
14191430
}
14201431
#endif
14211432

0 commit comments

Comments
 (0)