Jump to content

Recommended Posts

I am writing an import script and it is failing miserably. I am using the "standard" installation of PHP Version 5.2.6-2ubuntu4.1 that comes with Ubuntu 8.10. Basically after around 200 records the script dies and I get this in my apache error logs:

 

[Mon Mar 30 10:26:31 2009] [notice] child pid 6509 exit signal Segmentation fault (11)

 

I have read that using mpm-prefork in apache helps this but this is already installed by default. I am at my wits end. Even my super programmer buddy can't figure this one out. I think the issue is with the code because it always dies in the same function, different sections of the function, but always the same function. Anyone have any ideas on how to start fixing this problem? Most google searches on this deal with php4, very little is said about php5 and the only thing about 5 they mention is to use mpm-prefork. I haven't tried switching to worker because I am using the php imap module to get the emails to parse them and it says its not thread safe. I have turned off all mysql calls in the script and am only testing the parser and it still seg faults. Any help is most appreciated, I have been debugging this for days with no success. Help me phpfreaks, you're my only hope.

Link to comment
https://forums.phpfreaks.com/topic/151748-solved-php5-segmentation-fault-11/
Share on other sites

Probably your script causing this. Check the CPU usage on your server when its running.

I would guess your draining RAM. Only seen this once before with a script that tried to process too much data.

 

You need to post the code.

I've been looking at top(linux system monitor) while this runs and apache never gets above 4% of CPU and 1% of memory. Xorg uses the most CPU and firefox the most memory. So it doesn't seem to be effecting it that much, but I don't know to much about low level stuff like that. The code is below, in test.php I have tried it with and without the unset($regParser); line and it does not make a difference. The code always dies in the findGood function. It has died in any part of the switch statement, it doesn't matter which section.

 

test.php

<?php
// include proper class files
include("constants.php");
include("regParser.php");

print "<h1>Registration</h1>";

// connect to registration server and get new emails
$mbox = imap_open($server, $reg_username, $reg_password) or die("Could not connect");

// parse all unseen messags
$MC = imap_check($mbox);

// fetch an overview for all messages in INBOX
//$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
$result = imap_fetch_overview($mbox,"1:500",0);

// keep track of how many registartions were inserted
$total = 1;

foreach ($result as $overview) {
if($overview->seen==0) {
	$body = imap_body($mbox, $overview->msgno, FT_PEEK);

	// check encoding of email, fix if necessary
	$tmp = imap_fetchstructure($mbox, $overview->msgno);
	if($tmp->type!=0) {
		echo "\n\n<br><br>#$total: ".date('h:i:s')." ";
		echo "Email {$overview->subject} was marked as spam<br><br>";
	} else {
		echo "\n\n<br><br>#$total: ".date('h:i:s')." ";
		echo "Email {$overview->subject} was imported succesfully<br><br>";

		if($tmp->encoding==4) $body = imap_qprint($body); 

		$regParser = new regParser();

		list($registrations, $product, $displays, $registration_codes) = $regParser->parseEmail($header, $body, $overview);

		unset($regParser);
	}

	flush();

	$total++;
}

}

echo "A total of $total registrations were inserted.";

// close mail box
imap_close($mbox);

?>

 

regParser:

<?php
class regParser 
{
var $messageBody = array(); // holds the message body

var $onlyTheGoodStuff = array(); // holds the parsed information
var $onlyTheProduct = array(); // holds the product information
var $onlyTheBadStuff = array(); // holds the information we don't want for testing reasons
var $onlyDisplays = array(); // holds the display information
var $onlyRegistrationCodes = array(); // holds the registration code information

var $afterLicenseeFlag = false; // lets us know when we passed the 'Licensee' field

// array of all known NULL fields
var $nullArray = array("", " ", "null", "(null)");

// array of all fields we want to keep
var $knownGood = array(
	"license_type" => array("value" => "License Type", "label" => TRUE),
	"licensee" => array("value" => "Licensee", "label" => TRUE),
	"organization" => array("value" => "Organization", "label" => TRUE),
	"registration_code" => array("value" => "Registration code", "label" => TRUE),
	"upgrade_code" => array("value" => "Upgrade code", "label" => TRUE),
	"fx_factory" => array("value" => "FxFactory", "label" => FALSE),
	"mac_os" => array("value" => "Mac OS", "label" => FALSE),
	"final_cut_pro" => array("value" => "Final Cut Pro", "label" => FALSE),
	"motion" => array("value" => "Motion", "label" => FALSE),
	"final_cut_express" => array("value" => "Final Cut Express", "label" => FALSE),
	"after_effects" => array("value" => "After Effects", "label" => FALSE),
	"model" => array("value" => "Model", "label" => TRUE),
	"display" => array("value" => "Display", "label" => TRUE),
	"contact" => array("value" => "Wants to be contacted", "label" => TRUE),
	"email" => array("value" => "Preferred email", "label" => TRUE),
	"address" => array("value" => "Street", "label" => TRUE),
	"city" => array("value" => "City", "label" => TRUE),
	"state" => array("value" => "State", "label" => TRUE),
	"zip" => array("value" => "Zip", "label" => TRUE),
	"country" => array("value" => "Country", "label" => TRUE),
	"phone" => array("value" => "Phone", "label" => TRUE));

// array of all fields we do not want to parse
var $knownBad = array(
	"Below is the result of your feedback form.",
	"It was submitted by",
	"message:",
	"--------------------",
	"Machine ID:",
	"Full name:");

function parseEmail($header, $body, $overview) {
	// get rid of all blank lines
	$body = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $body);

	// get rid of excess spaces and tabs
	$body = ereg_replace("[ \t]+", " ", $body);

	// print full email for testing reasons
	//echo str_replace("\n","<br>",$body)."<br><br>";

	$header = $this->getHeader($overview);

	$body = utf8_encode($body);
	$body = htmlspecialchars($body, ENT_QUOTES, 'UTF-8');
	$body = utf8_decode($body);

	// put each message line in the array and pop off the end empty block
	$this->messageBody = explode("\n", $body); 
	array_pop($this->messageBody);

	// save the entire email
	$this->onlyTheGoodStuff['full_email'] = $header.str_replace("\n","<br>",$body);

	// find the bad values and remove them from array 
	array_walk($this->messageBody, array(&$this, 'findBad'));

	// find the good values and remove them from array
	array_walk($this->messageBody, array(&$this, 'findGood'));

	// only the product should be left
	$this->onlyTheProduct['product'] = array_shift($this->messageBody);

	// mark possible spam messages
	$this->onlyTheGoodStuff['spam'] = (empty($this->messageBody)) ? 0 : 1; 

	// print the arrays for testing reasons
	//foreach( $this->onlyTheGoodStuff as $key => $value ) { echo "$key: $value<br><br>"; }
	//foreach( $this->onlyTheProduct as $key => $value ) { echo "$key: $value<br><br>"; }
	//foreach( $this->onlyDisplays as $key => $value ) { echo "$key: $value<br><br>"; }
	//foreach( $this->onlyRegistrationCodes as $key => $value ) { echo "$key: {$value[0]}:{$value[1]}<br><br>"; }

	return array($this->onlyTheGoodStuff, $this->onlyTheProduct, $this->onlyDisplays, $this->onlyRegistrationCodes);
}

function getHeader($overview) {
	$tmp = array('Date' => $overview->date, 
		'From' => $overview->from, 
		'To' => $overview->to, 
		'Subject' => $overview->subject);

	$header = "";
	foreach( $tmp as $key => $value )
	{
		$value = utf8_encode($value);
		$value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
		$value = utf8_decode($value);

		$header .= "$key: $value<br>";
	}

	return $header."<br>";
}

function findGood($message_value, $message_key) {
	// check if the line is blank
	//if(trim($message_value)=="") { unset($this->messageBody[$message_key]); return; }

	// check value against current known good array
	foreach( $this->knownGood as $known_key => $known_value ) {
		if(substr($message_value, 0, strlen($known_value['value']))==$known_value['value']) {
			switch($known_value['value'])
			{
			case 'Display' : 
				if(!$this->isNull($message_value))
					$this->onlyDisplays[] = $this->getValue($message_value);
				break;

			case 'Registration code' :
				if(!$this->isNull($message_value))
					$this->onlyRegistrationCodes[] = array("type" => "REGISTRATION", "code" => $this->getValue($message_value));
				break;

			case 'Upgrade code' :
				if(!$this->isNull($message_value))
					$this->onlyRegistrationCodes[] = array("type" => "UPGRADE", "code" => $this->getValue($message_value));
				break;

			case 'Licensee' :
				if(!$this->isNull($message_value))
					$this->onlyTheGoodStuff[$known_key] = $this->getValue($message_value); 
				$this->afterLicenseeFlag = true;
				break;

			case 'FxFactory' :
				if(!$this->afterLicenseeFlag)
					return;
				$this->onlyTheGoodStuff[$known_key] = $message_value;
				break;

			case 'Street' :
				if(!$this->isNull($message_value))
					$this->onlyTheGoodStuff[$known_key] = $this->getValue($message_value); 

				// this covers all addresses that have multiple lines
				$x = 1;
				while(substr($this->messageBody[$message_key + $x], 0, 5)!="City:") {
					$this->onlyTheGoodStuff[$known_key] .= ", ".$this->messageBody[$message_key + $x];
					unset($this->messageBody[$message_key + $x]);
					$x++;
				}

				break;

			default : 
				if($known_value['label']) {
					if(!$this->isNull($message_value))
						$this->onlyTheGoodStuff[$known_key] = $this->getValue($message_value); 
				} else {
					$this->onlyTheGoodStuff[$known_key] = $message_value;
				}
				break;
			}

			// unset the item that was just checked
			unset($this->messageBody[$message_key]);
		}
	}	
}

function findBad($message_value, $message_key) {
	foreach( $this->knownBad as $known_key => $known_value ) {
		if(substr($message_value, 0, strlen($known_value))==$known_value) {
			// unset the items we don't need
			unset($this->messageBody[$message_key]);
		}
	}	
}

function getValue($string) {
	$tmp = substr(strstr($string, ":"), 2);
	//$tmp = htmlspecialchars($tmp, ENT_QUOTES);
	//$tmp = htmlspecialchars($tmp, ENT_QUOTES, 'UTF-8');
	return trim($tmp);
}

function isNull($string) {
	$tmp = $this->getValue($string);
	if(in_array($tmp, $this->nullArray)) return true;
	return false;
}

}

https://bugs.launchpad.net/ubuntu/+source/php5/+bug/343870

 

Only option is comment internals from regParser() class - see if script runs. May need to post a bug http://bugs.php.net/

Unfamiliar with this distro release. Could also be apache.

Maybe I read it wrong, but I don't think that bug applies because I'm not using mysql at all. It would be more of an imap issue then mysql. I'll keep looking, let me know if anybody might have an idea. Anything, even if it's an extremely remote possibility or if you just have a hunch, please let me know. Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.