1,62 → 1,50 |
--- /dev/null |
+++ b/shared/dlopen_templates.c |
@@ -0,0 +1,51 @@ |
@@ -0,0 +1,40 @@ |
+#include <dlfcn.h> |
+#include "shared.h" |
+ |
+struct template_map_index *template_map_index = 0; |
+struct template_map_index template_map_index[] = { |
+ {NIL, NIL, NIL}, |
+ {NIL, NIL, NIL} |
+}; |
+ |
+void |
+dlopen_templates(struct config *config, const char *suffix, struct pool *pool) |
+struct template_map_index * |
+dlopen_templates(const char *set, const char *suffix) |
+{ |
+ if (config->template_list && !template_map_index) { |
+ struct list_item *li; |
+ struct template_map_index *tmi; |
+ struct template_map_index *tmi = &template_map_index[0]; |
+ static void *handle = 0; |
+ |
+ template_map_index = tmi = pool_alloc |
+ (pool, |
+ (config->template_list->length+1) |
+ * sizeof(struct template_map_index)); |
+ |
+ for (li = config->template_list->head; li; li = li->next) { |
+ if (!handle || strcmp(tmi->name, set)) { |
+ char *filename = pool_printf(NIL, "/usr/lib/prayer/templates/%s%s.so", |
+ li->name, suffix); |
+ set, suffix); |
+ char *map_symbol = pool_printf(NIL, "template_map_%s", |
+ li->name); |
+ set); |
+ char *count_symbol = pool_printf(NIL, "template_map_%s_count", |
+ li->name); |
+ set); |
+ |
+ tmi->name = pool_strdup(pool, li->name); |
+ if (!(tmi->handle = dlopen(filename, RTLD_NOW)) |
+ || !(tmi->template_map = dlsym(tmi->handle, map_symbol)) |
+ || !(tmi->count = dlsym(tmi->handle, count_symbol))) { |
+ if (handle) { |
+ dlclose(handle); |
+ free(tmi->name); |
+ } |
+ tmi->name = pool_strdup(NIL, set); |
+ if (!(handle = dlopen(filename, RTLD_NOW)) |
+ || !(tmi->template_map = dlsym(handle, map_symbol)) |
+ || !(tmi->count = dlsym(handle, count_symbol))) { |
+ log_fatal("Failed to load template library: \"%s\": %s", |
+ filename, dlerror()); |
+ } |
+ log_debug("Loaded library %s with %d templates", filename, *tmi->count); |
+ free(filename); |
+ tmi++; |
+ free(map_symbol); |
+ free(count_symbol); |
+ } |
+ *tmi = (struct template_map_index){0}; |
+ return tmi; |
+ } |
+} |
+ |
+void |
+dlopen_templates_close() |
+{ |
+ if (template_map_index) { |
+ struct template_map_index *tmi = &template_map_index[0]; |
+ |
+ while (tmi->name) { |
+ dlclose(tmi->handle); |
+ } |
+ } |
+} |
--- /dev/null |
+++ b/shared/dlopen_templates.h |
@@ -0,0 +1,2 @@ |
+void dlopen_templates(struct config *config, const char *suffix, struct pool *pool); |
+void dlopen_templates_close(); |
@@ -0,0 +1 @@ |
+struct template_map_index *dlopen_templates(const char *set, const char *suffix); |
--- a/templates/cam/Makefile |
+++ b/templates/cam/Makefile |
@@ -11,7 +11,7 @@ LDFLAGS = $(BASELDFLAGS) |
139,22 → 127,11 |
config_extract_ssl(config, &ssl_config); |
iostream_init(&ssl_config); |
|
+ dlopen_templates(config, "_frontend", prayer->pool); |
+ dlopen_templates(config->template_set, "_frontend"); |
+ |
prayer_main_write_pid(config); |
|
if (want_prefork && !want_foreground) |
--- a/servers/session_main.c |
+++ b/servers/session_main.c |
@@ -187,6 +187,8 @@ int main(int argc, char *argv[]) |
if (config->tmp_dir) |
chdir(config->tmp_dir); |
|
+ dlopen_templates(config, "", config->pool); |
+ |
session_main_write_pid(config); |
|
session_server(config, want_foreground); |
--- a/shared/Makefile |
+++ b/shared/Makefile |
@@ -33,7 +33,7 @@ MYCFLAGS = $(BASECFLAGS) |
168,32 → 145,15 |
all: $(SHARED_OBJS) |
--- a/servers/prayer_main.c |
+++ b/servers/prayer_main.c |
@@ -328,6 +328,8 @@ int main(int argc, char *argv[]) |
config_extract_ssl(config, &ssl_config); |
iostream_init(&ssl_config); |
@@ -350,6 +350,8 @@ int main(int argc, char *argv[]) |
open("/dev/null", O_WRONLY); /* Reopen stderr */ |
} |
|
+ dlopen_templates(config, "_frontend", prayer->pool); |
+ dlopen_templates(config->template_set, "_frontend"); |
+ |
if (config->prayer_background && !want_foreground) { |
pid_t pid = fork(); |
prayer_main_write_pid(config); |
|
--- a/lib/template.h |
+++ b/lib/template.h |
@@ -22,6 +22,7 @@ struct template_map_index { |
char *name; |
struct template_map *template_map; |
unsigned long *count; |
+ void *handle; |
}; |
|
struct template_map { |
--- a/servers/prayer_shared.h |
+++ b/servers/prayer_shared.h |
@@ -44,3 +44,4 @@ extern int errno; /* just |
#include "mymutex.h" |
#include "log.h" |
#include "utf8.h" |
+#include "dlopen_templates.h" |
if (want_prefork && !want_foreground) |
--- a/shared/shared.h |
+++ b/shared/shared.h |
@@ -40,3 +40,4 @@ extern int errno; /* just |
240,20 → 200,14 |
$(MAKE) -C servers install |
$(MAKE) -C utils install |
ifeq ($(strip $(ACCOUNTD_ENABLE)), true) |
--- a/lib/template.c |
+++ b/lib/template.c |
@@ -190,12 +190,12 @@ template_getexpr(char **sp, struct pool |
--- a/servers/session_exchange.c |
+++ b/servers/session_exchange.c |
@@ -212,6 +212,8 @@ BOOL session_exchange(struct session * s |
else |
template_set = config->template_set; /* Safe default */ |
|
/* ====================================================================== */ |
|
-extern struct template_map_index template_map_index[]; |
+extern struct template_map_index *template_map_index; |
|
struct template * |
template_find(char *set, char *name, struct pool *pool) |
{ |
- struct template_map_index *tmi = &template_map_index[0]; |
+ struct template_map_index *tmi = template_map_index; |
struct template_map *tm; |
unsigned long count; |
unsigned long first, last, middle; |
+ dlopen_templates(template_set, ""); |
+ |
/* Set up template_vars ready for dispatch */ |
session->template_vals = tvals |
= template_vals_create(request->pool, |