]> git.sur5r.net Git - openldap/blob - servers/slapd/cancel.c
Use a separate mutex for the replication timestamp
[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-2003 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 #ifdef LDAP_EXOP_X_CANCEL
29
30 #include <lber_pvt.h>
31 #include <lutil.h>
32
33 int cancel_extop( Operation *op, SlapReply *rs )
34 {
35         Operation *o;
36         int rc;
37         int found = 0;
38         int opid;
39         BerElement *ber;
40         int i;
41
42         assert( ber_bvcmp( &slap_EXOP_CANCEL, &op->ore_reqoid ) == 0 );
43
44         if ( op->ore_reqdata == NULL ) {
45                 rs->sr_text = "no message ID supplied";
46                 return LDAP_PROTOCOL_ERROR;
47         }
48
49         ber = ber_init( op->ore_reqdata );
50         if ( ber == NULL ) {
51                 rs->sr_text = "internal error";
52                 return LDAP_OTHER;
53         }
54
55         if ( ber_scanf( ber, "{i}", &opid ) == LBER_ERROR ) {
56                 rs->sr_text = "message ID parse failed";
57                 return LDAP_PROTOCOL_ERROR;
58         }
59
60         (void) ber_free( ber, 1 );
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         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_pending_ops, o_next ) {
69                 if ( o->o_msgid == opid ) {
70                         LDAP_STAILQ_REMOVE( &op->o_conn->c_pending_ops, o, slap_op, o_next );
71                         slap_op_free( o );
72                         found = 1;
73                         break;
74                 }
75         }
76         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
77
78         if ( found ) return LDAP_SUCCESS;
79
80         found = 0;
81         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
82         LDAP_STAILQ_FOREACH( o, &op->o_conn->c_ops, o_next ) {
83                 if ( o->o_msgid == opid ) {
84                         found = 1;
85                         break;
86                 }
87         }
88
89         if ( !found ) {
90                 for ( i = 0; i < nbackends; i++ ) {
91                         op->o_bd = &backends[i];
92                         if( !op->o_bd->be_cancel ) continue;
93
94                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
95
96                         op->oq_cancel.rs_msgid = opid;
97                         if ( op->o_bd->be_cancel( op, rs ) == LDAP_SUCCESS ) {
98                                 return LDAP_SUCCESS;
99                         }
100                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
101                 }
102                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
103                 rs->sr_text = "message ID not found";
104                 return LDAP_NO_SUCH_OPERATION;
105         }
106
107         if ( op->o_cancel != SLAP_CANCEL_NONE ) {
108                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
109                 rs->sr_text = "message ID already being cancelled";
110                 return LDAP_PROTOCOL_ERROR;
111         }
112
113         op->o_cancel = SLAP_CANCEL_REQ;
114         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
115
116         while ( op->o_cancel == SLAP_CANCEL_REQ ) {
117                 ldap_pvt_thread_yield();
118         }
119
120         if ( op->o_cancel == SLAP_CANCEL_ACK ) {
121                 rc = LDAP_SUCCESS;
122         } else {
123                 rc = op->o_cancel;
124         }
125
126         op->o_cancel = SLAP_CANCEL_DONE;
127
128         return rc;
129 }
130
131 #endif /* LDAP_EXOP_X_CANCEL */