summaryrefslogtreecommitdiffstats
path: root/agents/virt/server/plugin.c
blob: d9a69fb56c86d7c4620b8c32c6dfa341befd8f9a (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
  Copyright Red Hat, Inc. 2002-2004, 2009

  The Magma Cluster API Library is free software; you can redistribute
  it and/or modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either version
  2.1 of the License, or (at your option) any later version.

  The Magma Cluster API Library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
 */
/** @file
 * Plugin loading routines
 */

#include "config.h"

#include <dlfcn.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <malloc.h>
#include <string.h>
#include <dirent.h>

#include "list.h"
#include "simpleconfig.h"
#include "static_map.h"
#include "server_plugin.h"
#include "debug.h"

typedef struct _plugin_list {
	list_head();
	const listener_plugin_t *listener;
	const backend_plugin_t *backend;
	void *handle;
	plugin_type_t type;
} plugin_list_t;

static plugin_list_t *server_plugins = NULL;


static int
plugin_reg_backend(void *handle, const backend_plugin_t *plugin)
{
	plugin_list_t *newplug;

	if (plugin_find_backend(plugin->name)) {
		errno = EEXIST;
		return -1;
	}

	newplug = malloc(sizeof(*newplug));
	if (!newplug) 
		return -1;
	memset(newplug, 0, sizeof(*newplug));
	newplug->backend = plugin;
	newplug->type = PLUGIN_BACKEND;
	newplug->handle = handle;

	list_insert(&server_plugins, newplug);
	return 0;
}


static int
plugin_reg_listener(void *handle, const listener_plugin_t *plugin)
{
	plugin_list_t *newplug;

	if (plugin_find_listener(plugin->name)) {
		errno = EEXIST;
		return -1;
	}

	newplug = malloc(sizeof(*newplug));
	if (!newplug) 
		return -1;
	memset(newplug, 0, sizeof(*newplug));
	newplug->listener = plugin;
	newplug->type = PLUGIN_LISTENER;
	newplug->handle = handle;

	list_insert(&server_plugins, newplug);
	return 0;
}


void
plugin_dump(void)
{
	plugin_list_t *p;
	int x, y;

	y = 0;
	list_for(&server_plugins, p, x) {
		if (p->type == PLUGIN_BACKEND) {
			if (!y) {
				y = 1;
				printf("Available backends:\n");
			}
			printf("    %s %s\n",
			       p->backend->name, p->backend->version);
		}
	}

	y = 0;
	list_for(&server_plugins, p, x) {
		if (p->type == PLUGIN_LISTENER) {
			if (!y) {
				y = 1;
				printf("Available listeners:\n");
			}
			printf("    %s %s\n",
			       p->listener->name, p->listener->version);
		}
	}
}


const backend_plugin_t *
plugin_find_backend(const char *name)
{
	plugin_list_t *p;
	int x;

	list_for(&server_plugins, p, x) {
		if (p->type != PLUGIN_BACKEND)
			continue;
		if (!strcasecmp(name, p->backend->name))
			return p->backend;
	}

	return NULL;
}


const listener_plugin_t *
plugin_find_listener(const char *name)
{
	plugin_list_t *p;
	int x;

	list_for(&server_plugins, p, x) {
		if (p->type != PLUGIN_LISTENER)
			continue;
		if (!strcasecmp(name, p->listener->name))
			return p->listener;
	}

	return NULL;
}


static int
backend_plugin_load(void *handle, const char *libpath)
{
	const backend_plugin_t *plug = NULL;
	double (*modversion)(void);
	backend_plugin_t *(*modinfo)(void);

	modversion = dlsym(handle, BACKEND_VER_STR);
	if (!modversion) {
		dbg_printf(1, "Failed to map %s\n", BACKEND_VER_STR);
		errno = EINVAL;
		return -1;
	}

	if (modversion() != PLUGIN_VERSION_BACKEND) {
		dbg_printf(1, "API version mismatch in %s: \n"
			   "       %f expected; %f received.\n", libpath,
			   PLUGIN_VERSION_BACKEND, modversion());
		errno = EINVAL;
		return -1;
	}

	modinfo = dlsym(handle, BACKEND_INFO_STR);
	if (!modinfo) {
		dbg_printf(1, "Failed to map %s\n", BACKEND_INFO_STR);
		errno = EINVAL;
		return -1;
	}

	plug = modinfo();
	if (plugin_reg_backend(handle, plug) < 0) {
		dbg_printf(1, "Failed to register %s %s\n", plug->name,
			   plug->version);
		errno = EINVAL;
		return -1;
	} else {
		dbg_printf(1, "Registered backend plugin %s %s\n",
			   plug->name, plug->version);
	}

	return 0;
}


static int
listener_plugin_load(void *handle, const char *libpath)
{
	const listener_plugin_t *plug = NULL;
	double (*modversion)(void);
	listener_plugin_t *(*modinfo)(void);

	modversion = dlsym(handle, LISTENER_VER_STR);
	if (!modversion) {
		dbg_printf(1, "Failed to map %s\n", LISTENER_VER_STR);
		errno = EINVAL;
		return -1;
	}

	if (modversion() != PLUGIN_VERSION_LISTENER) {
		dbg_printf(1, "API version mismatch in %s: \n"
		       	   "       %f expected; %f received.\n", libpath,
			   PLUGIN_VERSION_LISTENER, modversion());
		dlclose(handle);
		errno = EINVAL;
		return -1;
	}

	modinfo = dlsym(handle, LISTENER_INFO_STR);
	if (!modinfo) {
		dbg_printf(1, "Failed to map %s\n", LISTENER_INFO_STR);
		errno = EINVAL;
		return -1;
	}

	plug = modinfo();
	if (plugin_reg_listener(handle, plug) < 0) {
		dbg_printf(1, "Failed to register %s %s\n", plug->name,
			   plug->version);
		errno = EINVAL;
		return -1;
	} else {
		dbg_printf(1, "Registered listener plugin %s %s\n",
			   plug->name, plug->version);
	}

	return 0;
}


/**
 * Load a cluster plugin .so file and map all the functions
 * provided to entries in a backend_plugin_t structure.
 *
 * @param libpath	Path to file.
 * @return		NULL on failure, or plugin-specific
 * 			(const) backend_plugin_t * structure on
 * 			success.
 */
int
plugin_load(const char *libpath)
{
	void *handle = NULL;

	errno = 0;

	if (!libpath) {
		errno = EINVAL;
		return -1;
	}

	dbg_printf(3, "Loading plugin from %s\n", libpath);
	handle = dlopen(libpath, RTLD_NOW);
	if (!handle) {
		dbg_printf(3, "Could not dlopen %s: %s\n", libpath, dlerror());
		errno = ELIBACC;
		return -1;
	}

	if (!backend_plugin_load(handle, libpath) ||
	    !listener_plugin_load(handle, libpath))
		return 0;

	dbg_printf(3, "%s is not a valid plugin\n", libpath);
	dlclose(handle);
	errno = EINVAL;
	return -1;
}

void
plugin_unload(void)
{
	plugin_list_t *p;
	int x;

	list_for(&server_plugins, p, x) {
		dlclose(p->handle);
	}
}

/**
  Free up a null-terminated array of strings
 */
static void
free_dirnames(char **dirnames)
{
	int x = 0;

	for (; dirnames[x]; x++)
		free(dirnames[x]);

	free(dirnames);
}


static int
_compare(const void *a, const void *b)
{
	return strcmp((const char *)a, (const char *)b);
}


/**
  Read all entries in a directory and return them in a NULL-terminated,
  sorted array.
 */
static int
read_dirnames_sorted(const char *directory, char ***dirnames)
{
	DIR *dir;
	struct dirent *entry;
	char filename[1024];
	int count = 0, x = 0;

	dir = opendir(directory);
	if (!dir)
		return -1;

	/* Count the number of plugins */
	while ((entry = readdir(dir)) != NULL)
		++count;

	/* Malloc the entries */
	*dirnames = malloc(sizeof(char *) * (count+1));
	if (!*dirnames) {
#ifdef DEBUG
		printf("%s: Failed to malloc %d bytes",
		       __FUNCTION__, (int)(sizeof(char *) * (count+1)));
#endif
		closedir(dir);
		errno = ENOMEM;
		return -1;
	}
	memset(*dirnames, 0, sizeof(char *) * (count + 1));
	rewinddir(dir);

	/* Store the directory names. */
	while ((entry = readdir(dir)) != NULL) {
		snprintf(filename, sizeof(filename), "%s/%s", directory,
			 entry->d_name);

		(*dirnames)[x] = strdup(filename);
		if (!(*dirnames)[x]) {
#ifdef DEBUG
			printf("Failed to duplicate %s\n",
			       filename);
#endif
			free_dirnames(*dirnames);
			closedir(dir);
			errno = ENOMEM;
			return -1;
		}
		++x;
	}

	closedir(dir);

	/* Sort the directory names. */
	qsort((*dirnames), count, sizeof(char *), _compare);

	return 0;
}


/**
 */
int
plugin_search(const char *pathname)
{
	int found = 0;
	int fcount = 0;
	char **filenames;

	dbg_printf(1, "Searching for plugins in %s\n", pathname);
	if (read_dirnames_sorted(pathname, &filenames) != 0) {
		return -1;
	}

	for (fcount = 0; filenames[fcount]; fcount++) {

		if (plugin_load(filenames[fcount]) == 0)
			++found;
	}

	free_dirnames(filenames);
	if (!found) {
		dbg_printf(1, "No usable plugins found.\n");
		errno = ELIBACC;
		return -1;
	}

	return found;
}