]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy_balancer.c
Add some more log message tags
[apache] / modules / proxy / mod_proxy_balancer.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* Load balancer module for Apache proxy */
18
19 #include "mod_proxy.h"
20 #include "scoreboard.h"
21 #include "ap_mpm.h"
22 #include "apr_version.h"
23 #include "ap_hooks.h"
24 #include "apr_date.h"
25
26 static const char *balancer_mutex_type = "proxy-balancer-shm";
27 ap_slotmem_provider_t *storage = NULL;
28
29 module AP_MODULE_DECLARE_DATA proxy_balancer_module;
30
31 static int (*ap_proxy_retry_worker_fn)(const char *proxy_function,
32         proxy_worker *worker, server_rec *s) = NULL;
33
34 /*
35  * Register our mutex type before the config is read so we
36  * can adjust the mutex settings using the Mutex directive.
37  */
38 static int balancer_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
39                                apr_pool_t *ptemp)
40 {
41
42     apr_status_t rv;
43
44     rv = ap_mutex_register(pconf, balancer_mutex_type, NULL,
45                                APR_LOCK_DEFAULT, 0);
46     if (rv != APR_SUCCESS) {
47         return rv;
48     }
49
50     return OK;
51 }
52
53 #if 0
54 extern void proxy_update_members(proxy_balancer **balancer, request_rec *r,
55                                  proxy_server_conf *conf);
56 #endif
57
58 static int proxy_balancer_canon(request_rec *r, char *url)
59 {
60     char *host, *path;
61     char *search = NULL;
62     const char *err;
63     apr_port_t port = 0;
64
65     /* TODO: offset of BALANCER_PREFIX ?? */
66     if (strncasecmp(url, "balancer:", 9) == 0) {
67         url += 9;
68     }
69     else {
70         return DECLINED;
71     }
72
73     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, "canonicalising URL %s", url);
74
75     /* do syntatic check.
76      * We break the URL into host, port, path, search
77      */
78     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
79     if (err) {
80         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01157)
81                       "error parsing URL %s: %s",
82                       url, err);
83         return HTTP_BAD_REQUEST;
84     }
85     /*
86      * now parse path/search args, according to rfc1738:
87      * process the path. With proxy-noncanon set (by
88      * mod_proxy) we use the raw, unparsed uri
89      */
90     if (apr_table_get(r->notes, "proxy-nocanon")) {
91         path = url;   /* this is the raw path */
92     }
93     else {
94         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
95                                  r->proxyreq);
96         search = r->args;
97     }
98     if (path == NULL)
99         return HTTP_BAD_REQUEST;
100
101     r->filename = apr_pstrcat(r->pool, "proxy:", BALANCER_PREFIX, host,
102             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
103
104     r->path_info = apr_pstrcat(r->pool, "/", path, NULL);
105
106     return OK;
107 }
108
109 static void init_balancer_members(apr_pool_t *p, server_rec *s,
110                                  proxy_balancer *balancer)
111 {
112     int i;
113     proxy_worker **workers;
114
115     workers = (proxy_worker **)balancer->workers->elts;
116
117     for (i = 0; i < balancer->workers->nelts; i++) {
118         int worker_is_initialized;
119         proxy_worker *worker = *workers;
120         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01158)
121                      "Looking at %s -> %s initialized?", balancer->s->name, worker->s->name);
122         worker_is_initialized = PROXY_WORKER_IS_INITIALIZED(worker);
123         if (!worker_is_initialized) {
124             ap_proxy_initialize_worker(worker, s, p);
125         }
126         ++workers;
127     }
128
129     /* Set default number of attempts to the number of
130      * workers.
131      */
132     if (!balancer->s->max_attempts_set && balancer->workers->nelts > 1) {
133         balancer->s->max_attempts = balancer->workers->nelts - 1;
134         balancer->s->max_attempts_set = 1;
135     }
136 }
137
138 /* Retrieve the parameter with the given name
139  * Something like 'JSESSIONID=12345...N'
140  */
141 static char *get_path_param(apr_pool_t *pool, char *url,
142                             const char *name, int scolon_sep)
143 {
144     char *path = NULL;
145     char *pathdelims = "?&";
146
147     if (scolon_sep) {
148         pathdelims = ";?&";
149     }
150     for (path = strstr(url, name); path; path = strstr(path + 1, name)) {
151         path += strlen(name);
152         if (*path == '=') {
153             /*
154              * Session path was found, get its value
155              */
156             ++path;
157             if (*path) {
158                 char *q;
159                 path = apr_strtok(apr_pstrdup(pool, path), pathdelims, &q);
160                 return path;
161             }
162         }
163     }
164     return NULL;
165 }
166
167 static char *get_cookie_param(request_rec *r, const char *name)
168 {
169     const char *cookies;
170     const char *start_cookie;
171
172     if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
173         for (start_cookie = ap_strstr_c(cookies, name); start_cookie;
174              start_cookie = ap_strstr_c(start_cookie + 1, name)) {
175             if (start_cookie == cookies ||
176                 start_cookie[-1] == ';' ||
177                 start_cookie[-1] == ',' ||
178                 isspace(start_cookie[-1])) {
179
180                 start_cookie += strlen(name);
181                 while(*start_cookie && isspace(*start_cookie))
182                     ++start_cookie;
183                 if (*start_cookie++ == '=' && *start_cookie) {
184                     /*
185                      * Session cookie was found, get its value
186                      */
187                     char *end_cookie, *cookie;
188                     cookie = apr_pstrdup(r->pool, start_cookie);
189                     if ((end_cookie = strchr(cookie, ';')) != NULL)
190                         *end_cookie = '\0';
191                     if((end_cookie = strchr(cookie, ',')) != NULL)
192                         *end_cookie = '\0';
193                     return cookie;
194                 }
195             }
196         }
197     }
198     return NULL;
199 }
200
201 /* Find the worker that has the 'route' defined
202  */
203 static proxy_worker *find_route_worker(proxy_balancer *balancer,
204                                        const char *route, request_rec *r)
205 {
206     int i;
207     int checking_standby;
208     int checked_standby;
209
210     proxy_worker **workers;
211
212     checking_standby = checked_standby = 0;
213     while (!checked_standby) {
214         workers = (proxy_worker **)balancer->workers->elts;
215         for (i = 0; i < balancer->workers->nelts; i++, workers++) {
216             proxy_worker *worker = *workers;
217             if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
218                 continue;
219             if (*(worker->s->route) && strcmp(worker->s->route, route) == 0) {
220                 if (worker && PROXY_WORKER_IS_USABLE(worker)) {
221                     return worker;
222                 } else {
223                     /*
224                      * If the worker is in error state run
225                      * retry on that worker. It will be marked as
226                      * operational if the retry timeout is elapsed.
227                      * The worker might still be unusable, but we try
228                      * anyway.
229                      */
230                     ap_proxy_retry_worker_fn("BALANCER", worker, r->server);
231                     if (PROXY_WORKER_IS_USABLE(worker)) {
232                             return worker;
233                     } else {
234                         /*
235                          * We have a worker that is unusable.
236                          * It can be in error or disabled, but in case
237                          * it has a redirection set use that redirection worker.
238                          * This enables to safely remove the member from the
239                          * balancer. Of course you will need some kind of
240                          * session replication between those two remote.
241                          */
242                         if (*worker->s->redirect) {
243                             proxy_worker *rworker = NULL;
244                             rworker = find_route_worker(balancer, worker->s->redirect, r);
245                             /* Check if the redirect worker is usable */
246                             if (rworker && !PROXY_WORKER_IS_USABLE(rworker)) {
247                                 /*
248                                  * If the worker is in error state run
249                                  * retry on that worker. It will be marked as
250                                  * operational if the retry timeout is elapsed.
251                                  * The worker might still be unusable, but we try
252                                  * anyway.
253                                  */
254                                 ap_proxy_retry_worker_fn("BALANCER", rworker, r->server);
255                             }
256                             if (rworker && PROXY_WORKER_IS_USABLE(rworker))
257                                 return rworker;
258                         }
259                     }
260                 }
261             }
262         }
263         checked_standby = checking_standby++;
264     }
265     return NULL;
266 }
267
268 static proxy_worker *find_session_route(proxy_balancer *balancer,
269                                         request_rec *r,
270                                         char **route,
271                                         const char **sticky_used,
272                                         char **url)
273 {
274     proxy_worker *worker = NULL;
275
276     if (!*balancer->s->sticky)
277         return NULL;
278     /* Try to find the sticky route inside url */
279     *route = get_path_param(r->pool, *url, balancer->s->sticky_path, balancer->s->scolonsep);
280     if (*route) {
281         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01159)
282                      "Found value %s for stickysession %s",
283                      *route, balancer->s->sticky_path);
284         *sticky_used =  balancer->s->sticky_path;
285     }
286     else {
287         *route = get_cookie_param(r, balancer->s->sticky);
288         if (*route) {
289             *sticky_used =  balancer->s->sticky;
290             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01160)
291                          "Found value %s for stickysession %s",
292                          *route, balancer->s->sticky);
293         }
294     }
295     /*
296      * If we found a value for sticksession, find the first '.' within.
297      * Everything after '.' (if present) is our route.
298      */
299     if ((*route) && ((*route = strchr(*route, '.')) != NULL ))
300         (*route)++;
301     if ((*route) && (**route)) {
302         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01161) "Found route %s", *route);
303         /* We have a route in path or in cookie
304          * Find the worker that has this route defined.
305          */
306         worker = find_route_worker(balancer, *route, r);
307         if (worker && strcmp(*route, worker->s->route)) {
308             /*
309              * Notice that the route of the worker chosen is different from
310              * the route supplied by the client.
311              */
312             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
313             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01162)
314                           "Route changed from %s to %s",
315                           *route, worker->s->route);
316         }
317         return worker;
318     }
319     else
320         return NULL;
321 }
322
323 static proxy_worker *find_best_worker(proxy_balancer *balancer,
324                                       request_rec *r)
325 {
326     proxy_worker *candidate = NULL;
327     apr_status_t rv;
328
329     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
330         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01163)
331                       "%s: Lock failed for find_best_worker()",
332                       balancer->s->name);
333         return NULL;
334     }
335
336     candidate = (*balancer->lbmethod->finder)(balancer, r);
337
338     if (candidate)
339         candidate->s->elected++;
340
341     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
342         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01164)
343                       "%s: Unlock failed for find_best_worker()",
344                       balancer->s->name);
345     }
346
347     if (candidate == NULL) {
348         /* All the workers are in error state or disabled.
349          * If the balancer has a timeout sleep for a while
350          * and try again to find the worker. The chances are
351          * that some other thread will release a connection.
352          * By default the timeout is not set, and the server
353          * returns SERVER_BUSY.
354          */
355         if (balancer->s->timeout) {
356             /* XXX: This can perhaps be build using some
357              * smarter mechanism, like tread_cond.
358              * But since the statuses can came from
359              * different childs, use the provided algo.
360              */
361             apr_interval_time_t timeout = balancer->s->timeout;
362             apr_interval_time_t step, tval = 0;
363             /* Set the timeout to 0 so that we don't
364              * end in infinite loop
365              */
366             balancer->s->timeout = 0;
367             step = timeout / 100;
368             while (tval < timeout) {
369                 apr_sleep(step);
370                 /* Try again */
371                 if ((candidate = find_best_worker(balancer, r)))
372                     break;
373                 tval += step;
374             }
375             /* restore the timeout */
376             balancer->s->timeout = timeout;
377         }
378     }
379
380     return candidate;
381
382 }
383
384 static int rewrite_url(request_rec *r, proxy_worker *worker,
385                         char **url)
386 {
387     const char *scheme = strstr(*url, "://");
388     const char *path = NULL;
389
390     if (scheme)
391         path = ap_strchr_c(scheme + 3, '/');
392
393     /* we break the URL into host, port, uri */
394     if (!worker) {
395         return ap_proxyerror(r, HTTP_BAD_REQUEST, apr_pstrcat(r->pool,
396                              "missing worker. URI cannot be parsed: ", *url,
397                              NULL));
398     }
399
400     *url = apr_pstrcat(r->pool, worker->s->name, path, NULL);
401
402     return OK;
403 }
404
405 static void force_recovery(proxy_balancer *balancer, server_rec *s)
406 {
407     int i;
408     int ok = 0;
409     proxy_worker **worker;
410
411     worker = (proxy_worker **)balancer->workers->elts;
412     for (i = 0; i < balancer->workers->nelts; i++, worker++) {
413         if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
414             ok = 1;
415             break;
416         }
417         else {
418             /* Try if we can recover */
419             ap_proxy_retry_worker_fn("BALANCER", *worker, s);
420             if (!((*worker)->s->status & PROXY_WORKER_IN_ERROR)) {
421                 ok = 1;
422                 break;
423             }
424         }
425     }
426     if (!ok) {
427         /* If all workers are in error state force the recovery.
428          */
429         worker = (proxy_worker **)balancer->workers->elts;
430         for (i = 0; i < balancer->workers->nelts; i++, worker++) {
431             ++(*worker)->s->retries;
432             (*worker)->s->status &= ~PROXY_WORKER_IN_ERROR;
433             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01165)
434                          "%s: Forcing recovery for worker (%s)",
435                          balancer->s->name, (*worker)->s->hostname);
436         }
437     }
438 }
439
440 static int proxy_balancer_pre_request(proxy_worker **worker,
441                                       proxy_balancer **balancer,
442                                       request_rec *r,
443                                       proxy_server_conf *conf, char **url)
444 {
445     int access_status;
446     proxy_worker *runtime;
447     char *route = NULL;
448     const char *sticky = NULL;
449     apr_status_t rv;
450
451     *worker = NULL;
452     /* Step 1: check if the url is for us
453      * The url we can handle starts with 'balancer://'
454      * If balancer is already provided skip the search
455      * for balancer, because this is failover attempt.
456      */
457     if (!*balancer &&
458         !(*balancer = ap_proxy_get_balancer(r->pool, conf, *url, 1)))
459         return DECLINED;
460
461     /* Step 2: Lock the LoadBalancer
462      * XXX: perhaps we need the process lock here
463      */
464     if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
465         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01166)
466                       "%s: Lock failed for pre_request", (*balancer)->s->name);
467         return DECLINED;
468     }
469
470     /* Step 3: force recovery */
471     force_recovery(*balancer, r->server);
472
473     /* Step 3.5: Update member list for the balancer */
474     /* TODO: Implement as provider! */
475     ap_proxy_sync_balancer(*balancer, r->server, conf);
476
477     /* Step 4: find the session route */
478     runtime = find_session_route(*balancer, r, &route, &sticky, url);
479     if (runtime) {
480         if ((*balancer)->lbmethod && (*balancer)->lbmethod->updatelbstatus) {
481             /* Call the LB implementation */
482             (*balancer)->lbmethod->updatelbstatus(*balancer, runtime, r->server);
483         }
484         else { /* Use the default one */
485             int i, total_factor = 0;
486             proxy_worker **workers;
487             /* We have a sticky load balancer
488              * Update the workers status
489              * so that even session routes get
490              * into account.
491              */
492             workers = (proxy_worker **)(*balancer)->workers->elts;
493             for (i = 0; i < (*balancer)->workers->nelts; i++) {
494                 /* Take into calculation only the workers that are
495                  * not in error state or not disabled.
496                  */
497                 if (PROXY_WORKER_IS_USABLE(*workers)) {
498                     (*workers)->s->lbstatus += (*workers)->s->lbfactor;
499                     total_factor += (*workers)->s->lbfactor;
500                 }
501                 workers++;
502             }
503             runtime->s->lbstatus -= total_factor;
504         }
505         runtime->s->elected++;
506
507         *worker = runtime;
508     }
509     else if (route && (*balancer)->s->sticky_force) {
510         int i, member_of = 0;
511         proxy_worker **workers;
512         /*
513          * We have a route provided that doesn't match the
514          * balancer name. See if the provider route is the
515          * member of the same balancer in which case return 503
516          */
517         workers = (proxy_worker **)(*balancer)->workers->elts;
518         for (i = 0; i < (*balancer)->workers->nelts; i++) {
519             if (*((*workers)->s->route) && strcmp((*workers)->s->route, route) == 0) {
520                 member_of = 1;
521                 break;
522             }
523             workers++;
524         }
525         if (member_of) {
526             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01167)
527                           "%s: All workers are in error state for route (%s)",
528                           (*balancer)->s->name, route);
529             if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
530                 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01168)
531                               "%s: Unlock failed for pre_request",
532                               (*balancer)->s->name);
533             }
534             return HTTP_SERVICE_UNAVAILABLE;
535         }
536     }
537
538     if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
539         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01169)
540                       "%s: Unlock failed for pre_request",
541                       (*balancer)->s->name);
542     }
543     if (!*worker) {
544         runtime = find_best_worker(*balancer, r);
545         if (!runtime) {
546             if ((*balancer)->workers->nelts) {
547                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01170)
548                               "%s: All workers are in error state",
549                               (*balancer)->s->name);
550             } else {
551                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01171)
552                               "%s: No workers in balancer",
553                               (*balancer)->s->name);
554             }
555
556             return HTTP_SERVICE_UNAVAILABLE;
557         }
558         if (*(*balancer)->s->sticky && runtime) {
559             /*
560              * This balancer has sticky sessions and the client either has not
561              * supplied any routing information or all workers for this route
562              * including possible redirect and hotstandby workers are in error
563              * state, but we have found another working worker for this
564              * balancer where we can send the request. Thus notice that we have
565              * changed the route to the backend.
566              */
567             apr_table_setn(r->subprocess_env, "BALANCER_ROUTE_CHANGED", "1");
568         }
569         *worker = runtime;
570     }
571
572     (*worker)->s->busy++;
573
574     /* Add balancer/worker info to env. */
575     apr_table_setn(r->subprocess_env,
576                    "BALANCER_NAME", (*balancer)->s->name);
577     apr_table_setn(r->subprocess_env,
578                    "BALANCER_WORKER_NAME", (*worker)->s->name);
579     apr_table_setn(r->subprocess_env,
580                    "BALANCER_WORKER_ROUTE", (*worker)->s->route);
581
582     /* Rewrite the url from 'balancer://url'
583      * to the 'worker_scheme://worker_hostname[:worker_port]/url'
584      * This replaces the balancers fictional name with the
585      * real hostname of the elected worker.
586      */
587     access_status = rewrite_url(r, *worker, url);
588     /* Add the session route to request notes if present */
589     if (route) {
590         apr_table_setn(r->notes, "session-sticky", sticky);
591         apr_table_setn(r->notes, "session-route", route);
592
593         /* Add session info to env. */
594         apr_table_setn(r->subprocess_env,
595                        "BALANCER_SESSION_STICKY", sticky);
596         apr_table_setn(r->subprocess_env,
597                        "BALANCER_SESSION_ROUTE", route);
598     }
599     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01172)
600                   "%s: worker (%s) rewritten to %s",
601                   (*balancer)->s->name, (*worker)->s->name, *url);
602
603     return access_status;
604 }
605
606 static int proxy_balancer_post_request(proxy_worker *worker,
607                                        proxy_balancer *balancer,
608                                        request_rec *r,
609                                        proxy_server_conf *conf)
610 {
611
612     apr_status_t rv;
613
614     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
615         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01173)
616                       "%s: Lock failed for post_request",
617                       balancer->s->name);
618         return HTTP_INTERNAL_SERVER_ERROR;
619     }
620
621     if (!apr_is_empty_array(balancer->errstatuses)) {
622         int i;
623         for (i = 0; i < balancer->errstatuses->nelts; i++) {
624             int val = ((int *)balancer->errstatuses->elts)[i];
625             if (r->status == val) {
626                 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01174)
627                               "%s:  Forcing recovery for worker (%s), "
628                               "failonstatus %d",
629                               balancer->s->name, worker->s->name, val);
630                 worker->s->status |= PROXY_WORKER_IN_ERROR;
631                 worker->s->error_time = apr_time_now();
632                 break;
633             }
634         }
635     }
636
637     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
638         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01175)
639                       "%s: Unlock failed for post_request", balancer->s->name);
640     }
641     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01176)
642                   "proxy_balancer_post_request for (%s)", balancer->s->name);
643
644     if (worker && worker->s->busy)
645         worker->s->busy--;
646
647     return OK;
648
649 }
650
651 static void recalc_factors(proxy_balancer *balancer)
652 {
653     int i;
654     proxy_worker **workers;
655
656
657     /* Recalculate lbfactors */
658     workers = (proxy_worker **)balancer->workers->elts;
659     /* Special case if there is only one worker its
660      * load factor will always be 1
661      */
662     if (balancer->workers->nelts == 1) {
663         (*workers)->s->lbstatus = (*workers)->s->lbfactor = 1;
664         return;
665     }
666     for (i = 0; i < balancer->workers->nelts; i++) {
667         /* Update the status entries */
668         workers[i]->s->lbstatus = workers[i]->s->lbfactor;
669     }
670 }
671
672 static apr_status_t lock_remove(void *data)
673 {
674     int i;
675     proxy_balancer *balancer;
676     server_rec *s = data;
677     void *sconf = s->module_config;
678     proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
679
680     balancer = (proxy_balancer *)conf->balancers->elts;
681     for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
682         if (balancer->gmutex) {
683             apr_global_mutex_destroy(balancer->gmutex);
684             balancer->gmutex = NULL;
685         }
686     }
687     return(0);
688 }
689
690 /* post_config hook: */
691 static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
692                          apr_pool_t *ptemp, server_rec *s)
693 {
694     apr_status_t rv;
695     void *sconf = s->module_config;
696     proxy_server_conf *conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
697     ap_slotmem_instance_t *new = NULL;
698     apr_time_t tstamp;
699
700     /* balancer_post_config() will be called twice during startup.  So, don't
701      * set up the static data the 1st time through. */
702     if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) {
703         return OK;
704     }
705
706     if (!ap_proxy_retry_worker_fn) {
707         ap_proxy_retry_worker_fn =
708                 APR_RETRIEVE_OPTIONAL_FN(ap_proxy_retry_worker);
709         if (!ap_proxy_retry_worker_fn) {
710             ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02230)
711                          "mod_proxy must be loaded for mod_proxy_balancer");
712             return !OK;
713         }
714     }
715
716     /*
717      * Get slotmem setups
718      */
719     storage = ap_lookup_provider(AP_SLOTMEM_PROVIDER_GROUP, "shm",
720                                  AP_SLOTMEM_PROVIDER_VERSION);
721     if (!storage) {
722         ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01177)
723                      "Failed to lookup provider 'shm' for '%s': is "
724                      "mod_slotmem_shm loaded??",
725                      AP_SLOTMEM_PROVIDER_GROUP);
726         return !OK;
727     }
728
729     tstamp = apr_time_now();
730     /*
731      * Go thru each Vhost and create the shared mem slotmem for
732      * each balancer's workers
733      */
734     while (s) {
735         int i,j;
736         proxy_balancer *balancer;
737         sconf = s->module_config;
738         conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
739
740         if (conf->balancers->nelts) {
741             conf->max_balancers = conf->balancers->nelts + conf->bgrowth;
742             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01178) "Doing balancers create: %d, %d (%d)",
743                          (int)ALIGNED_PROXY_BALANCER_SHARED_SIZE,
744                          (int)conf->balancers->nelts, conf->max_balancers);
745
746             rv = storage->create(&new, conf->id,
747                                  ALIGNED_PROXY_BALANCER_SHARED_SIZE,
748                                  conf->max_balancers, AP_SLOTMEM_TYPE_PREGRAB, pconf);
749             if (rv != APR_SUCCESS) {
750                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01179) "balancer slotmem_create failed");
751                 return !OK;
752             }
753             conf->bslot = new;
754         }
755         conf->storage = storage;
756
757         /* Initialize shared scoreboard data */
758         balancer = (proxy_balancer *)conf->balancers->elts;
759         for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
760             proxy_worker **workers;
761             proxy_worker *worker;
762             proxy_balancer_shared *bshm;
763             unsigned int index;
764
765             balancer->max_workers = balancer->workers->nelts + balancer->growth;
766
767             /* Create global mutex */
768             rv = ap_global_mutex_create(&(balancer->gmutex), NULL, balancer_mutex_type,
769                                         balancer->s->sname, s, pconf, 0);
770             if (rv != APR_SUCCESS || !balancer->gmutex) {
771                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01180)
772                              "mutex creation of %s : %s failed", balancer_mutex_type,
773                              balancer->s->sname);
774                 return HTTP_INTERNAL_SERVER_ERROR;
775             }
776
777             apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
778                                       apr_pool_cleanup_null);
779
780             /* setup shm for balancers */
781             if ((rv = storage->grab(conf->bslot, &index)) != APR_SUCCESS) {
782                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01181) "balancer slotmem_grab failed");
783                 return !OK;
784
785             }
786             if ((rv = storage->dptr(conf->bslot, index, (void *)&bshm)) != APR_SUCCESS) {
787                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01182) "balancer slotmem_dptr failed");
788                 return !OK;
789             }
790             if ((rv = ap_proxy_share_balancer(balancer, bshm, index)) != APR_SUCCESS) {
791                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01183) "Cannot share balancer");
792                 return !OK;
793             }
794
795             /* create slotmem slots for workers */
796             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01184) "Doing workers create: %s (%s), %d, %d",
797                          balancer->s->name, balancer->s->sname,
798                          (int)ALIGNED_PROXY_WORKER_SHARED_SIZE,
799                          (int)balancer->max_workers);
800
801             rv = storage->create(&new, balancer->s->sname,
802                                  ALIGNED_PROXY_WORKER_SHARED_SIZE,
803                                  balancer->max_workers, AP_SLOTMEM_TYPE_PREGRAB, pconf);
804             if (rv != APR_SUCCESS) {
805                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01185) "worker slotmem_create failed");
806                 return !OK;
807             }
808             balancer->wslot = new;
809             balancer->storage = storage;
810
811             /* sync all timestamps */
812             balancer->wupdated = balancer->s->wupdated = tstamp;
813
814             /* now go thru each worker */
815             workers = (proxy_worker **)balancer->workers->elts;
816             for (j = 0; j < balancer->workers->nelts; j++, workers++) {
817                 proxy_worker_shared *shm;
818
819                 worker = *workers;
820                 if ((rv = storage->grab(balancer->wslot, &index)) != APR_SUCCESS) {
821                     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01186) "worker slotmem_grab failed");
822                     return !OK;
823
824                 }
825                 if ((rv = storage->dptr(balancer->wslot, index, (void *)&shm)) != APR_SUCCESS) {
826                     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01187) "worker slotmem_dptr failed");
827                     return !OK;
828                 }
829                 if ((rv = ap_proxy_share_worker(worker, shm, index)) != APR_SUCCESS) {
830                     ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(01188) "Cannot share worker");
831                     return !OK;
832                 }
833                 worker->s->updated = tstamp;
834             }
835         }
836         s = s->next;
837     }
838
839     return OK;
840 }
841
842 static void create_radio(const char *name, unsigned int flag, request_rec *r)
843 {
844     ap_rvputs(r, "<td>On <input name='", name, "' id='", name, "' value='1' type=radio", NULL);
845     if (flag)
846         ap_rputs(" checked", r);
847     ap_rvputs(r, "> <br/> Off <input name='", name, "' id='", name, "' value='0' type=radio", NULL);
848     if (!flag)
849         ap_rputs(" checked", r);
850     ap_rputs("></td>\n", r);
851 }
852
853 static void push2table(const char *input, apr_table_t *params,
854                        const char *allowed[], apr_pool_t *p)
855 {
856     char *args;
857     char *tok, *val;
858     char *key;
859
860     if (input == NULL) {
861         return;
862     }
863     args = apr_pstrdup(p, input);
864
865     key = apr_strtok(args, "&", &tok);
866     while (key) {
867         val = strchr(key, '=');
868         if (val) {
869             *val++ = '\0';
870         }
871         else {
872             val = "";
873         }
874         ap_unescape_url(key);
875         ap_unescape_url(val);
876         if (allowed == NULL) { /* allow all */
877             apr_table_set(params, key, val);
878         }
879         else {
880             const char *ok = *allowed;
881             while (ok) {
882                 if (strcmp(ok, key) == 0) {
883                     apr_table_set(params, key, val);
884                     break;
885                 }
886                 ok++;
887             }
888         }
889         key = apr_strtok(NULL, "&", &tok);
890     }
891 }
892
893 /* Manages the loadfactors and member status
894  *   The balancer, worker and nonce are obtained from
895  *   the request args (?b=...&w=...&nonce=....).
896  *   All other params are pulled from any POST
897  *   data that exists.
898  * TODO:
899  *   /.../<whatever>/balancer/worker/nonce
900  */
901 static int balancer_handler(request_rec *r)
902 {
903     void *sconf;
904     proxy_server_conf *conf;
905     proxy_balancer *balancer, *bsel = NULL;
906     proxy_worker *worker, *wsel = NULL;
907     proxy_worker **workers = NULL;
908     apr_table_t *params;
909     int i, n;
910     int ok2change = 1;
911     const char *name;
912     const char *action;
913     apr_status_t rv;
914
915     /* is this for us? */
916     if (strcmp(r->handler, "balancer-manager")) {
917         return DECLINED;
918     }
919
920     r->allowed = 0
921     | (AP_METHOD_BIT << M_GET)
922     | (AP_METHOD_BIT << M_POST);
923     if ((r->method_number != M_GET) && (r->method_number != M_POST)) {
924         return DECLINED;
925     }
926
927     sconf = r->server->module_config;
928     conf = (proxy_server_conf *) ap_get_module_config(sconf, &proxy_module);
929     params = apr_table_make(r->pool, 10);
930
931     balancer = (proxy_balancer *)conf->balancers->elts;
932     for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
933         if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
934             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01189)
935                           "%s: Lock failed for balancer_handler",
936                           balancer->s->name);
937         }
938         ap_proxy_sync_balancer(balancer, r->server, conf);
939         if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
940             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01190)
941                           "%s: Unlock failed for balancer_handler",
942                           balancer->s->name);
943         }
944     }
945
946     if (r->args && (r->method_number == M_GET)) {
947         const char *allowed[] = { "w", "b", "nonce", NULL };
948         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01191) "parsing r->args");
949
950         push2table(r->args, params, allowed, r->pool);
951     }
952     if (r->method_number == M_POST) {
953         apr_bucket_brigade *ib;
954         apr_size_t len = 1024;
955         char *buf = apr_pcalloc(r->pool, len+1);
956
957         ib = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
958         rv = ap_get_brigade(r->input_filters, ib, AP_MODE_READBYTES,
959                                 APR_BLOCK_READ, len);
960         if (rv != APR_SUCCESS) {
961             return HTTP_INTERNAL_SERVER_ERROR;
962         }
963         apr_brigade_flatten(ib, buf, &len);
964         buf[len] = '\0';
965         push2table(buf, params, NULL, r->pool);
966     }
967     if ((name = apr_table_get(params, "b")))
968         bsel = ap_proxy_get_balancer(r->pool, conf,
969             apr_pstrcat(r->pool, BALANCER_PREFIX, name, NULL), 0);
970
971     if ((name = apr_table_get(params, "w"))) {
972         wsel = ap_proxy_get_worker(r->pool, bsel, conf, name);
973     }
974
975
976     /* Check that the supplied nonce matches this server's nonce;
977      * otherwise ignore all parameters, to prevent a CSRF attack. */
978     if (!bsel ||
979         (*bsel->s->nonce &&
980          (
981           (name = apr_table_get(params, "nonce")) == NULL ||
982           strcmp(bsel->s->nonce, name) != 0
983          )
984         )
985        ) {
986         apr_table_clear(params);
987         ok2change = 0;
988     }
989
990     /* First set the params */
991     if (wsel && ok2change) {
992         const char *val;
993         int was_usable = PROXY_WORKER_IS_USABLE(wsel);
994
995         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01192) "settings worker params");
996
997         if ((val = apr_table_get(params, "w_lf"))) {
998             int ival = atoi(val);
999             if (ival >= 1 && ival <= 100) {
1000                 wsel->s->lbfactor = ival;
1001                 if (bsel)
1002                     recalc_factors(bsel);
1003             }
1004         }
1005         if ((val = apr_table_get(params, "w_wr"))) {
1006             if (strlen(val) && strlen(val) < sizeof(wsel->s->route))
1007                 strcpy(wsel->s->route, val);
1008             else
1009                 *wsel->s->route = '\0';
1010         }
1011         if ((val = apr_table_get(params, "w_rr"))) {
1012             if (strlen(val) && strlen(val) < sizeof(wsel->s->redirect))
1013                 strcpy(wsel->s->redirect, val);
1014             else
1015                 *wsel->s->redirect = '\0';
1016         }
1017         if ((val = apr_table_get(params, "w_status_I"))) {
1018             ap_proxy_set_wstatus('I', atoi(val), wsel);
1019         }
1020         if ((val = apr_table_get(params, "w_status_N"))) {
1021             ap_proxy_set_wstatus('N', atoi(val), wsel);
1022         }
1023         if ((val = apr_table_get(params, "w_status_D"))) {
1024             ap_proxy_set_wstatus('D', atoi(val), wsel);
1025         }
1026         if ((val = apr_table_get(params, "w_status_H"))) {
1027             ap_proxy_set_wstatus('H', atoi(val), wsel);
1028         }
1029         if ((val = apr_table_get(params, "w_ls"))) {
1030             int ival = atoi(val);
1031             if (ival >= 0 && ival <= 99) {
1032                 wsel->s->lbset = ival;
1033              }
1034         }
1035         /* if enabling, we need to reset all lb params */
1036         if (bsel && !was_usable && PROXY_WORKER_IS_USABLE(wsel)) {
1037             bsel->s->need_reset = 1;
1038         }
1039
1040     }
1041
1042     if (bsel && ok2change) {
1043         const char *val;
1044         int ival;
1045         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01193)
1046                       "settings balancer params");
1047         if ((val = apr_table_get(params, "b_lbm"))) {
1048             if ((strlen(val) < (sizeof(bsel->s->lbpname)-1)) &&
1049                 strcmp(val, bsel->s->lbpname)) {
1050                 proxy_balancer_method *lbmethod;
1051                 lbmethod = ap_lookup_provider(PROXY_LBMETHOD, val, "0");
1052                 if (lbmethod) {
1053                     PROXY_STRNCPY(bsel->s->lbpname, val);
1054                     bsel->lbmethod = lbmethod;
1055                     bsel->s->wupdated = apr_time_now();
1056                     bsel->s->need_reset = 1;
1057                 }
1058             }
1059         }
1060         if ((val = apr_table_get(params, "b_tmo"))) {
1061             ival = atoi(val);
1062             if (ival >= 0 && ival <= 7200) { /* 2 hrs enuff? */
1063                 bsel->s->timeout = apr_time_from_sec(ival);
1064             }
1065         }
1066         if ((val = apr_table_get(params, "b_max"))) {
1067             ival = atoi(val);
1068             if (ival >= 0 && ival <= 99) {
1069                 bsel->s->max_attempts = ival;
1070             }
1071         }
1072         if ((val = apr_table_get(params, "b_sforce"))) {
1073             ival = atoi(val);
1074             bsel->s->sticky_force = (ival != 0);
1075         }
1076         if ((val = apr_table_get(params, "b_ss")) && *val) {
1077             if (strlen(val) < (sizeof(bsel->s->sticky_path)-1)) {
1078                 if (*val == '-' && *(val+1) == '\0')
1079                     *bsel->s->sticky_path = *bsel->s->sticky = '\0';
1080                 else {
1081                     char *path;
1082                     PROXY_STRNCPY(bsel->s->sticky_path, val);
1083                     PROXY_STRNCPY(bsel->s->sticky, val);
1084
1085                     if ((path = strchr((char *)bsel->s->sticky, '|'))) {
1086                         *path++ = '\0';
1087                         PROXY_STRNCPY(bsel->s->sticky_path, path);
1088                     }
1089                 }
1090             }
1091         }
1092         if ((val = apr_table_get(params, "b_wyes")) &&
1093             (*val == '1' && *(val+1) == '\0') &&
1094             (val = apr_table_get(params, "b_nwrkr"))) {
1095             char *ret;
1096             proxy_worker *nworker;
1097             nworker = ap_proxy_get_worker(conf->pool, bsel, conf, val);
1098             if (!nworker && storage->num_free_slots(bsel->wslot)) {
1099                 if ((rv = PROXY_GLOBAL_LOCK(bsel)) != APR_SUCCESS) {
1100                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01194)
1101                                   "%s: Lock failed for adding worker",
1102                                   bsel->s->name);
1103                 }
1104                 ret = ap_proxy_define_worker(conf->pool, &nworker, bsel, conf, val, 0);
1105                 if (!ret) {
1106                     unsigned int index;
1107                     proxy_worker_shared *shm;
1108                     PROXY_COPY_CONF_PARAMS(nworker, conf);
1109                     if ((rv = storage->grab(bsel->wslot, &index)) != APR_SUCCESS) {
1110                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01195)
1111                                       "worker slotmem_grab failed");
1112                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1113                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01196)
1114                                           "%s: Unlock failed for adding worker",
1115                                           bsel->s->name);
1116                         }
1117                         return HTTP_BAD_REQUEST;
1118                     }
1119                     if ((rv = storage->dptr(bsel->wslot, index, (void *)&shm)) != APR_SUCCESS) {
1120                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01197)
1121                                       "worker slotmem_dptr failed");
1122                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1123                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01198)
1124                                           "%s: Unlock failed for adding worker",
1125                                           bsel->s->name);
1126                         }
1127                         return HTTP_BAD_REQUEST;
1128                     }
1129                     if ((rv = ap_proxy_share_worker(nworker, shm, index)) != APR_SUCCESS) {
1130                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01199)
1131                                       "Cannot share worker");
1132                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1133                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01200)
1134                                           "%s: Unlock failed for adding worker",
1135                                           bsel->s->name);
1136                         }
1137                         return HTTP_BAD_REQUEST;
1138                     }
1139                     if ((rv = ap_proxy_initialize_worker(nworker, r->server, conf->pool)) != APR_SUCCESS) {
1140                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01201)
1141                                       "Cannot init worker");
1142                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1143                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01202)
1144                                           "%s: Unlock failed for adding worker",
1145                                           bsel->s->name);
1146                         }
1147                         return HTTP_BAD_REQUEST;
1148                     }
1149                     /* sync all timestamps */
1150                     bsel->wupdated = bsel->s->wupdated = nworker->s->updated = apr_time_now();
1151                     /* by default, all new workers are disabled */
1152                     ap_proxy_set_wstatus('D', 1, nworker);
1153                 }
1154                 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
1155                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01203)
1156                                   "%s: Unlock failed for adding worker",
1157                                   bsel->s->name);
1158                 }
1159             }
1160
1161         }
1162
1163     }
1164
1165     action = ap_construct_url(r->pool, r->uri, r);
1166     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01204) "genning page");
1167
1168     if (apr_table_get(params, "xml")) {
1169         ap_set_content_type(r, "text/xml");
1170         ap_rputs("<?xml version='1.0' encoding='UTF-8' ?>\n", r);
1171         ap_rputs("<httpd:manager xmlns:httpd='http://httpd.apache.org'>\n", r);
1172         ap_rputs("  <httpd:balancers>\n", r);
1173         balancer = (proxy_balancer *)conf->balancers->elts;
1174         for (i = 0; i < conf->balancers->nelts; i++) {
1175             ap_rputs("    <httpd:balancer>\n", r);
1176             ap_rvputs(r, "      <httpd:name>", balancer->s->name, "</httpd:name>\n", NULL);
1177             ap_rputs("      <httpd:workers>\n", r);
1178             workers = (proxy_worker **)balancer->workers->elts;
1179             for (n = 0; n < balancer->workers->nelts; n++) {
1180                 worker = *workers;
1181                 ap_rputs("        <httpd:worker>\n", r);
1182                 ap_rvputs(r, "          <httpd:scheme>", worker->s->scheme,
1183                           "</httpd:scheme>\n", NULL);
1184                 ap_rvputs(r, "          <httpd:hostname>", worker->s->hostname,
1185                           "</httpd:hostname>\n", NULL);
1186                 ap_rprintf(r, "          <httpd:loadfactor>%d</httpd:loadfactor>\n",
1187                           worker->s->lbfactor);
1188                 ap_rputs("        </httpd:worker>\n", r);
1189                 ++workers;
1190             }
1191             ap_rputs("      </httpd:workers>\n", r);
1192             ap_rputs("    </httpd:balancer>\n", r);
1193             ++balancer;
1194         }
1195         ap_rputs("  </httpd:balancers>\n", r);
1196         ap_rputs("</httpd:manager>", r);
1197     }
1198     else {
1199         ap_set_content_type(r, "text/html; charset=ISO-8859-1");
1200         ap_rputs(DOCTYPE_HTML_3_2
1201                  "<html><head><title>Balancer Manager</title>\n", r);
1202         ap_rputs("<style type='text/css'>\n"
1203                  "table {\n"
1204                  " border-width: 1px;\n"
1205                  " border-spacing: 3px;\n"
1206                  " border-style: solid;\n"
1207                  " border-color: gray;\n"
1208                  " border-collapse: collapse;\n"
1209                  " background-color: white;\n"
1210                  " text-align: center;\n"
1211                  "}\n"
1212                  "th {\n"
1213                  " border-width: 1px;\n"
1214                  " padding: 2px;\n"
1215                  " border-style: dotted;\n"
1216                  " border-color: gray;\n"
1217                  " background-color: white;\n"
1218                  " text-align: center;\n"
1219                  "}\n"
1220                  "td {\n"
1221                  " border-width: 1px;\n"
1222                  " padding: 2px;\n"
1223                  " border-style: dotted;\n"
1224                  " border-color: gray;\n"
1225                  " background-color: white;\n"
1226                  " text-align: center;\n"
1227                  "}\n"
1228                  "</style>\n</head>\n", r);
1229         ap_rputs("<body><h1>Load Balancer Manager for ", r);
1230         ap_rvputs(r, ap_get_server_name(r), "</h1>\n\n", NULL);
1231         ap_rvputs(r, "<dl><dt>Server Version: ",
1232                   ap_get_server_description(), "</dt>\n", NULL);
1233         ap_rvputs(r, "<dt>Server Built: ",
1234                   ap_get_server_built(), "\n</dt></dl>\n", NULL);
1235         balancer = (proxy_balancer *)conf->balancers->elts;
1236         for (i = 0; i < conf->balancers->nelts; i++) {
1237
1238             ap_rputs("<hr />\n<h3>LoadBalancer Status for ", r);
1239             ap_rvputs(r, "<a href='", r->uri, "?b=",
1240                       balancer->s->name + sizeof(BALANCER_PREFIX) - 1,
1241                       "&nonce=", balancer->s->nonce,
1242                       "'>", NULL);
1243             ap_rvputs(r, balancer->s->name, "</a></h3>\n", NULL);
1244             ap_rputs("\n\n<table><tr>"
1245                 "<th>MaxMembers</th><th>StickySession</th><th>DisableFailover</th><th>Timeout</th><th>FailoverAttempts</th><th>Method</th>"
1246                 "<th>Path</th><th>Active</th></tr>\n<tr>", r);
1247             /* the below is a safe cast, since the number of slots total will
1248              * never be more than max_workers, which is restricted to int */
1249             ap_rprintf(r, "<td>%d [%d Used]</td>\n", balancer->max_workers,
1250                        balancer->max_workers - (int)storage->num_free_slots(balancer->wslot));
1251             if (*balancer->s->sticky) {
1252                 if (strcmp(balancer->s->sticky, balancer->s->sticky_path)) {
1253                     ap_rvputs(r, "<td>", balancer->s->sticky, " | ",
1254                               balancer->s->sticky_path, NULL);
1255                 }
1256                 else {
1257                     ap_rvputs(r, "<td>", balancer->s->sticky, NULL);
1258                 }
1259             }
1260             else {
1261                 ap_rputs("<td> (None) ", r);
1262             }
1263             ap_rprintf(r, "<td>%s</td>\n",
1264                        balancer->s->sticky_force ? "On" : "Off");
1265             ap_rprintf(r, "</td><td>%" APR_TIME_T_FMT "</td>",
1266                 apr_time_sec(balancer->s->timeout));
1267             ap_rprintf(r, "<td>%d</td>\n", balancer->s->max_attempts);
1268             ap_rprintf(r, "<td>%s</td>\n",
1269                        balancer->s->lbpname);
1270             ap_rputs("<td>", r);
1271             if (balancer->s->vhost && *(balancer->s->vhost)) {
1272                 ap_rvputs(r, balancer->s->vhost, " -> ", NULL);
1273             }
1274             ap_rvputs(r, balancer->s->vpath, "</td>\n", NULL);
1275             ap_rprintf(r, "<td>%s</td>\n",
1276                        !balancer->s->inactive ? "Yes" : "No");
1277             ap_rputs("</table>\n<br />", r);
1278             ap_rputs("\n\n<table><tr>"
1279                 "<th>Worker URL</th>"
1280                 "<th>Route</th><th>RouteRedir</th>"
1281                 "<th>Factor</th><th>Set</th><th>Status</th>"
1282                 "<th>Elected</th><th>Busy</th><th>Load</th><th>To</th><th>From</th>"
1283                 "</tr>\n", r);
1284
1285             workers = (proxy_worker **)balancer->workers->elts;
1286             for (n = 0; n < balancer->workers->nelts; n++) {
1287                 char fbuf[50];
1288                 worker = *workers;
1289                 ap_rvputs(r, "<tr>\n<td><a href='", r->uri, "?b=",
1290                           balancer->s->name + sizeof(BALANCER_PREFIX) - 1, "&w=",
1291                           ap_escape_uri(r->pool, worker->s->name),
1292                           "&nonce=", balancer->s->nonce,
1293                           "'>", NULL);
1294                 ap_rvputs(r, worker->s->name, "</a></td>", NULL);
1295                 ap_rvputs(r, "<td>", ap_escape_html(r->pool, worker->s->route),
1296                           NULL);
1297                 ap_rvputs(r, "</td><td>",
1298                           ap_escape_html(r->pool, worker->s->redirect), NULL);
1299                 ap_rprintf(r, "</td><td>%d</td>", worker->s->lbfactor);
1300                 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbset);
1301                 ap_rvputs(r, ap_proxy_parse_wstatus(r->pool, worker), NULL);
1302                 ap_rputs("</td>", r);
1303                 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->elected);
1304                 ap_rprintf(r, "<td>%" APR_SIZE_T_FMT "</td>", worker->s->busy);
1305                 ap_rprintf(r, "<td>%d</td><td>", worker->s->lbstatus);
1306                 ap_rputs(apr_strfsize(worker->s->transferred, fbuf), r);
1307                 ap_rputs("</td><td>", r);
1308                 ap_rputs(apr_strfsize(worker->s->read, fbuf), r);
1309                 ap_rputs("</td></tr>\n", r);
1310
1311                 ++workers;
1312             }
1313             ap_rputs("</table>\n", r);
1314             ++balancer;
1315         }
1316         ap_rputs("<hr />\n", r);
1317         if (wsel && bsel) {
1318             ap_rputs("<h3>Edit worker settings for ", r);
1319             ap_rvputs(r, wsel->s->name, "</h3>\n", NULL);
1320             ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1321             ap_rvputs(r, action, "'>\n", NULL);
1322             ap_rputs("<dl>\n<table><tr><td>Load factor:</td><td><input name='w_lf' id='w_lf' type=text ", r);
1323             ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbfactor);
1324             ap_rputs("<tr><td>LB Set:</td><td><input name='w_ls' id='w_ls' type=text ", r);
1325             ap_rprintf(r, "value='%d'></td></tr>\n", wsel->s->lbset);
1326             ap_rputs("<tr><td>Route:</td><td><input name='w_wr' id='w_wr' type=text ", r);
1327             ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->route),
1328                       NULL);
1329             ap_rputs("'></td></tr>\n", r);
1330             ap_rputs("<tr><td>Route Redirect:</td><td><input name='w_rr' id='w_rr' type=text ", r);
1331             ap_rvputs(r, "value='", ap_escape_html(r->pool, wsel->s->redirect),
1332                       NULL);
1333             ap_rputs("'></td></tr>\n", r);
1334             ap_rputs("<tr><td>Status:</td>", r);
1335             ap_rputs("<td><table><tr><th>Ign</th><th>Drn</th><th>Dis</th><th>Stby</th></tr>\n<tr>", r);
1336             create_radio("w_status_I", (PROXY_WORKER_IGNORE_ERRORS & wsel->s->status), r);
1337             create_radio("w_status_N", (PROXY_WORKER_DRAIN & wsel->s->status), r);
1338             create_radio("w_status_D", (PROXY_WORKER_DISABLED & wsel->s->status), r);
1339             create_radio("w_status_H", (PROXY_WORKER_HOT_STANDBY & wsel->s->status), r);
1340             ap_rputs("</tr></table>\n", r);
1341             ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1342             ap_rvputs(r, "</table>\n<input type=hidden name='w' id='w' ",  NULL);
1343             ap_rvputs(r, "value='", ap_escape_uri(r->pool, wsel->s->name), "'>\n", NULL);
1344             ap_rvputs(r, "<input type=hidden name='b' id='b' ", NULL);
1345             ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1346                       "'>\n", NULL);
1347             ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1348                       bsel->s->nonce, "'>\n", NULL);
1349             ap_rvputs(r, "</form>\n", NULL);
1350             ap_rputs("<hr />\n", r);
1351         } else if (bsel) {
1352             const apr_array_header_t *provs;
1353             const ap_list_provider_names_t *pname;
1354             int i;
1355             ap_rputs("<h3>Edit balancer settings for ", r);
1356             ap_rvputs(r, bsel->s->name, "</h3>\n", NULL);
1357             ap_rputs("<form method='POST' enctype='application/x-www-form-urlencoded' action='", r);
1358             ap_rvputs(r, action, "'>\n", NULL);
1359             ap_rputs("<dl>\n<table>\n", r);
1360             provs = ap_list_provider_names(r->pool, PROXY_LBMETHOD, "0");
1361             if (provs) {
1362                 ap_rputs("<tr><td>LBmethod:</td>", r);
1363                 ap_rputs("<td>\n<select name='b_lbm' id='b_lbm'>", r);
1364                 pname = (ap_list_provider_names_t *)provs->elts;
1365                 for (i = 0; i < provs->nelts; i++, pname++) {
1366                     ap_rvputs(r,"<option value='", pname->provider_name, "'", NULL);
1367                     if (strcmp(pname->provider_name, bsel->s->lbpname) == 0)
1368                         ap_rputs(" selected ", r);
1369                     ap_rvputs(r, ">", pname->provider_name, "\n", NULL);
1370                 }
1371                 ap_rputs("</select>\n</td></tr>\n", r);
1372             }
1373             ap_rputs("<tr><td>Timeout:</td><td><input name='b_tmo' id='b_tmo' type=text ", r);
1374             ap_rprintf(r, "value='%" APR_TIME_T_FMT "'></td></tr>\n", apr_time_sec(bsel->s->timeout));
1375             ap_rputs("<tr><td>Failover Attempts:</td><td><input name='b_max' id='b_max' type=text ", r);
1376             ap_rprintf(r, "value='%d'></td></tr>\n", bsel->s->max_attempts);
1377             ap_rputs("<tr><td>Disable Failover:</td>", r);
1378             create_radio("b_sforce", bsel->s->sticky_force, r);
1379             ap_rputs("<tr><td>Sticky Session:</td><td><input name='b_ss' id='b_ss' size=64 type=text ", r);
1380             if (strcmp(bsel->s->sticky, bsel->s->sticky_path)) {
1381                 ap_rvputs(r, "value ='", bsel->s->sticky, " | ",
1382                           bsel->s->sticky_path, NULL);
1383             }
1384             else {
1385                 ap_rvputs(r, "value ='", bsel->s->sticky, NULL);
1386             }
1387             ap_rputs("'>&nbsp;&nbsp;&nbsp;&nbsp;(Use '-' to delete)</td></tr>\n", r);
1388             if (storage->num_free_slots(bsel->wslot) != 0) {
1389                 ap_rputs("<tr><td>Add New Worker:</td><td><input name='b_nwrkr' id='b_nwrkr' size=32 type=text>"
1390                          "&nbsp;&nbsp;&nbsp;&nbsp;Are you sure? <input name='b_wyes' id='b_wyes' type=checkbox value='1'>"
1391                          "</td></tr>", r);
1392             }
1393             ap_rputs("<tr><td colspan=2><input type=submit value='Submit'></td></tr>\n", r);
1394             ap_rvputs(r, "</table>\n<input type=hidden name='b' id='b' ", NULL);
1395             ap_rvputs(r, "value='", bsel->s->name + sizeof(BALANCER_PREFIX) - 1,
1396                       "'>\n", NULL);
1397             ap_rvputs(r, "<input type=hidden name='nonce' id='nonce' value='",
1398                       bsel->s->nonce, "'>\n", NULL);
1399             ap_rvputs(r, "</form>\n", NULL);
1400             ap_rputs("<hr />\n", r);
1401         }
1402         ap_rputs(ap_psignature("",r), r);
1403         ap_rputs("</body></html>\n", r);
1404         ap_rflush(r);
1405     }
1406     return DONE;
1407 }
1408
1409 static void balancer_child_init(apr_pool_t *p, server_rec *s)
1410 {
1411     while (s) {
1412         proxy_balancer *balancer;
1413         int i;
1414         void *sconf = s->module_config;
1415         proxy_server_conf *conf = (proxy_server_conf *)ap_get_module_config(sconf, &proxy_module);
1416         apr_status_t rv;
1417
1418         if (conf->balancers->nelts) {
1419             apr_size_t size;
1420             unsigned int num;
1421             storage->attach(&(conf->bslot), conf->id, &size, &num, p);
1422             if (!conf->bslot) {
1423                 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01205) "slotmem_attach failed");
1424                 exit(1); /* Ugly, but what else? */
1425             }
1426         }
1427
1428         balancer = (proxy_balancer *)conf->balancers->elts;
1429         for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
1430             rv = ap_proxy_initialize_balancer(balancer, s, p);
1431
1432             if (rv != APR_SUCCESS) {
1433                 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(01206)
1434                              "Failed to init balancer %s in child",
1435                              balancer->s->name);
1436                 exit(1); /* Ugly, but what else? */
1437             }
1438             init_balancer_members(conf->pool, s, balancer);
1439         }
1440         s = s->next;
1441     }
1442
1443 }
1444
1445 static void ap_proxy_balancer_register_hook(apr_pool_t *p)
1446 {
1447     /* Only the mpm_winnt has child init hook handler.
1448      * make sure that we are called after the mpm
1449      * initializes
1450      */
1451     static const char *const aszPred[] = { "mpm_winnt.c", "mod_slotmem_shm.c", NULL};
1452      /* manager handler */
1453     ap_hook_post_config(balancer_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1454     ap_hook_pre_config(balancer_pre_config, NULL, NULL, APR_HOOK_MIDDLE);
1455     ap_hook_handler(balancer_handler, NULL, NULL, APR_HOOK_FIRST);
1456     ap_hook_child_init(balancer_child_init, aszPred, NULL, APR_HOOK_MIDDLE);
1457     proxy_hook_pre_request(proxy_balancer_pre_request, NULL, NULL, APR_HOOK_FIRST);
1458     proxy_hook_post_request(proxy_balancer_post_request, NULL, NULL, APR_HOOK_FIRST);
1459     proxy_hook_canon_handler(proxy_balancer_canon, NULL, NULL, APR_HOOK_FIRST);
1460 }
1461
1462 AP_DECLARE_MODULE(proxy_balancer) = {
1463     STANDARD20_MODULE_STUFF,
1464     NULL,       /* create per-directory config structure */
1465     NULL,       /* merge per-directory config structures */
1466     NULL,       /* create per-server config structure */
1467     NULL,       /* merge per-server config structures */
1468     NULL,       /* command apr_table_t */
1469     ap_proxy_balancer_register_hook /* register hooks */
1470 };