1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* GKrellM
| Copyright (C) 1999-2021 Bill Wilson
|
| Author: Bill Wilson
| Latest versions might be found at: https://gkrellm.srcbox.net
|
|
| GKrellM is free software: you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by
| the Free Software Foundation, either version 3 of the License, or
| (at your option) any later version.
|
| GKrellM is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
| or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
| License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program. If not, see http://www.gnu.org/licenses/
|
|
| Additional permission under GNU GPL version 3 section 7
|
| If you modify this program, or any covered work, by linking or
| combining it with the OpenSSL project's OpenSSL library (or a
| modified version of that library), containing parts covered by
| the terms of the OpenSSL or SSLeay licenses, you are granted
| additional permission to convey the resulting work.
| Corresponding Source for a non-source form of such a combination
| shall include the source code for the parts of OpenSSL used as well
| as that of the covered work.
*/
#include "gkrellmd.h"
#include "gkrellmd-private.h"
#include <gmodule.h>
static GList *plugin_list;
GList *gkrellmd_plugin_enable_list;
gchar *plugin_install_log;
/* ======================================================================= */
/* Plugin interface to gkrellmd.
*/
static void
gkrellmd_plugin_log(gchar *string1, ...)
{
va_list args;
gchar *s, *old_log;
if (!string1)
return;
va_start(args, string1);
s = string1;
while (s)
{
old_log = plugin_install_log;
if (plugin_install_log)
plugin_install_log = g_strconcat(plugin_install_log, s, NULL);
else
plugin_install_log = g_strconcat(s, NULL);
g_free(old_log);
s = va_arg(args, gchar *);
}
va_end(args);
}
static GkrellmdMonitor *
gkrellmd_plugin_install(gchar *plugin_name)
{
GModule *module;
GkrellmdMonitor *m;
GkrellmdMonitor *(*init_plugin)(void);
gchar buf[256];
if (!g_module_supported())
return NULL;
gkrellm_debug(DEBUG_PLUGIN, "Loading plugin %s\n", plugin_name);
module = g_module_open(plugin_name, 0);
gkrellmd_plugin_log(plugin_name, "\n", NULL);
if (! module)
{
snprintf(buf, sizeof(buf), _("\tError: %s\n"), g_module_error());
gkrellmd_plugin_log(buf, NULL);
return NULL;
}
if (!g_module_symbol(module, "gkrellmd_init_plugin",
(gpointer) &init_plugin))
{
snprintf(buf, sizeof(buf), _("\tError: %s\n"), g_module_error());
gkrellmd_plugin_log(buf, NULL);
g_module_close(module);
return NULL;
}
#if defined(WIN32)
// Detect old plugins and skip/warn about them
gpointer plugin_cb = NULL;
if (g_module_symbol(module, "cb", &plugin_cb))
{
gkrellmd_plugin_log("\tSkipping outdated plugin\n", NULL);
g_module_close(module);
return NULL;
}
#endif
m = (*init_plugin)();
if (m == NULL)
{
gkrellmd_plugin_log(
_("\tOoops! plugin returned NULL, not installng\n"), NULL);
g_module_close(module);
return NULL;
}
gkrellmd_plugin_log(_("\tInstalled OK\n"), NULL);
if (!m->privat) /* may get set in gkrellm_config_getline() */
{
m->privat = g_new0(GkrellmdMonitorPrivate, 1);
m->privat->config_list = gkrellmd_plugin_config_list;
}
m->privat->handle = module;
m->privat->path = plugin_name;
if (!m->name)
m->name = g_path_get_basename(m->privat->path);
return m;
}
#if defined(WIN32)
static const char *plugin_suffix = ".dll";
#else // macOS/Darwin also uses .so for historic reasons
static const char *plugin_suffix = ".so";
#endif
static gboolean
gkrellmd_plugin_enabled(gchar *name)
{
GList *list;
gchar *check, *s;
gint len;
for (list = gkrellmd_plugin_enable_list; list; list = list->next)
{
check = (gchar *) list->data;
s = strstr(name, check);
len = strlen(check);
if ( s
&& s == name
&& (*(name + len) == '\0' || !strcmp(name + len, plugin_suffix))
)
return TRUE;
}
return FALSE;
}
static void
gkrellmd_plugin_scan(gchar *path)
{
gchar *name;
GList *list;
GkrellmdMonitor *m = NULL;
gchar *s;
gboolean exists;
if (!path || !*path)
return
gkrellm_debug(DEBUG_PLUGIN, "Searching for plugins in %s\n", path);
GDir *dir = g_dir_open(path, 0, NULL);
if (!dir)
return;
while ((name = (gchar *) g_dir_read_name(dir)) != NULL)
{
if (!g_str_has_suffix(name, plugin_suffix))
continue;
if (_GK.list_plugins)
{
// TODO: list plugins support on win32
g_message("%s (%s)\n", name, path);
continue;
}
for (list = plugin_list; list; list = list->next)
{
m = (GkrellmdMonitor *) list->data;
s = g_path_get_basename(m->privat->path);
exists = !strcmp(s, name);
g_free(s);
if (exists)
break;
m = NULL;
}
s = g_strconcat(path, G_DIR_SEPARATOR_S, name, NULL);
if (m)
{
gkrellmd_plugin_log(_("Ignoring duplicate plugin "), s, "\n", NULL);
g_free(s);
continue;
}
if (!gkrellmd_plugin_enabled(name))
{
gkrellmd_plugin_log(s, "\n",
"\tNot enabled in gkrellmd.conf - skipping\n", NULL);
continue;
}
m = gkrellmd_plugin_install(s);
if (m) /* s is saved for use */
plugin_list = g_list_append(plugin_list, m);
else
g_free(s);
}
g_dir_close(dir);
}
GList *
gkrellmd_plugins_load(void)
{
GkrellmdMonitor *m;
gchar *path;
if (_GK.command_line_plugin)
{
if ( *_GK.command_line_plugin != '.'
&& !strchr(_GK.command_line_plugin, G_DIR_SEPARATOR)
)
path = g_strconcat(".", G_DIR_SEPARATOR_S,
_GK.command_line_plugin, NULL);
else
path = g_strdup(_GK.command_line_plugin);
gkrellmd_plugin_log(_("*** Command line plugin:\n"), NULL);
if ((m = gkrellmd_plugin_install(path)) == NULL)
g_free(path);
else
plugin_list = g_list_append(plugin_list, m);
gkrellmd_plugin_log("\n", NULL);
}
const char *homedir = g_get_home_dir();
if (homedir)
{
path = g_build_filename(homedir, GKRELLMD_PLUGINS_DIR, NULL);
gkrellmd_plugin_scan(path);
g_free(path);
}
#if defined(WIN32)
gchar *install_path;
install_path = g_win32_get_package_installation_directory_of_module(NULL);
if (install_path != NULL)
{
path = g_build_filename(install_path, "lib", "gkrellm2", "plugins-gkrellmd", NULL);
gkrellmd_plugin_scan(path);
g_free(path);
g_free(install_path);
}
#endif
#if defined(GKRELLMD_LOCAL_PLUGINS_DIR)
gkrellmd_plugin_scan(GKRELLMD_LOCAL_PLUGINS_DIR);
#endif
#if defined(GKRELLMD_SYSTEM_PLUGINS_DIR)
gkrellmd_plugin_scan(GKRELLMD_SYSTEM_PLUGINS_DIR);
#endif
return plugin_list;
}
|