1 /* operation.c - routines to deal with pending ldap operations */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
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.
31 #include <ac/string.h>
32 #include <ac/socket.h>
37 #include "slapi/slapi.h"
40 static ldap_pvt_thread_mutex_t slap_op_mutex;
41 static time_t last_time;
44 void slap_op_init(void)
46 ldap_pvt_thread_mutex_init( &slap_op_mutex );
49 void slap_op_destroy(void)
51 ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
55 slap_op_q_destroy( void *key, void *data )
58 for ( op = data; op; op = op2 ) {
59 op2 = LDAP_STAILQ_NEXT( op, o_next );
60 ber_memfree_x( op, NULL );
65 slap_op_groups_free( Operation *op )
67 GroupAssertion *g, *n;
68 for ( g = op->o_groups; g; g = n ) {
70 slap_sl_free( g, op->o_tmpmemctx );
76 slap_op_free( Operation *op, void *ctx )
78 OperationBuffer *opbuf;
80 assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
82 if ( op->o_ber != NULL ) {
83 ber_free( op->o_ber, 1 );
85 if ( !BER_BVISNULL( &op->o_dn ) ) {
86 ch_free( op->o_dn.bv_val );
88 if ( !BER_BVISNULL( &op->o_ndn ) ) {
89 ch_free( op->o_ndn.bv_val );
91 if ( !BER_BVISNULL( &op->o_authmech ) ) {
92 ch_free( op->o_authmech.bv_val );
94 if ( op->o_ctrls != NULL ) {
95 slap_free_ctrls( op, op->o_ctrls );
98 #ifdef LDAP_CONNECTIONLESS
99 if ( op->o_res_ber != NULL ) {
100 ber_free( op->o_res_ber, 1 );
104 if ( op->o_groups ) {
105 slap_op_groups_free( op );
108 #if defined( LDAP_SLAPI )
109 if ( slapi_plugins_used ) {
110 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
112 #endif /* defined( LDAP_SLAPI ) */
114 if ( !BER_BVISNULL( &op->o_csn ) ) {
115 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
118 if ( op->o_pagedresults_state != NULL ) {
119 op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
122 /* Selectively zero out the struct. Ignore fields that will
123 * get explicitly initialized later anyway. Keep o_abandon intact.
125 opbuf = (OperationBuffer *) op;
127 BER_BVZERO( &op->o_req_dn );
128 BER_BVZERO( &op->o_req_ndn );
129 memset( &op->o_request, 0, sizeof( op->o_request ));
130 memset( &op->o_do_not_cache, 0, sizeof( Operation ) - offsetof( Operation, o_do_not_cache ));
131 op->o_controls = opbuf->ob_controls;
134 Operation *op2 = NULL;
135 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
136 op, slap_op_q_destroy, (void **)&op2, NULL );
137 LDAP_STAILQ_NEXT( op, o_next ) = op2;
139 op->o_tincr = op2->o_tincr + 1;
140 /* No more than 10 ops on per-thread free list */
141 if ( op->o_tincr > 10 ) {
142 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
143 op2, slap_op_q_destroy, NULL, NULL );
144 ber_memfree_x( op, NULL );
150 ber_memfree_x( op, NULL );
155 slap_op_time(time_t *t, int *nop)
157 ldap_pvt_thread_mutex_lock( &slap_op_mutex );
158 *t = slap_get_time();
159 if ( *t == last_time ) {
166 ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
177 Operation *op = NULL;
181 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_op_free, &otmp, NULL );
184 otmp = LDAP_STAILQ_NEXT( op, o_next );
185 ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
186 otmp, slap_op_q_destroy, NULL, NULL );
192 op = (Operation *) ch_calloc( 1, sizeof(OperationBuffer) );
193 op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
194 op->o_controls = ((OperationBuffer *) op)->ob_controls;
201 slap_op_time( &op->o_time, &op->o_tincr );
204 #if defined( LDAP_SLAPI )
205 if ( slapi_plugins_used ) {
206 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
208 #endif /* defined( LDAP_SLAPI ) */
214 slap_req2op( ber_tag_t tag )
219 case LDAP_REQ_UNBIND:
220 return SLAP_OP_UNBIND;
223 case LDAP_REQ_DELETE:
224 return SLAP_OP_DELETE;
225 case LDAP_REQ_MODRDN:
226 return SLAP_OP_MODRDN;
227 case LDAP_REQ_MODIFY:
228 return SLAP_OP_MODIFY;
229 case LDAP_REQ_COMPARE:
230 return SLAP_OP_COMPARE;
231 case LDAP_REQ_SEARCH:
232 return SLAP_OP_SEARCH;
233 case LDAP_REQ_ABANDON:
234 return SLAP_OP_ABANDON;
235 case LDAP_REQ_EXTENDED:
236 return SLAP_OP_EXTENDED;