25
25
#include <fluent-bit/flb_time.h>
26
26
#include <fluent-bit/flb_parser.h>
27
27
#include <fluent-bit/flb_error.h>
28
+ #include <fluent-bit/flb_utils.h>
28
29
29
30
#include <msgpack.h>
30
31
@@ -106,7 +107,7 @@ static int in_stdin_collect(struct flb_input_instance *ins,
106
107
107
108
bytes = read (ctx -> fd ,
108
109
ctx -> buf + ctx -> buf_len ,
109
- sizeof ( ctx -> buf ) - ctx -> buf_len - 1 );
110
+ ctx -> buf_size - ctx -> buf_len - 1 );
110
111
flb_plg_trace (ctx -> ins , "stdin read() = %i" , bytes );
111
112
112
113
if (bytes == 0 ) {
@@ -210,45 +211,91 @@ static int in_stdin_collect(struct flb_input_instance *ins,
210
211
return 0 ;
211
212
}
212
213
214
+ /* Read config file and*/
215
+ static int in_stdin_config_init (struct flb_in_stdin_config * ctx ,
216
+ struct flb_input_instance * in ,
217
+ struct flb_config * config )
218
+ {
219
+ const char * pval = NULL ;
220
+
221
+ /* parser settings */
222
+ pval = flb_input_get_property ("parser" , in );
223
+ if (pval ) {
224
+ ctx -> parser = flb_parser_get (pval , config );
225
+ if (!ctx -> parser ) {
226
+ flb_plg_error (ctx -> ins , "requested parser '%s' not found" , pval );
227
+ }
228
+ }
229
+ else {
230
+ ctx -> parser = NULL ;
231
+ }
232
+
233
+ /* buffer size setting */
234
+ pval = flb_input_get_property ("buffer_size" , in );
235
+ if (pval != NULL && flb_utils_size_to_bytes (pval ) > 0 ) {
236
+ ctx -> buf_size = flb_utils_size_to_bytes (pval );
237
+ }
238
+ else {
239
+ ctx -> buf_size = DEFAULT_BUF_SIZE ;
240
+ }
241
+
242
+ flb_plg_debug (ctx -> ins , "buf_size=%zu" , ctx -> buf_size );
243
+ return 0 ;
244
+ }
245
+
246
+ static void in_stdin_config_destroy (struct flb_in_stdin_config * ctx )
247
+ {
248
+ if (!ctx ) {
249
+ return ;
250
+ }
251
+
252
+ /* release buffer */
253
+ if (ctx -> buf ) {
254
+ flb_free (ctx -> buf );
255
+ }
256
+ flb_free (ctx );
257
+ }
258
+
213
259
/* Initialize plugin */
214
260
static int in_stdin_init (struct flb_input_instance * in ,
215
261
struct flb_config * config , void * data )
216
262
{
217
263
int fd ;
218
264
int ret ;
219
- const char * tmp ;
220
265
struct flb_in_stdin_config * ctx ;
221
266
(void ) data ;
222
267
223
- /* Allocate space for the configuration */
268
+ /* Allocate space for the configuration context */
224
269
ctx = flb_malloc (sizeof (struct flb_in_stdin_config ));
225
270
if (!ctx ) {
226
271
return -1 ;
227
272
}
273
+
274
+ ctx -> buf = NULL ;
228
275
ctx -> buf_len = 0 ;
229
276
ctx -> ins = in ;
230
277
278
+ /* Initialize stdin config */
279
+ ret = in_stdin_config_init (ctx , in , config );
280
+ if (ret < 0 ) {
281
+ goto init_error ;
282
+ }
283
+
284
+ ctx -> buf = flb_malloc (ctx -> buf_size );
285
+ if (!ctx -> buf ) {
286
+ flb_errno ();
287
+ goto init_error ;
288
+ }
289
+
231
290
/* Clone the standard input file descriptor */
232
291
fd = dup (STDIN_FILENO );
233
292
if (fd == -1 ) {
234
293
flb_errno ();
235
294
flb_plg_error (ctx -> ins , "Could not open standard input!" );
236
- flb_free (ctx );
237
- return -1 ;
295
+ goto init_error ;
238
296
}
239
297
ctx -> fd = fd ;
240
298
241
- tmp = flb_input_get_property ("parser" , in );
242
- if (tmp ) {
243
- ctx -> parser = flb_parser_get (tmp , config );
244
- if (!ctx -> parser ) {
245
- flb_plg_error (ctx -> ins , "requested parser '%s' not found" , tmp );
246
- }
247
- }
248
- else {
249
- ctx -> parser = NULL ;
250
- }
251
-
252
299
/* Always initialize built-in JSON pack state */
253
300
flb_pack_state_init (& ctx -> pack_state );
254
301
ctx -> pack_state .multiple = FLB_TRUE ;
@@ -263,12 +310,16 @@ static int in_stdin_init(struct flb_input_instance *in,
263
310
config );
264
311
if (ret == -1 ) {
265
312
flb_plg_error (ctx -> ins , "Could not set collector for STDIN input plugin" );
266
- flb_free (ctx );
267
- return -1 ;
313
+ goto init_error ;
268
314
}
269
315
ctx -> coll_fd = ret ;
270
316
271
317
return 0 ;
318
+
319
+ init_error :
320
+ in_stdin_config_destroy (ctx );
321
+
322
+ return -1 ;
272
323
}
273
324
274
325
/* Cleanup serial input */
@@ -284,7 +335,7 @@ static int in_stdin_exit(void *in_context, struct flb_config *config)
284
335
close (ctx -> fd );
285
336
}
286
337
flb_pack_state_reset (& ctx -> pack_state );
287
- flb_free (ctx );
338
+ in_stdin_config_destroy (ctx );
288
339
289
340
return 0 ;
290
341
}
0 commit comments