]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_lua_utils.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rgw / rgw_lua_utils.cc
1 #include <string>
2 #include <lua.hpp>
3 #include "common/ceph_context.h"
4 #include "common/dout.h"
5 #include "rgw_lua_utils.h"
6 #include "rgw_lua_version.h"
7
8 #define dout_subsys ceph_subsys_rgw
9
10 namespace rgw::lua {
11
12 // TODO - add the folowing generic functions
13 // lua_push(lua_State* L, const std::string& str)
14 // template<typename T> lua_push(lua_State* L, const std::optional<T>& val)
15 // lua_push(lua_State* L, const ceph::real_time& tp)
16
17 constexpr const char* RGWDebugLogAction{"RGWDebugLog"};
18
19 int RGWDebugLog(lua_State* L)
20 {
21 auto cct = reinterpret_cast<CephContext*>(lua_touserdata(L, lua_upvalueindex(1)));
22
23 auto message = luaL_checkstring(L, 1);
24 ldout(cct, 20) << "Lua INFO: " << message << dendl;
25 return 0;
26 }
27
28 void create_debug_action(lua_State* L, CephContext* cct) {
29 lua_pushlightuserdata(L, cct);
30 lua_pushcclosure(L, RGWDebugLog, ONE_UPVAL);
31 lua_setglobal(L, RGWDebugLogAction);
32 }
33
34 void stack_dump(lua_State* L) {
35 int top = lua_gettop(L);
36 std::cout << std::endl << " ---------------- Stack Dump ----------------" << std::endl;
37 std::cout << "Stack Size: " << top << std::endl;
38 for (int i = 1, j = -top; i <= top; i++, j++) {
39 std::cout << "[" << i << "," << j << "]: " << luaL_tolstring(L, i, NULL) << std::endl;
40 lua_pop(L, 1);
41 }
42 std::cout << "--------------- Stack Dump Finished ---------------" << std::endl;
43 }
44
45 void set_package_path(lua_State* L, const std::string& install_dir) {
46 if (install_dir.empty()) {
47 return;
48 }
49 lua_getglobal(L, "package");
50 if (!lua_istable(L, -1)) {
51 return;
52 }
53 const auto path = install_dir+"/share/lua/"+CEPH_LUA_VERSION+"/?.lua";
54 pushstring(L, path);
55 lua_setfield(L, -2, "path");
56
57 const auto cpath = install_dir+"/lib/lua/"+CEPH_LUA_VERSION+"/?.so";
58 pushstring(L, cpath);
59 lua_setfield(L, -2, "cpath");
60 }
61
62 void open_standard_libs(lua_State* L) {
63 luaL_openlibs(L);
64 unsetglobal(L, "load");
65 unsetglobal(L, "loadfile");
66 unsetglobal(L, "loadstring");
67 unsetglobal(L, "dofile");
68 unsetglobal(L, "debug");
69 // remove os.exit()
70 lua_getglobal(L, "os");
71 lua_pushstring(L, "exit");
72 lua_pushnil(L);
73 lua_settable(L, -3);
74 }
75
76 }