]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
fix endless loop in canceling child requests; cleanup
[openldap] / libraries / libldap / abandon.c
1 /* abandon.c */
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 /* Portions  Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "ldap-int.h"
31
32 /*
33  * An abandon request looks like this:
34  *              AbandonRequest ::= [APPLICATION 16] MessageID
35  * and has no response.  (Source: RFC 4511)
36  */
37
38 static int
39 do_abandon(
40         LDAP *ld,
41         ber_int_t origid,
42         ber_int_t msgid,
43         LDAPControl **sctrls,
44         int sendabandon );
45
46 /*
47  * ldap_abandon_ext - perform an ldap extended abandon operation.
48  *
49  * Parameters:
50  *      ld                      LDAP descriptor
51  *      msgid           The message id of the operation to abandon
52  *      scntrls         Server Controls
53  *      ccntrls         Client Controls
54  *
55  * ldap_abandon_ext returns a LDAP error code.
56  *              (LDAP_SUCCESS if everything went ok)
57  *
58  * Example:
59  *      ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
60  */
61 int
62 ldap_abandon_ext(
63         LDAP *ld,
64         int msgid,
65         LDAPControl **sctrls,
66         LDAPControl **cctrls )
67 {
68         int     rc;
69
70         Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
71
72         /* check client controls */
73 #ifdef LDAP_R_COMPILE
74         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
75 #endif
76
77         rc = ldap_int_client_controls( ld, cctrls );
78         if ( rc == LDAP_SUCCESS ) {
79                 rc = do_abandon( ld, msgid, msgid, sctrls, 1 );
80         }
81
82 #ifdef LDAP_R_COMPILE
83         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
84 #endif
85
86         return rc;
87 }
88
89
90 /*
91  * ldap_abandon - perform an ldap abandon operation. Parameters:
92  *
93  *      ld              LDAP descriptor
94  *      msgid           The message id of the operation to abandon
95  *
96  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
97  *
98  * Example:
99  *      ldap_abandon( ld, msgid );
100  */
101 int
102 ldap_abandon( LDAP *ld, int msgid )
103 {
104         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
105         return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
106                 ? 0 : -1;
107 }
108
109
110 int
111 ldap_pvt_discard(
112         LDAP *ld,
113         ber_int_t msgid )
114 {
115         int     rc;
116
117 #ifdef LDAP_R_COMPILE
118         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
119 #endif
120
121         rc = do_abandon( ld, msgid, msgid, NULL, 0 );
122
123 #ifdef LDAP_R_COMPILE
124         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
125 #endif
126
127         return rc;
128 }
129
130 static int
131 do_abandon(
132         LDAP *ld,
133         ber_int_t origid,
134         ber_int_t msgid,
135         LDAPControl **sctrls,
136         int sendabandon )
137 {
138         BerElement      *ber;
139         int             i, err;
140         ber_int_t       *old_abandon;
141         Sockbuf         *sb;
142         LDAPRequest     *lr;
143
144         Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
145                 origid, msgid, 0 );
146
147         /* find the request that we are abandoning */
148 start_again:;
149         lr = ld->ld_requests;
150         while ( lr != NULL ) {
151                 /* this message */
152                 if ( lr->lr_msgid == msgid ) {
153                         break;
154                 }
155
156                 /* child: abandon it */
157                 if ( lr->lr_origid == msgid ) {
158                         (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid,
159                                 sctrls, sendabandon );
160
161                         /* restart, as lr may now be dangling... */
162                         goto start_again;
163                 }
164
165                 lr = lr->lr_next;
166         }
167
168         if ( lr != NULL ) {
169                 if ( origid == msgid && lr->lr_parent != NULL ) {
170                         /* don't let caller abandon child requests! */
171                         ld->ld_errno = LDAP_PARAM_ERROR;
172                         return( LDAP_PARAM_ERROR );
173                 }
174                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
175                         /* no need to send abandon message */
176                         sendabandon = 0;
177                 }
178         }
179
180         /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
181          * while we're in there.
182          */
183 #ifdef LDAP_R_COMPILE
184         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
185 #endif
186         err = ldap_msgdelete( ld, msgid );
187 #ifdef LDAP_R_COMPILE
188         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
189 #endif
190         if ( err == 0 ) {
191                 ld->ld_errno = LDAP_SUCCESS;
192                 return LDAP_SUCCESS;
193         }
194
195         /* fetch again the request that we are abandoning */
196         if ( lr != NULL ) {
197                 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
198                         /* this message */
199                         if ( lr->lr_msgid == msgid ) {
200                                 break;
201                         }
202                 }
203         }
204
205         err = 0;
206         if ( sendabandon ) {
207                 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
208                         /* not connected */
209                         err = -1;
210                         ld->ld_errno = LDAP_SERVER_DOWN;
211
212                 } else if ( ( ber = ldap_alloc_ber_with_options( ld ) ) == NULL ) {
213                         /* BER element allocation failed */
214                         err = -1;
215                         ld->ld_errno = LDAP_NO_MEMORY;
216
217                 } else {
218                         /*
219                          * We already have the mutex in LDAP_R_COMPILE, so
220                          * don't try to get it again.
221                          *              LDAP_NEXT_MSGID(ld, i);
222                          */
223
224                         i = ++(ld)->ld_msgid;
225 #ifdef LDAP_CONNECTIONLESS
226                         if ( LDAP_IS_UDP(ld) ) {
227                                 err = ber_write( ber, ld->ld_options.ldo_peer,
228                                         sizeof(struct sockaddr), 0);
229                         }
230                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
231                                 LDAP_VERSION2 )
232                         {
233                                 char *dn = ld->ld_options.ldo_cldapdn;
234                                 if (!dn) dn = "";
235                                 err = ber_printf( ber, "{isti",  /* '}' */
236                                         i, dn,
237                                         LDAP_REQ_ABANDON, msgid );
238                         } else
239 #endif
240                         {
241                                 /* create a message to send */
242                                 err = ber_printf( ber, "{iti",  /* '}' */
243                                         i,
244                                         LDAP_REQ_ABANDON, msgid );
245                         }
246
247                         if ( err == -1 ) {
248                                 /* encoding error */
249                                 ld->ld_errno = LDAP_ENCODING_ERROR;
250
251                         } else {
252                                 /* Put Server Controls */
253                                 if ( ldap_int_put_controls( ld, sctrls, ber )
254                                         != LDAP_SUCCESS )
255                                 {
256                                         err = -1;
257
258                                 } else {
259                                         /* close '{' */
260                                         err = ber_printf( ber, /*{*/ "N}" );
261
262                                         if ( err == -1 ) {
263                                                 /* encoding error */
264                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
265                                         }
266                                 }
267                         }
268
269                         if ( err == -1 ) {
270                                 ber_free( ber, 1 );
271
272                         } else {
273                                 /* send the message */
274                                 if ( lr != NULL ) {
275                                         assert( lr->lr_conn != NULL );
276                                         sb = lr->lr_conn->lconn_sb;
277                                 } else {
278                                         sb = ld->ld_sb;
279                                 }
280
281                                 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
282                                         ld->ld_errno = LDAP_SERVER_DOWN;
283                                         err = -1;
284                                 } else {
285                                         err = 0;
286                                 }
287                         }
288                 }
289         }
290
291         if ( lr != NULL ) {
292                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
293                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
294                 }
295
296 #if 0
297                 /* FIXME: this is needed so that restarting
298                  * the initial search for lr doesn't result
299                  * in an endless loop */
300                 if ( origid == msgid )
301 #endif
302                 {
303                         ldap_free_request( ld, lr );
304                 }
305         }
306
307 #ifdef LDAP_R_COMPILE
308         /* ld_abandoned is actually protected by the ld_res_mutex;
309          * give up the ld_req_mutex and get the other */
310         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
311         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
312 #endif
313
314         /* use bisection */
315         i = 0;
316         if ( ld->ld_abandoned != NULL ) {
317                 int             begin,
318                                 end;
319
320                 assert( ld->ld_nabandoned >= 0 );
321
322                 begin = 0;
323                 end = ld->ld_nabandoned - 1;
324
325                 if ( ld->ld_nabandoned == 0 || ld->ld_abandoned[ begin ] > msgid ) {
326                         i = 0;
327
328                 } else if ( ld->ld_abandoned[ end ] < msgid ) {
329                         i = ld->ld_nabandoned;
330
331                 } else {
332                         int     pos, curid;
333
334                         while ( end >= begin ) {
335                                 pos = (begin + end)/2;
336                                 curid = ld->ld_abandoned[ pos ];
337
338                                 if ( msgid < curid ) {
339                                         end = pos - 1;
340
341                                 } else if ( msgid > curid ) {
342                                         begin = pos + 1;
343
344                                 } else {
345                                         /* already abandoned? */
346                                         i = -1;
347                                         break;
348                                 }
349                         }
350
351                         if ( i == 0 ) {
352                                 i = pos;
353                         }
354                 }
355         }
356
357         if ( i != -1 ) {
358                 int     pos = i;
359
360                 old_abandon = ld->ld_abandoned;
361
362                 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)ld->ld_abandoned,
363                         ( ld->ld_nabandoned + 1 ) * sizeof( ber_int_t ) );
364
365                 if ( ld->ld_abandoned == NULL ) {
366                         ld->ld_abandoned = old_abandon;
367                         ld->ld_errno = LDAP_NO_MEMORY;
368                         goto done;
369                 }
370
371                 for ( i = ld->ld_nabandoned; i > pos; i-- ) {
372                         ld->ld_abandoned[ i ] = ld->ld_abandoned[ i - 1 ];
373                 }
374                 ld->ld_abandoned[ pos ] = msgid;
375                 ++ld->ld_nabandoned;
376         }
377
378         if ( err != -1 ) {
379                 ld->ld_errno = LDAP_SUCCESS;
380         }
381
382 done:;
383 #ifdef LDAP_R_COMPILE
384         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
385         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
386 #endif
387         return( ld->ld_errno );
388 }