1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #include <fluent-bit/flb_info.h>
22 #include <fluent-bit/flb_mem.h>
23 #include <fluent-bit/flb_log.h>
24 #include <fluent-bit/flb_callback.h>
25 
flb_callback_create(char * name)26 struct flb_callback *flb_callback_create(char *name)
27 {
28     struct flb_callback *ctx;
29 
30     /* Create context */
31     ctx = flb_malloc(sizeof(struct flb_callback));
32     if (!ctx) {
33         flb_errno();
34         return NULL;
35     }
36 
37     ctx->ht = flb_hash_create(FLB_HASH_EVICT_NONE, 16, 0);
38     if (!ctx->ht) {
39         flb_error("[callback] error allocating hash table");
40         flb_free(ctx);
41         return NULL;
42     }
43     mk_list_init(&ctx->entries);
44 
45     return ctx;
46 }
47 
flb_callback_set(struct flb_callback * ctx,char * name,void (* cb)(char *,void *,void *))48 int flb_callback_set(struct flb_callback *ctx, char *name,
49                      void (*cb)(char *, void *, void *))
50 {
51     int ret;
52     int len;
53     struct flb_callback_entry *entry;
54 
55     entry = flb_malloc(sizeof(struct flb_callback_entry));
56     if (!entry) {
57         flb_errno();
58         return -1;
59     }
60     entry->name = flb_sds_create(name);
61     if (!entry->name) {
62         flb_free(entry);
63         return -1;
64     }
65     entry->cb = cb;
66 
67     len = strlen(name);
68     ret = flb_hash_add(ctx->ht, name, len,
69                        (char *) &entry, sizeof(struct flb_callback_entry *));
70     if (ret == -1) {
71         flb_sds_destroy(entry->name);
72         flb_free(entry);
73         return -1;
74     }
75     mk_list_add(&entry->_head, &ctx->entries);
76 
77     return ret;
78 }
79 
flb_callback_exists(struct flb_callback * ctx,char * name)80 int flb_callback_exists(struct flb_callback *ctx, char *name)
81 {
82     int ret;
83     int len;
84     size_t out_size;
85     void *cb_addr;
86 
87     len = strlen(name);
88     ret = flb_hash_get(ctx->ht, name, len, &cb_addr, &out_size);
89     if (ret == -1) {
90         return FLB_FALSE;
91     }
92 
93     return FLB_TRUE;
94 }
95 
flb_callback_do(struct flb_callback * ctx,char * name,void * p1,void * p2)96 int flb_callback_do(struct flb_callback *ctx, char *name, void *p1, void *p2)
97 {
98     int ret;
99     int len;
100     size_t out_size;
101     void *cb_addr;
102     struct flb_callback_entry *entry;
103 
104     if (!ctx) {
105         return -1;
106     }
107 
108     len = strlen(name);
109     ret = flb_hash_get(ctx->ht, name, len, &cb_addr, &out_size);
110     if (ret == -1) {
111         return -1;
112     }
113 
114     memcpy(&entry, cb_addr, sizeof(struct flb_callback_entry *));
115     entry->cb(entry->name, p1, p2);
116     return 0;
117 }
118 
flb_callback_destroy(struct flb_callback * ctx)119 void flb_callback_destroy(struct flb_callback *ctx)
120 {
121     struct mk_list *tmp;
122     struct mk_list *head;
123     struct flb_callback_entry *entry;
124 
125     flb_hash_destroy(ctx->ht);
126 
127     mk_list_foreach_safe(head, tmp, &ctx->entries) {
128         entry = mk_list_entry(head, struct flb_callback_entry, _head);
129         mk_list_del(&entry->_head);
130         flb_sds_destroy(entry->name);
131         flb_free(entry);
132     }
133 
134     flb_free(ctx);
135 }
136