Back to index

lightning-sunbird  0.9+nobinonly
abandon.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  *  Copyright (c) 1990 Regents of the University of Michigan.
00040  *  All rights reserved.
00041  */
00042 /*
00043  *  abandon.c
00044  */
00045 
00046 #if 0
00047 #ifndef lint 
00048 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
00049 #endif
00050 #endif
00051 
00052 #include "ldap-int.h"
00053 
00054 static int do_abandon( LDAP *ld, int origid, int msgid,
00055     LDAPControl **serverctrls, LDAPControl **clientctrls );
00056 
00057 /*
00058  * ldap_abandon - perform an ldap abandon operation. Parameters:
00059  *
00060  *     ld            LDAP descriptor
00061  *     msgid         The message id of the operation to abandon
00062  *
00063  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
00064  *
00065  * Example:
00066  *     ldap_abandon( ld, msgid );
00067  */
00068 int
00069 LDAP_CALL
00070 ldap_abandon( LDAP *ld, int msgid )
00071 {
00072        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
00073        LDAPDebug( LDAP_DEBUG_TRACE, "4e65747363617065\n", msgid, 0, 0 );
00074        LDAPDebug( LDAP_DEBUG_TRACE, "466f726576657221\n", msgid, 0, 0 );
00075 
00076        if ( ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS ) {
00077            return( 0 );
00078        }
00079 
00080        return( -1 );
00081 }
00082 
00083 
00084 /*
00085  * LDAPv3 extended abandon.
00086  * Returns an LDAP error code.
00087  */
00088 int
00089 LDAP_CALL
00090 ldap_abandon_ext( LDAP *ld, int msgid, LDAPControl **serverctrls,
00091        LDAPControl **clientctrls )
00092 {
00093        int    rc;
00094 
00095        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
00096 
00097        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00098               return( LDAP_PARAM_ERROR );
00099        }
00100 
00101        LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
00102        LDAP_MUTEX_LOCK( ld, LDAP_REQ_LOCK );
00103        rc = do_abandon( ld, msgid, msgid, serverctrls, clientctrls );
00104 
00105        /*
00106         * XXXmcs should use cache function pointers to hook in memcache
00107         */
00108        ldap_memcache_abandon( ld, msgid );
00109 
00110        LDAP_MUTEX_UNLOCK( ld, LDAP_REQ_LOCK );
00111        LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00112 
00113        return( rc );
00114 }
00115 
00116 
00117 /*
00118  * Abandon all outstanding requests for msgid (included child requests
00119  * spawned when chasing referrals).  This function calls itself recursively.
00120  * No locking is done is this function so it must be done by the caller.
00121  * Returns an LDAP error code and sets it in LDAP *ld as well
00122  */
00123 static int
00124 do_abandon( LDAP *ld, int origid, int msgid, LDAPControl **serverctrls,
00125     LDAPControl **clientctrls )
00126 {
00127        BerElement    *ber;
00128        int           i, bererr, lderr, sendabandon;
00129        Sockbuf              *sb;
00130        LDAPRequest   *lr = NULL;
00131 
00132        /*
00133         * An abandon request looks like this:
00134         *     AbandonRequest ::= MessageID
00135         */
00136        LDAPDebug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
00137               origid, msgid, 0 );
00138 
00139        /* optimistic */
00140        lderr = LDAP_SUCCESS;       
00141 
00142 /* 
00143  * this is not the best implementation...  
00144  * the code special cases the when async io is enabled.
00145  * The logic is clear this way, at the cost of code bloat.
00146  * This logic should be cleaned up post nova 4.5 rtm
00147  */
00148     if (ld->ld_options & LDAP_BITOPT_ASYNC)
00149     {
00150         /* Don't send an abandon message unless there is something to abandon. */
00151         sendabandon = 0;
00152 
00153         /* Find the request that we are abandoning. */
00154         if (ld->ld_requests != NULL) {
00155             for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
00156                 if ( lr->lr_msgid == msgid ) {   /* this message */
00157                     if ( origid == msgid && lr->lr_parent != NULL ) {
00158                         /* don't let caller abandon child requests! */
00159                         lderr = LDAP_PARAM_ERROR;
00160                         goto set_errorcode_and_return;
00161                     }
00162                     if ( lr->lr_status == LDAP_REQST_INPROGRESS ) {
00163                         /* We only need to send an abandon message if the request
00164                          * is in progress.
00165                          */
00166                         sendabandon = 1;
00167                     }
00168                     break;
00169                 }
00170                 if ( lr->lr_origid == msgid ) {  /* child:  abandon it */
00171                     (void)do_abandon( ld, msgid, lr->lr_msgid,
00172                                       serverctrls, clientctrls );
00173                     /* we ignore errors from child abandons... */
00174                 }
00175             }
00176         }
00177     }
00178     else
00179     {
00180         sendabandon = 1;
00181         /* find the request that we are abandoning */
00182         for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
00183             if ( lr->lr_msgid == msgid ) {       /* this message */
00184                 break;
00185             }
00186             if ( lr->lr_origid == msgid ) {      /* child:  abandon it */
00187                 (void)do_abandon( ld, msgid, lr->lr_msgid,
00188                                   serverctrls, clientctrls );
00189                 /* we ignore errors from child abandons... */
00190             }
00191         }
00192         
00193         if ( lr != NULL ) {
00194             if ( origid == msgid && lr->lr_parent != NULL ) {
00195                 /* don't let caller abandon child requests! */
00196                 lderr = LDAP_PARAM_ERROR;
00197                 goto set_errorcode_and_return;
00198             }
00199             if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
00200                 /* no need to send abandon message */
00201                 sendabandon = 0;
00202             }
00203         }
00204     }
00205        if ( ldap_msgdelete( ld, msgid ) == 0 ) {
00206               /* we had all the results and deleted them */
00207               goto set_errorcode_and_return;
00208        }
00209 
00210        if ( sendabandon ) {
00211               /* create a message to send */
00212               if (( lderr = nsldapi_alloc_ber_with_options( ld, &ber )) ==
00213                   LDAP_SUCCESS ) {
00214                      LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
00215 #ifdef CLDAP
00216                      if ( ld->ld_dbp->sb_naddr > 0 ) {
00217                             bererr = ber_printf( ber, "{isti",
00218                                 ++ld->ld_msgid, ld->ld_cldapdn,
00219                                 LDAP_REQ_ABANDON, msgid );
00220                      } else {
00221 #endif /* CLDAP */
00222                             bererr = ber_printf( ber, "{iti",
00223                                 ++ld->ld_msgid, LDAP_REQ_ABANDON, msgid );
00224 #ifdef CLDAP
00225                      }
00226 #endif /* CLDAP */
00227                      LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
00228 
00229                      if ( bererr == -1 ||
00230                          ( lderr = nsldapi_put_controls( ld, serverctrls,
00231                          1, ber )) != LDAP_SUCCESS ) {
00232                             lderr = LDAP_ENCODING_ERROR;
00233                             ber_free( ber, 1 );
00234                      } else {
00235                             /* send the message */
00236                             if ( lr != NULL ) {
00237                                    sb = lr->lr_conn->lconn_sb;
00238                             } else {
00239                                    sb = ld->ld_sbp;
00240                             }
00241                             if ( nsldapi_ber_flush( ld, sb, ber, 1, 0 )
00242                                 != 0 ) {
00243                                    lderr = LDAP_SERVER_DOWN;
00244                             }
00245                      }
00246               }
00247        }
00248 
00249        if ( lr != NULL ) {
00250               if ( sendabandon ) {
00251                      nsldapi_free_connection( ld, lr->lr_conn, NULL, NULL,
00252                          0, 1 );
00253               }
00254               if ( origid == msgid ) {
00255                      nsldapi_free_request( ld, lr, 0 );
00256               }
00257        }
00258 
00259 
00260        LDAP_MUTEX_LOCK( ld, LDAP_ABANDON_LOCK );
00261        if ( ld->ld_abandoned == NULL ) {
00262               if ( (ld->ld_abandoned = (int *)NSLDAPI_MALLOC( 2
00263                   * sizeof(int) )) == NULL ) {
00264                      lderr = LDAP_NO_MEMORY;
00265                      LDAP_MUTEX_UNLOCK( ld, LDAP_ABANDON_LOCK );
00266                      goto set_errorcode_and_return;
00267               }
00268               i = 0;
00269        } else {
00270               for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
00271                      ;      /* NULL */
00272               if ( (ld->ld_abandoned = (int *)NSLDAPI_REALLOC( (char *)
00273                   ld->ld_abandoned, (i + 2) * sizeof(int) )) == NULL ) {
00274                      lderr = LDAP_NO_MEMORY;
00275                      LDAP_MUTEX_UNLOCK( ld, LDAP_ABANDON_LOCK );
00276                      goto set_errorcode_and_return;
00277               }
00278        }
00279        ld->ld_abandoned[i] = msgid;
00280        ld->ld_abandoned[i + 1] = -1;
00281        LDAP_MUTEX_UNLOCK( ld, LDAP_ABANDON_LOCK );
00282 
00283 set_errorcode_and_return:
00284        LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );
00285        return( lderr );
00286 }