358 lines
8.8 KiB
C
358 lines
8.8 KiB
C
#include <Void/Plugin/CAbi.h>
|
|
|
|
#include <stdatomic.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef struct cabi_calc_v1 {
|
|
uint32_t (*version)(void_module_handle* h);
|
|
int32_t (*accumulate)(void_module_handle* h, int32_t delta);
|
|
} cabi_calc_v1;
|
|
|
|
struct void_module_handle {
|
|
void_host_api_v1 host;
|
|
atomic_int total;
|
|
int ready;
|
|
};
|
|
|
|
static const void_interface_id_v1 kCalcIfaceId = {
|
|
{0x76, 0x6f, 0x69, 0x64, 0x63, 0x61, 0x6c, 0x63, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x01}};
|
|
|
|
static void free_c_str(void* ctx, const char* msg)
|
|
{
|
|
(void)ctx;
|
|
free((void*)msg);
|
|
}
|
|
|
|
static void free_c_bytes(void* ctx, const uint8_t* data, uint64_t size)
|
|
{
|
|
(void)ctx;
|
|
(void)size;
|
|
free((void*)data);
|
|
}
|
|
|
|
static void set_err(void_error_detail_v1* out_err,
|
|
int32_t status,
|
|
const char* text)
|
|
{
|
|
if (out_err == NULL || out_err->struct_size < sizeof(void_error_detail_v1)) {
|
|
return;
|
|
}
|
|
out_err->status = status;
|
|
if (text != NULL) {
|
|
size_t n = strlen(text);
|
|
char* copy = (char*)malloc(n + 1);
|
|
if (copy != NULL) {
|
|
memcpy(copy, text, n + 1);
|
|
out_err->message = copy;
|
|
out_err->release = free_c_str;
|
|
out_err->release_ctx = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int cfg_has(void_bytes_v1 cfg, const char* needle)
|
|
{
|
|
if (cfg.data == NULL || cfg.size == 0) {
|
|
return 0;
|
|
}
|
|
size_t nlen = strlen(needle);
|
|
if (cfg.size < nlen) {
|
|
return 0;
|
|
}
|
|
for (uint64_t i = 0; i + nlen <= cfg.size; ++i) {
|
|
if (memcmp(cfg.data + i, needle, nlen) == 0) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int32_t cabi_v2_get_info(void_module_info_v1* out_info)
|
|
{
|
|
if (out_info == NULL) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
memset(out_info, 0, sizeof(*out_info));
|
|
out_info->struct_size = (uint32_t)sizeof(void_module_info_v1);
|
|
out_info->module_id = "cabi.calc";
|
|
out_info->version = "2.0.0";
|
|
out_info->activation_policy = VOID_POLICY_RELOADABLE;
|
|
out_info->state_schema_version = 2;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_create(const void_host_api_v1* host_api,
|
|
void_module_handle** out_handle)
|
|
{
|
|
if (out_handle == NULL) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
*out_handle = NULL;
|
|
if (host_api == NULL || host_api->struct_size < sizeof(void_host_api_v1)) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
struct void_module_handle* h =
|
|
(struct void_module_handle*)calloc(1, sizeof(*h));
|
|
if (h == NULL) {
|
|
return VOID_STATUS_EXHAUSTED;
|
|
}
|
|
h->host = *host_api;
|
|
atomic_init(&h->total, 0);
|
|
h->ready = 1;
|
|
*out_handle = h;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_activate(void_module_handle* h,
|
|
void_bytes_v1 config,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
if (h == NULL) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
if (cfg_has(config, "fail_activate")) {
|
|
set_err(out_err, VOID_STATUS_INTERNAL, "V2 activation failed");
|
|
return VOID_STATUS_INTERNAL;
|
|
}
|
|
h->ready = cfg_has(config, "not_ready") ? 0 : 1;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_readiness(void_module_handle* h,
|
|
uint32_t* out_ready,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
if (h == NULL || out_ready == NULL) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
*out_ready = h->ready ? 1u : 0u;
|
|
if (!h->ready) {
|
|
set_err(out_err, VOID_STATUS_NOT_READY, "V2 not ready");
|
|
}
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_quiesce(void_module_handle* h,
|
|
const void_op_options_v1* opts,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
(void)h;
|
|
(void)opts;
|
|
(void)out_err;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_resume(void_module_handle* h,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
(void)h;
|
|
(void)out_err;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_checkpoint(void_module_handle* h,
|
|
const void_op_options_v1* opts,
|
|
void_owned_bytes_v1* out_state,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
(void)opts;
|
|
(void)out_err;
|
|
if (h == NULL || out_state == NULL) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
char buf[64];
|
|
int val = atomic_load(&h->total);
|
|
int len = 0;
|
|
if (val == 0) {
|
|
memcpy(buf, "v2:0", 4);
|
|
len = 4;
|
|
} else {
|
|
/* Decimal conversion without stdio overhead. */
|
|
char rev[32];
|
|
int rlen = 0;
|
|
int neg = val < 0;
|
|
unsigned u = neg ? (unsigned)(-val) : (unsigned)val;
|
|
while (u > 0) {
|
|
rev[rlen++] = (char)('0' + (u % 10u));
|
|
u /= 10u;
|
|
}
|
|
buf[len++] = 'v';
|
|
buf[len++] = '2';
|
|
buf[len++] = ':';
|
|
if (neg) {
|
|
buf[len++] = '-';
|
|
}
|
|
while (rlen > 0) {
|
|
buf[len++] = rev[--rlen];
|
|
}
|
|
}
|
|
uint8_t* copy = (uint8_t*)malloc((size_t)len);
|
|
if (copy == NULL) {
|
|
return VOID_STATUS_EXHAUSTED;
|
|
}
|
|
memcpy(copy, buf, (size_t)len);
|
|
out_state->data = copy;
|
|
out_state->size = (uint64_t)len;
|
|
out_state->release = free_c_bytes;
|
|
out_state->release_ctx = NULL;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_restore(void_module_handle* h,
|
|
void_bytes_v1 state,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
if (h == NULL || state.data == NULL || state.size < 4) {
|
|
set_err(out_err, VOID_STATUS_STATE_REJECTED, "V2 invalid checkpoint");
|
|
return VOID_STATUS_STATE_REJECTED;
|
|
}
|
|
if (memcmp(state.data, "v1:", 3) != 0 &&
|
|
memcmp(state.data, "v2:", 3) != 0) {
|
|
set_err(out_err, VOID_STATUS_STATE_REJECTED, "V2 unknown checkpoint header");
|
|
return VOID_STATUS_STATE_REJECTED;
|
|
}
|
|
char buf[64];
|
|
size_t n = (size_t)(state.size - 3);
|
|
if (n >= sizeof(buf)) {
|
|
return VOID_STATUS_STATE_REJECTED;
|
|
}
|
|
memcpy(buf, state.data + 3, n);
|
|
buf[n] = '\0';
|
|
int val = atoi(buf);
|
|
atomic_store(&h->total, val);
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static uint32_t iface_v2_version(void_module_handle* h)
|
|
{
|
|
(void)h;
|
|
return 2u;
|
|
}
|
|
|
|
static int32_t iface_v2_accumulate(void_module_handle* h, int32_t delta)
|
|
{
|
|
if (h == NULL) {
|
|
return -1;
|
|
}
|
|
return atomic_fetch_add(&h->total, delta) + delta;
|
|
}
|
|
|
|
static const cabi_calc_v1 kCalcV2 = {iface_v2_version, iface_v2_accumulate};
|
|
|
|
static int32_t cabi_v2_query_interface(void_module_handle* h,
|
|
const void_interface_id_v1* iface_id,
|
|
uint32_t min_major,
|
|
uint32_t min_table_size,
|
|
void_interface_view_v1* out_view)
|
|
{
|
|
if (h == NULL || iface_id == NULL || out_view == NULL) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
if (memcmp(iface_id->bytes, kCalcIfaceId.bytes, 16) != 0) {
|
|
return VOID_STATUS_NOT_FOUND;
|
|
}
|
|
if (min_major != 1u || min_table_size > sizeof(cabi_calc_v1)) {
|
|
return VOID_STATUS_UNSUPPORTED;
|
|
}
|
|
out_view->struct_size = (uint32_t)sizeof(void_interface_view_v1);
|
|
out_view->major = 1u;
|
|
out_view->minor = 1u;
|
|
out_view->table_size = (uint32_t)sizeof(cabi_calc_v1);
|
|
out_view->function_table = &kCalcV2;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static void cabi_v2_release_interface(void_module_handle* h,
|
|
const void_interface_id_v1* iface_id,
|
|
const void_interface_view_v1* view)
|
|
{
|
|
(void)h;
|
|
(void)iface_id;
|
|
(void)view;
|
|
}
|
|
|
|
static int32_t cabi_v2_submit_async(void_module_handle* h,
|
|
uint32_t op_kind,
|
|
void_bytes_v1 payload,
|
|
const void_op_options_v1* opts,
|
|
void_async_complete_fn cb,
|
|
void* cb_ctx,
|
|
void_operation_handle** out_op,
|
|
void_error_detail_v1* out_err)
|
|
{
|
|
(void)h;
|
|
(void)op_kind;
|
|
(void)payload;
|
|
(void)opts;
|
|
(void)cb;
|
|
(void)cb_ctx;
|
|
(void)out_op;
|
|
(void)out_err;
|
|
return VOID_STATUS_UNSUPPORTED;
|
|
}
|
|
|
|
static int32_t cabi_v2_cancel_async(void_module_handle* h,
|
|
void_operation_handle* op)
|
|
{
|
|
(void)h;
|
|
(void)op;
|
|
return VOID_STATUS_UNSUPPORTED;
|
|
}
|
|
|
|
static void cabi_v2_release_async(void_module_handle* h,
|
|
void_operation_handle* op)
|
|
{
|
|
(void)h;
|
|
(void)op;
|
|
}
|
|
|
|
static int32_t cabi_v2_request_stop(void_module_handle* h)
|
|
{
|
|
(void)h;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static int32_t cabi_v2_deactivate(void_module_handle* h)
|
|
{
|
|
(void)h;
|
|
return VOID_STATUS_OK;
|
|
}
|
|
|
|
static void cabi_v2_destroy(void_module_handle* h) { free(h); }
|
|
|
|
int32_t void_module_query_v1(uint32_t host_struct_size,
|
|
void_module_api_v1* out_api)
|
|
{
|
|
if (out_api == NULL || host_struct_size < VOID_MODULE_API_V1_MIN_SIZE) {
|
|
return VOID_STATUS_INVALID_ARGUMENT;
|
|
}
|
|
memset(out_api, 0, sizeof(*out_api));
|
|
out_api->struct_size = (uint32_t)sizeof(void_module_api_v1);
|
|
out_api->abi_major = VOID_CABI_MAJOR_V1;
|
|
out_api->abi_minor = VOID_CABI_MINOR_V1;
|
|
out_api->reserved = 0;
|
|
out_api->capabilities = VOID_CAP_STATELESS_RELOAD |
|
|
VOID_CAP_STATEFUL_CHECKPOINT |
|
|
VOID_CAP_SERVICE_INTERFACES;
|
|
out_api->get_info = cabi_v2_get_info;
|
|
out_api->create = cabi_v2_create;
|
|
out_api->activate = cabi_v2_activate;
|
|
out_api->readiness = cabi_v2_readiness;
|
|
out_api->quiesce = cabi_v2_quiesce;
|
|
out_api->resume = cabi_v2_resume;
|
|
out_api->checkpoint = cabi_v2_checkpoint;
|
|
out_api->restore = cabi_v2_restore;
|
|
out_api->query_interface = cabi_v2_query_interface;
|
|
out_api->release_interface = cabi_v2_release_interface;
|
|
out_api->submit_async = cabi_v2_submit_async;
|
|
out_api->cancel_async = cabi_v2_cancel_async;
|
|
out_api->release_async = cabi_v2_release_async;
|
|
out_api->request_stop = cabi_v2_request_stop;
|
|
out_api->deactivate = cabi_v2_deactivate;
|
|
out_api->destroy = cabi_v2_destroy;
|
|
return VOID_STATUS_OK;
|
|
}
|