aboutsummaryrefslogtreecommitdiff
path: root/src/module.c
blob: a40182f60b1212c6f87fed037619a386d2ce4572 (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
/* This file is part of Smap.
   Copyright (C) 2006, 2007, 2010 Sergey Poznyakoff

   Smap 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, or (at your option)
   any later version.

   Smap 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 Smap.  If not, see <http://www.gnu.org/licenses/>. */

#include "smapd.h"

char *mod_load_path;

void
add_load_path(const char *path)
{
	if (path[0] == ':')
		path++;
	if (mod_load_path) {
		mod_load_path = erealloc(mod_load_path,
					 strlen(mod_load_path) +
					 strlen(path) + 2);
		strcat(mod_load_path, ":");
		strcat(mod_load_path, path);
	} else
		mod_load_path = estrdup(path);
}

void
smap_modules_init()
{
	lt_dlinit();

	debug(DBG_MODULE, 1,
	      ("adding %s to the load path", SMAP_MODDIR));
	lt_dladdsearchdir(SMAP_MODDIR);

	if (mod_load_path) {
		int i;
		struct wordsplit ws;
		ws.ws_delim = ":";
		ws.ws_error = smap_error;

		if (wordsplit(mod_load_path, &ws,
			      WRDSF_NOCMD | WRDSF_NOVAR |
			      WRDSF_ENOMEMABRT | WRDSF_SQUEEZE_DELIMS |
			      WRDSF_DELIM | WRDSF_ERROR)) {
			smap_error("cannot parse load path: %s",
				   strerror(errno));
			return;
		}

		for (i = 0; i < ws.ws_wordc; i++) {
			if (ws.ws_wordv[i][0]) {
				debug(DBG_MODULE, 1,
				      ("adding %s to the load path",
				       ws.ws_wordv[i]));
				if (lt_dladdsearchdir(ws.ws_wordv[i])) {
					smap_error("cannot add `%s' to "
						   "the load path: %s",
						   ws.ws_wordv[i],
						   lt_dlerror());
				}
			}
		}
		wordsplit_free(&ws);
	}
}



DCLIDLIST(module, smap_module_instance)

void remove_dependent_databases(const char *id);

int
module_declare(const char *file, unsigned line,
	       const char *id, int argc, char **argv,
	       struct smap_module_instance **pmod)
{
	int i;
	struct smap_module_instance *mip;

	mip = module_locate(id);
	if (mip) {
		*pmod = mip;
		return 1;
	}

	mip = emalloc(sizeof(*mip));
	mip->file = estrdup(file);
	mip->line = line;
	mip->id = estrdup(id);
	mip->argc = argc;
	mip->argv = ecalloc(argc + 1, sizeof(mip->argv[0]));
	for (i = 0; i < argc; i++)
		mip->argv[i] = estrdup(argv[i]);
	mip->argv[i] = NULL;
	mip->module = NULL;
	mip->handle = NULL;
	module_attach(mip);
	*pmod = mip;
	return 0;
}

#define MODULE_ASSERT(cond)						\
	if (!(cond)) {							\
		lt_dlclose(handle);					\
		smap_error("%s: faulty module: (%s) failed",		\
			   inst->id,					\
			   #cond);					\
		return 1;                                               \
	}

static int
_load_module(struct smap_module_instance *inst)
{
	lt_dlhandle handle = NULL;
	lt_dladvise advise = NULL;
	struct smap_module *pmod;

	debug(DBG_MODULE, 2, ("loading module %s", inst->id));

	if (inst->handle) {
		smap_error("module %s already loaded", inst->id);
		return 1;
	}

	if (!lt_dladvise_init(&advise) && !lt_dladvise_ext(&advise)
	    && !lt_dladvise_global(&advise))
		handle = lt_dlopenadvise(inst->argv[0], advise);
	lt_dladvise_destroy(&advise);

	if (!handle) {
		smap_error("cannot load module %s: %s", inst->id,
			   lt_dlerror());
		return 1;
	}

	pmod = (struct smap_module *) lt_dlsym(handle, "module");
	MODULE_ASSERT(pmod);
	MODULE_ASSERT(pmod->smap_version <= SMAP_MODULE_VERSION);
	MODULE_ASSERT(pmod->smap_init_db);
	MODULE_ASSERT(pmod->smap_free_db);
	MODULE_ASSERT(pmod->smap_query);
	if (pmod->smap_init && pmod->smap_init(inst->argc, inst->argv)) {
		lt_dlclose(handle);
		smap_error("%s: initialization failed", inst->argv[0]);
		return 1;
	}
	inst->handle = handle;
	inst->module = pmod;
	return 0;
}

void
module_instance_free(struct smap_module_instance *inst)
{
	int i;
	free(inst->file);
	free(inst->id);
	for (i = 0; i < inst->argc; i++)
		free(inst->argv[i]);
	free(inst->argv);
	/* FIXME: Handle */
	free(inst);
}

void
smap_modules_load()
{
	struct smap_module_instance *p;

	debug(DBG_MODULE, 1, ("loading modules"));
	for (p = module_head; p; ) {
		struct smap_module_instance *next = p->next;

		if (_load_module(p)) {
			remove_dependent_databases(p->id);
			debug(DBG_MODULE, 2, ("removing module %s", p->id));
			module_detach(p);
			module_instance_free(p);
		}
		p = next;
	}
}

void
smap_modules_unload()
{
	struct smap_module_instance *p;

	for (p = module_head; p; p = p->next) {
		debug(DBG_MODULE, 2, ("unloading module %s", p->id));
		lt_dlclose(p->handle);
		p->handle = NULL;
	}
}

DCLIDLIST(database, smap_database_instance);

int
database_declare(const char *file, unsigned line,
		 const char *id, const char *modname, int argc, char **argv,
		 struct smap_database_instance **pdb)
{
	int i;
	struct smap_database_instance *db;

	db = database_locate(id);
	if (db) {
		*pdb = db;
		return 1;
	}
	db = emalloc(sizeof(*db));
	db->file = estrdup(file);
	db->line = line;
	db->id = estrdup(id);
	db->modname = estrdup(modname);
	db->argc = argc;
	db->argv = ecalloc(argc + 1, sizeof(db->argv[0]));
	for (i = 0; i < argc; i++)
		db->argv[i] = estrdup(argv[i]);
	db->argv[i] = NULL;
	db->inst = NULL;
	database_attach(db);
	*pdb = db;
	return 0;
}

void
database_free(struct smap_database_instance *db)
{
	int i;
	free(db->file);
	free(db->id);
	free(db->modname);
	for (i = 0; i < db->argc; i++)
		free(db->argv[i]);
	free(db->argv);
	free(db);
}

void
remove_dependent_databases(const char *modname)
{
	struct smap_database_instance *p;

	for (p = database_head; p; ) {
		struct smap_database_instance *next = p->next;
		if (strcmp(p->modname, modname) == 0) {
			debug(DBG_MODULE, 1, ("removing database %s", p->id));
			database_detach(p);
			database_free(p);
		}
		p = next;
	}
}

void
init_databases()
{
	struct smap_database_instance *p;

	debug(DBG_DATABASE, 1, ("initializing databases"));
	for (p = database_head; p; ) {
		struct smap_database_instance *next = p->next;
		struct smap_module_instance *inst = module_locate(p->modname);

		if (!inst)
			smap_error("%s:%u: module %s is not declared",
				   p->file, p->line, p->modname);
		else {
			debug(DBG_DATABASE, 2, ("initializing database %s",
						inst->id));
			
			p->dbh = inst->module->smap_init_db(p->id,
				                            p->argc,
							    p->argv);
			if (!p->dbh)
				smap_error("%s:%u: module %s: "
					   "database initialization failed",
					   p->file, p->line, p->modname);
		}
		if (!p->dbh) {
			debug(DBG_DATABASE, 2,
			      ("removing database %s", p->id));
			database_detach(p);
			database_free(p);
		}
		p->inst = inst;
		p = next;
	}
}

void
close_databases()
{
	struct smap_database_instance *p;

	debug(DBG_DATABASE, 1, ("closing databases"));
	for (p = database_head; p; p = p->next) {
		if (p->opened) {
			debug(DBG_DATABASE, 2,
			      ("closing database %s", p->id));
			if (p->inst->module->smap_close)
				p->inst->module->smap_close(p->dbh);
			p->opened = 0;
		}
	}
}

void
free_databases()
{
	struct smap_database_instance *p;

	debug(DBG_DATABASE, 1, ("freeing databases"));
	for (p = database_head; p; p = p->next) {
		struct smap_module *mod = p->inst->module;
		debug(DBG_DATABASE, 2,
		      ("freeing database %s", p->id));
		if (mod->smap_free_db)
			mod->smap_free_db(p->dbh);
		p->dbh = NULL;
	}
}

Return to:

Send suggestions and report system problems to the System administrator.