]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
don't leak callbacks if stuff cannot be registered; provide a means to dispose of...
[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-2006 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 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_CANCEL);
32
33 int cancel_extop( Operation *op, SlapReply *rs )
34 {
35         Operation *o;
36         int rc;
37         int opid;
38         BerElement *ber;
39
40         assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 );
41
42         if ( op->ore_reqdata == NULL ) {
43                 rs->sr_text = "no message ID supplied";
44                 return LDAP_PROTOCOL_ERROR;
45         }
46
47         ber = ber_init( op->ore_reqdata );
48         if ( ber == NULL ) {
49                 rs->sr_text = "internal error";
50                 return LDAP_OTHER;
51         }
52
53         if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
54                 rs->sr_text = "message ID parse failed";
55                 return LDAP_PROTOCOL_ERROR;
56         }
57
58         (void) ber_free( ber, 1 );
59
60         if ( opid < 0 ) {
61                 rs->sr_text = "message ID invalid";
62                 return LDAP_PROTOCOL_ERROR;
63         }
64
65         Statslog( LDAP_DEBUG_STATS, "%s CANCEL msg=%d\n",
66                 op->o_log_prefix, opid, 0, 0, 0 );
67
68         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
69         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) {
70                 if ( o->o_msgid == opid ) {
71                         LDAP_STAILQ_REMOVE( &op->o_conn->c_pending_ops, o, slap_op, o_next );
72                         LDAP_STAILQ_NEXT(o, o_next) = NULL;
73                         op->o_conn->c_n_ops_pending--;
74                         slap_op_free( o );
75                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
76                         return LDAP_SUCCESS;
77                 }
78         }
79
80         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
81                 if ( o->o_msgid == opid ) {
82                         o->o_abandon = 1;
83                         break;
84                 }
85         }
86
87         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
88
89         if ( o ) {
90                 if ( o->o_cancel != SLAP_CANCEL_NONE ) {
91                         rs->sr_text = "message ID already being cancelled";
92                         return LDAP_PROTOCOL_ERROR;
93                 }
94
95                 o->o_cancel = SLAP_CANCEL_REQ;
96
97                 LDAP_STAILQ_FOREACH( op->o_bd, &backendDB, be_next ) {
98                         if( !op->o_bd->be_cancel ) continue;
99
100                         op->oq_cancel.rs_msgid = opid;
101                         if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
102                                 return LDAP_SUCCESS;
103                         }
104                 }
105
106                 while ( o->o_cancel == SLAP_CANCEL_REQ ) {
107                         ldap_pvt_thread_yield();
108                 }
109
110                 if ( o->o_cancel == SLAP_CANCEL_ACK ) {
111                         rc = LDAP_SUCCESS;
112                 } else {
113                         rc = o->o_cancel;
114                 }
115
116                 o->o_cancel = SLAP_CANCEL_DONE;
117         } else {
118                 rs->sr_text = "message ID not found";
119                 rc = LDAP_NO_SUCH_OPERATION;
120         }
121
122         return rc;
123 }