]> git.sur5r.net Git - openldap/blob - servers/slapd/operation.c
7e855049cc2d0708c6a6e2b0df4e78b0f40962d5
[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-2017 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 struct timeval last_time;
42
43 void slap_op_init(void)
44 {
45         ldap_pvt_thread_mutex_init( &slap_op_mutex );
46 }
47
48 void slap_op_destroy(void)
49 {
50         ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
51 }
52
53 static void
54 slap_op_q_destroy( void *key, void *data )
55 {
56         Operation *op, *op2;
57         for ( op = data; op; op = op2 ) {
58                 op2 = LDAP_STAILQ_NEXT( op, o_next );
59                 ber_memfree_x( op, NULL );
60         }
61 }
62
63 void
64 slap_op_groups_free( Operation *op )
65 {
66         GroupAssertion *g, *n;
67         for ( g = op->o_groups; g; g = n ) {
68                 n = g->ga_next;
69                 slap_sl_free( g, op->o_tmpmemctx );
70         }
71         op->o_groups = NULL;
72 }
73
74 void
75 slap_op_free( Operation *op, void *ctx )
76 {
77         OperationBuffer *opbuf;
78
79         assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
80
81         /* paranoia */
82         op->o_abandon = 1;
83
84         if ( op->o_ber != NULL ) {
85                 ber_free( op->o_ber, 1 );
86         }
87         if ( !BER_BVISNULL( &op->o_dn ) ) {
88                 ch_free( op->o_dn.bv_val );
89         }
90         if ( !BER_BVISNULL( &op->o_ndn ) ) {
91                 ch_free( op->o_ndn.bv_val );
92         }
93         if ( !BER_BVISNULL( &op->o_authmech ) ) {
94                 ch_free( op->o_authmech.bv_val );
95         }
96         if ( op->o_ctrls != NULL ) {
97                 slap_free_ctrls( op, op->o_ctrls );
98         }
99
100 #ifdef LDAP_CONNECTIONLESS
101         if ( op->o_res_ber != NULL ) {
102                 ber_free( op->o_res_ber, 1 );
103         }
104 #endif
105
106         if ( op->o_groups ) {
107                 slap_op_groups_free( op );
108         }
109
110 #if defined( LDAP_SLAPI )
111         if ( slapi_plugins_used ) {
112                 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
113         }
114 #endif /* defined( LDAP_SLAPI ) */
115
116         if ( !BER_BVISNULL( &op->o_csn ) ) {
117                 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
118         }
119
120         if ( op->o_pagedresults_state != NULL ) {
121                 op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
122         }
123
124         /* Selectively zero out the struct. Ignore fields that will
125          * get explicitly initialized later anyway. Keep o_abandon intact.
126          */
127         opbuf = (OperationBuffer *) op;
128         op->o_bd = NULL;
129         BER_BVZERO( &op->o_req_dn );
130         BER_BVZERO( &op->o_req_ndn );
131         memset( op->o_hdr, 0, sizeof( *op->o_hdr ));
132         memset( &op->o_request, 0, sizeof( op->o_request ));
133         memset( &op->o_do_not_cache, 0, sizeof( Operation ) - offsetof( Operation, o_do_not_cache ));
134         memset( opbuf->ob_controls, 0, sizeof( opbuf->ob_controls ));
135         op->o_controls = opbuf->ob_controls;
136
137         if ( ctx ) {
138                 Operation *op2 = NULL;
139                 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
140                         op, slap_op_q_destroy, (void **)&op2, NULL );
141                 LDAP_STAILQ_NEXT( op, o_next ) = op2;
142                 if ( op2 ) {
143                         op->o_tincr = op2->o_tincr + 1;
144                         /* No more than 10 ops on per-thread free list */
145                         if ( op->o_tincr > 10 ) {
146                                 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
147                                         op2, slap_op_q_destroy, NULL, NULL );
148                                 ber_memfree_x( op, NULL );
149                         }
150                 } else {
151                         op->o_tincr = 1;
152                 }
153         } else {
154                 ber_memfree_x( op, NULL );
155         }
156 }
157
158 void
159 slap_op_time(time_t *t, int *nop)
160 {
161         struct timeval tv;
162 #if SLAP_STATS_ETIME
163         gettimeofday( &tv, NULL );
164 #else
165         tv.tv_sec = slap_get_time();
166         tv.tv_usec = 0;
167 #endif
168         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
169         /* Usually tv.tv_sec cannot be < last_time.tv_sec
170          * but it might happen if we wrapped around tv_usec.
171          */
172         if ( tv.tv_sec <= last_time.tv_sec &&
173                 tv.tv_usec <= last_time.tv_usec ) {
174                 tv.tv_sec = last_time.tv_sec;
175                 tv.tv_usec = last_time.tv_usec + 1;
176         }
177         if (tv.tv_usec >= 1000000) {
178                 tv.tv_usec -= 1000000;
179                 tv.tv_sec++;
180                 last_time.tv_sec = tv.tv_sec;
181         }
182         last_time.tv_usec = tv.tv_usec;
183         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
184         *t = tv.tv_sec;
185         *nop = tv.tv_usec;
186 }
187
188 Operation *
189 slap_op_alloc(
190     BerElement          *ber,
191     ber_int_t   msgid,
192     ber_tag_t   tag,
193     ber_int_t   id,
194         void *ctx )
195 {
196         Operation       *op = NULL;
197
198         if ( ctx ) {
199                 void *otmp = NULL;
200                 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_op_free, &otmp, NULL );
201                 if ( otmp ) {
202                         op = otmp;
203                         otmp = LDAP_STAILQ_NEXT( op, o_next );
204                         ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
205                                 otmp, slap_op_q_destroy, NULL, NULL );
206                         op->o_abandon = 0;
207                         op->o_cancel = 0;
208                 }
209         }
210         if (!op) {
211                 op = (Operation *) ch_calloc( 1, sizeof(OperationBuffer) );
212                 op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
213                 op->o_controls = ((OperationBuffer *) op)->ob_controls;
214         }
215
216         op->o_ber = ber;
217         op->o_msgid = msgid;
218         op->o_tag = tag;
219
220         slap_op_time( &op->o_time, &op->o_tincr );
221         op->o_opid = id;
222
223 #if defined( LDAP_SLAPI )
224         if ( slapi_plugins_used ) {
225                 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
226         }
227 #endif /* defined( LDAP_SLAPI ) */
228
229         return( op );
230 }
231
232 slap_op_t
233 slap_req2op( ber_tag_t tag )
234 {
235         switch ( tag ) {
236         case LDAP_REQ_BIND:
237                 return SLAP_OP_BIND;
238         case LDAP_REQ_UNBIND:
239                 return SLAP_OP_UNBIND;
240         case LDAP_REQ_ADD:
241                 return SLAP_OP_ADD;
242         case LDAP_REQ_DELETE:
243                 return SLAP_OP_DELETE;
244         case LDAP_REQ_MODRDN:
245                 return SLAP_OP_MODRDN;
246         case LDAP_REQ_MODIFY:
247                 return SLAP_OP_MODIFY;
248         case LDAP_REQ_COMPARE:
249                 return SLAP_OP_COMPARE;
250         case LDAP_REQ_SEARCH:
251                 return SLAP_OP_SEARCH;
252         case LDAP_REQ_ABANDON:
253                 return SLAP_OP_ABANDON;
254         case LDAP_REQ_EXTENDED:
255                 return SLAP_OP_EXTENDED;
256         }
257
258         return SLAP_OP_LAST;
259 }