2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
librc-plugin.c
|
|
|
|
Simple plugin handler
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
Released under the GPLv2
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
2007-04-20 15:09:47 +05:30
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2007-08-09 20:03:20 +05:30
|
|
|
#include <limits.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc.h"
|
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "rc-plugin.h"
|
|
|
|
#include "strlist.h"
|
|
|
|
|
2007-07-04 21:32:01 +05:30
|
|
|
bool rc_in_plugin = false;
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
typedef struct plugin
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *name;
|
|
|
|
void *handle;
|
2007-05-14 22:35:55 +05:30
|
|
|
int (*hook) (rc_hook_t, const char *);
|
2007-04-11 18:14:47 +05:30
|
|
|
struct plugin *next;
|
2007-04-05 16:48:42 +05:30
|
|
|
} plugin_t;
|
|
|
|
|
|
|
|
static plugin_t *plugins = NULL;
|
|
|
|
|
2007-05-15 01:20:13 +05:30
|
|
|
#ifndef __FreeBSD__
|
|
|
|
dlfunc_t dlfunc (void * __restrict handle, const char * __restrict symbol)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
void *d;
|
|
|
|
dlfunc_t f;
|
|
|
|
} rv;
|
|
|
|
|
|
|
|
rv.d = dlsym (handle, symbol);
|
|
|
|
return (rv.f);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
void rc_plugin_load (void)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char **files;
|
|
|
|
char *file;
|
|
|
|
int i;
|
|
|
|
plugin_t *plugin = plugins;
|
|
|
|
|
2007-07-04 21:32:01 +05:30
|
|
|
/* Don't load plugins if we're in one */
|
|
|
|
if (rc_in_plugin)
|
|
|
|
return;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
/* Ensure some sanity here */
|
|
|
|
rc_plugin_unload ();
|
|
|
|
|
|
|
|
if (! rc_exists (RC_PLUGINDIR))
|
|
|
|
return;
|
|
|
|
|
|
|
|
files = rc_ls_dir (NULL, RC_PLUGINDIR, 0);
|
|
|
|
STRLIST_FOREACH (files, file, i) {
|
|
|
|
char *p = rc_strcatpaths (RC_PLUGINDIR, file, NULL);
|
2007-04-20 15:09:47 +05:30
|
|
|
void *h = dlopen (p, RTLD_LAZY);
|
2007-04-11 18:14:47 +05:30
|
|
|
char *func;
|
2007-05-14 22:35:55 +05:30
|
|
|
int (*fptr) (rc_hook_t, const char *);
|
2007-04-11 18:14:47 +05:30
|
|
|
int len;
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
free (p);
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! h) {
|
2007-04-19 20:11:10 +05:30
|
|
|
eerror ("dlopen: %s", dlerror ());
|
2007-04-11 18:14:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
func = file;
|
|
|
|
file = strsep (&func, ".");
|
|
|
|
len = strlen (file) + 7;
|
|
|
|
func = rc_xmalloc (sizeof (char *) * len);
|
|
|
|
snprintf (func, len, "_%s_hook", file);
|
|
|
|
|
2007-05-14 22:35:55 +05:30
|
|
|
fptr = (int (*)(rc_hook_t, const char*)) dlfunc (h, func);
|
|
|
|
if (! fptr) {
|
2007-04-11 18:14:47 +05:30
|
|
|
eerror ("`%s' does not expose the symbol `%s'", p, func);
|
|
|
|
dlclose (h);
|
|
|
|
} else {
|
|
|
|
if (plugin) {
|
|
|
|
plugin->next = rc_xmalloc (sizeof (plugin_t));
|
|
|
|
plugin = plugin->next;
|
|
|
|
} else
|
|
|
|
plugin = plugins = rc_xmalloc (sizeof (plugin_t));
|
|
|
|
|
|
|
|
memset (plugin, 0, sizeof (plugin_t));
|
2007-04-19 20:24:35 +05:30
|
|
|
plugin->name = rc_xstrdup (file);
|
2007-04-11 18:14:47 +05:30
|
|
|
plugin->handle = h;
|
2007-05-14 22:35:55 +05:30
|
|
|
plugin->hook = fptr;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
free (func);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (files);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void rc_plugin_run (rc_hook_t hook, const char *value)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
plugin_t *plugin = plugins;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-04 21:32:01 +05:30
|
|
|
/* Don't run plugins if we're in one */
|
|
|
|
if (rc_in_plugin)
|
|
|
|
return;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
while (plugin) {
|
2007-04-20 15:09:47 +05:30
|
|
|
if (plugin->hook) {
|
|
|
|
int i;
|
|
|
|
int flags;
|
|
|
|
int pfd[2];
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* We create a pipe so that plugins can affect our environment
|
|
|
|
* vars, which in turn influence our scripts. */
|
|
|
|
if (pipe (pfd) == -1) {
|
|
|
|
eerror ("pipe: %s", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop any scripts from inheriting us.
|
|
|
|
* This is actually quite important as without this, the splash
|
|
|
|
* plugin will probably hang when running in silent mode. */
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
if ((flags = fcntl (pfd[i], F_GETFD, 0)) < 0 ||
|
|
|
|
fcntl (pfd[i], F_SETFD, flags | FD_CLOEXEC) < 0)
|
|
|
|
eerror ("fcntl: %s", strerror (errno));
|
|
|
|
|
|
|
|
/* We run the plugin in a new process so we never crash
|
|
|
|
* or otherwise affected by it */
|
|
|
|
if ((pid = fork ()) == -1) {
|
|
|
|
eerror ("fork: %s", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
int retval;
|
|
|
|
|
2007-07-04 21:32:01 +05:30
|
|
|
rc_in_plugin = true;
|
2007-04-20 15:09:47 +05:30
|
|
|
close (pfd[0]);
|
|
|
|
rc_environ_fd = fdopen (pfd[1], "w");
|
|
|
|
retval = plugin->hook (hook, value);
|
|
|
|
fclose (rc_environ_fd);
|
|
|
|
rc_environ_fd = NULL;
|
2007-07-05 14:09:32 +05:30
|
|
|
|
|
|
|
/* Just in case the plugin sets this to false */
|
|
|
|
rc_in_plugin = true;
|
2007-07-04 21:32:01 +05:30
|
|
|
exit (retval);
|
2007-04-20 15:09:47 +05:30
|
|
|
} else {
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
char *token;
|
|
|
|
char *p;
|
2007-04-25 21:18:54 +05:30
|
|
|
ssize_t nr;
|
2007-04-20 15:09:47 +05:30
|
|
|
|
|
|
|
close (pfd[1]);
|
|
|
|
memset (buffer, 0, sizeof (buffer));
|
|
|
|
|
2007-04-25 21:18:54 +05:30
|
|
|
while ((nr = read (pfd[0], buffer, sizeof (buffer))) > 0) {
|
2007-04-20 15:09:47 +05:30
|
|
|
p = buffer;
|
2007-04-25 21:18:54 +05:30
|
|
|
while (*p && p - buffer < nr) {
|
|
|
|
token = strsep (&p, "=");
|
|
|
|
if (token) {
|
|
|
|
unsetenv (token);
|
|
|
|
if (*p) {
|
|
|
|
setenv (token, p, 1);
|
|
|
|
p += strlen (p) + 1;
|
|
|
|
} else
|
|
|
|
p++;
|
|
|
|
}
|
2007-04-20 15:09:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close (pfd[0]);
|
|
|
|
}
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
plugin = plugin->next;
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void rc_plugin_unload (void)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
plugin_t *plugin = plugins;
|
|
|
|
plugin_t *next;
|
|
|
|
|
|
|
|
while (plugin) {
|
|
|
|
next = plugin->next;
|
|
|
|
dlclose (plugin->handle);
|
|
|
|
free (plugin->name);
|
|
|
|
free (plugin);
|
|
|
|
plugin = next;
|
|
|
|
}
|
|
|
|
plugins = NULL;
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|