Debian Codegraph @

modules/module-lua-scripting/script.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "script.h"
10 #include <pipewire/keys.h>
11
12 #define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
13 WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
14
15 /*
16 * This is a WpPlugin subclass that wraps a single lua script and acts like
17 * a handle for that script. When enabled, through the WpObject activation
18 * mechanism, the script is executed. It then provides an API for the script
19 * to declare when it has finished its activation procedure, which can be
20 * asynchronous (this is Script.finish_activation in Lua).
21 * When disabled, this class destroys the global environment that was used
22 * in the Lua engine for executing that script, effectively destroying all
23 * objects that were held in Lua as global variables.
24 */
25
26 struct _WpLuaScript
27 {
28 WpPlugin parent;
29
30 lua_State *L;
31 gchar *filename;
32 WpSpaJson *args;
33 };
34
35 enum {
36 PROP_0,
37 PROP_LUA_ENGINE,
38 PROP_FILENAME,
39 PROP_ARGUMENTS,
40 };
41
42 G_DEFINE_TYPE (WpLuaScript, wp_lua_script, WP_TYPE_PLUGIN)
43
44 static void
45 wp_lua_script_init (WpLuaScript * self)
46 {
47 }
48
49 static void
50 wp_lua_script_cleanup (WpLuaScript * self)
51 {
52 /* LUA_REGISTRYINDEX[self] = nil */
53 if (self->L) {
54 lua_pushnil (self->L);
55 lua_rawsetp (self->L, LUA_REGISTRYINDEX, self);
56 }
57 }
58
59 static void
60 wp_lua_script_finalize (GObject * object)
61 {
62 WpLuaScript *self = WP_LUA_SCRIPT (object);
63
64 wp_lua_script_cleanup (self);
65 g_clear_pointer (&self->L, wplua_unref);
66 g_clear_pointer (&self->filename, g_free);
67 g_clear_pointer (&self->args, wp_spa_json_unref);
68
69 G_OBJECT_CLASS (wp_lua_script_parent_class)->finalize (object);
70 }
71
72 static void
73 wp_lua_script_set_property (GObject * object, guint property_id,
74 const GValue * value, GParamSpec * pspec)
75 {
76 WpLuaScript *self = WP_LUA_SCRIPT (object);
77
78 switch (property_id) {
79 case PROP_LUA_ENGINE:
80 g_return_if_fail (self->L == NULL);
81 self->L = g_value_get_pointer (value);
82 if (self->L)
83 self->L = wplua_ref (self->L);
84 break;
85 case PROP_FILENAME:
86 self->filename = g_value_dup_string (value);
87 break;
88 case PROP_ARGUMENTS:
89 self->args = g_value_dup_boxed (value);
90 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93 break;
94 }
95 }
96
97 static gboolean
98 wp_lua_script_check_async_activation (WpLuaScript * self)
99 {
100 gboolean ret;
101 lua_rawgetp (self->L, LUA_REGISTRYINDEX, self);
102 lua_pushliteral (self->L, "Script");
103 lua_gettable (self->L, -2);
104 lua_pushliteral (self->L, "async_activation");
105 lua_gettable (self->L, -2);
106 ret = lua_toboolean (self->L, -1);
107 lua_pop (self->L, 3);
108 return ret;
109 }
110
111 static void
112 wp_lua_script_detach_transition (WpLuaScript * self)
113 {
114 lua_rawgetp (self->L, LUA_REGISTRYINDEX, self);
115 lua_pushliteral (self->L, "Script");
116 lua_gettable (self->L, -2);
117 lua_pushliteral (self->L, "__transition");
118 lua_pushnil (self->L);
119 lua_settable (self->L, -3);
120 lua_pop (self->L, 2);
121 }
122
123 static int
124 script_finish_activation (lua_State * L)
125 {
126 WpLuaScript *self;
127
128 luaL_checktype (L, 1, LUA_TTABLE);
129
130 lua_pushliteral (L, "__self");
131 lua_gettable (L, 1);
132 luaL_checktype (L, -1, LUA_TLIGHTUSERDATA);
133 self = WP_LUA_SCRIPT ((gpointer) lua_topointer (L, -1));
134 lua_pop (L, 2);
135
136 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
137 return 0;
138 }
139
140 static int
141 script_finish_activation_with_error (lua_State * L)
142 {
143 WpTransition *transition = NULL;
144 const char *msg = NULL;
145
146 luaL_checktype (L, 1, LUA_TTABLE);
147 msg = luaL_checkstring (L, 2);
148
149 lua_pushliteral (L, "__transition");
150 lua_gettable (L, 1);
151 if (lua_type (L, -1) == LUA_TLIGHTUSERDATA)
152 transition = WP_TRANSITION ((gpointer) lua_topointer (L, -1));
153 lua_pop (L, 2);
154
155 if (transition)
156 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
157 WP_LIBRARY_ERROR_OPERATION_FAILED, "%s", msg));
158 return 0;
159 }
160
161 static const luaL_Reg script_api_methods[] = {
162 { "finish_activation", script_finish_activation },
163 { "finish_activation_with_error", script_finish_activation_with_error },
164 { NULL, NULL }
165 };
166
167 static int
168 wp_lua_script_sandbox (lua_State *L)
169 {
170 luaL_checktype (L, 1, LUA_TLIGHTUSERDATA); // self
171 luaL_checktype (L, 2, LUA_TLIGHTUSERDATA); // transition
172 luaL_checktype (L, 3, LUA_TFUNCTION); // the script chunk
173
174 /* create unique environment for this script */
175 lua_getglobal (L, "create_sandbox_env");
176 lua_call (L, 0, 1);
177
178 /* create "Script" API */
179 lua_pushliteral (L, "Script");
180 luaL_newlib (L, script_api_methods);
181 lua_pushliteral (L, "__self");
182 lua_pushvalue (L, 1);
183 lua_settable (L, -3);
184 lua_pushliteral (L, "__transition");
185 lua_pushvalue (L, 2);
186 lua_settable (L, -3);
187 lua_settable (L, -3);
188
189 /* store the environment */
190 /* LUA_REGISTRYINDEX[self] = env */
191 lua_pushvalue (L, 1); // self
192 lua_pushvalue (L, -2); // the table returned by create_sandbox_env
193 lua_rawset (L, LUA_REGISTRYINDEX);
194
195 /* set it as the 1st upvalue (_ENV) on the loaded script chunk (at index 3) */
196 lua_setupvalue (L, 3, 1);
197
198 /* anything remaining on the stack are function arguments */
199 int nargs = lua_gettop (L) - 3;
200
201 /* execute script */
202 lua_call (L, nargs, 0);
203 return 0;
204 }
205
206 static void
207 wp_lua_script_enable (WpPlugin * plugin, WpTransition * transition)
208 {
209 WpLuaScript *self = WP_LUA_SCRIPT (plugin);
210 g_autoptr (GError) error = NULL;
211 int top, nargs = 3;
212
213 if (!self->L) {
214 error = g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
215 "No lua state open; lua-scripting plugin is not enabled");
216 wp_transition_return_error (transition, g_steal_pointer (&error));
217 return;
218 }
219
220 top = lua_gettop (self->L);
221 lua_pushcfunction (self->L, wp_lua_script_sandbox);
222 lua_pushlightuserdata (self->L, self);
223 lua_pushlightuserdata (self->L, transition);
224
225 /* load script */
226 if (!wplua_load_path (self->L, self->filename, &error)) {
227 lua_settop (self->L, top);
228 wp_transition_return_error (transition, g_steal_pointer (&error));
229 return;
230 }
231
232 /* push script arguments */
233 if (self->args) {
234 wplua_pushboxed (self->L, WP_TYPE_SPA_JSON, wp_spa_json_ref (self->args));
235 nargs++;
236 }
237
238 /* execute script */
239 if (!wplua_pcall (self->L, nargs, 0, &error)) {
240 lua_settop (self->L, top);
241 wp_transition_return_error (transition, g_steal_pointer (&error));
242 wp_lua_script_cleanup (self);
243 return;
244 }
245
246 if (!wp_lua_script_check_async_activation (self)) {
247 wp_lua_script_detach_transition (self);
248 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
249 } else {
250 g_signal_connect_object (transition, "notify::completed",
251 (GCallback) wp_lua_script_detach_transition, self, G_CONNECT_SWAPPED);
252 }
253
254 lua_settop (self->L, top);
255 }
256
257 static void
258 wp_lua_script_disable (WpPlugin * plugin)
259 {
260 WpLuaScript *self = WP_LUA_SCRIPT (plugin);
261 wp_lua_script_cleanup (self);
262 }
263
264 static void
265 wp_lua_script_class_init (WpLuaScriptClass * klass)
266 {
267 GObjectClass *object_class = (GObjectClass *) klass;
268 WpPluginClass *plugin_class = (WpPluginClass *) klass;
269
270 object_class->finalize = wp_lua_script_finalize;
271 object_class->set_property = wp_lua_script_set_property;
272
273 plugin_class->enable = wp_lua_script_enable;
274 plugin_class->disable = wp_lua_script_disable;
275
276 g_object_class_install_property (object_class, PROP_LUA_ENGINE,
277 g_param_spec_pointer ("lua-engine", "lua-engine", "lua-engine",
278 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
279
280 g_object_class_install_property (object_class, PROP_FILENAME,
281 g_param_spec_string ("filename", "filename", "filename", NULL,
282 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
283
284 g_object_class_install_property (object_class, PROP_ARGUMENTS,
285 g_param_spec_boxed ("arguments", "arguments", "arguments",
286 WP_TYPE_SPA_JSON,
287 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
288 }