]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
Memory context tweaks for Bind
[openldap] / servers / slapd / cancel.c
1 /* $OpenLDAP$ */
2 /* cancel.c - LDAP cancel extended operation */
3 /*
4  * Copyright 1998-2002 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/krb.h>
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16
17 #include "slap.h"
18
19 #ifdef LDAP_EXOP_X_CANCEL
20
21 #include <lber_pvt.h>
22 #include <lutil.h>
23
24 int cancel_extop( Operation *op, SlapReply *rs )
25 {
26         Operation *o;
27         int rc;
28         int found = 0;
29         int opid;
30         BerElement *ber;
31         int i;
32
33         assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 );
34
35         if ( op->ore_reqdata == NULL ) {
36                 rs->sr_text = "no message ID supplied";
37                 return LDAP_PROTOCOL_ERROR;
38         }
39
40         ber = ber_init( op->ore_reqdata );
41         if ( ber == NULL ) {
42                 rs->sr_text = "internal error";
43                 return LDAP_OTHER;
44         }
45
46         if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
47                 rs->sr_text = "message ID parse failed";
48                 return LDAP_PROTOCOL_ERROR;
49         }
50
51         (void) ber_free( ber, 1 );
52
53         if ( opid < 0 ) {
54                 rs->sr_text = "message ID invalid";
55                 return LDAP_PROTOCOL_ERROR;
56         }
57
58         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) {
60                 if ( o->o_msgid == opid ) {
61                         LDAP_STAILQ_REMOVE( &op->o_conn->c_pending_ops, o, slap_op, o_next );
62                         slap_op_free( o );
63                         found = 1;
64                         break;
65                 }
66         }
67         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
68
69         if ( found ) return LDAP_SUCCESS;
70
71         found = 0;
72         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
73         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
74                 if ( o->o_msgid == opid ) {
75                         found = 1;
76                         break;
77                 }
78         }
79
80         if ( !found ) {
81 #ifdef LDAP_SYNC
82                 for ( i = 0; i < nbackends; i++ ) {
83                         op->o_bd = &backends[i];
84                         if( !op->o_bd->be_cancel ) continue;
85
86                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
87
88                         op->oq_cancel.rs_msgid = opid;
89                         if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
90                                 return LDAP_SUCCESS;
91                         }
92                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
93                 }
94 #endif
95                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
96                 rs->sr_text = "message ID not found";
97                 return LDAP_NO_SUCH_OPERATION;
98         }
99
100         if ( op->o_cancel != SLAP_CANCEL_NONE ) {
101                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
102                 rs->sr_text = "message ID already being cancelled";
103                 return LDAP_PROTOCOL_ERROR;
104         }
105
106         op->o_cancel = SLAP_CANCEL_REQ;
107         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
108
109         while ( op->o_cancel == SLAP_CANCEL_REQ ) {
110                 ldap_pvt_thread_yield();
111         }
112
113         if ( op->o_cancel == SLAP_CANCEL_ACK ) {
114                 rc = LDAP_SUCCESS;
115         } else {
116                 rc = op->o_cancel;
117         }
118
119         op->o_cancel = SLAP_CANCEL_DONE;
120
121         return rc;
122 }
123
124 #endif /* LDAP_EXOP_X_CANCEL */