aboutsummaryrefslogtreecommitdiff
path: root/src/module.c
blob: ce021b1276bc515dbf4586ccfb40db2d881713d0 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* This file is part of Smap.
   Copyright (C) 2006-2021 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"
#include <dlfcn.h>

struct search_path {
	char **pathv;
	size_t pathc;
	size_t pathn;
};

struct search_path mod_load_path[2];

void
add_load_path(const char *path, int pathid)
{
	struct search_path *sp = &mod_load_path[pathid];
	size_t i;
	struct wordsplit ws;

	ws.ws_delim = ":";
	ws.ws_error = smap_error;
	if (wordsplit(path, &ws,
		      WRDSF_NOCMD | WRDSF_NOVAR |
		      WRDSF_ENOMEMABRT |
		      WRDSF_DELIM | WRDSF_ERROR)) {
		smap_error("cannot parse load path: %s",
			   wordsplit_strerror(&ws));
		abort();
	}

	for (i = 0; i < ws.ws_wordc; i++) {
		if (sp->pathc == sp->pathn)
			sp->pathv = e2nrealloc(sp->pathv, &sp->pathn,
					       sizeof(sp->pathv[0]));
		sp->pathv[sp->pathc++] = ws.ws_wordv[i];
	}
	ws.ws_wordc = 0;
	
	wordsplit_free(&ws);
}

void
smap_modules_init(void)
{
}


struct list_head module_head = LIST_HEAD_INITIALIZER(module_head);

static struct smap_module_instance *
module_locate(char const *id)
{
	struct smap_module_instance *p;
	LIST_FOREACH(p, &module_head, link) {
		if (strcmp(p->id, id) == 0)
			return p;
	}
	return NULL;
}

static inline void
module_attach(struct smap_module_instance *inst)
{
	LIST_HEAD_INSERT_LAST(&module_head, inst, link);
}

static inline void
module_detach(struct smap_module_instance *inst)
{
	LIST_REMOVE(inst, link);
}

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;
	list_head_init(&mip->link);
	module_attach(mip);
	*pmod = mip;
	return 0;
}

static void *
try_load_in_path(char const *file, char **pathv, size_t pathc)
{
	void *handle = NULL;
	size_t i;
	char *namebuf = NULL;
	size_t namelen = 0;

	size_t flen = strlen(file);
	
	static char const libdir[] = ".libs";
	static size_t libdirlen = sizeof(libdir)-1;

	for (i = 0; i < pathc; i++) {
		size_t plen = strlen(pathv[i]);
		size_t len;
		
		if (plen == 0)
			continue;
		if (pathv[i][plen-1] == '/')
			plen--;

		len = plen + flen + 2;
		if (len > namelen) {
			namebuf = erealloc(namebuf, len);
			namelen = len;
		}
		memcpy(namebuf, pathv[i], plen);
		namebuf[plen] = '/';
		strcpy(namebuf + plen + 1, file);

 		handle = dlopen(namebuf, RTLD_NOW | RTLD_GLOBAL);
		if (handle)
			break;

		/* Append .libs */
		if (!(plen > libdirlen + 1
		      && pathv[i][plen - libdirlen - 1] == '/'
		      && memcmp(pathv[i] + plen - libdirlen, libdir, libdirlen) == 0)) {
			len += libdirlen + 1;

			if (len > namelen) {
				namebuf = erealloc(namebuf, len);
				namelen = len;
			}

			strcpy(namebuf + plen + 1, libdir);
			strcat(namebuf, "/");
			strcat(namebuf, file);

			handle = dlopen(namebuf, RTLD_NOW | RTLD_GLOBAL);
			if (handle)
				break;
		}
	}
	free(namebuf);
	return handle;
}

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

static int
_load_module(struct smap_module_instance *inst)
{
	void *handle;
	struct smap_module *pmod;
	static char suf[] = ".so";
	static char suflen = sizeof(suf) - 1;
	size_t len;
	char *libtmp = NULL;
	char const *libname;
	
	debug(DBG_MODULE, 2, ("loading module %s", inst->id));

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

	len = strlen(inst->argv[0]);
	if (len > suflen
	    && memcmp(inst->argv[0] + len - suflen, suf, suflen) == 0)
		libname = inst->argv[0];
	else {
		libtmp = emalloc(len + suflen + 1);
		libname = strcat(strcpy(libtmp, inst->argv[0]), suf);
	}
	
	handle = try_load_in_path(libname, mod_load_path[PATH_PREPEND].pathv,
				  mod_load_path[PATH_PREPEND].pathc);
	if (handle == NULL) {
		char *path = getenv("LD_LIBRARY_PATH");
		if (path) {
			struct wordsplit ws;
			ws.ws_delim = ":";
			ws.ws_error = smap_error;

			if (wordsplit(path, &ws,
				      WRDSF_NOCMD | WRDSF_NOVAR |
				      WRDSF_ENOMEMABRT |
				      WRDSF_DELIM | WRDSF_ERROR)) {
				smap_error("cannot parse load path: %s",
					   wordsplit_strerror(&ws));
				abort();
			}

			handle = try_load_in_path(libname,
						  ws.ws_wordv, ws.ws_wordc);
			wordsplit_free(&ws);
		}

		if (handle == NULL) {
			char *moddir = SMAP_MODDIR;
			handle = try_load_in_path(libname, &moddir, 1);
		}

		if (handle == NULL) {
			try_load_in_path(libname,
					 mod_load_path[PATH_APPEND].pathv,
					 mod_load_path[PATH_APPEND].pathc);
		}
	}
	free(libtmp);
	
	if (!handle) {
		smap_error("cannot load module %s: %s", inst->id, dlerror());
		return 1;
	}

	pmod = (struct smap_module *) dlsym(handle, "smap_module");
	MODULE_ASSERT(pmod != NULL);
	MODULE_ASSERT(pmod->smap_version <= SMAP_MODULE_VERSION);
	MODULE_ASSERT(pmod->smap_init_db);
	MODULE_ASSERT(pmod->smap_free_db);
	if (pmod->smap_version == 1)
	    	MODULE_ASSERT(pmod->smap_query);
	else {
		if (pmod->smap_capabilities & SMAP_CAPA_QUERY)
			MODULE_ASSERT(pmod->smap_query);
		if (pmod->smap_capabilities & SMAP_CAPA_XFORM)
			MODULE_ASSERT(pmod->smap_xform);
		if (pmod->smap_capabilities & SMAP_CAPA_DBCAP)
			MODULE_ASSERT(pmod->smap_dbcap);
	}
	
	if (pmod->smap_init && pmod->smap_init(inst->argc, inst->argv)) {
		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, *tmp;

	debug(DBG_MODULE, 1, ("loading modules"));
	LIST_FOREACH_SAFE(p, tmp, &module_head, link) {
		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);
		}
	}
}

void
smap_modules_unload()
{
	struct smap_module_instance *p;

	LIST_FOREACH(p, &module_head, link) {
		debug(DBG_MODULE, 2, ("unloading module %s", p->id));
		dlclose(p->handle);
		p->handle = NULL;
	}
}

struct list_head database_head = LIST_HEAD_INITIALIZER(database_head);

struct smap_database_instance *
database_locate(char const *id)
{
	struct smap_database_instance *p;
	LIST_FOREACH(p, &database_head, link) {
		if (strcmp(p->id, id) == 0)
			return p;
	}
	return NULL;
}

static inline void
database_attach(struct smap_database_instance *inst)
{
	LIST_HEAD_INSERT_LAST(&database_head, inst, link);
}

static inline void
database_detach(struct smap_database_instance *inst)
{
	LIST_REMOVE(inst, link);
}

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 = ecalloc(1, 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;
	list_head_init(&db->link);
	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, *tmp;

	LIST_FOREACH_SAFE(p, tmp, &database_head, link) {
		if (strcmp(p->modname, modname) == 0) {
			debug(DBG_MODULE, 1, ("removing database %s", p->id));
			database_detach(p);
			database_free(p);
		}
	}
}

void
init_databases(void)
{
	struct smap_database_instance *p, *tmp;

	debug(DBG_DATABASE, 1, ("initializing databases"));
	LIST_FOREACH_SAFE(p, tmp, &database_head, link) {
		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)
			p->inst = inst;
		else {
			debug(DBG_DATABASE, 2,
			      ("removing database %s", p->id));
			database_detach(p);
			database_free(p);
		} 
	}
}

void
close_databases()
{
	struct smap_database_instance *p;

	debug(DBG_DATABASE, 1, ("closing databases"));
	LIST_FOREACH(p, &database_head, link) {
		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"));
	LIST_FOREACH(p, &database_head, link) {
		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.