Back to index

openldap  2.4.31
operation.c
Go to the documentation of this file.
00001 /* operation.c - routines to deal with pending ldap operations */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 
00027 #include "portable.h"
00028 
00029 #include <stdio.h>
00030 
00031 #include <ac/string.h>
00032 #include <ac/socket.h>
00033 
00034 #include "slap.h"
00035 
00036 #ifdef LDAP_SLAPI
00037 #include "slapi/slapi.h"
00038 #endif
00039 
00040 static ldap_pvt_thread_mutex_t     slap_op_mutex;
00041 static time_t last_time;
00042 static int last_incr;
00043 
00044 void slap_op_init(void)
00045 {
00046        ldap_pvt_thread_mutex_init( &slap_op_mutex );
00047 }
00048 
00049 void slap_op_destroy(void)
00050 {
00051        ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
00052 }
00053 
00054 static void
00055 slap_op_q_destroy( void *key, void *data )
00056 {
00057        Operation *op, *op2;
00058        for ( op = data; op; op = op2 ) {
00059               op2 = LDAP_STAILQ_NEXT( op, o_next );
00060               ber_memfree_x( op, NULL );
00061        }
00062 }
00063 
00064 void
00065 slap_op_groups_free( Operation *op )
00066 {
00067        GroupAssertion *g, *n;
00068        for ( g = op->o_groups; g; g = n ) {
00069               n = g->ga_next;
00070               slap_sl_free( g, op->o_tmpmemctx );
00071        }
00072        op->o_groups = NULL;
00073 }
00074 
00075 void
00076 slap_op_free( Operation *op, void *ctx )
00077 {
00078        OperationBuffer *opbuf;
00079 
00080        assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
00081 
00082        /* paranoia */
00083        op->o_abandon = 1;
00084 
00085        if ( op->o_ber != NULL ) {
00086               ber_free( op->o_ber, 1 );
00087        }
00088        if ( !BER_BVISNULL( &op->o_dn ) ) {
00089               ch_free( op->o_dn.bv_val );
00090        }
00091        if ( !BER_BVISNULL( &op->o_ndn ) ) {
00092               ch_free( op->o_ndn.bv_val );
00093        }
00094        if ( !BER_BVISNULL( &op->o_authmech ) ) {
00095               ch_free( op->o_authmech.bv_val );
00096        }
00097        if ( op->o_ctrls != NULL ) {
00098               slap_free_ctrls( op, op->o_ctrls );
00099        }
00100 
00101 #ifdef LDAP_CONNECTIONLESS
00102        if ( op->o_res_ber != NULL ) {
00103               ber_free( op->o_res_ber, 1 );
00104        }
00105 #endif
00106 
00107        if ( op->o_groups ) {
00108               slap_op_groups_free( op );
00109        }
00110 
00111 #if defined( LDAP_SLAPI )
00112        if ( slapi_plugins_used ) {
00113               slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
00114        }
00115 #endif /* defined( LDAP_SLAPI ) */
00116 
00117        if ( !BER_BVISNULL( &op->o_csn ) ) {
00118               op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
00119        }
00120 
00121        if ( op->o_pagedresults_state != NULL ) {
00122               op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
00123        }
00124 
00125        /* Selectively zero out the struct. Ignore fields that will
00126         * get explicitly initialized later anyway. Keep o_abandon intact.
00127         */
00128        opbuf = (OperationBuffer *) op;
00129        op->o_bd = NULL;
00130        BER_BVZERO( &op->o_req_dn );
00131        BER_BVZERO( &op->o_req_ndn );
00132        memset( op->o_hdr, 0, sizeof( *op->o_hdr ));
00133        memset( &op->o_request, 0, sizeof( op->o_request ));
00134        memset( &op->o_do_not_cache, 0, sizeof( Operation ) - offsetof( Operation, o_do_not_cache ));
00135        memset( opbuf->ob_controls, 0, sizeof( opbuf->ob_controls ));
00136        op->o_controls = opbuf->ob_controls;
00137 
00138        if ( ctx ) {
00139               Operation *op2 = NULL;
00140               ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
00141                      op, slap_op_q_destroy, (void **)&op2, NULL );
00142               LDAP_STAILQ_NEXT( op, o_next ) = op2;
00143               if ( op2 ) {
00144                      op->o_tincr = op2->o_tincr + 1;
00145                      /* No more than 10 ops on per-thread free list */
00146                      if ( op->o_tincr > 10 ) {
00147                             ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
00148                                    op2, slap_op_q_destroy, NULL, NULL );
00149                             ber_memfree_x( op, NULL );
00150                      }
00151               } else {
00152                      op->o_tincr = 1;
00153               }
00154        } else {
00155               ber_memfree_x( op, NULL );
00156        }
00157 }
00158 
00159 void
00160 slap_op_time(time_t *t, int *nop)
00161 {
00162        ldap_pvt_thread_mutex_lock( &slap_op_mutex );
00163        *t = slap_get_time();
00164        if ( *t == last_time ) {
00165               *nop = ++last_incr;
00166        } else {
00167               last_time = *t;
00168               last_incr = 0;
00169               *nop = 0;
00170        }
00171        ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
00172 }
00173 
00174 Operation *
00175 slap_op_alloc(
00176     BerElement              *ber,
00177     ber_int_t msgid,
00178     ber_tag_t tag,
00179     ber_int_t id,
00180        void *ctx )
00181 {
00182        Operation     *op = NULL;
00183 
00184        if ( ctx ) {
00185               void *otmp = NULL;
00186               ldap_pvt_thread_pool_getkey( ctx, (void *)slap_op_free, &otmp, NULL );
00187               if ( otmp ) {
00188                      op = otmp;
00189                      otmp = LDAP_STAILQ_NEXT( op, o_next );
00190                      ldap_pvt_thread_pool_setkey( ctx, (void *)slap_op_free,
00191                             otmp, slap_op_q_destroy, NULL, NULL );
00192                      op->o_abandon = 0;
00193                      op->o_cancel = 0;
00194               }
00195        }
00196        if (!op) {
00197               op = (Operation *) ch_calloc( 1, sizeof(OperationBuffer) );
00198               op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
00199               op->o_controls = ((OperationBuffer *) op)->ob_controls;
00200        }
00201 
00202        op->o_ber = ber;
00203        op->o_msgid = msgid;
00204        op->o_tag = tag;
00205 
00206        slap_op_time( &op->o_time, &op->o_tincr );
00207        op->o_opid = id;
00208 
00209 #if defined( LDAP_SLAPI )
00210        if ( slapi_plugins_used ) {
00211               slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
00212        }
00213 #endif /* defined( LDAP_SLAPI ) */
00214 
00215        return( op );
00216 }
00217 
00218 slap_op_t
00219 slap_req2op( ber_tag_t tag )
00220 {
00221        switch ( tag ) {
00222        case LDAP_REQ_BIND:
00223               return SLAP_OP_BIND;
00224        case LDAP_REQ_UNBIND:
00225               return SLAP_OP_UNBIND;
00226        case LDAP_REQ_ADD:
00227               return SLAP_OP_ADD;
00228        case LDAP_REQ_DELETE:
00229               return SLAP_OP_DELETE;
00230        case LDAP_REQ_MODRDN:
00231               return SLAP_OP_MODRDN;
00232        case LDAP_REQ_MODIFY:
00233               return SLAP_OP_MODIFY;
00234        case LDAP_REQ_COMPARE:
00235               return SLAP_OP_COMPARE;
00236        case LDAP_REQ_SEARCH:
00237               return SLAP_OP_SEARCH;
00238        case LDAP_REQ_ABANDON:
00239               return SLAP_OP_ABANDON;
00240        case LDAP_REQ_EXTENDED:
00241               return SLAP_OP_EXTENDED;
00242        }
00243 
00244        return SLAP_OP_LAST;
00245 }