summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/flb_callback.c
blob: 760604ce7cb6d149c31dab36a0dcd00491bbb252 (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
/* -*- 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 <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_callback.h>

struct flb_callback *flb_callback_create(char *name)
{
    struct flb_callback *ctx;

    /* Create context */
    ctx = flb_malloc(sizeof(struct flb_callback));
    if (!ctx) {
        flb_errno();
        return NULL;
    }

    ctx->ht = flb_hash_table_create(FLB_HASH_TABLE_EVICT_NONE, 16, 0);
    if (!ctx->ht) {
        flb_error("[callback] error allocating hash table");
        flb_free(ctx);
        return NULL;
    }
    mk_list_init(&ctx->entries);

    return ctx;
}

int flb_callback_set(struct flb_callback *ctx, char *name,
                     void (*cb)(char *, void *, void *))
{
    int ret;
    int len;
    struct flb_callback_entry *entry;

    entry = flb_malloc(sizeof(struct flb_callback_entry));
    if (!entry) {
        flb_errno();
        return -1;
    }
    entry->name = flb_sds_create(name);
    if (!entry->name) {
        flb_free(entry);
        return -1;
    }
    entry->cb = cb;

    len = strlen(name);
    ret = flb_hash_table_add(ctx->ht, name, len,
                             (char *) &entry, sizeof(struct flb_callback_entry *));
    if (ret == -1) {
        flb_sds_destroy(entry->name);
        flb_free(entry);
        return -1;
    }
    mk_list_add(&entry->_head, &ctx->entries);

    return ret;
}

int flb_callback_exists(struct flb_callback *ctx, char *name)
{
    int ret;
    int len;
    size_t out_size;
    void *cb_addr;

    len = strlen(name);
    ret = flb_hash_table_get(ctx->ht, name, len, &cb_addr, &out_size);
    if (ret == -1) {
        return FLB_FALSE;
    }

    return FLB_TRUE;
}

int flb_callback_do(struct flb_callback *ctx, char *name, void *p1, void *p2)
{
    int ret;
    int len;
    size_t out_size;
    void *cb_addr;
    struct flb_callback_entry *entry;

    if (!ctx) {
        return -1;
    }

    len = strlen(name);
    ret = flb_hash_table_get(ctx->ht, name, len, &cb_addr, &out_size);
    if (ret == -1) {
        return -1;
    }

    memcpy(&entry, cb_addr, sizeof(struct flb_callback_entry *));
    entry->cb(entry->name, p1, p2);
    return 0;
}

void flb_callback_destroy(struct flb_callback *ctx)
{
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_callback_entry *entry;

    flb_hash_table_destroy(ctx->ht);

    mk_list_foreach_safe(head, tmp, &ctx->entries) {
        entry = mk_list_entry(head, struct flb_callback_entry, _head);
        mk_list_del(&entry->_head);
        flb_sds_destroy(entry->name);
        flb_free(entry);
    }

    flb_free(ctx);
}