summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_lib/in_lib.c
blob: 466f1afe87618dbbfe226e29c90a6d01e6f10a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_log_event_decoder.h>
#include "in_lib.h"

static int in_lib_collect(struct flb_input_instance *ins,
                          struct flb_config *config, void *in_context)
{
    int ret;
    int dec_ret;
    int enc_ret;
    int bytes;
    int out_size;
    int capacity;
    int size;
    char *ptr;
    char *pack;
    struct flb_log_event record;
    struct flb_log_event_decoder decoder;
    struct flb_in_lib_config *ctx = in_context;

    capacity = (ctx->buf_size - ctx->buf_len);

    /* Allocate memory as required (FIXME: this will be limited in later) */
    if (capacity == 0) {
        size = ctx->buf_size + LIB_BUF_CHUNK;
        ptr = flb_realloc(ctx->buf_data, size);
        if (!ptr) {
            flb_errno();
            return -1;
        }
        ctx->buf_data = ptr;
        ctx->buf_size = size;
        capacity = LIB_BUF_CHUNK;
    }

    bytes = flb_pipe_r(ctx->fd,
                       ctx->buf_data + ctx->buf_len,
                       capacity);
    flb_plg_trace(ctx->ins, "in_lib read() = %i", bytes);
    if (bytes == -1) {
        perror("read");
        if (errno == -EPIPE) {
            return -1;
        }
        return 0;
    }
    ctx->buf_len += bytes;

    /* initially we should support json input */
    ret = flb_pack_json_state(ctx->buf_data, ctx->buf_len,
                              &pack, &out_size, &ctx->state);
    if (ret == FLB_ERR_JSON_PART) {
        flb_plg_warn(ctx->ins, "lib data incomplete, waiting for more data...");
        return 0;
    }
    else if (ret == FLB_ERR_JSON_INVAL) {
        flb_plg_warn(ctx->ins, "lib data invalid");
        flb_pack_state_reset(&ctx->state);
        flb_pack_state_init(&ctx->state);
        return -1;
    }
    ctx->buf_len = 0;

    dec_ret = flb_log_event_decoder_init(&decoder, pack, out_size);
    if (dec_ret != FLB_EVENT_DECODER_SUCCESS) {
        flb_plg_error(ctx->ins,
                      "Log event decoder initialization error : %s",
                      flb_log_event_decoder_get_error_description(dec_ret));
        flb_free(pack);
        flb_pack_state_reset(&ctx->state);
        flb_pack_state_init(&ctx->state);
        return -1;
    }

    while ((dec_ret = flb_log_event_decoder_next(
                      &decoder,
                      &record)) == FLB_EVENT_DECODER_SUCCESS) {
        enc_ret = flb_log_event_encoder_begin_record(&ctx->log_encoder);
        if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
            flb_plg_error(ctx->ins,
                          "flb_log_event_encoder_begin_record error : %s",
                          flb_log_event_encoder_get_error_description(enc_ret));
            flb_log_event_encoder_rollback_record(&ctx->log_encoder);
            continue;
        }

        enc_ret = flb_log_event_encoder_set_timestamp(
                  &ctx->log_encoder,
                  &record.timestamp);
        if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
            flb_plg_error(ctx->ins,
                          "flb_log_event_encoder_set_timestamp error : %s",
                          flb_log_event_encoder_get_error_description(enc_ret));
            flb_log_event_encoder_rollback_record(&ctx->log_encoder);
            continue;
        }

        enc_ret = flb_log_event_encoder_set_metadata_from_msgpack_object(
                  &ctx->log_encoder,
                  record.metadata);
        if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
            flb_plg_error(ctx->ins,
                          "flb_log_event_encoder_set_metadata_from_msgpack_object error : %s",
                          flb_log_event_encoder_get_error_description(enc_ret));
            flb_log_event_encoder_rollback_record(&ctx->log_encoder);
            continue;
        }

        enc_ret = flb_log_event_encoder_set_body_from_msgpack_object(
                  &ctx->log_encoder,
                  record.body);
        if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
            flb_plg_error(ctx->ins,
                          "flb_log_event_encoder_set_body_from_msgpack_object error : %s",
                          flb_log_event_encoder_get_error_description(enc_ret));
            flb_log_event_encoder_rollback_record(&ctx->log_encoder);
            continue;
        }

        enc_ret = flb_log_event_encoder_commit_record(&ctx->log_encoder);
        if (enc_ret != FLB_EVENT_ENCODER_SUCCESS) {
            flb_plg_error(ctx->ins,
                          "flb_log_event_encoder_commit_record error : %s",
                          flb_log_event_encoder_get_error_description(enc_ret));
            flb_log_event_encoder_rollback_record(&ctx->log_encoder);
            continue;
        }
    }

    dec_ret = flb_log_event_decoder_get_last_result(&decoder);
    if (dec_ret == FLB_EVENT_DECODER_SUCCESS) {
        flb_input_log_append(ctx->ins, NULL, 0,
                             ctx->log_encoder.output_buffer,
                             ctx->log_encoder.output_length);

        ret = 0;
    }
    else {
        flb_plg_error(ctx->ins,
                      "flb_log_event_decoder_get_last_result error : %s",
                      flb_log_event_decoder_get_error_description(dec_ret));
        ret = -1;
    }

    flb_log_event_encoder_reset(&ctx->log_encoder);
    flb_log_event_decoder_destroy(&decoder);

    /* Reset the state */
    flb_free(pack);

    flb_pack_state_reset(&ctx->state);
    flb_pack_state_init(&ctx->state);

    return ret;
}

/* Initialize plugin */
static int in_lib_init(struct flb_input_instance *in,
                       struct flb_config *config, void *data)
{
    int ret;
    struct flb_in_lib_config *ctx;
    (void) data;

    /* Allocate space for the configuration */
    ctx = flb_malloc(sizeof(struct flb_in_lib_config));
    if (!ctx) {
        return -1;
    }
    ctx->ins = in;

    /* Buffer for incoming data */
    ctx->buf_size = LIB_BUF_CHUNK;
    ctx->buf_data = flb_calloc(1, LIB_BUF_CHUNK);
    ctx->buf_len = 0;

    if (!ctx->buf_data) {
        flb_errno();
        flb_plg_error(ctx->ins, "Could not allocate initial buf memory buffer");
        flb_free(ctx);
        return -1;
    }

    /* Init communication channel */
    flb_input_channel_init(in);
    ctx->fd = in->channel[0];

    /* Set the context */
    flb_input_set_context(in, ctx);

    /* Collect upon data available on the standard input */
    ret = flb_input_set_collector_event(in,
                                        in_lib_collect,
                                        ctx->fd,
                                        config);
    if (ret == -1) {
        flb_plg_error(ctx->ins, "Could not set collector for LIB input plugin");
        flb_free(ctx->buf_data);
        flb_free(ctx);
        return -1;
    }

    ret = flb_log_event_encoder_init(&ctx->log_encoder,
                                     FLB_LOG_EVENT_FORMAT_DEFAULT);

    if (ret != FLB_EVENT_ENCODER_SUCCESS) {
        flb_plg_error(ctx->ins, "error initializing event encoder : %d", ret);

        flb_free(ctx->buf_data);
        flb_free(ctx);

        return -1;
    }

    flb_pack_state_init(&ctx->state);

    return 0;
}

static int in_lib_exit(void *data, struct flb_config *config)
{
    struct flb_in_lib_config *ctx = data;
    struct flb_pack_state *s;

    (void) config;

    flb_log_event_encoder_destroy(&ctx->log_encoder);

    if (ctx->buf_data) {
        flb_free(ctx->buf_data);
    }

    s = &ctx->state;
    flb_pack_state_reset(s);
    flb_free(ctx);
    return 0;
}

/* Plugin reference */
struct flb_input_plugin in_lib_plugin = {
    .name         = "lib",
    .description  = "Library mode Input",
    .cb_init      = in_lib_init,
    .cb_pre_run   = NULL,
    .cb_collect   = NULL,
    .cb_ingest    = NULL,
    .cb_flush_buf = NULL,
    .cb_exit      = in_lib_exit
};