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_luajit.h>
25
flb_luajit_create(struct flb_config * config)26 struct flb_luajit *flb_luajit_create(struct flb_config *config)
27 {
28 struct flb_luajit *lj;
29
30 lj = flb_malloc(sizeof(struct flb_luajit));
31 if (!lj) {
32 flb_errno();
33 return NULL;
34 }
35
36 lj->state = luaL_newstate();
37 if (!lj->state) {
38 flb_error("[luajit] error creating new context");
39 flb_free(lj);
40 return NULL;
41 }
42 luaL_openlibs(lj->state);
43 lj->config = config;
44 mk_list_add(&lj->_head, &config->luajit_list);
45
46 return lj;
47 }
48
flb_luajit_load_script(struct flb_luajit * lj,char * script)49 int flb_luajit_load_script(struct flb_luajit *lj, char *script)
50 {
51 int ret;
52
53 ret = luaL_loadfile(lj->state, script);
54 if (ret != 0) {
55 flb_error("[luajit] error loading script: %s",
56 lua_tostring(lj->state, -1));
57 return -1;
58 }
59
60 return 0;
61 }
62
flb_luajit_destroy(struct flb_luajit * lj)63 void flb_luajit_destroy(struct flb_luajit *lj)
64 {
65 lua_close(lj->state);
66 mk_list_del(&lj->_head);
67 flb_free(lj);
68 }
69
flb_luajit_destroy_all(struct flb_config * ctx)70 int flb_luajit_destroy_all(struct flb_config *ctx)
71 {
72 int c = 0;
73 struct mk_list *tmp;
74 struct mk_list *head;
75 struct flb_luajit *lj;
76
77 mk_list_foreach_safe(head, tmp, &ctx->luajit_list) {
78 lj = mk_list_entry(head, struct flb_luajit, _head);
79 flb_luajit_destroy(lj);
80 c++;
81 }
82
83 return c;
84 }
85