I use opendbx -1.4.0 with backend oracle ,an example function like :

vchar_t *KMC_database_singlecert_query(char *queryname,char *fingerprint)
{
        vchar_t *ret=NULL;
        char *tmp=NULL;
        unsigned int tmplen=0,len=0;
        unsigned int column=0;
        int err=0;
        int i=0;
        odbx_result_t *result=NULL;
        bool r=false;
        vchar_t *sql=NULL;
        BIO *bio=NULL;
        odbx_t *handle=NULL;
        char *tp=NULL;
        //assert((queryname!=NULL)&&(fingerprint!=NULL));
        err=KMC_database_bind(&handle);
        KMC_WARN_IF(err!=true,goto end,"can't bind database\n");
        bio=BIO_new(BIO_s_mem());
        if(bio==NULL)
                goto end;
        /** FIXME BIO_printf function NULL pointer leak */

len=BIO_printf(bio,KMC_DATABASE_CERTIFICATE_FOR_SINGLECERT_QUERY,queryname,fingerprint);
        KMC_WARN_IF(len<=0,goto end,"\n");
        err=KMC_bio_to_vbuf(bio,&sql);
        KMC_WARN_IF(err==false,goto end,"\n");
        err=KMC_database_query(handle,sql->v,&result);
        KMC_WARN_IF(err!=ODBX_RES_DONE&&err!=ODBX_RES_ROWS,goto end,"\n");
        column=odbx_column_count(result);
        KMC_WARN_IF(column!=1,goto end,"The fingerprint isn't primary
key\n");
        while((err=odbx_row_fetch(result))!=ODBX_ROW_DONE)
        {
                KMC_WARN_IF(err<0,goto
end,"odbx_row_fetch():%s\n",odbx_error(handle,err));
                tp=KMC_toupper(odbx_column_name(result,i));
                KMC_WARN_IF(NULL==tp,goto end,"toupper failed\n");

--
if(strcmp(odbx_column_name(result,i),queryname)==0||strcmp(tp,queryname)==0)
                {
                        tmplen=odbx_field_length(result,i);
                        if(tmplen==0)
                                goto end;
                        tmp=(char *)odbx_field_value(result,i);
                        if(tmp==NULL)
                                goto end;
                        ret=vcalloc(tmplen);
                        memcpy(ret->v,tmp,tmplen);
                }
                if(tp){
                        free(tp);
                        tp=NULL;
                }
        }
        err=true;
end:
        if(tp){
                free(tp);
                tp=NULL;
        }
        KMC_FREEX(V,sql);
        KMC_FREEX(BIO,bio);
        if(result&&ret>=0)
                odbx_result_free(result);
        if(handle)
        {
                KMC_database_unbind(handle);
                handle=NULL;
        }
        if(err!=true)
                KMC_FREEX(V,ret);
        return ret;
}

the function KMC_database_bind :

int KMC_database_bind(odbx_t **handle)
{
        int   err = 0;
        char *backend, *host, *port, *db, *user, *pass ;
        int encrypt=false;
        bool ret=false;
        int i=0;
        backend = host = port = db = user = pass = NULL;
        //assert(dbe!=NULL);
        backend=g_lc->db_backend;
        host=g_lc->db_host;
        port=g_lc->db_port;
        db=g_lc->db_name;
        user=g_lc->db_user;
        pass=g_lc->db_passwd;
        encrypt=g_lc->dbencrypt;
        if( backend == NULL ) {  return false; }

        if( ( err = odbx_init( handle, backend, host, port ) ) < 0 )
        {
                p_log( LLV_ERR, "Error in odbx_init(): %s\n", odbx_error(
*handle, err ) );
                goto end;
        }
        for(i=0;i<CAPMAX;i++)
        {
                if( ( err = odbx_capabilities( *handle, (unsigned
int)cap[i].value ) ) < 0 )
                {
                        p_log(LLV_ERR, "Error in odbx_capabilities(): %s\n",
odbx_error( *handle, err ) );
                        goto end;
                }
                cap[i].result=err;
                p_log(LLV_DEBUG2,"      %s:
%d\n",cap[i].name,cap[i].result);
        }
        for(i=0;i<OPTMAX;i++)
        {
                if((err=odbx_get_option(*handle,opt[i].value,(void
*)&(opt[i].result)))<0)
                {
                        p_log(LLV_ERR,"Error in odbx_get_option():
%s\n",odbx_error(*handle,err));
                        goto end;
                }
                p_log(LLV_DEBUG2,"      %s: %d
%s\n",opt[i].name,opt[i].result,opt[i].result&&opt[i].tryit?"(using)":"");

if(opt[i].result&&opt[i].tryit&&(err=odbx_set_option(*handle,opt[i].value,(void
*)&(opt[i].tryit)))<0)
                {
                        p_log(LLV_ERR,"Error in odbx_set_option():
%s\n",odbx_error(*handle,err));
                        goto end;
                }
        }
        if( encrypt && ( err = odbx_set_option( *handle, ODBX_OPT_TLS,
(void*) &encrypt ) ) < 0 )
        {
                p_log(LLV_DEBUG, "Error in odbx_set_option(): %s\n",
odbx_error( *handle, err ) );
                goto end;
        }
        if( ( err = odbx_bind_simple( *handle, db, user, pass ) ) < 0 )
        {
                p_log(LLV_ERR, "Error in odbx_bind(): %s\n", odbx_error(
*handle, err ) );
                goto end;
        }
        err=true;
        return err;
end:
        return err;
}

the function KMC_database_unbind
int KMC_database_unbind(odbx_t *handle)
{
        int err=0;
        //assert(handle!=NULL);
        if( ( err = odbx_unbind( handle ) ) < 0 )
        {
                p_log(LLV_ERR,"Error in odbx_unbind(): %s\n", odbx_error(
handle, err ) );
                goto end;
        }
        if( ( err = odbx_finish( handle ) ) < 0 )
        {
                p_log(LLV_ERR,"Error in odbx_finish(): %s\n", odbx_error(
handle, err ) );
                goto end;
        }
end:
        return -err;
}


the function KMC_database_query
static int KMC_database_query(odbx_t* handle,char *query,odbx_result_t
**result )
{
        int err = 0;
        struct timeval tv;
        struct database_lock *b=NULL;

        //assert((handle!=NULL)&&(query!=NULL));
        locked=1;
        timeout_ref=0;
        if( ( err = odbx_capabilities( handle, ODBX_CAP_LO ) ) < 0 )
        {
                p_log(LLV_ERR,"Error in odbx_capabilities(): %s\n",
odbx_error( handle, err ) );
                goto end;
        }
        if ( query != NULL )
        {
                p_log(LLV_DEBUG2,"  odbx_query:(%s)  '%d'\n",
query,strlen(query) );
                if( ( err = odbx_query( handle, query, strlen(query) ) ) < 0
)
                {
                        p_log(LLV_ERR,"Error in odbx_query(): %s\n",
odbx_error( handle, err ) );
                        if(-err==ODBX_RES_TIMEOUT)
                        {
                                ref++;
                                if(ref>=KMC_DATABASE_TIMEOUT_NUMBER)
                                {
                                        p_log(LLV_ERR,"Database connect
failed,check database services\n");
                                        goto end;
                                }
                                else
                                {
                                        KMC_database_unbind(handle);
                                        /** new handle */
                                        err=KMC_database_bind(&handle);
                                        KMC_WARN_IF(err!=true,goto end,"Bind
database failed\n");

err=KMC_database_thread_query(handle,query,ref,result);
                                        goto end;               ;
                                }
                                }
                        }
                        else
                                if( odbx_error_type( handle, err ) < 0 )
{goto end; }
                }
                tv.tv_sec = 3;
                tv.tv_usec = 0;
                /* use values >1 for paged output (if supported) or 0 for
all rows */
                if( ( err = odbx_result( handle, result, &tv, 0 ) ) !=
ODBX_RES_DONE )
                {
                        p_log(LLV_DEBUG2,"  odbx_result()\n" );
                        tv.tv_sec = 3;
                        tv.tv_usec = 0;
                        if( err < 0 )
                        {
                                p_log( LLV_ERR,"Error in odbx_result():
%s\n", odbx_error( handle, err ) );
                                if( odbx_error_type( handle, err ) < 0 ) {
goto end; }
                        }
                        switch( err )
                        {
                                case ODBX_RES_TIMEOUT:
                                        p_log(LLV_ERR, "    odbx_result():
Timeout\n" );
                                        break;
                                case ODBX_RES_NOROWS:
                                        p_log(LLV_DEBUG,"db  success
Affected rows: %lld\n", odbx_rows_affected( *result ) );
                        }
                }
        }
end:
        ref=0;
        locked=0;
        return err;
}


I use valgrind test memleak,the definitely lost's log file :
==32453== 43 bytes in 1 blocks are definitely lost in loss record 124 of 284
==32453==    at 0x401D38B: malloc (vg_replace_malloc.c:149)
==32453==    by 0x60D6471: slzsetevar (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60DA05E: lfvSetOHome (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60F2E32: slpmloclfv (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60F2FBA: slpmloc (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60F091B: lpmloadpkg (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60D9E38: lfvLoadPkg (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60D9B3E: lfvSetShlMode (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60D9146: lfvini1 (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x60D8EBD: lfvinit (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5EAE53B: kpummpin (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x57B7697: kpuenvcr (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)

==32453== 156 (36 direct, 120 indirect) bytes in 1 blocks are definitely
lost in loss record 212 of 284
==32453==    at 0x401D38B: malloc (vg_replace_malloc.c:149)
==32453==    by 0x43D0A09: (within /lib/tls/i686/cmov/libc-2.3.6.so)
==32453==    by 0x43D10F6: __nss_database_lookup (in /lib/tls/i686/cmov/
libc-2.3.6.so)
==32453==    by 0x66FA149: ???
==32453==    by 0x66FB28C: ???
==32453==    by 0x4380A94: getpwuid_r (in /lib/tls/i686/cmov/libc-2.3.6.so)
==32453==    by 0x5AD735F: snlpcgetpwuid_r (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5A81DDD: snigun (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5A142EE: niqnamcd (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5A14877: niqname (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5A16236: osncon (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x576873F: kpuadef (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)

==32453== 2,940 (856 direct, 2,084 indirect) bytes in 1 blocks are
definitely lost in loss record 271 of 284
==32453==    at 0x401D38B: malloc (vg_replace_malloc.c:149)
==32453==    by 0x5ACAD64: nlpains (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x59D207D: nlstdipi (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x59CF401: nlstdggo (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x59CF13B: nlstdgg (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5A13E38: nigini1 (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5A161FD: osncon (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x576873F: kpuadef (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5881E08: upiini (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5862D13: upiah0 (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x5767FA5: kpuatch (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)
==32453==    by 0x58855BC: OCIServerAttach (in
/home/test/kmc_setup/ORACLE_10/libclntsh.so.10.1)

Is there any problem with my program or opendbx library?
Best regards,

Tongyi ,Zhao
------------------------------------------------------------------------------

_______________________________________________
libopendbx-devel mailing list
libopendbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libopendbx-devel
http://www.linuxnetworks.de/doc/index.php/OpenDBX

Reply via email to