]> git.sur5r.net Git - openldap/blobdiff - libraries/liblber/options.c
ITS#897 Internal connection that is closed on one end and about to
[openldap] / libraries / liblber / options.c
index dc61707dfd73d42b75259aa5f33695c8f12ead67..d234e98081e9383498d2f8f4d3a6df5547750edc 100644 (file)
@@ -7,10 +7,13 @@
 
 #include <ac/stdlib.h>
 #include <ac/string.h>
-
+#include <ac/stdarg.h>
 #include "lber-int.h"
 
-extern void * ber_pvt_err_file;        /* bprint.c */
+/* bprint.c */
+typedef void (*BER_LOG_FN) LDAP_P((FILE *file, char *subsys, int level, const char *fmt, va_list vl));
+extern void * ber_pvt_err_file;
+extern BER_LOG_FN ber_int_log_proc;
 
 struct lber_options ber_int_options = {
        LBER_UNINITIALIZED, 0, 0 };
@@ -36,6 +39,23 @@ ber_get_option(
                if(option == LBER_OPT_BER_DEBUG) {
                        * (int *) outvalue = ber_int_debug;
                        return LBER_OPT_SUCCESS;
+               } else if(option == LBER_OPT_MEMORY_INUSE) {
+                       /* The memory inuse is a global variable on kernal implementations.
+                        * This means that memory debug is shared by all LDAP processes
+                        * so for this variable to have much meaning, only one LDAP process
+                        * should be running and memory inuse should be initialized to zero
+                        * using the lber_set_option() function during startup.
+                        * The counter is not accurate for multithreaded ldap applications.
+                        */
+#ifdef LDAP_MEMORY_DEBUG
+                       * (int *) outvalue = ber_int_options.lbo_meminuse;
+                       return LBER_OPT_SUCCESS;
+#else
+                       return LBER_OPT_ERROR;
+#endif
+               } else if(option == LBER_OPT_LOG_PRINT_FILE) {
+                       *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
+                       return LBER_OPT_SUCCESS;
                }
 
                ber_errno = LBER_ERROR_PARAM;
@@ -57,15 +77,18 @@ ber_get_option(
                return LBER_OPT_SUCCESS;
 
        case LBER_OPT_BER_REMAINING_BYTES:
-               *((ber_len_t *) outvalue) = ber->ber_end - ber->ber_ptr;
+               assert( BER_VALID( ber ) );
+               *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
                return LBER_OPT_SUCCESS;
 
        case LBER_OPT_BER_TOTAL_BYTES:
-               *((ber_len_t *) outvalue) = ber->ber_end - ber->ber_buf;
+               assert( BER_VALID( ber ) );
+               *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
                return LBER_OPT_SUCCESS;
 
        case LBER_OPT_BER_BYTES_TO_WRITE:
-               *((ber_len_t *) outvalue) = ber->ber_ptr - ber->ber_buf;
+               assert( BER_VALID( ber ) );
+               *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
                return LBER_OPT_SUCCESS;
 
        default:
@@ -110,7 +133,7 @@ ber_set_option(
                        return LBER_OPT_ERROR;
                }
 
-               memcpy(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
+               AC_MEMCPY(ber_int_memory_fns, f, sizeof(BerMemoryFunctions));
 
                ber_int_options.lbo_valid = LBER_INITIALIZED;
                return LBER_OPT_SUCCESS;
@@ -135,6 +158,22 @@ ber_set_option(
                } else if(option == LBER_OPT_LOG_PRINT_FILE) {
                        ber_pvt_err_file = (void *) invalue;
                        return LBER_OPT_SUCCESS;
+               } else if(option == LBER_OPT_MEMORY_INUSE) {
+                       /* The memory inuse is a global variable on kernal implementations.
+                        * This means that memory debug is shared by all LDAP processes
+                        * so for this variable to have much meaning, only one LDAP process
+                        * should be running and memory inuse should be initialized to zero
+                        * using the lber_set_option() function during startup.
+                        * The counter is not accurate for multithreaded applications.
+                        */
+#ifdef LDAP_MEMORY_DEBUG
+                       ber_int_options.lbo_meminuse = * (int *) invalue;
+                       return LBER_OPT_SUCCESS;
+#else
+                       return LBER_OPT_ERROR;
+#endif
+               } else if(option == LBER_OPT_LOG_PROC) {
+                       ber_int_log_proc = (BER_LOG_FN)invalue;
                }
 
                ber_errno = LBER_ERROR_PARAM;
@@ -156,14 +195,17 @@ ber_set_option(
                return LBER_OPT_SUCCESS;
 
        case LBER_OPT_BER_REMAINING_BYTES:
+               assert( BER_VALID( ber ) );
                ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
                return LBER_OPT_SUCCESS;
 
        case LBER_OPT_BER_TOTAL_BYTES:
+               assert( BER_VALID( ber ) );
                ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
                return LBER_OPT_SUCCESS;
 
        case LBER_OPT_BER_BYTES_TO_WRITE:
+               assert( BER_VALID( ber ) );
                ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
                return LBER_OPT_SUCCESS;