]> git.sur5r.net Git - openldap/blob - servers/slapd/operation.c
75773e4ee1514a0d17611eddd4d95a3e43c7ea6d
[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-2008 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 static time_t last_time;
43 static int last_incr;
44
45 void slap_op_init(void)
46 {
47         ldap_pvt_thread_mutex_init( &slap_op_mutex );
48         LDAP_STAILQ_INIT(&slap_free_ops);
49 }
50
51 void slap_op_destroy(void)
52 {
53         Operation *o;
54
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;
58                 ch_free( o );
59         }
60         ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
61 }
62
63 void
64 slap_op_groups_free( Operation *op )
65 {
66         GroupAssertion *g, *n;
67         for ( g = op->o_groups; g; g = n ) {
68                 n = g->ga_next;
69                 slap_sl_free( g, op->o_tmpmemctx );
70         }
71         op->o_groups = NULL;
72 }
73
74 void
75 slap_op_free( Operation *op )
76 {
77         assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
78
79         if ( op->o_ber != NULL ) {
80                 ber_free( op->o_ber, 1 );
81         }
82         if ( !BER_BVISNULL( &op->o_dn ) ) {
83                 ch_free( op->o_dn.bv_val );
84         }
85         if ( !BER_BVISNULL( &op->o_ndn ) ) {
86                 ch_free( op->o_ndn.bv_val );
87         }
88         if ( !BER_BVISNULL( &op->o_authmech ) ) {
89                 ch_free( op->o_authmech.bv_val );
90         }
91         if ( op->o_ctrls != NULL ) {
92                 slap_free_ctrls( op, op->o_ctrls );
93         }
94
95 #ifdef LDAP_CONNECTIONLESS
96         if ( op->o_res_ber != NULL ) {
97                 ber_free( op->o_res_ber, 1 );
98         }
99 #endif
100
101         if ( op->o_groups ) {
102                 slap_op_groups_free( op );
103         }
104
105 #if defined( LDAP_SLAPI )
106         if ( slapi_plugins_used ) {
107                 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
108         }
109 #endif /* defined( LDAP_SLAPI ) */
110
111
112         memset( op, 0, sizeof(Operation) + sizeof(Opheader) + SLAP_MAX_CIDS * sizeof(void *) );
113         op->o_hdr = (Opheader *)(op+1);
114         op->o_controls = (void **)(op->o_hdr+1);
115
116         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
117         LDAP_STAILQ_INSERT_HEAD( &slap_free_ops, op, o_next );
118         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
119 }
120
121 void
122 slap_op_time(time_t *t, int *nop)
123 {
124         *t = slap_get_time();
125         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
126         if ( *t == last_time ) {
127                 *nop = ++last_incr;
128         } else {
129                 last_time = *t;
130                 last_incr = 0;
131                 *nop = 0;
132         }
133         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
134 }
135
136 Operation *
137 slap_op_alloc(
138     BerElement          *ber,
139     ber_int_t   msgid,
140     ber_tag_t   tag,
141     ber_int_t   id
142 )
143 {
144         Operation       *op;
145
146         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
147         if ((op = LDAP_STAILQ_FIRST( &slap_free_ops ))) {
148                 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
149         }
150         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
151
152         if (!op) {
153                 op = (Operation *) ch_calloc( 1, sizeof(Operation)
154                         + sizeof(Opheader) + SLAP_MAX_CIDS * sizeof(void *) );
155                 op->o_hdr = (Opheader *)(op + 1);
156                 op->o_controls = (void **)(op->o_hdr+1);
157         }
158
159         op->o_ber = ber;
160         op->o_msgid = msgid;
161         op->o_tag = tag;
162
163         slap_op_time( &op->o_time, &op->o_tincr );
164         op->o_opid = id;
165         op->o_res_ber = NULL;
166
167 #if defined( LDAP_SLAPI )
168         if ( slapi_plugins_used ) {
169                 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
170         }
171 #endif /* defined( LDAP_SLAPI ) */
172
173         return( op );
174 }
175
176 slap_op_t
177 slap_req2op( ber_tag_t tag )
178 {
179         switch ( tag ) {
180         case LDAP_REQ_BIND:
181                 return SLAP_OP_BIND;
182         case LDAP_REQ_UNBIND:
183                 return SLAP_OP_UNBIND;
184         case LDAP_REQ_ADD:
185                 return SLAP_OP_ADD;
186         case LDAP_REQ_DELETE:
187                 return SLAP_OP_DELETE;
188         case LDAP_REQ_MODRDN:
189                 return SLAP_OP_MODRDN;
190         case LDAP_REQ_MODIFY:
191                 return SLAP_OP_MODIFY;
192         case LDAP_REQ_COMPARE:
193                 return SLAP_OP_COMPARE;
194         case LDAP_REQ_SEARCH:
195                 return SLAP_OP_SEARCH;
196         case LDAP_REQ_ABANDON:
197                 return SLAP_OP_ABANDON;
198         case LDAP_REQ_EXTENDED:
199                 return SLAP_OP_EXTENDED;
200         }
201
202         return SLAP_OP_LAST;
203 }