Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AppSession functions. |
| 3 | * |
| 4 | * Copyright 2004-2006 Alexander Lazic, Klaus Wagner |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5 | * Copyright 2006-2007 Willy Tarreau |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6 | * |
| 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 15 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 16 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/appsession.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 18 | #include <common/config.h> |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 19 | #include <common/memory.h> |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 20 | #include <common/sessionhash.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 21 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
| 24 | #include <types/buffers.h> |
| 25 | #include <types/global.h> |
| 26 | #include <types/proxy.h> |
| 27 | #include <types/server.h> |
| 28 | |
| 29 | #include <proto/task.h> |
| 30 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 31 | static struct task *appsess_refresh = NULL; |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 32 | struct pool_head *pool2_appsess; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | struct app_pool apools; |
| 34 | int have_appsession; |
| 35 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | int appsession_init(void) |
| 37 | { |
| 38 | static int initialized = 0; |
| 39 | int idlen; |
| 40 | struct server *s; |
| 41 | struct proxy *p = proxy; |
| 42 | |
| 43 | if (!initialized) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 44 | pool2_appsess = create_pool("appsess", sizeof(appsess), MEM_F_SHARED); |
| 45 | if (pool2_appsess == NULL) |
| 46 | return -1; |
| 47 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | if (!appsession_task_init()) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 49 | int ser_msize, ses_msize; |
| 50 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | apools.sessid = NULL; |
| 52 | apools.serverid = NULL; |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 53 | |
| 54 | ser_msize = sizeof(void *); |
| 55 | ses_msize = sizeof(void *); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | while (p) { |
| 57 | s = p->srv; |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 58 | if (ses_msize < p->appsession_len) |
| 59 | ses_msize = p->appsession_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | while (s) { |
| 61 | idlen = strlen(s->id); |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 62 | if (ser_msize < idlen) |
| 63 | ser_msize = idlen; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | s = s->next; |
| 65 | } |
| 66 | p = p->next; |
| 67 | } |
| 68 | /* we use strings, so reserve space for '\0' */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 69 | ser_msize ++; |
| 70 | ses_msize ++; |
| 71 | |
| 72 | apools.sessid = create_pool("sessid", ses_msize, MEM_F_SHARED); |
| 73 | if (!apools.sessid) |
| 74 | return -1; |
| 75 | apools.serverid = create_pool("serverid", ser_msize, MEM_F_SHARED); |
| 76 | if (!apools.serverid) |
| 77 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | } |
| 79 | else { |
| 80 | fprintf(stderr, "appsession_task_init failed\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | initialized ++; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int appsession_task_init(void) |
| 89 | { |
| 90 | static int initialized = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | if (!initialized) { |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 92 | if ((appsess_refresh = pool_alloc2(pool2_task)) == NULL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | return -1; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 95 | task_init(appsess_refresh); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 96 | appsess_refresh->context = NULL; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 97 | appsess_refresh->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT)); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 98 | appsess_refresh->process = appsession_refresh; |
| 99 | task_queue(appsess_refresh); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | initialized ++; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 105 | void appsession_refresh(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | { |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 107 | struct proxy *p = proxy; |
| 108 | struct appsession_hash *htbl; |
| 109 | appsess *element, *back; |
| 110 | int i; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | |
| 112 | while (p) { |
| 113 | if (p->appsession_name != NULL) { |
| 114 | htbl = &p->htbl_proxy; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 115 | as_hash_for_each_entry_safe(i, element, back, &p->htbl_proxy, hash_list) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 116 | if (tick_is_expired(element->expire, now_ms)) { |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 117 | if ((global.mode & MODE_DEBUG) && |
| 118 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 119 | int len; |
| 120 | /* |
| 121 | on Linux NULL pointers are caught by sprintf, on solaris -> segfault |
| 122 | */ |
| 123 | len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", |
| 124 | element->sessid, element->serverid?element->serverid:"(null)"); |
| 125 | write(1, trash, len); |
| 126 | } |
| 127 | /* delete the expired element from within the hash table */ |
| 128 | LIST_DEL(&element->hash_list); |
| 129 | htbl->destroy(element); |
| 130 | }/* end if (tv_isle(&asession->expire, &now)) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | p = p->next; |
| 134 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 135 | t->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT)); /* check expiration every 5 seconds */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 136 | task_queue(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 137 | *next = t->expire; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | } /* end appsession_refresh */ |
| 139 | |
| 140 | int match_str(const void *key1, const void *key2) |
| 141 | { |
| 142 | appsess *temp1,*temp2; |
| 143 | temp1 = (appsess *)key1; |
| 144 | temp2 = (appsess *)key2; |
| 145 | |
| 146 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 147 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 148 | |
| 149 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 150 | }/* end match_str */ |
| 151 | |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 152 | void destroy(appsess *temp1) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 153 | if (temp1->sessid) |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 154 | pool_free2(apools.sessid, temp1->sessid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | |
| 156 | if (temp1->serverid) |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 157 | pool_free2(apools.serverid, temp1->serverid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 158 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 159 | pool_free2(pool2_appsess, temp1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | } /* end destroy */ |
| 161 | |
| 162 | void appsession_cleanup( void ) |
| 163 | { |
| 164 | struct proxy *p = proxy; |
| 165 | |
| 166 | while(p) { |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 167 | appsession_hash_destroy(&(p->htbl_proxy)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | p = p->next; |
| 169 | } |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 170 | |
| 171 | if (appsess_refresh) { |
| 172 | task_delete(appsess_refresh); |
| 173 | task_free(appsess_refresh); |
| 174 | appsess_refresh = NULL; |
| 175 | } |
| 176 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 177 | }/* end appsession_cleanup() */ |
| 178 | |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * Local variables: |
| 183 | * c-indent-level: 8 |
| 184 | * c-basic-offset: 8 |
| 185 | * End: |
| 186 | */ |