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 | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame^] | 5 | * Copyright 2006-2009 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 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 26 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <proto/task.h> |
| 28 | |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 29 | static struct task *appsess_refresh = NULL; |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 30 | struct pool_head *pool2_appsess; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | struct app_pool apools; |
| 32 | int have_appsession; |
| 33 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | int 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 Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 42 | pool2_appsess = create_pool("appsess", sizeof(appsess), MEM_F_SHARED); |
| 43 | if (pool2_appsess == NULL) |
| 44 | return -1; |
| 45 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | if (!appsession_task_init()) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 47 | int ser_msize, ses_msize; |
| 48 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | apools.sessid = NULL; |
| 50 | apools.serverid = NULL; |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 51 | |
| 52 | ser_msize = sizeof(void *); |
| 53 | ses_msize = sizeof(void *); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | while (p) { |
| 55 | s = p->srv; |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 56 | if (ses_msize < p->appsession_len) |
| 57 | ses_msize = p->appsession_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | while (s) { |
| 59 | idlen = strlen(s->id); |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 60 | if (ser_msize < idlen) |
| 61 | ser_msize = idlen; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | s = s->next; |
| 63 | } |
| 64 | p = p->next; |
| 65 | } |
| 66 | /* we use strings, so reserve space for '\0' */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 67 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | } |
| 77 | else { |
| 78 | fprintf(stderr, "appsession_task_init failed\n"); |
| 79 | return -1; |
| 80 | } |
| 81 | initialized ++; |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int appsession_task_init(void) |
| 87 | { |
| 88 | static int initialized = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | if (!initialized) { |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 90 | if ((appsess_refresh = pool_alloc2(pool2_task)) == NULL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | return -1; |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | 9789f7b | 2008-06-24 08:17:16 +0200 | [diff] [blame] | 93 | task_init(appsess_refresh); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 94 | appsess_refresh->context = NULL; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 95 | appsess_refresh->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT)); |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 96 | appsess_refresh->process = appsession_refresh; |
| 97 | task_queue(appsess_refresh); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | initialized ++; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame^] | 103 | struct task *appsession_refresh(struct task *t) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | { |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 105 | struct proxy *p = proxy; |
| 106 | struct appsession_hash *htbl; |
| 107 | appsess *element, *back; |
| 108 | int i; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | |
| 110 | while (p) { |
| 111 | if (p->appsession_name != NULL) { |
| 112 | htbl = &p->htbl_proxy; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 113 | 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] | 114 | if (tick_is_expired(element->expire, now_ms)) { |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 115 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | p = p->next; |
| 132 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 133 | t->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT)); /* check expiration every 5 seconds */ |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame^] | 134 | return t; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 135 | } /* end appsession_refresh */ |
| 136 | |
| 137 | int match_str(const void *key1, const void *key2) |
| 138 | { |
| 139 | appsess *temp1,*temp2; |
| 140 | temp1 = (appsess *)key1; |
| 141 | temp2 = (appsess *)key2; |
| 142 | |
| 143 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 144 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 145 | |
| 146 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 147 | }/* end match_str */ |
| 148 | |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 149 | void destroy(appsess *temp1) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 150 | pool_free2(apools.sessid, temp1->sessid); |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 151 | pool_free2(apools.serverid, temp1->serverid); |
Willy Tarreau | 48d63db | 2008-08-03 17:41:33 +0200 | [diff] [blame] | 152 | pool_free2(pool2_appsess, temp1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 153 | } /* end destroy */ |
| 154 | |
| 155 | void appsession_cleanup( void ) |
| 156 | { |
| 157 | struct proxy *p = proxy; |
| 158 | |
| 159 | while(p) { |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 160 | appsession_hash_destroy(&(p->htbl_proxy)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | p = p->next; |
| 162 | } |
Krzysztof Piotr Oledzki | a643baf | 2008-05-29 23:53:44 +0200 | [diff] [blame] | 163 | |
| 164 | if (appsess_refresh) { |
| 165 | task_delete(appsess_refresh); |
| 166 | task_free(appsess_refresh); |
| 167 | appsess_refresh = NULL; |
| 168 | } |
| 169 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | }/* end appsession_cleanup() */ |
| 171 | |
| 172 | |
| 173 | |
| 174 | /* |
| 175 | * Local variables: |
| 176 | * c-indent-level: 8 |
| 177 | * c-basic-offset: 8 |
| 178 | * End: |
| 179 | */ |