-=| Alex Peshkoff, 20.09.2011 14:17:54 +0400 |=-
>  On 09/20/11 14:08, Alex Peshkoff wrote:
> >  On 09/20/11 12:59, Damyan Ivanov wrote:
> >> perl alarm.pl db.fdb
> > Well, now I see
> > 20665817
> > on the screen. Must say that this is DEV_BUILD. I will try with release
> > one now.
> 
> With release build I confirm segfault.

Thanks!

Attached is a simple C program I wrote as an exercise which also 
segfaults. Sometimes after the first signal delivery, sometimes I have 
to wait several seconds.

This eliminates any side effects Perl of IBPerl may have and makes it 
a bit easier to debug, I hope.

Build with 'cc -Wall -lfbclient -o alarm alarm.c', run with './alarm 
database.fdb'.
#include "stdio.h"
#include "stdlib.h"
#include "ibase.h"
#include "string.h"
#include "signal.h"
#include "unistd.h"
#include "errno.h"

void errorcheck(const ISC_STATUS *status) {
    if( status[0] == 1 && status[1] ) {
        char buf[512] = "";
        fprintf( stderr, "Firebird Error:\n" );
        while ( fb_interpret(buf, sizeof(buf), &(status) ) ) {
            fprintf( stderr, "  %s\n", buf );
        }

        exit(1);
    }
}

void verbose(const char *msg) {
    if (1) {
        fprintf( stderr, "V: %s\n", msg );
        fflush(stderr);
    }
}

int flag;

void alrm(int signal) {
    flag = 1;
    alarm(1);
}

int main(int argc, char** argv) {
        isc_db_handle   db = 0;
        isc_tr_handle   tr = 0;
        isc_stmt_handle st = 0;
        ISC_STATUS      status[100];
        XSQLDA          *osqlda;
        int             sqlda_size;
        int             num_fields;
        short           *sqlind;
        int             i;
        XSQLVAR         *var;
        int             counter = 0;
        struct sigaction   act;

    if ( argc != 2 ) {
            fprintf( stderr, "Usage: alarm <database>\n" );
            return(1);
    }

    verbose("Connecting");
    isc_attach_database( status, 0, argv[1], &db, 0, NULL );
    errorcheck(status);

    verbose("Starting transaction");
    isc_start_transaction( status, &tr, 1, &db, 0, NULL );
    errorcheck(status);

    verbose("Allocating statement");
    isc_dsql_allocate_statement( status, &db, &st );
    errorcheck(status);

    osqlda = malloc(sizeof(XSQLDA));
    memset(osqlda, '\0', sizeof(XSQLDA));
    osqlda->version = SQLDA_VERSION1;

    verbose("Prearing");
    isc_dsql_prepare( status, &tr, &st, 0, "SELECT 1 FROM rdb$database", 3, osqlda);
    errorcheck(status);

    verbose("Describing");
    isc_dsql_describe(status, &st, 3, osqlda);
    errorcheck(status);

    num_fields = osqlda->sqld;
    sqlda_size = XSQLDA_LENGTH(num_fields);
    osqlda = realloc(osqlda, sqlda_size);
    osqlda->sqln = osqlda->sqld;

    verbose("Re-describing");
    isc_dsql_describe(status, &st, 3, osqlda);
    errorcheck(status);

    sqlind = malloc(osqlda->sqld * sizeof(short));

    for( i = 0, var = osqlda->sqlvar; i < osqlda->sqld; i++, var++) {
        int buf_len;
        switch (var->sqltype & ~1) {
            case SQL_SHORT:
            case SQL_LONG:
                buf_len = var->sqllen;
                break;
            default:
                fprintf(stderr, "Unknown SQL type %d for output parameter %d\n", var->sqltype & ~1, i+1 );
                exit(2);
        }

        var->sqldata = malloc(buf_len);
        var->sqlind = &(sqlind[i]);
    }

    flag = 0;

    act.sa_handler = alrm;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    act.sa_restorer = NULL;

    if( sigaction(SIGALRM, &act, NULL) != 0 ) {
        fprintf(stderr, "Error %d setting alarm\n", errno);
        exit(errno);
    }

    alarm(1);

    while(1) {
        isc_dsql_execute( status, &tr, &st, 3, NULL );
        errorcheck(status);

        isc_dsql_fetch( status, &st, 3, osqlda );
        errorcheck(status);

        isc_dsql_free_statement(status, &st, DSQL_close);
        errorcheck(status);

        counter++;

        if (flag) {
            printf("\r%d", counter);
            fflush(stdout);
            flag = 0;
        }
    }

    // never reached

    alarm(0);

    verbose("Deallocating statement");
    isc_dsql_free_statement(status, &st, DSQL_drop);
    errorcheck(status);

    verbose("Committing transaction");
    isc_commit_transaction(status, &tr);
    errorcheck(status);

    verbose("Disconnecting");
    isc_detach_database(status, &db);
    errorcheck(status);

    return(0);
}
------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
Firebird-Devel mailing list, web interface at 
https://lists.sourceforge.net/lists/listinfo/firebird-devel

Reply via email to