1 /* $OpenLDAP: pkg/ldap/libraries/librewrite/session.c,v 1.19.2.3 2008/02/11 23:26:42 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2000-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Pierangelo Masarati for
17 * inclusion in OpenLDAP Software.
22 #include "rewrite-int.h"
25 * Compares two cookies
33 const struct rewrite_session
*s1
, *s2
;
35 s1
= ( const struct rewrite_session
* )c1
;
36 s2
= ( const struct rewrite_session
* )c2
;
40 assert( s1
->ls_cookie
!= NULL
);
41 assert( s2
->ls_cookie
!= NULL
);
43 return ( ( s1
->ls_cookie
< s2
->ls_cookie
) ? -1 :
44 ( ( s1
->ls_cookie
> s2
->ls_cookie
) ? 1 : 0 ) );
56 struct rewrite_session
*s1
, *s2
;
58 s1
= ( struct rewrite_session
* )c1
;
59 s2
= ( struct rewrite_session
* )c2
;
63 assert( s1
->ls_cookie
!= NULL
);
64 assert( s2
->ls_cookie
!= NULL
);
66 assert( s1
->ls_cookie
!= s2
->ls_cookie
);
68 return ( ( s1
->ls_cookie
== s2
->ls_cookie
) ? -1 : 0 );
74 struct rewrite_session
*
76 struct rewrite_info
*info
,
80 struct rewrite_session
*session
, tmp
;
83 assert( info
!= NULL
);
84 assert( cookie
!= NULL
);
86 #ifdef USE_REWRITE_LDAP_PVT_THREADS
87 ldap_pvt_thread_rdwr_wlock( &info
->li_cookies_mutex
);
88 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
90 tmp
.ls_cookie
= ( void * )cookie
;
91 session
= ( struct rewrite_session
* )avl_find( info
->li_cookies
,
92 ( caddr_t
)&tmp
, rewrite_cookie_cmp
);
95 #ifdef USE_REWRITE_LDAP_PVT_THREADS
96 ldap_pvt_thread_rdwr_wunlock( &info
->li_cookies_mutex
);
97 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
101 session
= calloc( sizeof( struct rewrite_session
), 1 );
102 if ( session
== NULL
) {
105 session
->ls_cookie
= ( void * )cookie
;
106 session
->ls_count
= 1;
108 #ifdef USE_REWRITE_LDAP_PVT_THREADS
109 if ( ldap_pvt_thread_mutex_init( &session
->ls_mutex
) ) {
113 if ( ldap_pvt_thread_rdwr_init( &session
->ls_vars_mutex
) ) {
114 ldap_pvt_thread_mutex_destroy( &session
->ls_mutex
);
118 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
120 rc
= avl_insert( &info
->li_cookies
, ( caddr_t
)session
,
121 rewrite_cookie_cmp
, rewrite_cookie_dup
);
122 info
->li_num_cookies
++;
124 #ifdef USE_REWRITE_LDAP_PVT_THREADS
125 ldap_pvt_thread_rdwr_wunlock( &info
->li_cookies_mutex
);
126 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
129 #ifdef USE_REWRITE_LDAP_PVT_THREADS
130 ldap_pvt_thread_rdwr_destroy( &session
->ls_vars_mutex
);
131 ldap_pvt_thread_mutex_destroy( &session
->ls_mutex
);
132 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
144 struct rewrite_session
*
145 rewrite_session_find(
146 struct rewrite_info
*info
,
150 struct rewrite_session
*session
, tmp
;
152 assert( info
!= NULL
);
153 assert( cookie
!= NULL
);
155 tmp
.ls_cookie
= ( void * )cookie
;
156 #ifdef USE_REWRITE_LDAP_PVT_THREADS
157 ldap_pvt_thread_rdwr_rlock( &info
->li_cookies_mutex
);
158 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
159 session
= ( struct rewrite_session
* )avl_find( info
->li_cookies
,
160 ( caddr_t
)&tmp
, rewrite_cookie_cmp
);
161 #ifdef USE_REWRITE_LDAP_PVT_THREADS
163 ldap_pvt_thread_mutex_lock( &session
->ls_mutex
);
165 ldap_pvt_thread_rdwr_runlock( &info
->li_cookies_mutex
);
166 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
175 rewrite_session_return(
176 struct rewrite_info
*info
,
177 struct rewrite_session
*session
180 assert( session
!= NULL
);
181 ldap_pvt_thread_mutex_unlock( &session
->ls_mutex
);
185 * Defines and inits a var with session scope
188 rewrite_session_var_set_f(
189 struct rewrite_info
*info
,
196 struct rewrite_session
*session
;
197 struct rewrite_var
*var
;
199 assert( info
!= NULL
);
200 assert( cookie
!= NULL
);
201 assert( name
!= NULL
);
202 assert( value
!= NULL
);
204 session
= rewrite_session_find( info
, cookie
);
205 if ( session
== NULL
) {
206 session
= rewrite_session_init( info
, cookie
);
207 if ( session
== NULL
) {
211 #ifdef USE_REWRITE_LDAP_PVT_THREADS
212 ldap_pvt_thread_mutex_lock( &session
->ls_mutex
);
213 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
216 #ifdef USE_REWRITE_LDAP_PVT_THREADS
217 ldap_pvt_thread_rdwr_wlock( &session
->ls_vars_mutex
);
218 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
220 var
= rewrite_var_find( session
->ls_vars
, name
);
222 assert( var
->lv_value
.bv_val
!= NULL
);
224 (void)rewrite_var_replace( var
, value
, flags
);
227 var
= rewrite_var_insert_f( &session
->ls_vars
, name
, value
, flags
);
229 #ifdef USE_REWRITE_LDAP_PVT_THREADS
230 ldap_pvt_thread_rdwr_wunlock( &session
->ls_vars_mutex
);
231 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
232 rewrite_session_return( info
, session
);
237 #ifdef USE_REWRITE_LDAP_PVT_THREADS
238 ldap_pvt_thread_rdwr_wunlock( &session
->ls_vars_mutex
);
239 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
241 rewrite_session_return( info
, session
);
243 return REWRITE_SUCCESS
;
247 * Gets a var with session scope
250 rewrite_session_var_get(
251 struct rewrite_info
*info
,
257 struct rewrite_session
*session
;
258 struct rewrite_var
*var
;
260 assert( info
!= NULL
);
261 assert( cookie
!= NULL
);
262 assert( name
!= NULL
);
263 assert( value
!= NULL
);
265 value
->bv_val
= NULL
;
268 if ( cookie
== NULL
) {
272 session
= rewrite_session_find( info
, cookie
);
273 if ( session
== NULL
) {
277 #ifdef USE_REWRITE_LDAP_PVT_THREADS
278 ldap_pvt_thread_rdwr_rlock( &session
->ls_vars_mutex
);
279 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
281 var
= rewrite_var_find( session
->ls_vars
, name
);
284 #ifdef USE_REWRITE_LDAP_PVT_THREADS
285 ldap_pvt_thread_rdwr_runlock( &session
->ls_vars_mutex
);
286 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
288 rewrite_session_return( info
, session
);
292 value
->bv_val
= strdup( var
->lv_value
.bv_val
);
293 value
->bv_len
= var
->lv_value
.bv_len
;
296 #ifdef USE_REWRITE_LDAP_PVT_THREADS
297 ldap_pvt_thread_rdwr_runlock( &session
->ls_vars_mutex
);
298 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
300 rewrite_session_return( info
, session
);
302 return REWRITE_SUCCESS
;
306 rewrite_session_clean( void *v_session
)
308 struct rewrite_session
*session
= (struct rewrite_session
*)v_session
;
310 #ifdef USE_REWRITE_LDAP_PVT_THREADS
311 ldap_pvt_thread_rdwr_wlock( &session
->ls_vars_mutex
);
312 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
314 rewrite_var_delete( session
->ls_vars
);
316 #ifdef USE_REWRITE_LDAP_PVT_THREADS
317 ldap_pvt_thread_rdwr_wunlock( &session
->ls_vars_mutex
);
318 ldap_pvt_thread_rdwr_destroy( &session
->ls_vars_mutex
);
319 ldap_pvt_thread_mutex_unlock( &session
->ls_mutex
);
320 ldap_pvt_thread_mutex_destroy( &session
->ls_mutex
);
321 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
325 rewrite_session_free( void *v_session
)
327 struct rewrite_session
*session
= (struct rewrite_session
*)v_session
;
329 ldap_pvt_thread_mutex_lock( &session
->ls_mutex
);
330 rewrite_session_clean( v_session
);
338 rewrite_session_delete(
339 struct rewrite_info
*info
,
343 struct rewrite_session
*session
, tmp
= { 0 };
345 assert( info
!= NULL
);
346 assert( cookie
!= NULL
);
348 session
= rewrite_session_find( info
, cookie
);
350 if ( session
== NULL
) {
351 return REWRITE_SUCCESS
;
354 if ( --session
->ls_count
> 0 ) {
355 rewrite_session_return( info
, session
);
356 return REWRITE_SUCCESS
;
359 rewrite_session_clean( session
);
361 #ifdef USE_REWRITE_LDAP_PVT_THREADS
362 ldap_pvt_thread_rdwr_wlock( &info
->li_cookies_mutex
);
363 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
365 assert( info
->li_num_cookies
> 0 );
366 info
->li_num_cookies
--;
369 * There is nothing to delete in the return value
371 tmp
.ls_cookie
= ( void * )cookie
;
372 avl_delete( &info
->li_cookies
, ( caddr_t
)&tmp
, rewrite_cookie_cmp
);
376 #ifdef USE_REWRITE_LDAP_PVT_THREADS
377 ldap_pvt_thread_rdwr_wunlock( &info
->li_cookies_mutex
);
378 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
380 return REWRITE_SUCCESS
;
384 * Destroys the cookie tree
387 rewrite_session_destroy(
388 struct rewrite_info
*info
393 assert( info
!= NULL
);
395 #ifdef USE_REWRITE_LDAP_PVT_THREADS
396 ldap_pvt_thread_rdwr_wlock( &info
->li_cookies_mutex
);
397 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
400 * Should call per-session destruction routine ...
403 count
= avl_free( info
->li_cookies
, rewrite_session_free
);
404 info
->li_cookies
= NULL
;
407 fprintf( stderr
, "count = %d; num_cookies = %d\n",
408 count
, info
->li_num_cookies
);
411 assert( count
== info
->li_num_cookies
);
412 info
->li_num_cookies
= 0;
414 #ifdef USE_REWRITE_LDAP_PVT_THREADS
415 ldap_pvt_thread_rdwr_wunlock( &info
->li_cookies_mutex
);
416 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
418 return REWRITE_SUCCESS
;