2020-12-11 17:07:20 +02:00
|
|
|
/* WirePlumber
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2020 Collabora Ltd.
|
|
|
|
|
* @author George Kiagiadakis <george.kiagiadakis@collabora.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "wplua.h"
|
|
|
|
|
#include "private.h"
|
|
|
|
|
#include <wp/wp.h>
|
|
|
|
|
|
log: implement a log topics system, like pipewire
The intention is to make checks for enabled log topics faster.
Every topic has its own structure that is statically defined in the file
where the logs are printed from. The structure is initialized transparently
when it is first used and it contains all the log level flags for the levels
that this topic should print messages. It is then checked on the wp_log()
macro before printing the message.
Topics from SPA/PipeWire are also handled natively, so messages are printed
directly without checking if the topic is enabled, since the PipeWire and SPA
macros do the checking themselves.
Messages coming from GLib are checked inside the handler.
An internal WpLogFields object is used to manage the state of each log
message, populating all the fields appropriately from the place they
are coming from (wp_log, spa_log, glib log), formatting the message and
then printing it. For printing to the journald, we still use the glib
message handler, converting all the needed fields to GLogField on demand.
That message handler does not do any checks for the topic or the level, so
we can just call it to send the message.
2023-05-16 11:51:29 +03:00
|
|
|
WP_LOG_TOPIC (log_topic_wplua, "wplua")
|
|
|
|
|
|
2020-12-15 18:28:28 +02:00
|
|
|
#define URI_SANDBOX "resource:///org/freedesktop/pipewire/wireplumber/wplua/sandbox.lua"
|
|
|
|
|
|
|
|
|
|
extern void _wplua_register_resource (void);
|
|
|
|
|
|
2025-07-08 13:51:46 -04:00
|
|
|
typedef struct {
|
|
|
|
|
guint64 refcount;
|
|
|
|
|
} WpLuaExtraData;
|
|
|
|
|
|
2020-12-15 18:36:14 +02:00
|
|
|
G_DEFINE_QUARK (wplua, wp_domain_lua);
|
|
|
|
|
|
2025-07-08 13:51:46 -04:00
|
|
|
static WpLuaExtraData *
|
|
|
|
|
wplua_getextradata (lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
WpLuaExtraData *data;
|
|
|
|
|
|
|
|
|
|
G_STATIC_ASSERT(LUA_EXTRASPACE >= sizeof(data));
|
|
|
|
|
memcpy (&data, lua_getextraspace (L), sizeof(data));
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 17:07:20 +02:00
|
|
|
static void
|
|
|
|
|
_wplua_openlibs (lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
/* http://www.lua.org/manual/5.3/manual.html#luaL_requiref
|
|
|
|
|
* http://www.lua.org/source/5.3/linit.c.html */
|
|
|
|
|
static const luaL_Reg loadedlibs[] = {
|
|
|
|
|
{"_G", luaopen_base},
|
2022-03-31 09:37:49 +03:00
|
|
|
{LUA_LOADLIBNAME, luaopen_package},
|
2022-04-06 18:05:26 +03:00
|
|
|
{LUA_COLIBNAME, luaopen_coroutine},
|
2020-12-11 17:07:20 +02:00
|
|
|
{LUA_TABLIBNAME, luaopen_table},
|
|
|
|
|
/* {LUA_IOLIBNAME, luaopen_io}, */
|
2020-12-15 18:28:28 +02:00
|
|
|
{LUA_OSLIBNAME, luaopen_os},
|
2020-12-11 17:07:20 +02:00
|
|
|
{LUA_STRLIBNAME, luaopen_string},
|
|
|
|
|
{LUA_MATHLIBNAME, luaopen_math},
|
|
|
|
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
|
|
|
|
{LUA_DBLIBNAME, luaopen_debug},
|
|
|
|
|
{NULL, NULL}
|
|
|
|
|
};
|
|
|
|
|
const luaL_Reg *lib;
|
|
|
|
|
|
|
|
|
|
for (lib = loadedlibs; lib->func; lib++) {
|
|
|
|
|
luaL_requiref (L, lib->name, lib->func, 1);
|
|
|
|
|
lua_pop (L, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 18:25:43 +02:00
|
|
|
static int
|
|
|
|
|
_wplua_errhandler (lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
luaL_traceback (L, L, NULL, 1);
|
|
|
|
|
wp_warning ("%s\n%s", lua_tostring (L, -2), lua_tostring (L, -1));
|
|
|
|
|
lua_pop (L, 2);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
_wplua_pcall (lua_State *L, int nargs, int nret)
|
|
|
|
|
{
|
2025-07-04 17:00:31 -04:00
|
|
|
int slots = lua_gettop (L);
|
2020-12-18 18:25:43 +02:00
|
|
|
int ret = LUA_OK;
|
2025-07-04 17:00:31 -04:00
|
|
|
/* 1 stack slot needed for error handler. */
|
|
|
|
|
int hpos, stack_slots = 1;
|
|
|
|
|
|
|
|
|
|
if (nargs < 0)
|
|
|
|
|
g_error ("negative number of arguments");
|
|
|
|
|
/* Need nargs + 1 stack slots for function and its arguments. */
|
|
|
|
|
if (slots <= nargs)
|
|
|
|
|
g_error ("not enough stack slots for arguments and function");
|
|
|
|
|
if (nret != LUA_MULTRET) {
|
|
|
|
|
if (nret - nargs > 1) {
|
|
|
|
|
/* Need more stack slots: 1 for the error handler and (nret - (nargs + 1))
|
|
|
|
|
* for the return values (after popping the function and its arguments). */
|
|
|
|
|
stack_slots = nret - nargs;
|
|
|
|
|
} else if (nret < 0)
|
|
|
|
|
g_error ("negative number of return values");
|
|
|
|
|
}
|
|
|
|
|
if (!lua_checkstack (L, stack_slots)) {
|
|
|
|
|
wp_critical ("_wplua_pcall: cannot grow Lua stack");
|
|
|
|
|
return LUA_ERRMEM;
|
|
|
|
|
}
|
2020-12-18 18:25:43 +02:00
|
|
|
|
2025-07-04 17:00:31 -04:00
|
|
|
hpos = slots - nargs;
|
2020-12-18 18:25:43 +02:00
|
|
|
lua_pushcfunction (L, _wplua_errhandler);
|
|
|
|
|
lua_insert (L, hpos);
|
|
|
|
|
|
|
|
|
|
ret = lua_pcall (L, nargs, nret, hpos);
|
|
|
|
|
switch (ret) {
|
|
|
|
|
case LUA_ERRMEM:
|
|
|
|
|
wp_critical ("not enough memory");
|
|
|
|
|
break;
|
|
|
|
|
case LUA_ERRERR:
|
|
|
|
|
wp_critical ("error running the message handler");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_remove (L, hpos);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 17:07:20 +02:00
|
|
|
lua_State *
|
|
|
|
|
wplua_new (void)
|
|
|
|
|
{
|
2020-12-15 18:28:28 +02:00
|
|
|
static gboolean resource_registered = FALSE;
|
2020-12-11 17:07:20 +02:00
|
|
|
lua_State *L = luaL_newstate ();
|
|
|
|
|
|
2025-07-08 13:51:46 -04:00
|
|
|
if (L == NULL)
|
|
|
|
|
g_error ("cannot create Lua state");
|
2020-12-11 17:07:20 +02:00
|
|
|
wp_debug ("initializing lua_State %p", L);
|
2025-07-08 13:51:46 -04:00
|
|
|
WpLuaExtraData *extradata = g_malloc(sizeof(*extradata));
|
|
|
|
|
extradata->refcount = 1;
|
|
|
|
|
memcpy (lua_getextraspace (L), &extradata, sizeof(extradata));
|
2020-12-11 17:07:20 +02:00
|
|
|
|
2020-12-15 18:28:28 +02:00
|
|
|
if (!resource_registered) {
|
|
|
|
|
_wplua_register_resource ();
|
|
|
|
|
resource_registered = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 17:07:20 +02:00
|
|
|
_wplua_openlibs (L);
|
|
|
|
|
_wplua_init_gboxed (L);
|
|
|
|
|
_wplua_init_gobject (L);
|
|
|
|
|
_wplua_init_closure (L);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
GHashTable *t = g_hash_table_new (g_direct_hash, g_direct_equal);
|
2020-12-16 23:19:07 +02:00
|
|
|
lua_pushliteral (L, "wplua_vtables");
|
2020-12-11 17:07:20 +02:00
|
|
|
wplua_pushboxed (L, G_TYPE_HASH_TABLE, t);
|
2020-12-16 23:19:07 +02:00
|
|
|
lua_settable (L, LUA_REGISTRYINDEX);
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-04 17:56:36 +03:00
|
|
|
return L;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lua_State *
|
|
|
|
|
wplua_ref (lua_State *L)
|
|
|
|
|
{
|
2025-07-08 13:51:46 -04:00
|
|
|
WpLuaExtraData *data = wplua_getextradata(L);
|
|
|
|
|
|
|
|
|
|
if (data->refcount < 1 || data->refcount == UINT64_MAX)
|
|
|
|
|
g_error ("bad refcount");
|
|
|
|
|
else {
|
|
|
|
|
data->refcount++;
|
|
|
|
|
return L;
|
|
|
|
|
}
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-04-04 17:56:36 +03:00
|
|
|
wplua_unref (lua_State * L)
|
2020-12-11 17:07:20 +02:00
|
|
|
{
|
2025-07-08 13:51:46 -04:00
|
|
|
WpLuaExtraData *data = wplua_getextradata(L);
|
|
|
|
|
|
|
|
|
|
if (data->refcount < 1)
|
|
|
|
|
g_error ("bad refcount");
|
|
|
|
|
else if (data->refcount > 1)
|
|
|
|
|
data->refcount--;
|
|
|
|
|
else {
|
2022-04-04 17:56:36 +03:00
|
|
|
wp_debug ("closing lua_State %p", L);
|
2025-07-08 13:51:46 -04:00
|
|
|
g_free (data);
|
2022-04-04 17:56:36 +03:00
|
|
|
lua_close (L);
|
|
|
|
|
}
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-15 18:28:28 +02:00
|
|
|
void
|
2021-02-01 16:14:34 +02:00
|
|
|
wplua_enable_sandbox (lua_State * L, WpLuaSandboxFlags flags)
|
2020-12-15 18:28:28 +02:00
|
|
|
{
|
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
|
wp_debug ("enabling Lua sandbox");
|
2021-02-01 16:14:34 +02:00
|
|
|
|
2022-03-30 18:22:21 +03:00
|
|
|
if (!wplua_load_uri (L, URI_SANDBOX, &error)) {
|
|
|
|
|
wp_critical ("Failed to load sandbox: %s", error->message);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 16:14:34 +02:00
|
|
|
lua_newtable (L);
|
|
|
|
|
lua_pushliteral (L, "isolate_env");
|
|
|
|
|
lua_pushboolean (L, (flags & WP_LUA_SANDBOX_ISOLATE_ENV));
|
|
|
|
|
lua_settable (L, -3);
|
|
|
|
|
|
2022-03-30 18:22:21 +03:00
|
|
|
if (!wplua_pcall (L, 1, 0, &error)) {
|
2020-12-15 18:28:28 +02:00
|
|
|
wp_critical ("Failed to load sandbox: %s", error->message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 18:22:21 +03:00
|
|
|
int
|
|
|
|
|
wplua_push_sandbox (lua_State * L)
|
|
|
|
|
{
|
|
|
|
|
return (lua_getglobal (L, "sandbox") == LUA_TFUNCTION) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 17:07:20 +02:00
|
|
|
void
|
|
|
|
|
wplua_register_type_methods (lua_State * L, GType type,
|
|
|
|
|
lua_CFunction constructor, const luaL_Reg * methods)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (L != NULL);
|
|
|
|
|
g_return_if_fail (G_TYPE_FUNDAMENTAL (type) == G_TYPE_OBJECT ||
|
2021-02-16 18:50:27 +05:30
|
|
|
G_TYPE_FUNDAMENTAL (type) == G_TYPE_BOXED ||
|
|
|
|
|
G_TYPE_FUNDAMENTAL (type) == G_TYPE_INTERFACE);
|
2020-12-11 17:07:20 +02:00
|
|
|
|
|
|
|
|
/* register methods */
|
|
|
|
|
if (methods) {
|
|
|
|
|
GHashTable *vtables;
|
|
|
|
|
|
2020-12-16 23:19:07 +02:00
|
|
|
lua_pushliteral (L, "wplua_vtables");
|
|
|
|
|
lua_gettable (L, LUA_REGISTRYINDEX);
|
2020-12-11 17:07:20 +02:00
|
|
|
vtables = wplua_toboxed (L, -1);
|
|
|
|
|
lua_pop (L, 1);
|
|
|
|
|
|
|
|
|
|
wp_debug ("Registering methods for '%s'", g_type_name (type));
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (g_hash_table_contains (vtables, GUINT_TO_POINTER (type)))) {
|
|
|
|
|
wp_critical ("type '%s' was already registered", g_type_name (type));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_hash_table_insert (vtables, GUINT_TO_POINTER (type), (gpointer) methods);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* register constructor */
|
|
|
|
|
if (constructor) {
|
2020-12-16 23:13:56 +02:00
|
|
|
luaL_Buffer b;
|
|
|
|
|
|
2020-12-11 17:07:20 +02:00
|
|
|
wp_debug ("Registering class for '%s'", g_type_name (type));
|
|
|
|
|
|
2020-12-16 23:13:56 +02:00
|
|
|
luaL_buffinit (L, &b);
|
|
|
|
|
luaL_addstring (&b, g_type_name (type));
|
|
|
|
|
luaL_addchar (&b, '_');
|
|
|
|
|
luaL_addstring (&b, "new");
|
|
|
|
|
luaL_pushresult (&b);
|
2020-12-11 17:07:20 +02:00
|
|
|
lua_pushcfunction (L, constructor);
|
2020-12-16 23:13:56 +02:00
|
|
|
lua_setglobal (L, lua_tostring (L, -2));
|
|
|
|
|
lua_pop (L, 1);
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 18:28:28 +02:00
|
|
|
static gboolean
|
2020-12-11 17:07:20 +02:00
|
|
|
_wplua_load_buffer (lua_State * L, const gchar *buf, gsize size,
|
2022-03-30 18:22:21 +03:00
|
|
|
const gchar * name, GError **error)
|
2020-12-11 17:07:20 +02:00
|
|
|
{
|
2020-12-15 18:28:28 +02:00
|
|
|
int ret;
|
|
|
|
|
|
2021-02-18 13:44:14 +02:00
|
|
|
/* skip shebang, if present */
|
|
|
|
|
if (g_str_has_prefix (buf, "#!/")) {
|
|
|
|
|
const char *tmp = strchr (buf, '\n');
|
|
|
|
|
size -= (tmp - buf);
|
|
|
|
|
buf = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 18:28:28 +02:00
|
|
|
ret = luaL_loadbuffer (L, buf, size, name);
|
2020-12-11 17:07:20 +02:00
|
|
|
if (ret != LUA_OK) {
|
2020-12-15 18:36:14 +02:00
|
|
|
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_COMPILATION,
|
2020-12-11 17:07:20 +02:00
|
|
|
"Failed to compile: %s", lua_tostring (L, -1));
|
2022-03-30 18:22:21 +03:00
|
|
|
lua_pop (L, 1);
|
2020-12-11 17:07:20 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
2022-03-30 18:22:21 +03:00
|
|
|
wplua_load_buffer (lua_State * L, const gchar *buf, gsize size, GError **error)
|
2020-12-11 17:07:20 +02:00
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (L != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (buf != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (size != 0, FALSE);
|
|
|
|
|
|
|
|
|
|
g_autofree gchar *name =
|
|
|
|
|
g_strdup_printf ("buffer@%p;size=%" G_GSIZE_FORMAT, buf, size);
|
2022-03-30 18:22:21 +03:00
|
|
|
return _wplua_load_buffer (L, buf, size, name, error);
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
2022-03-30 18:22:21 +03:00
|
|
|
wplua_load_uri (lua_State * L, const gchar *uri, GError **error)
|
2020-12-11 17:07:20 +02:00
|
|
|
{
|
|
|
|
|
g_autoptr (GFile) file = NULL;
|
|
|
|
|
g_autoptr (GBytes) bytes = NULL;
|
|
|
|
|
g_autoptr (GError) err = NULL;
|
2020-12-18 18:26:31 +02:00
|
|
|
g_autofree gchar *name = NULL;
|
2020-12-11 17:07:20 +02:00
|
|
|
gconstpointer data;
|
|
|
|
|
gsize size;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (L != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (uri != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
file = g_file_new_for_uri (uri);
|
|
|
|
|
if (!(bytes = g_file_load_bytes (file, NULL, NULL, &err))) {
|
2020-12-15 18:28:28 +02:00
|
|
|
g_propagate_prefixed_error (error, err, "Failed to load '%s':", uri);
|
|
|
|
|
err = NULL;
|
2020-12-11 17:07:20 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 18:26:31 +02:00
|
|
|
name = g_path_get_basename (uri);
|
2020-12-11 17:07:20 +02:00
|
|
|
data = g_bytes_get_data (bytes, &size);
|
2022-03-30 18:22:21 +03:00
|
|
|
return _wplua_load_buffer (L, data, size, name, error);
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
2022-03-30 18:22:21 +03:00
|
|
|
wplua_load_path (lua_State * L, const gchar *path, GError **error)
|
2020-12-11 17:07:20 +02:00
|
|
|
{
|
2021-02-02 17:56:42 +02:00
|
|
|
g_autofree gchar *abs_path = NULL;
|
2020-12-11 17:07:20 +02:00
|
|
|
g_autofree gchar *uri = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (L != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (path != NULL, FALSE);
|
|
|
|
|
|
2021-02-02 17:56:42 +02:00
|
|
|
if (!g_path_is_absolute (path)) {
|
|
|
|
|
g_autofree gchar *cwd = g_get_current_dir ();
|
|
|
|
|
abs_path = g_build_filename (cwd, path, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(uri = g_filename_to_uri (abs_path ? abs_path : path, NULL, error)))
|
2020-12-11 17:07:20 +02:00
|
|
|
return FALSE;
|
|
|
|
|
|
2022-03-30 18:22:21 +03:00
|
|
|
return wplua_load_uri (L, uri, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
wplua_pcall (lua_State * L, int nargs, int nres, GError **error)
|
|
|
|
|
{
|
|
|
|
|
int ret = _wplua_pcall (L, nargs, nres);
|
|
|
|
|
if (ret != LUA_OK) {
|
|
|
|
|
g_set_error (error, WP_DOMAIN_LUA, WP_LUA_ERROR_RUNTIME,
|
|
|
|
|
"Lua runtime error");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
2020-12-11 17:07:20 +02:00
|
|
|
}
|