~ubuntu-branches/ubuntu/maverick/openldap/maverick-proposed

« back to all changes in this revision

Viewing changes to libraries/libldap/result.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Gug
  • Date: 2009-02-18 18:44:00 UTC
  • mfrom: (0.1.1 upstream)
  • mto: (0.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20090218184400-xmj1e22xo7i50ar6
Tags: upstream-2.4.14
ImportĀ upstreamĀ versionĀ 2.4.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* result.c - wait for an ldap result */
2
 
/* $OpenLDAP: pkg/ldap/libraries/libldap/result.c,v 1.124.2.12 2008/07/09 23:16:48 quanah Exp $ */
 
2
/* $OpenLDAP: pkg/ldap/libraries/libldap/result.c,v 1.124.2.17 2009/02/10 23:42:16 quanah Exp $ */
3
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
4
 *
5
 
 * Copyright 1998-2008 The OpenLDAP Foundation.
 
5
 * Copyright 1998-2009 The OpenLDAP Foundation.
6
6
 * All rights reserved.
7
7
 *
8
8
 * Redistribution and use in source and binary forms, with or without
71
71
static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
72
72
        LDAPMessage **result ));
73
73
static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
74
 
        int all, LDAPConn **lc, LDAPMessage **result ));
 
74
        int all, LDAPConn *lc, LDAPMessage **result ));
75
75
static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
76
76
static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
77
77
static LDAPMessage * chkResponseList LDAP_P(( LDAP *ld, int msgid, int all));
106
106
        struct timeval *timeout,
107
107
        LDAPMessage **result )
108
108
{
109
 
        LDAPMessage     *lm = NULL;
 
109
        LDAPMessage     *lm;
110
110
        int             rc;
111
111
 
112
112
        assert( ld != NULL );
118
118
        ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
119
119
#endif
120
120
 
121
 
#if 0
122
 
        /* this is already done inside wait4msg(), right?... */
123
 
        lm = chkResponseList( ld, msgid, all );
124
 
#endif
125
 
 
126
 
        if ( lm == NULL ) {
127
 
                rc = wait4msg( ld, msgid, all, timeout, result );
128
 
 
129
 
        } else {
130
 
                *result = lm;
131
 
                ld->ld_errno = LDAP_SUCCESS;
132
 
                rc = lm->lm_msgtype;
133
 
        }
 
121
        rc = wait4msg( ld, msgid, all, timeout, result );
134
122
 
135
123
#ifdef LDAP_R_COMPILE
136
124
        ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
335
323
                                if ( ber_sockbuf_ctrl( lc->lconn_sb,
336
324
                                        LBER_SB_OPT_DATA_READY, NULL ) )
337
325
                                {
338
 
#ifdef LDAP_R_COMPILE
339
 
                                        ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
340
 
#endif
341
 
                                        rc = try_read1msg( ld, msgid, all, &lc, result );
342
 
#ifdef LDAP_R_COMPILE
343
 
                                        ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
344
 
#endif
345
326
                                        lc_ready = 1;
346
327
                                        break;
347
328
                                }
375
356
                                        rc = LDAP_MSG_X_KEEP_LOOKING;   /* select interrupted: loop */
376
357
 
377
358
                                } else {
378
 
                                        rc = LDAP_MSG_X_KEEP_LOOKING;
379
 
#ifdef LDAP_R_COMPILE
380
 
                                        ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
381
 
#endif
382
 
                                        if ( ld->ld_requests &&
383
 
                                                ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
384
 
                                                ldap_is_write_ready( ld,
385
 
                                                        ld->ld_requests->lr_conn->lconn_sb ) )
386
 
                                        {
387
 
                                                ldap_int_flush_request( ld, ld->ld_requests );
388
 
                                        }
389
 
#ifdef LDAP_R_COMPILE
390
 
                                        ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
391
 
                                        ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
392
 
#endif
393
 
                                        for ( lc = ld->ld_conns;
394
 
                                                rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL; )
395
 
                                        {
396
 
                                                if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
397
 
                                                        ldap_is_read_ready( ld, lc->lconn_sb ) )
398
 
                                                {
399
 
#ifdef LDAP_R_COMPILE
400
 
                                                        ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
401
 
#endif
402
 
                                                        rc = try_read1msg( ld, msgid, all, &lc, result );
403
 
#ifdef LDAP_R_COMPILE
404
 
                                                        ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
405
 
#endif
406
 
                                                        if ( lc == NULL ) {
407
 
                                                                /* if lc gets free()'d,
408
 
                                                                 * there's no guarantee
409
 
                                                                 * lc->lconn_next is still
410
 
                                                                 * sane; better restart
411
 
                                                                 * (ITS#4405) */
412
 
                                                                lc = ld->ld_conns;
 
359
                                        lc_ready = 1;
 
360
                                }
 
361
                        }
 
362
                        if ( lc_ready ) {
 
363
                                LDAPConn *lnext;
 
364
                                rc = LDAP_MSG_X_KEEP_LOOKING;
 
365
#ifdef LDAP_R_COMPILE
 
366
                                ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
 
367
#endif
 
368
                                if ( ld->ld_requests &&
 
369
                                        ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
 
370
                                        ldap_is_write_ready( ld,
 
371
                                                ld->ld_requests->lr_conn->lconn_sb ) )
 
372
                                {
 
373
                                        ldap_int_flush_request( ld, ld->ld_requests );
 
374
                                }
 
375
#ifdef LDAP_R_COMPILE
 
376
                                ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
 
377
                                ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
 
378
#endif
 
379
                                for ( lc = ld->ld_conns;
 
380
                                        rc == LDAP_MSG_X_KEEP_LOOKING && lc != NULL;
 
381
                                        lc = lnext )
 
382
                                {
 
383
                                        if ( lc->lconn_status == LDAP_CONNST_CONNECTED &&
 
384
                                                ldap_is_read_ready( ld, lc->lconn_sb ) )
 
385
                                        {
 
386
                                                /* Don't let it get freed out from under us */
 
387
                                                ++lc->lconn_refcnt;
 
388
#ifdef LDAP_R_COMPILE
 
389
                                                ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
 
390
#endif
 
391
                                                rc = try_read1msg( ld, msgid, all, lc, result );
 
392
                                                lnext = lc->lconn_next;
413
393
 
414
 
                                                                /* don't get to next conn! */
415
 
                                                                break;
416
 
                                                        }
 
394
                                                /* Only take locks if we're really freeing */
 
395
                                                if ( lc->lconn_refcnt <= 1 ) {
 
396
#ifdef LDAP_R_COMPILE
 
397
                                                        ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
 
398
#endif
 
399
                                                        ldap_free_connection( ld, lc, 0, 1 );
 
400
#ifdef LDAP_R_COMPILE
 
401
                                                        ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
 
402
#endif
 
403
                                                } else {
 
404
                                                        --lc->lconn_refcnt;
417
405
                                                }
418
 
 
419
 
                                                /* next conn */
420
 
                                                lc = lc->lconn_next;
 
406
#ifdef LDAP_R_COMPILE
 
407
                                                ldap_pvt_thread_mutex_lock( &ld->ld_conn_mutex );
 
408
#endif
 
409
                                        } else {
 
410
                                                lnext = lc->lconn_next;
421
411
                                        }
422
 
#ifdef LDAP_R_COMPILE
423
 
                                        ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
424
 
#endif
425
412
                                }
 
413
#ifdef LDAP_R_COMPILE
 
414
                                ldap_pvt_thread_mutex_unlock( &ld->ld_conn_mutex );
 
415
#endif
426
416
                        }
427
417
                }
428
418
 
482
472
        LDAP *ld,
483
473
        ber_int_t msgid,
484
474
        int all,
485
 
        LDAPConn **lcp,
 
475
        LDAPConn *lc,
486
476
        LDAPMessage **result )
487
477
{
488
478
        BerElement      *ber;
493
483
        ber_len_t       len;
494
484
        int             foundit = 0;
495
485
        LDAPRequest     *lr, *tmplr, dummy_lr = { 0 };
496
 
        LDAPConn        *lc;
497
486
        BerElement      tmpber;
498
487
        int             rc, refer_cnt, hadref, simple_request, err;
499
488
        ber_int_t       lderr;
504
493
#endif
505
494
 
506
495
        assert( ld != NULL );
507
 
        assert( lcp != NULL );
508
 
        assert( *lcp != NULL );
 
496
        assert( lc != NULL );
509
497
        
510
498
#ifdef LDAP_R_COMPILE
511
499
        LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex );
514
502
        Debug( LDAP_DEBUG_TRACE, "read1msg: ld %p msgid %d all %d\n",
515
503
                (void *)ld, msgid, all );
516
504
 
517
 
        lc = *lcp;
518
 
 
519
505
retry:
520
506
        if ( lc->lconn_ber == NULL ) {
521
507
                lc->lconn_ber = ldap_alloc_ber_with_options( ld );
561
547
                if ( err == EAGAIN ) return LDAP_MSG_X_KEEP_LOOKING;
562
548
#endif
563
549
                ld->ld_errno = LDAP_SERVER_DOWN;
564
 
#ifdef LDAP_R_COMPILE
565
 
                ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
566
 
#endif
567
 
                ldap_free_connection( ld, lc, 1, 0 );
568
 
#ifdef LDAP_R_COMPILE
569
 
                ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
570
 
#endif
571
 
                lc = *lcp = NULL;
 
550
                --lc->lconn_refcnt;
 
551
                lc->lconn_status = 0;
572
552
                return -1;
573
553
 
574
554
        default:
648
628
                        ber_scanf(ber, "x{");
649
629
                }
650
630
nextresp2:
 
631
                ;
651
632
#endif
652
633
        }
653
634
 
937
918
                         * shouldn't necessarily end the connection
938
919
                         */
939
920
                        if ( lc != NULL && id != 0 ) {
940
 
#ifdef LDAP_R_COMPILE
941
 
                                ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
942
 
#endif
943
 
                                ldap_free_connection( ld, lc, 0, 1 );
944
 
#ifdef LDAP_R_COMPILE
945
 
                                ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
946
 
#endif
947
 
                                lc = *lcp = NULL;
 
921
                                --lc->lconn_refcnt;
 
922
                                lc = NULL;
948
923
                        }
949
924
                }
950
925
        }
1010
985
 
1011
986
                        /* get rid of the connection... */
1012
987
                        if ( lc != NULL ) {
1013
 
#ifdef LDAP_R_COMPILE
1014
 
                                ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
1015
 
#endif
1016
 
                                ldap_free_connection( ld, lc, 0, 1 );
1017
 
#ifdef LDAP_R_COMPILE
1018
 
                                ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
1019
 
#endif
1020
 
                                lc = *lcp = NULL;
 
988
                                --lc->lconn_refcnt;
1021
989
                        }
1022
990
 
1023
991
                        /* need to return -1, because otherwise
1126
1094
        if ( msgid == LDAP_RES_ANY || id == msgid ) {
1127
1095
                if ( all == LDAP_MSG_ONE
1128
1096
                        || ( newmsg->lm_msgtype != LDAP_RES_SEARCH_RESULT
1129
 
                                && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
 
1097
                                && newmsg->lm_msgtype != LDAP_RES_SEARCH_ENTRY
 
1098
                                && newmsg->lm_msgtype != LDAP_RES_INTERMEDIATE
1130
1099
                                && newmsg->lm_msgtype != LDAP_RES_SEARCH_REFERENCE ) )
1131
1100
                {
1132
1101
                        *result = newmsg;
1434
1403
 
1435
1404
        /* NOTE: those assertions are repeated in ldap_int_bisect_delete() */
1436
1405
        assert( idx >= 0 );
1437
 
        assert( idx < ld->ld_nabandoned );
 
1406
        assert( (unsigned) idx < ld->ld_nabandoned );
1438
1407
        assert( ld->ld_abandoned[ idx ] == msgid );
1439
1408
 
1440
1409
        return ldap_int_bisect_delete( &ld->ld_abandoned, &ld->ld_nabandoned,