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