blob: b3f2c76a4937a7dd20553adf3ee6fe69ceb9224e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * AppSession functions.
3 *
4 * Copyright 2004-2006 Alexander Lazic, Klaus Wagner
Willy Tarreau63963c62007-05-13 21:29:55 +02005 * Copyright 2006-2007 Willy Tarreau
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <stdio.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020015#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/appsession.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
Willy Tarreau63963c62007-05-13 21:29:55 +020019#include <common/memory.h>
Willy Tarreau51041c72007-09-09 21:56:53 +020020#include <common/sessionhash.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020021#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025
Willy Tarreauec6c5df2008-07-15 00:22:45 +020026#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <proto/task.h>
28
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020029static struct task *appsess_refresh = NULL;
Willy Tarreau63963c62007-05-13 21:29:55 +020030struct pool_head *pool2_appsess;
Willy Tarreaubaaee002006-06-26 02:48:02 +020031struct app_pool apools;
32int have_appsession;
33
Willy Tarreaubaaee002006-06-26 02:48:02 +020034int appsession_init(void)
35{
36 static int initialized = 0;
37 int idlen;
38 struct server *s;
39 struct proxy *p = proxy;
40
41 if (!initialized) {
Willy Tarreau63963c62007-05-13 21:29:55 +020042 pool2_appsess = create_pool("appsess", sizeof(appsess), MEM_F_SHARED);
43 if (pool2_appsess == NULL)
44 return -1;
45
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 if (!appsession_task_init()) {
Willy Tarreau63963c62007-05-13 21:29:55 +020047 int ser_msize, ses_msize;
48
Willy Tarreaubaaee002006-06-26 02:48:02 +020049 apools.sessid = NULL;
50 apools.serverid = NULL;
Willy Tarreau63963c62007-05-13 21:29:55 +020051
52 ser_msize = sizeof(void *);
53 ses_msize = sizeof(void *);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 while (p) {
55 s = p->srv;
Willy Tarreau63963c62007-05-13 21:29:55 +020056 if (ses_msize < p->appsession_len)
57 ses_msize = p->appsession_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 while (s) {
59 idlen = strlen(s->id);
Willy Tarreau63963c62007-05-13 21:29:55 +020060 if (ser_msize < idlen)
61 ser_msize = idlen;
Willy Tarreaubaaee002006-06-26 02:48:02 +020062 s = s->next;
63 }
64 p = p->next;
65 }
66 /* we use strings, so reserve space for '\0' */
Willy Tarreau63963c62007-05-13 21:29:55 +020067 ser_msize ++;
68 ses_msize ++;
69
70 apools.sessid = create_pool("sessid", ses_msize, MEM_F_SHARED);
71 if (!apools.sessid)
72 return -1;
73 apools.serverid = create_pool("serverid", ser_msize, MEM_F_SHARED);
74 if (!apools.serverid)
75 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 }
77 else {
78 fprintf(stderr, "appsession_task_init failed\n");
79 return -1;
80 }
81 initialized ++;
82 }
83 return 0;
84}
85
86int appsession_task_init(void)
87{
88 static int initialized = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +020089 if (!initialized) {
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020090 if ((appsess_refresh = pool_alloc2(pool2_task)) == NULL)
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 return -1;
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020092
Willy Tarreau9789f7b2008-06-24 08:17:16 +020093 task_init(appsess_refresh);
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020094 appsess_refresh->context = NULL;
Willy Tarreau0c303ee2008-07-07 00:09:58 +020095 appsess_refresh->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT));
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +020096 appsess_refresh->process = appsession_refresh;
97 task_queue(appsess_refresh);
Willy Tarreaubaaee002006-06-26 02:48:02 +020098 initialized ++;
99 }
100 return 0;
101}
102
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200103void appsession_refresh(struct task *t, int *next)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104{
Willy Tarreau51041c72007-09-09 21:56:53 +0200105 struct proxy *p = proxy;
106 struct appsession_hash *htbl;
107 appsess *element, *back;
108 int i;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
110 while (p) {
111 if (p->appsession_name != NULL) {
112 htbl = &p->htbl_proxy;
Willy Tarreau51041c72007-09-09 21:56:53 +0200113 as_hash_for_each_entry_safe(i, element, back, &p->htbl_proxy, hash_list) {
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200114 if (tick_is_expired(element->expire, now_ms)) {
Willy Tarreau51041c72007-09-09 21:56:53 +0200115 if ((global.mode & MODE_DEBUG) &&
116 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
117 int len;
118 /*
119 on Linux NULL pointers are caught by sprintf, on solaris -> segfault
120 */
121 len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n",
122 element->sessid, element->serverid?element->serverid:"(null)");
123 write(1, trash, len);
124 }
125 /* delete the expired element from within the hash table */
126 LIST_DEL(&element->hash_list);
127 htbl->destroy(element);
128 }/* end if (tv_isle(&asession->expire, &now)) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 }
130 }
131 p = p->next;
132 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200133 t->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT)); /* check expiration every 5 seconds */
Willy Tarreau63963c62007-05-13 21:29:55 +0200134 task_queue(t);
Willy Tarreaud825eef2007-05-12 22:35:00 +0200135 *next = t->expire;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136} /* end appsession_refresh */
137
138int match_str(const void *key1, const void *key2)
139{
140 appsess *temp1,*temp2;
141 temp1 = (appsess *)key1;
142 temp2 = (appsess *)key2;
143
144 //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid);
145 //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid);
146
147 return (strcmp(temp1->sessid,temp2->sessid) == 0);
148}/* end match_str */
149
Willy Tarreau51041c72007-09-09 21:56:53 +0200150void destroy(appsess *temp1) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 if (temp1->sessid)
Willy Tarreau63963c62007-05-13 21:29:55 +0200152 pool_free2(apools.sessid, temp1->sessid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153
154 if (temp1->serverid)
Willy Tarreau63963c62007-05-13 21:29:55 +0200155 pool_free2(apools.serverid, temp1->serverid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156
Willy Tarreau63963c62007-05-13 21:29:55 +0200157 pool_free2(pool2_appsess, temp1);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158} /* end destroy */
159
160void appsession_cleanup( void )
161{
162 struct proxy *p = proxy;
163
164 while(p) {
Willy Tarreau51041c72007-09-09 21:56:53 +0200165 appsession_hash_destroy(&(p->htbl_proxy));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 p = p->next;
167 }
Krzysztof Piotr Oledzkia643baf2008-05-29 23:53:44 +0200168
169 if (appsess_refresh) {
170 task_delete(appsess_refresh);
171 task_free(appsess_refresh);
172 appsess_refresh = NULL;
173 }
174
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175}/* end appsession_cleanup() */
176
177
178
179/*
180 * Local variables:
181 * c-indent-level: 8
182 * c-basic-offset: 8
183 * End:
184 */