]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
4416869ad26f936ab92d9a959e0489ee475cd012
[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(
25         Connection *conn,
26         Operation *op,
27         const char *reqoid,
28         struct berval *reqdata,
29         char **rspoid,
30         struct berval **rspdata,
31         LDAPControl ***rspctrls,
32         const char **text,
33         BerVarray *refs )
34 {
35         Backend *be;
36         int rc;
37         int found = 0;
38         int opid;
39         BerElement *ber;
40         int i;
41
42         assert( reqoid != NULL );
43         assert( strcmp( LDAP_EXOP_X_CANCEL, reqoid ) == 0 );
44
45         if ( reqdata == NULL ) {
46                 *text = "no message ID supplied";
47                 return LDAP_PROTOCOL_ERROR;
48         }
49
50         ber = ber_init( reqdata );
51         if ( ber == NULL ) {
52                 *text = "internal error";
53                 return LDAP_OTHER;
54         }
55
56         if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
57                 *text = "message ID parse failed";
58                 return LDAP_PROTOCOL_ERROR;
59         }
60
61         (void) ber_free( ber, 1 );
62
63         if ( opid < 0 ) {
64                 *text = "message ID invalid";
65                 return LDAP_PROTOCOL_ERROR;
66         }
67
68         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
69         LDAP_STAILQ_FOREACH( op, &conn->c_pending_ops, o_next ) {
70                 if ( op->o_msgid == opid ) {
71                         LDAP_STAILQ_REMOVE( &conn->c_pending_ops, op, slap_op, o_next );
72                         slap_op_free( op );
73                         found = 1;
74                         break;
75                 }
76         }
77         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
78
79         if ( found ) return LDAP_SUCCESS;
80
81         found = 0;
82         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
83         LDAP_STAILQ_FOREACH( op, &conn->c_ops, o_next ) {
84                 if ( op->o_msgid == opid ) {
85                         found = 1;
86                         break;
87                 }
88         }
89
90         if ( !found ) {
91 #ifdef LDAP_SYNC
92                 for ( i = 0; i < nbackends; i++ ) {
93                         Backend *be = &backends[i];
94                         if( !be->be_abandon ) continue;
95
96
97                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
98
99                         if ( be->be_cancel( be, conn, op, opid ) == LDAP_SUCCESS ) {
100                                 return LDAP_SUCCESS;
101                         } else {
102                                 *text = "message ID not found";
103                                 return LDAP_NO_SUCH_OPERATION;
104                         }
105                 }
106 #else
107                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
108                 *text = "message ID not found";
109                 return LDAP_NO_SUCH_OPERATION;
110 #endif
111         }
112
113         if ( op->o_cancel != SLAP_CANCEL_NONE ) {
114                 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
115                 *text = "message ID already being cancelled";
116                 return LDAP_PROTOCOL_ERROR;
117         }
118
119         op->o_cancel = SLAP_CANCEL_REQ;
120         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
121
122         while ( op->o_cancel == SLAP_CANCEL_REQ ) {
123                 ldap_pvt_thread_yield();
124         }
125
126         if ( op->o_cancel == SLAP_CANCEL_ACK ) {
127                 rc = LDAP_SUCCESS;
128         } else {
129                 rc = op->o_cancel;
130         }
131
132         op->o_cancel = SLAP_CANCEL_DONE;
133
134         return rc;
135 }
136
137 #endif /* LDAP_EXOP_X_CANCEL */