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