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