#include "ap_mpm.h"
#include "scoreboard.h"
+#include "mod_watchdog.h"
#ifndef HEARTBEAT_INTERVAL
#define HEARTBEAT_INTERVAL (1)
int server_limit;
int thread_limit;
apr_status_t status;
- volatile int keep_running;
- apr_proc_mutex_t *mutex;
- const char *mutex_path;
- apr_thread_mutex_t *start_mtx;
- apr_thread_t *thread;
- apr_file_t *lockf;
} hb_ctx_t;
static const char *msg_format = "v=%u&ready=%u&busy=%u";
return OK;
}
-#ifndef apr_time_from_msec
-#define apr_time_from_msec(x) (x * 1000)
-#endif
-
-static void* APR_THREAD_FUNC hb_worker(apr_thread_t *thd, void *data)
+static int hb_watchdog_init(server_rec *s, const char *name, apr_pool_t *pool)
{
- apr_pool_t *tpool;
- hb_ctx_t *ctx = (hb_ctx_t *) data;
apr_status_t rv;
+ hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
- apr_pool_t *pool = apr_thread_pool_get(thd);
- apr_pool_tag(pool, "heartbeat_worker");
- ctx->status = APR_SUCCESS;
- ctx->keep_running = 1;
- apr_thread_mutex_unlock(ctx->start_mtx);
-
- while (ctx->keep_running) {
- rv = apr_proc_mutex_trylock(ctx->mutex);
- if (rv == APR_SUCCESS) {
- break;
- }
- apr_sleep(apr_time_from_msec(200));
- }
-
- apr_pool_create(&tpool, pool);
- apr_pool_tag(tpool, "heartbeat_worker_temp");
- while (ctx->keep_running) {
- int mpm_state = 0;
- apr_pool_clear(tpool);
-
- rv = ap_mpm_query(AP_MPMQ_MPM_STATE, &mpm_state);
-
- if (rv != APR_SUCCESS) {
- break;
- }
-
- if (mpm_state == AP_MPMQ_STOPPING) {
- ctx->keep_running = 0;
- break;
- }
-
- hb_monitor(ctx, tpool);
- apr_sleep(apr_time_from_sec(HEARTBEAT_INTERVAL));
- }
-
- apr_pool_destroy(tpool);
- apr_proc_mutex_unlock(ctx->mutex);
- apr_thread_exit(ctx->thread, APR_SUCCESS);
+ ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &ctx->thread_limit);
+ ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &ctx->server_limit);
- return NULL;
+ return OK;
}
-static apr_status_t hb_pool_cleanup(void *baton)
+static int hb_watchdog_exit(server_rec *s, const char *name, apr_pool_t *pool)
{
apr_status_t rv;
- hb_ctx_t *ctx = (hb_ctx_t *) baton;
-
- ctx->keep_running = 0;
-
- apr_thread_join(&rv, ctx->thread);
+ hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
- return rv;
+ return OK;
}
-static void start_hb_worker(apr_pool_t *p, hb_ctx_t *ctx)
+static int hb_watchdog_step(server_rec *s, const char *name, apr_pool_t *pool)
{
apr_status_t rv;
-
- rv = apr_thread_mutex_create(&ctx->start_mtx, APR_THREAD_MUTEX_UNNESTED,
- p);
-
- if (rv) {
- ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
- "Heartbeat: apr_thread_mutex_create failed");
- ctx->status = rv;
- return;
- }
-
- /* This mutex fixes problems with a fast start/fast end, where the pool
- * cleanup was being invoked before the thread completely spawned.
- */
- apr_thread_mutex_lock(ctx->start_mtx);
-
- apr_pool_cleanup_register(p, ctx, hb_pool_cleanup, apr_pool_cleanup_null);
-
- rv = apr_thread_create(&ctx->thread, NULL, hb_worker, ctx, p);
- if (rv) {
- apr_pool_cleanup_kill(p, ctx, hb_pool_cleanup);
- ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL,
- "Heartbeat: apr_thread_create failed");
- ctx->status = rv;
- }
-
- apr_thread_mutex_lock(ctx->start_mtx);
- apr_thread_mutex_unlock(ctx->start_mtx);
- apr_thread_mutex_destroy(ctx->start_mtx);
-}
-
-static void hb_child_init(apr_pool_t *p, server_rec *s)
-{
hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
- if (ctx->active) {
- apr_proc_mutex_child_init(&ctx->mutex, ctx->mutex_path, p);
-
- ctx->status = APR_EGENERAL;
-
- start_hb_worker(p, ctx);
- if (ctx->status) {
- ap_log_error(APLOG_MARK, APLOG_CRIT, ctx->status, s,
- "Heartbeat: Failed to start worker thread.");
- return;
- }
+ if (!ctx->active) {
+ return OK;
}
-
- return;
+ return hb_monitor(ctx, pool);
}
-static int hb_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
- server_rec *s)
+static int hb_watchdog_need(server_rec *s, const char *name,
+ int parent, int singleton)
{
- apr_lockmech_e mech;
- apr_status_t rv;
hb_ctx_t *ctx = ap_get_module_config(s->module_config, &heartbeat_module);
- ap_mpm_query(AP_MPMQ_HARD_LIMIT_THREADS, &ctx->thread_limit);
- ap_mpm_query(AP_MPMQ_HARD_LIMIT_DAEMONS, &ctx->server_limit);
-
- if (!ctx->active) {
+ if (ctx->active && singleton && !strcmp(name, AP_WATCHDOG_SINGLETON))
return OK;
- }
-
-#if APR_HAS_FCNTL_SERIALIZE
- mech = APR_LOCK_FCNTL;
-#else
-#if APR_HAS_FLOCK_SERIALIZE
- mech = APR_LOCK_FLOCK;
-#else
-#error port me to a non crap platform.
-#endif
-#endif
-
- rv = apr_proc_mutex_create(&ctx->mutex, ctx->mutex_path,
- mech,
- p);
-
- if (rv) {
- ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
- "Heartbeat: mutex failed creation at %s (type=%d)",
- ctx->mutex_path, mech);
- return !OK;
- }
-
- return OK;
+ else
+ return DECLINED;
}
static void hb_register_hooks(apr_pool_t *p)
{
- ap_hook_post_config(hb_init, NULL, NULL, APR_HOOK_MIDDLE);
- ap_hook_child_init(hb_child_init, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_watchdog_need(hb_watchdog_need, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_watchdog_init(hb_watchdog_init, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_watchdog_step(hb_watchdog_step, NULL, NULL, APR_HOOK_MIDDLE);
+ ap_hook_watchdog_exit(hb_watchdog_exit, NULL, NULL, APR_HOOK_MIDDLE);
}
static void *hb_create_config(apr_pool_t *p, server_rec *s)
return "HeartbeatAddress: apr_sockaddr_info_get failed.";
}
- rv = apr_temp_dir_get(&tmpdir, cmd->temp_pool);
- if (rv) {
- return "HeartbeatAddress: unable to find temp directory.";
- }
-
- path = apr_pstrcat(cmd->temp_pool, tmpdir, "/hb-tmp.XXXXXX", NULL);
-
- rv = apr_file_mktemp(&ctx->lockf, path, 0, cmd->temp_pool);
-
- if (rv) {
- return "HeartbeatAddress: unable to allocate temp file.";
- }
-
- rv = apr_file_name_get(&ctx->mutex_path, ctx->lockf);
-
- if (rv) {
- return "HeartbeatAddress: unable to get lockf name.";
- }
-
- apr_file_close(ctx->lockf);
-
return NULL;
}