]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
0916bcac9802f5fec580c87db00e78fc7c6cccf9
[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                 for ( i = 0; i < nbackends; i++ ) {
82                         op->o_bd = &backends[i];
83                         if( !op->o_bd->be_cancel ) continue;
84
85                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
86
87                         op->oq_cancel.rs_msgid = opid;
88                         if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
89                                 return LDAP_SUCCESS;
90                         }
91                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
92                 }
93                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
94                 rs->sr_text = "message ID not found";
95                 return LDAP_NO_SUCH_OPERATION;
96         }
97
98         if ( op->o_cancel != SLAP_CANCEL_NONE ) {
99                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
100                 rs->sr_text = "message ID already being cancelled";
101                 return LDAP_PROTOCOL_ERROR;
102         }
103
104         op->o_cancel = SLAP_CANCEL_REQ;
105         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
106
107         while ( op->o_cancel == SLAP_CANCEL_REQ ) {
108                 ldap_pvt_thread_yield();
109         }
110
111         if ( op->o_cancel == SLAP_CANCEL_ACK ) {
112                 rc = LDAP_SUCCESS;
113         } else {
114                 rc = op->o_cancel;
115         }
116
117         op->o_cancel = SLAP_CANCEL_DONE;
118
119         return rc;
120 }
121
122 #endif /* LDAP_EXOP_X_CANCEL */