]> git.sur5r.net Git - openldap/blob - servers/slapd/operation.c
ae7b4a8a7271e78c910873a33431ba71eb32a73b
[openldap] / servers / slapd / operation.c
1 /* operation.c - routines to deal with pending ldap operations */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/socket.h>
33
34 #include "slap.h"
35
36 #ifdef LDAP_SLAPI
37 #include "slapi/slapi.h"
38 #endif
39
40 static ldap_pvt_thread_mutex_t  slap_op_mutex;
41 static time_t last_time;
42 static int last_incr;
43
44 void slap_op_init(void)
45 {
46         ldap_pvt_thread_mutex_init( &slap_op_mutex );
47 }
48
49 void slap_op_destroy(void)
50 {
51         ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
52 }
53
54 static void
55 slap_op_q_destroy( void *key, void *data )
56 {
57         Operation *op, *op2;
58         for ( op = data; op; op = op2 ) {
59                 op2 = LDAP_STAILQ_NEXT( op, o_next );
60                 ber_memfree_x( op, NULL );
61         }
62 }
63
64 void
65 slap_op_groups_free( Operation *op )
66 {
67         GroupAssertion *g, *n;
68         for ( g = op->o_groups; g; g = n ) {
69                 n = g->ga_next;
70                 slap_sl_free( g, op->o_tmpmemctx );
71         }
72         op->o_groups = NULL;
73 }
74
75 void
76 slap_op_free( Operation *op, void *ctx )
77 {
78         OperationBuffer *opbuf;
79
80         assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
81
82         if ( op->o_ber != NULL ) {
83                 ber_free( op->o_ber, 1 );
84         }
85         if ( !BER_BVISNULL( &op->o_dn ) ) {
86                 ch_free( op->o_dn.bv_val );
87         }
88         if ( !BER_BVISNULL( &op->o_ndn ) ) {
89                 ch_free( op->o_ndn.bv_val );
90         }
91         if ( !BER_BVISNULL( &op->o_authmech ) ) {
92                 ch_free( op->o_authmech.bv_val );
93         }
94         if ( op->o_ctrls != NULL ) {
95                 slap_free_ctrls( op, op->o_ctrls );
96         }
97
98 #ifdef LDAP_CONNECTIONLESS
99         if ( op->o_res_ber != NULL ) {
100                 ber_free( op->o_res_ber, 1 );
101         }
102 #endif
103
104         if ( op->o_groups ) {
105                 slap_op_groups_free( op );
106         }
107
108 #if defined( LDAP_SLAPI )
109         if ( slapi_plugins_used ) {
110                 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
111         }
112 #endif /* defined( LDAP_SLAPI ) */
113
114         if ( !BER_BVISNULL( &op->o_csn ) ) {
115                 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
116                 BER_BVZERO( &op->o_csn );
117         }
118
119         if ( op->o_pagedresults_state != NULL ) {
120                 op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
121                 op->o_pagedresults_state = NULL;
122         }
123
124         opbuf = (OperationBuffer *) op;
125         memset( opbuf, 0, sizeof(*opbuf) );
126         op->o_hdr = &opbuf->ob_hdr;
127         op->o_controls = opbuf->ob_controls;
128
129         if ( ctx ) {
130                 Operation *op2 = NULL;
131                 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
132                         op, slap_op_q_destroy, &op2, NULL );
133                 LDAP_STAILQ_NEXT( op, o_next ) = op2;
134                 if ( op2 ) {
135                         op->o_tincr = op2->o_tincr + 1;
136                         /* No more than 10 ops on per-thread free list */
137                         if ( op->o_tincr > 10 ) {
138                                 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
139                                         op2, slap_op_q_destroy, NULL, NULL );
140                                 ber_memfree_x( op, NULL );
141                         }
142                 } else {
143                         op->o_tincr = 1;
144                 }
145         } else {
146                 ber_memfree_x( op, NULL );
147         }
148 }
149
150 void
151 slap_op_time(time_t *t, int *nop)
152 {
153         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
154         *t = slap_get_time();
155         if ( *t == last_time ) {
156                 *nop = ++last_incr;
157         } else {
158                 last_time = *t;
159                 last_incr = 0;
160                 *nop = 0;
161         }
162         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
163 }
164
165 Operation *
166 slap_op_alloc(
167     BerElement          *ber,
168     ber_int_t   msgid,
169     ber_tag_t   tag,
170     ber_int_t   id,
171         void *ctx )
172 {
173         Operation       *op = NULL;
174
175         if ( ctx ) {
176                 void *otmp = NULL;
177                 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_op_free, &otmp, NULL );
178                 if ( otmp ) {
179                         op = otmp;
180                         otmp = LDAP_STAILQ_NEXT( op, o_next );
181                         ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
182                                 otmp, slap_op_q_destroy, NULL, NULL );
183                 }
184         }
185         if (!op) {
186                 op = (Operation *) ch_calloc( 1, sizeof(OperationBuffer) );
187                 op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
188                 op->o_controls = ((OperationBuffer *) op)->ob_controls;
189         }
190
191         op->o_ber = ber;
192         op->o_msgid = msgid;
193         op->o_tag = tag;
194
195         slap_op_time( &op->o_time, &op->o_tincr );
196         op->o_opid = id;
197         op->o_res_ber = NULL;
198
199 #if defined( LDAP_SLAPI )
200         if ( slapi_plugins_used ) {
201                 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
202         }
203 #endif /* defined( LDAP_SLAPI ) */
204
205         return( op );
206 }
207
208 slap_op_t
209 slap_req2op( ber_tag_t tag )
210 {
211         switch ( tag ) {
212         case LDAP_REQ_BIND:
213                 return SLAP_OP_BIND;
214         case LDAP_REQ_UNBIND:
215                 return SLAP_OP_UNBIND;
216         case LDAP_REQ_ADD:
217                 return SLAP_OP_ADD;
218         case LDAP_REQ_DELETE:
219                 return SLAP_OP_DELETE;
220         case LDAP_REQ_MODRDN:
221                 return SLAP_OP_MODRDN;
222         case LDAP_REQ_MODIFY:
223                 return SLAP_OP_MODIFY;
224         case LDAP_REQ_COMPARE:
225                 return SLAP_OP_COMPARE;
226         case LDAP_REQ_SEARCH:
227                 return SLAP_OP_SEARCH;
228         case LDAP_REQ_ABANDON:
229                 return SLAP_OP_ABANDON;
230         case LDAP_REQ_EXTENDED:
231                 return SLAP_OP_EXTENDED;
232         }
233
234         return SLAP_OP_LAST;
235 }