1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #include "common/ceph_context.h"
6 #include "common/ceph_releases.h"
7 #include "common/config.h"
8 #include "common/debug.h"
10 #include "objclass/objclass.h"
11 #include "osd/ClassHandler.h"
13 #include "auth/Crypto.h"
14 #include "common/armor.h"
16 #define dout_context ClassHandler::get_instance().cct
18 static constexpr int dout_subsys
= ceph_subsys_objclass
;
20 void *cls_alloc(size_t size
)
25 void cls_free(void *p
)
30 int cls_register(const char *name
, cls_handle_t
*handle
)
32 ClassHandler::ClassData
*cls
= \
33 ClassHandler::get_instance().register_class(name
);
34 *handle
= (cls_handle_t
)cls
;
38 int cls_unregister(cls_handle_t handle
)
40 ClassHandler::ClassData
*cls
= (ClassHandler::ClassData
*)handle
;
41 ClassHandler::get_instance().unregister_class(cls
);
45 int cls_register_method(cls_handle_t hclass
, const char *method
,
47 cls_method_call_t class_call
, cls_method_handle_t
*handle
)
49 if (!(flags
& (CLS_METHOD_RD
| CLS_METHOD_WR
)))
51 ClassHandler::ClassData
*cls
= (ClassHandler::ClassData
*)hclass
;
52 cls_method_handle_t hmethod
=(cls_method_handle_t
)cls
->register_method(method
, flags
, class_call
);
55 return (hmethod
!= NULL
);
58 int cls_register_cxx_method(cls_handle_t hclass
, const char *method
,
60 cls_method_cxx_call_t class_call
, cls_method_handle_t
*handle
)
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
);
66 return (hmethod
!= NULL
);
69 int cls_unregister_method(cls_method_handle_t handle
)
71 ClassHandler::ClassMethod
*method
= (ClassHandler::ClassMethod
*)handle
;
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
)
81 ClassHandler::ClassData
*cls
= (ClassHandler::ClassData
*)hclass
;
82 cls_filter_handle_t hfilter
= (cls_filter_handle_t
)cls
->register_cxx_filter(filter_name
, fn
);
86 return (hfilter
!= NULL
);
89 void cls_unregister_filter(cls_filter_handle_t handle
)
91 ClassHandler::ClassFilter
*filter
= (ClassHandler::ClassFilter
*)handle
;
95 int cls_cxx_read(cls_method_context_t hctx
, int ofs
, int len
, bufferlist
*outbl
)
97 return cls_cxx_read2(hctx
, ofs
, len
, outbl
, 0);
100 int cls_cxx_write(cls_method_context_t hctx
, int ofs
, int len
, bufferlist
*inbl
)
102 return cls_cxx_write2(hctx
, ofs
, len
, inbl
, 0);
105 int cls_gen_random_bytes(char *buf
, int size
)
107 ClassHandler::get_instance().cct
->random()->get_bytes(buf
, size
);
111 int cls_gen_rand_base64(char *dest
, int size
) /* size should be the required string size + 1 */
114 char tmp_dest
[size
+ 4]; /* so that there's space for the extra '=' characters, and some */
117 ret
= cls_gen_random_bytes(buf
, sizeof(buf
));
119 derr
<< "cannot get random bytes: " << ret
<< dendl
;
123 ret
= ceph_armor(tmp_dest
, &tmp_dest
[sizeof(tmp_dest
)],
124 (const char *)buf
, ((const char *)buf
) + ((size
- 1) * 3 + 4 - 1) / 4);
126 derr
<< "ceph_armor failed" << dendl
;
129 tmp_dest
[ret
] = '\0';
130 memcpy(dest
, tmp_dest
, size
);
136 void cls_cxx_subop_version(cls_method_context_t hctx
, string
*s
)
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
);
149 int cls_log(int level
, const char *format
, ...)
155 va_start(ap
, format
);
156 int n
= vsnprintf(buf
, size
, format
, ap
);
158 #define MAX_SIZE 8196
159 if ((n
> -1 && n
< size
) || size
> MAX_SIZE
) {
160 dout(ceph::dout::need_dynamic(level
)) << buf
<< dendl
;