<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Engwar &#187; mod_perl</title>
	<atom:link href="http://engwar.com/tags/mod_perl/feed" rel="self" type="application/rss+xml" />
	<link>http://engwar.com</link>
	<description>Chintana Wilamuna&#039;s weblog</description>
	<lastBuildDate>Sat, 03 Jul 2010 08:48:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Perl embedding woes</title>
		<link>http://engwar.com/post/40</link>
		<comments>http://engwar.com/post/40#comments</comments>
		<pubDate>Thu, 03 Jul 2008 14:33:55 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[mod_perl]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsf/c]]></category>
		<category><![CDATA[wsf/perl]]></category>

		<guid isPermaLink="false">http://engwar.com/?p=40</guid>
		<description><![CDATA[While developing server side components that enables providing Web services using Perl bumped into a brick wall. First the requirement is when a client request comes, need to extract the payload and pass it to WSF/C which acts as the underlying Web services engine. So WSF/C knows how to process the incoming XML payload and [...]]]></description>
			<content:encoded><![CDATA[<p>While developing server side components that enables providing Web services using Perl bumped into a brick wall.  First the requirement is when a client request comes, need to extract the payload and pass it to WSF/C which acts as the underlying Web services engine.  So WSF/C knows how to process the incoming XML payload and do what it does and return another XML payload which is then passed back to the client who send the request.  And the business logic is written using Perl.  Simple, I know.</p>
<p>We used SWIG to generate some wrapper functions (plus some custom written C code) which belongs to WSF/C and wrote a Perl module encapsulating the lower level stuff.  So when you have to write a Web service in Perl all you have to do is <code>use WSO2::WSF::Service;</code> in your script and call a couple of functions.  As the deployment scenario we choose Apache with ModPerl.  That combination seemd logical and some of the heavy trafficked sites are using that.</p>
<p>It gets interesting when you test the whole thing.  Apache is configured to run ModPerl on a folder named <code>/perl</code> so requests matching <code>/perl/something</code> will go through ModPerl.  <code>ResponseHandler</code> is <code>ModPerl::Registry</code> and also <code>GlobalRequest</code> has been enabled to get the global request via <code>Apache::RequestUtil->request()</code>.</p>
<p>Say, we have a service script named <code>echo_service.pl</code> which simply return back whatever it gets.  Here&#8217;s the code,</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">::</span><span style="color: #006600;">WSService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Apache2<span style="color: #339933;">::</span><span style="color: #006600;">RequestUtil</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$payload</span> <span style="color: #339933;">=</span><span style="color: #cc0000; font-style: italic;">&lt;&lt;E;
&lt;ns1:echoString xmlns:ns1=&quot;http://perl.axis2.org/samples&quot;&gt;
  &lt;text&gt;plurk u can haz&lt;/text&gt;
&lt;/ns1:echoString&gt;
E</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> echoFunction <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$arg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">::</span><span style="color: #006600;">WSMessage</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'payload'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$arg</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">return</span> <span style="color: #0000ff;">$message</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%operations</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">'echoString'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'echoFunction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">defined</span> <span style="color: #0000ff;">$ENV</span><span style="color: #009900;">&#123;</span>MOD_PERL<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$r</span> <span style="color: #339933;">=</span> Apache2<span style="color: #339933;">::</span><span style="color: #006600;">RequestUtil</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WSO2<span style="color: #339933;">::</span><span style="color: #006600;">WSF</span><span style="color: #339933;">::</span><span style="color: #006600;">WSService</span><span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'operations'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\%operations</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'testAction'</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #0000ff;">$service</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">reply</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$r</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When it goes to the reply method it&#8217;ll call up some C routines.  Inside one of those C functions, after determining the function name to call by looking at the request payload, we need to call it.  From a C function.  How can you do that?  By embedding the Perl interpreter.  AFAIK, you cannot call a Perl function from C without embedding the interpreter (please correct me if I&#8217;m wrong).  In WSF/Ruby server side there was no such issue because the entire Ruby module (<code>WSService</code> class and methods) is written in C.</p>
<p>Let me show you the code that embeds the interpreter and calls the function after looking at the XML payload in the request,</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> axiom_node_t <span style="color: #339933;">*</span>
wsf_xml_msg_recv_invoke_other<span style="color: #009900;">&#40;</span>axis2_msg_recv_t<span style="color: #339933;">*</span> msg_recv<span style="color: #339933;">,</span>
        <span style="color: #993333;">const</span> axutil_env_t<span style="color: #339933;">*</span> env<span style="color: #339933;">,</span>
        wsf_svc_info_t<span style="color: #339933;">*</span> svc_info<span style="color: #339933;">,</span>
        axis2_msg_ctx_t<span style="color: #339933;">*</span> in_msg_ctx<span style="color: #339933;">,</span>
        axis2_msg_ctx_t<span style="color: #339933;">*</span> out_msg_ctx<span style="color: #339933;">,</span>
        axis2_char_t<span style="color: #339933;">*</span> function_name<span style="color: #339933;">,</span>
        axis2_char_t<span style="color: #339933;">*</span> class_name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    AXIS2_PARAM_CHECK<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>error<span style="color: #339933;">,</span> svc_info<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AXIS2_PARAM_CHECK<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>error<span style="color: #339933;">,</span> in_msg_ctx<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AXIS2_PARAM_CHECK<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>error<span style="color: #339933;">,</span> out_msg_ctx<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    axiom_node_t <span style="color: #339933;">*</span>node <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_node_t <span style="color: #339933;">*</span>om_node <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_soap_envelope_t <span style="color: #339933;">*</span>envelope <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_soap_body_t <span style="color: #339933;">*</span>body <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axis2_char_t <span style="color: #339933;">*</span>retstr <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axiom_node_t <span style="color: #339933;">*</span>soap_body_node <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* extracting payload from the soap message */</span>
    envelope <span style="color: #339933;">=</span> axis2_msg_ctx_get_soap_envelope<span style="color: #009900;">&#40;</span>in_msg_ctx<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>envelope<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;[wsf-perl-service] soap envelope not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    body <span style="color: #339933;">=</span> axiom_soap_envelope_get_body<span style="color: #009900;">&#40;</span>envelope<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>body<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;[wsf-perl-service] soap body not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    soap_body_node <span style="color: #339933;">=</span> axiom_soap_body_get_base_node<span style="color: #009900;">&#40;</span>body<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>soap_body_node<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;[wsf-perl-service] soap body base node not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    om_node <span style="color: #339933;">=</span> axiom_node_get_first_child<span style="color: #009900;">&#40;</span>soap_body_node<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>om_node<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    axis2_char_t <span style="color: #339933;">*</span>embedding<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;-M'WSO2::WSF::C; WSO2::WSF::Service;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>svc_info<span style="color: #339933;">-&gt;</span>script_filename<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span>
                <span style="color: #ff0000;">&quot;perl function invocation failed, script_file name not found for &quot;</span>
                <span style="color: #ff0000;">&quot;service %s&quot;</span><span style="color: #339933;">,</span> svc_info<span style="color: #339933;">-&gt;</span>svc_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #808080; font-style: italic;">/* passing script real path into perl interpreter. */</span>
    embedding<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> svc_info<span style="color: #339933;">-&gt;</span>script_filename<span style="color: #339933;">;</span>
&nbsp;
    my_perl <span style="color: #339933;">=</span> perl_alloc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PL_perl_destruct_level <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/*    PL_use_safe_putenv = 1; */</span>
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    perl_construct<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    PL_origalen <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>perl_parse<span style="color: #009900;">&#40;</span>my_perl<span style="color: #339933;">,</span> xs_init<span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> embedding<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;perl_parse method failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    PL_exit_flags <span style="color: #339933;">|=</span> PERL_EXIT_DESTRUCT_END<span style="color: #339933;">;</span>
    <span style="color: #808080; font-style: italic;">/* perl_run(my_perl); */</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>SvTRUE<span style="color: #009900;">&#40;</span>ERRSV<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_DEBUG<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;invoke perl function failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    retstr <span style="color: #339933;">=</span> invoke_perl_function<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">,</span> om_node<span style="color: #339933;">,</span> function_name<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>retstr<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        node <span style="color: #339933;">=</span> wsf_util_deserialize_buffer<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">,</span> retstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    perl_destruct<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PERL_SET_CONTEXT<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    perl_free<span style="color: #009900;">&#40;</span>my_perl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PERL_SYS_TERM<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    AXIS2_LOG_DEBUG<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> axiom_node_to_string<span style="color: #009900;">&#40;</span>node<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> node<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> axis2_char_t <span style="color: #339933;">*</span>
invoke_perl_function<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> axutil_env_t <span style="color: #339933;">*</span>env<span style="color: #339933;">,</span> axiom_node_t <span style="color: #339933;">*</span>om_node<span style="color: #339933;">,</span>
        axis2_char_t <span style="color: #339933;">*</span>operation<span style="color: #339933;">,</span> axis2_char_t <span style="color: #339933;">*</span>class_name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    axis2_char_t <span style="color: #339933;">*</span>inmsg <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    SV <span style="color: #339933;">**</span>wsmsg_str <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* hold the value for the key 'payload' in WSMessage */</span>
    SV <span style="color: #339933;">*</span>wsmsg_ref <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* reference to a WSMessage object */</span>
    HV <span style="color: #339933;">*</span>wsmsg <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* WSMessage object */</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>tmp_str <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    axis2_char_t <span style="color: #339933;">*</span>res_payload_str <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>operation<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        AXIS2_LOG_DEBUG<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span>
                <span style="color: #ff0000;">&quot;invoking perl function failed, operation not available&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>om_node<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        inmsg <span style="color: #339933;">=</span> axiom_node_to_string<span style="color: #009900;">&#40;</span>om_node<span style="color: #339933;">,</span> env<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* declare and init a local copy of the Perl stack pointer */</span>
    dSP<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* mortal SVs for the stack */</span>
    ENTER<span style="color: #339933;">;</span>
    SAVETMPS<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* &quot;record&quot; the current stack pointer */</span>
    PUSHMARK<span style="color: #009900;">&#40;</span>SP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* push parameters to the stack */</span>
    XPUSHs<span style="color: #009900;">&#40;</span>sv_2mortal<span style="color: #009900;">&#40;</span>newSVpv<span style="color: #009900;">&#40;</span>inmsg<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* make the global copy of the stack pointer same as the local copy */</span>
    PUTBACK<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* call the Perl function, expecting a scalar to be returned */</span>
    count <span style="color: #339933;">=</span> call_pv<span style="color: #009900;">&#40;</span>operation<span style="color: #339933;">,</span> G_SCALAR<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* refreshing the local copy of the stack pointer, call_pv might have reallocated it */</span>
    SPAGAIN<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>count <span style="color: #339933;">!=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        croak<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;perl function invocation failed&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
        AXIS2_LOG_ERROR<span style="color: #009900;">&#40;</span>env<span style="color: #339933;">-&gt;</span>log<span style="color: #339933;">,</span> AXIS2_LOG_SI<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;perl function %s invocation failed&quot;</span><span style="color: #339933;">,</span> operation<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* get the scalar reference from the stack */</span>
    wsmsg_ref <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SV <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> POPs<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* getting the object from the reference */</span>
    wsmsg <span style="color: #339933;">=</span> SvRV<span style="color: #009900;">&#40;</span>wsmsg_ref<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* fetching the value of the member variable 'payload' from WSMessage */</span>
    wsmsg_str <span style="color: #339933;">=</span> hv_fetch<span style="color: #009900;">&#40;</span>wsmsg<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;payload&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> FALSE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* perl scalar to c string */</span>
    tmp_str <span style="color: #339933;">=</span> SvPVutf8_nolen<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>wsmsg_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    res_payload_str <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>axis2_char_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> savepvn<span style="color: #009900;">&#40;</span>tmp_str<span style="color: #339933;">,</span> strlen<span style="color: #009900;">&#40;</span>tmp_str<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    PUTBACK<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* cleaning up mortal SVs */</span>
    FREETMPS<span style="color: #339933;">;</span>
    LEAVE<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> res_payload_str<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href="http://wso2.org/repos/wso2/trunk/wsf/perl/service/wsf_xml_msg_recv.c">Full source is here</a> (note that the actual source might be different from what you see here since it&#8217;s being modified at the time of this writing).  Also <code>my_perl</code> is declared as a static variable.   The problem is when my_perl gets freed it looks like ModPerl is also affected.  A valgrind report shows that it tries to read from a pointer after <code>perl_free()</code> gets called.  As the following valgrind exerpt shows,</p>
<pre>
==16101== Invalid read of size 4
==16101==    at 0x62AD118: _wrap_wsf_worker_process_request (wsf_wrap.c:4167)
==16101==    by 0x5476D68: Perl_pp_entersub (pp_hot.c:2847)
==16101==    by 0x5438092: Perl_runops_debug (dump.c:1931)
==16101==    by 0x546FF97: Perl_call_sv (perl.c:2646)
==16101==    by 0x4DC2AF3: modperl_callback (modperl_callback.c:101)
==16101==    by 0x4DC388B: modperl_callback_run_handlers (modperl_callback.c:262)
==16101==    by 0x4DC4289: modperl_callback_per_dir (modperl_callback.c:369)
==16101==    by 0x4DBB53E: modperl_response_handler_run (mod_perl.c:1004)
==16101==    by 0x4DBB704: modperl_response_handler_cgi (mod_perl.c:1099)
==16101==    by 0x25A6C: ap_run_handler (config.c:158)
==16101==    by 0x295BE: ap_invoke_handler (config.c:372)
==16101==    by 0x35D80: ap_process_request (http_request.c:258)
==16101==  Address 0x649051c is 12 bytes inside a block of size 1,692 free'd
==16101==    at 0x480590A: free (vg_replace_malloc.c:323)
==16101==    by 0x546AED4: perl_free (perl.c:1394)
==16101==    by 0x62BD599: wsf_xml_msg_recv_invoke_business_logic_sync (wsf_xml_msg_recv.c:452)
==16101==    by 0x613D503: axis2_msg_recv_invoke_business_logic (msg_recv.c:392)
==16101==    by 0x613DB34: axis2_msg_recv_receive_impl (msg_recv.c:319)
==16101==    by 0x613D583: axis2_msg_recv_receive (msg_recv.c:431)
==16101==    by 0x61327D4: axis2_engine_receive (engine.c:315)
==16101==    by 0x6167177: axis2_http_transport_utils_process_http_post_request (http_transport_utils.c:595)
==16101==    by 0x62BE9B3: wsf_worker_process_request (wsf_worker.c:301)
==16101==    by 0x62AD0F7: _wrap_wsf_worker_process_request (wsf_wrap.c:4166)
==16101==    by 0x5476D68: Perl_pp_entersub (pp_hot.c:2847)
==16101==    by 0x5438092: Perl_runops_debug (dump.c:1931)
</pre>
<p>Still haven&#8217;t been able to pin down what exactly happens when <code>perl_free()</code> gets called <img src='http://engwar.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/40/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
