aboutsummaryrefslogtreecommitdiff
path: root/src/module.c
blob: 41bbb395e04051083cd4a5806e2440eafee308c2 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007-2012 Sergey Poznyakoff

   Wydawca 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.

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

#include "wydawca.h"
#include <ltdl.h>

static struct grecs_symtab *modtab;
struct grecs_list *module_load_path, *module_prepend_load_path;

static void
modfree(void *p)
{
	struct module *mod = p;
	free(mod->locus.beg.file);
	free(mod->locus.end.file);
	free(mod->path);
	free(mod->name);
	free(mod);
}

static struct module *
modinstall(const char *name, const char *path, grecs_locus_t *loc)
{
	struct module key;
	struct module *ent;
	int install = 1;

	if (!modtab) {
		modtab = grecs_symtab_create(sizeof(struct module),
					     NULL,
					     NULL,
					     NULL,
					     NULL,
					     modfree);
		if (!modtab)
			grecs_alloc_die();
	}
  
	key.name = (char*) name;
	ent = grecs_symtab_lookup_or_install(modtab, &key, &install);
	if (!ent) 
		grecs_alloc_die();
	if (install == 0) {
		grecs_error(loc, 0, _("module %s already declared"), name);
		grecs_error(&ent->locus, 0, _("previously declared here"));
		return NULL;
	}
	ent->path = grecs_strdup(path);
	ent->locus.beg.file = grecs_strdup(loc->beg.file);
	ent->locus.beg.line = loc->beg.line;
	ent->locus.beg.col = loc->beg.col;
	ent->locus.end.file = grecs_strdup(loc->end.file);
	ent->locus.end.line = loc->end.line;
	ent->locus.end.col = loc->end.col;
	return ent;
}

static struct module *
modlookup(const char *name)
{
	struct module key;

	if (!modtab)
		return NULL;
  
	key.name = (char*) name;
	return grecs_symtab_lookup_or_install(modtab, &key, NULL);
}

int
cb_module(enum grecs_callback_command cmd, grecs_node_t *node,
	  void *varptr, void *cb_data)
{
	grecs_value_t *name;
	grecs_value_t *path;
	grecs_locus_t *locus = &node->locus;
	grecs_value_t *value = node->v.value;
		
	if (cmd != grecs_callback_set_value) {
		grecs_error(locus, 0, _("Unexpected block statement"));
		return 1;
	}

	if (!(name = get_arg(value, 0, GRECS_TYPE_STRING)))
		return 1;
	if (!(path = get_arg(value, 1, GRECS_TYPE_STRING)))
		return 1;
	
	modinstall(name->v.string, path->v.string, locus);

	return 0;
};

static void *
resolve_sym(struct module *mod, const char *name)
{
	void *sym = lt_dlsym(mod->handle, name);
	if (!sym) {
		grecs_error(&mod->locus, 0,
			    _("module \"%s\" does not define symbol \"%s\""),
			    mod->name, name);
	}
	return sym;
}

static int
modload(void *sym, void *data)
{
	struct module *mod = sym;
	lt_dlhandle handle = NULL;
	lt_dladvise advise = data;
	
	if (mod->handle) {
		grecs_error(&mod->locus, 0, _("already loaded"));
		return 0;
	}

	handle = lt_dlopenadvise(mod->path, advise);

	if (!handle) {
		grecs_error(&mod->locus, 0,
			    _("cannot load module %s: %s"), mod->path,
			    lt_dlerror());
		return 1;
	}
	mod->handle = handle;
	mod->config = resolve_sym(mod, "config");
	mod->notify = resolve_sym(mod, "notify");
	
	return 0;
}

static int
spoolmodcfg(struct spool *spool, void *unused)
{
	struct notification *np;

	for (np = spool->notification; np; np = np->next) {
		if (np->modname) {
			struct module *mod = modlookup(np->modname);
			if (!mod) {
				logmsg(LOG_ERR, "spool %s: no such module: %s",
				       spool->tag, np->modname);
				return 1;
			}
			if (mod->config) {
				np->modcfg = mod->config(np->modnode);
				if (!np->modcfg) {
					logmsg(LOG_ERR,
					       "spool %s: failed to configure "
					       "module \"%s\"",
					       spool->tag, np->modname);
					return 1;
				}
			}
		}
	}
	return 0;
}

void
modules_load()
{
	lt_dladvise advise = NULL;
	struct grecs_list_entry *ep;
	
	if (lt_dlinit()) {
		logmsg(LOG_ERR, _("failed to initialize libtool"));
		return;
	}

	/* Prepare load path */
	if (module_prepend_load_path)
		for (ep = module_prepend_load_path->head; ep; ep = ep->next)
			lt_dladdsearchdir(ep->data);
	lt_dladdsearchdir(WYDAWCA_MODDIR);
	if (module_load_path)
		for (ep = module_load_path->head; ep; ep = ep->next)
			lt_dladdsearchdir(ep->data);
	

	if (lt_dladvise_init(&advise))
		logmsg(LOG_ERR, "lt_dladvise_init: %s", lt_dlerror());
	else {
		if (lt_dladvise_ext(&advise))
			logmsg(LOG_ERR, "lt_dladvise_ext: %s", lt_dlerror());
		if (lt_dladvise_global(&advise))
			logmsg(LOG_ERR, "lt_dladvise_global: %s",
			       lt_dlerror());
	}
	if (grecs_symtab_enumerate(modtab, modload, NULL)) {
		logmsg(LOG_CRIT, _("some modules failed to load, exiting"));
		exit(EX_UNAVAILABLE);
	}
	lt_dladvise_destroy(&advise);

	if (for_each_spool(spoolmodcfg, NULL)) {
		logmsg(LOG_CRIT,
		       _("some modules failed to configure, exiting"));
		exit(EX_UNAVAILABLE);
	}
}

static char *
getfn(void *trp, const char *fmt)
{
	return triplet_expand_param(fmt, trp);
}

void
module_notify(const char *name, void *modcfg,
	      enum notification_event ev, struct file_triplet *trp)
{
	struct module *mod = modlookup(name);

	if (!mod) {
		logmsg(LOG_ERR, "no such module: %s", name);
		return;
	}
	if (mod->notify)
		mod->notify(modcfg, ev, getfn, trp);
}

Return to:

Send suggestions and report system problems to the System administrator.