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-2007 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 LDAP_STAILQ_HEAD(s_o, Operation) slap_free_ops;
42 static time_t last_time;
45 void slap_op_init(void)
47 ldap_pvt_thread_mutex_init( &slap_op_mutex );
48 LDAP_STAILQ_INIT(&slap_free_ops);
51 void slap_op_destroy(void)
55 while ( (o = LDAP_STAILQ_FIRST( &slap_free_ops )) != NULL) {
56 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
57 LDAP_STAILQ_NEXT(o, o_next) = NULL;
60 ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
64 slap_op_groups_free( Operation *op )
66 GroupAssertion *g, *n;
67 for ( g = op->o_groups; g; g = n ) {
69 slap_sl_free( g, op->o_tmpmemctx );
75 slap_op_free( Operation *op )
77 OperationBuffer *opbuf;
79 assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
81 if ( op->o_ber != NULL ) {
82 ber_free( op->o_ber, 1 );
84 if ( !BER_BVISNULL( &op->o_dn ) ) {
85 ch_free( op->o_dn.bv_val );
87 if ( !BER_BVISNULL( &op->o_ndn ) ) {
88 ch_free( op->o_ndn.bv_val );
90 if ( !BER_BVISNULL( &op->o_authmech ) ) {
91 ch_free( op->o_authmech.bv_val );
93 if ( op->o_ctrls != NULL ) {
94 slap_free_ctrls( op, op->o_ctrls );
97 #ifdef LDAP_CONNECTIONLESS
98 if ( op->o_res_ber != NULL ) {
99 ber_free( op->o_res_ber, 1 );
103 if ( op->o_groups ) {
104 slap_op_groups_free( op );
107 #if defined( LDAP_SLAPI )
108 if ( slapi_plugins_used ) {
109 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
111 #endif /* defined( LDAP_SLAPI ) */
114 opbuf = (OperationBuffer *) op;
115 memset( opbuf, 0, sizeof(*opbuf) );
116 op->o_hdr = &opbuf->ob_hdr;
117 op->o_controls = opbuf->ob_controls;
119 ldap_pvt_thread_mutex_lock( &slap_op_mutex );
120 LDAP_STAILQ_INSERT_HEAD( &slap_free_ops, op, o_next );
121 ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
125 slap_op_time(time_t *t, int *nop)
127 *t = slap_get_time();
128 ldap_pvt_thread_mutex_lock( &slap_op_mutex );
129 if ( *t == last_time ) {
136 ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
148 ldap_pvt_thread_mutex_lock( &slap_op_mutex );
149 if ((op = LDAP_STAILQ_FIRST( &slap_free_ops ))) {
150 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
152 ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
155 op = (Operation *) ch_calloc( 1, sizeof(OperationBuffer) );
156 op->o_hdr = &((OperationBuffer *) op)->ob_hdr;
157 op->o_controls = ((OperationBuffer *) op)->ob_controls;
164 slap_op_time( &op->o_time, &op->o_tincr );
166 op->o_res_ber = NULL;
168 #if defined( LDAP_SLAPI )
169 if ( slapi_plugins_used ) {
170 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
172 #endif /* defined( LDAP_SLAPI ) */
178 slap_req2op( ber_tag_t tag )
183 case LDAP_REQ_UNBIND:
184 return SLAP_OP_UNBIND;
187 case LDAP_REQ_DELETE:
188 return SLAP_OP_DELETE;
189 case LDAP_REQ_MODRDN:
190 return SLAP_OP_MODRDN;
191 case LDAP_REQ_MODIFY:
192 return SLAP_OP_MODIFY;
193 case LDAP_REQ_COMPARE:
194 return SLAP_OP_COMPARE;
195 case LDAP_REQ_SEARCH:
196 return SLAP_OP_SEARCH;
197 case LDAP_REQ_ABANDON:
198 return SLAP_OP_ABANDON;
199 case LDAP_REQ_EXTENDED:
200 return SLAP_OP_EXTENDED;