]> git.sur5r.net Git - openldap/blob - servers/slapd/operation.c
e65fd4d7ad9c5e3dd3411d26128e7ce6cdf23077
[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-2005 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 );
57         }
58         ldap_pvt_thread_mutex_destroy( &slap_op_mutex );
59 }
60
61 void
62 slap_op_free( Operation *op )
63 {
64         struct berval slap_empty_bv_dup;
65
66         assert( LDAP_STAILQ_NEXT(op, o_next) == NULL );
67
68         if ( op->o_ber != NULL ) {
69                 ber_free( op->o_ber, 1 );
70         }
71         if ( op->o_dn.bv_val != NULL ) {
72                 free( op->o_dn.bv_val );
73         }
74         if ( op->o_ndn.bv_val != NULL ) {
75                 free( op->o_ndn.bv_val );
76         }
77         if ( op->o_authmech.bv_val != NULL ) {
78                 free( op->o_authmech.bv_val );
79         }
80         if ( op->o_ctrls != NULL ) {
81                 slap_free_ctrls( op, op->o_ctrls );
82         }
83
84 #ifdef LDAP_CONNECTIONLESS
85         if ( op->o_res_ber != NULL ) {
86                 ber_free( op->o_res_ber, 1 );
87         }
88 #endif
89
90         {
91                 GroupAssertion *g, *n;
92                 for (g = op->o_groups; g; g=n) {
93                         n = g->ga_next;
94                         slap_sl_free(g, op->o_tmpmemctx);
95                 }
96                 op->o_groups = NULL;
97         }
98
99 #if defined( LDAP_SLAPI )
100         if ( op->o_pb != NULL ) {
101                 slapi_pblock_destroy( (Slapi_PBlock *)op->o_pb );
102                 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, op );
103         }
104 #endif /* defined( LDAP_SLAPI ) */
105
106
107         memset( op, 0, sizeof(Operation) + sizeof(Opheader) + SLAP_MAX_CIDS * sizeof(void *) );
108         op->o_hdr = (Opheader *)(op+1);
109         op->o_controls = (void **)(op->o_hdr+1);
110
111         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
112         LDAP_STAILQ_INSERT_HEAD( &slap_free_ops, op, o_next );
113         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
114 }
115
116 Operation *
117 slap_op_alloc(
118     BerElement          *ber,
119     ber_int_t   msgid,
120     ber_tag_t   tag,
121     ber_int_t   id
122 )
123 {
124         Operation       *op;
125         struct berval slap_empty_bv_dup;
126
127         ldap_pvt_thread_mutex_lock( &slap_op_mutex );
128         if ((op = LDAP_STAILQ_FIRST( &slap_free_ops ))) {
129                 LDAP_STAILQ_REMOVE_HEAD( &slap_free_ops, o_next );
130         }
131         ldap_pvt_thread_mutex_unlock( &slap_op_mutex );
132
133         if (!op) {
134                 op = (Operation *) ch_calloc( 1, sizeof(Operation)
135                         + sizeof(Opheader) + SLAP_MAX_CIDS * sizeof(void *) );
136                 op->o_hdr = (Opheader *)(op + 1);
137                 op->o_controls = (void **)(op->o_hdr+1);
138         }
139
140         op->o_ber = ber;
141         op->o_msgid = msgid;
142         op->o_tag = tag;
143
144         op->o_time = slap_get_time();
145         op->o_opid = id;
146         op->o_res_ber = NULL;
147
148 #if defined( LDAP_SLAPI )
149         if ( slapi_plugins_used ) {
150                 op->o_pb = slapi_pblock_new();
151                 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
152         }
153 #endif /* defined( LDAP_SLAPI ) */
154
155         return( op );
156 }