1 /* cancel.c - LDAP cancel extended operation */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
30 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_CANCEL);
32 int cancel_extop( Operation *op, SlapReply *rs )
39 assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 );
41 if ( op->ore_reqdata == NULL ) {
42 rs->sr_text = "no message ID supplied";
43 return LDAP_PROTOCOL_ERROR;
46 ber = ber_init( op->ore_reqdata );
48 rs->sr_text = "internal error";
52 if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
53 rs->sr_text = "message ID parse failed";
54 return LDAP_PROTOCOL_ERROR;
57 (void) ber_free( ber, 1 );
59 Statslog( LDAP_DEBUG_STATS, "%s CANCEL msg=%d\n",
60 op->o_log_prefix, opid, 0, 0, 0 );
63 rs->sr_text = "message ID invalid";
64 return LDAP_PROTOCOL_ERROR;
67 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_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
72 /* TODO: We could instead remove the cancelled operation
73 * from c_pending_ops like Abandon does, and send its
74 * response here. Not if it is pending because of a
75 * congested connection though.
77 rs->sr_text = "too busy for Cancel, try Abandon instead";
78 return LDAP_CANNOT_CANCEL;
82 LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
83 if ( o->o_msgid == opid ) {
89 rc = LDAP_NO_SUCH_OPERATION;
90 rs->sr_text = "message ID not found";
91 } else if ( o->o_cancel != SLAP_CANCEL_NONE ) {
92 rc = LDAP_PROTOCOL_ERROR;
93 rs->sr_text = "message ID already being cancelled";
96 o->o_cancel = SLAP_CANCEL_REQ;
100 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
102 if ( rc == LDAP_SUCCESS ) {
103 LDAP_STAILQ_FOREACH( op->o_bd, &backendDB, be_next ) {
104 if( !op->o_bd->be_cancel ) continue;
106 op->oq_cancel.rs_msgid = opid;
107 if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
112 while ( o->o_cancel == SLAP_CANCEL_REQ ) {
113 ldap_pvt_thread_yield();
116 if ( o->o_cancel == SLAP_CANCEL_ACK ) {
122 o->o_cancel = SLAP_CANCEL_DONE;