~ubuntu-branches/ubuntu/quantal/haproxy/quantal

« back to all changes in this revision

Viewing changes to src/appsession.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Cornet
  • Date: 2007-08-17 09:33:41 UTC
  • Revision ID: james.westby@ubuntu.com-20070817093341-h0t6aeeoyzo25z3r
Tags: upstream-1.3.12.dfsg
ImportĀ upstreamĀ versionĀ 1.3.12.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * AppSession functions.
 
3
 *
 
4
 * Copyright 2004-2006 Alexander Lazic, Klaus Wagner
 
5
 * Copyright 2006-2007 Willy Tarreau
 
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>
 
15
#include <string.h>
 
16
 
 
17
#include <common/appsession.h>
 
18
#include <common/config.h>
 
19
#include <common/memory.h>
 
20
#include <common/sessionhash.h>
 
21
#include <common/time.h>
 
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
 
 
30
 
 
31
struct pool_head *pool2_appsess;
 
32
struct app_pool apools;
 
33
int have_appsession;
 
34
 
 
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) {
 
43
                pool2_appsess = create_pool("appsess", sizeof(appsess), MEM_F_SHARED);
 
44
                if (pool2_appsess == NULL)
 
45
                        return -1;
 
46
 
 
47
                if (!appsession_task_init()) {
 
48
                        int ser_msize, ses_msize;
 
49
 
 
50
                        apools.sessid = NULL;
 
51
                        apools.serverid = NULL;
 
52
 
 
53
                        ser_msize = sizeof(void *);
 
54
                        ses_msize = sizeof(void *);
 
55
                        while (p) {
 
56
                                s = p->srv;
 
57
                                if (ses_msize < p->appsession_len)
 
58
                                        ses_msize = p->appsession_len;
 
59
                                while (s) {
 
60
                                        idlen = strlen(s->id);
 
61
                                        if (ser_msize < idlen)
 
62
                                                ser_msize = idlen;
 
63
                                        s = s->next;
 
64
                                }
 
65
                                p = p->next;
 
66
                        }
 
67
                        /* we use strings, so reserve space for '\0' */
 
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;
 
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;
 
90
        struct task *t;
 
91
        if (!initialized) {
 
92
                if ((t = pool_alloc2(pool2_task)) == NULL)
 
93
                        return -1;
 
94
                t->wq = NULL;
 
95
                t->qlist.p = NULL;
 
96
                t->state = TASK_IDLE;
 
97
                t->context = NULL;
 
98
                tv_ms_add(&t->expire, &now, TBLCHKINT);
 
99
                t->process = appsession_refresh;
 
100
                task_queue(t);
 
101
                initialized ++;
 
102
        }
 
103
        return 0;
 
104
}
 
105
 
 
106
void appsession_refresh(struct task *t, struct timeval *next)
 
107
{
 
108
        struct proxy           *p = proxy;
 
109
        struct appsession_hash *htbl;
 
110
        appsess                *element, *back;
 
111
        int                    i;
 
112
 
 
113
        while (p) {
 
114
                if (p->appsession_name != NULL) {
 
115
                        htbl = &p->htbl_proxy;
 
116
                        as_hash_for_each_entry_safe(i, element, back, &p->htbl_proxy, hash_list) {
 
117
                                if (tv_isle(&element->expire, &now)) {
 
118
                                        if ((global.mode & MODE_DEBUG) &&
 
119
                                            (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
 
120
                                                int len;
 
121
                                                /*
 
122
                                                  on Linux NULL pointers are caught by sprintf, on solaris -> segfault 
 
123
                                                */
 
124
                                                len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", 
 
125
                                                              element->sessid, element->serverid?element->serverid:"(null)");
 
126
                                                write(1, trash, len);
 
127
                                        }
 
128
                                        /* delete the expired element from within the hash table */
 
129
                                        LIST_DEL(&element->hash_list);
 
130
                                        htbl->destroy(element);
 
131
                                }/* end if (tv_isle(&asession->expire, &now)) */
 
132
                        }
 
133
                }
 
134
                p = p->next;
 
135
        }
 
136
        tv_ms_add(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */
 
137
        task_queue(t);
 
138
        *next = t->expire;
 
139
} /* end appsession_refresh */
 
140
 
 
141
int match_str(const void *key1, const void *key2)
 
142
{
 
143
    appsess *temp1,*temp2;
 
144
    temp1 = (appsess *)key1;
 
145
    temp2 = (appsess *)key2;
 
146
 
 
147
    //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid);
 
148
    //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid);
 
149
  
 
150
    return (strcmp(temp1->sessid,temp2->sessid) == 0);
 
151
}/* end match_str */
 
152
 
 
153
void destroy(appsess *temp1) {
 
154
    if (temp1->sessid)
 
155
        pool_free2(apools.sessid, temp1->sessid);
 
156
 
 
157
    if (temp1->serverid)
 
158
        pool_free2(apools.serverid, temp1->serverid);
 
159
 
 
160
    pool_free2(pool2_appsess, temp1);
 
161
} /* end destroy */
 
162
 
 
163
void appsession_cleanup( void )
 
164
{
 
165
        struct proxy *p = proxy;
 
166
 
 
167
        while(p) {
 
168
                appsession_hash_destroy(&(p->htbl_proxy));
 
169
                p = p->next;
 
170
        }
 
171
}/* end appsession_cleanup() */
 
172
 
 
173
 
 
174
 
 
175
/*
 
176
 * Local variables:
 
177
 *  c-indent-level: 8
 
178
 *  c-basic-offset: 8
 
179
 * End:
 
180
 */