Calling Perl module from C coredumps on connection to MySQL database
- From: "rr" <perrfekto@xxxxxxxxx>
- Date: 27 Dec 2006 23:34:10 -0800
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
.
- Prev by Date: Type ulong not defined on MacOS X 10.4.8 for DBD::MySQL 4.00
- Next by Date: Multiple Tables
- Previous by thread: Type ulong not defined on MacOS X 10.4.8 for DBD::MySQL 4.00
- Next by thread: Multiple Tables
- Index(es):
Relevant Pages
|
|