]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/luabackend/reload.cc
Logging: have a global g_log
[thirdparty/pdns.git] / modules / luabackend / reload.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 * originally authored by Fredrik Danerklint
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * In addition, for the avoidance of any doubt, permission is granted to
11 * link this program with OpenSSL and to (re)distribute the binaries
12 * produced as the result of such linking.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "luabackend.hh"
27
28 #include "pdns/logger.hh"
29 #include "pdns/arguments.hh"
30
31 #include <iostream>
32 #include <sstream>
33 using namespace std;
34
35 #include "lua_functions.hh"
36
37 /*
38 virtual void reload();
39 virtual void rediscover(string* status=0);
40 */
41
42 void LUABackend::get_lua_function(lua_State *lua, const char *name, int *function) {
43 *function = 0;
44
45 string f = "f_";
46 f.append(name);
47
48 string arg = "";
49 if (!::arg().isEmpty(f))
50 arg = getArg(f);
51
52 lua_getglobal(lua, arg == "" ? name : arg.c_str());
53 if (!lua_isnil(lua, -1)) {
54 lua_pushvalue(lua, -1);
55 *function = luaL_ref(lua, LUA_REGISTRYINDEX);
56 }
57 }
58
59
60 void LUABackend::reload() {
61
62 backend_name.clear();
63
64 // backend_name = "[LUABackend: " + uitoa(backend_pid) + " (" + uitoa(backend_count) +")] ";
65 backend_name = "[LUABackend: (" + uitoa(backend_count) +")] ";
66
67 if (lua)
68 lua_close(lua);
69
70 logging = ::arg().mustDo("query-logging") || mustDo("logging-query");
71
72 #if LUA_VERSION_NUM >= 502
73 lua = luaL_newstate();
74 #else
75 lua = lua_open();
76 #endif
77
78 if (lua != NULL) {
79 lua_atpanic(lua, my_lua_panic);
80
81 string filename = getArg("filename"); //"powerdns-luabackend.lua";
82
83 if (luaL_loadfile (lua, filename.c_str()) != 0) {
84 stringstream e;
85 e << backend_name << "Error loading the file '" << filename << "' : " << lua_tostring(lua,-1) << endl;
86
87 lua_pop(lua, 1);
88 throw LUAException (e.str());
89 } else {
90
91 lua_pushlightuserdata(lua, (void*)this);
92 lua_setfield(lua, LUA_REGISTRYINDEX, "__LUABACKEND");
93
94 register_lua_functions(lua);
95
96 if(lua_pcall(lua, 0, 0, 0)) {
97 stringstream e;
98 e << backend_name << "Error running the file '" << filename << "' : " << lua_tostring(lua,-1) << endl;
99
100 lua_pop(lua, 1);
101 throw LUAException (e.str());
102
103 } else {
104 get_lua_function(lua, "exec_error", &f_lua_exec_error);
105
106 //minimal functions....
107 get_lua_function(lua, "list", &f_lua_list);
108 get_lua_function(lua, "lookup", &f_lua_lookup);
109 get_lua_function(lua, "get", &f_lua_get);
110 get_lua_function(lua, "getsoa", &f_lua_getsoa);
111
112 if (f_lua_list == 0 || f_lua_lookup == 0 || f_lua_get == 0 || f_lua_getsoa == 0) {
113 throw LUAException (backend_name + "MINIMAL BACKEND: Missing required function(s)!");
114 }
115
116 //master functions....
117 get_lua_function(lua, "getupdatedmasters", &f_lua_getupdatedmasters);
118 get_lua_function(lua, "setnotified", &f_lua_setnotified);
119
120 //slave functions....
121 get_lua_function(lua, "getdomaininfo", &f_lua_getdomaininfo);
122 get_lua_function(lua, "ismaster", &f_lua_ismaster);
123 get_lua_function(lua, "getunfreshslaveinfos", &f_lua_getunfreshslaveinfos);
124 get_lua_function(lua, "setfresh", &f_lua_setfresh);
125 get_lua_function(lua, "starttransaction", &f_lua_starttransaction);
126 get_lua_function(lua, "committransaction", &f_lua_committransaction);
127 get_lua_function(lua, "aborttransaction", &f_lua_aborttransaction);
128 get_lua_function(lua, "feedrecord", &f_lua_feedrecord);
129
130 //supermaster functions....
131 get_lua_function(lua, "supermasterbackend", &f_lua_supermasterbackend);
132 get_lua_function(lua, "createslavedomain", &f_lua_createslavedomain);
133
134 //rediscover
135 get_lua_function(lua, "rediscover", &f_lua_rediscover);
136
137 //dnssec
138 get_lua_function(lua, "alsonotifies", &f_lua_alsonotifies);
139 get_lua_function(lua, "getdomainmetadata", &f_lua_getdomainmetadata);
140 get_lua_function(lua, "setdomainmetadata", &f_lua_setdomainmetadata);
141
142 get_lua_function(lua, "getdomainkeys", &f_lua_getdomainkeys);
143 get_lua_function(lua, "removedomainkey", &f_lua_removedomainkey);
144 get_lua_function(lua, "activatedomainkey", &f_lua_activatedomainkey);
145 get_lua_function(lua, "deactivatedomainkey", &f_lua_deactivatedomainkey);
146 get_lua_function(lua, "updatedomainkey", &f_lua_updatedomainkey);
147 get_lua_function(lua, "adddomainkey", &f_lua_adddomainkey);
148
149 get_lua_function(lua, "gettsigkey", &f_lua_gettsigkey);
150
151 get_lua_function(lua, "getbeforeandafternamesabsolute", &f_lua_getbeforeandafternamesabsolute);
152 get_lua_function(lua, "updatednssecorderandauthabsolute", &f_lua_updatednssecorderandauthabsolute);
153 get_lua_function(lua, "updatednssecorderandauth", &f_lua_updatednssecorderandauth); // not needed...
154
155 }
156 }
157 } else {
158 //a big kaboom here!
159 throw LUAException (backend_name + "LUA OPEN FAILED!");
160 }
161 }
162
163 void LUABackend::rediscover(string* status) {
164
165 if (f_lua_rediscover == 0)
166 return;
167
168 if (logging)
169 g_log << Logger::Info << backend_name << "(rediscover) BEGIN" << endl;
170
171 lua_rawgeti(lua, LUA_REGISTRYINDEX, f_lua_rediscover);
172
173 if(lua_pcall(lua, 0, 1, f_lua_exec_error) != 0) {
174 string e = backend_name + lua_tostring(lua, -1);
175 lua_pop(lua, 1);
176
177 throw runtime_error(e);
178 }
179
180 size_t returnedwhat = lua_type(lua, -1);
181 if (returnedwhat != LUA_TSTRING) {
182 lua_pop(lua, 1 );
183 return;
184 }
185
186 string s = lua_tostring(lua, -1);
187 lua_pop(lua, 1 );
188 *status = s;
189
190 if (logging)
191 g_log << Logger::Info << backend_name << "(rediscover) END" << endl;
192
193 return;
194 }
195