11 #ifdef HAVE_VALGRIND_VALGRIND_H
12 # include <valgrind/valgrind.h>
15 #if !defined(__WIN32) && !defined(LIGHTTPD_STATIC)
20 * if you change this enum to add a new callback, be sure
21 * - that PLUGIN_FUNC_SIZEOF is the last entry
22 * - that you add PLUGIN_TO_SLOT twice:
23 * 1. as callback-dispatcher
24 * 2. in plugins_call_init()
35 PLUGIN_FUNC_HANDLE_URI_CLEAN
,
36 PLUGIN_FUNC_HANDLE_URI_RAW
,
37 PLUGIN_FUNC_HANDLE_REQUEST_DONE
,
38 PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE
,
39 PLUGIN_FUNC_HANDLE_TRIGGER
,
40 PLUGIN_FUNC_HANDLE_SIGHUP
,
41 PLUGIN_FUNC_HANDLE_SUBREQUEST
,
42 PLUGIN_FUNC_HANDLE_SUBREQUEST_START
,
43 PLUGIN_FUNC_HANDLE_RESPONSE_START
,
44 PLUGIN_FUNC_HANDLE_DOCROOT
,
45 PLUGIN_FUNC_HANDLE_PHYSICAL
,
46 PLUGIN_FUNC_CONNECTION_RESET
,
49 PLUGIN_FUNC_SET_DEFAULTS
,
54 static plugin
*plugin_init(void) {
57 p
= calloc(1, sizeof(*p
));
58 force_assert(NULL
!= p
);
63 static void plugin_free(plugin
*p
) {
64 #if !defined(LIGHTTPD_STATIC)
68 if (p
->name
) buffer_free(p
->name
);
69 #if defined(HAVE_VALGRIND_VALGRIND_H) && !defined(LIGHTTPD_STATIC)
70 /*if (RUNNING_ON_VALGRIND) use_dlclose = 0;*/
73 #if !defined(LIGHTTPD_STATIC)
74 if (use_dlclose
&& p
->lib
) {
76 ) FreeLibrary(p
->lib
);
86 static int plugins_register(server
*srv
, plugin
*p
) {
88 if (0 == srv
->plugins
.size
) {
89 srv
->plugins
.size
= 4;
90 srv
->plugins
.ptr
= malloc(srv
->plugins
.size
* sizeof(*ps
));
91 force_assert(NULL
!= srv
->plugins
.ptr
);
92 srv
->plugins
.used
= 0;
93 } else if (srv
->plugins
.used
== srv
->plugins
.size
) {
94 srv
->plugins
.size
+= 4;
95 srv
->plugins
.ptr
= realloc(srv
->plugins
.ptr
, srv
->plugins
.size
* sizeof(*ps
));
96 force_assert(NULL
!= srv
->plugins
.ptr
);
99 ps
= srv
->plugins
.ptr
;
100 ps
[srv
->plugins
.used
++] = p
;
111 #if defined(LIGHTTPD_STATIC)
113 /* pre-declare functions, as there is no header for them */
114 #define PLUGIN_INIT(x)\
115 int x ## _plugin_init(plugin *p);
117 #include "plugin-static.h"
121 /* build NULL-terminated table of name + init-function */
125 int (*plugin_init
)(plugin
*p
);
126 } plugin_load_functions
;
128 static const plugin_load_functions load_functions
[] = {
129 #define PLUGIN_INIT(x) \
130 { #x, &x ## _plugin_init },
132 #include "plugin-static.h"
138 int plugins_load(server
*srv
) {
142 for (i
= 0; i
< srv
->srvconf
.modules
->used
; i
++) {
143 data_string
*d
= (data_string
*)srv
->srvconf
.modules
->data
[i
];
144 char *module
= d
->value
->ptr
;
146 for (j
= 0; j
< i
; j
++) {
147 if (buffer_is_equal(d
->value
, ((data_string
*) srv
->srvconf
.modules
->data
[j
])->value
)) {
148 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
149 "Cannot load plugin", d
->value
,
150 "more than once, please fix your config (lighttpd may not accept such configs in future releases)");
155 for (j
= 0; load_functions
[j
].name
; ++j
) {
156 if (0 == strcmp(load_functions
[j
].name
, module
)) {
158 if ((*load_functions
[j
].plugin_init
)(p
)) {
159 log_error_write(srv
, __FILE__
, __LINE__
, "ss", module
, "plugin init failed" );
163 plugins_register(srv
, p
);
167 if (!load_functions
[j
].name
) {
168 log_error_write(srv
, __FILE__
, __LINE__
, "ss", module
, " plugin not found" );
175 #else /* defined(LIGHTTPD_STATIC) */
176 int plugins_load(server
*srv
) {
178 int (*init
)(plugin
*pl
);
181 for (i
= 0; i
< srv
->srvconf
.modules
->used
; i
++) {
182 data_string
*d
= (data_string
*)srv
->srvconf
.modules
->data
[i
];
183 char *module
= d
->value
->ptr
;
185 for (j
= 0; j
< i
; j
++) {
186 if (buffer_is_equal(d
->value
, ((data_string
*) srv
->srvconf
.modules
->data
[j
])->value
)) {
187 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
188 "Cannot load plugin", d
->value
,
189 "more than once, please fix your config (lighttpd may not accept such configs in future releases)");
194 buffer_copy_buffer(srv
->tmp_buf
, srv
->srvconf
.modules_dir
);
196 buffer_append_string_len(srv
->tmp_buf
, CONST_STR_LEN("/"));
197 buffer_append_string(srv
->tmp_buf
, module
);
198 #if defined(__WIN32) || defined(__CYGWIN__)
199 buffer_append_string_len(srv
->tmp_buf
, CONST_STR_LEN(".dll"));
201 buffer_append_string_len(srv
->tmp_buf
, CONST_STR_LEN(".so"));
206 if (NULL
== (p
->lib
= LoadLibrary(srv
->tmp_buf
->ptr
))) {
209 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
210 FORMAT_MESSAGE_FROM_SYSTEM
,
213 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
217 log_error_write(srv
, __FILE__
, __LINE__
, "ssb", "LoadLibrary() failed",
218 lpMsgBuf
, srv
->tmp_buf
);
226 if (NULL
== (p
->lib
= dlopen(srv
->tmp_buf
->ptr
, RTLD_NOW
|RTLD_GLOBAL
))) {
227 log_error_write(srv
, __FILE__
, __LINE__
, "sbs", "dlopen() failed for:",
228 srv
->tmp_buf
, dlerror());
236 buffer_reset(srv
->tmp_buf
);
237 buffer_copy_string(srv
->tmp_buf
, module
);
238 buffer_append_string_len(srv
->tmp_buf
, CONST_STR_LEN("_plugin_init"));
241 init
= GetProcAddress(p
->lib
, srv
->tmp_buf
->ptr
);
246 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
247 FORMAT_MESSAGE_FROM_SYSTEM
,
250 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
254 log_error_write(srv
, __FILE__
, __LINE__
, "sbs", "getprocaddress failed:", srv
->tmp_buf
, lpMsgBuf
);
262 init
= (int (*)(plugin
*))(intptr_t)dlsym(p
->lib
, srv
->tmp_buf
->ptr
);
264 *(void **)(&init
) = dlsym(p
->lib
, srv
->tmp_buf
->ptr
);
267 const char *error
= dlerror();
269 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "dlsym:", error
);
271 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "dlsym symbol not found:", srv
->tmp_buf
->ptr
);
280 log_error_write(srv
, __FILE__
, __LINE__
, "ss", module
, "plugin init failed" );
286 log_error_write(srv
, __FILE__
, __LINE__
, "ss", module
, "plugin loaded" );
288 plugins_register(srv
, p
);
293 #endif /* defined(LIGHTTPD_STATIC) */
295 #define PLUGIN_TO_SLOT(x, y) \
296 handler_t plugins_call_##y(server *srv, connection *con) {\
299 if (!srv->plugin_slots) return HANDLER_GO_ON;\
300 slot = ((plugin ***)(srv->plugin_slots))[x];\
301 if (!slot) return HANDLER_GO_ON;\
302 for (j = 0; j < srv->plugins.used && slot[j]; j++) { \
303 plugin *p = slot[j];\
305 switch(r = p->y(srv, con, p->data)) {\
308 case HANDLER_FINISHED:\
309 case HANDLER_COMEBACK:\
310 case HANDLER_WAIT_FOR_EVENT:\
311 case HANDLER_WAIT_FOR_FD:\
315 log_error_write(srv, __FILE__, __LINE__, "sbs", #x, p->name, "unknown state");\
316 return HANDLER_ERROR;\
319 return HANDLER_GO_ON;\
327 * - void *p_d (plugin_data *)
330 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_URI_CLEAN
, handle_uri_clean
)
331 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_URI_RAW
, handle_uri_raw
)
332 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_REQUEST_DONE
, handle_request_done
)
333 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE
, handle_connection_close
)
334 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_SUBREQUEST
, handle_subrequest
)
335 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_SUBREQUEST_START
, handle_subrequest_start
)
336 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_RESPONSE_START
, handle_response_start
)
337 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_DOCROOT
, handle_docroot
)
338 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_PHYSICAL
, handle_physical
)
339 PLUGIN_TO_SLOT(PLUGIN_FUNC_CONNECTION_RESET
, connection_reset
)
341 #undef PLUGIN_TO_SLOT
343 #define PLUGIN_TO_SLOT(x, y) \
344 handler_t plugins_call_##y(server *srv) {\
347 if (!srv->plugin_slots) return HANDLER_GO_ON;\
348 slot = ((plugin ***)(srv->plugin_slots))[x];\
349 if (!slot) return HANDLER_GO_ON;\
350 for (j = 0; j < srv->plugins.used && slot[j]; j++) { \
351 plugin *p = slot[j];\
353 switch(r = p->y(srv, p->data)) {\
356 case HANDLER_FINISHED:\
357 case HANDLER_COMEBACK:\
358 case HANDLER_WAIT_FOR_EVENT:\
359 case HANDLER_WAIT_FOR_FD:\
363 log_error_write(srv, __FILE__, __LINE__, "sbsd", #x, p->name, "unknown state:", r);\
364 return HANDLER_ERROR;\
367 return HANDLER_GO_ON;\
374 * - void *p_d (plugin_data *)
377 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_TRIGGER
, handle_trigger
)
378 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_SIGHUP
, handle_sighup
)
379 PLUGIN_TO_SLOT(PLUGIN_FUNC_CLEANUP
, cleanup
)
380 PLUGIN_TO_SLOT(PLUGIN_FUNC_SET_DEFAULTS
, set_defaults
)
382 #undef PLUGIN_TO_SLOT
390 handler_t
plugins_call_handle_fdevent(server
*srv
, const fd_conn
*fdc
) {
394 ps
= srv
->plugins
.ptr
;
396 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
398 if (p
->handle_fdevent
) {
400 switch(r
= p
->handle_fdevent(srv
, fdc
, p
->data
)) {
403 case HANDLER_FINISHED
:
404 case HANDLER_COMEBACK
:
405 case HANDLER_WAIT_FOR_EVENT
:
409 log_error_write(srv
, __FILE__
, __LINE__
, "d", r
);
415 return HANDLER_GO_ON
;
420 * - call init function of all plugins to init the plugin-internals
421 * - added each plugin that supports has callback to the corresponding slot
423 * - is only called once.
426 handler_t
plugins_call_init(server
*srv
) {
430 ps
= srv
->plugins
.ptr
;
434 srv
->plugin_slots
= calloc(PLUGIN_FUNC_SIZEOF
, sizeof(ps
));
435 force_assert(NULL
!= srv
->plugin_slots
);
437 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
439 /* check which calls are supported */
443 #define PLUGIN_TO_SLOT(x, y) \
445 plugin **slot = ((plugin ***)(srv->plugin_slots))[x]; \
447 slot = calloc(srv->plugins.used, sizeof(*slot));\
448 force_assert(NULL != slot); \
449 ((plugin ***)(srv->plugin_slots))[x] = slot; \
451 for (j = 0; j < srv->plugins.used; j++) { \
452 if (slot[j]) continue;\
459 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_URI_CLEAN
, handle_uri_clean
);
460 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_URI_RAW
, handle_uri_raw
);
461 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_REQUEST_DONE
, handle_request_done
);
462 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_CONNECTION_CLOSE
, handle_connection_close
);
463 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_TRIGGER
, handle_trigger
);
464 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_SIGHUP
, handle_sighup
);
465 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_SUBREQUEST
, handle_subrequest
);
466 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_SUBREQUEST_START
, handle_subrequest_start
);
467 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_RESPONSE_START
, handle_response_start
);
468 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_DOCROOT
, handle_docroot
);
469 PLUGIN_TO_SLOT(PLUGIN_FUNC_HANDLE_PHYSICAL
, handle_physical
);
470 PLUGIN_TO_SLOT(PLUGIN_FUNC_CONNECTION_RESET
, connection_reset
);
471 PLUGIN_TO_SLOT(PLUGIN_FUNC_CLEANUP
, cleanup
);
472 PLUGIN_TO_SLOT(PLUGIN_FUNC_SET_DEFAULTS
, set_defaults
);
473 #undef PLUGIN_TO_SLOT
476 if (NULL
== (p
->data
= p
->init())) {
477 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
478 "plugin-init failed for module", p
->name
);
479 return HANDLER_ERROR
;
482 /* used for con->mode, DIRECT == 0, plugins above that */
483 ((plugin_data
*)(p
->data
))->id
= i
+ 1;
485 if (p
->version
!= LIGHTTPD_VERSION_ID
) {
486 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
487 "plugin-version doesn't match lighttpd-version for", p
->name
);
488 return HANDLER_ERROR
;
495 return HANDLER_GO_ON
;
498 void plugins_free(server
*srv
) {
500 plugins_call_cleanup(srv
);
502 for (i
= 0; i
< srv
->plugins
.used
; i
++) {
503 plugin
*p
= ((plugin
**)srv
->plugins
.ptr
)[i
];
508 for (i
= 0; srv
->plugin_slots
&& i
< PLUGIN_FUNC_SIZEOF
; i
++) {
509 plugin
**slot
= ((plugin
***)(srv
->plugin_slots
))[i
];
511 if (slot
) free(slot
);
514 free(srv
->plugin_slots
);
515 srv
->plugin_slots
= NULL
;
517 free(srv
->plugins
.ptr
);
518 srv
->plugins
.ptr
= NULL
;
519 srv
->plugins
.used
= 0;