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