]> git.sur5r.net Git - openldap/blob - servers/slapd/operation.c
10bacb21d8d957d4bd3c9040f3b8bf9d147f1877
[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-2004 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 LDAP_STAILQ_HEAD(s_o, slap_op)   slap_free_ops;
42
43 void slap_op_init(void)
44 {
45         ldap_pvt_thread_mutex_init( &slap_op_mutex );
46         LDAP_STAILQ_INIT(&slap_free_ops);
47 }
48
49 void slap_op_destroy(void)
50 {
51         Operation *o;
52
53         while ( (o = LDAP_STAILQ_FIRST( &slap_free_ops )) != NULL) {
54                 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
55                 LDAP_STAILQ_NEXT(o, o_next) = NULL;
56                 ch_free( o->o_controls );
57                 ch_free( o );
58         }
59         ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
60 }
61
62 void
63 slap_op_free( Operation *op )
64 {
65         struct berval slap_empty_bv_dup;
66         void **controls;
67
68         assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
69
70         if ( op->o_ber != NULL ) {
71                 ber_free( op->o_ber, 1 );
72         }
73         if ( op->o_dn.bv_val != NULL ) {
74                 free( op->o_dn.bv_val );
75         }
76         if ( op->o_ndn.bv_val != NULL ) {
77                 free( op->o_ndn.bv_val );
78         }
79         if ( op->o_authmech.bv_val != NULL ) {
80                 free( op->o_authmech.bv_val );
81         }
82         if ( op->o_ctrls != NULL ) {
83                 slap_free_ctrls( op, op->o_ctrls );
84         }
85
86 #ifdef LDAP_CONNECTIONLESS
87         if ( op->o_res_ber != NULL ) {
88                 ber_free( op->o_res_ber, 1 );
89         }
90 #endif
91
92         slap_sync_cookie_free( &op->o_sync_state, 0 );
93
94         {
95                 GroupAssertion *g, *n;
96                 for (g = op->o_groups; g; g=n) {
97                         n = g->ga_next;
98                         slap_sl_free(g, op->o_tmpmemctx);
99                 }
100                 op->o_groups = NULL;
101         }
102
103 #if defined( LDAP_SLAPI )
104         if ( op->o_pb != NULL ) {
105                 slapi_pblock_destroy( (Slapi_PBlock *)op->o_pb );
106                 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
107         }
108 #endif /* defined( LDAP_SLAPI ) */
109
110         if ( op->o_sync_csn.bv_val != NULL ) {
111                 ch_free( op->o_sync_csn.bv_val );
112         }
113
114         controls = op->o_controls;
115         memset( controls, 0, sizeof(void *) * SLAP_MAX_CIDS );
116         memset( op, 0, sizeof(Operation) );
117         op->o_controls = controls;
118
119         op->o_sync_state.sid = -1;
120         op->o_sync_slog_size = -1;
121         op->o_sync_state.rid = -1;
122         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
123         LDAP_STAILQ_INSERT_HEAD( &slap_free_ops, op, o_next );
124         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
125 }
126
127 Operation *
128 slap_op_alloc(
129     BerElement          *ber,
130     ber_int_t   msgid,
131     ber_tag_t   tag,
132     ber_int_t   id
133 )
134 {
135         Operation       *op;
136         struct berval slap_empty_bv_dup;
137
138         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
139         if ((op = LDAP_STAILQ_FIRST( &slap_free_ops ))) {
140                 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
141         }
142         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
143
144         if (!op) {
145                 op = (Operation *) ch_calloc( 1, sizeof(Operation) );
146                 op->o_controls = (void **) ch_calloc( SLAP_MAX_CIDS, sizeof( void *));
147         }
148
149         op->o_ber = ber;
150         op->o_msgid = msgid;
151         op->o_tag = tag;
152
153         op->o_time = slap_get_time();
154         op->o_opid = id;
155         op->o_res_ber = NULL;
156
157         op->o_sync_state.sid = -1;
158         op->o_sync_slog_size = -1;
159         op->o_sync_state.rid = -1;
160         LDAP_STAILQ_FIRST( &op->o_sync_slog_list ) = NULL;
161         op->o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST( &op->o_sync_slog_list );
162
163 #if defined( LDAP_SLAPI )
164         if ( slapi_plugins_used ) {
165                 op->o_pb = slapi_pblock_new();
166                 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
167         }
168 #endif /* defined( LDAP_SLAPI ) */
169
170         return( op );
171 }