]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | #ifndef CEPH_CLASSHANDLER_H | |
4 | #define CEPH_CLASSHANDLER_H | |
5 | ||
9f95a23c TL |
6 | #include <variant> |
7 | ||
7c673cae | 8 | #include "include/types.h" |
9f95a23c TL |
9 | #include "include/common_fwd.h" |
10 | #include "common/ceph_mutex.h" | |
7c673cae | 11 | #include "objclass/objclass.h" |
7c673cae FG |
12 | |
13 | //forward declaration | |
7c673cae FG |
14 | class ClassHandler |
15 | { | |
16 | public: | |
17 | CephContext *cct; | |
7c673cae FG |
18 | struct ClassData; |
19 | ||
20 | struct ClassMethod { | |
9f95a23c TL |
21 | const std::string name; |
22 | using func_t = std::variant<cls_method_cxx_call_t, cls_method_call_t>; | |
23 | func_t func; | |
24 | int flags = 0; | |
25 | ClassData *cls = nullptr; | |
26 | ||
27 | int exec(cls_method_context_t ctx, | |
28 | ceph::bufferlist& indata, | |
29 | ceph::bufferlist& outdata); | |
7c673cae FG |
30 | void unregister(); |
31 | ||
32 | int get_flags() { | |
11fdf7f2 | 33 | std::lock_guard l(cls->handler->mutex); |
7c673cae FG |
34 | return flags; |
35 | } | |
9f95a23c TL |
36 | ClassMethod(const char* name, func_t call, int flags, ClassData* cls) |
37 | : name{name}, func{call}, flags{flags}, cls{cls} | |
38 | {} | |
7c673cae FG |
39 | }; |
40 | ||
41 | struct ClassFilter { | |
9f95a23c | 42 | ClassData *cls = nullptr; |
7c673cae | 43 | std::string name; |
9f95a23c | 44 | cls_cxx_filter_factory_t fn = nullptr; |
7c673cae FG |
45 | |
46 | void unregister(); | |
7c673cae FG |
47 | }; |
48 | ||
49 | struct ClassData { | |
50 | enum Status { | |
51 | CLASS_UNKNOWN, | |
52 | CLASS_MISSING, // missing | |
53 | CLASS_MISSING_DEPS, // missing dependencies | |
54 | CLASS_INITIALIZING, // calling init() right now | |
55 | CLASS_OPEN, // initialized, usable | |
9f95a23c | 56 | } status = CLASS_UNKNOWN; |
7c673cae | 57 | |
9f95a23c TL |
58 | std::string name; |
59 | ClassHandler *handler = nullptr; | |
60 | void *handle = nullptr; | |
7c673cae | 61 | |
f67539c2 | 62 | bool allowed = false; |
7c673cae | 63 | |
9f95a23c TL |
64 | std::map<std::string, ClassMethod> methods_map; |
65 | std::map<std::string, ClassFilter> filters_map; | |
7c673cae | 66 | |
9f95a23c TL |
67 | std::set<ClassData *> dependencies; /* our dependencies */ |
68 | std::set<ClassData *> missing_dependencies; /* only missing dependencies */ | |
7c673cae | 69 | |
9f95a23c | 70 | ClassMethod *_get_method(const std::string& mname); |
7c673cae | 71 | |
9f95a23c TL |
72 | ClassMethod *register_method(const char *mname, |
73 | int flags, | |
74 | cls_method_call_t func); | |
75 | ClassMethod *register_cxx_method(const char *mname, | |
76 | int flags, | |
77 | cls_method_cxx_call_t func); | |
7c673cae FG |
78 | void unregister_method(ClassMethod *method); |
79 | ||
9f95a23c TL |
80 | ClassFilter *register_cxx_filter(const std::string &filter_name, |
81 | cls_cxx_filter_factory_t fn); | |
7c673cae FG |
82 | void unregister_filter(ClassFilter *method); |
83 | ||
9f95a23c | 84 | ClassMethod *get_method(const std::string& mname) { |
11fdf7f2 | 85 | std::lock_guard l(handler->mutex); |
7c673cae FG |
86 | return _get_method(mname); |
87 | } | |
9f95a23c | 88 | int get_method_flags(const std::string& mname); |
7c673cae | 89 | |
9f95a23c | 90 | ClassFilter *get_filter(const std::string &filter_name) { |
11fdf7f2 | 91 | std::lock_guard l(handler->mutex); |
9f95a23c TL |
92 | if (auto i = filters_map.find(filter_name); i == filters_map.end()) { |
93 | return nullptr; | |
7c673cae FG |
94 | } else { |
95 | return &(i->second); | |
96 | } | |
97 | } | |
98 | }; | |
99 | ||
100 | private: | |
9f95a23c | 101 | std::map<std::string, ClassData> classes; |
7c673cae | 102 | |
9f95a23c | 103 | ClassData *_get_class(const std::string& cname, bool check_allowed); |
7c673cae FG |
104 | int _load_class(ClassData *cls); |
105 | ||
106 | static bool in_class_list(const std::string& cname, | |
107 | const std::string& list); | |
108 | ||
9f95a23c | 109 | ceph::mutex mutex = ceph::make_mutex("ClassHandler"); |
7c673cae | 110 | |
9f95a23c TL |
111 | public: |
112 | explicit ClassHandler(CephContext *cct) : cct(cct) {} | |
7c673cae FG |
113 | |
114 | int open_all_classes(); | |
9f95a23c | 115 | int open_class(const std::string& cname, ClassData **pcls); |
7c673cae | 116 | |
7c673cae FG |
117 | ClassData *register_class(const char *cname); |
118 | void unregister_class(ClassData *cls); | |
119 | ||
120 | void shutdown(); | |
9f95a23c TL |
121 | |
122 | static ClassHandler& get_instance(); | |
7c673cae FG |
123 | }; |
124 | ||
125 | ||
126 | #endif |