]> git.sur5r.net Git - openldap/blob - servers/slapd/operation.c
c144c19cfcc4b0a95b2fb2122c4236de73b6b882
[openldap] / servers / slapd / operation.c
1 /* operation.c - routines to deal with pending ldap operations */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16
17 #ifdef LDAP_SLAPI
18 #include "slapi.h"
19 #endif
20
21 static ldap_pvt_thread_mutex_t  slap_op_mutex;
22 static LDAP_STAILQ_HEAD(s_o, slap_op)   slap_free_ops;
23
24 void slap_op_init(void)
25 {
26         ldap_pvt_thread_mutex_init( &slap_op_mutex );
27         LDAP_STAILQ_INIT(&slap_free_ops);
28 }
29
30 void slap_op_destroy(void)
31 {
32         Operation *o;
33
34         while ( (o = LDAP_STAILQ_FIRST( &slap_free_ops )) != NULL) {
35                 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
36                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
37                 ch_free( o );
38         }
39         ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
40 }
41
42 void
43 slap_op_free( Operation *op )
44 {
45         assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
46
47         if ( op->o_ber != NULL ) {
48                 ber_free( op->o_ber, 1 );
49         }
50         if ( op->o_dn.bv_val != NULL ) {
51                 free( op->o_dn.bv_val );
52         }
53         if ( op->o_ndn.bv_val != NULL ) {
54                 free( op->o_ndn.bv_val );
55         }
56         if ( op->o_authmech.bv_val != NULL ) {
57                 free( op->o_authmech.bv_val );
58         }
59         if ( op->o_ctrls != NULL ) {
60                 slap_free_ctrls( op, op->o_ctrls );
61         }
62
63 #ifdef LDAP_CONNECTIONLESS
64         if ( op->o_res_ber != NULL ) {
65                 ber_free( op->o_res_ber, 1 );
66         }
67 #endif
68         if ( op->o_sync_state.bv_val != NULL ) {
69                 free( op->o_sync_state.bv_val );
70         }
71
72         {
73                 GroupAssertion *g, *n;
74                 for (g = op->o_groups; g; g=n) {
75                         n = g->ga_next;
76                         sl_free(g, op->o_tmpmemctx);
77                 }
78                 op->o_groups = NULL;
79         }
80
81 #if defined( LDAP_SLAPI )
82         if ( op->o_pb != NULL ) {
83                 slapi_pblock_destroy( (Slapi_PBlock *)op->o_pb );
84                 slapi_x_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
85         }
86 #endif /* defined( LDAP_SLAPI ) */
87
88         memset( op, 0, sizeof(Operation) );
89         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
90         LDAP_STAILQ_INSERT_HEAD( &slap_free_ops, op, o_next );
91         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
92 }
93
94 Operation *
95 slap_op_alloc(
96     BerElement          *ber,
97     ber_int_t   msgid,
98     ber_tag_t   tag,
99     ber_int_t   id
100 )
101 {
102         Operation       *op;
103
104         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
105         if ((op = LDAP_STAILQ_FIRST( &slap_free_ops ))) {
106                 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
107         }
108         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
109
110         if (!op)
111                 op = (Operation *) ch_calloc( 1, sizeof(Operation) );
112
113         op->o_ber = ber;
114         op->o_msgid = msgid;
115         op->o_tag = tag;
116
117         op->o_time = slap_get_time();
118         op->o_opid = id;
119         op->o_res_ber = NULL;
120
121 #if defined( LDAP_SLAPI )
122         if ( slapi_plugins_used ) {
123                 op->o_pb = slapi_pblock_new();
124                 slapi_x_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
125         }
126 #endif /* defined( LDAP_SLAPI ) */
127
128         return( op );
129 }