Calling Perl module from C coredumps on connection to MySQL database



Good day

I've written a C program that calls a perl script. Both the C program
and the perl script needs to connect to the MySQL database. However,
when the perl script tries to connect to the database, the program
coredumps.

I've tried to compile both the DBI and the C program with the static
libmysqlclient library, but this had no effect.
Any suggestions and help will be greatly appreciated.

I am using perl 5.8.7 on a Ubuntu 6.06 DapperDrake linux box.
The MySql version is 5.0.22-Debian_0ubuntu6.06.2.
The DBI tarball that were used for the installation is
DBD-mysql-3.0006.tar.gz.

When I run the perl script on its own, I have no problems. I found that
taking the connect statement out prevents the C program from dumping.

I include the source of the C program as well as the perl script.

a.pl
#!/usr/bin/perl

use strict;
use warnings;

use DBI;

#-------------------------------------------------------------[
aPerlSub ]
# Mail routine to check DBI
#
# Input :
# database name
# db user
# db pwd
#
# Return :
# a string value
#-----------------------------------------------------------------------------

sub bmSendBatch
{
my ($database, $username, $pwd, $mailRunId, $batchSize, $smtp) = @_;
my $dbh;
my $sth;
my $rc;
my $sVal;

# Connect to the database
my $connStr = "DBI:mysql:" . $database;
eval { $dbh = DBI->connect ($connStr, $username, $pwd); };
if ($@) {
return (1);
}

if (!$dbh) {
return (1);
}

eval { $sth = $dbh->prepare( q{SELECT smtp FROM bmAccount}); };
if ($@) {
return (1);
}

if (!$sth) {
return (1);
}

eval { $rc = $sth->execute; };
if ($@) {
return (1);
}


if (!$rc) {
return (1);
}

($sVal) = $sth->fetchrow_array;
$sth->finish;

$dbh->disconnect;

return (0);
}

my $sVal = &aPerlSub ("itpBulkMail", "bulkMailService",
"1tpBulkMailS3rvic3");
print "$sVal\n";

=============================================================


a.c:
#include <mysql/mysql.h>
#include <mysql/errmsg.h>

#include <EXTERN.h>
#include <perl.h>

static PerlInterpreter *my_perl;
static void xs_init (pTHX);

EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void boot_MIMELite (pTHX_ CV* cv);

EXTERN_C void xs_init(pTHX)
{
char *file = __FILE__;

/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}



/*---------------------------------------------------------------[
CallPerl ]
Call the perl subroutine^[
----------------------------------------------------------------------------*/
static int CallPerl (
char * host,
char * userName,
char * pwd,
char * dbName)

{
int perlret; /* Perl return value - should be 1
*/
MYSQL * myPtr; /* Connection to MySQL
*/
int argc = 1;
char * argv[] = {"a"};
char * send_args[] = {"", "a.pl"};
int sVal;


perlret = 0;
sVal = 0;

myPtr = (MYSQL *) NULL;

if (mysql_library_init (0, (char **) NULL, (char **) NULL) != 0) {
fprintf (stderr, "BM: MySQL Error - Can't init MySQL library\n");

return 1;
}

myPtr = mysql_init ((MYSQL *) NULL);
if (myPtr == (MYSQL *) NULL) {
fprintf (stderr, "BM: MySQL Error - Can't init MySQL
connection\n");

return 1;
}

if (mysql_real_connect (myPtr, host, userName, pwd, dbName,
0, (char *) NULL, CLIENT_MULTI_STATEMENTS) == (MYSQL *) NULL) {
return 1;
}

mysql_close (myPtr);
myPtr = (MYSQL *) NULL;

mysql_library_end ();

/* Call Perl routine bmSendBatch */
PERL_SYS_INIT3 (&argc, &argv, NULL);
my_perl = perl_alloc ();
perl_construct (my_perl);

perl_parse (my_perl, xs_init, 2, send_args, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;

dSP;

ENTER;
SAVETMPS;

PUSHMARK (SP);
XPUSHs (sv_2mortal (newSVpv ("myDatabase", 0)));
XPUSHs (sv_2mortal (newSVpv ("myLogin", 0)));
XPUSHs (sv_2mortal (newSVpv ("myPassword", 0)));
XPUSHs (sv_2mortal (newSVuv (1)));
XPUSHs (sv_2mortal (newSViv (2)));
XPUSHs (sv_2mortal (newSVpv ("asd", 0)));

PUTBACK;

fprintf (stderr, "1\n");
fflush (stderr);

perlret = call_pv ("bmSendBatch", G_SCALAR);
fprintf (stderr, "2\n");
fflush (stderr);


SPAGAIN;

if (perlret != 1) {
fprintf (stderr, "\nPerl function aPerlSub did NOT return the
correct number of values!!\n");
fflush (stderr);

return 1;
}

sVal = POPi;
fprintf (stdout, "Returned %d\n", sVal);

PUTBACK;
FREETMPS;
LEAVE;

perl_destruct (my_perl);
perl_free (my_perl);
PERL_SYS_TERM ();

return 0;
}



int main (int argc, char **argv, char **env)
{
if (argc < 5) {
printf ("Use : a <host> <user> <pwd> <db>\n");
return 1;
}

return CallPerl (argv[1], argv[2], argv[3], argv[4]);
}

========================================================================

Thanks in advance
RR

.



Relevant Pages

  • Re: MySQL time
    ... Yes I know that I can benchmark the perl script using but I am ... Or that time shown by mysql client is not returned by MySQL server but by ... > it is exactly that you need (elapsed time to execute a query? ...
    (perl.beginners)
  • Re: Is there free SQL or Oracle servers for FC4?
    ... > I need t write perl script to work with one of the subject-servers. ... > will work with SQL? ... though the DBD module that is appropriate for the target database. ... use MySQL or PostgreSQL but there are DBD modules for about everything. ...
    (Fedora)
  • setup automatic init services
    ... I use apache+mysql for a website. ... I want to setup autonomous init for the following services: ... some perl script to update IP/DNS ... how to properly setup the init sequence for mysql ...
    (comp.os.linux.misc)
  • Re: Using MySQL with Perl
    ... > I'm having a problem running a perl script that uses mySQL. ... > installed both DBI and DBD-mysql using ppm, but when I run a script ...
    (comp.lang.perl.misc)
  • Re: Parsing XML and storing attributes in MySQL using Perl
    ... cooordinates chrom, start, and strand. ... perl script to pull this data out and put it in MySQL, ... MySQL, ...
    (comp.lang.perl.misc)