Skip to content

Commit

Permalink
Add base interface for declaring plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Oct 4, 2023
1 parent f71fae5 commit 404b8ef
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 98 deletions.
62 changes: 7 additions & 55 deletions source/cli/plugins/cli_core_plugin/source/cli_core_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

#include <cli_core_plugin/cli_core_plugin.h>

#include <log/log.h>
#include <metacall/metacall.h>
#include <plugin/plugin_interface.h>

#include <string.h>

Expand Down Expand Up @@ -320,59 +319,12 @@ int cli_core_plugin(void *loader, void *handle, void *context)
{
(void)handle;

{
enum metacall_value_id *arg_types = NULL;
if (metacall_register_loaderv(loader, context, "inspect", inspect, METACALL_STRING, 0, arg_types) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: inspect");
return 1;
}
}

{
enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_STRING };
if (metacall_register_loaderv(loader, context, "clear", clear, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: clear");
return 1;
}
}

{
enum metacall_value_id arg_types[] = { METACALL_STRING };
if (metacall_register_loaderv(loader, context, "call", call, METACALL_PTR, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: call");
return 1;
}
}

{
enum metacall_value_id arg_types[] = { METACALL_STRING };
if (metacall_register_loaderv(loader, context, "await", await, METACALL_PTR, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: await");
return 1;
}
}

{
enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_STRING };
if (metacall_register_loaderv(loader, context, "eval", eval, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: eval");
return 1;
}
}

{
enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_ARRAY };
if (metacall_register_loaderv(loader, context, "load", load, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types) != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: load");
return 1;
}
}
EXTENSION_FUNCTION(METACALL_STRING, inspect);
EXTENSION_FUNCTION(METACALL_INT, clear, METACALL_STRING, METACALL_STRING);
EXTENSION_FUNCTION(METACALL_PTR, call, METACALL_STRING);
EXTENSION_FUNCTION(METACALL_PTR, await, METACALL_STRING);
EXTENSION_FUNCTION(METACALL_INT, eval, METACALL_STRING, METACALL_STRING);
EXTENSION_FUNCTION(METACALL_INT, load, METACALL_STRING, METACALL_ARRAY);

return 0;
}
58 changes: 58 additions & 0 deletions source/plugin/include/plugin/plugin_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Plugin Library by Parra Studios
* A library for plugins at run-time into a process.
*
* Copyright (C) 2016 - 2022 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef PLUGIN_INTERFACE_H
#define PLUGIN_INTERFACE_H 1

/* Private interface for using it inside any loader, extension or plugin for registering functions */

#include <preprocessor/preprocessor.h>

#include <log/log.h>

#include <metacall/metacall.h>

#define EXTENSION_FUNCTION_IMPL_VOID(ret, name) \
do \
{ \
if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, 0, NULL) != 0) \
{ \
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \
return 1; \
} \
} while (0)

#define EXTENSION_FUNCTION_IMPL(ret, name, ...) \
do \
{ \
enum metacall_value_id arg_types[] = { __VA_ARGS__ }; \
if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, PREPROCESSOR_ARGS_COUNT(__VA_ARGS__), arg_types) != 0) \
{ \
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \
return 1; \
} \
} while (0)

#define EXTENSION_FUNCTION(ret, name, ...) \
PREPROCESSOR_IF(PREPROCESSOR_ARGS_EMPTY(__VA_ARGS__), \
EXTENSION_FUNCTION_IMPL_VOID(ret, name), \
EXTENSION_FUNCTION_IMPL(ret, name, __VA_ARGS__))

#endif /* PLUGIN_INTERFACE_H */
44 changes: 5 additions & 39 deletions source/plugins/sandbox_plugin/source/sandbox_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@

#include <sandbox_plugin/sandbox_plugin.h>

#include <seccomp.h>

#include <preprocessor/preprocessor.h>
#include <plugin/plugin_interface.h>

#include <log/log.h>

#include <metacall/metacall.h>
#include <seccomp.h>

/* TODO: Use SCMP_ACT_KILL_PROCESS for catching the signal and showing the stack trace? */
#define SANDBOX_DEFAULT_ACTION SCMP_ACT_ALLOW // SCMP_ACT_KILL
Expand Down Expand Up @@ -148,39 +144,9 @@ int sandbox_plugin(void *loader, void *handle, void *context)
{
(void)handle;

#define SANDBOX_FUNCTION_IMPL_0(ret, name) \
do \
{ \
if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, 0, NULL) != 0) \
{ \
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \
return 1; \
} \
} while (0)

#define SANDBOX_FUNCTION_IMPL(ret, name, ...) \
do \
{ \
enum metacall_value_id arg_types[] = { __VA_ARGS__ }; \
if (metacall_register_loaderv(loader, context, PREPROCESSOR_STRINGIFY(name), name, ret, PREPROCESSOR_ARGS_COUNT(__VA_ARGS__), arg_types) != 0) \
{ \
log_write("metacall", LOG_LEVEL_ERROR, "Failed to register function: " PREPROCESSOR_STRINGIFY(name)); \
return 1; \
} \
} while (0)

#define SANDBOX_FUNCTION(ret, name, ...) \
PREPROCESSOR_IF(PREPROCESSOR_ARGS_EMPTY(__VA_ARGS__), \
SANDBOX_FUNCTION_IMPL_0(ret, PREPROCESSOR_CONCAT(sandbox_, name)), \
SANDBOX_FUNCTION_IMPL(ret, PREPROCESSOR_CONCAT(sandbox_, name), __VA_ARGS__))

SANDBOX_FUNCTION(METACALL_PTR, initialize);
SANDBOX_FUNCTION_IMPL(METACALL_INT, sandbox_uname, METACALL_PTR, METACALL_BOOL);
SANDBOX_FUNCTION_IMPL(METACALL_INT, sandbox_destroy, METACALL_PTR);

#undef SANDBOX_FUNCTION_IMPL_0
#undef SANDBOX_FUNCTION_IMPL
#undef SANDBOX_FUNCTION
EXTENSION_FUNCTION(METACALL_PTR, sandbox_initialize);
EXTENSION_FUNCTION(METACALL_INT, sandbox_uname, METACALL_PTR, METACALL_BOOL);
EXTENSION_FUNCTION(METACALL_INT, sandbox_destroy, METACALL_PTR);

#if 0 /* TODO: Fork safety */
#ifdef METACALL_FORK_SAFE
Expand Down
12 changes: 8 additions & 4 deletions source/preprocessor/include/preprocessor/preprocessor_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ extern "C" {

/* -- Macros -- */

#define PREPROCESSOR_IIF_IMPL_0(true_expr, false_expr) false_expr
#define PREPROCESSOR_IIF_IMPL_1(true_expr, false_expr) true_expr
#define PREPROCESSOR_IIF_IMPL_EXPAND_ARGS(...) __VA_ARGS__
#define PREPROCESSOR_IIF_IMPL_EXPAND(expr) expr
#define PREPROCESSOR_IIF_IMPL_EXPAND_REMOVE_PARENTHESIS(expr) PREPROCESSOR_IIF_IMPL_EXPAND(PREPROCESSOR_IIF_IMPL_EXPAND_ARGS expr)

#define PREPROCESSOR_IIF_IMPL_0(true_expr, false_expr) PREPROCESSOR_IIF_IMPL_EXPAND_REMOVE_PARENTHESIS(false_expr)
#define PREPROCESSOR_IIF_IMPL_1(true_expr, false_expr) PREPROCESSOR_IIF_IMPL_EXPAND_REMOVE_PARENTHESIS(true_expr)

#if defined(_MSC_VER) && !defined(__clang__)
#define PREPROCESSOR_IIF_IMPL_I(expr) expr
Expand All @@ -40,9 +44,9 @@ extern "C" {

#if defined(__EDG__) || defined(__EDG_VERSION__)
#define PREPROCESSOR_IF_IMPL(condition, true_expr, false_expr) PREPROCESSOR_IIF(PREPROCESSOR_BOOL(condition), true_expr, false_expr)
#define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IF_IMPL(condition, true_expr, false_expr)
#define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IF_IMPL(condition, (true_expr), (false_expr))
#else
#define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IIF(PREPROCESSOR_BOOL(condition), true_expr, false_expr)
#define PREPROCESSOR_IF(condition, true_expr, false_expr) PREPROCESSOR_IIF(PREPROCESSOR_BOOL(condition), (true_expr), (false_expr))
#endif

#ifdef __cplusplus
Expand Down

0 comments on commit 404b8ef

Please sign in to comment.