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