@@ -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+
143207flb_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 ()) {
0 commit comments