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