]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
now I remember why I introduced the 'has_ldapinfo_dn_ru' flag
[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-2004 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 found = 0;
36         int opid;
37         BerElement *ber;
38         int i;
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         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
66         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) {
67                 if ( o->o_msgid == opid ) {
68                         LDAP_STAILQ_REMOVE( &op->o_conn->c_pending_ops, o, slap_op, o_next );
69                         slap_op_free( o );
70                         found = 1;
71                         break;
72                 }
73         }
74         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
75
76         if ( found ) return LDAP_SUCCESS;
77
78         found = 0;
79         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
80         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
81                 if ( o->o_msgid == opid ) {
82                         found = 1;
83                         break;
84                 }
85         }
86
87         if ( !found ) {
88                 for ( i = 0; i < nbackends; i++ ) {
89                         op->o_bd = &backends[i];
90                         if( !op->o_bd->be_cancel ) continue;
91
92                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
93
94                         op->oq_cancel.rs_msgid = opid;
95                         if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
96                                 return LDAP_SUCCESS;
97                         }
98                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
99                 }
100                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
101                 rs->sr_text = "message ID not found";
102                 return LDAP_NO_SUCH_OPERATION;
103         }
104
105         if ( op->o_cancel != SLAP_CANCEL_NONE ) {
106                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
107                 rs->sr_text = "message ID already being cancelled";
108                 return LDAP_PROTOCOL_ERROR;
109         }
110
111         op->o_cancel = SLAP_CANCEL_REQ;
112         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
113
114         while ( op->o_cancel == SLAP_CANCEL_REQ ) {
115                 ldap_pvt_thread_yield();
116         }
117
118         if ( op->o_cancel == SLAP_CANCEL_ACK ) {
119                 rc = LDAP_SUCCESS;
120         } else {
121                 rc = op->o_cancel;
122         }
123
124         op->o_cancel = SLAP_CANCEL_DONE;
125
126         return rc;
127 }