]>
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 | ||
9f95a23c TL |
4 | #include <cstdarg> |
5 | #include "common/ceph_context.h" | |
6 | #include "common/ceph_releases.h" | |
7c673cae FG |
7 | #include "common/config.h" |
8 | #include "common/debug.h" | |
9 | ||
10 | #include "objclass/objclass.h" | |
7c673cae FG |
11 | #include "osd/ClassHandler.h" |
12 | ||
13 | #include "auth/Crypto.h" | |
14 | #include "common/armor.h" | |
15 | ||
9f95a23c | 16 | #define dout_context ClassHandler::get_instance().cct |
7c673cae | 17 | |
9f95a23c | 18 | static constexpr int dout_subsys = ceph_subsys_objclass; |
7c673cae FG |
19 | |
20 | void *cls_alloc(size_t size) | |
21 | { | |
22 | return malloc(size); | |
23 | } | |
24 | ||
25 | void cls_free(void *p) | |
26 | { | |
27 | free(p); | |
28 | } | |
29 | ||
30 | int cls_register(const char *name, cls_handle_t *handle) | |
31 | { | |
9f95a23c TL |
32 | ClassHandler::ClassData *cls = \ |
33 | ClassHandler::get_instance().register_class(name); | |
7c673cae FG |
34 | *handle = (cls_handle_t)cls; |
35 | return (cls != NULL); | |
36 | } | |
37 | ||
38 | int cls_unregister(cls_handle_t handle) | |
39 | { | |
40 | ClassHandler::ClassData *cls = (ClassHandler::ClassData *)handle; | |
9f95a23c | 41 | ClassHandler::get_instance().unregister_class(cls); |
7c673cae FG |
42 | return 1; |
43 | } | |
44 | ||
45 | int cls_register_method(cls_handle_t hclass, const char *method, | |
46 | int flags, | |
47 | cls_method_call_t class_call, cls_method_handle_t *handle) | |
48 | { | |
49 | if (!(flags & (CLS_METHOD_RD | CLS_METHOD_WR))) | |
50 | return -EINVAL; | |
51 | ClassHandler::ClassData *cls = (ClassHandler::ClassData *)hclass; | |
52 | cls_method_handle_t hmethod =(cls_method_handle_t)cls->register_method(method, flags, class_call); | |
53 | if (handle) | |
54 | *handle = hmethod; | |
55 | return (hmethod != NULL); | |
56 | } | |
57 | ||
58 | int cls_register_cxx_method(cls_handle_t hclass, const char *method, | |
59 | int flags, | |
60 | cls_method_cxx_call_t class_call, cls_method_handle_t *handle) | |
61 | { | |
62 | ClassHandler::ClassData *cls = (ClassHandler::ClassData *)hclass; | |
63 | cls_method_handle_t hmethod = (cls_method_handle_t)cls->register_cxx_method(method, flags, class_call); | |
64 | if (handle) | |
65 | *handle = hmethod; | |
66 | return (hmethod != NULL); | |
67 | } | |
68 | ||
69 | int cls_unregister_method(cls_method_handle_t handle) | |
70 | { | |
71 | ClassHandler::ClassMethod *method = (ClassHandler::ClassMethod *)handle; | |
72 | method->unregister(); | |
73 | return 1; | |
74 | } | |
75 | ||
76 | int cls_register_cxx_filter(cls_handle_t hclass, | |
77 | const std::string &filter_name, | |
78 | cls_cxx_filter_factory_t fn, | |
79 | cls_filter_handle_t *handle) | |
80 | { | |
81 | ClassHandler::ClassData *cls = (ClassHandler::ClassData *)hclass; | |
82 | cls_filter_handle_t hfilter = (cls_filter_handle_t)cls->register_cxx_filter(filter_name, fn); | |
83 | if (handle) { | |
84 | *handle = hfilter; | |
85 | } | |
86 | return (hfilter != NULL); | |
87 | } | |
88 | ||
89 | void cls_unregister_filter(cls_filter_handle_t handle) | |
90 | { | |
91 | ClassHandler::ClassFilter *filter = (ClassHandler::ClassFilter *)handle; | |
92 | filter->unregister(); | |
93 | } | |
94 | ||
7c673cae FG |
95 | int cls_cxx_read(cls_method_context_t hctx, int ofs, int len, bufferlist *outbl) |
96 | { | |
97 | return cls_cxx_read2(hctx, ofs, len, outbl, 0); | |
98 | } | |
99 | ||
7c673cae FG |
100 | int cls_cxx_write(cls_method_context_t hctx, int ofs, int len, bufferlist *inbl) |
101 | { | |
102 | return cls_cxx_write2(hctx, ofs, len, inbl, 0); | |
103 | } | |
104 | ||
7c673cae FG |
105 | int cls_gen_random_bytes(char *buf, int size) |
106 | { | |
9f95a23c | 107 | ClassHandler::get_instance().cct->random()->get_bytes(buf, size); |
11fdf7f2 | 108 | return 0; |
7c673cae FG |
109 | } |
110 | ||
111 | int cls_gen_rand_base64(char *dest, int size) /* size should be the required string size + 1 */ | |
112 | { | |
113 | char buf[size]; | |
114 | char tmp_dest[size + 4]; /* so that there's space for the extra '=' characters, and some */ | |
115 | int ret; | |
116 | ||
117 | ret = cls_gen_random_bytes(buf, sizeof(buf)); | |
118 | if (ret < 0) { | |
9f95a23c | 119 | derr << "cannot get random bytes: " << ret << dendl; |
7c673cae FG |
120 | return -1; |
121 | } | |
122 | ||
123 | ret = ceph_armor(tmp_dest, &tmp_dest[sizeof(tmp_dest)], | |
124 | (const char *)buf, ((const char *)buf) + ((size - 1) * 3 + 4 - 1) / 4); | |
125 | if (ret < 0) { | |
9f95a23c | 126 | derr << "ceph_armor failed" << dendl; |
7c673cae FG |
127 | return -1; |
128 | } | |
129 | tmp_dest[ret] = '\0'; | |
130 | memcpy(dest, tmp_dest, size); | |
131 | dest[size-1] = '\0'; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
7c673cae FG |
136 | void cls_cxx_subop_version(cls_method_context_t hctx, string *s) |
137 | { | |
138 | if (!s) | |
139 | return; | |
140 | ||
141 | char buf[32]; | |
142 | uint64_t ver = cls_current_version(hctx); | |
143 | int subop_num = cls_current_subop_num(hctx); | |
144 | snprintf(buf, sizeof(buf), "%lld.%d", (long long)ver, subop_num); | |
145 | ||
146 | *s = buf; | |
147 | } | |
148 | ||
149 | int cls_log(int level, const char *format, ...) | |
150 | { | |
151 | int size = 256; | |
152 | va_list ap; | |
153 | while (1) { | |
154 | char buf[size]; | |
155 | va_start(ap, format); | |
156 | int n = vsnprintf(buf, size, format, ap); | |
157 | va_end(ap); | |
158 | #define MAX_SIZE 8196 | |
159 | if ((n > -1 && n < size) || size > MAX_SIZE) { | |
9f95a23c | 160 | dout(ceph::dout::need_dynamic(level)) << buf << dendl; |
7c673cae FG |
161 | return n; |
162 | } | |
163 | size *= 2; | |
164 | } | |
165 | } |