0

过去五个小时我一直忙于解决这个问题,所以我希望有人能帮助我。在我的 C++ 程序(我在 lubuntu 上的 QTcreator 中开发)中,我想在我的程序的子进程中运行 airodump-ng。airodump-ng 的输出应定向到父进程的 STDOUT。这适用于许多其他程序,但奇怪的是不适用于 airodump-ng。控制台中根本没有输出。这或者我的 Linux 崩溃,我被注销,当我重新登录时,我的所有程序都关闭了。有人知道为什么吗?

#include <QCoreApplication>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <sys/wait.h>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //execl("/usr/sbin/airodump-ng", "airodump-ng", (char*)0 );
    //dup2(1, 2); //pipe stderr to stdout

    pid_t pidAirodump;
    pid_t pidAircrack;
    int pip[2];

    if (pipe(pip) < 0) {
        perror("allocating pipe for child input redirect");
        return -1;
      }

    pidAirodump = fork();
    if(pidAirodump > 0)//parent
    {
        pidAircrack = fork();
        if(pidAircrack == 0)//pidAircrack
        {
            close(pip[0]);
            dup2(pip[1], 2);
            cout << "test" << endl;

            //execl("/usr/sbin/arp", "arp", (char*)0 );
             execl("/usr/sbin/airodump-ng", "airodump-ng ", "mon0", (char*)0 );
            exit(0);
        }
    }
    else//pidAirodump
    {
        exit(0);
    }
    wait(NULL);
    return a.exec();
}
4

1 回答 1

0

你的程序有一些奇怪的地方。但是让我们从这个问题开始——你应该区分execl不工作和你试图执行的程序行为不端。如果没有特殊权限,您不应该能够从用户空间程序中使 linux 崩溃。您发布的代码片段不应该这样做(做什么airpodump-ng是另一个问题)。

如果execl失败,它将返回并设置errno,我建议您在之后检查,execl而不是仅仅exiting。

现在来看看奇怪的东西:

第一个fork?你为什么这样做?你基本上forkexit马上就带孩子。你fork再次让父母wait- 这应该触发第一个孩子已经立即终止的事实。

pipe如果你不想把标准排除在外,你为什么要这样做?相反,您dup对管道的写入端标准错误,但您似乎对读取端没有做任何事情。

于 2015-10-06T10:42:12.747 回答