2

What i need to do if next code gives me seg fault ?

    $handle = opendir("ssh2.sftp://$sftp".'/usr/bin/');
    $file = readdir($handle);
    closedir($handle);

where $sftp is

    $this->_connection = ssh2_connect($this->_server, 22);
    if($this->_authType==ExportInterface::CONN_AUTH_KEY) {
        ssh2_auth_pubkey_file($this->_connection,$this->_user,$this->_key,$this->_privateKey);
    } else {
        ssh2_auth_password($this->_connection,$this->_user,$this->_password);
    }
    $sftp = ssh2_sftp($this->_connection);

Connect work well. and segfault is when only i use readdir function.

RusAlex
  • 8,245
  • 6
  • 36
  • 44
  • Can you isolate that in a test-script and run it through [gdb](http://stackoverflow.com/questions/6343674/phpunit-segmentation-fault/6344061#6344061)? – hakre Sep 14 '11 at 09:40
  • By a long way, the most common cause of segfaults in PHP in my experience is a recursive function that recurses indefinitely. – DaveRandom Sep 14 '11 at 09:48
  • @DaveRandom That shouldn't happen with suhosin installed. But the other likely cause for a segfault is a rarely tested native module, like ... ssh/sftp ;) – phihag Sep 14 '11 at 10:51

6 Answers6

4

I'm answering this old question due to high ranking on Google search. I was experiencing the same segmentation fault and the real solution wasn't here.

It turns out that since PHP 5.6, the behavior of the function ssh2_sftp changed and is now returning a resource that doesn't allow to be concatenated as a string to form filesystem paths.

So, once you have the return of the ssh2_sftp, you have first to pass it through intval in order to build the remote file path:

$sftp = ssh2_sftp($this->_connection);
$handle = opendir('ssh2.sftp://' . intval($sftp) . '/usr/bin/');
$file = readdir($handle);
closedir($handle);

You are welcome.

Victor Schröder
  • 6,738
  • 2
  • 42
  • 45
3

I had the same problem and I solved it upgrading the ssh2 pecl packaage to version 0.11.3

sudo pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3

It worked for me after some problems.

Regards

plopesc
  • 91
  • 3
2

Not applicable to the OP's situation, but if you fail to call closedir explicitly on the handle created by opendir, you'll also get a segfault during PHP internal cleanup at script end.

roktechie
  • 1,345
  • 1
  • 10
  • 15
2

A seg(mentation) fault is an internal error in php or the SSH/SFTP extension. You should file a bug against the extension in question.

Don't forget to include a short, reproducible example. Since this may be only reproducible with your specific combination of ssh client and server, you should reproduce the bug first in a brand-new VM and record every step you make, like this:

  1. Install Ubuntu 11.04 amd64 in the VM
  2. Install ssh with $ sudo apt-get install ssh
  3. Configure ssh with ...
  4. Install php with $ sudo apt-get install php5-cli
  5. Copy the script [link to http://pastebin.com/ here] to the VM
  6. Type php ssh-segfault.php and receive a segfault.
phihag
  • 278,196
  • 72
  • 453
  • 469
1

I had a similar problem, though with some additional factors. I'll start with the solution: use absolute paths, rather than relative. And avoid the ~.

As for my findings. Take this script:

#!/usr/bin/php
<?php

$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');
unset($res); unset($ssh);

$ssh = ssh2_connect('hostname.example.com');
$res = ssh2_auth_pubkey_file($ssh, 'user', '~/.ssh/various-keys/special-key_2015.pub', '~/.ssh/various-keys/special-key_2015');

Results in an authentication error in the 2nd ssh2_auth_pubkey_file call. A bit strange to encounter, after two identical calls - but easy enough to analyze.

But having the rest of my application code around it would result in the segfault. Eventually I narrowed it down to that that including a file, that defines a function that uses the __FILE__ constant changes the error to turn into a segmentation fault.

Even a file as simple as this:

<?php
function nothing() { 
    error_log(__FILE__);
}

I hope this helps somebody else at some point...

Wouter van Vliet
  • 1,484
  • 11
  • 20
0

One of the answers above suggests you need to cast the resource to an int intval($sftp) to avoid the segfault.

While I believe this used to be the case, this now appears to have inverted, at least on php 7.1.9 and casting to an int now causes the segfault, at least with file_put_contents:

$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . intval($sftp) . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');  

ssh2.sftp://2675/var/file.text

Segmentation Fault

Without a cast it works:

$connection = ssh2_connect($host, $port);
$sftp = ssh2_sftp($connection);
$remoteFile = 'ssh2.sftp://' . $sftp . '/var/file.text'
echo $remoteFile
file_put_contents($remoteFile, 'Content');  

ssh2.sftp://Resource id #2675/var/file.text

Peter Ward
  • 23
  • 2