/[baselayout]/trunk/src/rc-plugin.c
Gentoo

Contents of /trunk/src/rc-plugin.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2633 - (show annotations) (download) (as text)
Thu Apr 19 14:41:10 2007 UTC (7 years, 6 months ago) by uberlord
File MIME type: text/x-csrc
File size: 2202 byte(s)
Use RTLD_NOW instead of RTLD_LAZY
1 /*
2 librc-plugin.c
3 Simple plugin handler
4 Copyright 2007 Gentoo Foundation
5 Released under the GPLv2
6 */
7
8 #include <dlfcn.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "einfo.h"
15 #include "rc.h"
16 #include "rc-misc.h"
17 #include "rc-plugin.h"
18 #include "strlist.h"
19
20 typedef struct plugin
21 {
22 char *name;
23 void *handle;
24 int (*hook) (rc_hook_t hook, const char *name);
25 struct plugin *next;
26 } plugin_t;
27
28 static plugin_t *plugins = NULL;
29
30 void rc_plugin_load (void)
31 {
32 char **files;
33 char *file;
34 int i;
35 plugin_t *plugin = plugins;
36
37 /* Ensure some sanity here */
38 rc_plugin_unload ();
39
40 if (! rc_exists (RC_PLUGINDIR))
41 return;
42
43 files = rc_ls_dir (NULL, RC_PLUGINDIR, 0);
44 STRLIST_FOREACH (files, file, i) {
45 char *p = rc_strcatpaths (RC_PLUGINDIR, file, NULL);
46 /*
47 * We load the use RTLD_NOW so that we know it works
48 * as if we have any unknown symbols when we run then the
49 * program bails out in rc_plugin_run which is very very bad.
50 */
51 void *h = dlopen (p, RTLD_NOW);
52 char *func;
53 void *f;
54 int len;
55
56 if (! h) {
57 eerror ("dlopen: %s", dlerror ());
58 free (p);
59 continue;
60 }
61
62 func = file;
63 file = strsep (&func, ".");
64 len = strlen (file) + 7;
65 func = rc_xmalloc (sizeof (char *) * len);
66 snprintf (func, len, "_%s_hook", file);
67
68 f = dlsym (h, func);
69 if (! f) {
70 eerror ("`%s' does not expose the symbol `%s'", p, func);
71 dlclose (h);
72 } else {
73 if (plugin) {
74 plugin->next = rc_xmalloc (sizeof (plugin_t));
75 plugin = plugin->next;
76 } else
77 plugin = plugins = rc_xmalloc (sizeof (plugin_t));
78
79 memset (plugin, 0, sizeof (plugin_t));
80 plugin->name = strdup (file);
81 plugin->handle = h;
82 plugin->hook = f;
83 }
84
85 free (func);
86 free (p);
87 }
88
89 rc_strlist_free (files);
90 }
91
92 void rc_plugin_run (rc_hook_t hook, const char *value)
93 {
94 plugin_t *plugin = plugins;
95
96 while (plugin) {
97 if (plugin->hook)
98 plugin->hook (hook, value);
99
100 plugin = plugin->next;
101 }
102 }
103
104 void rc_plugin_unload (void)
105 {
106 plugin_t *plugin = plugins;
107 plugin_t *next;
108
109 while (plugin) {
110 next = plugin->next;
111 dlclose (plugin->handle);
112 free (plugin->name);
113 free (plugin);
114 plugin = next;
115 }
116 plugins = NULL;
117 }

  ViewVC Help
Powered by ViewVC 1.1.20