简体   繁体   English

php sftp seg fault

[英]php sftp seg fault

What i need to do if next code gives me seg fault ? 如果下一个代码给我seg错误,我需要做什么?

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

where $sftp is 其中$ sftp是

    $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. 和segfault是我只使用readdir函数。

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. 事实证明,自PHP 5.6以来,函数ssh2_sftp的行为发生了变化,现在返回的资源不允许作为字符串连接形成文件系统路径。

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: 因此,一旦你返回ssh2_sftp ,你首先要通过intval传递它,以便构建远程文件路径:

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

You are welcome. 别客气。

I had the same problem and I solved it upgrading the ssh2 pecl packaage to version 0.11.3 我有同样的问题,我解决了它将ssh2 pecl packaage升级到版本0.11.3

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

It worked for me after some problems. 在遇到一些问题后,它对我有用。

Regards 问候

A seg(mentation) fault is an internal error in php or the SSH/SFTP extension. seg(mentation)错误是php或SSH / SFTP扩展中的内部错误。 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: 由于这可能只能与您的ssh客户端和服务器的特定组合重现,您应该首先在全新的VM中重现该错误并记录您所做的每一步,如下所示:

  1. Install Ubuntu 11.04 amd64 in the VM 在VM中安装Ubuntu 11.04 amd64
  2. Install ssh with $ sudo apt-get install ssh 使用$ sudo apt-get install ssh
  3. Configure ssh with ... 使用ssh配置ssh
  4. Install php with $ sudo apt-get install php5-cli 使用$ sudo apt-get install php5-cli安装php
  5. Copy the script [link to http://pastebin.com/ here] to the VM 将脚本[链接到http://pastebin.com/ here]复制到VM
  6. Type php ssh-segfault.php and receive a segfault. 输入php ssh-segfault.php并收到段错误。

不适用于OP的情况,但如果您未能在opendir创建的句柄上显式调用closedir,则在脚本结束时的PHP内部清理期间也会出现段错误。

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. 导致第二次ssh2_auth_pubkey_file调用中的身份验证错误。 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. 最后,我将其缩小到包含一个文件,该文件定义了一个使用__FILE__常量的函数,将错误更改为分段错误。

Even a file as simple as this: 即使是像这样简单的文件:

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

I hope this helps somebody else at some point... 我希望这在某些方面可以帮助别人......

One of the answers above suggests you need to cast the resource to an int intval($sftp) to avoid the segfault. 上面的答案之一表明您需要将资源转换为int intval($sftp)以避免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 : 虽然我相信这曾经是这种情况,但现在似乎已经倒置了,至少在php 7.1.9上并且转换为int现在会导致segfault,至少使用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 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 ssh2.sftp://资源ID#2675 / var / file.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM