]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_5
[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-2014 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/socket.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
24
25 #include "slap.h"
26
27 #include <lber_pvt.h>
28 #include <lutil.h>
29
30 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_CANCEL);
31
32 int cancel_extop( Operation *op, SlapReply *rs )
33 {
34         Operation *o;
35         int rc;
36         int opid;
37         BerElement *ber;
38
39         assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 );
40
41         if ( op->ore_reqdata == NULL ) {
42                 rs->sr_text = "no message ID supplied";
43                 return LDAP_PROTOCOL_ERROR;
44         }
45
46         ber = ber_init( op->ore_reqdata );
47         if ( ber == NULL ) {
48                 rs->sr_text = "internal error";
49                 return LDAP_OTHER;
50         }
51
52         if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
53                 rs->sr_text = "message ID parse failed";
54                 return LDAP_PROTOCOL_ERROR;
55         }
56
57         (void) ber_free( ber, 1 );
58
59         Statslog( LDAP_DEBUG_STATS, "%s CANCEL msg=%d\n",
60                 op->o_log_prefix, opid, 0, 0, 0 );
61
62         if ( opid < 0 ) {
63                 rs->sr_text = "message ID invalid";
64                 return LDAP_PROTOCOL_ERROR;
65         }
66
67         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
68
69         if ( op->o_abandon ) {
70                 /* FIXME: Should instead reject the cancel/abandon of this op, but
71                  * it seems unsafe to reset op->o_abandon once it is set. ITS#6138.
72                  */
73                 rc = LDAP_OPERATIONS_ERROR;
74                 rs->sr_text = "tried to abandon or cancel this operation";
75                 goto out;
76         }
77
78         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) {
79                 if ( o->o_msgid == opid ) {
80                         /* TODO: We could instead remove the cancelled operation
81                          * from c_pending_ops like Abandon does, and send its
82                          * response here.  Not if it is pending because of a
83                          * congested connection though.
84                          */
85                         rc = LDAP_CANNOT_CANCEL;
86                         rs->sr_text = "too busy for Cancel, try Abandon instead";
87                         goto out;
88                 }
89         }
90
91         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
92                 if ( o->o_msgid == opid ) {
93                         break;
94                 }
95         }
96
97         if ( o == NULL ) {
98                 rc = LDAP_NO_SUCH_OPERATION;
99                 rs->sr_text = "message ID not found";
100
101         } else if ( o->o_tag == LDAP_REQ_BIND
102                         || o->o_tag == LDAP_REQ_UNBIND
103                         || o->o_tag == LDAP_REQ_ABANDON ) {
104                 rc = LDAP_CANNOT_CANCEL;
105
106         } else if ( o->o_cancel != SLAP_CANCEL_NONE ) {
107                 rc = LDAP_OPERATIONS_ERROR;
108                 rs->sr_text = "message ID already being cancelled";
109
110 #if 0
111         } else if ( o->o_abandon ) {
112                 /* TODO: Would this break something when
113                  * o_abandon="suppress response"? (ITS#6138)
114                  */
115                 rc = LDAP_TOO_LATE;
116 #endif
117
118         } else {
119                 rc = LDAP_SUCCESS;
120                 o->o_cancel = SLAP_CANCEL_REQ;
121                 o->o_abandon = 1;
122         }
123
124  out:
125         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
126
127         if ( rc == LDAP_SUCCESS ) {
128                 LDAP_STAILQ_FOREACH( op->o_bd, &backendDB, be_next ) {
129                         if( !op->o_bd->be_cancel ) continue;
130
131                         op->oq_cancel.rs_msgid = opid;
132                         if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
133                                 return LDAP_SUCCESS;
134                         }
135                 }
136
137                 do {
138                         /* Fake a cond_wait with thread_yield, then
139                          * verify the result properly mutex-protected.
140                          */
141                         while ( o->o_cancel == SLAP_CANCEL_REQ )
142                                 ldap_pvt_thread_yield();
143                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
144                         rc = o->o_cancel;
145                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
146                 } while ( rc == SLAP_CANCEL_REQ );
147
148                 if ( rc == SLAP_CANCEL_ACK ) {
149                         rc = LDAP_SUCCESS;
150                 }
151
152                 o->o_cancel = SLAP_CANCEL_DONE;
153         }
154
155         return rc;
156 }