Apache 不想在 FIFO(管道)中进行 fopen 或 fwrite

Apache does not want to fopen or fwrite in FIFO (pipe)

我在设置 .cpp 文件和 .html 文件之间的正确通信时遇到了问题。

所以我有三个文件:一个是 .cpp 服务器,创建管道并在其上阻塞直到消息到来,.php 文件打开管道并向其中写入内容,以及一个 .html 文件,它只有一个按钮,运行打开脚本。

不寻常的是,当我 运行 我的服务器然后 运行 php 来自控制台的脚本时一切正常,但如果我想通过浏览器和 html 页面 - 管道无法打开。

Php 和 html 文件放在 /var/www/html/ 目录中,我的 cpp 文件在我的主文件夹中。

我正在尝试在 /tmp/ 文件夹中创建管道。

我尝试更改对这两个路径的 chmod 和 chown 权限(并且我知道 apache = www-data)但现在无济于事。我 100% 没有安装 SELinux。

非常感谢任何帮助,我已经尝试解决这个问题好几个小时了...

server.cpp

#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std; 

const char MSG_LENGTH = 9; 
string FIFO_1 = "/tmp/fifo"; 

int main() {

    if ( mkfifo(FIFO_1.c_str(), S_IFIFO | 0666 ) == -1 ) {
        cout << "Cannot create fifo" << endl; 
        return 1; 
    }

    cout << "Fifo created" << endl;

    int readfd = open(FIFO_1.c_str(), O_RDONLY); 
    if ( readfd < 0 ) {
        cout << "Cannot open fifo" << endl; 
        return 1; 
    }

    char buffer[1024]; 
    if ( read(readfd, buffer, MSG_LENGTH) < 1 ) {
        cout << "Cannot read from fifo" << endl; 
        return 1; 
    }
    buffer[MSG_LENGTH] = '[=10=]'; 

    cout << "Message: " << buffer << endl; 

    close(readfd); 
    unlink(FIFO_1.c_str()); 

    return 0; 
}

function.php

<?php

    $pipe_name = '/tmp/fifo'; 
    $msg = "message"; 

    $pipe = fopen($pipe_name, 'w'); 
    if ( $pipe == false) 
        echo "Cannot open fifo"; 

    if ( fwrite($pipe, $msg) == false ) 
        echo 'Cannot write to fifo';
    else 
        echo 'Can write to fifo'; 

?>

我的代码 运行:

#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std; 

string FIFO_1 = "/blah/fifo"; 

int main() {

    if ( mkfifo(FIFO_1.c_str(), S_IFIFO | 0666 ) == -1 ) {
        cout << "Cannot create fifo" << endl; 
//        return 1; 
    }
    else
    {
    cout << "Fifo created" << endl;
    }

    int readfd = open(FIFO_1.c_str(), O_WRONLY); 
    if ( readfd < 0 ) {
        cout << "Cannot open fifo" << endl; 
        return 1; 
    }

    int count = 0;
    for(;;)
    {
    count++;
    cout << "Sending message " << count << endl;
    time_t t = time(NULL);
    string msg = ctime(&t);
    if ( write(readfd, msg.c_str(), msg.length()) < 1 ) {
        cout << "Cannot write to fifo" << endl; 
        return 1; 
    }
    sleep(1);
    }

    close(readfd); 
//    unlink(FIFO_1.c_str()); 

    return 0; 
}

和 PHP 代码:

<?php

$pipe_name = '/blah/fifo'; 
$msg = "";

$pipe = fopen($pipe_name, 'r'); 
if ( $pipe == false) 
{
    echo "Cannot open fifo<br/>";
    $err = error_get_last();
    var_dump($err);
}
else
{   
    $count = 0;
    $data = stream_get_meta_data($pipe);
    var_dump($data);
    echo "<br/>";
    echo date("Y-M-d H:i:s") . "<br/>";
    echo "Waiting for message:";
    while(1)
    {
        $msg = fgets($pipe, 30);
        $err = error_get_last();
        if ($err)
        {
            echo "Err = "; var_dump($err); echo "<br/>";
        }
        else
        {
            echo "Got = " . $msg . "<br/>";
        }
        $count ++;
        if ($count > 20)
        {
            break;
        }
    }
}

echo "<br/>Done...";

?>