Skip to content

Commit 4d12928

Browse files
Yuichiro NAITOyuichiro-naito
authored andcommitted
Re-create the event loop after daemonizing on BSD platforms.
Because kqueue(2) is not inherited by the child process.
1 parent 73d20d0 commit 4d12928

File tree

3 files changed

+77
-29
lines changed

3 files changed

+77
-29
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: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,70 @@ 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 = ctx->config;
147+
148+
/* Create the event loop to receive notifications */
149+
ctx->event_loop = mk_event_loop_create(256);
150+
if (!ctx->event_loop) {
151+
flb_config_exit(ctx->config);
152+
flb_free(ctx);
153+
return FLB_LIB_ERROR;
154+
}
155+
config->ch_evl = ctx->event_loop;
156+
157+
/* Prepare the notification channels */
158+
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
159+
if (!ctx->event_channel) {
160+
perror("calloc");
161+
flb_config_exit(ctx->config);
162+
flb_free(ctx);
163+
return FLB_LIB_ERROR;
164+
}
165+
166+
MK_EVENT_ZERO(ctx->event_channel);
167+
168+
ret = mk_event_channel_create(config->ch_evl,
169+
&config->ch_notif[0],
170+
&config->ch_notif[1],
171+
ctx->event_channel);
172+
if (ret != 0) {
173+
flb_error("[lib] could not create notification channels");
174+
flb_stop(ctx);
175+
flb_destroy(ctx);
176+
return FLB_LIB_ERROR;
177+
}
178+
179+
return 0;
180+
}
181+
182+
int flb_destroy_event_loop(flb_ctx_t *ctx)
183+
{
184+
int ret;
185+
struct flb_config *config;
186+
187+
if (ctx == NULL)
188+
return 0;
189+
190+
config = ctx->config;
191+
ret = mk_event_channel_destroy(config->ch_evl,
192+
config->ch_notif[0],
193+
config->ch_notif[1],
194+
ctx->event_channel);
195+
if (ret != 0) {
196+
/* make sure to close file descriptors */
197+
close(config->ch_notif[0]);
198+
close(config->ch_notif[1]);
199+
}
200+
201+
free(ctx->event_channel);
202+
203+
mk_event_loop_destroy(ctx->event_loop);
204+
return 0;
205+
}
206+
143207
flb_ctx_t *flb_create()
144208
{
145209
int ret;
@@ -184,36 +248,9 @@ flb_ctx_t *flb_create()
184248
return NULL;
185249
}
186250

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");
200-
flb_config_exit(ctx->config);
201-
flb_free(ctx);
202-
return NULL;
203-
}
204-
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);
251+
ret = flb_create_event_loop(ctx);
252+
if (ret != 0)
215253
return NULL;
216-
}
217254

218255
#ifdef FLB_HAVE_AWS_ERROR_REPORTER
219256
if (is_error_reporting_enabled()) {

src/fluent-bit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,16 @@ 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_utils_error(FLB_ERR_CFG_FLUSH);
1427+
#endif
14191428
}
14201429
#endif
14211430

0 commit comments

Comments
 (0)