Debian Codegraph @

modules/scripting/perl/perl_module.c

raw
1 /*
2 * Copyright (c) 2010 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Perl scripting module. Provides a basic interface to load
6 * and call into the perl interpreter; the other direction is
7 * handled in the perl API module.
8 */
9
10 #include "api/atheme_perl.h"
11
12 #include "conf.h"
13
14 #include <dlfcn.h>
15
16 DECLARE_MODULE_V1
17 (
18 "scripting/perl", false, _modinit, _moddeinit,
19 "$Id$",
20 VENDOR_STRING
21 );
22
23 /*
24 * Definitions:
25 * PERL_INIT_FILE is the perl script that is used to boot the Atheme interface.
26 */
27 #define PERL_INIT_FILE PERL_MODDIR "/lib/init.pl"
28
29 /*
30 * External functions:
31 * xs_init is defined in the auto-generated perlxsi.c
32 */
33 extern void xs_init(pTHX);
34
35 /*
36 * Required static variables:
37 */
38 static PerlInterpreter *my_perl = NULL;
39 static void *libperl_handle = NULL;
40 static char *_perl_argv[3] = { "", PERL_INIT_FILE, NULL };
41 static char **perl_argv = &_perl_argv[0];
42
43 static char perl_error[512];
44
45 typedef struct {
46 module_t mod;
47 char filename[BUFSIZE];
48 }perl_script_module_t;
49
50 static mowgli_heap_t *perl_script_module_heap;
51
52 static module_t *do_script_load(const char *filename);
53 static bool do_script_unload(const char *filename);
54 static void perl_script_module_unload_handler(module_t *m, module_unload_intent_t intent);
55
56 /*
57 * Startup and shutdown routines.
58 *
59 * These deal with starting and stopping the perl interpreter.
60 */
61 static bool startup_perl(void)
62 {
63 /*
64 * Hack: atheme modules (hence our dependent libperl.so) are loaded with
65 * RTLD_LOCAL, meaning that they're not available for later resolution. Perl
66 * extension modules assume that libperl.so is already loaded and available.
67 * Make it so.
68 *
69 * Secondary hack: some linkers do not respect rpath in dlopen(), so we fall back
70 * to some secondary paths where libperl.so may be living. --nenolod
71 */
72 if (!(libperl_handle = dlopen("libperl.so", RTLD_NOW | RTLD_GLOBAL)) &&
73 !(libperl_handle = dlopen("/usr/lib/perl5/core_perl/CORE/libperl.so", RTLD_NOW | RTLD_GLOBAL)) &&
74 !(libperl_handle = dlopen("/usr/lib64/perl5/core_perl/CORE/libperl.so", RTLD_NOW | RTLD_GLOBAL)))
75 {
76 slog(LG_INFO, "Couldn't dlopen libperl.so");
77 return false;
78 }
79
80 int perl_argc = 2;
81 char **env = NULL;
82 PERL_SYS_INIT3(&perl_argc, &perl_argv, &env);
83
84 if (!(my_perl = perl_alloc()))
85 {
86 slog(LG_INFO, "Couldn't allocate a perl interpreter.");
87 return false;
88 }
89
90 PL_perl_destruct_level = 1;
91 perl_construct(my_perl);
92
93 PL_origalen = 1;
94 int exitstatus = perl_parse(my_perl, xs_init, perl_argc, perl_argv, NULL);
95 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
96
97 if (exitstatus != 0)
98 {
99 slog(LG_INFO, "Couldn't parse perl startup file: %s", SvPV_nolen(ERRSV));
100 return false;
101 }
102
103 exitstatus = perl_run(my_perl);
104
105 if (exitstatus != 0)
106 {
107 slog(LG_INFO, "Couldn't run perl startup file: %s", SvPV_nolen(ERRSV));
108 return false;
109 }
110
111 invalidate_object_references();
112
113 return true;
114 }
115
116 static void shutdown_perl(void)
117 {
118 PL_perl_destruct_level = 1;
119 perl_destruct(my_perl);
120 perl_free(my_perl);
121 my_perl = 0;
122 PERL_SYS_TERM();
123
124 if (libperl_handle)
125 {
126 dlclose(libperl_handle);
127 libperl_handle = NULL;
128 }
129
130 free_object_list();
131 }
132
133 /*
134 * Implementation functions: load or unload a perl script.
135 */
136 static module_t *do_script_load(const char *filename)
137 {
138 /* Remember, this must now be re-entrant. The use of the static
139 * perl_error buffer is still OK, as it's only used immediately after
140 * setting, without control passing from this function.
141 */
142 perl_script_module_t *m = mowgli_heap_alloc(perl_script_module_heap);
143 mowgli_strlcpy(m->filename, filename, sizeof(m->filename));
144
145 snprintf(perl_error, sizeof(perl_error), "Unknown error attempting to load perl script %s",
146 filename);
147
148 dSP;
149 ENTER;
150
151 SAVETMPS;
152 PUSHMARK(SP);
153
154 XPUSHs(newRV_noinc((SV*)get_cv("Atheme::Init::load_script", 0)));
155 XPUSHs(sv_2mortal(newSVpv(filename, 0)));
156 PUTBACK;
157
158 int perl_return_count = call_pv("Atheme::Init::call_wrapper", G_EVAL | G_SCALAR);
159
160 SPAGAIN;
161
162 if (SvTRUE(ERRSV))
163 {
164 mowgli_strlcpy(perl_error, SvPV_nolen(ERRSV), sizeof(perl_error));
165 goto fail;
166 }
167 if (1 != perl_return_count)
168 {
169 snprintf(perl_error, sizeof(perl_error), "Script load didn't return a package name");
170 goto fail;
171 }
172
173 /* load_script should have returned the package name that was just
174 * loaded...
175 */
176 const char *packagename = POPp;
177 char info_varname[BUFSIZE];
178 snprintf(info_varname, BUFSIZE, "%s::Info", packagename);
179
180 /* ... so use that name to grab the script information hash...
181 */
182 HV *info_hash = get_hv(info_varname, 0);
183 if (!info_hash)
184 {
185 snprintf(perl_error, sizeof(perl_error), "Couldn't get package info hash %s", info_varname);
186 goto fail;
187 }
188
189 /* ..., extract the canonical name...
190 */
191 SV **name_var = hv_fetch(info_hash, "name", 4, 0);
192 if (!name_var)
193 {
194 snprintf(perl_error, sizeof(perl_error), "Couldn't find canonical name in package info hash");
195 goto fail;
196 }
197
198 mowgli_strlcpy(m->mod.name, SvPV_nolen(*name_var), sizeof(m->mod.name));
199
200 /* ... and dependency list.
201 */
202 SV **deplist_var = hv_fetch(info_hash, "depends", 7, 0);
203 /* Not declaring this is legal... */
204 if (deplist_var)
205 {
206 /* ... but having it as anything but an arrayref isn't. */
207 if (!SvROK(*deplist_var) || SvTYPE(SvRV(*deplist_var)) != SVt_PVAV)
208 {
209 snprintf(perl_error, sizeof(perl_error), "$Info::depends must be an array reference");
210 goto fail;
211 }
212
213 AV *deplist = (AV*)SvRV(*deplist_var);
214 I32 len = av_len(deplist);
215 /* av_len returns max index, not number of items */
216 for (I32 i = 0; i <= len; ++i)
217 {
218 SV **item = av_fetch(deplist, i, 0);
219 if (!item)
220 continue;
221 const char *dep_name = SvPV_nolen(*item);
222 if (!module_request(dep_name))
223 {
224 snprintf(perl_error, sizeof(perl_error), "Dependent module %s failed to load",
225 dep_name);
226 goto fail;
227 }
228 module_t *dep_mod = module_find_published(dep_name);
229 mowgli_node_add(dep_mod, mowgli_node_create(), &m->mod.deplist);
230 mowgli_node_add(m, mowgli_node_create(), &dep_mod->dephost);
231 }
232 }
233
234 FREETMPS;
235 LEAVE;
236 invalidate_object_references();
237
238 /* Now that everything's loaded, do the module housekeeping stuff. */
239 m->mod.unload_handler = perl_script_module_unload_handler;
240 /* Can't do much better than the address of the module_t here */
241 m->mod.address = m;
242 m->mod.can_unload = MODULE_UNLOAD_CAPABILITY_OK;
243 return (module_t*)m;
244
245 fail:
246 slog(LG_ERROR, "Failed to load Perl script %s: %s", filename, perl_error);
247
248 if (info_hash)
249 SvREFCNT_dec((SV*)info_hash);
250
251 do_script_unload(filename);
252
253 mowgli_heap_free(perl_script_module_heap, m);
254 POPs;
255
256 FREETMPS;
257 LEAVE;
258
259 invalidate_object_references();
260
261 return NULL;
262 }
263
264 static bool do_script_unload(const char *filename)
265 {
266 bool retval = true;
267
268 dSP;
269 ENTER;
270
271 SAVETMPS;
272 PUSHMARK(SP);
273 XPUSHs(sv_2mortal(newSVpv(filename, 0)));
274 PUTBACK;
275
276 call_pv("Atheme::Init::unload_script", G_EVAL | G_DISCARD);
277
278 SPAGAIN;
279
280 if (SvTRUE(ERRSV))
281 {
282 retval = false;
283 mowgli_strlcpy(perl_error, SvPV_nolen(ERRSV), sizeof(perl_error));
284 POPs;
285 }
286
287 FREETMPS;
288 LEAVE;
289
290 invalidate_object_references();
291
292 return retval;
293 }
294
295 static bool do_script_list(sourceinfo_t *si)
296 {
297 bool retval = true;
298
299 dSP;
300 ENTER;
301
302 SAVETMPS;
303 PUSHMARK(SP);
304
305 SV *arg = newSV(0);
306 sv_setref_pv(arg, "Atheme::Sourceinfo", si);
307 XPUSHs(sv_2mortal(arg));
308 PUTBACK;
309
310 call_pv("Atheme::Init::list_scripts", G_EVAL | G_DISCARD);
311
312 SPAGAIN;
313
314 if (SvTRUE(ERRSV))
315 {
316 retval = false;
317 mowgli_strlcpy(perl_error, SvPV_nolen(ERRSV), sizeof(perl_error));
318 POPs;
319 }
320
321 FREETMPS;
322 LEAVE;
323
324 invalidate_object_references();
325
326 return retval;
327 }
328
329 /*
330 * Connect all of the above to OperServ.
331 */
332 static void os_cmd_perl(sourceinfo_t *si, int parc, char *parv[]);
333
334 command_t os_perl = { "PERL", N_("Inspect the Perl interpreter"), PRIV_ADMIN, 2, os_cmd_perl, { .path = "oservice/perl" } };
335
336 static int conf_loadscript(mowgli_config_file_entry_t *);
337
338 static void hook_module_load(hook_module_load_t *data)
339 {
340 struct stat s;
341 char buf[BUFSIZE];
342
343 slog(LG_DEBUG, "Perl hook handler trying to load %s", data->name);
344
345 if (data->module)
346 return;
347
348 if (0 == stat(data->path, &s))
349 {
350 data->handled = 1;
351 data->module = do_script_load(data->path);
352 return;
353 }
354
355 snprintf(buf, BUFSIZE, "%s.pl", data->path);
356 if (0 == stat(buf, &s))
357 {
358 data->handled = 1;
359 data->module = do_script_load(buf);
360 return;
361 }
362
363 snprintf(buf, BUFSIZE, "%s/%s.pl", MODDIR, data->name);
364 if (0 == stat(buf, &s))
365 {
366 data->handled = 1;
367 data->module = do_script_load(buf);
368 return;
369 }
370
371 snprintf(buf, BUFSIZE, "%s/%s.pl", MODDIR "/scripts", data->name);
372 if (0 == stat(buf, &s))
373 {
374 data->handled = 1;
375 data->module = do_script_load(buf);
376 return;
377 }
378
379 snprintf(buf, BUFSIZE, "%s/%s", MODDIR, data->name);
380 if (0 == stat(buf, &s))
381 {
382 data->handled = 1;
383 data->module = do_script_load(buf);
384 return;
385 }
386
387 snprintf(buf, BUFSIZE, "%s/%s", MODDIR "/scripts", data->name);
388 if (0 == stat(buf, &s))
389 {
390 data->handled = 1;
391 data->module = do_script_load(buf);
392 return;
393 }
394 }
395
396 void perl_script_module_unload_handler(module_t *m, module_unload_intent_t intent)
397 {
398 perl_script_module_t *pm = (perl_script_module_t *)m;
399 do_script_unload(pm->filename);
400 mowgli_heap_free(perl_script_module_heap, pm);
401 }
402
403 /*
404 * Module startup/shutdown
405 */
406 void _modinit(module_t *m)
407 {
408 perl_script_module_heap = mowgli_heap_create(sizeof(perl_script_module_t), 256, BH_NOW);
409 if (!perl_script_module_heap)
410 {
411 m->mflags |= MODTYPE_FAIL;
412 return;
413 }
414
415 if (! startup_perl())
416 {
417 m->mflags |= MODTYPE_FAIL;
418 return;
419 }
420
421 service_named_bind_command("operserv", &os_perl);
422
423 hook_add_event("module_load");
424 hook_add_module_load(hook_module_load);
425
426 add_top_conf("LOADSCRIPT", conf_loadscript);
427 }
428
429 void _moddeinit(module_unload_intent_t intent)
430 {
431 service_named_unbind_command("operserv", &os_perl);
432
433 shutdown_perl();
434
435 /* Since all our perl pseudo-modules depend on us, we know they'll
436 * all be deallocated before this. No need to clean them up.
437 */
438 mowgli_heap_destroy(perl_script_module_heap);
439 }
440
441 /*
442 * Actual command handlers.
443 */
444 static void os_cmd_perl(sourceinfo_t *si, int parc, char *parv[])
445 {
446 if (!do_script_list(si))
447 command_fail(si, fault_badparams, _("Failed to retrieve script list: %s"), perl_error);
448 }
449
450 static int conf_loadscript(mowgli_config_file_entry_t *ce)
451 {
452 char pathbuf[4096];
453 char *name;
454
455 if (!cold_start)
456 return 0;
457
458 if (ce->vardata == NULL)
459 {
460 conf_report_warning(ce, "no parameter for configuration option");
461 return 0;
462 }
463
464 name = ce->vardata;
465
466 if (*name == '/')
467 {
468 do_script_load(name);
469 }
470 else
471 {
472 snprintf(pathbuf, 4096, "%s/%s", MODDIR, name);
473 do_script_load(pathbuf);
474 }
475
476 return 0;
477 }